Beispiel #1
0
 def __init__(self,
              size=None,
              scale_factor=None,
              return_quant_tensor: bool = True):
     UpsamplingNearest2d.__init__(self,
                                  size=size,
                                  scale_factor=scale_factor)
     QuantLayerMixin.__init__(self, return_quant_tensor)
Beispiel #2
0
    def __init__(self, num_classes):
        super(UNetMini, self).__init__()

        self.block1 = Sequential(
            Conv2d(1, 32, kernel_size=3, padding=1),
            ReLU(),
            Dropout2d(0.2),
            Conv2d(32, 32, kernel_size=3, padding=1),
            ReLU(),
        )
        self.pool1 = MaxPool2d((2, 2))

        self.block2 = Sequential(
            Conv2d(32, 64, kernel_size=3, padding=1),
            ReLU(),
            Dropout2d(0.2),
            Conv2d(64, 64, kernel_size=3, padding=1),
            ReLU(),
        )
        self.pool2 = MaxPool2d((2, 2))

        self.block3 = Sequential(
            Conv2d(64, 128, kernel_size=3, padding=1),
            ReLU(),
            Dropout2d(0.2),
            Conv2d(128, 128, kernel_size=3, padding=1),
            ReLU()
        )

        self.up1 = UpsamplingNearest2d(scale_factor=2)
        self.block4 = Sequential(
            Conv2d(192, 64, kernel_size=3, padding=1),
            ReLU(),
            Dropout2d(0.2),
            Conv2d(64, 64, kernel_size=3, padding=1),
            ReLU()
        )

        self.up2 = UpsamplingNearest2d(scale_factor=2)
        self.block5 = Sequential(
            Conv2d(96, 32, kernel_size=3, padding=1),
            ReLU(),
            Dropout2d(0.2),
            Conv2d(32, 32, kernel_size=3, padding=1),
            ReLU()
        )

        self.interpolate = Interpolate(mode='bilinear')
        self.conv2d = Conv2d(32, num_classes, kernel_size=1)
        self.sigmoid = Sigmoid()
Beispiel #3
0
    def __init__(self):
        super(VAE, self).__init__()
        self.e1 = Conv2d(IMAGE_CHANNEL, NDF, 4, 2, 1)
        self.bn1 = BatchNorm2d(NDF)

        self.e2 = Conv2d(NDF, NDF * 2, 4, 2, 1)
        self.bn2 = BatchNorm2d(NDF * 2)

        self.e3 = Conv2d(NDF * 2, NDF * 4, 4, 2, 1)
        self.bn3 = BatchNorm2d(NDF * 4)

        self.e4 = Conv2d(NDF * 4, NDF * 8, 4, 2, 1)
        self.bn4 = BatchNorm2d(NDF * 8)

        self.e5 = Conv2d(NDF * 8, NDF * 8, 4, 2, 1)
        self.bn5 = BatchNorm2d(NDF * 8)

        self.fc1 = Linear(NDF * 8 * 4 * 4, LATENT_VARIABLE_SIZE)
        self.fc2 = Linear(NDF * 8 * 4 * 4, LATENT_VARIABLE_SIZE)

        # decoder
        self.d1 = Linear(LATENT_VARIABLE_SIZE, NGF * 8 * 2 * 4 * 4)

        self.up1 = UpsamplingNearest2d(scale_factor=2)
        self.pd1 = ReplicationPad2d(1)
        self.d2 = Conv2d(NGF * 8 * 2, NGF * 8, 3, 1)
        self.bn6 = BatchNorm2d(NGF * 8, 1.e-3)

        self.up2 = UpsamplingNearest2d(scale_factor=2)
        self.pd2 = ReplicationPad2d(1)
        self.d3 = Conv2d(NGF * 8, NGF * 4, 3, 1)
        self.bn7 = BatchNorm2d(NGF * 4, 1.e-3)

        self.up3 = UpsamplingNearest2d(scale_factor=2)
        self.pd3 = ReplicationPad2d(1)
        self.d4 = Conv2d(NGF * 4, NGF * 2, 3, 1)
        self.bn8 = BatchNorm2d(NGF * 2, 1.e-3)

        self.up4 = UpsamplingNearest2d(scale_factor=2)
        self.pd4 = ReplicationPad2d(1)
        self.d5 = Conv2d(NGF * 2, NGF, 3, 1)
        self.bn9 = BatchNorm2d(NGF, 1.e-3)

        self.up5 = UpsamplingNearest2d(scale_factor=2)
        self.pd5 = ReplicationPad2d(1)
        self.d6 = Conv2d(NGF, IMAGE_CHANNEL, 3, 1)

        self.leakyrelu = LeakyReLU(0.2)
        self.relu = ReLU()
        self.sigmoid = Sigmoid()
