7 Star 51 Fork 15

肆十二/Flower_tf2.3

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
train_model.py 4.09 KB
一键复制 编辑 原始数据 按行查看 历史
NULL 提交于 2020-12-18 10:56 . 'go'
import tensorflow as tf
import matplotlib.pyplot as plt
# 数据集下载地址:https://storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz
# 数据加载,按照8:2的比例加载花卉数据
def data_load(data_dir, img_height, img_width, batch_size):
train_ds = tf.keras.preprocessing.image_dataset_from_directory(
data_dir,
label_mode='categorical',
validation_split=0.2,
subset="training",
seed=123,
image_size=(img_height, img_width),
batch_size=batch_size)
val_ds = tf.keras.preprocessing.image_dataset_from_directory(
data_dir,
label_mode='categorical',
validation_split=0.2,
subset="validation",
seed=123,
image_size=(img_height, img_width),
batch_size=batch_size)
class_names = train_ds.class_names
return train_ds, val_ds, class_names
# 模型加载,指定图片处理的大小和是否进行迁移学习
def model_load(IMG_SHAPE=(224, 224, 3), is_transfer=False):
# data_augmentation = tf.keras.Sequential([
# tf.keras.layers.experimental.preprocessing.RandomFlip('horizontal'),
# tf.keras.layers.experimental.preprocessing.RandomRotation(0.2),
# ])
if is_transfer:
# 微调的过程中不需要进行归一化的处理
base_model = tf.keras.applications.MobileNetV2(input_shape=IMG_SHAPE,
include_top=False,
weights='imagenet')
base_model.trainable = False
model = tf.keras.models.Sequential([
tf.keras.layers.experimental.preprocessing.Rescaling(1./127.5, offset=-1, input_shape=IMG_SHAPE),
base_model,
tf.keras.layers.GlobalAveragePooling2D(),
tf.keras.layers.Dense(5, activation='softmax')
])
else:
model = tf.keras.models.Sequential([
tf.keras.layers.experimental.preprocessing.Rescaling(1. / 255, input_shape=IMG_SHAPE),
tf.keras.layers.Conv2D(32, (3, 3), activation='relu'),
tf.keras.layers.MaxPooling2D(2, 2),
# Add another convolution
tf.keras.layers.Conv2D(64, (3, 3), activation='relu'),
tf.keras.layers.MaxPooling2D(2, 2),
# Now flatten the output. After this you'll just have the same DNN structure as the non convolutional version
tf.keras.layers.Flatten(),
# The same 128 dense layers, and 10 output layers as in the pre-convolution example:
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dense(5, activation='softmax')
])
model.summary()
# 模型训练
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
return model
# 展示训练过程的曲线
def show_loss_acc(history):
acc = history.history['accuracy']
val_acc = history.history['val_accuracy']
loss = history.history['loss']
val_loss = history.history['val_loss']
plt.figure(figsize=(8, 8))
plt.subplot(2, 1, 1)
plt.plot(acc, label='Training Accuracy')
plt.plot(val_acc, label='Validation Accuracy')
plt.legend(loc='lower right')
plt.ylabel('Accuracy')
plt.ylim([min(plt.ylim()), 1])
plt.title('Training and Validation Accuracy')
plt.subplot(2, 1, 2)
plt.plot(loss, label='Training Loss')
plt.plot(val_loss, label='Validation Loss')
plt.legend(loc='upper right')
plt.ylabel('Cross Entropy')
plt.ylim([0, 1.0])
plt.title('Training and Validation Loss')
plt.xlabel('epoch')
plt.show()
def train(epochs, is_transfer=False):
train_ds, val_ds, class_names = data_load("../data/flower_photos", 224, 224, 4)
model = model_load(is_transfer=is_transfer)
history = model.fit(train_ds, validation_data=val_ds, epochs=epochs)
if is_transfer:
# model.evaluate(val_ds)
model.save("models/mobilenet_flower.h5")
else:
model.save("models/cnn_flower.h5")
show_loss_acc(history)
if __name__ == '__main__':
train(epochs=10, is_transfer=True)
train(epochs=5, is_transfer=False)
# test()
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/song-laogou/Flower_tf2.3.git
git@gitee.com:song-laogou/Flower_tf2.3.git
song-laogou
Flower_tf2.3
Flower_tf2.3
master

搜索帮助