当前位置:首页 » 编程语言 » pythonzeros

pythonzeros

发布时间: 2022-06-20 10:31:23

① 如何用 python 实现一个抠图功能

要制作这样的组合图其实很简单,最主要用到的就是美图秀秀独家新增的“添加前景”按钮。当然,组合之前需要多抠取几张图片备用。
每抠好一张图片后单击“完成抠图”,软件就会将抠图保存了。

② Python小问题

出现这个问题是因为索引出现了浮点数,不是索引允许的数据类型,可以验证一下

importnumpyasnp
y=np.zeros(shape=(1,5))
arr=[nforninnp.linspace(1,5,5)]

arr里存储的就是源代码中会用的索引,下图是结果

importnumpyasnp
y=np.zeros(shape=(1,5))

forninnp.int16(np.linspace(1,5,5)):
y[0,n-1]=n**2
print(y)

③ 想用python处理比较大的单色图片文件,如何提高速度

你好,你可以考虑使用numpy的函数来做,下面是例子的python代码

image=np.zeros((400,400,3),dtype="uint8")
raw=image.()
image[np.where((image==[0,0,0]).all(axis=2))]=[255,255,255]
cv2.imshow('Test0',image)
lower_black=np.array([0,0,0],dtype="uint16")
upper_black=np.array([70,70,70],dtype="uint16")
black_mask=cv2.inRange(image,lower_black,upper_black)
image[np.where((image==[0,0,0]).all(axis=2))]=[155,255,155]
black_mask[np.where(black_mask==[0])]=[155]

你把上面的那个image的数值改成你需要改的目标就可以直接替换了。

④ python np.zeros ValueError: array is too big.

这个库我没用过,不过一般产生这个问题,可能因为是array内部有限制,两种方法:
一种是找到代码中限制的地方,看看如果去掉的话会不会有问题。
另一种就是把这个array分成多个array进行操作。
如果解决了您的问题请采纳!
如果未解决请继续追问

⑤ 怎样用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中如何生成一个全是0和1的矩阵

溢出测试时,常常需要生成一长串字符串去填充缓冲区,用循环的话比较麻烦。python中直接可以用乘号来操作字符串:

shellcode = 'x90' * 1000

执行后,shellcode的值为1000个x90。

同时也可以用加号来操作字符串,连接两个字符串的例子如下:

import struct

buffer = 'A' * 100

jmpesp = struct('<L', 0x7ffa4512) #将0x7ffa4512转化为x12x45xfax7f的格式

buffer += jmpesp

⑦ python中向量指的是什么意思


一、向量是什么

在数学中,向量(也称为欧几里得向量、几何向量、矢量),指具有大小(magnitude)和方向的量。它可以形象化地表示为带箭头的线段。箭头所指:代表向量的方向;线段长度:代表向量的大小。与向量对应的只有大小,没有方向的量叫做数量(物理学中称标量)

在这里,向量即一维数组,用 arange 函数创建向量是最简单的方式之一:

arange函数也可以指定初始值、终止值和步长来创建一维数组:

向量还能直接对每个元素进行运算:

二、创建向量

上面使用 arange 则是创建向量的一种方式,其实只要是数组创建的函数均可以创建向量,如:

linspace() 函数

前文介绍:linspace 通过制定初始值、终止值和元素个数创建等差数列向量,通过endpoint 参数指定是否包含终止值,默认为True

logspace() 函数

同linspace,创建等比数列,基数通过base参数指定,默认基数为10

zeros() 函数和 ones() 函数

这两个函数分别可以创建指定长度或形状的全0或全1的 ndarray 数组,比如:

指定数据类型:

empty() 函数

这个函数可以创建一个没有任何具体值的 ndarray 数组,例如:

random.randn() 函数

randn 是 numpy.random 中生成正态分布随机数据的函数

fromstring() 函数

从字符串创建数组

上面从字符串创建的数组,定义为整形8bit,创建出来的其实就是字符串的ASCII 码

fromfunction() 函数

从函数创建数组,是数据分析常见的方法

可先定义一个从下标计算数值的函数,然后用fromfunction 创建数组

fromfunction 第一个参数为计算每个数组元素的函数名,第二个参数指定数组的形状。因为它支持多维数组,所以第二个参数必须是一个序列。

例如我创建一个九九乘法表:

注意,fromfunction 函数中的第二个参数指定的是数组的下标,下标作为实参通过遍历的方式传递给函数的形参。

众多python培训视频,尽在python学习网,欢迎在线学习!

⑧ python 怎么遍历numpy.zeros

遍历一般是数列,而不是某个文件,如果是文件的话,你需要打开用with open()as 方法

⑨ python中np.zeros什么意思

zeros(m, n); % 生成一个m*n的零矩阵zeros(m); % 生成一个m*m的零矩阵(即m阶方阵)zeros(m, n, k, ...); % 生成一个m*n*k*...的零矩阵zeros(size(A)); % 生成一个与矩阵A的维度一致的零矩阵

热点内容
编程一首诗 发布:2025-02-06 06:45:04 浏览:528
惊声尖笑5下载ftp 发布:2025-02-06 06:33:16 浏览:528
共享文件夹让输入密码 发布:2025-02-06 06:32:28 浏览:970
收银服务器响应出错什么意思 发布:2025-02-06 06:24:43 浏览:607
sql用户授权 发布:2025-02-06 06:24:42 浏览:677
苹果手机相册显示正在上传 发布:2025-02-06 06:05:43 浏览:542
hadoop下载文件夹 发布:2025-02-06 06:05:08 浏览:187
铠最强配置是哪些 发布:2025-02-06 06:04:22 浏览:360
编译器的制作环境 发布:2025-02-06 05:54:34 浏览:829
学车网源码 发布:2025-02-06 05:47:40 浏览:386