脉冲神经网络2:SNN的仿真2—感受野


import imageio
import numpy
img = imageio.imread("data/training/{}.png".format(0))
print(img)

# 定义感受野函数为rf
#  接受野的卷积
def rf(inp):
	sca1 =  0.625
	sca2 =  0.125
	sca3 = -0.125
	sca4 = -.5

	# 感受野核函数
	w = [[	sca4 ,sca3 , sca2 ,sca3 ,sca4],
	 	[	sca3 ,sca2 , sca1 ,sca2 ,sca3],
	 	[ 	sca2 ,sca1 , 	1 ,sca1 ,sca2],
	 	[	sca3 ,sca2 , sca1 ,sca2 ,sca3],
	 	[	sca4 ,sca3 , sca2 ,sca3 ,sca4]]

	pot = np.zeros([inp.shape[0],inp.shape[1]])
	ran = [-2,-1,0,1,2]
	ox = 2
	oy = 2

	# 卷积
	for i in range(inp.shape[0]):
		for j in range(inp.shape[1]):
			summ = 0
			for m in ran:
				for n in ran:
					if (i+m)>=0 and (i+m)<=inp.shape[0]-1 and (j+n)>=0 and (j+n)<=inp.shape[0]-1:
						summ = summ + w[ox+m][oy+n] * inp[i+m][j+n]/255
			pot[i][j] = summ
	return pot

pot = rf(img)

np.set_printoptions(formatter={'float': '{: 0.3f}'.format})
print(pot)

# 感受野处理后的图片返回后
imageio.imsave("rf_image.png",pot)

 0.png如下图所示

print(img)结果为:

[[255   0   0   0   0   0   0   0   0   0   0   0   0   0   0 255]
 [  0 255   0   0   0   0   0   0   0   0   0   0   0   0 255   0]
 [  0   0 255   0   0   0   0   0   0   0   0   0   0 255   0   0]
 [  0   0   0 255   0   0   0   0   0   0   0   0 255   0   0   0]
 [  0   0   0   0 255   0   0   0   0   0   0 255   0   0   0   0]
 [  0   0   0   0   0 255   0   0   0   0 255   0   0   0   0   0]
 [  0   0   0   0   0   0 255   0   0 255   0   0   0   0   0   0]
 [  0   0   0   0   0   0   0 255 255   0   0   0   0   0   0   0]
 [  0   0   0   0   0   0   0 255 255   0   0   0   0   0   0   0]
 [  0   0   0   0   0   0 255   0   0 255   0   0   0   0   0   0]
 [  0   0   0   0   0 255   0   0   0   0 255   0   0   0   0   0]
 [  0   0   0   0 255   0   0   0   0   0   0 255   0   0   0   0]
 [  0   0   0 255   0   0   0   0   0   0   0   0 255   0   0   0]
 [  0   0 255   0   0   0   0   0   0   0   0   0   0 255   0   0]
 [  0 255   0   0   0   0   0   0   0   0   0   0   0   0 255   0]
 [255   0   0   0   0   0   0   0   0   0   0   0   0   0   0 255]]

print(pot)显示为

[[ 0.625  1.125  0.375 -0.250 -0.500  0.000  0.000  0.000  0.000  0.000
   0.000 -0.500 -0.250  0.375  1.125  0.625]
 [ 1.125  0.750  1.000  0.375 -0.250 -0.500  0.000  0.000  0.000  0.000
  -0.500 -0.250  0.375  1.000  0.750  1.125]
 [ 0.375  1.000  0.250  1.000  0.375 -0.250 -0.500  0.000  0.000 -0.500
  -0.250  0.375  1.000  0.250  1.000  0.375]
 [-0.250  0.375  1.000  0.250  1.000  0.375 -0.250 -0.500 -0.500 -0.250
   0.375  1.000  0.250  1.000  0.375 -0.250]
 [-0.500 -0.250  0.375  1.000  0.250  1.000  0.375 -0.750 -0.750  0.375
   1.000  0.250  1.000  0.375 -0.250 -0.500]
 [ 0.000 -0.500 -0.250  0.375  1.000  0.250  0.500  0.125  0.125  0.500
   0.250  1.000  0.375 -0.250 -0.500  0.000]
 [ 0.000  0.000 -0.500 -0.250  0.375  0.500  0.000  1.375  1.375  0.000
   0.500  0.375 -0.250 -0.500  0.000  0.000]
 [ 0.000  0.000  0.000 -0.500 -0.750  0.125  1.375  1.250  1.250  1.375
   0.125 -0.750 -0.500  0.000  0.000  0.000]
 [ 0.000  0.000  0.000 -0.500 -0.750  0.125  1.375  1.250  1.250  1.375
   0.125 -0.750 -0.500  0.000  0.000  0.000]
 [ 0.000  0.000 -0.500 -0.250  0.375  0.500  0.000  1.375  1.375  0.000
   0.500  0.375 -0.250 -0.500  0.000  0.000]
 [ 0.000 -0.500 -0.250  0.375  1.000  0.250  0.500  0.125  0.125  0.500
   0.250  1.000  0.375 -0.250 -0.500  0.000]
 [-0.500 -0.250  0.375  1.000  0.250  1.000  0.375 -0.750 -0.750  0.375
   1.000  0.250  1.000  0.375 -0.250 -0.500]
 [-0.250  0.375  1.000  0.250  1.000  0.375 -0.250 -0.500 -0.500 -0.250
   0.375  1.000  0.250  1.000  0.375 -0.250]
 [ 0.375  1.000  0.250  1.000  0.375 -0.250 -0.500  0.000  0.000 -0.500
  -0.250  0.375  1.000  0.250  1.000  0.375]
 [ 1.125  0.750  1.000  0.375 -0.250 -0.500  0.000  0.000  0.000  0.000
  -0.500 -0.250  0.375  1.000  0.750  1.125]
 [ 0.625  1.125  0.375 -0.250 -0.500  0.000  0.000  0.000  0.000  0.000
   0.000 -0.500 -0.250  0.375  1.125  0.625]]

感受野处理后保存的图片为:

你可能感兴趣的:(脉冲神经网络)