使用pycaffe提取时为什么要覆盖这些功能?

时间:2017-04-06 11:38:34

标签: caffe 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 个答案:

答案 0 :(得分:1)

fts_1只是指向net.blobs['fc5'].data。您需要对对象进行深度复制。因此,您的第一个作业应为fts_1 = copy.deepcopy(net.blobs['fc5'].data)