2017-10-05 43 views
1

我正在嘗試重新使用下面的代碼段。代碼gt_bg = gt_bg.reshape(*gt_bg.shape, 1)特定行給我的錯誤信息,如可能與python 2.7相關的一個代碼段錯誤與python 3.x

gt_bg = gt_bg.reshape(*gt_bg.shape, 1) 
SyntaxError: only named arguments may follow *expression 

我使用Python 2.7,這是什麼問題?如果是這種情況,如何修改它以使其適合Python2.7?謝謝。

for image_file in image_paths[batch_i:batch_i+batch_size]: 
      gt_image_file = label_paths[os.path.basename(image_file)] 

      image = scipy.misc.imresize(scipy.misc.imread(image_file), image_shape) 
      gt_image = scipy.misc.imresize(scipy.misc.imread(gt_image_file), image_shape) 

      gt_bg = np.all(gt_image == background_color, axis=2) 
      gt_bg = gt_bg.reshape(*gt_bg.shape, 1) 
      gt_image = np.concatenate((gt_bg, np.invert(gt_bg)), axis=2) 

      images.append(image) 
      gt_images.append(gt_image) 

回答

1

這並不是真的與Python 2/Python 3的區別,那是一個紅色的鯡魚。

numpy數組的重塑方法預計直接接收新形狀,作爲元組,不解壓縮到維度。如此,而不是這樣的:

gt_bg = gt_bg.reshape(*gt_bg.shape, 1) 

該公司預計本:

gt_bg = gt_bg.reshape(gt_bg.shape + (1,)) 

或者,如果你想成爲很酷,你可以設置形狀直接:

gt_bg.shape += (1,) 

或者,如果你想變得奇怪,你可以在一個切片中使用一個Ellipsis

gt_bg = gt_bg[...,None] 
+0

感謝您的回覆。聲稱原始代碼段可以正常運行python3.x。我在Python 2.7下運行它給了我錯誤的信息。這就是爲什麼我懷疑它是python版本問題。而且,gt_bg.shape +(1,)是什麼意思?謝謝 – user297850