Tensorflow实现CIFAR10分类并保存模型

from tensorflow.keras.datasets import cifar10
from tensorflow.python.keras.utils import np_utils
from tensorflow.keras.models import Sequential
from tensorflow.python.keras.layers.core import Dense, Dropout, Activation, Flatten
from tensorflow.python.keras.layers.convolutional import Conv2D, MaxPooling2D
from tensorflow.keras.optimizers import SGD, Adam, RMSprop
from tensorflow.keras.preprocessing.image import ImageDataGenerator

IMG_CHANNEL = 3
IMG_WIDTH = 32
IMG_HEIGHT = 32
BATCH_SIZE = 128
NB_EPOCH = 100
NB_CLASSES = 10
VERBOSE = 1
VALIDATTION_SPLIT = 0.2
OPTIM = RMSprop()

(x_train, y_train), (x_test, y_test) = cifar10.load_data()
print('x_train shape:', x_train.shape)
print('x_test shape:',x_test.shape)
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')

# convert to categorical
y_train = np_utils.to_categorical(y_train, NB_CLASSES)
y_test = np_utils.to_categorical(y_test, NB_CLASSES)

# normalization
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255

# net
model = Sequential()
model.add(Conv2D(32, kernel_size=3, padding='same',
                 input_shape=(IMG_WIDTH, IMG_HEIGHT, IMG_CHANNEL)))
model.add(Activation('relu'))
model.add(Conv2D(32, kernel_size=3, padding='same'))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Conv2D(64, kernel_size=3, padding='same'))
model.add(Activation('relu'))
model.add(Conv2D(64, 3, 3))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(512))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(NB_CLASSES))
model.add(Activation('softmax'))

model.summary()

# 编译
model.compile(loss='categorical_crossentropy',
              optimizer=OPTIM,
              metrics=['accuracy'])

# train
history = model.fit(x_train, y_train, batch_size=BATCH_SIZE,
                    epochs=NB_EPOCH, verbose=VERBOSE,
                    validation_split=VALIDATTION_SPLIT)

print('testing...')

score = model.evaluate(x_test, y_test, batch_size=BATCH_SIZE,
                       verbose=VERBOSE)
print("\nTest score:", score[0])
print('Test accuracy:', score[1])

# save model
model_json = model.to_json()
open('model/cifar10_architecture.json', 'w').write(model_json)
model.save_weights('./model/cifar10_weights.h5', overwrite=True)


上一篇:python-安装tensorflow


下一篇:树莓派3 搭建tensorflow并进行物体识别