AlexNet实现cifar10数据集分类
import tensorflow as tf import os from matplotlib import pyplot as plt import tensorflow.keras.datasets from tensorflow.keras import Model import numpy as np from tensorflow.keras.layers import Dense,Flatten,BatchNormalization,Dropout,Conv2D,Activation,MaxPool2D cifar10=tf.keras.datasets.cifar10 (x_train,y_train),(x_test,y_test)=cifar10.load_data() x_train=x_train/255. x_test=x_test/255. class AlexNet(Model): def __init__(self): super(AlexNet, self).__init__() self.c1=Conv2D(filters=96,kernel_size=(3,3),strides=1,padding='valid') self.b1=BatchNormalization() self.a1=Activation('relu') self.p1=MaxPool2D(pool_size=(3,3),strides=2) self.c2 = Conv2D(filters=384, kernel_size=(3, 3), strides=1, padding='same') #self.b2 = BatchNormalization() self.a2 = Activation('relu') #self.p2 = MaxPool2D(pool_size=(3, 3), strides=2) self.c3 = Conv2D(filters=256, kernel_size=(3, 3), strides=1, padding='same') # self.b2 = BatchNormalization() self.a3 = Activation('relu') self.p3 = MaxPool2D(pool_size=(3, 3), strides=2) self.flatten=Flatten() self.f1 = Dense(2048,activation='relu') self.d1=Dropout(0.5) self.f2 = Dense(2048, activation='relu') self.d2 = Dropout(0.5) self.f3 = Dense(10, activation='softmax') def call(self,x): x = self.c1(x) x = self.b1(x) x = self.a1(x) x = self.p1(x) x = self.c2(x) x = self.a2(x) x = self.c3(x) x = self.a3(x) x = self.p3(x) x = self.flatten(x) x=self.f1(x) x=self.d1(x) x=self.f2(x) x=self.d2(x) y=self.f3(x) return y model=AlexNet() model.compile(optimizer='adam', loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False), metrics=['sparse_categorical_accuracy']) check_save_path='./checkpoint/AlexNet.ckpt' if os.path.exists(check_save_path+'.index'): print('-------------lodel the model------------') model.load_weights(check_save_path) cp_callback=tf.keras.callbacks.ModelCheckpoint(filepath=check_save_path,save_best_only=True, save_weights_only=True) history=model.fit(x_train,y_train,batch_size=128,epochs=5,validation_data=(x_test,y_test), validation_freq=1,callbacks=[cp_callback]) model.summary() file=open('./AlexNet_wights.txt','w') for v in model.trainable_variables: file.write(str(v.name) + '\n') file.write(str(v.shape) + '\n') file.write(str(v.np()) + '\n') file.close() ############可视化图像############### acc=history.history['sparse_categorical_accuracy'] val_acc=history.history['sparse_categorical_val_accuracy'] loss=history.history['loss'] val_loss=history.history['val_loss'] plt.subplot(1,2,1) plt.plot(acc) plt.plot(val_acc) plt.legend() plt.subplot(1,2,2) plt.plot(loss) plt.plot(val_loss) plt.legend() plt.show()
此代码运行较慢,单次遍历需要近15分钟,由此可见两层全连接层2048个神经元远远拖慢运行速度