当前位置:首页 > 教育综合 > 正文

python CNN

Python keras构建CNN

data.py:

#coding:utf-8
"""
Author:wepon
Source:https://github.com/wepe
"""
importos
fromPILimportImage
importnumpyasnp
#读取文件夹mnist下的42000张图片,图片为灰度图,所以为1通道,图像大小28*28
#如果是将彩色图作为输入,则将1替换为3,并且data[i,:,:,:]=arr改为data[i,:,:,:]=[arr[:,:,0],arr[:,:,1],arr[:,:,2]]
defload_data():
data=np.empty((42000,1,28,28),dtype="float32")
label=np.empty((42000,),dtype="uint8")
imgs=os.listdir("./mnist")
num=len(imgs)
foriinrange(num):
img=Image.open("./mnist/"+imgs[i])
arr=np.asarray(img,dtype="float32")
data[i,:,:,:]=arr
label[i]=int(imgs[i].split('.')[0])
returndata,label

由于Keras系统升级,cnn.py代码调整如下:

#coding:utf-8
'''
GPUruncommand:
THEANO_FLAGS=mode=FAST_RUN,device=gpu,floatX=float32pythoncnn.py
CPUruncommand:
pythoncnn.py
'''
#导入各种用到的模块组件
from__future__importabsolute_import
from__future__importprint_function
fromkeras.preprocessing.imageimportImageDataGenerator
fromkeras.modelsimportSequential
fromkeras.layers.coreimportDense,Dropout,Activation,Flatten
fromkeras.layers.advanced_activationsimportPReLU
fromkeras.layers.convolutionalimportConvolution2D,MaxPooling2D
fromkeras.optimizersimportSGD,Adadelta,Adagrad
fromkeras.utilsimportnp_utils,generic_utils
fromsix.movesimportrange
fromdataimportload_data
importrandom
#加载数据
data,label=load_data()
#打乱数据
index=[iforiinrange(len(data))]
random.shuffle(index)
data=data[index]
label=label[index]
print(data.shape[0],'samples')
#label为0~9共10个类别,keras要求格式为binaryclassmatrices,转化一下,直接调用keras提供的这个函数
label=np_utils.to_categorical(label,10)

怎样用python构建一个卷积神经网络

用keras框架较为方便

首先安装anaconda,然后通过pip安装keras


以下转自wphh的博客。

#coding:utf-8
'''
GPUruncommand:
THEANO_FLAGS=mode=FAST_RUN,device=gpu,floatX=float32pythoncnn.py
CPUruncommand:
pythoncnn.py
2016.06.06更新:
这份代码是keras开发初期写的,当时keras还没有现在这么流行,文档也还没那么丰富,所以我当时写了一些简单的教程。
现在keras的API也发生了一些的变化,建议及推荐直接上keras.io看更加详细的教程。
'''
#导入各种用到的模块组件
from__future__importabsolute_import
from__future__importprint_function
fromkeras.preprocessing.imageimportImageDataGenerator
fromkeras.modelsimportSequential
fromkeras.layers.coreimportDense,Dropout,Activation,Flatten
fromkeras.layers.advanced_activationsimportPReLU
fromkeras.layers.convolutionalimportConvolution2D,MaxPooling2D
fromkeras.optimizersimportSGD,Adadelta,Adagrad
fromkeras.utilsimportnp_utils,generic_utils
fromsix.movesimportrange
fromdataimportload_data
importrandom
importnumpyasnp
np.random.seed(1024)#forreproducibility
#加载数据
data,label=load_data()
#打乱数据
index=[iforiinrange(len(data))]
random.shuffle(index)
data=data[index]
label=label[index]
print(data.shape[0],'samples')
#label为0~9共10个类别,keras要求格式为binaryclassmatrices,转化一下,直接调用keras提供的这个函数
label=np_utils.to_categorical(label,10)

如何用caffe的python接口实现cnn

简单的说,你需要把py-faster-rcnn下的caffe-fast-rcnn迁移到Win下重新编译,主要是为了编译pycaffe,开启WITH_PYTHON_LAYER,还要在Win下把lib目录下的python代码重新编译以上两项我都用的是CPU编译这样就可以运行Tools下的Demo.py了,参数--cpu。模型文件我用linux下训练好的。

怎样用python构建一个卷积神经网络模型

上周末利用python简单实现了一个卷积神经网络,只包含一个卷积层和一个maxpooling层,pooling层后面的多层神经网络采用了softmax形式的输出。实验输入仍然采用MNIST图像使用10个feature map时,卷积和pooling的结果分别如下所示。



部分源码如下:

