Exemple #1
0
    def joint_transform(image, mask, weight):
        # transforming to PIL image
        image, mask, weight = F.to_pil_image(image), F.to_pil_image(
            mask), F.to_pil_image(weight)

        # random crop
        i, j, h, w = T.RandomCrop.get_params(image, size)
        image, mask, weight = F.crop(image, i, j, h,
                                     w), F.crop(mask, i, j, h,
                                                w), F.crop(weight, i, j, h, w)
        if np.random.rand() < p_flip:
            image, mask, weight = F.hflip(image), F.hflip(mask), F.hflip(
                weight)

        # color transforms || ONLY ON IMAGE
        if color_tf is not None:
            image = color_tf(image)

        # transforming to tensor
        image, weight = F.to_tensor(image), F.to_tensor(weight)
        if not long_mask:
            mask = F.to_tensor(mask)
        else:
            mask = to_long_tensor(mask)

        return image, mask, weight
Exemple #2
0
 def log_image(image_tensor):
     global _image_log_count
     import torchvision.transforms.functional as F
     import os
     
     _image_log_count += 1
     os.makedirs("./logs.do_not_sync", exist_ok=True)
     image_path = f"./logs.do_not_sync/display_{_image_log_count}.png"
     F.to_pil_image(image_tensor).save(image_path)
     print("image logged: "+image_path)
Exemple #3
0
 def forward(self, input):
     input00=torch.empty(1,3,32,280).cpu().float()#.cuda()
     for ii in range(0,input.size()[0]):
         input0=input[ii,:,:,:].cpu()
         input0=F.to_pil_image(input0)
         input0=np.array(input0)
         input0=transform.resize(input0,(32,280,3)) 
         transform1 = transforms.Compose([transforms.ToTensor(), ])
         input0=transform1(input0).reshape(1,3,32,280)
         input00=torch.cat([input00,input0.cpu().float()],0)
     input00=input00[1:input00.size()[0],:,:,:]       
     #torch.Size([32, 3, 32, 280])#32是bach-size
     #print(type(input)) #<class 'torch.Tensor'>
     conv = self.cnn(input00.cuda())
     
     print('1')
     print(conv[0,:,:,:].sum())
     print('2')
     print(conv[:,0,:,:].sum())
     print('3')
     print(conv[:,:,0,:].sum())
     print('4')
     print(conv[:,:,:,0].sum())
     
     return conv
Exemple #4
0
 def train2(x,model, criterion, optimizer):
     #model.train2()
     #target = labels
     output=torch.empty(1,36352).cuda()
     labels=torch.empty(1).cuda()
     outget=torch.empty(1,512,1,71).cuda()
     for ii in range(0,x.size()[0]):
         x0=x[ii,:,:,:].cpu()
         x0=F.to_pil_image(x0)
         x0=np.array(x0)
         x0=transform.resize(x0,(32,280,3)) 
         transform1 = transforms.Compose([transforms.ToTensor(), ])
         x0=transform1(x0)
         x00 = sin2set(Variable(x0))
         output0 = model(Variable(x00.cpu().float()))
         output=torch.cat([output,output0],0)
         output00=output0.sum(dim=0).view(1,-1)
         output00=output00.reshape(1,512,1,71)
         output00=output00/output00[:,:,0,:].sum()   
         outget=torch.cat([outget,output00])
         labels0=torch.Tensor([ii,ii,ii]).cuda()
         labels=torch.cat([labels,labels0],0)
     labels=labels[1:labels.size()[0]]
     output=output[1:output.size()[0],:]
     outget=outget[1:outget.size()[0],:]
     loss = criterion(output, labels)
     optimizer.zero_grad()
     loss.backward(retain_graph=True)
     optimizer.step()
     #print('loss')
     #print(loss.item())
     return (loss.item(),outget)
