环境:Python 3.8.8 64-bit; VS Code; tensorflow 2.0
数据集:https://momodel.cn/explore/60e6b9babb5ba072ad72755d?type=dataset
文件结构:
使用GPU加速训练:
然而我的输出为0,只能用CPU,寄!
- 卷积层和池化层的区别?
略。
卷积层主要提取图像特征(F不宜过大,否则参数数量过多),池化层用来缩放图像。
- 卷积核的深度和个数?
train.py
from keras import layers, models, utils, optimizers
from keras.preprocessing.image import ImageDataGenerator
import tensorflow as tf
import os
def define_cnn_model():
model = models.Sequential()
# 卷积层
model.add(layers.Conv2D(32, (3, 3), activation='relu', padding='same', input_shape=(200, 200, 3)))
# 最大池化层
model.add(layers.MaxPooling2D((2, 2)))
# Flatten层
model.add(layers.Flatten())
# 全连接层
model.add(layers.Dense(128, activation='relu'))
model.add(layers.Dense(1, activation='sigmoid'))
# 编译模型
opt = optimizers.SGD(lr=0.03, momentum=0.9)
model.compile(
optimizer = opt,
loss = 'binary_crossentropy',
metrics = ['accuracy']
)
return model
def train_cnn_model():
model = define_cnn_model()
# 创建图片生成器
datagen = ImageDataGenerator(rescale = 1.0 / 255.0)
train_it = datagen.flow_from_directory(
"ma1ogo3ushu4ju4ji2/dogs_cats/data/train",
class_mode = 'binary',
batch_size = 64,
target_size = (200, 200)
)
model.fit_generator(train_it, steps_per_epoch=len(train_it), epochs=30, verbose=1)
return model
print('文件当前路径:', os.getcwd())
print('开始训练')
model = train_cnn_model()
print('训练结束')
print('开始存储模型')
model.save('ma1ogo3ushu4ju4ji2/myModel.h5')
print('模型存储完毕')
test.py
import keras
import os, random
import matplotlib
from matplotlib.pyplot import imshow
import numpy as np
from PIL import Image
from keras.preprocessing import image
def read_random_image():
folder = r"ma1ogo3ushu4ju4ji2/dogs_cats/data/test/"
file_path = folder + random.choice(os.listdir(folder))
pil_im = Image.open(file_path, 'r')
return pil_im
def get_predict(pil_im, model):
# 图片缩放
pil_im = pil_im.resize((200, 200))
# 将图片转为 numpy array 格式
array_im = image.img_to_array(pil_im)
array_im = np.expand_dims(pil_im, axis=0)
# 对图片进行预测
result = model.predict([[array_im]])
if result[0][0] > 0.5:
print('预测结果:狗')
else:
print('预测结果:猫')
# model = keras.models.load_model('ma1ogo3ushu4ju4ji2/myModel.h5')
model = keras.models.load_model("ma1ogo3ushu4ju4ji2/dogs_cats/model/basic_cnn_model.h5")
pil_im = read_random_image()
pil_im.show()
#imshow(image.img_to_array(pil_im))
get_predict(pil_im, model)
运行结果:
我的评价是:感觉不如瞎猜……准确度。
当然可以通过改进模型或者增大epochs来尝试提高accuracy,不过我没有GPU,就不折腾了,训练时间实在是太长了,代码一会上传到Github,大家有兴趣可以自己尝试。