0%

python object reference

Python function object reference导致的bug

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
imsave('./generated_inputs/' +str(iters) +'_0'+str(i)+ '_two.png', deprocess_image(imgs[0]))

def deprocess_image(x):
tmp = x
tmp = tmp.reshape((100, 100, 3))
# Remove zero-center by mean pixel
tmp[:, :, 0] += 103.939
tmp[:, :, 1] += 116.779
tmp[:, :, 2] += 123.68
# 'BGR'->'RGB'
tmp = tmp[:, :, ::-1]
tmp = np.clip(tmp, 0, 255).astype('uint8')
return tmp
// 如此添加tmp。x的值仍会一直改变。则重复调用deprocess无法得到相同结果。
// 正确做法 使用x.copy()
def deprocess_image(x):
tmp = x.copy()
tmp = tmp.reshape((100, 100, 3))
# Remove zero-center by mean pixel
tmp[:, :, 0] += 103.939
tmp[:, :, 1] += 116.779
tmp[:, :, 2] += 123.68
# 'BGR'->'RGB'
tmp = tmp[:, :, ::-1]
tmp = np.clip(tmp, 0, 255).astype('uint8')
return tmp