码迷,mamicode.com
首页 > Web开发 > 详细

cv-lenet5

时间:2019-10-25 23:19:23      阅读:243      评论:0      收藏:0      [点我收藏+]

标签:lse   img   pil   name   com   import   batch   model   sgd   

实现


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')

cv-lenet5

标签:lse   img   pil   name   com   import   batch   model   sgd   

原文地址:https://www.cnblogs.com/zhouyu0-0/p/11741262.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!