Exemple #5
0
    def joint_transform(image, mask):
        # random magnification
        if random_resize:
            magnification_ratio = (random_resize[1] - random_resize[0]
                                   ) * np.random.rand() + random_resize[0]
            new_shape = (int(magnification_ratio * image.shape[0]),
                         int(magnification_ratio * image.shape[1]))
            image = img_as_ubyte(resize(image, new_shape))
            mask = resize(mask, new_shape)
            mask = img_as_ubyte(mask > 0.5)

        # resizing
        if image.shape[0] < size[0] or image.shape[1] < size[1]:
            new_im_shape = np.max([image.shape[0],
                                   size[0]]), np.max([image.shape[1],
                                                      size[1]]), 3
            new_mask_shape = np.max([image.shape[0], size[0]
                                     ]), np.max([image.shape[1], size[1]]), 1
            image = pad_to_shape(image, new_im_shape)
            mask = pad_to_shape(mask, new_mask_shape)

        # transforming to PIL image
        image, mask = F.to_pil_image(image), F.to_pil_image(mask)

        # random crop
        i, j, h, w = T.RandomCrop.get_params(image, size)
        image, mask = F.crop(image, i, j, h, w), F.crop(mask, i, j, h, w)
        if np.random.rand() < p_flip:
            image, mask = F.hflip(image), F.hflip(mask)

        # color transforms || ONLY ON IMAGE
        if color_tf is not None:
            image = color_tf(image)

        # transforming to tensor
        image = F.to_tensor(image)
        if not long_mask:
            mask = F.to_tensor(mask)
        else:
            mask = to_long_tensor(mask)

        # normalizing image
        if normalize:
            image = tf_normalize(image)

        return image, mask
Exemple #6
0
    def __call__(self, pic):
        """
        Args:
            pic (Tensor or numpy.ndarray): Image to be converted to PIL Image.

        Returns:
            PIL Image: Image converted to PIL Image.

        """
        return F.to_pil_image(pic, self.mode)
Exemple #7
0
    def transform(image):
        # transforming to PIL image
        image = F.to_pil_image(image)
        # random crop
        i, j, h, w = T.RandomCrop.get_params(image, size)
        image = F.crop(image, i, j, h, w)
        if np.random.rand() < p_flip:
            image = F.hflip(image)

        # color transforms || ONLY ON IMAGE
        if color_tf is not None:
            image = color_tf(image)

        # transforming to tensor
        image = F.to_tensor(image)

        return image
Exemple #8
0
    def single_transform(image):
        # resizing
        if image.shape[0] < size[0] or image.shape[1] < size[1]:
            new_im_shape = np.max([image.shape[0],
                                   size[0]]), np.max([image.shape[1],
                                                      size[1]]), 3
            image = resize(image, new_im_shape, preserve_range=True).astype(
                np.uint8)  #pad_to_shape(image, new_im_shape)

        # transforming to PIL image
        image = F.to_pil_image(image)

        # transforming to tensor
        image = F.to_tensor(image)

        # normalizing image
        if normalize:
            image = tf_normalize(image)

        return image
def visualize_model_cnn_output(image_size,
                               model,
                               image_transforms=None,
                               h=15,
                               w=15):
    image = None
    can_continue = True
    total_time = 0
    for m in model.modules():

        # The below condition checks for the first 'Conv2d' layer and
        # set the image variable and never gets executed there after.
        # I assume that there is no 'Conv2d' layer after the 'classifier' layer
        if (isinstance(m, nn.Dropout) or isinstance(m, nn.Dropout) or isinstance(m, nn.ReLU) or \
            isinstance(m, nn.Conv2d)  or isinstance(m, nn.MaxPool2d)  ) and can_continue:
            can_continue = False
            # read a image
            data_dir = 'data/dogImages_crop_350/'
            file = os.path.join(
                data_dir, 'train/002.Afghan_hound/Afghan_hound_00142.jpg')
            image = cv2.imread(file)
            image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
            #image = cv2.resize(image,(image_size,image_size))
            #crop_size=int(image_size*0.9)
            #image = crop_around_center(image, crop_size, crop_size)
            print('Original image size: ', image.shape)
            plt.imshow(image)
            plt.show()
            if image_transforms is not None:
                image = F.to_pil_image(image)
                for transform in image_transforms:
                    start = time.time()
                    image = transform(image)
                    end = time.time() - start
                    total_time += end
                    print('-' * 127)
                    print('transform: {}  \nimage size: {}  time: {}'.format(
                        transform,
                        np.array(image).shape, end))
                    print('-' * 127)
                    plt.imshow(np.array(image))
                    plt.show()

                #image = image.permute(1, 2, 0).numpy()
                image = np.array(image)
            # show the image

            # transfer the x(image) to tensor
            image = transforms.ToTensor()(image)
            image = image.unsqueeze(0)

        # Assuming that there should be no layer after classifier
        # layer, I set image to 'None' so that the function 'ShowImgMap'
        # below doesn't get called
        if isinstance(m, nn.Linear) or isinstance(m, nn.Sequential):
            image = None

        #
        if image is not None:
            image, total_time = ShowImgMap(image, m, h, w, total_time)

    print('Total time: {}'.format(total_time))