>- **🍨 本文为[🔗365天深度学习训练营]中的学习记录博客**
>- **🍖 原作者:[K同学啊]**
📌 本周任务:
●1. 在DenseNet系列算法中插入SE-Net通道注意力机制,并完成猴痘病识别
●2. 改进思路是否可以迁移到其他地方呢
●3. 测试集accuracy到达89%(拔高,可选)
🚀 我的环境:
- 语言环境:Python3.11.7
- 编译器:jupyter notebook
- 深度学习框架:TensorFlow2.13.0
本文完全根据 第J5周:DenseNet+SE-Net实战(pytorch版) 中的内容转换为TensorFlow,所以前述性的内容不在一一重复,仅就TensorFlow的内容进行叙述。
一、前期工作
1、设置CPU(也可以是GPU)
import tensorflow as tf
gpus=tf.config.list_physical_devices("GPU")
if gpus:
tf.config.experimental.set_memory_growth(gpus[0],True)
tf.config.set_visible_devices([gpus[0]],"GPU")
2、导入数据
import pathlib
data_dir=r'D:\THE MNIST DATABASE\P4-data'
data_dir=pathlib.Path(data_dir)
3、查看数据
image_count=len(list(data_dir.glob('*/*')))
print("图片总数为:",image_count)
运行结果:
图片总数为: 2142
二、数据预处理
1、加载数据
加载训练集:
train_ds=tf.keras.preprocessing.image_dataset_from_directory(
data_dir,
validation_split=0.2,
subset="training",
seed=123,
image_size=(224,224),
batch_size=8
)
运行结果:
Found 2142 files belonging to 2 classes.
Using 1714 files for training.
加载验证集:
val_ds=tf.keras.preprocessing.image_dataset_from_directory(
data_dir,
validation_split=0.2,
subset="validation",
seed=123,
image_size=(224,224),
batch_size=8
)
运行结果:
Found 2142 files belonging to 2 classes.
Using 428 files for validation.
查看分类名称
classNames=train_ds.class_names
classNames
运行结果:
['Monkeypox', 'Others']
2、可视化数据
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']=['SimHei'] #正常显示中文标签
plt.rcParams['axes.unicode_minus']=False #正常显示负号
plt.figure(figsize=(10,5))
plt.suptitle("OreoCC的案例")
for images,labels in train_ds.take(1):
for i in range(8):
ax=plt.subplot(2,4,i+1)
plt.imshow(images[i].numpy().astype("uint8"))
plt.title(classNames[labels[i]])
plt.axis("off")
运行结果:
单独查看其中一张图片
plt.imshow(images[1].numpy().astype("uint8"))
运行结果:
3、再次检查数据
for image_batch,labels_batch in train_ds:
print(image_batch.shape)
print(labels_batch.shape)
break
运行结果:
(8, 224, 224, 3)
(8,)
image_batch是形状的张量(8,224,224,3)。这是一批形状224*224*4的8张图片(最后一维指的是彩色通道RGB)
labels_batch是形状(8,)的张量,这些标签对应8张图片。
4、配置数据集
shuffle() : 打乱数据,关于此函数的详细介绍可以参考:https://zhuanlan.zhihu.com/p/42417456
prefetch() :预取数据,加速运行,其详细介绍可以参考前面文章,里面都有讲解。
cache() :将数据集缓存到内存当中,加速运行
AUTOTUNE=tf.data.AUTOTUNE
train_ds=train_ds.cache().shuffle(1000).prefetch(buffer_size=AUTOTUNE)
val_ds=val_ds.prefetch(buffer_size=AUTOTUNE)
三、构建DenseNet网络模型
1、搭建SE-Net模块
from tensorflow import keras
from keras.layers import Input,Reshape,Activation,BatchNormalization,GlobalAveragePooling2D,Dense
from keras.models import Model
# SE-Net
class SqueezeExcitationLayer(Model):
def __init__(self,filter_sq):
# filter_sq是Excitation中第一个卷积过程中卷积核的个数
super.__init__()
self.avgpool=GlobalAveragePooling2D()
self.dense=Dense(filter_sq)
self.relu=Activation('relu')
self.sigmoid=Activation('sigmoid')
def call(self,inputs):
x=self.avgpool(inputs)
x=self.dense(x)
x=self.relu(x)
x=Dense(inputs.shape[-1])(x)
x=self.sigmoid(x)
x=Reshape((1,1,inputs.shape[-1]))(x)
scale=inputs*x
return scale
2、搭建DenseLayer
from keras.layers import Input,Activation,BatchNormalization,Flatten
from keras.layers import Dense,Conv2D,MaxPooling2D,ZeroPadding2D,AveragePooling2D
from keras.models import Model
# DenseLayer
def DenseLayer(x,growth_rate):
f=BatchNormalization()(x)
f=Activation('relu')(x)
f=Conv2D(4*growth_rate,kernel_size=1,strides=1,padding='same',use_bias=False)(f)
f=BatchNormalization()(f)
f=Activation('relu')(f)
f=Conv2D(growth_rate,kernel_size=3,strides=1,padding='same',use_bias=False)(f)
return layers.Concatenate(axis=3)([x,f])
3、搭建DenseBlock模块
# DenseBlock
def DenseBlock(x,block,growth_rate=32):
for i in range(block):
x=DenseLayer(x,growth_rate)
return x
4、搭建TransitionBlock模块
# Transition
k=keras.backend
def Transition(x,theta):
f=BatchNormalization()(x)
f=Activation('relu')(f)
f=Conv2D(int(k.int_shape(x)[3]*theta),kernel_size=1,strides=1,use_bias=False)(f)
f=AveragePooling2D(pool_size=(2,2),strides=2,padding='valid')(f)
return f
5、搭建DenseNet+SE 网络模型
# DenseNet
def DenseNet(input_shape,block,num_classes=4):
# 56*56*64
img_input=Input(shape=input_shape)
x=Conv2D(64,kernel_size=(7,7),strides=2,padding='same',use_bias=False)(img_input)
x=BatchNormalization()(x)
x=MaxPooling2D(pool_size=3,strides=2,padding='same')(x)
x=DenseBlock(x,block[0])
x=Transition(x,0.5) #28*28
x=DenseBlock(x,block[1])
x=Transition(x,0.5) #14*14
x=DenseBlock(x,block[2])
x=Transition(x,0.5) #7*7
x=DenseBlock(x,block[3])
#加入SE注意力机制
x=SqueezeExcitationLayer(16)(x)
# final bn+ReLU
x=BatchNormalization()(x)
x=Activation('relu')(x)
x=GlobalAveragePooling2D()(x)
outputs=Dense(num_classes,activation='softmax')(x)
model=Model(inputs=[img_input],outputs=[outputs])
return model
6、建立DenseNet-121模型
# DenseNet 121
DenseNet121=DenseNet([224,224,3],[6,12,24,16]) # DenseNet-121
DenseNet169=DenseNet([224,224,3],[6,12,32,32]) # DenseNet-169
DenseNet201=DenseNet([224,224,3],[6,12,48,32]) # DenseNet-201
DenseNet269=DenseNet([224,224,3],[6,12,64,48]) # DenseNet-269
model=DenseNet121
model.summary()
运行结果:
Model: "model"
__________________________________________________________________________________________________
Layer (type) Output Shape Param # Connected to
==================================================================================================
input_1 (InputLayer) [(None, 224, 224, 3)] 0 []
conv2d (Conv2D) (None, 112, 112, 64) 9408 ['input_1[0][0]']
batch_normalization (Batch (None, 112, 112, 64) 256 ['conv2d[0][0]']
Normalization)
max_pooling2d (MaxPooling2 (None, 56, 56, 64) 0 ['batch_normalization[0][0]']
D)
activation (Activation) (None, 56, 56, 64) 0 ['max_pooling2d[0][0]']
conv2d_1 (Conv2D) (None, 56, 56, 128) 8192 ['activation[0][0]']
batch_normalization_2 (Bat (None, 56, 56, 128) 512 ['conv2d_1[0][0]']
chNormalization)
activation_1 (Activation) (None, 56, 56, 128) 0 ['batch_normalization_2[0][0]'
]
conv2d_2 (Conv2D) (None, 56, 56, 32) 36864 ['activation_1[0][0]']
concatenate (Concatenate) (None, 56, 56, 96) 0 ['max_pooling2d[0][0]',
'conv2d_2[0][0]']
activation_2 (Activation) (None, 56, 56, 96) 0 ['concatenate[0][0]']
conv2d_3 (Conv2D) (None, 56, 56, 128) 12288 ['activation_2[0][0]']
batch_normalization_4 (Bat (None, 56, 56, 128) 512 ['conv2d_3[0][0]']
chNormalization)
activation_3 (Activation) (None, 56, 56, 128) 0 ['batch_normalization_4[0][0]'
]
conv2d_4 (Conv2D) (None, 56, 56, 32) 36864 ['activation_3[0][0]']
concatenate_1 (Concatenate (None, 56, 56, 128) 0 ['concatenate[0][0]',
) 'conv2d_4[0][0]']
activation_4 (Activation) (None, 56, 56, 128) 0 ['concatenate_1[0][0]']
conv2d_5 (Conv2D) (None, 56, 56, 128) 16384 ['activation_4[0][0]']
batch_normalization_6 (Bat (None, 56, 56, 128) 512 ['conv2d_5[0][0]']
chNormalization)
activation_5 (Activation) (None, 56, 56, 128) 0 ['batch_normalization_6[0][0]'
]
conv2d_6 (Conv2D) (None, 56, 56, 32) 36864 ['activation_5[0][0]']
concatenate_2 (Concatenate (None, 56, 56, 160) 0 ['concatenate_1[0][0]',
) 'conv2d_6[0][0]']
activation_6 (Activation) (None, 56, 56, 160) 0 ['concatenate_2[0][0]']
conv2d_7 (Conv2D) (None, 56, 56, 128) 20480 ['activation_6[0][0]']
batch_normalization_8 (Bat (None, 56, 56, 128) 512 ['conv2d_7[0][0]']
chNormalization)
activation_7 (Activation) (None, 56, 56, 128) 0 ['batch_normalization_8[0][0]'
]
conv2d_8 (Conv2D) (None, 56, 56, 32) 36864 ['activation_7[0][0]']
concatenate_3 (Concatenate (None, 56, 56, 192) 0 ['concatenate_2[0][0]',
) 'conv2d_8[0][0]']
activation_8 (Activation) (None, 56, 56, 192) 0 ['concatenate_3[0][0]']
conv2d_9 (Conv2D) (None, 56, 56, 128) 24576 ['activation_8[0][0]']
batch_normalization_10 (Ba (None, 56, 56, 128) 512 ['conv2d_9[0][0]']
tchNormalization)
activation_9 (Activation) (None, 56, 56, 128) 0 ['batch_normalization_10[0][0]
']
conv2d_10 (Conv2D) (None, 56, 56, 32) 36864 ['activation_9[0][0]']
concatenate_4 (Concatenate (None, 56, 56, 224) 0 ['concatenate_3[0][0]',
) 'conv2d_10[0][0]']
activation_10 (Activation) (None, 56, 56, 224) 0 ['concatenate_4[0][0]']
conv2d_11 (Conv2D) (None, 56, 56, 128) 28672 ['activation_10[0][0]']
batch_normalization_12 (Ba (None, 56, 56, 128) 512 ['conv2d_11[0][0]']
tchNormalization)
activation_11 (Activation) (None, 56, 56, 128) 0 ['batch_normalization_12[0][0]
']
conv2d_12 (Conv2D) (None, 56, 56, 32) 36864 ['activation_11[0][0]']
concatenate_5 (Concatenate (None, 56, 56, 256) 0 ['concatenate_4[0][0]',
) 'conv2d_12[0][0]']
batch_normalization_13 (Ba (None, 56, 56, 256) 1024 ['concatenate_5[0][0]']
tchNormalization)
activation_12 (Activation) (None, 56, 56, 256) 0 ['batch_normalization_13[0][0]
']
conv2d_13 (Conv2D) (None, 56, 56, 128) 32768 ['activation_12[0][0]']
average_pooling2d (Average (None, 28, 28, 128) 0 ['conv2d_13[0][0]']
Pooling2D)
activation_13 (Activation) (None, 28, 28, 128) 0 ['average_pooling2d[0][0]']
conv2d_14 (Conv2D) (None, 28, 28, 128) 16384 ['activation_13[0][0]']
batch_normalization_15 (Ba (None, 28, 28, 128) 512 ['conv2d_14[0][0]']
tchNormalization)
activation_14 (Activation) (None, 28, 28, 128) 0 ['batch_normalization_15[0][0]
']
conv2d_15 (Conv2D) (None, 28, 28, 32) 36864 ['activation_14[0][0]']
concatenate_6 (Concatenate (None, 28, 28, 160) 0 ['average_pooling2d[0][0]',
) 'conv2d_15[0][0]']
activation_15 (Activation) (None, 28, 28, 160) 0 ['concatenate_6[0][0]']
conv2d_16 (Conv2D) (None, 28, 28, 128) 20480 ['activation_15[0][0]']
batch_normalization_17 (Ba (None, 28, 28, 128) 512 ['conv2d_16[0][0]']
tchNormalization)
activation_16 (Activation) (None, 28, 28, 128) 0 ['batch_normalization_17[0][0]
']
conv2d_17 (Conv2D) (None, 28, 28, 32) 36864 ['activation_16[0][0]']
concatenate_7 (Concatenate (None, 28, 28, 192) 0 ['concatenate_6[0][0]',
) 'conv2d_17[0][0]']
activation_17 (Activation) (None, 28, 28, 192) 0 ['concatenate_7[0][0]']
conv2d_18 (Conv2D) (None, 28, 28, 128) 24576 ['activation_17[0][0]']
batch_normalization_19 (Ba (None, 28, 28, 128) 512 ['conv2d_18[0][0]']
tchNormalization)
activation_18 (Activation) (None, 28, 28, 128) 0 ['batch_normalization_19[0][0]
']
conv2d_19 (Conv2D) (None, 28, 28, 32) 36864 ['activation_18[0][0]']
concatenate_8 (Concatenate (None, 28, 28, 224) 0 ['concatenate_7[0][0]',
) 'conv2d_19[0][0]']
activation_19 (Activation) (None, 28, 28, 224) 0 ['concatenate_8[0][0]']
conv2d_20 (Conv2D) (None, 28, 28, 128) 28672 ['activation_19[0][0]']
batch_normalization_21 (Ba (None, 28, 28, 128) 512 ['conv2d_20[0][0]']
tchNormalization)
activation_20 (Activation) (None, 28, 28, 128) 0 ['batch_normalization_21[0][0]
']
conv2d_21 (Conv2D) (None, 28, 28, 32) 36864 ['activation_20[0][0]']
concatenate_9 (Concatenate (None, 28, 28, 256) 0 ['concatenate_8[0][0]',
) 'conv2d_21[0][0]']
activation_21 (Activation) (None, 28, 28, 256) 0 ['concatenate_9[0][0]']
conv2d_22 (Conv2D) (None, 28, 28, 128) 32768 ['activation_21[0][0]']
batch_normalization_23 (Ba (None, 28, 28, 128) 512 ['conv2d_22[0][0]']
tchNormalization)
activation_22 (Activation) (None, 28, 28, 128) 0 ['batch_normalization_23[0][0]
']
conv2d_23 (Conv2D) (None, 28, 28, 32) 36864 ['activation_22[0][0]']
concatenate_10 (Concatenat (None, 28, 28, 288) 0 ['concatenate_9[0][0]',
e) 'conv2d_23[0][0]']
activation_23 (Activation) (None, 28, 28, 288) 0 ['concatenate_10[0][0]']
conv2d_24 (Conv2D) (None, 28, 28, 128) 36864 ['activation_23[0][0]']
batch_normalization_25 (Ba (None, 28, 28, 128) 512 ['conv2d_24[0][0]']
tchNormalization)
activation_24 (Activation) (None, 28, 28, 128) 0 ['batch_normalization_25[0][0]
']
conv2d_25 (Conv2D) (None, 28, 28, 32) 36864 ['activation_24[0][0]']
concatenate_11 (Concatenat (None, 28, 28, 320) 0 ['concatenate_10[0][0]',
e) 'conv2d_25[0][0]']
activation_25 (Activation) (None, 28, 28, 320) 0 ['concatenate_11[0][0]']
conv2d_26 (Conv2D) (None, 28, 28, 128) 40960 ['activation_25[0][0]']
batch_normalization_27 (Ba (None, 28, 28, 128) 512 ['conv2d_26[0][0]']
tchNormalization)
activation_26 (Activation) (None, 28, 28, 128) 0 ['batch_normalization_27[0][0]
']
conv2d_27 (Conv2D) (None, 28, 28, 32) 36864 ['activation_26[0][0]']
concatenate_12 (Concatenat (None, 28, 28, 352) 0 ['concatenate_11[0][0]',
e) 'conv2d_27[0][0]']
activation_27 (Activation) (None, 28, 28, 352) 0 ['concatenate_12[0][0]']
conv2d_28 (Conv2D) (None, 28, 28, 128) 45056 ['activation_27[0][0]']
batch_normalization_29 (Ba (None, 28, 28, 128) 512 ['conv2d_28[0][0]']
tchNormalization)
activation_28 (Activation) (None, 28, 28, 128) 0 ['batch_normalization_29[0][0]
']
conv2d_29 (Conv2D) (None, 28, 28, 32) 36864 ['activation_28[0][0]']
concatenate_13 (Concatenat (None, 28, 28, 384) 0 ['concatenate_12[0][0]',
e) 'conv2d_29[0][0]']
activation_29 (Activation) (None, 28, 28, 384) 0 ['concatenate_13[0][0]']
conv2d_30 (Conv2D) (None, 28, 28, 128) 49152 ['activation_29[0][0]']
batch_normalization_31 (Ba (None, 28, 28, 128) 512 ['conv2d_30[0][0]']
tchNormalization)
activation_30 (Activation) (None, 28, 28, 128) 0 ['batch_normalization_31[0][0]
']
conv2d_31 (Conv2D) (None, 28, 28, 32) 36864 ['activation_30[0][0]']
concatenate_14 (Concatenat (None, 28, 28, 416) 0 ['concatenate_13[0][0]',
e) 'conv2d_31[0][0]']
activation_31 (Activation) (None, 28, 28, 416) 0 ['concatenate_14[0][0]']
conv2d_32 (Conv2D) (None, 28, 28, 128) 53248 ['activation_31[0][0]']
batch_normalization_33 (Ba (None, 28, 28, 128) 512 ['conv2d_32[0][0]']
tchNormalization)
activation_32 (Activation) (None, 28, 28, 128) 0 ['batch_normalization_33[0][0]
']
conv2d_33 (Conv2D) (None, 28, 28, 32) 36864 ['activation_32[0][0]']
concatenate_15 (Concatenat (None, 28, 28, 448) 0 ['concatenate_14[0][0]',
e) 'conv2d_33[0][0]']
activation_33 (Activation) (None, 28, 28, 448) 0 ['concatenate_15[0][0]']
conv2d_34 (Conv2D) (None, 28, 28, 128) 57344 ['activation_33[0][0]']
batch_normalization_35 (Ba (None, 28, 28, 128) 512 ['conv2d_34[0][0]']
tchNormalization)
activation_34 (Activation) (None, 28, 28, 128) 0 ['batch_normalization_35[0][0]
']
conv2d_35 (Conv2D) (None, 28, 28, 32) 36864 ['activation_34[0][0]']
concatenate_16 (Concatenat (None, 28, 28, 480) 0 ['concatenate_15[0][0]',
e) 'conv2d_35[0][0]']
activation_35 (Activation) (None, 28, 28, 480) 0 ['concatenate_16[0][0]']
conv2d_36 (Conv2D) (None, 28, 28, 128) 61440 ['activation_35[0][0]']
batch_normalization_37 (Ba (None, 28, 28, 128) 512 ['conv2d_36[0][0]']
tchNormalization)
activation_36 (Activation) (None, 28, 28, 128) 0 ['batch_normalization_37[0][0]
']
conv2d_37 (Conv2D) (None, 28, 28, 32) 36864 ['activation_36[0][0]']
concatenate_17 (Concatenat (None, 28, 28, 512) 0 ['concatenate_16[0][0]',
e) 'conv2d_37[0][0]']
batch_normalization_38 (Ba (None, 28, 28, 512) 2048 ['concatenate_17[0][0]']
tchNormalization)
activation_37 (Activation) (None, 28, 28, 512) 0 ['batch_normalization_38[0][0]
']
conv2d_38 (Conv2D) (None, 28, 28, 256) 131072 ['activation_37[0][0]']
average_pooling2d_1 (Avera (None, 14, 14, 256) 0 ['conv2d_38[0][0]']
gePooling2D)
activation_38 (Activation) (None, 14, 14, 256) 0 ['average_pooling2d_1[0][0]']
conv2d_39 (Conv2D) (None, 14, 14, 128) 32768 ['activation_38[0][0]']
batch_normalization_40 (Ba (None, 14, 14, 128) 512 ['conv2d_39[0][0]']
tchNormalization)
activation_39 (Activation) (None, 14, 14, 128) 0 ['batch_normalization_40[0][0]
']
conv2d_40 (Conv2D) (None, 14, 14, 32) 36864 ['activation_39[0][0]']
concatenate_18 (Concatenat (None, 14, 14, 288) 0 ['average_pooling2d_1[0][0]',
e) 'conv2d_40[0][0]']
activation_40 (Activation) (None, 14, 14, 288) 0 ['concatenate_18[0][0]']
conv2d_41 (Conv2D) (None, 14, 14, 128) 36864 ['activation_40[0][0]']
batch_normalization_42 (Ba (None, 14, 14, 128) 512 ['conv2d_41[0][0]']
tchNormalization)
activation_41 (Activation) (None, 14, 14, 128) 0 ['batch_normalization_42[0][0]
']
conv2d_42 (Conv2D) (None, 14, 14, 32) 36864 ['activation_41[0][0]']
concatenate_19 (Concatenat (None, 14, 14, 320) 0 ['concatenate_18[0][0]',
e) 'conv2d_42[0][0]']
activation_42 (Activation) (None, 14, 14, 320) 0 ['concatenate_19[0][0]']
conv2d_43 (Conv2D) (None, 14, 14, 128) 40960 ['activation_42[0][0]']
batch_normalization_44 (Ba (None, 14, 14, 128) 512 ['conv2d_43[0][0]']
tchNormalization)
activation_43 (Activation) (None, 14, 14, 128) 0 ['batch_normalization_44[0][0]
']
conv2d_44 (Conv2D) (None, 14, 14, 32) 36864 ['activation_43[0][0]']
concatenate_20 (Concatenat (None, 14, 14, 352) 0 ['concatenate_19[0][0]',
e) 'conv2d_44[0][0]']
activation_44 (Activation) (None, 14, 14, 352) 0 ['concatenate_20[0][0]']
conv2d_45 (Conv2D) (None, 14, 14, 128) 45056 ['activation_44[0][0]']
batch_normalization_46 (Ba (None, 14, 14, 128) 512 ['conv2d_45[0][0]']
tchNormalization)
activation_45 (Activation) (None, 14, 14, 128) 0 ['batch_normalization_46[0][0]
']
conv2d_46 (Conv2D) (None, 14, 14, 32) 36864 ['activation_45[0][0]']
concatenate_21 (Concatenat (None, 14, 14, 384) 0 ['concatenate_20[0][0]',
e) 'conv2d_46[0][0]']
activation_46 (Activation) (None, 14, 14, 384) 0 ['concatenate_21[0][0]']
conv2d_47 (Conv2D) (None, 14, 14, 128) 49152 ['activation_46[0][0]']
batch_normalization_48 (Ba (None, 14, 14, 128) 512 ['conv2d_47[0][0]']
tchNormalization)
activation_47 (Activation) (None, 14, 14, 128) 0 ['batch_normalization_48[0][0]
']
conv2d_48 (Conv2D) (None, 14, 14, 32) 36864 ['activation_47[0][0]']
concatenate_22 (Concatenat (None, 14, 14, 416) 0 ['concatenate_21[0][0]',
e) 'conv2d_48[0][0]']
activation_48 (Activation) (None, 14, 14, 416) 0 ['concatenate_22[0][0]']
conv2d_49 (Conv2D) (None, 14, 14, 128) 53248 ['activation_48[0][0]']
batch_normalization_50 (Ba (None, 14, 14, 128) 512 ['conv2d_49[0][0]']
tchNormalization)
activation_49 (Activation) (None, 14, 14, 128) 0 ['batch_normalization_50[0][0]
']
conv2d_50 (Conv2D) (None, 14, 14, 32) 36864 ['activation_49[0][0]']
concatenate_23 (Concatenat (None, 14, 14, 448) 0 ['concatenate_22[0][0]',
e) 'conv2d_50[0][0]']
activation_50 (Activation) (None, 14, 14, 448) 0 ['concatenate_23[0][0]']
conv2d_51 (Conv2D) (None, 14, 14, 128) 57344 ['activation_50[0][0]']
batch_normalization_52 (Ba (None, 14, 14, 128) 512 ['conv2d_51[0][0]']
tchNormalization)
activation_51 (Activation) (None, 14, 14, 128) 0 ['batch_normalization_52[0][0]
']
conv2d_52 (Conv2D) (None, 14, 14, 32) 36864 ['activation_51[0][0]']
concatenate_24 (Concatenat (None, 14, 14, 480) 0 ['concatenate_23[0][0]',
e) 'conv2d_52[0][0]']
activation_52 (Activation) (None, 14, 14, 480) 0 ['concatenate_24[0][0]']
conv2d_53 (Conv2D) (None, 14, 14, 128) 61440 ['activation_52[0][0]']
batch_normalization_54 (Ba (None, 14, 14, 128) 512 ['conv2d_53[0][0]']
tchNormalization)
activation_53 (Activation) (None, 14, 14, 128) 0 ['batch_normalization_54[0][0]
']
conv2d_54 (Conv2D) (None, 14, 14, 32) 36864 ['activation_53[0][0]']
concatenate_25 (Concatenat (None, 14, 14, 512) 0 ['concatenate_24[0][0]',
e) 'conv2d_54[0][0]']
activation_54 (Activation) (None, 14, 14, 512) 0 ['concatenate_25[0][0]']
conv2d_55 (Conv2D) (None, 14, 14, 128) 65536 ['activation_54[0][0]']
batch_normalization_56 (Ba (None, 14, 14, 128) 512 ['conv2d_55[0][0]']
tchNormalization)
activation_55 (Activation) (None, 14, 14, 128) 0 ['batch_normalization_56[0][0]
']
conv2d_56 (Conv2D) (None, 14, 14, 32) 36864 ['activation_55[0][0]']
concatenate_26 (Concatenat (None, 14, 14, 544) 0 ['concatenate_25[0][0]',
e) 'conv2d_56[0][0]']
activation_56 (Activation) (None, 14, 14, 544) 0 ['concatenate_26[0][0]']
conv2d_57 (Conv2D) (None, 14, 14, 128) 69632 ['activation_56[0][0]']
batch_normalization_58 (Ba (None, 14, 14, 128) 512 ['conv2d_57[0][0]']
tchNormalization)
activation_57 (Activation) (None, 14, 14, 128) 0 ['batch_normalization_58[0][0]
']
conv2d_58 (Conv2D) (None, 14, 14, 32) 36864 ['activation_57[0][0]']
concatenate_27 (Concatenat (None, 14, 14, 576) 0 ['concatenate_26[0][0]',
e) 'conv2d_58[0][0]']
activation_58 (Activation) (None, 14, 14, 576) 0 ['concatenate_27[0][0]']
conv2d_59 (Conv2D) (None, 14, 14, 128) 73728 ['activation_58[0][0]']
batch_normalization_60 (Ba (None, 14, 14, 128) 512 ['conv2d_59[0][0]']
tchNormalization)
activation_59 (Activation) (None, 14, 14, 128) 0 ['batch_normalization_60[0][0]
']
conv2d_60 (Conv2D) (None, 14, 14, 32) 36864 ['activation_59[0][0]']
concatenate_28 (Concatenat (None, 14, 14, 608) 0 ['concatenate_27[0][0]',
e) 'conv2d_60[0][0]']
activation_60 (Activation) (None, 14, 14, 608) 0 ['concatenate_28[0][0]']
conv2d_61 (Conv2D) (None, 14, 14, 128) 77824 ['activation_60[0][0]']
batch_normalization_62 (Ba (None, 14, 14, 128) 512 ['conv2d_61[0][0]']
tchNormalization)
activation_61 (Activation) (None, 14, 14, 128) 0 ['batch_normalization_62[0][0]
']
conv2d_62 (Conv2D) (None, 14, 14, 32) 36864 ['activation_61[0][0]']
concatenate_29 (Concatenat (None, 14, 14, 640) 0 ['concatenate_28[0][0]',
e) 'conv2d_62[0][0]']
activation_62 (Activation) (None, 14, 14, 640) 0 ['concatenate_29[0][0]']
conv2d_63 (Conv2D) (None, 14, 14, 128) 81920 ['activation_62[0][0]']
batch_normalization_64 (Ba (None, 14, 14, 128) 512 ['conv2d_63[0][0]']
tchNormalization)
activation_63 (Activation) (None, 14, 14, 128) 0 ['batch_normalization_64[0][0]
']
conv2d_64 (Conv2D) (None, 14, 14, 32) 36864 ['activation_63[0][0]']
concatenate_30 (Concatenat (None, 14, 14, 672) 0 ['concatenate_29[0][0]',
e) 'conv2d_64[0][0]']
activation_64 (Activation) (None, 14, 14, 672) 0 ['concatenate_30[0][0]']
conv2d_65 (Conv2D) (None, 14, 14, 128) 86016 ['activation_64[0][0]']
batch_normalization_66 (Ba (None, 14, 14, 128) 512 ['conv2d_65[0][0]']
tchNormalization)
activation_65 (Activation) (None, 14, 14, 128) 0 ['batch_normalization_66[0][0]
']
conv2d_66 (Conv2D) (None, 14, 14, 32) 36864 ['activation_65[0][0]']
concatenate_31 (Concatenat (None, 14, 14, 704) 0 ['concatenate_30[0][0]',
e) 'conv2d_66[0][0]']
activation_66 (Activation) (None, 14, 14, 704) 0 ['concatenate_31[0][0]']
conv2d_67 (Conv2D) (None, 14, 14, 128) 90112 ['activation_66[0][0]']
batch_normalization_68 (Ba (None, 14, 14, 128) 512 ['conv2d_67[0][0]']
tchNormalization)
activation_67 (Activation) (None, 14, 14, 128) 0 ['batch_normalization_68[0][0]
']
conv2d_68 (Conv2D) (None, 14, 14, 32) 36864 ['activation_67[0][0]']
concatenate_32 (Concatenat (None, 14, 14, 736) 0 ['concatenate_31[0][0]',
e) 'conv2d_68[0][0]']
activation_68 (Activation) (None, 14, 14, 736) 0 ['concatenate_32[0][0]']
conv2d_69 (Conv2D) (None, 14, 14, 128) 94208 ['activation_68[0][0]']
batch_normalization_70 (Ba (None, 14, 14, 128) 512 ['conv2d_69[0][0]']
tchNormalization)
activation_69 (Activation) (None, 14, 14, 128) 0 ['batch_normalization_70[0][0]
']
conv2d_70 (Conv2D) (None, 14, 14, 32) 36864 ['activation_69[0][0]']
concatenate_33 (Concatenat (None, 14, 14, 768) 0 ['concatenate_32[0][0]',
e) 'conv2d_70[0][0]']
activation_70 (Activation) (None, 14, 14, 768) 0 ['concatenate_33[0][0]']
conv2d_71 (Conv2D) (None, 14, 14, 128) 98304 ['activation_70[0][0]']
batch_normalization_72 (Ba (None, 14, 14, 128) 512 ['conv2d_71[0][0]']
tchNormalization)
activation_71 (Activation) (None, 14, 14, 128) 0 ['batch_normalization_72[0][0]
']
conv2d_72 (Conv2D) (None, 14, 14, 32) 36864 ['activation_71[0][0]']
concatenate_34 (Concatenat (None, 14, 14, 800) 0 ['concatenate_33[0][0]',
e) 'conv2d_72[0][0]']
activation_72 (Activation) (None, 14, 14, 800) 0 ['concatenate_34[0][0]']
conv2d_73 (Conv2D) (None, 14, 14, 128) 102400 ['activation_72[0][0]']
batch_normalization_74 (Ba (None, 14, 14, 128) 512 ['conv2d_73[0][0]']
tchNormalization)
activation_73 (Activation) (None, 14, 14, 128) 0 ['batch_normalization_74[0][0]
']
conv2d_74 (Conv2D) (None, 14, 14, 32) 36864 ['activation_73[0][0]']
concatenate_35 (Concatenat (None, 14, 14, 832) 0 ['concatenate_34[0][0]',
e) 'conv2d_74[0][0]']
activation_74 (Activation) (None, 14, 14, 832) 0 ['concatenate_35[0][0]']
conv2d_75 (Conv2D) (None, 14, 14, 128) 106496 ['activation_74[0][0]']
batch_normalization_76 (Ba (None, 14, 14, 128) 512 ['conv2d_75[0][0]']
tchNormalization)
activation_75 (Activation) (None, 14, 14, 128) 0 ['batch_normalization_76[0][0]
']
conv2d_76 (Conv2D) (None, 14, 14, 32) 36864 ['activation_75[0][0]']
concatenate_36 (Concatenat (None, 14, 14, 864) 0 ['concatenate_35[0][0]',
e) 'conv2d_76[0][0]']
activation_76 (Activation) (None, 14, 14, 864) 0 ['concatenate_36[0][0]']
conv2d_77 (Conv2D) (None, 14, 14, 128) 110592 ['activation_76[0][0]']
batch_normalization_78 (Ba (None, 14, 14, 128) 512 ['conv2d_77[0][0]']
tchNormalization)
activation_77 (Activation) (None, 14, 14, 128) 0 ['batch_normalization_78[0][0]
']
conv2d_78 (Conv2D) (None, 14, 14, 32) 36864 ['activation_77[0][0]']
concatenate_37 (Concatenat (None, 14, 14, 896) 0 ['concatenate_36[0][0]',
e) 'conv2d_78[0][0]']
activation_78 (Activation) (None, 14, 14, 896) 0 ['concatenate_37[0][0]']
conv2d_79 (Conv2D) (None, 14, 14, 128) 114688 ['activation_78[0][0]']
batch_normalization_80 (Ba (None, 14, 14, 128) 512 ['conv2d_79[0][0]']
tchNormalization)
activation_79 (Activation) (None, 14, 14, 128) 0 ['batch_normalization_80[0][0]
']
conv2d_80 (Conv2D) (None, 14, 14, 32) 36864 ['activation_79[0][0]']
concatenate_38 (Concatenat (None, 14, 14, 928) 0 ['concatenate_37[0][0]',
e) 'conv2d_80[0][0]']
activation_80 (Activation) (None, 14, 14, 928) 0 ['concatenate_38[0][0]']
conv2d_81 (Conv2D) (None, 14, 14, 128) 118784 ['activation_80[0][0]']
batch_normalization_82 (Ba (None, 14, 14, 128) 512 ['conv2d_81[0][0]']
tchNormalization)
activation_81 (Activation) (None, 14, 14, 128) 0 ['batch_normalization_82[0][0]
']
conv2d_82 (Conv2D) (None, 14, 14, 32) 36864 ['activation_81[0][0]']
concatenate_39 (Concatenat (None, 14, 14, 960) 0 ['concatenate_38[0][0]',
e) 'conv2d_82[0][0]']
activation_82 (Activation) (None, 14, 14, 960) 0 ['concatenate_39[0][0]']
conv2d_83 (Conv2D) (None, 14, 14, 128) 122880 ['activation_82[0][0]']
batch_normalization_84 (Ba (None, 14, 14, 128) 512 ['conv2d_83[0][0]']
tchNormalization)
activation_83 (Activation) (None, 14, 14, 128) 0 ['batch_normalization_84[0][0]
']
conv2d_84 (Conv2D) (None, 14, 14, 32) 36864 ['activation_83[0][0]']
concatenate_40 (Concatenat (None, 14, 14, 992) 0 ['concatenate_39[0][0]',
e) 'conv2d_84[0][0]']
activation_84 (Activation) (None, 14, 14, 992) 0 ['concatenate_40[0][0]']
conv2d_85 (Conv2D) (None, 14, 14, 128) 126976 ['activation_84[0][0]']
batch_normalization_86 (Ba (None, 14, 14, 128) 512 ['conv2d_85[0][0]']
tchNormalization)
activation_85 (Activation) (None, 14, 14, 128) 0 ['batch_normalization_86[0][0]
']
conv2d_86 (Conv2D) (None, 14, 14, 32) 36864 ['activation_85[0][0]']
concatenate_41 (Concatenat (None, 14, 14, 1024) 0 ['concatenate_40[0][0]',
e) 'conv2d_86[0][0]']
batch_normalization_87 (Ba (None, 14, 14, 1024) 4096 ['concatenate_41[0][0]']
tchNormalization)
activation_86 (Activation) (None, 14, 14, 1024) 0 ['batch_normalization_87[0][0]
']
conv2d_87 (Conv2D) (None, 14, 14, 512) 524288 ['activation_86[0][0]']
average_pooling2d_2 (Avera (None, 7, 7, 512) 0 ['conv2d_87[0][0]']
gePooling2D)
activation_87 (Activation) (None, 7, 7, 512) 0 ['average_pooling2d_2[0][0]']
conv2d_88 (Conv2D) (None, 7, 7, 128) 65536 ['activation_87[0][0]']
batch_normalization_89 (Ba (None, 7, 7, 128) 512 ['conv2d_88[0][0]']
tchNormalization)
activation_88 (Activation) (None, 7, 7, 128) 0 ['batch_normalization_89[0][0]
']
conv2d_89 (Conv2D) (None, 7, 7, 32) 36864 ['activation_88[0][0]']
concatenate_42 (Concatenat (None, 7, 7, 544) 0 ['average_pooling2d_2[0][0]',
e) 'conv2d_89[0][0]']
activation_89 (Activation) (None, 7, 7, 544) 0 ['concatenate_42[0][0]']
conv2d_90 (Conv2D) (None, 7, 7, 128) 69632 ['activation_89[0][0]']
batch_normalization_91 (Ba (None, 7, 7, 128) 512 ['conv2d_90[0][0]']
tchNormalization)
activation_90 (Activation) (None, 7, 7, 128) 0 ['batch_normalization_91[0][0]
']
conv2d_91 (Conv2D) (None, 7, 7, 32) 36864 ['activation_90[0][0]']
concatenate_43 (Concatenat (None, 7, 7, 576) 0 ['concatenate_42[0][0]',
e) 'conv2d_91[0][0]']
activation_91 (Activation) (None, 7, 7, 576) 0 ['concatenate_43[0][0]']
conv2d_92 (Conv2D) (None, 7, 7, 128) 73728 ['activation_91[0][0]']
batch_normalization_93 (Ba (None, 7, 7, 128) 512 ['conv2d_92[0][0]']
tchNormalization)
activation_92 (Activation) (None, 7, 7, 128) 0 ['batch_normalization_93[0][0]
']
conv2d_93 (Conv2D) (None, 7, 7, 32) 36864 ['activation_92[0][0]']
concatenate_44 (Concatenat (None, 7, 7, 608) 0 ['concatenate_43[0][0]',
e) 'conv2d_93[0][0]']
activation_93 (Activation) (None, 7, 7, 608) 0 ['concatenate_44[0][0]']
conv2d_94 (Conv2D) (None, 7, 7, 128) 77824 ['activation_93[0][0]']
batch_normalization_95 (Ba (None, 7, 7, 128) 512 ['conv2d_94[0][0]']
tchNormalization)
activation_94 (Activation) (None, 7, 7, 128) 0 ['batch_normalization_95[0][0]
']
conv2d_95 (Conv2D) (None, 7, 7, 32) 36864 ['activation_94[0][0]']
concatenate_45 (Concatenat (None, 7, 7, 640) 0 ['concatenate_44[0][0]',
e) 'conv2d_95[0][0]']
activation_95 (Activation) (None, 7, 7, 640) 0 ['concatenate_45[0][0]']
conv2d_96 (Conv2D) (None, 7, 7, 128) 81920 ['activation_95[0][0]']
batch_normalization_97 (Ba (None, 7, 7, 128) 512 ['conv2d_96[0][0]']
tchNormalization)
activation_96 (Activation) (None, 7, 7, 128) 0 ['batch_normalization_97[0][0]
']
conv2d_97 (Conv2D) (None, 7, 7, 32) 36864 ['activation_96[0][0]']
concatenate_46 (Concatenat (None, 7, 7, 672) 0 ['concatenate_45[0][0]',
e) 'conv2d_97[0][0]']
activation_97 (Activation) (None, 7, 7, 672) 0 ['concatenate_46[0][0]']
conv2d_98 (Conv2D) (None, 7, 7, 128) 86016 ['activation_97[0][0]']
batch_normalization_99 (Ba (None, 7, 7, 128) 512 ['conv2d_98[0][0]']
tchNormalization)
activation_98 (Activation) (None, 7, 7, 128) 0 ['batch_normalization_99[0][0]
']
conv2d_99 (Conv2D) (None, 7, 7, 32) 36864 ['activation_98[0][0]']
concatenate_47 (Concatenat (None, 7, 7, 704) 0 ['concatenate_46[0][0]',
e) 'conv2d_99[0][0]']
activation_99 (Activation) (None, 7, 7, 704) 0 ['concatenate_47[0][0]']
conv2d_100 (Conv2D) (None, 7, 7, 128) 90112 ['activation_99[0][0]']
batch_normalization_101 (B (None, 7, 7, 128) 512 ['conv2d_100[0][0]']
atchNormalization)
activation_100 (Activation (None, 7, 7, 128) 0 ['batch_normalization_101[0][0
) ]']
conv2d_101 (Conv2D) (None, 7, 7, 32) 36864 ['activation_100[0][0]']
concatenate_48 (Concatenat (None, 7, 7, 736) 0 ['concatenate_47[0][0]',
e) 'conv2d_101[0][0]']
activation_101 (Activation (None, 7, 7, 736) 0 ['concatenate_48[0][0]']
)
conv2d_102 (Conv2D) (None, 7, 7, 128) 94208 ['activation_101[0][0]']
batch_normalization_103 (B (None, 7, 7, 128) 512 ['conv2d_102[0][0]']
atchNormalization)
activation_102 (Activation (None, 7, 7, 128) 0 ['batch_normalization_103[0][0
) ]']
conv2d_103 (Conv2D) (None, 7, 7, 32) 36864 ['activation_102[0][0]']
concatenate_49 (Concatenat (None, 7, 7, 768) 0 ['concatenate_48[0][0]',
e) 'conv2d_103[0][0]']
activation_103 (Activation (None, 7, 7, 768) 0 ['concatenate_49[0][0]']
)
conv2d_104 (Conv2D) (None, 7, 7, 128) 98304 ['activation_103[0][0]']
batch_normalization_105 (B (None, 7, 7, 128) 512 ['conv2d_104[0][0]']
atchNormalization)
activation_104 (Activation (None, 7, 7, 128) 0 ['batch_normalization_105[0][0
) ]']
conv2d_105 (Conv2D) (None, 7, 7, 32) 36864 ['activation_104[0][0]']
concatenate_50 (Concatenat (None, 7, 7, 800) 0 ['concatenate_49[0][0]',
e) 'conv2d_105[0][0]']
activation_105 (Activation (None, 7, 7, 800) 0 ['concatenate_50[0][0]']
)
conv2d_106 (Conv2D) (None, 7, 7, 128) 102400 ['activation_105[0][0]']
batch_normalization_107 (B (None, 7, 7, 128) 512 ['conv2d_106[0][0]']
atchNormalization)
activation_106 (Activation (None, 7, 7, 128) 0 ['batch_normalization_107[0][0
) ]']
conv2d_107 (Conv2D) (None, 7, 7, 32) 36864 ['activation_106[0][0]']
concatenate_51 (Concatenat (None, 7, 7, 832) 0 ['concatenate_50[0][0]',
e) 'conv2d_107[0][0]']
activation_107 (Activation (None, 7, 7, 832) 0 ['concatenate_51[0][0]']
)
conv2d_108 (Conv2D) (None, 7, 7, 128) 106496 ['activation_107[0][0]']
batch_normalization_109 (B (None, 7, 7, 128) 512 ['conv2d_108[0][0]']
atchNormalization)
activation_108 (Activation (None, 7, 7, 128) 0 ['batch_normalization_109[0][0
) ]']
conv2d_109 (Conv2D) (None, 7, 7, 32) 36864 ['activation_108[0][0]']
concatenate_52 (Concatenat (None, 7, 7, 864) 0 ['concatenate_51[0][0]',
e) 'conv2d_109[0][0]']
activation_109 (Activation (None, 7, 7, 864) 0 ['concatenate_52[0][0]']
)
conv2d_110 (Conv2D) (None, 7, 7, 128) 110592 ['activation_109[0][0]']
batch_normalization_111 (B (None, 7, 7, 128) 512 ['conv2d_110[0][0]']
atchNormalization)
activation_110 (Activation (None, 7, 7, 128) 0 ['batch_normalization_111[0][0
) ]']
conv2d_111 (Conv2D) (None, 7, 7, 32) 36864 ['activation_110[0][0]']
concatenate_53 (Concatenat (None, 7, 7, 896) 0 ['concatenate_52[0][0]',
e) 'conv2d_111[0][0]']
activation_111 (Activation (None, 7, 7, 896) 0 ['concatenate_53[0][0]']
)
conv2d_112 (Conv2D) (None, 7, 7, 128) 114688 ['activation_111[0][0]']
batch_normalization_113 (B (None, 7, 7, 128) 512 ['conv2d_112[0][0]']
atchNormalization)
activation_112 (Activation (None, 7, 7, 128) 0 ['batch_normalization_113[0][0
) ]']
conv2d_113 (Conv2D) (None, 7, 7, 32) 36864 ['activation_112[0][0]']
concatenate_54 (Concatenat (None, 7, 7, 928) 0 ['concatenate_53[0][0]',
e) 'conv2d_113[0][0]']
activation_113 (Activation (None, 7, 7, 928) 0 ['concatenate_54[0][0]']
)
conv2d_114 (Conv2D) (None, 7, 7, 128) 118784 ['activation_113[0][0]']
batch_normalization_115 (B (None, 7, 7, 128) 512 ['conv2d_114[0][0]']
atchNormalization)
activation_114 (Activation (None, 7, 7, 128) 0 ['batch_normalization_115[0][0
) ]']
conv2d_115 (Conv2D) (None, 7, 7, 32) 36864 ['activation_114[0][0]']
concatenate_55 (Concatenat (None, 7, 7, 960) 0 ['concatenate_54[0][0]',
e) 'conv2d_115[0][0]']
activation_115 (Activation (None, 7, 7, 960) 0 ['concatenate_55[0][0]']
)
conv2d_116 (Conv2D) (None, 7, 7, 128) 122880 ['activation_115[0][0]']
batch_normalization_117 (B (None, 7, 7, 128) 512 ['conv2d_116[0][0]']
atchNormalization)
activation_116 (Activation (None, 7, 7, 128) 0 ['batch_normalization_117[0][0
) ]']
conv2d_117 (Conv2D) (None, 7, 7, 32) 36864 ['activation_116[0][0]']
concatenate_56 (Concatenat (None, 7, 7, 992) 0 ['concatenate_55[0][0]',
e) 'conv2d_117[0][0]']
activation_117 (Activation (None, 7, 7, 992) 0 ['concatenate_56[0][0]']
)
conv2d_118 (Conv2D) (None, 7, 7, 128) 126976 ['activation_117[0][0]']
batch_normalization_119 (B (None, 7, 7, 128) 512 ['conv2d_118[0][0]']
atchNormalization)
activation_118 (Activation (None, 7, 7, 128) 0 ['batch_normalization_119[0][0
) ]']
conv2d_119 (Conv2D) (None, 7, 7, 32) 36864 ['activation_118[0][0]']
concatenate_57 (Concatenat (None, 7, 7, 1024) 0 ['concatenate_56[0][0]',
e) 'conv2d_119[0][0]']
batch_normalization_120 (B (None, 7, 7, 1024) 4096 ['concatenate_57[0][0]']
atchNormalization)
activation_119 (Activation (None, 7, 7, 1024) 0 ['batch_normalization_120[0][0
) ]']
global_average_pooling2d ( (None, 1024) 0 ['activation_119[0][0]']
GlobalAveragePooling2D)
dense (Dense) (None, 4) 4100 ['global_average_pooling2d[0][
0]']
==================================================================================================
Total params: 6915524 (26.38 MB)
Trainable params: 6894916 (26.30 MB)
Non-trainable params: 20608 (80.50 KB)
四、编译
在准备对模型进行训练之前,还需要再对其进行一些设置。以下内容是在模型的编译步骤中添加的:
损失函数(loss):用于衡量模型在训练期间的准确率。
优化器(optimizer):决定模型如何根据其看到的数据和自身的损失函数进行更新。
指标(metrics):用于监控训练和测试步骤。以下示例使用了准确率,即被正确分类的图像的比率。
#设置优化器
opt=tf.keras.optimizers.Adam(learning_rate=1e-4)
model.compile(optimizer=opt,
loss=tf.keras.losses.SparseCategoricalCrossentropy(),
metrics=['accuracy'])
五、训练模型
epochs=50
history=model.fit(
train_ds,
validation_data=val_ds,
epochs=epochs
)
运行结果:
Epoch 1/50
215/215 [==============================] - 503s 2s/step - loss: 0.7688 - accuracy: 0.6126 - val_loss: 0.8110 - val_accuracy: 0.5748
Epoch 2/50
215/215 [==============================] - 490s 2s/step - loss: 0.6243 - accuracy: 0.6663 - val_loss: 0.6839 - val_accuracy: 0.6565
Epoch 3/50
215/215 [==============================] - 502s 2s/step - loss: 0.5891 - accuracy: 0.6890 - val_loss: 0.7193 - val_accuracy: 0.6028
Epoch 4/50
215/215 [==============================] - 494s 2s/step - loss: 0.5257 - accuracy: 0.7410 - val_loss: 0.5903 - val_accuracy: 0.6425
Epoch 5/50
215/215 [==============================] - 500s 2s/step - loss: 0.4632 - accuracy: 0.7847 - val_loss: 1.7842 - val_accuracy: 0.5000
Epoch 6/50
215/215 [==============================] - 495s 2s/step - loss: 0.4211 - accuracy: 0.8104 - val_loss: 0.4504 - val_accuracy: 0.7991
Epoch 7/50
215/215 [==============================] - 518s 2s/step - loss: 0.3487 - accuracy: 0.8582 - val_loss: 0.6944 - val_accuracy: 0.6846
Epoch 8/50
215/215 [==============================] - 509s 2s/step - loss: 0.3126 - accuracy: 0.8641 - val_loss: 0.8508 - val_accuracy: 0.6308
Epoch 9/50
215/215 [==============================] - 501s 2s/step - loss: 0.2512 - accuracy: 0.8985 - val_loss: 0.4950 - val_accuracy: 0.7687
Epoch 10/50
215/215 [==============================] - 500s 2s/step - loss: 0.2143 - accuracy: 0.9166 - val_loss: 0.4202 - val_accuracy: 0.8294
Epoch 11/50
215/215 [==============================] - 492s 2s/step - loss: 0.2079 - accuracy: 0.9131 - val_loss: 0.5640 - val_accuracy: 0.8131
Epoch 12/50
215/215 [==============================] - 484s 2s/step - loss: 0.1664 - accuracy: 0.9347 - val_loss: 0.4795 - val_accuracy: 0.8505
Epoch 13/50
215/215 [==============================] - 485s 2s/step - loss: 0.1393 - accuracy: 0.9492 - val_loss: 0.7909 - val_accuracy: 0.7523
Epoch 14/50
215/215 [==============================] - 485s 2s/step - loss: 0.1140 - accuracy: 0.9586 - val_loss: 0.4867 - val_accuracy: 0.8107
Epoch 15/50
215/215 [==============================] - 485s 2s/step - loss: 0.1082 - accuracy: 0.9603 - val_loss: 0.7982 - val_accuracy: 0.7710
Epoch 16/50
215/215 [==============================] - 485s 2s/step - loss: 0.1145 - accuracy: 0.9603 - val_loss: 1.1198 - val_accuracy: 0.7313
Epoch 17/50
215/215 [==============================] - 486s 2s/step - loss: 0.1114 - accuracy: 0.9580 - val_loss: 1.0163 - val_accuracy: 0.7710
Epoch 18/50
215/215 [==============================] - 486s 2s/step - loss: 0.0773 - accuracy: 0.9732 - val_loss: 0.5634 - val_accuracy: 0.8224
Epoch 19/50
215/215 [==============================] - 486s 2s/step - loss: 0.0346 - accuracy: 0.9918 - val_loss: 0.3425 - val_accuracy: 0.9042
Epoch 20/50
215/215 [==============================] - 485s 2s/step - loss: 0.0222 - accuracy: 0.9959 - val_loss: 0.5660 - val_accuracy: 0.8575
Epoch 21/50
215/215 [==============================] - 486s 2s/step - loss: 0.0870 - accuracy: 0.9702 - val_loss: 1.0307 - val_accuracy: 0.6916
Epoch 22/50
215/215 [==============================] - 487s 2s/step - loss: 0.1677 - accuracy: 0.9364 - val_loss: 0.5578 - val_accuracy: 0.8411
Epoch 23/50
215/215 [==============================] - 487s 2s/step - loss: 0.0665 - accuracy: 0.9772 - val_loss: 0.3254 - val_accuracy: 0.9136
Epoch 24/50
215/215 [==============================] - 487s 2s/step - loss: 0.0269 - accuracy: 0.9930 - val_loss: 0.4259 - val_accuracy: 0.8902
Epoch 25/50
215/215 [==============================] - 487s 2s/step - loss: 0.0170 - accuracy: 0.9947 - val_loss: 0.7115 - val_accuracy: 0.8341
Epoch 26/50
215/215 [==============================] - 486s 2s/step - loss: 0.0301 - accuracy: 0.9912 - val_loss: 0.6561 - val_accuracy: 0.8598
Epoch 27/50
215/215 [==============================] - 486s 2s/step - loss: 0.0815 - accuracy: 0.9726 - val_loss: 0.5297 - val_accuracy: 0.8411
Epoch 28/50
215/215 [==============================] - 487s 2s/step - loss: 0.1141 - accuracy: 0.9562 - val_loss: 0.4161 - val_accuracy: 0.8879
Epoch 29/50
215/215 [==============================] - 487s 2s/step - loss: 0.0538 - accuracy: 0.9831 - val_loss: 0.5807 - val_accuracy: 0.8621
Epoch 30/50
215/215 [==============================] - 487s 2s/step - loss: 0.0309 - accuracy: 0.9907 - val_loss: 0.3173 - val_accuracy: 0.9112
Epoch 31/50
215/215 [==============================] - 486s 2s/step - loss: 0.0164 - accuracy: 0.9971 - val_loss: 0.5159 - val_accuracy: 0.8388
Epoch 32/50
215/215 [==============================] - 486s 2s/step - loss: 0.0107 - accuracy: 0.9982 - val_loss: 0.3212 - val_accuracy: 0.9136
Epoch 33/50
215/215 [==============================] - 486s 2s/step - loss: 0.0031 - accuracy: 1.0000 - val_loss: 0.3175 - val_accuracy: 0.9229
Epoch 34/50
215/215 [==============================] - 486s 2s/step - loss: 0.0017 - accuracy: 1.0000 - val_loss: 0.2970 - val_accuracy: 0.9299
Epoch 35/50
215/215 [==============================] - 486s 2s/step - loss: 0.0015 - accuracy: 1.0000 - val_loss: 0.2981 - val_accuracy: 0.9252
Epoch 36/50
215/215 [==============================] - 487s 2s/step - loss: 0.0010 - accuracy: 1.0000 - val_loss: 0.3011 - val_accuracy: 0.9346
Epoch 37/50
215/215 [==============================] - 487s 2s/step - loss: 7.4752e-04 - accuracy: 1.0000 - val_loss: 0.3011 - val_accuracy: 0.9252
Epoch 38/50
215/215 [==============================] - 488s 2s/step - loss: 6.3077e-04 - accuracy: 1.0000 - val_loss: 0.2992 - val_accuracy: 0.9299
Epoch 39/50
215/215 [==============================] - 487s 2s/step - loss: 5.4920e-04 - accuracy: 1.0000 - val_loss: 0.3016 - val_accuracy: 0.9322
Epoch 40/50
215/215 [==============================] - 487s 2s/step - loss: 4.8682e-04 - accuracy: 1.0000 - val_loss: 0.3070 - val_accuracy: 0.9299
Epoch 41/50
215/215 [==============================] - 487s 2s/step - loss: 4.2537e-04 - accuracy: 1.0000 - val_loss: 0.3102 - val_accuracy: 0.9322
Epoch 42/50
215/215 [==============================] - 486s 2s/step - loss: 3.8510e-04 - accuracy: 1.0000 - val_loss: 0.3175 - val_accuracy: 0.9346
Epoch 43/50
215/215 [==============================] - 487s 2s/step - loss: 3.3861e-04 - accuracy: 1.0000 - val_loss: 0.3138 - val_accuracy: 0.9346
Epoch 44/50
215/215 [==============================] - 487s 2s/step - loss: 3.0387e-04 - accuracy: 1.0000 - val_loss: 0.3171 - val_accuracy: 0.9346
Epoch 45/50
215/215 [==============================] - 487s 2s/step - loss: 2.7470e-04 - accuracy: 1.0000 - val_loss: 0.3214 - val_accuracy: 0.9346
Epoch 46/50
215/215 [==============================] - 487s 2s/step - loss: 2.4226e-04 - accuracy: 1.0000 - val_loss: 0.3259 - val_accuracy: 0.9346
Epoch 47/50
215/215 [==============================] - 487s 2s/step - loss: 2.1893e-04 - accuracy: 1.0000 - val_loss: 0.3270 - val_accuracy: 0.9346
Epoch 48/50
215/215 [==============================] - 487s 2s/step - loss: 1.9667e-04 - accuracy: 1.0000 - val_loss: 0.3309 - val_accuracy: 0.9346
Epoch 49/50
215/215 [==============================] - 487s 2s/step - loss: 1.7684e-04 - accuracy: 1.0000 - val_loss: 0.3371 - val_accuracy: 0.9299
Epoch 50/50
215/215 [==============================] - 487s 2s/step - loss: 1.6318e-04 - accuracy: 1.0000 - val_loss: 0.3378 - val_accuracy: 0.9322
六、模型评估
acc=history.history['accuracy']
val_acc=history.history['val_accuracy']
loss=history.history['loss']
val_loss=history.history['val_loss']
epochs_range=range(epochs)
plt.figure(figsize=(12,4))
plt.suptitle("OreoCC")
plt.subplot(1,2,1)
plt.plot(epochs_range,acc,label='Training Accuracy')
plt.plot(epochs_range,val_acc,label='Validation Accuracy')
plt.legend(loc='lower right')
plt.title('Training and Validation Accuracy')
plt.subplot(1,2,2)
plt.plot(epochs_range,loss,label='Training Loss')
plt.plot(epochs_range,val_loss,label='Validation Loss')
plt.legend(loc='upper right')
plt.title('Training and Validation Loss')
plt.show()
运行结果:
七、预测
import numpy as np
#采用加载的模型(new_model)来看预测结果
plt.figure(figsize=(10,5))
plt.suptitle("OreoCC")
for images,labels in val_ds.take(1):
for i in range(8):
ax=plt.subplot(2,4,i+1)
#显示图片
plt.imshow(images[i].numpy().astype("uint8"))
#需要给图片增加一个维度
img_array=tf.expand_dims(images[i],0)
#使用模型预测图片中的人物
predictions=model.predict(img_array)
plt.title(classNames[np.argmax(predictions)])
plt.axis("off")
运行结果:
八、心得体会
模型整体结果没有达到十分满意的程度,应该再次调整模型参数,但由于自身TensorFlow为CPU模式的缘故,时间耗费太久,留待以后再次进行修正。