當前位置:首頁 » 編程語言 » Python卷積

Python卷積

發布時間: 2022-01-14 11:42:49

python三維卷積可以用什麼函數 matlab只要用convn

寫了一個輸入和卷積核dim=2是一樣的(都是3)的卷積函數,可以試試多加一個for循環變成三維卷積


def conv3D(image, filter):
'''
三維卷積
:param image: 輸入,shape為 [h,w,c], c=3
:param filter: 卷積核,shape為 [x,y,z], z=3
:return:
'''
h, w, c = image.shape
x, y, z = filter.shape
height_new = h - x + 1 # 輸出 h
width_new = w - y + 1 # 輸出 w
image_new = np.zeros((height_new, width_new), dtype=np.float)
for i in range(height_new):
for j in range(width_new):
r = np.sum(image[i:i+x, j:j+x, 0] * filter[:,:,0])
g = np.sum(image[i:i+y, j:j+y, 1] * filter[:,:,1])
b = np.sum(image[i:i+z, j:j+z, 2] * filter[:,:,2])
image_new[i, j] = np.sum([r,g,b])
image_new = image_new.clip(0, 255)
image_new = np.rint(image_new).astype('uint8')
return image_new

Ⅱ 怎樣用python構建一個卷積神經網路

用keras框架較為方便

首先安裝anaconda,然後通過pip安裝keras

Ⅲ python中的1維卷積神經網路是怎麼做的

進入python的官網,找到download頁面,點擊針對windows的安裝文件,下載安裝,注意區分32位及64位

Ⅳ Python 中用於兩個值卷積的函數是什麼,我知道matlab 中是conv,Python中有預知對應的嗎

全部用文件IO的話可以這樣: matlab把所有參數輸出到一個文件里,然後用system命令調python腳本。python腳本讀文件做計算結果再寫文件。最後matlab再讀文件得到結果。 假設python腳本的用法是: python xxx.py in.txt out.txt 則matlab調用命令...

Ⅳ 怎樣用python構建一個卷積神經網路模型

上周末利用python簡單實現了一個卷積神經網路,只包含一個卷積層和一個maxpooling層,pooling層後面的多層神經網路採用了softmax形式的輸出。實驗輸入仍然採用MNIST圖像使用10個feature map時,卷積和pooling的結果分別如下所示。


部分源碼如下:

[python]view plain

  • #coding=utf-8

  • '''''

  • Createdon2014年11月30日

  • @author:Wangliaofan

  • '''

  • importnumpy

  • importstruct

  • importmatplotlib.pyplotasplt

  • importmath

  • importrandom

  • import

  • #test

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

  • 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 keras CNN訓練文字的一位特徵向量怎麼構造卷積層

keras/imdb_cnn.py at master · fchollet/keras · GitHub
'''This example demonstrates the use of Convolution1D for text classification.
這個例子應該能幫到你
不過分類是 binary 的
要dense 層自己改成 softmax

我自己畢業論文也寫了一個 koalaGreener/Character-level-Convolutional-Network-for-Text-Classification-Applied-to-Chinese-Corpus
是把CNN用在文本分類的 不過dataset我是自己構建了新的中文字元和拼音字元,然後做了比較. 文章鏈接是[1611.04358] Character-level Convolutional Network for Text Classification Applied to Chinese Corpus 僅供參考啦 那時候不會做搜到這個鏈接也挺茫然的 後來者可以稍微參考一下

Ⅶ python深度卷積網路,普通計算機能跑嗎

這個名單並不詳盡,它只是我在計算機視覺的職業生涯中使用並在某個時間段發現特別有用的一個庫的列表。
這其中的一些庫我比別人用的多很多,尤其是Keras、mxnet和sklearn-theano。
其他的一些我是間接的使用,比如Theano和TensorFlow(庫包括Keras、deepy和Blocks等)。
另外的我只是在一些特別的任務中用過(比如nolearn和他們的Deep Belief Network implementation)。
這篇文章的目的是向你介紹這些庫。我建議你認真了解這里的每一個庫,然後在某個具體工作情境中你就可以確定一個最適用的庫。

Ⅷ python深度學習中經過卷積神經網路訓練後的輸出怎樣查看

這兩個概念實際上是互相交叉的,例如,卷積神經網路(Convolutional neural networks,簡稱CNNs)就是一種深度的監督學習下的機器學習模型,而深度置信網(Deep Belief Nets,簡稱DBNs)就是一種無監督學習下的機器學習模型。

熱點內容
好醫生連鎖店密碼多少 發布:2024-09-20 05:09:38 瀏覽:15
魔獸腳本代理 發布:2024-09-20 05:09:35 瀏覽:98
python登陸網頁 發布:2024-09-20 05:08:39 瀏覽:757
安卓qq飛車如何轉蘋果 發布:2024-09-20 04:54:30 瀏覽:178
存儲過程中in什麼意思 發布:2024-09-20 04:24:20 瀏覽:315
php顯示數據 發布:2024-09-20 03:48:38 瀏覽:501
源碼安裝軟體 發布:2024-09-20 03:44:31 瀏覽:354
入門編程游戲的書 發布:2024-09-20 03:31:26 瀏覽:236
e盒的演算法 發布:2024-09-20 03:30:52 瀏覽:144
win10登錄密碼如何修改登錄密碼 發布:2024-09-20 03:09:43 瀏覽:71