使用 CNN 进行图像分类 - 理解计算机视觉
卷积神经网络
1) 步幅卷积
2) 填充
3) 池化层
4) 上采样层
5) 了解尺寸
((n-f+2p)/s) + 1
n 是输入的大小,如果你有一个 32x32x3 的图像,那么 n 将为 32。 f 是过滤器的大小,如果过滤器的大小是 3×3,那么 f 将是 3。 p 是填充。 s 是你要滑动的系数
图像分类
二元分类:在这种类型的分类中,我们的输出是二进制值 0 或 1,让我们举个例子,给你一张猫的图像,你必须检测图像是猫的还是非猫的。 多类分类:在这种类型的分类中,我们的输出将是多类的,让我们举个例子,给你一个图像,你必须在 37 个类中检测狗的品种。
使用 CNN 构建猫狗图像分类器
步骤1)导入必要的库
import pandas as pd
import os
from pathlib import Path
import glob
import seaborn as sns
import matplotlib.pyplot as plt
import tensorflow as tf
from tensorflow.keras import layers
from tensorflow.keras import Model
from tensorflow.keras.optimizers import RMSprop
from keras_preprocessing.image import ImageDataGenerator
步骤2) 加载数据和基本 EDA
test_dir = data_dir / 'test_set/test_set'
dog_samples_dir_train = train_dir / 'dogs' # directory for dogs images
'''
Function for making a dataframe that contains images path as well as their labels.
Parameters:-
- CATS_PATH - Path for Cats Images
- DOGS_PATH - Path for Dogs Images
Output:-
It simply returns dataframe
'''
cat_images = CATS_PATH.glob('*.jpg')
dog_images = DOGS_PATH.glob('*.jpg')
df = []
for i in cat_images:
df.append((i, 0)) # appending cat images as 0
for j in dog_images:
df.append((i, 1)) # appending dog images as 0
df = pd.DataFrame(df, columns=['image_path', 'label'], index = None) # converting into dataframe
df = df.sample(frac = 1).reset_index(drop=True)
return df
train_csv = make_csv_with_image_labels(cat_samples_dir_train,dog_samples_dir_train)
train_csv.head()
len_dog = len(train_csv['label'][train_csv.label == 1])
arr = np.array([len_cat , len_dog])
labels = ['CAT', 'DOG']
print('Total No. Of CAT Samples :- ', len_cat)
print('Total No. Of DOG Samples :- ', len_dog)
plt.pie(arr, labels=labels, explode = [0.2,0.0] , shadow=True)
plt.show()
步骤3)准备训练和测试数据
'''
Function for preparing training data
'''
train_datagen = ImageDataGenerator(rescale = 1./255., # normalizing the image
rotation_range = 40,
width_shift_range = 0.2,
height_shift_range = 0.2,
shear_range = 0.2,
zoom_range = 0.2,
horizontal_flip = True)
train_generator = train_datagen.flow_from_directory(train_dir,
batch_size = batch_size,
color_mode='rgb',
class_mode = 'binary',
target_size = target_size)
return train_generator
train_generator = get_train_generator(train_dir)
'''
Function for preparing testing data
'''
test_datagen = ImageDataGenerator( rescale = 1.0/255. )
test_generator = test_datagen.flow_from_directory(test_dir,
batch_size = batch_size,
color_mode='rgb',
class_mode = 'binary',
target_size = target_size)
return test_generator
test_generator = get_testgenerator(test_dir)
步骤4)构建模型
layers.Conv2D(64, (3,3), strides=(2,2),padding='same',input_shape= (224,224,3),activation = 'relu'),
layers.MaxPool2D(2,2),
layers.Conv2D(128, (3,3), strides=(2,2),padding='same',activation = 'relu'),
layers.MaxPool2D(2,2),
layers.Conv2D(256, (3,3), strides=(2,2),padding='same',activation = 'relu'),
layers.MaxPool2D(2,2),
layers.Flatten(),
layers.Dense(158, activation ='relu'),
layers.Dense(256, activation = 'relu'),
layers.Dense(128, activation = 'relu'),
layers.Dense(1, activation = 'sigmoid'),
])
model.summary()
步骤5)编译和训练模型
epochs=15,
verbose=1,
validation_data=test_generator)
import matplotlib.pyplot as plt
acc=history.history['acc']
val_acc=history.history['val_acc']
loss=history.history['loss']
val_loss=history.history['val_loss']
epochs=range(len(acc))
plt.plot(epochs, acc, 'r', 'Training Accuracy')
plt.plot(epochs, val_acc, 'b', 'Validation Accuracy')
plt.title('Training and validation accuracy')
plt.figure()
plt.plot(epochs, loss, 'r', 'Training Loss')
plt.plot(epochs, val_loss, 'b', 'Validation Loss')
plt.title('Training and validation loss')
new_model = tf.keras.models.load_model('./my_model.h5') # loading the trained model