代码托管在github
https://github.com/sofiathefirst/AIcode
代码
model = tf.keras.models.Sequential([
tf.keras.layers.Flatten()),
tf.keras.layers.Dense(512, activation=tf.nn.relu),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(10, activation=tf.nn.softmax)
])
。。。。。。
model.save('epic_num_reader.model')
NotImplementedError: Currently `save` requires model to be a graph network. Consider using `save_weights`, in order to save the weights of the model
解决方法:以mnist 为例,input_shape是数据实例的维数
model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(input_shape=(28,28)),
tf.keras.layers.Dense(512, activation=tf.nn.relu),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(10, activation=tf.nn.softmax)
])
OK了。。
model.save('/home/q/my_minis.h5')# save model
new_model = keras.models.load_model('/home/q/my_minis.h5')
#!/usr/bin/env python
# -*- coding:UTF-8 -*-
import tensorflow as tf
import scipy.misc
import matplotlib
mnist = tf.keras.datasets.mnist
(x_train, y_train),(x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
import cv2
#imread(filename[, flags]) -> retval
imgPath = '/home/q/Desktop/3.jpg' #图片路径
#默认读取的是RGB三色图,得到三维矩阵
img = cv2.imread(imgPath,cv2.IMREAD_GRAYSCALE)
cv2.imshow(str(y_test[1]),x_test[1,:,:])
cv2.waitKey(1)
cv2.imshow(str(y_test[3]),x_test[3,:,:])
cv2.waitKey(1)
cv2.destroyAllWindows()
temp = cv2.imread('/home/q/Desktop/8.jpg',cv2.IMREAD_GRAYSCALE)
#等价于img = cv2.imread('test01.jpg',cv2.IMREAD_COLOR)
#查看图像维数(719,1280,3)
print(img)
scipy.misc.imsave('/home/q/Desktop/n2.png',x_test[1,:,:])
model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(input_shape=(28,28)),
tf.keras.layers.Dense(512, activation=tf.nn.relu),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(10, activation=tf.nn.softmax)
])
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
model.fit(x_train, y_train, epochs=5)
model.save('/home/q/my_minis.h5')
temp = cv2.imread('/home/q/Desktop/n5.png',cv2.IMREAD_GRAYSCALE)
temp = 1-temp/255.0
t3 = temp.reshape(1,28,28)
print('n5.png is:',model.predict_classes(t3,1)[0])
new_model = keras.models.load_model('/home/q/my_minis.h5')
temp = cv2.imread('/home/q/Desktop/n7.png',cv2.IMREAD_GRAYSCALE)
temp = 1-temp/255.0
t3 = temp.reshape(1,28,28)
new_model.predict_classes(t3,1)[0]