2017-04-06 78 views
0

我观察到当我使用pycaffe提取它们时,功能被覆盖。我的代码如下:为什么使用pycaffe提取功能时会覆盖功能?

tImg_1 = misc.imread('1.jpg') 
tImg_1 = tImg_1[:,:,::-1] # color channel swap 
tImg_2 = misc.imread('2.jpg') 
tImg_2 = tImg_2[:,:,::-1] # color channel swap 

tImg_1 = (np.float32(tImg_1)- 127.5)/128 # mean substruction 
tImg_2 = (np.float32(tImg_2)- 127.5)/128 # mean substruction 

tI_1 = np.moveaxis(tImg_1, 0, 1) # Transpose 
tI_2 = np.moveaxis(tImg_2, 0, 1) # Transpose 

# Extract features 
tI_1 = np.reshape(tImg_1, (1, tImg_1.shape[2], tImg_1.shape[0], tImg_1.shape[1])) 
tI_2 = np.reshape(tImg_2, (1, tImg_2.shape[2], tImg_2.shape[0],  tImg_2.shape[1]))     

net.blobs['data'].data[...] = tI_1 
net.forward() 
fts_1 = net.blobs['fc5'].data 
print(fts_1[0, 0])     

net.blobs['data'].data[...] = tI_2 
net.forward() 
fts_2 = net.blobs['fc5'].data     

print(fts_2[0, 0])     
print(fts_1[0, 0]) 

执行此提供了以下输出:

0.508398 
-0.176945 
-0.176945 

这意味着的fts_1的值是由fts_2覆盖。我怎样才能避免这个问题?

回答

1

fts_1只是指向net.blobs['fc5'].data。您需要对该对象进行深度复制。所以你的第一个任务应该是fts_1 = copy.deepcopy(net.blobs['fc5'].data)

+0

谢谢。那么这是一个python特定的问题。与咖啡无关! – Hasnat