Beispiel #1
0
        def untangled_res(self, x, input):
            # B, 1(Green), H, W
            x_G = x[:, 1:2, :, :]
            input_G = input[:, 1:2, :, :]

            input_G = F.interpolate(input_G, scale_factor=2, mode='bicubic')
            lpass_G = kornia.gaussian_blur2d(input_G, (5, 5), (1.5, 1.5))
            residual_lp = lpass_G - input_G
            residual_out = x_G - input_G

            res_product = residual_lp * residual_out
            res_product = res_product + 0.5

            # texture - 1 , artifact - 0, flat - 0.5
            texture = torch.ones_like(res_product)  # G channel
            artifact = torch.zeros_like(res_product)  # G channel
            res_product = torch.where(res_product < 0.5, texture,
                                      res_product)  # 纹理异号
            texture_mask = torch.where(res_product == 1, texture,
                                       artifact)  # 纹理mask
            res_product = torch.where(res_product > 0.5, artifact,
                                      res_product)  # 噪声同号

            res_product = res_product + texture_mask

            return res_product
Beispiel #2
0
def gaussian_blur(hms, kernel):
    """
    Heatmap distribution modulation

    the heatmaps predicted by a human pose estimation model do not exhibit good-shaped Gaussian structure
    compared to the training heatmap data. we propose to exploit a Gaussian kernel K with the same variation
    as the training data to smooth out the effects of multiple peaks in the heatmap while keeping the
    original maximum activation location.

    params:
    @ hms: numpy.ndarray([batch_size, 21(num_joints), height, width])
    @ kernel: int
    """
    border = (kernel - 1) // 2
    batch_size = hms.shape[0]
    num_joints = hms.shape[1]
    height = hms.shape[2]
    width = hms.shape[3]
    sigma = (kernel - 1) // 3

    for i in range(batch_size):
        for j in range(num_joints):
            origin_max = torch.max(hms[i, j])
            dr = torch.zeros(
                (height + 2 * border, width + 2 * border))  # zero-padding
            dr[border:-border, border:-border] = hms[i, j].clone()
            dr = kornia.gaussian_blur2d(
                dr.view(1, 1, dr.shape[0], dr.shape[1]), (kernel, kernel),
                (sigma, sigma))

            hms[i, j] = dr[0, 0, border:-border, border:-border].clone()
            hms[i, j] *= origin_max / torch.max(hms[i, j])
    return hms
Beispiel #3
0
def set_binary_maps_as_target(dataset: Dataset, invert: bool = False, binary_images: List[np.ndarray] = None,
                              smoothen_labels: bool = False) -> Dataset:
    if binary_images is None:
        binary_images = parse_binary_maps(copy.deepcopy(dataset.observations), invert=invert)
    binary_images = torch.stack([torch.as_tensor(b).unsqueeze(0) for b in binary_images], dim=0)
    # smoothen binary maps to punish line predictions close to line less severely
    if smoothen_labels:
        binary_images = gaussian_blur2d(binary_images, kernel_size=(11, 11), sigma=(4, 4))
    dataset.actions = [b.squeeze() for b in binary_images]
    return dataset
Beispiel #4
0
    def usm(x, kernel_size=(7, 7), amount=1.0, threshold=0):
        res = x.clone()

        blurred = gaussian_blur2d(x, kernel_size=kernel_size, sigma=(1.0, 1.0))
        sharpened = res * (amount + 1.0) - amount * blurred

        if threshold > 0:
            sharpened = torch.where(torch.abs(res - blurred) < threshold, sharpened, res)

        return F.relu(sharpened, inplace=True)
 def forward(self, x):
     if self.filter_type == 'Median_Filter':
         x_denoised = kornia.median_blur(x, (self.ksize, self.ksize))
     elif self.filter_type == 'Mean_Filter':
         x_denoised = kornia.box_blur(x, (self.ksize, self.ksize))
     elif self.filter_type == 'Gaussian_Filter':
         x_denoised = kornia.gaussian_blur2d(
             x, (self.ksize, self.ksize),
             (0.3 * ((x.shape[3] - 1) * 0.5 - 1) + 0.8, 0.3 *
              ((x.shape[2] - 1) * 0.5 - 1) + 0.8))
     new_x = x + self.conv(x_denoised)
     return new_x
