import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' # 不显示等级2以下的提示信息
import zipfile
# import random
# import tensorflow as tf
# from tensorflow.keras.optimizers import RMSprop
# from tensorflow.keras.preprocessing.image import ImageDataGenerator
# from shutil import copyfile
local_zip = 'tmp/cats-and-dogs.zip' # 数据集压缩包路径
zip_ref = zipfile.ZipFile(local_zip, 'r') # 打开压缩包,以读取方式
zip_ref.extractall('E:/Python/pythonProject_1/tmp') # 解压到以下路径
zip_ref.close()
print(len(os.listdir('tmp/cats-and-dogs/PetImages/Cat/'))) # 读取出解压文件夹“Cat”文件夹下面文件数量
print(len(os.listdir('tmp/cats-and-dogs/PetImages/Dog/'))) # 读取出解压文件夹“Dog”文件夹下面文件数量
try:
os.mkdir('/tmp/cats-and-dogs') # 创建虚拟路径名称'/tmp/cats-and-dogs'
os.mkdir('/tmp/cats-and-dogs/training') # 创建虚拟路径名称'/tmp/cats-and-dogs/training'
os.mkdir('/tmp/cats-and-dogs/testing') # 创建虚拟路径名称'/tmp/cats-and-dogs/testing'
os.mkdir('/tmp/cats-and-dogs/training/cats') # 创建虚拟路径名称'/tmp/cats-and-dogs/training/cats'
os.mkdir('/tmp/cats-and-dogs/training/dogs') # 创建虚拟路径名称'/tmp/cats-and-dogs/training/dogs'
os.mkdir('/tmp/cats-and-dogs/testing/cats') # 创建虚拟路径名称'/tmp/cats-and-dogs/testing/cats'
os.mkdir('/tmp/cats-and-dogs/testing/dogs') # 创建虚拟路径名称'/tmp/cats-and-dogs/testing/dogs'
except OSError:
pass
model_training_fit流程图:
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' # 不显示等级2以下的提示信息
# import shutil
# import random
import tensorflow as tf
# from tensorflow import keras
from tensorflow.keras.optimizers import RMSprop
from tensorflow.keras.preprocessing.image import ImageDataGenerator
# from shutil import copyfile
# import matplotlib.image as mpimg
import matplotlib.pyplot as plt
#======== 数据预处理 =========
TRAINING_DIR = "E:/Python/pythonProject_1/cats_vs_dogs/tmp/cats-and-dogs/training/"
train_datagen = ImageDataGenerator(rescale = 1/255) # 归一化,减少计算量
train_generator = train_datagen.flow_from_directory(TRAINING_DIR, batch_size = 100, class_mode = 'binary', target_size = (150,150))
VALIDATION_DIR = "E:/Python/pythonProject_1/cats_vs_dogs/tmp/cats-and-dogs/testing/"
validation_datagen = ImageDataGenerator(rescale = 1/255)
validation_generator = validation_datagen.flow_from_directory(VALIDATION_DIR, batch_size = 100, class_mode = 'binary', target_size = (150,150))
# Expected Output :
# Found 22498 images belonging to 2 classes.
# Found 2500 images belonging to 2 classes.
#======== 模型构建 =========
model = tf.keras.models.Sequential([
tf.keras.layers.Conv2D(16, (3, 3), activation = 'relu', input_shape = (150, 150, 3)), # 输入参数:过滤器数量,过滤器尺寸,激活函数:relu, 输入图像尺寸
tf.keras.layers.MaxPooling2D(2, 2), # 池化:增强特征
tf.keras.layers.Conv2D(32, (3, 3), activation = 'relu'), # 输入参数:过滤器数量、过滤器尺寸、激活函数:relu
tf.keras.layers.MaxPooling2D(2, 2),
tf.keras.layers.Conv2D(64, (3, 3), activation = 'relu'),
tf.keras.layers.MaxPooling2D(2, 2),
tf.keras.layers.Flatten(), # 输入层
tf.keras.layers.Dense(512, activation = 'relu'), # 神经元数量:512 ,激活函数:relu
tf.keras.layers.Dense(1, activation = 'sigmoid')
])
#======== 模型参数编译 =========
model.compile(optimizer = RMSprop(lr = 0.001), loss = 'binary_crossentropy', metrics = ['accuracy'])
# 输入参数:lr:学习步长 RMSprop算法:修正学习步长 loss:损失函数 metrics:网络性能度量
#======== 模型训练 =========
# Note that this may take some time.
history = model.fit_generator(train_generator, epochs = 2, verbose = 1, validation_data = validation_generator) # verbose=1 表示记录网络拟合过程
model.save('E:/Python/pythonProject_1/cats_vs_dogs/model.h5') # model 保存
#-----------------------------------------------------------
# Retrieve a list of list result on training and test data
# set for each training epoch
#-----------------------------------------------------------
acc = history.history['accuracy']
val_acc = history.history['val_accuracy']
loss = history.history['loss']
val_loss = history.history['val_loss']
epochs = range(len(acc)) # Get number of epochs
#-----------------------------------------------------------
# Plot training and validation accuracy per epoch
#-----------------------------------------------------------
plt.plot(epochs, acc, 'r', "train_acc")
plt.plot(epochs ,val_acc, 'b', "valida_acc")
plt.title("training and validation accuracy")
plt.figure()
plt.show()
# 曲线呈直线是因为epochs/轮次太少
#-----------------------------------------------------------
# Plot training and validation loss per epoch
#-----------------------------------------------------------
plt.plot(epochs, loss, 'r', "train_loss")
plt.plot(epochs ,val_loss, 'b', "valida_loss")
plt.title("training and validation loss")
plt.figure()
plt.show()
# 曲线呈直线是因为epochs/轮次太少
# Here's a codeblock just for fun,You should be able to upload an image here
# and have it claassified without crashing
import numpy as np
# from google.colab import files
from tensorflow.keras.preprocessing import image
from tensorflow import keras
model = keras.models.load_model('model.h5')
# upload = files.upload()
#
# for fn in upload.key():
# # predicting images
# path = '/content/' + fn
# img = image.load_img(path, target_size=(150, 150))
# x = image.img_to_array(img)
# x = np.expand_dims(x, axis=0)
#
# images = np.vstack([x])
# classes = model.predict(images, batch_size=10)
# print(classes[0])
# if classes[0]>0.5:
# print(fn + " is a dog")
# else:
# print(fn + " is a cat")
# predicting images
path = 'E:\Python\pythonProject_1\cats_vs_dogs\dog.jpg'
img = image.load_img(path, target_size=(150, 150))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
images = np.vstack([x])
classes = model.predict(images, batch_size=10)
print(classes[0])
if classes[0]>0.5:
print(" is a dog")
else:
print(" is a cat")
Epoch 1/2
36/225 [===>..........................] - ETA: 2:39 - loss: 0.9044 - accuracy: 0.5269E:\pycharm\Anaconda\envs\pythonProject_1\lib\site-packages\PIL\TiffImagePlugin.py:793: UserWarning: Truncated File Read
warnings.warn(str(msg))
225/225 [==============================] - 194s 864ms/step - loss: 0.6645 - accuracy: 0.6406 - val_loss: 0.6875 - val_accuracy: 0.6060
Epoch 2/2
225/225 [==============================] - 192s 855ms/step - loss: 0.5199 - accuracy: 0.7426 - val_loss: 0.4600 - val_accuracy: 0.7936
path = 'E:\Python\pythonProject_1\cat.jpg'
img = image.load_img(path, target_size=(150, 150))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
images = np.vstack([x])
classes = model.predict(images, batch_size=10)
print(classes[0])
if classes[0]>0.5:
print(" is a dog")
else:
print(" is a cat")
[0.]
is a cat
path = 'E:\Python\pythonProject_1\dog.jpg'
img = image.load_img(path, target_size=(150, 150))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
images = np.vstack([x])
classes = model.predict(images, batch_size=10)
print(classes[0])
if classes[0]>0.5:
print(" is a dog")
else:
print(" is a cat")
[1.]
is a dog
数据预处理等效图:
卷积、池化等效流程图(颜色和像素值仅作参考,与实际值无关):
模型结构:
model.summary()
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d (Conv2D) (None, 148, 148, 16) 448
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 74, 74, 16) 0
_________________________________________________________________
conv2d_1 (Conv2D) (None, 72, 72, 32) 4640
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 36, 36, 32) 0
_________________________________________________________________
conv2d_2 (Conv2D) (None, 34, 34, 64) 18496
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 17, 17, 64) 0
_________________________________________________________________
flatten (Flatten) (None, 18496) 0
_________________________________________________________________
dense (Dense) (None, 512) 9470464
_________________________________________________________________
dense_1 (Dense) (None, 1) 513
=================================================================
Total params: 9,494,561
Trainable params: 9,494,561
Non-trainable params: 0
_________________________________________________________________