Beispiel #4
0
def get_mnist_canvas(images, labels, nb_classes=10, dim=128):

    canvas = -torch.ones((dim, dim))
    noise_canvas = torch.zeros((nb_classes, dim, dim))
    condition_canvas = torch.zeros((nb_classes, dim, dim))

    num_objs, h, w = images.shape

    y, x = (torch.randint(0, dim - h, size=(num_objs, 1)),
            torch.randint(0, dim - w, size=(num_objs, 1)))

    bboxes = torch.cat([x, y, x + w, y + h], axis=1)

    for i, (x1, y1, x2, y2) in enumerate(bboxes):

        canvas[y1:y2, x1:x2] = torch.max(canvas[y1:y2, x1:x2],
                                         images[i].squeeze())

        z = torch.randn(1, 1, w // 4, h // 4)
        z = UpsamplingNearest2d(scale_factor=4)(z)

        noise_canvas[labels[i], y1:y2, x1:x2] = z.squeeze()

        condition_canvas[labels[i], y1:y2, x1:x2] = torch.ones((h, w))

    #bboxes = torch.cat([bboxes, labels[:, None]], axis=1)

    return canvas, noise_canvas, condition_canvas, bboxes
Beispiel #5
0
    def __init__(self, in_ch, out_ch):

        super(Upward, self).__init__()

        self.depool = Sequential(
            UpsamplingNearest2d(scale_factor=2), ZeroPad2d((1, 2, 1, 2)),
            Conv2d(in_ch, out_ch, kernel_size=4, stride=1), ReLU(inplace=True),
            InstanceNorm2d(out_ch))
Beispiel #6
0
    def __init__(self, num_classes):
        super(UNetMini, self).__init__()

        # Use padding 1 to mimic `padding='same'` in keras,
        # use this visualization tool https://ezyang.github.io/convolution-visualizer/index.html
        self.block1 = Sequential(
            Conv2d(1, 32, kernel_size=3, padding=1),
            ReLU(),
            Dropout2d(0.2),
            Conv2d(32, 32, kernel_size=3, padding=1),
            ReLU(),
        )
        self.pool1 = MaxPool2d((2, 2))

        self.block2 = Sequential(
            Conv2d(32, 64, kernel_size=3, padding=1),
            ReLU(),
            Dropout2d(0.2),
            Conv2d(64, 64, kernel_size=3, padding=1),
            ReLU(),
        )
        self.pool2 = MaxPool2d((2, 2))

        self.block3 = Sequential(Conv2d(64, 128, kernel_size=3, padding=1),
                                 ReLU(), Dropout2d(0.2),
                                 Conv2d(128, 128, kernel_size=3, padding=1),
                                 ReLU())

        self.up1 = UpsamplingNearest2d(scale_factor=2)
        self.block4 = Sequential(Conv2d(192, 64, kernel_size=3, padding=1),
                                 ReLU(), Dropout2d(0.2),
                                 Conv2d(64, 64, kernel_size=3, padding=1),
                                 ReLU())

        self.up2 = UpsamplingNearest2d(scale_factor=2)
        self.block5 = Sequential(Conv2d(96, 32, kernel_size=3, padding=1),
                                 ReLU(), Dropout2d(0.2),
                                 Conv2d(32, 32, kernel_size=3, padding=1),
                                 ReLU())

        self.conv2d = Conv2d(32, num_classes, kernel_size=1)
Beispiel #7
0
    def __init__(self):
        super(BasicAutoEncoder, self).__init__()

        self.pool = MaxPool2d(kernel_size=2, stride=2)
        self.poolt = UpsamplingNearest2d(scale_factor=2)

        self.cv1 = Conv2d(1, 64, kernel_size=4, stride=1)
        self.cv2 = Conv2d(64, 32, kernel_size=3, stride=1)
        self.cv3 = Conv2d(32, 16, kernel_size=3, stride=1)

        self.cv1t = ConvTranspose2d(16, 32, kernel_size=3, stride=1)
        self.cv2t = ConvTranspose2d(32, 64, kernel_size=3, stride=1)
        self.cv3t = ConvTranspose2d(64, 1, kernel_size=4, stride=1)
Beispiel #8
0
    def backward_G(self):
        """Calculate the loss for generators G_A and G_B"""
        lambda_idt = self.opt.lambda_identity
        lambda_A = self.opt.lambda_A
        lambda_B = self.opt.lambda_B

        # Identity loss 看变换之后和原来差多少(举例:用生成浮世绘的generator计算浮世绘,看生成图像和原图差多少)
        
        if lambda_idt > 0:
            # G_A should be identity if real_B is fed: ||G_A(B) - B||
            self.idt_A = self.netG_A(self.real_B)
            self.loss_idt_A = self.criterionIdt(self.idt_A, self.real_B) * lambda_B * lambda_idt
            # G_B should be identity if real_A is fed: ||G_B(A) - A||
            self.idt_B = self.netG_B(self.real_A)
            self.loss_idt_B = self.criterionIdt(self.idt_B, self.real_A) * lambda_A * lambda_idt
        else:
            self.loss_idt_A = 0
            self.loss_idt_B = 0
        
        self.loss_idt_A = 0
        self.loss_idt_B = 0
        # GAN loss D_A(G_A(A))
        self.loss_G_A = self.criterionGAN(self.netD_A(self.fake_B), True)
        # GAN loss D_B(G_B(B))
        self.loss_G_B = self.criterionGAN(self.netD_B(self.fake_A), True)
        # Forward cycle loss || G_B(G_A(A)) - A||
        self.loss_cycle_A = self.criterionCycle(self.rec_A, self.real_A) * lambda_A
        # Backward cycle loss || G_A(G_B(B)) - B||
        self.loss_cycle_B = self.criterionCycle(self.rec_B, self.real_B) * lambda_B
        #downsample fake micro to clinical scale and calculate loss
        if self.opt.downsample_loss > 0:
            #print('use the downsampleloss')
            m = AvgPool2d(8, stride=8)
            self.downsample_fake_B = m(self.fake_B)
            self.loss_downsample = self.criterionCycle(self.downsample_fake_B, self.real_A)
        else:
            self.loss_downsample = 0
        #upsample fake clinical to micro scale and calculate loss
        if self.opt.upsample_loss > 0:
            #print('use the upsampleloss')
            n = UpsamplingNearest2d(scale_factor=8)
            self.upsample_fake_A = n(self.fake_A)
            self.loss_upsample = self.criterionCycle(self.upsample_fake_A, self.real_B)

        else:
            self.loss_upsample = 0

        # combined loss and calculate gradients
        self.loss_G = self.loss_G_A + self.loss_G_B + self.loss_cycle_A + self.loss_cycle_B + self.loss_idt_A + self.loss_idt_B + self.loss_downsample + self.loss_upsample
        self.loss_G.backward()
Beispiel #9
0
    def __init__(self, nb_classes, nb_channels, base_filters=32):

        super(Generator, self).__init__()

        self.down1 = Downward(nb_classes, base_filters, normalise=False)
        self.down2 = Downward(base_filters, 2 * base_filters)
        self.down3 = Downward(2 * base_filters, 4 * base_filters)
        self.down4 = Downward(4 * base_filters, 8 * base_filters)

        self.up1 = Upward(8 * base_filters, 4 * base_filters)
        self.up2 = Upward(8 * base_filters, 2 * base_filters)
        self.up3 = Upward(4 * base_filters, base_filters)

        self.out_conv = Sequential(
            UpsamplingNearest2d(scale_factor=2), ZeroPad2d((1, 1, 1, 1)),
            Conv2d(2 * base_filters, nb_channels, kernel_size=3, stride=1),
            Tanh())
Beispiel #10
0
def get_mnist_knapsack(images, labels, nb_classes=10, dim=128):

    bboxes = []

    canvas = -torch.ones((dim, dim))
    noise_canvas = torch.zeros((nb_classes, dim, dim))
    condition_canvas = torch.zeros((nb_classes, dim, dim))

    hs, ws = 28 + 5 * np.random.randn(2, images.shape[0])
    hs = np.clip(hs, 14, 48).astype('int')
    ws = np.clip(ws, 14, 48).astype('int')

    rectangles = list(zip(hs, hs))
    bins = [(128, 128)]

    packer = newPacker()

    # Add the rectangles to packing queue
    for r in rectangles:
        packer.add_rect(*r)

    # Add the bins where the rectangles will be placed
    for b in bins:
        packer.add_bin(*b)

    # Start packing
    packer.pack()

    for i, rect in enumerate(packer.rect_list()):
        _, x, y, w, h, _ = rect

        scaled_crop = UpsamplingBilinear2d(size=(h, w))(images[i][None, None])
        canvas[y:y + h, x:x + w] = torch.max(canvas[y:y + h, x:x + w],
                                             scaled_crop)

        z = torch.randn(1, 1, 7, 7)
        z = UpsamplingNearest2d(size=(h, w))(z)
        noise_canvas[labels[i], y:y + h, x:x + w] = z

        condition_canvas[labels[i], y:y + h, x:x + w] = torch.ones((h, w))

        bboxes.append([x, y, x + w, y + h])

    return canvas, noise_canvas, condition_canvas, torch.Tensor(bboxes)
Beispiel #11
0
 def __init__(self, scale):
     self.scale = scale
     self.up = UpsamplingNearest2d(scale_factor=scale)