Beispiel #6
0
 def forward(self, x):
     if self.filter_type == 'Median_Filter':
         x_denoised = kornia.median_blur(x, (self.ksize, self.ksize))
     elif self.filter_type == 'Mean_Filter':
         x_denoised = kornia.box_blur(x, (self.ksize, self.ksize))
     elif self.filter_type == 'Gaussian_Filter':
         x_denoised = kornia.gaussian_blur2d(
             x, (self.ksize, self.ksize),
             (0.3 * ((x.shape[3] - 1) * 0.5 - 1) + 0.8, 0.3 *
              ((x.shape[2] - 1) * 0.5 - 1) + 0.8))
     elif self.filter_type == "NonLocal_Filter":
         x_denoised = self.non_local_op(x, self.embed, self.softmax)
     new_x = x + self.conv_1x1(x_denoised)
     return new_x
Beispiel #7
0
def smooth_noise(img: torch.Tensor,
                 ksize: int,
                 std: float,
                 p: float = 1.0,
                 inplace: bool = True) -> torch.Tensor:
    """
    Smoothens (blurs) the given noise images with a Gaussian kernel.
    :param img: torch tensor (n x c x h x w).
    :param ksize: the kernel size used for the Gaussian kernel.
    :param std: the standard deviation used for the Gaussian kernel.
    :param p: the chance smoothen an image, on average smoothens p * n images.
    :param inplace: whether to apply the operation inplace.
    """
    if not inplace:
        img = img.clone()
    ksize = ksize if ksize % 2 == 1 else ksize - 1
    picks = torch.from_numpy(np.random.binomial(1, p, size=img.size(0))).bool()
    if picks.sum() > 0:
        img[picks] = gaussian_blur2d(img[picks], (ksize, ) * 2, (std, ) * 2)
    return img
Beispiel #8
0
def main():
    try:
        img_bgr: np.ndarray = cv2.imread('model8.png', cv2.IMREAD_COLOR)
        x_bgr: torch.Tensor = kornia.image_to_tensor(img_bgr)
        x_rgb: torch.Tensor = kornia.bgr_to_rgb(x_bgr)
        x_rgb = x_rgb.expand(2, -1, -1, -1)
        x_rgb = x_rgb.float() / 255.

        imshow(x_rgb)
        # Box Blur
        x_blur: torch.Tensor = kornia.box_blur(x_rgb, (9, 9))
        imshow(x_blur)
        # Median Blur
        x_blur: torch.Tensor = kornia.median_blur(x_rgb, (5, 5))
        imshow(x_blur)
        # Gaussian Blur
        x_blur: torch.Tensor = kornia.gaussian_blur2d(x_rgb, (11, 11), (11., 11.))
        imshow(x_blur)

    except:
        print("Error found")
def Blur(x):
    return kornia.gaussian_blur2d(x, (3, 3), (1.0, 1.0))
Beispiel #10
0
    def _image_processing(self,
                          imgs: Tensor,
                          input_shape: torch.Size,
                          blur: bool = False,
                          maxres: int = 64,
                          qu: float = None,
                          norm: str = 'local',
                          colorize: bool = False,
                          ref: Tensor = None,
                          cmap: str = 'jet') -> Tensor:
        """
        Applies basic image processing techniques, including resizing, blurring, colorizing, and normalizing.
        The resize operation resizes the images automatically to match the input_shape. Other transformations
        are optional. Can be used to create pseudocolored heatmaps!
        :param imgs: a tensor of some images.
        :param input_shape: the shape of the inputs images the data loader returns.
        :param blur: whether to blur the image (has no effect for FCDD anomaly scores, where the
            anomaly scores are upsampled using a Gaussian kernel anyway).
        :param maxres: maximum allowed resolution in pixels (images are downsampled if they exceed this threshold).
        :param norm: what type of normalization to apply.
            None: no normalization.
            'local': normalizes each image w.r.t. itself only.
            'global': normalizes each image w.r.t. to ref (ref defaults to imgs).
        :param qu: quantile used for normalization, qu=1 yields the typical 0-1 normalization.
        :param colorize: whether to colorize grayscaled images using colormaps (-> pseudocolored heatmaps!).
        :param ref: a tensor of images used for global normalization (defaults to imgs).
        :param cmap: the colormap that is used to colorize grayscaled images.
        :return: transformed tensor of images
        """
        imgs = imgs.detach().clone()
        assert imgs.dim() == len(input_shape) == 4  # n x c x h x w
        std = self.gauss_std
        if qu is None:
            qu = self.quantile

        # upsample if necessary (img.shape != input_shape)
        if imgs.shape[2:] != input_shape[2:]:
            assert isinstance(self.net, ReceptiveNet), \
                'Some images are not of full resolution, and network is not a receptive net. This should not occur! '
            imgs = self.net.receptive_upsample(imgs, reception=True, std=std)

        # blur if requested
        if blur:
            if isinstance(self.net, ReceptiveNet):
                r = self.net.reception['r']
            elif self.objective == 'hsc':
                r = self.net.fcdd_cls(self.net.in_shape,
                                      bias=True).reception['r']
            elif self.objective == 'ae':
                enc = self.net.encoder
                if isinstance(enc, ReceptiveNet):
                    r = enc.reception['r']
                else:
                    r = enc.fcdd_cls(enc.in_shape, bias=True).reception['r']
            else:
                raise NotImplementedError()
            r = (r - 1) if r % 2 == 0 else r
            std = std or kernel_size_to_std(r)
            imgs = gaussian_blur2d(imgs, (r, ) * 2, (std, ) * 2)

        # downsample if resolution exceeds the limit given with maxres
        if maxres < max(imgs.shape[2:]):
            assert imgs.shape[-2] == imgs.shape[
                -1], 'Image provided is no square!'
            imgs = F.interpolate(imgs, (maxres, maxres), mode='nearest')

        # apply requested normalization
        if norm is not None:
            apply_norm = {
                'local': self.__local_norm,
                'global': self.__global_norm
            }
            imgs = apply_norm[norm](imgs, qu, ref)

        # if image is grayscaled, colorize, i.e. provide a pseudocolored heatmap!
        if colorize:
            imgs = imgs.mean(1).unsqueeze(1)
            imgs = colorize_img([
                imgs,
            ], norm=False, cmap=cmap)[0]
        else:
            imgs = imgs.repeat(1, 3, 1, 1) if imgs.size(1) == 1 else imgs

        return imgs
