接觸深度學(xué)習(xí)3個(gè)月以來,從當(dāng)初的小白零基礎(chǔ)學(xué)習(xí),過程十分艱苦,看了幾章大牛 YoshuaBengio 寫的deep learning一書,粗略了解了基本常用的神經(jīng)網(wǎng)絡(luò)以及梯度更新策略,參數(shù)優(yōu)化,也了解以及簡單的使用常用的深度學(xué)習(xí)開發(fā)框架caffe,tensorflow,theano,sklearn機(jī)器學(xué)習(xí)庫,目前keras比較火,所以使用keras來簡單的進(jìn)行圖片識別分類。
數(shù)據(jù)集準(zhǔn)備:
看caffe博客的時(shí)候看到的數(shù)據(jù)集,然后就下載使用,數(shù)據(jù)集可以在最后下載。
數(shù)據(jù)集一共有5類圖片,一共500張,每類圖片100張,訓(xùn)練集400張,每類80張,測試集100張,每類20張
第一步:
數(shù)據(jù)集進(jìn)行處理:
使用opencv對圖片進(jìn)行處理,縮放圖片大小為128×128大小,通道為單通道灰度圖像
#coding:utf8
import os
import cv2.cv as cv
import cv2
# 遍歷指定目錄,顯示目錄下的所有文件名
width_scale = 192 #縮放尺寸寬度
height_scale = 128#縮放尺寸高度
write_path = "/home/zhanghao/data/classification/test_scale/"#要寫入的圖片路徑
#遍歷每一張圖片進(jìn)行處理
def eachFile(filepath):
pathDir = os.listdir(filepath)
for allDir in pathDir:
child = os.path.join('%s%s' % (filepath,allDir))
write_child = os.path.join('%s%s' % (write_path,allDir))
image = cv.LoadImage(child,0)
des_image = cv.CreateImage((width_scale,height_scale),image.depth,1)
cv.Resize(image,des_image,cv2.INTER_AREA)
# cv.ShowImage('afe',des_image)
cv.SaveImage(write_child,des_image)
# break
if __name__ == '__main__':
filePathC = "/home/zhanghao/data/classification/test/"
eachFile(filePathC)
第二步
把圖片集制作成keras識別的數(shù)據(jù)集
#制作數(shù)據(jù)集
def data_label(path,count):
data = np.empty((count,1,128,192),dtype = 'float32')#建立空的四維張量類型32位浮點(diǎn)
label = np.empty((count,),dtype = 'uint8')
i = 0
pathDir = os.listdir(path)
for each_image in pathDir:
all_path = os.path.join('%s%s' % (path,each_image))#路徑進(jìn)行連接
image = cv2.imread(all_path,0)
mul_num = re.findall(r"d",all_path)#尋找字符串中的數(shù)字,由于圖像命名為300.jpg 標(biāo)簽設(shè)置為0
num = int(mul_num[0])-3
# print num,each_image
# cv2.imshow("fad",image)
# print child
array = np.asarray(image,dtype='float32')
data[i,:,:,:] = array
label[i] = int(num)
i += 1
return data,label
第三步
構(gòu)建卷積神經(jīng)網(wǎng)絡(luò)進(jìn)行訓(xùn)練和測試
#構(gòu)建卷積神經(jīng)網(wǎng)絡(luò)
def cnn_model(train_data,train_label,test_data,test_label):
model = Sequential()
model.add(Convolution2D(
nb_filter = 12,
nb_row = 3,
nb_col = 3,
border_mode = 'valid',
dim_ordering = 'th',
input_shape = (1,128,192)))
model.add(Activation('relu'))#激活函數(shù)使用修正線性單元
model.add(MaxPooling2D(
pool_size = (2,2),
strides = (2,2),
border_mode = 'valid'))
model.add(Convolution2D(
24,
3,
3,
border_mode = 'valid',
dim_ordering = 'th'))
model.add(Activation('relu'))
#池化層 24×29×29
model.add(MaxPooling2D(
pool_size = (2,2),
strides = (2,2),
border_mode = 'valid'))
model.add(Convolution2D(
48,
3,
3,
border_mode = 'valid',
dim_ordering = 'th'))
model.add(Activation('relu'))
model.add(MaxPooling2D(
pool_size = (2,2),
strides =(2,2),
border_mode = 'valid'))
model.add(Flatten())
model.add(Dense(20))
model.add(Activation(LeakyReLU(0.3)))
model.add(Dropout(0.5))
model.add(Dense(20))
model.add(Activation(LeakyReLU(0.3)))
model.add(Dropout(0.4))
model.add(Dense(5,init = 'normal'))
model.add(Activation('softmax'))
adam = Adam(lr = 0.001)
model.compile(optimizer = adam,
loss = 'categorical_crossentropy',
metrics = ['accuracy'])
print '----------------training-----------------------'
model.fit(train_data,train_label,batch_size = 12,nb_epoch = 35,shuffle = True,show_accuracy = True,validation_split = 0.1)
print '----------------testing------------------------'
loss,accuracy = model.evaluate(test_data,test_label)
print ' test loss:',loss
print ' test accuracy',accuracy
第四步
調(diào)用上述函數(shù)進(jìn)行訓(xùn)練預(yù)測
train_path = '/home/zhanghao/data/classification/train_scale/'
test_path = '/home/zhanghao/data/classification/test_scale/'
train_count = getnum(train_path)
test_count = getnum(test_path)
train_data,train_label = data_label(train_path,train_count)
test_data,test_label = data_label(test_path,test_count)
train_label = np_utils.to_categorical(train_label,nb_classes = 5)
test_label = np_utils.to_categorical(test_label,nb_classes = 5)
cnn_model(train_data,train_label,test_data,test_label)
用到的頭文件
import re
import cv2
import os
import numpy as np
import cv2.cv as cv
from keras.models import Sequential
from keras.layers.core import Dense,Activation,Flatten
from keras.layers.convolutional import Convolution2D,MaxPooling2D
from keras.optimizers import Adam
from keras.layers.advanced_activations import LeakyReLU
from keras.utils import np_utils
cpu運(yùn)行,迭代50次,預(yù)測準(zhǔn)確率達(dá)到92%,還算可以的準(zhǔn)確率
具體實(shí)現(xiàn)代碼:https://github.com/zhanghao-JNU/keras-training
路漫漫其修遠(yuǎn)兮,吾將上下而求索
責(zé)任編輯:lq
-
神經(jīng)網(wǎng)絡(luò)
+關(guān)注
關(guān)注
42文章
4814瀏覽量
103639 -
數(shù)據(jù)集
+關(guān)注
關(guān)注
4文章
1224瀏覽量
25449 -
深度學(xué)習(xí)
+關(guān)注
關(guān)注
73文章
5561瀏覽量
122799
原文標(biāo)題:使用CNN神經(jīng)網(wǎng)絡(luò)進(jìn)行圖片識別分類
文章出處:【微信號:vision263com,微信公眾號:新機(jī)器視覺】歡迎添加關(guān)注!文章轉(zhuǎn)載請注明出處。
發(fā)布評論請先 登錄
【嘉楠堪智K230開發(fā)板試用體驗(yàn)】01 Studio K230開發(fā)板Test2——手掌,手勢檢測,字符檢測
基于LockAI視覺識別模塊:手寫數(shù)字識別
在友晶LabCloud平臺上使用PipeCNN實(shí)現(xiàn)ImageNet圖像分類

基于RV1126開發(fā)板實(shí)現(xiàn)自學(xué)習(xí)圖像分類方案

如何將Keras H5模型轉(zhuǎn)換為中間表示 (IR) 格式?
轉(zhuǎn)換Keras H5模型,為什么無法確定--input_shape參數(shù)的值?
使用DLP4500進(jìn)行圖片投影時(shí),內(nèi)置Flash太小,無法投影更多的圖片,怎么解決?
基于鎖相放大器的電渦流金屬分類系統(tǒng)設(shè)計(jì)
LabVIEW使用Vision視覺進(jìn)行硬幣分類計(jì)數(shù)
如何使用ddc進(jìn)行數(shù)據(jù)分類
如何用OpenCV的相機(jī)捕捉視頻進(jìn)行人臉檢測--基于米爾NXP i.MX93開發(fā)板
使用卷積神經(jīng)網(wǎng)絡(luò)進(jìn)行圖像分類的步驟
減速電機(jī)該如何分類?
使用MODE引腳進(jìn)行簡單的恒壓調(diào)節(jié)

評論