2017-10-05 68 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