Beispiel #11
0
# Create batch and normalize
x_rgb = x_rgb.expand(2, -1, -1, -1)  # 4xCxHxW
x_rgb = x_rgb.float() / 255.


def imshow(input: torch.Tensor):
    out: torch.Tensor = torchvision.utils.make_grid(input, nrow=2, padding=1)
    out_np: np.array = kornia.tensor_to_image(out)
    plt.imshow(out_np)
    plt.axis('off')


#############################
# Show original
imshow(x_rgb)

#############################
# Box Blur
x_blur: torch.Tensor = kornia.box_blur(x_rgb, (9, 9))
imshow(x_blur)

#############################
# Media Blur
x_blur: torch.Tensor = kornia.median_blur(x_rgb, (5, 5))
imshow(x_blur)

#############################
# Gaussian Blur
x_blur: torch.Tensor = kornia.gaussian_blur2d(x_rgb, (11, 11), (11., 11.))
imshow(x_blur)
 def test_diag(self, device, dtype):
     input = torch.tensor(
         [[[[0., 0., 0., 0, 0], [0., 0., 0.0, 0, 0.], [0., 0, 0., 0, 0.],
            [0., 0., 0, 0, 0.], [0., 0., 0., 0, 0.]],
           [[0., 0., 0., 0, 0], [0., 0., 1, 0, 0.], [0., 1, 1.2, 1.1, 0.],
            [0., 0., 1., 0, 0.], [0., 0., 0., 0, 0.]],
           [
               [0., 0., 0., 0, 0],
               [0., 0., 0.0, 0, 0.],
               [0., 0, 0., 0, 0.],
               [0., 0., 0, 0, 0.],
               [0., 0., 0., 0, 0.],
           ]]],
         device=device,
         dtype=dtype)
     input = kornia.gaussian_blur2d(input, (5, 5), (0.5, 0.5)).unsqueeze(0)
     softargmax = kornia.geometry.ConvQuadInterp3d(10)
     expected_val = torch.tensor(
         [[[[[0., 0., 0., 0, 0], [0., 0., 0.0, 0, 0.], [0., 0, 0., 0, 0.],
             [0., 0., 0, 0, 0.], [0., 0., 0., 0, 0.]],
            [[2.2504e-04, 2.3146e-02, 1.6808e-01, 2.3188e-02, 2.3628e-04],
             [2.3146e-02, 1.8118e-01, 7.4338e-01, 1.8955e-01, 2.5413e-02],
             [1.6807e-01, 7.4227e-01, 1.1086e+01, 8.0414e-01, 1.8482e-01],
             [2.3146e-02, 1.8118e-01, 7.4338e-01, 1.8955e-01, 2.5413e-02],
             [2.2504e-04, 2.3146e-02, 1.6808e-01, 2.3188e-02, 2.3628e-04]],
            [[0., 0., 0., 0, 0], [0., 0., 0.0, 0, 0.], [0., 0, 0., 0, 0.],
             [0., 0., 0, 0, 0.], [0., 0., 0., 0, 0.]]]]],
         device=device,
         dtype=dtype)
     expected_coord = torch.tensor(
         [[[[[[0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0],
              [0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0],
              [0.0, 0.0, 0.0, 0.0, 0.0]],
             [[1.0, 1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0, 1.0],
              [1.0, 1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0, 1.0],
              [1.0, 1.0, 1.0, 1.0, 1.0]],
             [[2.0, 2.0, 2.0, 2.0, 2.0], [2.0, 2.0, 2.0, 2.0, 2.0],
              [2.0, 2.0, 2.0, 2.0, 2.0], [2.0, 2.0, 2.0, 2.0, 2.0],
              [2.0, 2.0, 2.0, 2.0, 2.0]]],
            [[[0.0, 1.0, 2.0, 3.0, 4.0], [0.0, 1.0, 2.0, 3.0, 4.0],
              [0.0, 1.0, 2.0, 3.0, 4.0], [0.0, 1.0, 2.0, 3.0, 4.0],
              [0.0, 1.0, 2.0, 3.0, 4.0]],
             [[0.0, 1.0, 2.0, 3.0, 4.0], [0.0, 1.0, 2.0, 3.0, 4.0],
              [0.0, 1.0, 2.0, 3.0, 4.0], [0.0, 1.0, 2.0, 3.0, 4.0],
              [0.0, 1.0, 2.0, 3.0, 4.0]],
             [[0.0, 1.0, 2.0, 3.0, 4.0], [0.0, 1.0, 2.0, 3.0, 4.0],
              [0.0, 1.0, 2.0, 3.0, 4.0], [0.0, 1.0, 2.0, 3.0, 4.0],
              [0.0, 1.0, 2.0, 3.0, 4.0]]],
            [[[0.0, 0.0, 0.0, 0.0, 0.0], [1.0, 1.0, 1.0, 1.0, 1.0],
              [2.0, 2.0, 2.0, 2.0, 2.0], [3.0, 3.0, 3.0, 3.0, 3.0],
              [4.0, 4.0, 4.0, 4.0, 4.0]],
             [[0.0, 0.0, 0.0, 0.0, 0.0], [1.0, 1.0, 1.0, 1.0, 1.0],
              [2.0, 2.0, 2.0495, 2.0, 2.0], [3.0, 3.0, 3.0, 3.0, 3.0],
              [4.0, 4.0, 4.0, 4.0, 4.0]],
             [[0.0, 0.0, 0.0, 0.0, 0.0], [1.0, 1.0, 1.0, 1.0, 1.0],
              [2.0, 2.0, 2.0, 2.0, 2.0], [3.0, 3.0, 3.0, 3.0, 3.0],
              [4.0, 4.0, 4.0, 4.0, 4.0]]]]]],
         device=device,
         dtype=dtype)
     coords, val = softargmax(input)
     assert_allclose(val, expected_val, atol=1e-4, rtol=1e-4)
     assert_allclose(coords, expected_coord, atol=1e-4, rtol=1e-4)
