1

I am trying to re-use the following code segment. The specific line of code gt_bg = gt_bg.reshape(*gt_bg.shape, 1) gives me the error messages such as

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

I am using Python 2.7, is this the problem? If that's the case, how to modify it to make it fit to Python2.7? Thanks.

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)
user297850
  • 7,705
  • 17
  • 54
  • 76

1 Answers1

1

This is not really related to the Python 2 / Python 3 difference, that's a red herring.

numpy array's reshape method expects to receive the new shape directly, as a tuple, not unpacked into dimensions. So, instead of this:

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

It's expecting this:

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

Or, if you want to be cool, you can just set the shape directly:

gt_bg.shape += (1,)

Or, if you want to be weird, you can use an Ellipsis in a slice:

gt_bg = gt_bg[...,None]
wim
  • 338,267
  • 99
  • 616
  • 750
  • thanks for the reply. The original code segment is claimed to be run fine for python3.x. My running it under Python 2.7 gave me the wrong message. That's why I suspect it is of the python version issue. Moreover, what does gt_bg.shape + (1,) mean? Thanks – user297850 Oct 05 '17 at 22:31