コード例 #1
0
ファイル: test_transforms.py プロジェクト: sandyhouse/Paddle
    def test_random_crop(self):
        trans = transforms.Compose([
            transforms.RandomCrop(200),
            transforms.RandomCrop((140, 160)),
        ])
        self.do_transform(trans)

        trans_random_crop1 = transforms.RandomCrop(224)
        trans_random_crop2 = transforms.RandomCrop((140, 160))

        fake_img = self.create_image((500, 400, 3))
        fake_img_crop1 = trans_random_crop1(fake_img)
        fake_img_crop2 = trans_random_crop2(fake_img_crop1)

        np.testing.assert_equal(self.get_shape(fake_img_crop1), (3, 224, 224))

        np.testing.assert_equal(self.get_shape(fake_img_crop2), (3, 140, 160))

        trans_random_crop_same = transforms.RandomCrop((140, 160))
        img = trans_random_crop_same(fake_img_crop2)

        trans_random_crop_bigger = transforms.RandomCrop((180, 200),
                                                         pad_if_needed=True)
        img = trans_random_crop_bigger(img)

        trans_random_crop_pad = transforms.RandomCrop((224, 256), 2, True)
        img = trans_random_crop_pad(img)
コード例 #2
0
    def test_random_crop(self):
        trans = transforms.Compose([
            transforms.RandomCrop(200),
            transforms.RandomCrop((140, 160)),
        ])
        self.do_transform(trans)

        trans_random_crop1 = transforms.RandomCrop(224)
        trans_random_crop2 = transforms.RandomCrop((140, 160))

        fake_img = np.random.rand(500, 400, 3).astype('float32')
        fake_img_crop1 = trans_random_crop1(fake_img)
        fake_img_crop2 = trans_random_crop2(fake_img_crop1)

        np.testing.assert_equal(fake_img_crop1.shape, (224, 224, 3))

        np.testing.assert_equal(fake_img_crop2.shape, (140, 160, 3))

        trans_random_crop_same = transforms.RandomCrop((140, 160))
        img = trans_random_crop_same(fake_img_crop2)

        trans_random_crop_bigger = transforms.RandomCrop((180, 200))
        img = trans_random_crop_bigger(img)

        trans_random_crop_pad = transforms.RandomCrop((224, 256), 2, True)
        img = trans_random_crop_pad(img)
コード例 #3
0
    def build_model(self):
        """ DataLoader """
        pad = int(30 * self.img_size // 256)
        train_transform = T.Compose([
            T.RandomHorizontalFlip(),
            T.Resize((self.img_size + pad, self.img_size + pad)),
            T.RandomCrop(self.img_size),
            T.ToTensor(),
            T.Normalize(mean=0.5, std=0.5),
        ])

        test_transform = T.Compose([
            T.Resize((self.img_size, self.img_size)),
            T.ToTensor(),
            T.Normalize(mean=0.5, std=0.5)
        ])

        self.trainA = ImageFolder('dataset/photo2cartoon/trainA', self.img_size, train_transform)
        self.trainB = ImageFolder('dataset/photo2cartoon/trainB', self.img_size, train_transform)
        self.testA = ImageFolder('dataset/photo2cartoon/testA', self.img_size, test_transform)
        self.testB = ImageFolder('dataset/photo2cartoon/testB', self.img_size, test_transform)

        self.trainA_loader = DataLoader(self.trainA, batch_size=self.batch_size, shuffle=True)
        self.trainB_loader = DataLoader(self.trainB, batch_size=self.batch_size, shuffle=True)
        self.testA_loader = DataLoader(self.testA, batch_size=1, shuffle=False)
        self.testB_loader = DataLoader(self.testB, batch_size=1, shuffle=False)

        """ Define Generator, Discriminator """
        self.genA2B = ResnetGenerator(ngf=self.ch, img_size=self.img_size, light=self.light)
        self.genB2A = ResnetGenerator(ngf=self.ch, img_size=self.img_size, light=self.light)
        self.disGA = Discriminator(input_nc=3, ndf=self.ch, n_layers=7)
        self.disGB = Discriminator(input_nc=3, ndf=self.ch, n_layers=7)
        self.disLA = Discriminator(input_nc=3, ndf=self.ch, n_layers=5)
        self.disLB = Discriminator(input_nc=3, ndf=self.ch, n_layers=5)

        """ Define Loss """
        self.L1_loss = nn.loss.L1Loss()
        self.MSE_loss = nn.loss.MSELoss()
        self.BCE_loss = nn.loss.BCEWithLogitsLoss()

        self.G_optim = paddle.optimizer.Adam(
            learning_rate=self.lr, beta1=0.5, beta2=0.999, weight_decay=0.0001,
            parameters=self.genA2B.parameters()+self.genB2A.parameters()
        )
        self.D_optim = paddle.optimizer.Adam(
            learning_rate=self.lr, beta1=0.5, beta2=0.999, weight_decay=0.0001,
            parameters=self.disGA.parameters()+self.disGB.parameters()+self.disLA.parameters()+self.disLB.parameters()
        )

        self.Rho_clipper = RhoClipper(0, self.rho_clipper)
        self.W_clipper = WClipper(0, self.w_clipper)
コード例 #4
0
    def __len__(self):
        return len(self.img_names)


if __name__ == '__main__':
    from paddle.vision.transforms import transforms as T
    from paddle.io import DataLoader

    img_size = 256
    pad = 30

    train_transform = T.Compose([
        T.RandomHorizontalFlip(),
        T.Resize((img_size + pad, img_size + pad)),
        T.RandomCrop(img_size),
        T.ToTensor(),
        T.Normalize(mean=0.5, std=0.5)
    ])

    dataloader = ImageFolder('dataset/photo2cartoon/trainB',
                             transform=train_transform)

    train_loader = DataLoader(dataloader, batch_size=1, shuffle=True)
    print('num: ', len(train_loader))
    for i in range(300):
        print(i)
        try:
            real_A, _ = next(trainA_iter)
        except:
            trainA_iter = iter(train_loader())