Beispiel #13
0
    def backward_G(self,epoch):

        self.loss_G_GAN_A = 0
        self.loss_G_GAN_B = 0
        self.loss_G_GAN_flash_B = 0
        self.loss_G_GAN_flash_A = 0
        # comment loss on recrations
        # self.loss_G_GAN_recA = 0
        # self.loss_G_GAN_recB = 0


        self.loss_cycle_A = 0
        self.loss_cycle_B = 0
        # self.loss_G_L1_A_comp = 0
        # self.loss_G_L1_B_comp = 0

        self.loss_G_L1_A_comp_color = 0
        self.loss_G_L1_B_comp_color = 0
        self.loss_color_dslr_A = 0
        self.loss_color_dslr_B = 0

        if self.opt.D_flash:
            fake_AC = torch.cat((self.real_A, self.flash_from_decomposition), 1)
            pred_fake = self.netD_Flash(fake_AC)
            self.loss_G_GAN_flash_A = self.criterionGAN(pred_fake, True)
            fake_BC = torch.cat((self.real_B, self.flash_from_generation), 1)
            pred_fake = self.netD_Flash(fake_BC)
            self.loss_G_GAN_flash_B = self.criterionGAN(pred_fake, True)
        else:
            fake_AB = torch.cat((self.real_A, self.fake_B), 1)
            pred_fake = self.netD_Decompostion(fake_AB)
            self.loss_G_GAN_A = self.criterionGAN(pred_fake, True)
            fake_AB_B = torch.cat((self.real_B, self.fake_A), 1)
            pred_fake = self.netD_Generation(fake_AB_B)
            self.loss_G_GAN_B = self.criterionGAN(pred_fake, True)

            # fake_AB = torch.cat((self.real_A, self.rec_A), 1)
            # pred_fake = self.netD_Decompostion(fake_AB)
            # self.loss_G_GAN_recA = self.criterionGAN(pred_fake, True)
            # fake_AB_B = torch.cat((self.real_B, self.rec_B), 1)
            # pred_fake = self.netD_Generation(fake_AB_B)
            # self.loss_G_GAN_recB = self.criterionGAN(pred_fake, True)

        # ## Flash L1 loss
        # if self.opt.lambda_comp != 0:
        #     self.loss_G_L1_A_comp = self.criterionL1(self.flash_from_decomposition, self.real_C) * self.opt.lambda_comp
        #     self.loss_G_L1_B_comp = self.criterionL1(self.flash_from_generation, self.real_C) * self.opt.lambda_comp


        ## Flash Color Loss
        if self.opt.lambda_color_uv != 0:
            fake_C_A = kornia.rgb_to_yuv(self.flash_from_decomposition)[:,1:2,:,:]

            fake_C_B = kornia.rgb_to_yuv(self.flash_from_generation)[:,1:2,:,:]

            real_C_color = kornia.rgb_to_yuv(self.real_C)[:,1:2,:,:]

            self.loss_G_L1_A_comp_color = self.criterionL1(fake_C_A, real_C_color) * self.opt.lambda_color_uv
            self.loss_G_L1_B_comp_color = self.criterionL1(fake_C_B, real_C_color) * self.opt.lambda_color_uv


        if epoch >= self.opt.cycle_epoch:
            self.loss_cycle_A = self.criterionCycle(self.rec_A, self.real_A) * self.opt.lambda_A
            self.loss_cycle_B = self.criterionCycle(self.rec_B, self.real_B) * self.opt.lambda_B

        if self.opt.dslr_color_loss:
            real_A_blurred = kornia.gaussian_blur2d(self.real_A, (21, 21), (3, 3))
            real_B_blurred = kornia.gaussian_blur2d(self.real_B, (21, 21), (3, 3))
            fake_A_blurred = kornia.gaussian_blur2d(self.fake_A, (21, 21), (3, 3))
            fake_B_blurred = kornia.gaussian_blur2d(self.fake_B, (21, 21), (3, 3))
            self.loss_color_dslr_A = self.criterionL1(real_A_blurred, fake_A_blurred) * self.opt.dslr_color_loss
            self.loss_color_dslr_B = self.criterionL1(real_B_blurred, fake_B_blurred) * self.opt.dslr_color_loss


        self.loss_G_L1_A = self.criterionL1(self.fake_A, self.real_A) * self.opt.lambda_L1
        self.loss_G_L1_B = self.criterionL1(self.fake_B, self.real_B) * self.opt.lambda_L1

        self.loss_G =self.loss_color_dslr_A + self.loss_color_dslr_B + \
                     self.loss_G_L1_B_comp_color + self.loss_G_L1_A_comp_color +\
                     self.loss_G_GAN_flash_A + self.loss_G_GAN_flash_B +\
                     self.loss_G_GAN_B + self.loss_G_GAN_A +\
                     self.loss_cycle_A + self.loss_cycle_B +\
                     self.loss_G_L1_A+ self.loss_G_L1_B

        self.loss_G.backward()
