[ML]记录一下训练的第一个人工智障-数字识别

正确率简直了 , 只能识别印刷体 , 手写基本不认识 无论如何还是应该发篇文章庆祝庆祝

import tensorflow as tf
from tensorflow.keras import layers
import numpy as np
from tensorflow_core.python.keras.datasets import mnist
from tensorflow_core.python.keras.utils.np_utils import to_categorical

print(tf.__version__)
print(tf.keras.__version__)
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()

model = tf.keras.Sequential()
model.add(layers.Dense(512, activation='relu', input_shape=(28 ,28)))
model.add(layers.Dense(10, activation='softmax'))
# model.compile(optimizer=tf.keras.optimizers.Adam(0.001),
#              loss=tf.keras.losses.categorical_crossentropy,
#              metrics=[tf.keras.metrics.categorical_accuracy])

model.compile(optimizer='rmsprop',
 loss='categorical_crossentropy',
 metrics=['accuracy'])
train_images1 = train_images.reshape((60000, 28 * 28))
train_images1 = train_images1.astype('float32') / 255
test_images1 = test_images.reshape((10000, 28 * 28))
test_images1 = test_images1.astype('float32') / 255
train_labels1 = to_categorical(train_labels)
test_labels1 = to_categorical(test_labels)
#
data = model.fit(train_images1, train_labels1, epochs=10, batch_size=100,validation_data=(test_images1, test_labels1))
model.save('E:\project\python\ml\digocr.hdf5')
import tensorflow as tf
from keras_preprocessing.image import load_img
from tensorflow.keras import layers
import numpy as np
from tensorflow_core.python.keras.datasets import mnist
from tensorflow_core.python.keras.models import load_model
from tensorflow_core.python.keras.utils.np_utils import to_categorical
import cv2 as cv
from fucntion import reverse

(train_images, train_labels), (test_images, test_labels) = mnist.load_data()
model = load_model('digocr.hdf5')
# print(model)

train_images1 = train_images.reshape((60000, 28 * 28))
train_images1 = train_images1.astype('float32') / 255
test_images1 = test_images.reshape((10000, 28 * 28))
test_images1 = test_images1.astype('float32') / 255
train_labels1 = to_categorical(train_labels)
test_labels1 = to_categorical(test_labels)

img = cv.imread('data/8.png')
print(img)
img = cv.resize(img, (28, 28), interpolation=cv.INTER_CUBIC)
img_gray = cv.cvtColor(img, cv.COLOR_RGB2GRAY)
img_gray = reverse(img_gray)
data = np.ndarray((1,28,28))
data[0] = img_gray
data = data.reshape((1,28*28))
data = data.astype('float32')/255
# print(img_gray)
res = model.predict(data)
# print(res)
# cv.imshow("ps", data[0])
# # cv.waitKey()
# res = model.predict()
ret = []

# np.set_printoptions(suppress=True)

for i in range(len(res)):
    # print('第{ {}'.format(i,res[i]))
    temp = res[i].argmax()
    print('识别结果:',temp)
# cv.imshow("ps", test_images[10000-2])
# cv.imshow("ps", data[0])
# cv.waitKey()
 # ret.append(temp)
# print(res)
# print(test_labels.tolist())
# print(ret)

本条目发表于2020年07 月25日。属于MachineLearning分类

  • 评论列表