cv-lenet5
作者:互联网
实现
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import datasets,layers,models
import matplotlib.pyplot as plt
(train_images,train_labels),(test_images,test_labels) = datasets.cifar10.load_data()
#normalize
train_images,test_images=train_images/255.0,test_images/255.0
#verify Data
class_names=['airplane','automobile','bird','cat','deer','dog','frog','horse','ship','truck']
plt.figure(figsize=(10,10))
for i in range(25):
plt.subplot(5,5,i+1)
plt.xticks([])
plt.yticks([])
plt.grid(False)
plt.imshow(train_images[i],cmap=plt.cm.binary)
plt.xlabel(class_names[train_labels[i][0]])
plt.show
#变换label为1-hot编码
train_labels=keras.utils.to_categorical(train_labels,10)
test_labels=keras.utils.to_categorical(test_labels,10)
train_images[0].shape
train_labels.shape
lenet_input = keras.Input(shape=(32,32,3),name='img')
x=layers.Conv2D(6,5,activation='relu')(lenet_input)
x=layers.MaxPooling2D(2)(x)
x=layers.Conv2D(16,5,activation='relu')(x)
x=layers.MaxPooling2D(2)(x)
#x=layers.Conv2D(120,5,activation='relu')(x)
x=layers.Flatten()(x)
x=layers.Dense(120,activation='relu')(x)
x=layers.Dense(84,activation='relu')(x)
lenet_output=layers.Dense(10,activation='softmax')(x)
lenet = keras.Model(lenet_input,lenet_output,name='lenet')
lenet.summary()
lenet.compile(optimizer='SGD',
loss='categorical_crossentropy',
metrics=['accuracy'])
history=lenet.fit(train_images,
train_labels,
#batch_size=128,
epochs=20,
verbose=1,
validation_data=(test_images,test_labels))
plt.figure()
plt.plot(history.history['acc'],label='accuracy')
plt.plot(history.history['val_acc'],label='val_accuracy')
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.ylim([0.1,1])
plt.legend(loc='lower right')
标签:layers,plt,lenet5,labels,train,lenet,images,cv 来源: https://www.cnblogs.com/zhouyu0-0/p/11741262.html