[python]view plaincopy

  • #coding=utf-8

  • '''''

  • Createdon2014年11月30日

  • @author:Wangliaofan

  • '''

  • importnumpy

  • importstruct

  • importmatplotlib.pyplotasplt

  • importmath

  • importrandom

  • importcopy

  • #test

  • fromBasicMultilayerNeuralNetworkimportBMNN2

  • defsigmoid(inX):

  • if1.0+numpy.exp(-inX)==0.0:

  • return999999999.999999999

  • return1.0/(1.0+numpy.exp(-inX))

  • defdifsigmoid(inX):

  • returnsigmoid(inX)*(1.0-sigmoid(inX))

  • deftangenth(inX):

  • return(1.0*math.exp(inX)-1.0*math.exp(-inX))/(1.0*math.exp(inX)+1.0*math.exp(-inX))

  • defcnn_conv(in_image,filter_map,B,type_func='sigmoid'):

  • #in_image[num,featuremap,row,col]=>in_image[Irow,Icol]

  • #featuresmap[kfilter,row,col]

  • #type_func['sigmoid','tangenth']

  • #out_feature[kfilter,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)#[kfilter,row,col]

  • ifshape_filter[1]>shape_image[0]orshape_filter[2]>shape_image[1]:

  • raiseException

  • 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)

  • fork_idxinrange(0,k):

  • #rotate180tocalculateconv

  • c_filter=numpy.rot90(filter_map[k_idx,:,:],2)

  • forr_idxinrange(0,m):

  • forc_idxinrange(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)

  • iftype_func=='sigmoid':

  • out_feature[k_idx,r_idx,c_idx]=sigmoid(sum_temp+B[k_idx])

  • eliftype_func=='tangenth':

  • out_feature[k_idx,r_idx,c_idx]=tangenth(sum_temp+B[k_idx])

  • else:

  • raiseException

  • returnout_feature

  • defcnn_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))

  • fork_idxinrange(0,k):

  • forr_idxinrange(0,out_row):

  • forc_idxinrange(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)

  • #printmax_index

  • #printmax_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

  • returnout_pooling,max_index_Matirx

  • defpoolwithfunc(in_pooling,W,B,type_func='sigmoid'):

  • k,row,col=numpy.shape(in_pooling)

  • out_pooling=numpy.zeros((k,row,col))

  • fork_idxinrange(0,k):

  • forr_idxinrange(0,row):

  • forc_idxinrange(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])

  • returnout_pooling

  • #out_featureistheoutputofconv

  • defbackErrorfromPoolToConv(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)

  • ifk1!=k2:

  • raiseException

  • foridx_kinrange(0,k1):

  • foridx_rowinrange(0,row):

  • foridx_colinrange(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])

  • returnerror_conv

  • defbackErrorfromConvToInput(theta,inputImage):

  • k1,row,col=numpy.shape(theta)

  • #print"theta",k1,row,col

  • i_row,i_col=numpy.shape(inputImage)

  • ifrow>i_roworcol>i_col:

  • raiseException

  • filter_row=i_row-row+1

  • filter_col=i_col-col+1

  • detaW=numpy.zeros((k1,filter_row,filter_col))

  • #thesamewithconvvalidinmatlab

  • fork_idxinrange(0,k1):

  • foridx_rowinrange(0,filter_row):

  • foridx_colinrange(0,filter_col):

  • subInputMatrix=inputImage[idx_row:idx_row+row,idx_col:idx_col+col]

  • #print"subInputMatrix",numpy.shape(subInputMatrix)

  • #rotatetheta180

  • #printnumpy.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))

  • fork_idxinrange(0,k1):

  • detaB[k_idx]=numpy.sum(theta[k_idx,:,:])

  • returndetaW,detaB

  • defloadMNISTimage(absFilePathandName,datanum=60000):

  • images=open(absFilePathandName,'rb')

  • buf=images.read()

  • index=0

  • magic,numImages,numRows,numColumns=struct.unpack_from('>IIII',buf,index)

  • printmagic,numImages,numRows,numColumns

  • index+=struct.calcsize('>IIII')

  • ifmagic!=2051:

  • raiseException

  • 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)

  • returnnextmatrix,numImages

  • defloadMNISTlabels(absFilePathandName,datanum=60000):

  • labels=open(absFilePathandName,'rb')

  • buf=labels.read()

  • index=0

  • magic,numLabels=struct.unpack_from('>II',buf,index)

  • printmagic,numLabels

  • index+=struct.calcsize('>II')

  • ifmagic!=2049:

  • raiseException

  • datablock=">"+str(datanum)+"B"

  • #nextmatrix=struct.unpack_from('>60000B',buf,index)

  • nextmatrix=struct.unpack_from(datablock,buf,index)

  • nextmatrix=numpy.array(nextmatrix)

  • returnnextmatrix,numLabels

  • defsimpleCNN(numofFilter,filter_size,pooling_size=2,maxIter=1000,imageNum=500):

  • decayRate=0.01

  • MNISTimage,num1=loadMNISTimage("F:\MachineLearning\UFLDL\data\common\\train-images-idx3-ubyte",imageNum)

  • printnum1

  • 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:\MachineLearning\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

  • whilecIter

  • 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)

  • #printnumpy.shape(MLP_input)

  • DetaW,DetaB,temperror=MLP.backwardPropogation(MLP_input,ImageNum)

  • ifcIter%50==0:

  • printcIter,"Temperror:",temperror

  • #printnumpy.shape(MLP.Theta[MLP.Nl-2])

  • #printnumpy.shape(MLP.Ztemp[0])

  • #printnumpy.shape(MLP.weightMatrix[0])

  • theta_pool=MLP.Theta[MLP.Nl-2]*MLP.weightMatrix[0].transpose()

  • #printnumpy.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)

  • #printerror_conv

  • conv_DetaW,conv_DetaB=backErrorfromConvToInput(error_conv,MNISTimage[ImageNum,0,:,:])

  • #print"W_k",W_k

  • #print"conv_DetaW",conv_DetaW

python怎么将预训练好的词向量初始化cnn

因为你从stopword.txt这个文件里读出来的是是str类型的,str类型跟unicode不相等
展开全文阅读