Beispiel #14
0
    def train(epoch):
        epoch_loss_g = 0
        epoch_loss_d = 0

        netG.train()
        for iteration, batch in enumerate(training_loader, 1):
            inputs, target, info, pqf = batch[0].to(device), batch[1].to(
                device), batch[2].to(device), batch[3].to(device)

            real_labels = torch.ones((target.size(0), 1, 4, 4)).to(device)
            fake_labels = torch.zeros((target.size(0), 1, 4, 4)).to(device)

            optimizer_netG.zero_grad()

            ##########################
            #   training generator   #
            ##########################

            # LR to HR
            i4, i3, i2, i1, i0 = netG(inputs, info, pqf)

            b, _, h4, w4 = i4.size()
            b, _, h3, w3 = i3.size()
            b, _, h2, w2 = i2.size()
            b, _, h1, w1 = i1.size()

            target_4 = F.interpolate(target,
                                     size=(h4, w4),
                                     mode='bilinear',
                                     align_corners=False)
            target_3 = F.interpolate(target,
                                     size=(h3, w3),
                                     mode='bilinear',
                                     align_corners=False)
            target_2 = F.interpolate(target,
                                     size=(h2, w2),
                                     mode='bilinear',
                                     align_corners=False)
            target_1 = F.interpolate(target,
                                     size=(h1, w1),
                                     mode='bilinear',
                                     align_corners=False)

            i0_lp = kornia.gaussian_blur2d(i0, (5, 5), (1.0, 1.0))
            i0_hp = i0 - i0_lp
            i0_hp = 0.5 + i0_hp * 0.5

            target_lp = kornia.gaussian_blur2d(target, (5, 5), (1.0, 1.0))
            target_hp = target - target_lp
            target_hp = 0.5 + target_hp * 0.5

            # loss_c1 = criterion_c(i4, target_4)
            # loss_c2 = criterion_c(i3, target_3)
            loss_c3 = criterion_c(i2, target_2)
            loss_c4 = criterion_c(i1, target_1)
            loss_c5 = criterion_c(i0_lp, target_lp)
            loss_c6 = criterion_c(i0, target)
            loss_gdl = criterion_g(i0, target)

            loss_lpips = loss_fn.forward(i0 * 2 - 1, target * 2 - 1)
            loss_lpips = torch.mean(loss_lpips)

            score_real = netD(target_hp)
            score_fake = netD(i0_hp)
            discriminator_rf = score_real - score_fake.mean()
            discriminator_fr = score_fake - score_real.mean()
            adversarial_loss_rf = criterion_adv(discriminator_rf, fake_labels)
            adversarial_loss_fr = criterion_adv(discriminator_fr, real_labels)
            loss_adv = (adversarial_loss_fr + adversarial_loss_rf) / 2

            netG_loss = loss_c3 * 0.08 + loss_c4 * 0.1 + loss_c5 * 1 + loss_c6 * 1 + loss_gdl + loss_lpips * 1.2 + loss_adv * 0.002

            epoch_loss_g += netG_loss.item()

            netG_loss.backward()
            optimizer_netG.step()

            print('===========> G_Epoch[{}]({}/{}): Loss: {:.6f}'.format(
                epoch, iteration, len(training_loader), netG_loss))

            ##########################
            # training discriminator #
            ##########################
            '''
            optimizer_netD.zero_grad()

            score_real = netD(target_hp)
            score_fake = netD(i0_hp.detach())
            discriminator_rf = score_real - score_fake.mean()
            discriminator_fr = score_fake - score_real.mean()
            adversarial_loss_rf = criterion_adv(discriminator_rf, real_labels)
            adversarial_loss_fr = criterion_adv(discriminator_fr, fake_labels)
            netD_loss = (adversarial_loss_fr + adversarial_loss_rf) / 2

            epoch_loss_d += netD_loss.item()
            netD_loss.backward()
            optimizer_netD.step()
            
            print('===> D_Epoch[{}]({}/{}): Loss: {:.6f}'.format(epoch, iteration, len(training_loader), netD_loss.item()))
            '''

        print('===> Epoch {} Complete: Avg. G_Loss: {:.6f}'.format(
            epoch, epoch_loss_g / len(training_loader)))
        # print('===> Epoch {} Complete: Avg. D_Loss: {:.6f}'.format(epoch, epoch_loss_d / len(training_loader)))

        logging.info('Epoch Avg. GLoss : {:.6f}'.format(epoch_loss_g /
                                                        len(training_loader)))
