用keras框架較為方便
創(chuàng)新互聯(lián)長期為超過千家客戶提供的網站建設服務,團隊從業(yè)經驗10年,關注不同地域、不同群體,并針對不同對象提供差異化的產品和服務;打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網生態(tài)環(huán)境。為民勤企業(yè)提供專業(yè)的網站設計制作、網站設計,民勤網站改版等技術服務。擁有十年豐富建站經驗和眾多成功案例,為您定制開發(fā)。
首先安裝anaconda,然后通過pip安裝keras
以下轉自wphh的博客。
#coding:utf-8
'''
GPU?run?command:
THEANO_FLAGS=mode=FAST_RUN,device=gpu,floatX=float32?python?cnn.py
CPU?run?command:
python?cnn.py
2016.06.06更新:
這份代碼是keras開發(fā)初期寫的,當時keras還沒有現在這么流行,文檔也還沒那么豐富,所以我當時寫了一些簡單的教程。
現在keras的API也發(fā)生了一些的變化,建議及推薦直接上keras.io看更加詳細的教程。
'''
#導入各種用到的模塊組件
from?__future__?import?absolute_import
from?__future__?import?print_function
from?keras.preprocessing.image?import?ImageDataGenerator
from?keras.models?import?Sequential
from?keras.layers.core?import?Dense,?Dropout,?Activation,?Flatten
from?keras.layers.advanced_activations?import?PReLU
from?keras.layers.convolutional?import?Convolution2D,?MaxPooling2D
from?keras.optimizers?import?SGD,?Adadelta,?Adagrad
from?keras.utils?import?np_utils,?generic_utils
from?six.moves?import?range
from?data?import?load_data
import?random
import?numpy?as?np
np.random.seed(1024)??#?for?reproducibility
#加載數據
data,?label?=?load_data()
#打亂數據
index?=?[i?for?i?in?range(len(data))]
random.shuffle(index)
data?=?data[index]
label?=?label[index]
print(data.shape[0],?'?samples')
#label為0~9共10個類別,keras要求格式為binary?class?matrices,轉化一下,直接調用keras提供的這個函數
label?=?np_utils.to_categorical(label,?10)
###############
#開始建立CNN模型
###############
#生成一個model
model?=?Sequential()
#第一個卷積層,4個卷積核,每個卷積核大小5*5。1表示輸入的圖片的通道,灰度圖為1通道。
#border_mode可以是valid或者full,具體看這里說明:
#激活函數用tanh
#你還可以在model.add(Activation('tanh'))后加上dropout的技巧:?model.add(Dropout(0.5))
model.add(Convolution2D(4,?5,?5,?border_mode='valid',input_shape=(1,28,28)))?
model.add(Activation('tanh'))
#第二個卷積層,8個卷積核,每個卷積核大小3*3。4表示輸入的特征圖個數,等于上一層的卷積核個數
#激活函數用tanh
#采用maxpooling,poolsize為(2,2)
model.add(Convolution2D(8,?3,?3,?border_mode='valid'))
model.add(Activation('tanh'))
model.add(MaxPooling2D(pool_size=(2,?2)))
#第三個卷積層,16個卷積核,每個卷積核大小3*3
#激活函數用tanh
#采用maxpooling,poolsize為(2,2)
model.add(Convolution2D(16,?3,?3,?border_mode='valid'))?
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2,?2)))
#全連接層,先將前一層輸出的二維特征圖flatten為一維的。
#Dense就是隱藏層。16就是上一層輸出的特征圖個數。4是根據每個卷積層計算出來的:(28-5+1)得到24,(24-3+1)/2得到11,(11-3+1)/2得到4
#全連接有128個神經元節(jié)點,初始化方式為normal
model.add(Flatten())
model.add(Dense(128,?init='normal'))
model.add(Activation('tanh'))
#Softmax分類,輸出是10類別
model.add(Dense(10,?init='normal'))
model.add(Activation('softmax'))
#############
#開始訓練模型
##############
#使用SGD?+?momentum
#model.compile里的參數loss就是損失函數(目標函數)
sgd?=?SGD(lr=0.05,?decay=1e-6,?momentum=0.9,?nesterov=True)
model.compile(loss='categorical_crossentropy',?optimizer=sgd,metrics=["accuracy"])
#調用fit方法,就是一個訓練過程.?訓練的epoch數設為10,batch_size為100.
#數據經過隨機打亂shuffle=True。verbose=1,訓練過程中輸出的信息,0、1、2三種方式都可以,無關緊要。show_accuracy=True,訓練時每一個epoch都輸出accuracy。
#validation_split=0.2,將20%的數據作為驗證集。
model.fit(data,?label,?batch_size=100,?nb_epoch=10,shuffle=True,verbose=1,validation_split=0.2)
"""
#使用data?augmentation的方法
#一些參數和調用的方法,請看文檔
datagen?=?ImageDataGenerator(
featurewise_center=True,?#?set?input?mean?to?0?over?the?dataset
samplewise_center=False,?#?set?each?sample?mean?to?0
featurewise_std_normalization=True,?#?divide?inputs?by?std?of?the?dataset
samplewise_std_normalization=False,?#?divide?each?input?by?its?std
zca_whitening=False,?#?apply?ZCA?whitening
rotation_range=20,?#?randomly?rotate?images?in?the?range?(degrees,?0?to?180)
width_shift_range=0.2,?#?randomly?shift?images?horizontally?(fraction?of?total?width)
height_shift_range=0.2,?#?randomly?shift?images?vertically?(fraction?of?total?height)
horizontal_flip=True,?#?randomly?flip?images
vertical_flip=False)?#?randomly?flip?images
#?compute?quantities?required?for?featurewise?normalization?
#?(std,?mean,?and?principal?components?if?ZCA?whitening?is?applied)
datagen.fit(data)
for?e?in?range(nb_epoch):
print('-'*40)
print('Epoch',?e)
print('-'*40)
print("Training...")
#?batch?train?with?realtime?data?augmentation
progbar?=?generic_utils.Progbar(data.shape[0])
for?X_batch,?Y_batch?in?datagen.flow(data,?label):
loss,accuracy?=?model.train(X_batch,?Y_batch,accuracy=True)
progbar.add(X_batch.shape[0],?values=[("train?loss",?loss),("accuracy:",?accuracy)]?)
"""
如何用python實現圖像的一維高斯濾波器
現在把卷積模板中的值換一下,不是全1了,換成一組符合高斯分布的數值放在模板里面,比如這時中間的數值最大,往兩邊走越來越小,構造一個小的高斯包。實現的函數為cv2.GaussianBlur()。對于高斯模板,我們需要制定的是高斯核的高和寬(奇數),沿x與y方向的標準差(如果只給x,y=x,如果都給0,那么函數會自己計算)。高斯核可以有效的出去圖像的高斯噪聲。當然也可以自己構造高斯核,相關函數:cv2.GaussianKernel().
import cv2
import numpy as np
import matplotlib.pyplot as plt
img = cv2.imread(‘flower.jpg‘,0) #直接讀為灰度圖像
for i in range(2000): #添加點噪聲
temp_x = np.random.randint(0,img.shape[0])
temp_y = np.random.randint(0,img.shape[1])
img[temp_x][temp_y] = 255
blur = cv2.GaussianBlur(img,(5,5),0)
plt.subplot(1,2,1),plt.imshow(img,‘gray‘)#默認彩色,另一種彩色bgr
plt.subplot(1,2,2),plt.imshow(blur,‘gray‘)
上周末利用python簡單實現了一個卷積神經網絡,只包含一個卷積層和一個maxpooling層,pooling層后面的多層神經網絡采用了softmax形式的輸出。實驗輸入仍然采用MNIST圖像使用10個feature map時,卷積和pooling的結果分別如下所示。
部分源碼如下:
[python]?view plain?copy
#coding=utf-8
'''''
Created?on?2014年11月30日
@author:?Wangliaofan
'''
import?numpy
import?struct
import?matplotlib.pyplot?as?plt
import?math
import?random
import?copy
#test
from?BasicMultilayerNeuralNetwork?import?BMNN2
def?sigmoid(inX):
if?1.0+numpy.exp(-inX)==?0.0:
return?999999999.999999999
return?1.0/(1.0+numpy.exp(-inX))
def?difsigmoid(inX):
return?sigmoid(inX)*(1.0-sigmoid(inX))
def?tangenth(inX):
return?(1.0*math.exp(inX)-1.0*math.exp(-inX))/(1.0*math.exp(inX)+1.0*math.exp(-inX))
def?cnn_conv(in_image,?filter_map,B,type_func='sigmoid'):
#in_image[num,feature?map,row,col]=in_image[Irow,Icol]
#features?map[k?filter,row,col]
#type_func['sigmoid','tangenth']
#out_feature[k?filter,Irow-row+1,Icol-col+1]
shape_image=numpy.shape(in_image)#[row,col]
#print?"shape_image",shape_image
shape_filter=numpy.shape(filter_map)#[k?filter,row,col]
if?shape_filter[1]shape_image[0]?or?shape_filter[2]shape_image[1]:
raise?Exception
shape_out=(shape_filter[0],shape_image[0]-shape_filter[1]+1,shape_image[1]-shape_filter[2]+1)
out_feature=numpy.zeros(shape_out)
k,m,n=numpy.shape(out_feature)
for?k_idx?in?range(0,k):
#rotate?180?to?calculate?conv
c_filter=numpy.rot90(filter_map[k_idx,:,:],?2)
for?r_idx?in?range(0,m):
for?c_idx?in?range(0,n):
#conv_temp=numpy.zeros((shape_filter[1],shape_filter[2]))
conv_temp=numpy.dot(in_image[r_idx:r_idx+shape_filter[1],c_idx:c_idx+shape_filter[2]],c_filter)
sum_temp=numpy.sum(conv_temp)
if?type_func=='sigmoid':
out_feature[k_idx,r_idx,c_idx]=sigmoid(sum_temp+B[k_idx])
elif?type_func=='tangenth':
out_feature[k_idx,r_idx,c_idx]=tangenth(sum_temp+B[k_idx])
else:
raise?Exception
return?out_feature
def?cnn_maxpooling(out_feature,pooling_size=2,type_pooling="max"):
k,row,col=numpy.shape(out_feature)
max_index_Matirx=numpy.zeros((k,row,col))
out_row=int(numpy.floor(row/pooling_size))
out_col=int(numpy.floor(col/pooling_size))
out_pooling=numpy.zeros((k,out_row,out_col))
for?k_idx?in?range(0,k):
for?r_idx?in?range(0,out_row):
for?c_idx?in?range(0,out_col):
temp_matrix=out_feature[k_idx,pooling_size*r_idx:pooling_size*r_idx+pooling_size,pooling_size*c_idx:pooling_size*c_idx+pooling_size]
out_pooling[k_idx,r_idx,c_idx]=numpy.amax(temp_matrix)
max_index=numpy.argmax(temp_matrix)
#print?max_index
#print?max_index/pooling_size,max_index%pooling_size
max_index_Matirx[k_idx,pooling_size*r_idx+max_index/pooling_size,pooling_size*c_idx+max_index%pooling_size]=1
return?out_pooling,max_index_Matirx
def?poolwithfunc(in_pooling,W,B,type_func='sigmoid'):
k,row,col=numpy.shape(in_pooling)
out_pooling=numpy.zeros((k,row,col))
for?k_idx?in?range(0,k):
for?r_idx?in?range(0,row):
for?c_idx?in?range(0,col):
out_pooling[k_idx,r_idx,c_idx]=sigmoid(W[k_idx]*in_pooling[k_idx,r_idx,c_idx]+B[k_idx])
return?out_pooling
#out_feature?is?the?out?put?of?conv
def?backErrorfromPoolToConv(theta,max_index_Matirx,out_feature,pooling_size=2):
k1,row,col=numpy.shape(out_feature)
error_conv=numpy.zeros((k1,row,col))
k2,theta_row,theta_col=numpy.shape(theta)
if?k1!=k2:
raise?Exception
for?idx_k?in?range(0,k1):
for?idx_row?in?range(?0,?row):
for?idx_col?in?range(?0,?col):
error_conv[idx_k,idx_row,idx_col]=\
max_index_Matirx[idx_k,idx_row,idx_col]*\
float(theta[idx_k,idx_row/pooling_size,idx_col/pooling_size])*\
difsigmoid(out_feature[idx_k,idx_row,idx_col])
return?error_conv
def?backErrorfromConvToInput(theta,inputImage):
k1,row,col=numpy.shape(theta)
#print?"theta",k1,row,col
i_row,i_col=numpy.shape(inputImage)
if?rowi_row?or?col?i_col:
raise?Exception
filter_row=i_row-row+1
filter_col=i_col-col+1
detaW=numpy.zeros((k1,filter_row,filter_col))
#the?same?with?conv?valid?in?matlab
for?k_idx?in?range(0,k1):
for?idx_row?in?range(0,filter_row):
for?idx_col?in?range(0,filter_col):
subInputMatrix=inputImage[idx_row:idx_row+row,idx_col:idx_col+col]
#print?"subInputMatrix",numpy.shape(subInputMatrix)
#rotate?theta?180
#print?numpy.shape(theta)
theta_rotate=numpy.rot90(theta[k_idx,:,:],?2)
#print?"theta_rotate",theta_rotate
dotMatrix=numpy.dot(subInputMatrix,theta_rotate)
detaW[k_idx,idx_row,idx_col]=numpy.sum(dotMatrix)
detaB=numpy.zeros((k1,1))
for?k_idx?in?range(0,k1):
detaB[k_idx]=numpy.sum(theta[k_idx,:,:])
return?detaW,detaB
def?loadMNISTimage(absFilePathandName,datanum=60000):
images=open(absFilePathandName,'rb')
buf=images.read()
index=0
magic,?numImages?,?numRows?,?numColumns?=?struct.unpack_from('IIII'?,?buf?,?index)
print?magic,?numImages?,?numRows?,?numColumns
index?+=?struct.calcsize('IIII')
if?magic?!=?2051:
raise?Exception
datasize=int(784*datanum)
datablock=""+str(datasize)+"B"
#nextmatrix=struct.unpack_from('47040000B'?,buf,?index)
nextmatrix=struct.unpack_from(datablock?,buf,?index)
nextmatrix=numpy.array(nextmatrix)/255.0
#nextmatrix=nextmatrix.reshape(numImages,numRows,numColumns)
#nextmatrix=nextmatrix.reshape(datanum,1,numRows*numColumns)
nextmatrix=nextmatrix.reshape(datanum,1,numRows,numColumns)
return?nextmatrix,?numImages
def?loadMNISTlabels(absFilePathandName,datanum=60000):
labels=open(absFilePathandName,'rb')
buf=labels.read()
index=0
magic,?numLabels??=?struct.unpack_from('II'?,?buf?,?index)
print?magic,?numLabels
index?+=?struct.calcsize('II')
if?magic?!=?2049:
raise?Exception
datablock=""+str(datanum)+"B"
#nextmatrix=struct.unpack_from('60000B'?,buf,?index)
nextmatrix=struct.unpack_from(datablock?,buf,?index)
nextmatrix=numpy.array(nextmatrix)
return?nextmatrix,?numLabels
def?simpleCNN(numofFilter,filter_size,pooling_size=2,maxIter=1000,imageNum=500):
decayRate=0.01
MNISTimage,num1=loadMNISTimage("F:\Machine?Learning\UFLDL\data\common\\train-images-idx3-ubyte",imageNum)
print?num1
row,col=numpy.shape(MNISTimage[0,0,:,:])
out_Di=numofFilter*((row-filter_size+1)/pooling_size)*((col-filter_size+1)/pooling_size)
MLP=BMNN2.MuiltilayerANN(1,[128],out_Di,10,maxIter)
MLP.setTrainDataNum(imageNum)
MLP.loadtrainlabel("F:\Machine?Learning\UFLDL\data\common\\train-labels-idx1-ubyte")
MLP.initialweights()
#MLP.printWeightMatrix()
rng?=?numpy.random.RandomState(23455)
W_shp?=?(numofFilter,?filter_size,?filter_size)
W_bound?=?numpy.sqrt(numofFilter?*?filter_size?*?filter_size)
W_k=rng.uniform(low=-1.0?/?W_bound,high=1.0?/?W_bound,size=W_shp)
B_shp?=?(numofFilter,)
B=?numpy.asarray(rng.uniform(low=-.5,?high=.5,?size=B_shp))
cIter=0
while?cItermaxIter:
cIter?+=?1
ImageNum=random.randint(0,imageNum-1)
conv_out_map=cnn_conv(MNISTimage[ImageNum,0,:,:],?W_k,?B,"sigmoid")
out_pooling,max_index_Matrix=cnn_maxpooling(conv_out_map,2,"max")
pool_shape?=?numpy.shape(out_pooling)
MLP_input=out_pooling.reshape(1,1,out_Di)
#print?numpy.shape(MLP_input)
DetaW,DetaB,temperror=MLP.backwardPropogation(MLP_input,ImageNum)
if?cIter%50?==0?:
print?cIter,"Temp?error:?",temperror
#print?numpy.shape(MLP.Theta[MLP.Nl-2])
#print?numpy.shape(MLP.Ztemp[0])
#print?numpy.shape(MLP.weightMatrix[0])
theta_pool=MLP.Theta[MLP.Nl-2]*MLP.weightMatrix[0].transpose()
#print?numpy.shape(theta_pool)
#print?"theta_pool",theta_pool
temp=numpy.zeros((1,1,out_Di))
temp[0,:,:]=theta_pool
back_theta_pool=temp.reshape(pool_shape)
#print?"back_theta_pool",numpy.shape(back_theta_pool)
#print?"back_theta_pool",back_theta_pool
error_conv=backErrorfromPoolToConv(back_theta_pool,max_index_Matrix,conv_out_map,2)
#print?"error_conv",numpy.shape(error_conv)
#print?error_conv
conv_DetaW,conv_DetaB=backErrorfromConvToInput(error_conv,MNISTimage[ImageNum,0,:,:])
#print?"W_k",W_k
#print?"conv_DetaW",conv_DetaW