python识别简单训练模型_使用已经得到的keras模型识别自己手写的数字方式
環(huán)境:Python+keras,后端為Tensorflow
訓(xùn)練集:MNIST
對于如何訓(xùn)練一個識別手寫數(shù)字的神經(jīng)網(wǎng)絡(luò),網(wǎng)上資源十分豐富,并且能達到相當高的精度。但是很少有人涉及到如何將圖片輸入到網(wǎng)絡(luò)中并讓已經(jīng)訓(xùn)練好的模型驚醒識別,下面來說說實現(xiàn)方法及注意事項。
首先import相關(guān)庫,這里就不說了。
然后需要將訓(xùn)練好的模型導(dǎo)入,可通過該語句實現(xiàn):
model = load_model('cnn_model_2.h5') (cnn_model_2.h5替換為你的模型名)
之后是導(dǎo)入圖片,需要的格式為28*28。可用opencv導(dǎo)入:
img = cv2.imread('temp3.png', 0) (temp3.png替換為你手寫的圖片)
然后reshape一下以符合模型的輸入要求:
img = (img.reshape(1,1,28,28)).astype("float32")/255
之后就可以用模型識別了:
predict = model.predict_classes(img)
最后print一下predict即可。
下面劃重點:因為MNIST使用的是黑底白字的圖片,所以你自己手寫數(shù)字的時候一定要注意把得到的圖片也改成黑底白字的,否則會識別錯(至少我得到的結(jié)論是這樣的 ,之前用白底黑字的圖總是識別出錯)
源碼一覽:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import cv2
import numpy as np
from keras.modelsimport load_model
model= load_model('cnn_model_2.h5')
image= cv2.imread('temp3.png',0)
img= cv2.imread('temp3.png',0)
img= (img.reshape(1,1,28,28)).astype("float32")/255
predict= model.predict_classes(img)
print ('識別為:')
print (predict)
cv2.imshow("Image1", image)
cv2.waitKey(0)
效果圖:
補充知識:keras編寫自定義的層
寫在前面的話
keras已經(jīng)有很多封裝好的庫供我們調(diào)用,但是有些時候我們需要的操作keras并沒有,這時就需要學會自定義keras層了
1.Lambda
這個東西很方便,但是只能完成簡單、無狀態(tài)的自定義操作,而不能建立含有可訓(xùn)練權(quán)重的自定義層。
1
2
3
4
5
6
7
8
9
from keras.layersimport Input,Lambda
from kerasimport Model
import tensorflow as tf
input=Input(shape=(224,224,3))
input.shape#Input第一個維度為batchsize維度
output=Lambda(lambda x: x[...,1])(input)#取最后一個維度的數(shù)據(jù),...表示前面所有的維度
Model=Model(inputs=input,outputs=output)
Model.output
2.keras_custom
學習自keras中文文檔
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
2.自定義keras層(帶有可訓(xùn)練權(quán)重)
① build:定義權(quán)重,且self.build=True,可以通過迪奧喲經(jīng)super([layer],self).build()完成
② call:功能邏輯實現(xiàn)
③ compute_output_shape:計算輸出張量的shape
import keras.backend as K
from keras.engine.topologyimport Layer#這里的Layer是一個父類,下面的MyLayer將會繼承Layer
class MyLayer(Layer):#自定義一個keras層類
def __init__(self,output_dim,**kwargs):#初始化方法
self.output_dim=output_dim
super(MyLayer,self).__init__(**kwargs)#必須要的初始化自定義層
def build(self,input_shape):#為Mylayer建立一個可訓(xùn)練的權(quán)重
#通過add_weight的形式來為Mylayer創(chuàng)建權(quán)重矩陣
self.kernel=self.add_weight(name='kernel',
shape=(input_shape[1],self.output_dim),#這里就是建立一個shape大小的權(quán)重矩陣
initializer='uniform',
trainable=True)
super(MyLayer,self).build(input_shape)#一定要用,也可以用下面一行
#self.build=True
def call(self,x):#call函數(shù)里就是定義了對x張量的計算圖,且x只是一個形式,所以不能被事先定義
return K.dot(x,self.kernel)#矩陣乘法
def compute_output_shape(self,input_shape):
return (input_shape[0],self.output_dim)#這里是自己手動計算出來的output_shape
--------------------------------------------------------------------------------
class Mylayer(Layer):
def __init__(self,output_dim,**kwargs):
self.output_dim=output_dim
super(MyLayer,self).__init__(**kwargs)
def build(self,input_shape):
assert isinstance(input_shape,list)#判斷input_shape是否是list類型的
self.kernel=self.add_weight(name='kernel',
shape=(input_shape[0][1],self.output_dim),#input_shape應(yīng)該長得像[(2,2),(3,3)]
initializer='uniform',
trainable=True)
super(MyLayer,self).build(input_shape)
def call(self,x):
assert isinstance(x,list)
a,b=x#從這里可以看出x應(yīng)該是一個類似[(2,2),(3,3)]的list,a=(2,2),b=(3,3)
return [K.dot(a,self.kernel)+b,K.mean(b,axis=-1)]
以上這篇使用已經(jīng)得到的keras模型識別自己手寫的數(shù)字方式就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://blog.csdn.net/baidu_35113561/article/details/79371716
總結(jié)
以上是生活随笔為你收集整理的python识别简单训练模型_使用已经得到的keras模型识别自己手写的数字方式的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 程序人生:教你写出让同事抓狂的代码
- 下一篇: 如何设置无线网络中计算机的ip,电脑wi