Beispiel #15
0
    def fit_on_blur(self,
                    root_dir,
                    num_epochs,
                    batch_size,
                    val_split=0.1,
                    num_workers=4):
        image_datasets = datasets.ImageFolder(root_dir, self.data_transforms)
        image_datasets.classes = [
            self.translate[i] for i in image_datasets.classes
        ]
        train_size = int((1 - val_split) * len(image_datasets))
        train, valid = torch.utils.data.random_split(
            image_datasets,
            [train_size, len(image_datasets) - train_size])
        dataloaders = {
            'train':
            torch.utils.data.DataLoader(train,
                                        batch_size=batch_size,
                                        shuffle=True,
                                        num_workers=num_workers),
            'val':
            torch.utils.data.DataLoader(valid,
                                        batch_size=batch_size,
                                        shuffle=True,
                                        num_workers=num_workers)
        }

        criterion = nn.CrossEntropyLoss()
        optimizer = optim.Adam(self.parameters(), lr=0.0001)
        epoch_bar = tqdm.tqdm(range(num_epochs), desc='Epochs---', position=0)
        self.train_epoch_loss = []
        self.val_epoch_loss = []
        train_acc = 0.
        val_acc = 0.
        for epoch in epoch_bar:
            self.train()
            pbar = tqdm.tqdm(dataloaders['train'],
                             desc='Epoch= {}'.format(epoch + 1),
                             position=1)
            running_loss = 0.0
            val_loss = 0.0
            total = 0
            correct = 0
            for inputs, labels in pbar:
                inputs = inputs.to(self.device)
                labels = labels.to(self.device)
                inputs = torch.stack([
                    kornia.gaussian_blur2d(inputs[i].view(1, 3, 224, 224),
                                           kernel_size=(25, 25),
                                           sigma=(4, 4)).view(3, 224, 224)
                    for i in range(len(inputs))
                ])
                # zero the parameter gradients
                optimizer.zero_grad()
                labels_hat = self(inputs)
                _, predicted = torch.max(labels_hat.data, 1)
                loss = criterion(labels_hat, labels)
                total += labels.size(0)
                correct += (predicted == labels).sum().item()
                pbar.set_postfix(train_run_loss='{:.4f}'.format(loss.item()))
                running_loss += loss.item()
                loss.backward()
                optimizer.step()
            self.train_epoch_loss.append(running_loss /
                                         len(dataloaders['train']))
            train_acc = 100 * correct / total
            epoch_bar.set_postfix(train_acc='{:.4f}'.format(train_acc),
                                  val_acc='{:.4f}'.format(val_acc))
            #             pbar = tqdm.tqdm(dataloaders['val'],desc='Epoch={}, train_epoch_loss= {}'.format(epoch+1,epoch_loss))
            total = 0
            correct = 0
            with torch.no_grad():
                self.eval()
                for inputs, labels in dataloaders['val']:
                    inputs = inputs.to(self.device)
                    inputs = torch.stack([
                        kornia.gaussian_blur2d(inputs[i].view(1, 3, 224, 224),
                                               kernel_size=(25, 25),
                                               sigma=(4, 4)).view(3, 224, 224)
                        for i in range(len(inputs))
                    ])
                    labels = labels.to(self.device)
                    labels_hat = self(inputs)
                    _, predicted = torch.max(labels_hat.data, 1)
                    total += labels.size(0)
                    correct += (predicted == labels).sum().item()
                    loss = criterion(labels_hat, labels)
                    val_loss += loss.item()
                self.val_epoch_loss.append(val_loss / len(dataloaders['val']))
                val_acc = 100 * correct / total
                epoch_bar.set_postfix(train_acc='{:.4f}'.format(train_acc),
                                      val_acc='{:.4f}'.format(val_acc))
