欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

使用 28x28 bmp測試訓練後的模型 tensorflow mnist jupyter notebook

发布时间:2024/1/8 编程问答 36 豆豆
生活随笔 收集整理的这篇文章主要介绍了 使用 28x28 bmp測試訓練後的模型 tensorflow mnist jupyter notebook 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

訓練CNN/mnist好後,用以下指令保存模型及weight
model.save("05.h5")

 

取回使用
model = load_model("05_cnn.h5")

關於BMP
24 bit bmp檔, 檔頭54 byte, 之後每個像素RGB佔用3 byte,
28x28像素 = 54 + 28x28x3 = 54+2352 = 2406 byte
用apt-get kolourpaint4可以產生bmp

 

 

讀取bmp時要注意2點
1.bmp是由下行往上行存,讀入圖檔要反向
2.bmp的白是255, mnist的圖,白是0

mnist test的第一個字,白部份是0

程式實測

jupyter note 範例如下:

from keras.datasets import mnist from keras.models import load_model from keras.utils import np_utils from keras.layers import Conv2D, MaxPooling2D from keras.layers import Dense, Activation, Flatten from keras.preprocessing.image import ImageDataGenerator import matplotlib.pyplot as plt import keras import tensorflow as tf import numpy as npdef plot_image(image):fig=plt.gcf()fig.set_size_inches(2,2)plt.imshow(image,cmap='binary')plt.show()model = load_model("05_cnn.h5")(X_train_image, y_train_label), (X_test_image, y_test_label) = mnist.load_data() print(X_train_image.shape) x_train = X_train_image.reshape(X_train_image.shape[0],28,28,1) print(x_train.shape) x_train = x_train.astype('float32') x_train /= 255 y_train = keras.utils.to_categorical(y_train_label, num_classes=10) x_test = X_test_image.reshape(X_test_image.shape[0], 28, 28, 1) x_test = x_test.astype('float32') x_test /= 255 y_test = keras.utils.to_categorical(y_test_label, num_classes=10)def draw_1_28_28_1(np_ary_1_28_28_1):for i2 in range(0,28) :st1=""for i3 in range(0,28) :if np_ary_1_28_28_1[0][i2][i3][0] == 0 :st1=st1+" "else:st1=st1+"*"print(st1) def prediction_one(test_id):x1 = x_test[test_id:(test_id+1)]print(x1.shape)draw_1_28_28_1(x1) plot_image(X_test_image[test_id]);prediction = model.predict(x1)answser=np.argmax(prediction[0])return answserfor i1 in range(0,1): print("id=",i1," p=",prediction_one(i1))import array as ary x3=np.zeros((28,28),dtype='float32') print(x3.shape)with open("./6.bmp","rb") as f_bmp1: byte_ary = f_bmp1.read(54) print(str(byte_ary[0:2]))for i1 in range(0,28):st1=""byte_ary = f_bmp1.read(28*3)x2=np.frombuffer(byte_ary,np.uint8,28*3,0)#x2=x2.dtype='float32'x2=x2.reshape(28,3)for i3 in range(0,28) :x3[28-i1-1][i3]=255-x2[i3][0] for i3 in range(0,28) :if x2[i3][0] == 0 :st1=st1+" "else:st1=st1+"#" print(i1,st1) for i2 in range(0,28) :st1=""for i3 in range(0,28) :if x3[i2][i3] == 0 :st1=st1+" "else:st1=st1+"*"print(st1)x5=x3.reshape(1,28,28,1) x5 /=255predi1 = model.predict(x5) print("prediction-"np.argmax(predi1[0]))

 

总结

以上是生活随笔为你收集整理的使用 28x28 bmp測試訓練後的模型 tensorflow mnist jupyter notebook的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。