2017-04-06 83 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