模型结构
代码中kernel_size=3
代码
import tensorflow as tf
import os
import datetime
from tensorflow.keras import Model
from tensorflow.keras import layers
cifar10=tf.keras.datasets.cifar10
(x_train,y_train),(x_test,y_test)=cifar10.load_data()
x_train=x_train/255.0
x_test=x_test/255.0
class VGGNet(Model):
def __init__(self):
super().__init__()
self.input_layer=layers.Conv2D(filters=64,kernel_size=(3,3),strides=1,padding='same',activation='relu')
self.middle_layers=[layers.BatchNormalization(),
layers.Conv2D(filters=64,kernel_size=(3,3),strides=1,padding='same',activation='relu'),
layers.BatchNormalization(),
layers.MaxPool2D(pool_size=(2,2),strides=2),
layers.Dropout(0.2),
layers.Conv2D(filters=128,kernel_size=(3,3),strides=1,padding='same',activation='relu'),
layers.BatchNormalization(),
layers.Conv2D(filters=128,kernel_size=(3,3),strides=1,padding='same',activation='relu'),
layers.BatchNormalization(),
layers.MaxPool2D(pool_size=(2,2),strides=2),
layers.Dropout(0.2),
layers.Conv2D(filters=256,kernel_size=(3,3),strides=1,padding='same',activation='relu'),
layers.BatchNormalization(),
layers.Conv2D(filters=256,kernel_size=(3,3),strides=1,padding='same',activation='relu'),
layers.BatchNormalization(),
layers.Conv2D(filters=256,kernel_size=(3,3),strides=1,padding='same',activation='relu'),
layers.BatchNormalization(),
layers.MaxPool2D(pool_size=(2,2),strides=2),
layers.Dropout(0.2),
layers.Conv2D(filters=512,kernel_size=(3,3),strides=1,padding='same',activation='relu'),
layers.BatchNormalization(),
layers.Conv2D(filters=512,kernel_size=(3,3),strides=1,padding='same',activation='relu'),
layers.BatchNormalization(),
layers.Conv2D(filters=512,kernel_size=(3,3),strides=1,padding='same',activation='relu'),
layers.BatchNormalization(),
layers.MaxPool2D(pool_size=(2,2),strides=2),
layers.Dropout(0.2),
layers.Conv2D(filters=512,kernel_size=(3,3),strides=1,padding='same',activation='relu'),
layers.BatchNormalization(),
layers.Conv2D(filters=512,kernel_size=(3,3),strides=1,padding='same',activation='relu'),
layers.BatchNormalization(),
layers.Conv2D(filters=512,kernel_size=(3,3),strides=1,padding='same',activation='relu'),
layers.BatchNormalization(),
layers.MaxPool2D(pool_size=(2,2),strides=2),
layers.Dropout(0.2),
layers.Flatten(),
layers.Dense(512,activation='relu'),
layers.Dropout(0.2),
layers.Dense(512,activation='relu'),
layers.Dropout(0.2)]
self.output_layer=layers.Dense(10,activation='softmax')
def call(self,x):
x=self.input_layer(x)
for layer in self.middle_layers:
x=layer(x)
y=self.output_layer(x)
return x
model=VGGNet()
##模型编译
model.compile(optimizer="adam",loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),metrics=['sparse_categorical_accuracy'])
log_dir = "logs/fit/" + datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
tensorboard_callback = tf.keras.callbacks.TensorBoard(log_dir=log_dir, histogram_freq=1)
##训练
history=model.fit(x_train,y_train,batch_size=32,epochs=5,validation_data=(x_test,y_test),validation_freq=1,callbacks=[tensorboard_callback])
##保存模型
weights_path="./weights/"
if os.path.exists(weights_path)==False:
model.save_weights(weights_path)