Channel change and blend method for image

username: rui_zhou
cooked: <p>I have use this method to image the image of 24bits (3 channels), but now the image change to 32bits with 4 channels, and it got this error , how can I edit it and use np.shape or other method? !<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/1/7/17704e15e690a553d0cac2bead03049356242a50.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/17704e15e690a553d0cac2bead03049356242a50" title="60"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/1/7/17704e15e690a553d0cac2bead03049356242a50_2_690x320.jpeg" alt="60" data-base62-sha1="3llAQbaa5ZhzWvyzxTbAaxoxpp6" width="690" height="320" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/1/7/17704e15e690a553d0cac2bead03049356242a50_2_690x320.jpeg, https://discuss.pytorch.org/uploads/default/original/3X/1/7/17704e15e690a553d0cac2bead03049356242a50.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/1/7/17704e15e690a553d0cac2bead03049356242a50.jpeg 2x" data-dominant-color="3C3D3E"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">60</span><span class="informations">978×454 42.1 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<blockquote>
<p>Blockquote<br>
def detect_image(self, image):</p>
</blockquote>
<pre><code>    old_img = copy.deepcopy(image)

    orininal_h = np.array(image).shape[0]

    orininal_w = np.array(image).shape[1]

    image, nw, nh = self.letterbox_image(image,(self.model_image_size[1],self.model_image_size[0]))

    images = [np.array(image)/255]

    images = np.transpose(images,(0,3,1,2))

    with torch.no_grad():

        images = Variable(torch.from_numpy(images).type(torch.FloatTensor))

        if self.cuda:

            images =images.cuda()

        pr = self.net(images)[0]

        pr = F.softmax(pr.permute(1,2,0),dim = -1).cpu().numpy().argmax(axis=-1)

        pr = pr[int((self.model_image_size[0]-nh)//2):int((self.model_image_size[0]-nh)//2+nh), int((self.model_image_size[1]-nw)//2):int((self.model_image_size[1]-nw)//2+nw)]

    seg_img = np.zeros((np.shape(pr)[0],np.shape(pr)[1],3))

    for c in range(self.num_classes):

        seg_img[:,:,0] += ((pr[:,: ] == c )*( self.colors[c][0] )).astype('uint8')

        seg_img[:,:,1] += ((pr[:,: ] == c )*( self.colors[c][1] )).astype('uint8')

        seg_img[:,:,2] += ((pr[:,: ] == c )*( self.colors[c][2] )).astype('uint8')

    image = Image.fromarray(np.uint8(seg_img)).resize((orininal_w,orininal_h))

    if self.blend:

        image = Image.blend(old_img,image,0.7)

    

    return image
</code></pre>

username: ptrblck
cooked: <p>This error is raised by <code>PIL</code>, which gets unexpected images and cannot blend them.<br>
Is the error raised in the forward pass of <code>self.net</code>?<br>
If so, it seems you are using <code>PIL</code> methods inside the model, while you are also passing tensors to its <code>forward</code>. Could you explain the use case a bit?</p>