Beispiel #16
0
        def op_script(img):

            return kornia.gaussian_blur2d(img, (5, 5), (1.2, 1.2), "replicate")
Beispiel #17
0
 def test_diag(self, device):
     input = torch.tensor([[[[0., 0., 0., 0, 0], [0., 0., 0.0, 0, 0.],
                             [0., 0, 0., 0, 0.], [0., 0., 0, 0, 0.],
                             [0., 0., 0., 0, 0.]],
                            [[0., 0., 0., 0, 0], [0., 0., 1, 0, 0.],
                             [0., 1, 1.2, 1.1, 0.], [0., 0., 1., 0, 0.],
                             [0., 0., 0., 0, 0.]],
                            [
                                [0., 0., 0., 0, 0],
                                [0., 0., 0.0, 0, 0.],
                                [0., 0, 0., 0, 0.],
                                [0., 0., 0, 0, 0.],
                                [0., 0., 0., 0, 0.],
                            ]]]).to(device)
     input = kornia.gaussian_blur2d(input, (5, 5), (0.5, 0.5)).unsqueeze(0)
     softargmax = kornia.geometry.ConvQuadInterp3d(1)
     expected_val = torch.tensor(
         [[[[[0., 0., 0., 0, 0], [0., 0., 0.0, 0, 0.], [0., 0, 0., 0, 0.],
             [0., 0., 0, 0, 0.], [0., 0., 0., 0, 0.]],
            [[2.2504e-04, 2.3146e-02, 1.6808e-01, 2.3188e-02, 2.3628e-04],
             [2.3146e-02, 1.8118e-01, 7.4338e-01, 1.8955e-01, 2.5413e-02],
             [1.6807e-01, 7.4227e-01, 2.1722e+00, 8.0414e-01, 1.8482e-01],
             [2.3146e-02, 1.8118e-01, 7.4338e-01, 1.8955e-01, 2.5413e-02],
             [2.2504e-04, 2.3146e-02, 1.6808e-01, 2.3188e-02, 2.3628e-04]],
            [[0., 0., 0., 0, 0], [0., 0., 0.0, 0, 0.], [0., 0, 0., 0, 0.],
             [0., 0., 0, 0, 0.], [0., 0., 0., 0, 0.]]]]]).to(device)
     expected_coord = torch.tensor(
         [[[[[[0.0000, 0.0000, 0.0000, 0.0000, 0.0000],
              [0.0000, 0.0000, 0.0000, 0.0000, 0.0000],
              [0.0000, 0.0000, 0.0000, 0.0000, 0.0000],
              [0.0000, 0.0000, 0.0000, 0.0000, 0.0000],
              [0.0000, 0.0000, 0.0000, 0.0000, 0.0000]],
             [[1.0000, 1.0000, 1.0000, 1.0000, 1.0000],
              [1.0000, 1.0000, 1.0000, 1.0000, 1.0000],
              [1.0000, 1.0000, 1.0688, 1.0000, 1.0000],
              [1.0000, 1.0000, 1.0000, 1.0000, 1.0000],
              [1.0000, 1.0000, 1.0000, 1.0000, 1.0000]],
             [[2.0000, 2.0000, 2.0000, 2.0000, 2.0000],
              [2.0000, 2.0000, 2.0000, 2.0000, 2.0000],
              [2.0000, 2.0000, 2.0000, 2.0000, 2.0000],
              [2.0000, 2.0000, 2.0000, 2.0000, 2.0000],
              [2.0000, 2.0000, 2.0000, 2.0000, 2.0000]]],
            [[[0.0000, 1.0000, 2.0000, 3.0000, 4.0000],
              [0.0000, 1.0000, 2.0000, 3.0000, 4.0000],
              [0.0000, 1.0000, 2.0000, 3.0000, 4.0000],
              [0.0000, 1.0000, 2.0000, 3.0000, 4.0000],
              [0.0000, 1.0000, 2.0000, 3.0000, 4.0000]],
             [[0.0000, 1.0000, 2.0000, 3.0000, 4.0000],
              [0.0000, 1.0000, 2.0000, 3.0000, 4.0000],
              [0.0000, 1.0000, 1.8366, 3.0000, 4.0000],
              [0.0000, 1.0000, 2.0000, 3.0000, 4.0000],
              [0.0000, 1.0000, 2.0000, 3.0000, 4.0000]],
             [[0.0000, 1.0000, 2.0000, 3.0000, 4.0000],
              [0.0000, 1.0000, 2.0000, 3.0000, 4.0000],
              [0.0000, 1.0000, 2.0000, 3.0000, 4.0000],
              [0.0000, 1.0000, 2.0000, 3.0000, 4.0000],
              [0.0000, 1.0000, 2.0000, 3.0000, 4.0000]]],
            [[[0.0000, 0.0000, 0.0000, 0.0000, 0.0000],
              [1.0000, 1.0000, 1.0000, 1.0000, 1.0000],
              [2.0000, 2.0000, 2.0000, 2.0000, 2.0000],
              [3.0000, 3.0000, 3.0000, 3.0000, 3.0000],
              [4.0000, 4.0000, 4.0000, 4.0000, 4.0000]],
             [[0.0000, 0.0000, 0.0000, 0.0000, 0.0000],
              [1.0000, 1.0000, 1.0000, 1.0000, 1.0000],
              [2.0000, 2.0000, 2.0244, 2.0000, 2.0000],
              [3.0000, 3.0000, 3.0000, 3.0000, 3.0000],
              [4.0000, 4.0000, 4.0000, 4.0000, 4.0000]],
             [[0.0000, 0.0000, 0.0000, 0.0000, 0.0000],
              [1.0000, 1.0000, 1.0000, 1.0000, 1.0000],
              [2.0000, 2.0000, 2.0000, 2.0000, 2.0000],
              [3.0000, 3.0000, 3.0000, 3.0000, 3.0000],
              [4.0000, 4.0000, 4.0000, 4.0000, 4.0000]]]]]]).to(device)
     coords, val = softargmax(input)
     assert_allclose(val, expected_val)
     assert_allclose(coords, expected_coord)
Beispiel #18
0
def gaussian_blur_mask(image: torch.Tensor, num_channels: int) -> torch.Tensor:
    mask = torch.stack([torch.as_tensor(image)] * num_channels, dim=0)
    kernel_size = int(np.random.choice(list(range(4))) * 2 + 1)  # select random (but odd) kernel size
    sigma = np.random.uniform(0.1, 3)  # select deviation
    return gaussian_blur2d(mask.unsqueeze(0), kernel_size=(kernel_size, kernel_size),
                           sigma=(sigma, sigma)).squeeze(0).permute(1, 2, 0).clamp(0, 1)