卷积神经网络被广泛应用于物体检测与分类,本帖从零开始搭建一套基于Keras的猫狗分类卷积网络入门贴。
一、下载猫狗数据集
下面是我在网络上找到的一个猫狗的数据集,如果不能使用可以在本帖下留言
链接:https://pan.baidu.com/s/13hw4LK8ihR6-6-8mpjLKDA 密码:dmp4
二、使用数据集
下载的数据集分为猫狗两类图片,其命名分别以cat、dog命名,我们需要读取文件名信息,将之设置为标签label,便于后续的训练。 下面程序除生成label外,还存在部分代码将图像与标签写入npy文件(方便我更换电脑继续使用训练,免得每次拷贝那么多文件)
def img_to_npy():
train_idx = 0
npy_idx = 0
path = './/Dataset//kaggle_cats_dogs//train6000/' #小批量数据集共6000张猫狗的图片
files = os.listdir(path)
print("shuffle ing..." )
random.shuffle(files) #打乱数据集图片顺序
images = []
labels = []
print("npy generating ..." )
for f in files:
train_idx = train_idx +1
img_path = path + f
img = image.load_img(img_path, target_size=image_size) #image_size = (150, 150)
img_array = image.img_to_array(img)
images.append(img_array)
if 'cat' in f:
labels.append(0)
else:
labels.append(1)
#***************************************************************************
if train_idx % npy_size == 0 and train_idx or train_idx == len(files):
print("train_idx = " + str(train_idx) +";total = " + str(len(files)))
data = np.array(images)
labels = np.array(labels)
labels = np_utils.to_categorical(labels, 2)
np.save('./Dataset/data_{}.npy'.format(npy_idx), data)
np.save('./Dataset/labels_{}.npy'.format(npy_idx), labels)
images = []
labels = []
npy_idx = npy_idx + 1
#***************************************************************************
print("preprocessing training data done." )
return data, labels
三、模型网络的搭建
共四层卷积层、与两个全连接层,二分类
model = Sequential()
model.add(Conv2D(32, kernel_size=(5, 5), input_shape=(img_h, img_h, 3), activation='relu', padding='same'))
model.add(MaxPool2D())
model.add(Dropout(0.3))
model.add(Conv2D(64, kernel_size=(5, 5), activation='relu', padding='same'))
model.add(MaxPool2D())
model.add(Dropout(0.3))
model.add(Conv2D(128, kernel_size=(5, 5), activation='relu', padding='same'))
model.add(MaxPool2D())
model.add(Dropout(0.5))
model.add(Conv2D(256, kernel_size=(5, 5), activation='relu', padding='same'))
model.add(MaxPool2D())
model.add(Dropout(0.5))
model.add(Flatten())
model.add(Dense(512, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(2, activation='softmax'))
model.summary()
四、训练
我使用自生成的npy文件、(可直接使用归类好的数据集)
sgd = Adam(lr=0.0003)
model.compile(loss='binary_crossentropy',optimizer=sgd, metrics=['accuracy'])
"""加载数据"""
print("img to npy......")
# img2npy()
"""Tensorboad"""
tbCallbacks = callbacks.TensorBoard(log_dir='./logs', histogram_freq=1, write_graph=True, write_images=True)
print("train.......")
for npy_idx_tmp in range(0, 3):
print('==============npy_idx_tmp:', npy_idx_tmp)
images = []
labels = []
images = np.load("./Dataset/data_{}.npy".format(npy_idx_tmp))
labels = np.load("./Dataset/labels_{}.npy".format(npy_idx_tmp))
# labels = np_utils.to_categorical(labels, 2)
images /= 255
x_train, x_test, y_train, y_test = train_test_split(images, labels, test_size=0.2) #随机划分数据集
print(x_train.shape, y_train.shape)
model.fit(x_train, y_train, batch_size=nbatch_size, epochs=nepochs, verbose=1, validation_data=(x_test, y_test),
callbacks=[tbCallbacks])
print("evaluate......")
scroe, accuracy = model.evaluate(x_test, y_test, batch_size=nbatch_size)
print('scroe:', scroe, 'accuracy:', accuracy)
yaml_string = model.to_yaml()
with open('./models/cat_dog.yaml', 'w') as outfile:
outfile.write(yaml_string)
model.save_weights('./models/cat_dog.h5')
五、预测
def pred_data():
with open('./models/cat_dog.yaml') as yamlfile:
loaded_model_yaml = yamlfile.read()
model = model_from_yaml(loaded_model_yaml)
model.load_weights('./models/cat_dog.h5')
sgd = Adam(lr=0.0003)
model.compile(loss='categorical_crossentropy',optimizer=sgd, metrics=['accuracy'])
images = []
path='./test_images/'
for f in os.listdir(path):
img = image.load_img(path + f, target_size=image_size)
img_array = image.img_to_array(img)
x = np.expand_dims(img_array, axis=0)
x = preprocess_input(x)
result = model.predict_classes(x,verbose=0)
print(f,result[0])
完整代码:https://download.csdn.net/download/harrison509/11122538