Skip to content

ValueError: cannot reshape array #36

Description

@yueqiw

Hi,

I got the following error when running style transfer with segmentation masks.

Elapsed time in stylization: 0.003414
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
...
...
/home/yueqi/Dropbox/lib/FastPhotoStyle/process_stylization.pyc in stylization(p_wct, content_image_path, style_image_path, content_seg_path, style_seg_path, output_image_path, cuda)
     60 
     61     with Timer("Elapsed time in stylization: %f"):
---> 62         stylized_img = p_wct.transform(cont_img, styl_img, cont_seg, styl_seg)
     63     utils.save_image(stylized_img.data.cpu().float(), output_image_path, nrow=1)
     64 

/home/yueqi/Dropbox/lib/FastPhotoStyle/photo_wct.py in transform(self, cont_img, styl_img, cont_seg, styl_seg)
     33         sF4 = sF4.data.squeeze(0)
     34         cF4 = cF4.data.squeeze(0)
---> 35         csF4 = self.__feature_wct(cF4, sF4, cont_seg, styl_seg)
     36         Im4 = self.d4(csF4, cpool_idx, cpool1, cpool_idx2, cpool2, cpool_idx3, cpool3)
     37 

/home/yueqi/Dropbox/lib/FastPhotoStyle/photo_wct.py in __feature_wct(self, cont_feat, styl_feat, cont_seg, styl_seg)
     86                 if self.label_indicator[l] == 0:
     87                     continue
---> 88                 cont_mask = np.where(t_cont_seg.reshape(t_cont_seg.shape[0] * t_cont_seg.shape[1]) == l)
     89                 styl_mask = np.where(t_styl_seg.reshape(t_styl_seg.shape[0] * t_styl_seg.shape[1]) == l)
     90                 if cont_mask[0].size <= 0 or styl_mask[0].size <= 0:

ValueError: cannot reshape array of size 4128 into shape (1376,)

Fixing these two lines

t_cont_seg = np.asarray(Image.fromarray(cont_seg, mode='RGB').resize((cont_w, cont_h), Image.NEAREST))
t_styl_seg = np.asarray(Image.fromarray(styl_seg, mode='RGB').resize((styl_w, styl_h), Image.NEAREST))

to the following resolved the issue.

t_cont_seg = np.asarray(Image.fromarray(cont_seg).resize((cont_w, cont_h), Image.NEAREST))
t_styl_seg = np.asarray(Image.fromarray(styl_seg).resize((styl_w, styl_h), Image.NEAREST))

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions