Contents ...
udn網路城邦
使用 28x28 bmp測試訓練後的模型 tensorflow mnist
2019/03/06 16:19
瀏覽115
迴響0
推薦0
引用0
訓練CNN/mnist好後,用以下指令保存模型及weight

model.save("05.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



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 np




def 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 answser




for 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 /=255

    

predi1 = model.predict(x5)

print("prediction-"np.argmax(predi1[0]))

全站分類:心情隨筆 心情日記
自訂分類:tensorflow
上一則: 0513 TF
下一則: tensorflow GPU ubuntu 16 Cuda 安裝 gtx1060

限會員,要發表迴響,請先登入