def get_dataloaders(dir_path, train_classes, batch_size=128):
    image_label = load_images(dir_path, train_classes)

    # split the dataset into train, validation and test
    num_images = len(image_label)
    train_split, val_split = int(num_images * 0.8), int(num_images * 0.9)
    # TODO: shuffle the images
    train, val, test = image_label[:train_split], image_label[
        train_split:val_split], image_label[val_split:]

    # make them into pytorch datasets
    train_dataset = ImageFolder(train, transform_image('train'))
    valid_dataset = ImageFolder(val, transform_image('valid'))
    test_dataset = ImageFolder(test, transform_image('test'))

    dataloaders_dict = {
        "train":
        torch.utils.data.DataLoader(train_dataset,
                                    batch_size=batch_size,
                                    shuffle=True,
                                    num_workers=1),
        "val":
        torch.utils.data.DataLoader(valid_dataset,
                                    batch_size=batch_size,
                                    shuffle=False,
                                    num_workers=1),
        "test":
        torch.utils.data.DataLoader(test_dataset,
                                    batch_size=batch_size,
                                    shuffle=False,
                                    num_workers=1)
    }

    return dataloaders_dict
Exemple #2
0
    def build_model(self):
        """ DataLoader """

        self.trainA = ImageFolder(os.path.join('dataset', self.dataset, 'trainA'))
        self.trainB = ImageFolder(os.path.join('dataset', self.dataset, 'trainB'))
        self.testA = ImageFolder(os.path.join('dataset', self.dataset, 'testA'))
        self.testB = ImageFolder(os.path.join('dataset', self.dataset, 'testB'))
        self.trainA_loader = self.trainA
        self.trainB_loader = self.trainB
        self.testA_loader = self.testA
        self.testB_loader = self.testB        
        self.Rho_clipper = RhoClipper(0, 1)
Exemple #3
0
    def build_model(self):
        """ DataLoader """
        train_transform = transforms.Compose([
            transforms.RandomHorizontalFlip(),
            transforms.Resize((self.img_size + 30, self.img_size+30)),
            transforms.RandomCrop(self.img_size),
            transforms.ToTensor(),
            transforms.Normalize(mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5))
        ])
        test_transform = transforms.Compose([
            transforms.Resize((self.img_size, self.img_size)),
            transforms.ToTensor(),
            transforms.Normalize(mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5))
        ])

        self.trainA = ImageFolder(os.path.join('dataset', self.dataset, 'trainA'), train_transform)
        self.trainB = ImageFolder(os.path.join('dataset', self.dataset, 'trainB'), train_transform)
        self.testA = ImageFolder(os.path.join('dataset', self.dataset, 'testA'), test_transform)
        self.testB = ImageFolder(os.path.join('dataset', self.dataset, 'testB'), 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(input_nc=3, output_nc=3, ngf=self.ch, n_blocks=self.n_res, img_size=self.img_size, light=self.light).to(self.device)
        self.genB2A = ResnetGenerator(input_nc=3, output_nc=3, ngf=self.ch, n_blocks=self.n_res, img_size=self.img_size, light=self.light).to(self.device)
        self.disGA = Discriminator(input_nc=3, ndf=self.ch, n_layers=7).to(self.device)
        self.disGB = Discriminator(input_nc=3, ndf=self.ch, n_layers=7).to(self.device)
        self.disLA = Discriminator(input_nc=3, ndf=self.ch, n_layers=5).to(self.device)
        self.disLB = Discriminator(input_nc=3, ndf=self.ch, n_layers=5).to(self.device)

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

        """ Trainer """
        self.G_optim = torch.optim.Adam(itertools.chain(self.genA2B.parameters(), self.genB2A.parameters()), lr=self.lr, betas=(0.5, 0.999), weight_decay=self.weight_decay)
        self.D_optim = torch.optim.Adam(itertools.chain(self.disGA.parameters(), self.disGB.parameters(), self.disLA.parameters(), self.disLB.parameters()), lr=self.lr, betas=(0.5, 0.999), weight_decay=self.weight_decay)

        """ Define Rho clipper to constraint the value of rho in AdaILN and ILN"""
        self.Rho_clipper = RhoClipper(0, 1)

        """ Initialize FP16 support through AMP """
        
        if self.fp16:
            try:
                from apex import amp
            except ImportError:
                raise ImportError("Please install apex from https://www.github.com/nvidia/apex to use fp16.")
            [self.genA2B, self.genB2A, self.disGA, self.disGB, self.disLA, self.disLB], [self.G_optim, self.D_optim] = amp.initialize([self.genA2B, self.genB2A, self.disGA, self.disGB, self.disLA, self.disLB], [self.G_optim, self.D_optim], num_losses=2, opt_level="O1")
Exemple #4
0
    def build_model(self):
        """ DataLoader """
        train_transform = transforms.Compose([
            transforms.RandomHorizontalFlip(),
            transforms.Resize((self.img_size + 30, self.img_size+30)),
            transforms.RandomCrop(self.img_size),
            transforms.ToTensor(),
            transforms.Normalize(mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5))
        ])
        test_transform = transforms.Compose([
            transforms.Resize((self.img_size, self.img_size)),
            transforms.ToTensor(),
            transforms.Normalize(mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5))
        ])

        self.trainA = ImageFolder(os.path.join('dataset', self.dataset, 'trainA'), train_transform)
        self.trainB = ImageFolder(os.path.join('dataset', self.dataset, 'trainB'), train_transform)
        self.testA = ImageFolder(os.path.join('dataset', self.dataset, 'testA'), test_transform)
        self.testB = ImageFolder(os.path.join('dataset', self.dataset, 'testB'), test_transform)
        self.trainA_loader = DataLoader(self.trainA, batch_size=self.batch_size, shuffle=True,pin_memory=True)
        self.trainB_loader = DataLoader(self.trainB, batch_size=self.batch_size, shuffle=True,pin_memory=True)
        self.testA_loader = DataLoader(self.testA, batch_size=1, shuffle=False,pin_memory=True)
        self.testB_loader = DataLoader(self.testB, batch_size=1, shuffle=False,pin_memory=True)

        """ Define Generator, Discriminator """
        self.gen2B = ResnetGenerator(input_nc=self.img_ch, output_nc=self.img_ch, ngf=self.ch, n_blocks=self.n_res, img_size=self.img_size, light=self.light).to(self.device)
        self.gen2A = ResnetGenerator(input_nc=self.img_ch, output_nc=self.img_ch, ngf=self.ch, n_blocks=self.n_res, img_size=self.img_size, light=self.light).to(self.device)
        self.disA = Discriminator(input_nc=self.img_ch, ndf=self.ch, n_layers=self.n_dis).to(self.device)
        self.disB = Discriminator(input_nc=self.img_ch, ndf=self.ch, n_layers=self.n_dis).to(self.device)
        
        print('-----------------------------------------------')
        input = torch.randn([1, 3, 256, 256]).to(self.device)
        macs, params = profile(self.disA, inputs=(input, ))
        macs, params = clever_format([macs*2, params*2], "%.3f")
        print('[Network %s] Total number of parameters: ' % 'disA', params)
        print('[Network %s] Total number of FLOPs: ' % 'disA', macs)
        print('-----------------------------------------------')
        _,_, _,  _, real_A_ae = self.disA(input)
        macs, params = profile(self.gen2B, inputs=(real_A_ae, ))
        macs, params = clever_format([macs*2, params*2], "%.3f")
        print('[Network %s] Total number of parameters: ' % 'gen2B', params)
        print('[Network %s] Total number of FLOPs: ' % 'gen2B', macs)
        print('-----------------------------------------------')

        """ Define Loss """
        self.L1_loss = nn.L1Loss().to(self.device)
        self.MSE_loss = nn.MSELoss().to(self.device)

        """ Trainer """ 
        self.G_optim = torch.optim.Adam(itertools.chain(self.gen2B.parameters(), self.gen2A.parameters()), lr=self.lr, betas=(0.5, 0.999), weight_decay=self.weight_decay)
        self.D_optim = torch.optim.Adam(itertools.chain(self.disA.parameters(), self.disB.parameters()), lr=self.lr, betas=(0.5, 0.999), weight_decay=self.weight_decay)
Exemple #5
0
    def build_model(self):
        """ DataLoader """
        train_transform = transforms.Compose([
            transforms.RandomHorizontalFlip(),
            transforms.Resize((self.img_size + 30, self.img_size+30)),
            transforms.RandomCrop(self.img_size),
            transforms.ToTensor(),
            transforms.Normalize(mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5))
        ])
        test_transform = transforms.Compose([
            transforms.Resize((self.img_size, self.img_size)),
            transforms.ToTensor(),
            transforms.Normalize(mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5))
        ])
        self.trainA = ImageFolder(os.path.join('dataset', self.dataset, 'trainA'), train_transform)
        self.trainB = ImageFolder(os.path.join('dataset', self.dataset, 'trainB'), train_transform)
        self.testA = ImageFolder(os.path.join('dataset', self.dataset, 'testA'), test_transform)
        self.testB = ImageFolder(os.path.join('dataset', self.dataset, 'testB'), 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).to(self.device)
        self.genB2A = ResnetGenerator(ngf=self.ch, img_size=self.img_size, light=self.light).to(self.device)
        self.disGA = Discriminator(input_nc=3, ndf=self.ch, n_layers=7).to(self.device)
        self.disGB = Discriminator(input_nc=3, ndf=self.ch, n_layers=7).to(self.device)
        self.disLA = Discriminator(input_nc=3, ndf=self.ch, n_layers=5).to(self.device)
        self.disLB = Discriminator(input_nc=3, ndf=self.ch, n_layers=5).to(self.device)

        self.facenet = FaceFeatures('models/model_mobilefacenet.pth', self.device)

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

        """ Trainer """
        self.G_optim = torch.optim.Adam(itertools.chain(self.genA2B.parameters(), self.genB2A.parameters()), lr=self.lr, betas=(0.5, 0.999), weight_decay=0.0001)
        self.D_optim = torch.optim.Adam(
            itertools.chain(self.disGA.parameters(), self.disGB.parameters(), self.disLA.parameters(), self.disLB.parameters()),
            lr=self.lr, betas=(0.5, 0.999), weight_decay=0.0001
        )

        """ Define Rho clipper to constraint the value of rho in AdaLIN and LIN"""
        self.Rho_clipper = RhoClipper(0, self.rho_clipper)
        self.W_Clipper = WClipper(0, self.w_clipper)
    def build_model(self):
        """ DataLoader """
        # train_transform = None
        # train_transform = transforms.Compose([
        #     transforms.RandomHorizontalFlip(),
        #     transforms.Resize((self.img_size + 30, self.img_size+30)),
        #     RandomCrop(self.img_size),
        #     # transforms.RandomResizedCrop(self.img_size)
        #     # transforms.ToTensor(),
        #     transforms.Permute(to_rgb=True),
        #     transforms.Normalize(mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5))
        #     # https://github.com/PaddlePaddle/Paddle/issues/26155
        #     # https://github.com/PaddlePaddle/hapi
        # ])
        # test_transform = None
        # test_transform = transforms.Compose([
        #     transforms.Resize((self.img_size, self.img_size)),
        #     # transforms.ToTensor(),
        #     transforms.Permute(to_rgb=True),
        #     transforms.Normalize(mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5))
        # ])

        # self.trainA = custom_reader(os.path.join( self.dataset, 'trainA'), train_transform)
        # self.trainB = custom_reader(os.path.join(self.dataset, 'trainB'), train_transform)
        # self.testA = custom_reader(os.path.join(self.dataset, 'testA'), test_transform)
        # self.testB = custom_reader(os.path.join( self.dataset, 'testB'), test_transform)

        self.trainA = ImageFolder(os.path.join(self.dataset, 'trainA'))
        self.trainB = ImageFolder(os.path.join(self.dataset, 'trainB'))
        self.testA = ImageFolder(os.path.join(self.dataset, 'testA'))
        self.testB = ImageFolder(os.path.join(self.dataset, 'testB'))
        # self.trainA_loader = fluid.io.shuffle(paddle.batch(self.trainA, batch_size=self.batch_size),buf_size=self.batch_size)
        # self.trainA_loader = fluid.io.shuffle(paddle.batch(self.trainA, batch_size=self.batch_size),buf_size=self.batch_size)
        # self.trainA_loader =paddle.shuffle(paddle.batch(self.trainA, batch_size=self.batch_size))
        # self.trainB_loader = fluid.io.shuffle(paddle.batch(self.trainB, batch_size=self.batch_size),buf_size=self.batch_size)#######
        # self.trainB_loader = paddle.shuffle(paddle.batch(self.trainB, batch_size=self.batch_size))
        # self.trainA_loader = paddle.batch(self.trainA,batch_size=1)
        # self.trainA_loader = paddle.batch(fluid.io.shuffle(self.trainA,3),batch_size=self.batch_size)
        # self.trainB_loader = paddle.batch(self.trainB,batch_size=1)
        # self.testA_loader = paddle.batch(self.testA, batch_size=1)
        # self.testB_loader = paddle.batch(self.testB, batch_size=1)
        self.trainA_loader = self.trainA
        self.trainB_loader = self.trainB
        self.testA_loader = self.testA
        self.testB_loader = self.testB
        print('self.trainA_loader=', self.trainA_loader)
Exemple #7
0
    def test(self):
        model_list = glob(os.path.join('model', 'model.pt'))

        print(torch.__version__)

        if not len(model_list) == 0:
            self.load()
            print(" [*] Load SUCCESS")
        else:
            print(" [*] Load FAILURE")
            return

        self.genA2B.eval()  #, self.genB2A.eval()

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

        testA = ImageFolder(os.path.join('dataset', 'picture2art', 'testA'),
                            test_transform)

        testA_loader = DataLoader(testA, batch_size=1, shuffle=False)
        """
        real_A = real_A.to(self.device)

        fake_A2B, _, fake_A2B_heatmap = self.genA2B(real_A)
        
        A2B = np.concatenate((RGB2BGR(tensor2numpy(denorm(real_A[0]))),
                                  RGB2BGR(tensor2numpy(denorm(fake_A2B[0])))), 0)

        cv2.imwrite(os.path.join(self.result_dir, self.dataset, 'test', 'Base_%d.png' % (n + 1)), RGB2BGR(tensor2numpy(denorm(real_A[0]))) * 255.0)
            
        cv2.imwrite(os.path.join(self.result_dir, self.dataset, 'test', 'A2B_%d.png' % (n + 1)), RGB2BGR(tensor2numpy(denorm(fake_A2B[0]))) * 255.0)
        """
        for n, (real_A, _) in enumerate(testA_loader):
            real_A = real_A.to(self.device)
            #print(type(real_A))
            fake_A2B, _, fake_A2B_heatmap = self.genA2B(real_A)
            """
            fake_A2B2A, _, fake_A2B2A_heatmap = self.genB2A(fake_A2B)

            fake_A2A, _, fake_A2A_heatmap = self.genB2A(real_A)
            """
            A2B = np.concatenate((RGB2BGR(tensor2numpy(denorm(
                real_A[0]))), RGB2BGR(tensor2numpy(denorm(fake_A2B[0])))), 0)

            cv2.imwrite(
                os.path.join('results', self.dataset, 'base',
                             'A2B_%d.png' % (n + 1)),
                RGB2BGR(tensor2numpy(denorm(real_A[0]))) * 255.0)
            cv2.imwrite(
                os.path.join('results', self.dataset, 'test',
                             'A2B_%d.png' % (n + 1)),
                RGB2BGR(tensor2numpy(denorm(fake_A2B[0]))) * 255.0)
Exemple #8
0
    def build_model(self):
        """ DataLoader """
        train_transform = {
            'random_horizontalflip_prob': 0.5,
            'resize_size': self.img_size + 30,
            'crop_size': self.img_size
        }
        test_transform = {
            'resize_size': self.img_size,
        }

        self.trainA = ImageFolder(os.path.join('data', self.dataset, 'trainA'),
                                  train_transform)
        self.trainB = ImageFolder(os.path.join('data', self.dataset, 'trainB'),
                                  train_transform)
        self.testA = ImageFolder(os.path.join('data', self.dataset, 'testA'),
                                 test_transform)
        self.testB = ImageFolder(os.path.join('data', self.dataset, 'testB'),
                                 test_transform)

        self.trainA_loader = paddle.batch(paddle.reader.shuffle(
            myreader(self.trainA), 30000),
                                          batch_size=self.batch_size)
        self.trainB_loader = paddle.batch(paddle.reader.shuffle(
            myreader(self.trainB), 30000),
                                          batch_size=self.batch_size)
        self.testA_loader = paddle.batch(paddle.reader.shuffle(
            myreader(self.testA), 30000),
                                         batch_size=self.batch_size)
        self.testB_loader = paddle.batch(paddle.reader.shuffle(
            myreader(self.testB), 30000),
                                         batch_size=self.batch_size)

        tmp = next(self.trainA_loader())
        print('一个batch图片数据的形状:batch_size =', len(tmp), ', data_shape =',
              tmp[0].shape)

        tmp = next(self.testA_loader())
        print('一个batch图片数据的形状:batch_size =', len(tmp), ', data_shape =',
              tmp[0].shape)
        """ Define Rho clipper to constraint the value of rho in AdaILN and ILN"""
        self.Rho_clipper = RhoClipper(0, 1)
    def build_model(self):
        """ DataLoader """
        # train_transform = transforms.Compose([
            # transforms.RandomHorizontalFlip(),
            # transforms.Resize((self.img_size + 30, self.img_size+30)),
            # transforms.RandomCrop(self.img_size),
            # transforms.ToTensor(),
            # transforms.Normalize(mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5))])
        # test_transform = transforms.Compose([
            # transforms.Resize((self.img_size, self.img_size)),
            # transforms.ToTensor(),
            # transforms.Normalize(mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5))])

        self.trainA = ImageFolder(os.path.join('dataset', self.dataset, 'trainA'))
        self.trainB = ImageFolder(os.path.join('dataset', self.dataset, 'trainB'))
        self.testA = ImageFolder(os.path.join('dataset', self.dataset, 'testA'))
        self.testB = ImageFolder(os.path.join('dataset', self.dataset, 'testB'))
        self.trainA_loader = self.trainA
        self.trainB_loader = self.trainB
        self.testA_loader = self.testA
        self.testB_loader = self.testB

        """ Define Generator, Discriminator """
        self.genA2B = ResnetGenerator(input_nc=3, output_nc=3, ngf=self.ch, n_blocks=self.n_res, img_size=self.img_size, light=self.light)
        self.genB2A = ResnetGenerator(input_nc=3, output_nc=3, ngf=self.ch, n_blocks=self.n_res, 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 = fluid.dygraph.L1Loss()
        self.MSE_loss = fluid.dygraph.MSELoss()
        self.BCE_loss = fluid.dygraph.BCELoss()

        """ Trainer """
        self.G_optim = fluid.optimizer.AdamOptimizer(learning_rate=self.lr, parameter_list=self.genA2B.parameters() + self.genB2A.parameters(), beta1=0.5, beta2=0.999, regularization=fluid.regularizer.L2Decay(regularization_coeff=self.weight_decay)) # weight_decay=self.weight_decay
        self.D_optim = fluid.optimizer.AdamOptimizer(learning_rate=self.lr, parameter_list=self.disGA.parameters() + self.disGB.parameters() + self.disLA.parameters() + self.disLB.parameters(), beta1=0.5, beta2=0.999, regularization=fluid.regularizer.L2Decay(regularization_coeff=self.weight_decay))# weight_decay=self.weight_decay

        """ Define Rho clipper to constraint the value of rho in AdaILN and ILN"""
        self.Rho_clipper = RhoClipper(0, 1)
    def test_dataloader(self):
        test_dataset = ImageFolder(self.testing_path,
                                   img_transform=self.img_transform,
                                   target_transform=self.mask_transform,
                                   add_real_imgs=(self.args.developer_mode
                                                  and not self.args.train))
        loader = DataLoader(test_dataset,
                            batch_size=self.args.test_batch_size,
                            num_workers=4,
                            shuffle=False)

        return loader
Exemple #11
0
    def build_model(self):
        """ DataLoader """
        train_transform = transforms.Compose([
            transforms.RandomHorizontalFlip(),
            transforms.Resize((self.img_size + 30, self.img_size+30)),
            transforms.RandomCrop(self.img_size),
            transforms.ToTensor(),
            transforms.Normalize(mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5))
        ])
        test_transform = transforms.Compose([
            transforms.Resize((self.img_size, self.img_size)),
            transforms.ToTensor(),
            transforms.Normalize(mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5))
        ])

        self.trainA = ImageFolder(os.path.join('dataset', self.dataset, 'trainA'), train_transform)
        self.trainB = ImageFolder(os.path.join('dataset', self.dataset, 'trainB'), train_transform)
        self.testA = ImageFolder(os.path.join('dataset', self.dataset, 'testA'), test_transform)
        self.testB = ImageFolder(os.path.join('dataset', self.dataset, 'testB'), 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(input_nc=3, output_nc=3, ngf=self.ch, n_blocks=self.n_res, img_size=self.img_size, light=self.light).to(self.device)
        self.genB2A = ResnetGenerator(input_nc=3, output_nc=3, ngf=self.ch, n_blocks=self.n_res, img_size=self.img_size, light=self.light).to(self.device)
        self.disGA = Discriminator(input_nc=3, ndf=self.ch, n_layers=7).to(self.device)
        self.disGB = Discriminator(input_nc=3, ndf=self.ch, n_layers=7).to(self.device)
        self.disLA = Discriminator(input_nc=3, ndf=self.ch, n_layers=5).to(self.device)
        self.disLB = Discriminator(input_nc=3, ndf=self.ch, n_layers=5).to(self.device)

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

        """ Trainer """
        self.G_optim = torch.optim.Adam(itertools.chain(self.genA2B.parameters(), self.genB2A.parameters()), lr=self.lr, betas=(0.5, 0.999))
        self.D_optim = torch.optim.Adam(itertools.chain(self.disGA.parameters(), self.disGB.parameters(), self.disLA.parameters(), self.disLB.parameters()), lr=self.lr, betas=(0.5, 0.999))
Exemple #12
0
def train():
    if not os.path.exists(config.backupdir):
        os.mkdir(config.backupdir)
    model = torchvision.models.resnet18(pretrained=True)
    model.fc = nn.Linear(in_features=512, out_features=1, bias=True)
    use_cuda = torch.cuda.is_available() and config.use_cuda
    if use_cuda:
        os.environ['CUDA_VISIBLE_DEVICES'] = config.gpus
        if config.ngpus > 1:
            model = nn.DataParallel(model).cuda()
        else:
            model = model.cuda()
    model.train()
    dataloader = torch.utils.data.DataLoader(ImageFolder(
        config.imagespath, config.labpath, config.img_shape, config.is_shuffle,
        'train'),
                                             batch_size=config.batch_size,
                                             shuffle=False,
                                             num_workers=config.num_workers)
    FloatTensor = torch.cuda.FloatTensor if use_cuda else torch.FloatTensor
    optimizer = torch.optim.Adam(
        filter(lambda p: p.requires_grad, model.parameters()))
    criterion = nn.MSELoss()
    for epoch in range(1, config.num_epochs + 1):
        Logging('[INFO]: epoch now is %d...' % epoch, config.logfile)
        for batch_i, (_, imgs, targets) in enumerate(dataloader):
            imgs = imgs.type(FloatTensor)
            targets = targets.type(FloatTensor)
            optimizer.zero_grad()
            preds = model(imgs)
            loss = criterion(preds, targets)
            if config.ngpus > 1:
                loss = loss.sum()
            Logging(
                '[INFO]: batch%d of epoch%d, loss is %.2f...' %
                (batch_i, epoch, loss.item()), config.logfile)
            loss.backward()
            optimizer.step()
        if (epoch % config.save_interval == 0) and (epoch > 0):
            pklpath = os.path.join(config.backupdir,
                                   'epoch_%s.pkl' % str(epoch))
            if config.ngpus > 1:
                cur_model = model.module
            else:
                cur_model = model
            torch.save(cur_model.state_dict(), pklpath)
            acc = test(model)
            Logging('[INFO]: Accuracy of epoch %d is %.2f...' % (epoch, acc),
                    config.logfile)
Exemple #13
0
    def generate(self, source_dir, output_path):
        model_list = glob(
            os.path.join(self.result_dir, self.dataset, 'model', '*.pt'))
        print(os.path.join(self.result_dir, self.dataset, 'model', '*.pt'))
        print(model_list)
        if not len(model_list) == 0:
            model_list.sort()
            iter = int(model_list[-1].split('_')[-1].split('.')[0])
            self.load(os.path.join(self.result_dir, self.dataset, 'model'),
                      iter)
            print(" [*] Load SUCCESS")
        else:
            print(" [*] Load FAILURE")
            return

        test_transform = transforms.Compose([
            transforms.Resize((self.img_size, self.img_size)),
            transforms.ToTensor(),
            transforms.Normalize(mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5))
        ])
        source_data = ImageFolder(source_dir, test_transform)
        gen_loader = DataLoader(source_data, batch_size=1, shuffle=False)
        self.genA2B.eval()
        self.genB2A.eval()

        # if not os.path.exists(os.path.join(output_path, 'source_datasets')):
        #     os.makedirs(os.path.join(output_path, 'source_datasets'))
        # print("mkdir")
        # if not os.path.exists(os.path.join(output_path, 'follow_up_datasets')):
        #     os.makedirs(os.path.join(output_path, 'follow_up_datasets'))
        source_images = os.listdir(source_dir)
        for img_name in source_images:
            img = cv2.imread(os.path.join(source_dir, img_name))
            img = cv2.resize(img, (self.img_size, self.img_size))
            img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
            # img = torch.from_numpy(img)
            img = transforms.ToTensor()(img)
            img = img.unsqueeze(0)
            # print(img.size())
            # for n, (real_A, _) in enumerate(gen_loader):
            # print(real_A.size())
            real_A = img.to(self.device)

            fake_A2B, _, fake_A2B_heatmap = self.genA2B(real_A)

            cv2.imwrite(os.path.join(output_path, '%s' % (img_name)),
                        RGB2BGR(tensor2numpy(denorm(real_A[0]))) * 255.0)
            cv2.imwrite(os.path.join(output_path, '%s' % (img_name)),
                        RGB2BGR(tensor2numpy(denorm(fake_A2B[0]))) * 255.0)
    def train_dataloader(self):
        # if self.args.developer_mode:
        #     # To include the real images and masks
        #     dataset = ImageFolder(self.training_path, img_transform= self.img_transform, target_transform= self.mask_transform, add_real_imgs=True)
        # else:
        #     dataset = ImageFolder(self.training_path, img_transform= self.img_transform, target_transform= self.mask_transform)
        dataset = ImageFolder(self.training_path,
                              img_transform=self.img_transform,
                              target_transform=self.mask_transform)

        loader = DataLoader(dataset,
                            batch_size=self.args.batch_size,
                            num_workers=4,
                            shuffle=self.args.shuffle_dataset)

        return loader
    def val_dataloader(self):
        # if self.args.developer_mode:
        #     # To include the real images and masks
        #     eval_dataset = ImageFolder(self.eval_path, img_transform= self.img_transform, target_transform= self.mask_transform, add_real_imgs=True)
        # else:
        #     eval_dataset = ImageFolder(self.eval_path, img_transform= self.img_transform, target_transform= self.mask_transform)

        eval_dataset = ImageFolder(self.eval_path,
                                   img_transform=self.img_transform,
                                   target_transform=self.mask_transform)
        loader = DataLoader(eval_dataset,
                            batch_size=self.args.eval_batch_size,
                            num_workers=4,
                            shuffle=False)
        self.eval_set = eval_dataset
        return loader
Exemple #16
0
def _make_dataloader(style_or_content, opts):
    assert style_or_content in {'style', 'content'}
    txform = transforms.Compose([
        transforms.Resize(256),
        transforms.RandomCrop(256),
        transforms.RandomHorizontalFlip(),
        transforms.ToTensor(),
        transforms.Normalize(mean=[0.485, 0.456, 0.406],
                             std=[0.229, 0.224, 0.225]),
    ])
    imgs = ImageFolder(getattr(opts, f'{style_or_content}_images'), txform)
    is_content = style_or_content == 'content'
    batch_size = opts.batch_size if is_content else 1
    return torch.utils.data.DataLoader(imgs,
                                       batch_size=batch_size,
                                       shuffle=is_content,
                                       num_workers=opts.num_workers,
                                       pin_memory=True)
Exemple #17
0
def main():
    net = STFM().cuda()

    if len(args['snapshot']) > 0:
        print('load snapshot \'%s\' for testing' % args['snapshot'])
        net.load_state_dict(
            torch.load(os.path.join(ckpt_path, exp_name,
                                    args['snapshot'] + '.pth'),
                       map_location='cuda:0'))

    net.eval()
    mpath = 'F:\\DAVSOD-master\\Datasets\\STFA\\'
    with torch.no_grad():
        # for target in sorted(os.listdir(mpath)):
        target = 'DAVIS'
        save_path = 'results\\STFA\\STFA_' + target
        image_root = mpath + target + '\\'
        test_set = ImageFolder(image_root, joint_transform, img_transform,
                               target_transform)
        test_loader = DataLoader(test_set,
                                 batch_size=1,
                                 num_workers=0,
                                 shuffle=False)
        for _, (image, name, img_name, width,
                height) in enumerate(test_loader):
            image = image.cuda()
            image = torch.squeeze(image)
            begin = time.time()
            result = net(image)
            print('{:.5f}'.format(time.time() - begin))
            for t in range(result.size(0)):
                result = F.upsample(result,
                                    size=(height[0], width[0]),
                                    mode='bilinear',
                                    align_corners=False)
                res = result[t].data.cpu().numpy().squeeze()
                # res = np.round(res*255)
                # res = res.astype(np.uint8)
                res = (res - res.min()) / (res.max() - res.min() + 1e-8)
                save_name = save_path + '\\' + name[0] + '\\'
                if not os.path.exists(save_name):
                    os.makedirs(save_name)
                # print(img_name[t][0][:-4])
                misc.imsave(save_name + img_name[t][0][:-4] + '.png', res)
Exemple #18
0
def test(model):
    model.eval()
    dataloader = torch.utils.data.DataLoader(ImageFolder(
        config.imagespath, config.labpath, config.img_shape, config.is_shuffle,
        'test'),
                                             batch_size=config.batch_size,
                                             shuffle=False,
                                             num_workers=config.num_workers)
    use_cuda = torch.cuda.is_available() and config.use_cuda
    FloatTensor = torch.cuda.FloatTensor if use_cuda else torch.FloatTensor
    n_correct = 0
    n_total = 0
    for batch_i, (_, imgs, targets) in enumerate(dataloader):
        imgs = imgs.type(FloatTensor)
        targets = targets.type(FloatTensor)
        preds = model(imgs)
        n_correct += (abs(targets - preds) <
                      config.error_tolerance).sum().item()
        n_total += imgs.size(0)
    acc = n_correct / n_total
    model.train()
    return acc
Exemple #19
0
def deploy(path):
    assert os.path.exists(path), f'{path} not found : ('
    dataset = 'YOUR_DATASET_NAME'

    img_size = 256
    test_transform = transforms.Compose([
        transforms.Resize((img_size, img_size)),
        transforms.ToTensor(),
        transforms.Normalize(mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5))
    ])
    testA = ImageFolder(os.path.join('dataset', dataset, 'testA'), test_transform)
    with fluid.dygraph.guard(): 
        testA_loader = DataLoader(testA, batch_size=1, shuffle=False)
        real_A, _ = next(iter(testA_loader))
        in_np = real_A.numpy()

    # load model
    place = fluid.CPUPlace()
    exe = fluid.Executor(place)
    program, feed_vars, fetch_vars = fluid.io.load_inference_model(path, exe)

    # inference
    fetch, = exe.run(program, feed={feed_vars[0]: in_np}, fetch_list=fetch_vars)
    def img_postprocess(img):
        assert isinstance(img, np.ndarray), type(img)
        img = img * 0.5 + 0.5
        img = img.squeeze(0).transpose((1, 2, 0))
        # BGR to RGB
        img = img[:, :, ::-1]
        return img
    in_img = img_postprocess(in_np)
    out_img = img_postprocess(fetch)
    plt.subplot(121)
    plt.title('real A')
    plt.imshow(in_img)
    plt.subplot(122)
    plt.title('A to B')
    plt.imshow(out_img)
    plt.show()
Exemple #20
0
    def __init__(self, args):
        self.light = args.light

        if self.light:
            self.model_name = 'UGATIT_light'
        else:
            self.model_name = 'UGATIT'

        self.result_dir = args.result_dir
        self.dataset = args.dataset

        self.iteration = args.iteration
        self.decay_flag = args.decay_flag

        self.batch_size = args.batch_size
        self.print_freq = args.print_freq
        self.save_freq = args.save_freq

        self.lr = args.lr
        self.weight_decay = args.weight_decay
        self.ch = args.ch

        # Weight
        self.adv_weight = args.adv_weight
        self.cycle_weight = args.cycle_weight
        self.identity_weight = args.identity_weight
        self.cam_weight = args.cam_weight

        # Generator
        self.n_res = args.n_res

        # Discriminator
        self.n_dis = args.n_dis

        self.img_size = args.img_size
        self.img_ch = args.img_ch

        self.device = args.device
        self.benchmark_flag = args.benchmark_flag
        self.resume = args.resume

        print("##### Parameters #####")
        print("# light:", self.light)
        print("# dataset:", self.dataset)
        print("# batch_size:", self.batch_size)
        print("# iteration per epoch:", self.iteration)

        print()

        print("##### Generator #####")
        print("# residual blocks:", self.n_res)

        print()

        print("##### Discriminator #####")
        print("# discriminator layer:", self.n_dis)

        print()

        print("##### Weight #####")
        print("# adv_weight:", self.adv_weight)
        print("# cycle_weight:", self.cycle_weight)
        print("# identity_weight:", self.identity_weight)
        print("# cam_weight:", self.cam_weight)

        # DataLoader
        train_transform = transforms.Compose([
            transforms.RandomHorizontalFlip(),
            transforms.Resize((self.img_size + 30, self.img_size+30)),
            transforms.RandomCrop(self.img_size),
            transforms.ToTensor(),
            transforms.Normalize(mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5))
        ])
        test_transform = transforms.Compose([
            transforms.Resize((self.img_size, self.img_size)),
            transforms.ToTensor(),
            transforms.Normalize(mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5))
        ])

        self.trainA = ImageFolder(os.path.join('dataset', self.dataset, 'trainA'), train_transform)
        self.trainB = ImageFolder(os.path.join('dataset', self.dataset, 'trainB'), train_transform)
        self.testA = ImageFolder(os.path.join('dataset', self.dataset, 'testA'), test_transform)
        self.testB = ImageFolder(os.path.join('dataset', self.dataset, 'testB'), 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(input_nc=3, output_nc=3, ngf=self.ch, n_blocks=self.n_res, img_size=self.img_size, light=self.light).to(self.device)
        self.genB2A = ResnetGenerator(input_nc=3, output_nc=3, ngf=self.ch, n_blocks=self.n_res, img_size=self.img_size, light=self.light).to(self.device)
        self.disGA = Discriminator(input_nc=3, ndf=self.ch, n_layers=7).to(self.device)
        self.disGB = Discriminator(input_nc=3, ndf=self.ch, n_layers=7).to(self.device)
        self.disLA = Discriminator(input_nc=3, ndf=self.ch, n_layers=5).to(self.device)
        self.disLB = Discriminator(input_nc=3, ndf=self.ch, n_layers=5).to(self.device)

        # Define Loss
        self.L1_loss = nn.L1Loss().to(self.device)
        self.MSE_loss = nn.MSELoss().to(self.device)
        self.BCE_loss = nn.BCEWithLogitsLoss().to(self.device)

        # Trainer
        self.G_optim = torch.optim.Adam(itertools.chain(self.genA2B.parameters(), self.genB2A.parameters()), lr=self.lr, betas=(0.5, 0.999), weight_decay=self.weight_decay)
        self.D_optim = torch.optim.Adam(itertools.chain(self.disGA.parameters(), self.disGB.parameters(), self.disLA.parameters(), self.disLB.parameters()), lr=self.lr, betas=(0.5, 0.999), weight_decay=self.weight_decay)

        # Define Rho clipper to constraint the value of rho in AdaILN and ILN
        self.Rho_clipper = RhoClipper(0, 1)
Exemple #21
0
    def build_model(self):
        # DataLoader
        train_transform = transforms.Compose([
            transforms.RandomFlipLeftRight(),
            transforms.Resize((self.img_size + 30, self.img_size + 30)),
            transforms.RandomResizedCrop(self.img_size),
            transforms.ToTensor(),
            transforms.Normalize(mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5))
        ])
        test_transform = transforms.Compose([
            transforms.Resize((self.img_size, self.img_size)),
            transforms.ToTensor(),
            transforms.Normalize(mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5))
        ])

        self.trainA = ImageFolder(
            os.path.join('dataset', self.dataset, 'trainA'), train_transform)
        self.trainB = ImageFolder(
            os.path.join('dataset', self.dataset, 'trainB'), train_transform)
        self.testA = ImageFolder(
            os.path.join('dataset', self.dataset, 'testA'), test_transform)
        self.testB = ImageFolder(
            os.path.join('dataset', self.dataset, 'testB'), test_transform)
        self.trainA_loader = DataLoader(self.trainA,
                                        batch_size=self.batch_size,
                                        shuffle=True,
                                        num_workers=self.num_workers)
        self.trainB_loader = DataLoader(self.trainB,
                                        batch_size=self.batch_size,
                                        shuffle=True,
                                        num_workers=self.num_workers)
        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(input_nc=3,
                                      output_nc=3,
                                      ngf=self.ch,
                                      n_blocks=self.n_res,
                                      img_size=self.img_size,
                                      light=self.light)
        self.genB2A = ResnetGenerator(input_nc=3,
                                      output_nc=3,
                                      ngf=self.ch,
                                      n_blocks=self.n_res,
                                      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)

        self.whole_model = nn.HybridSequential()
        self.whole_model.add(*[
            self.genA2B, self.genB2A, self.disGA, self.disGB, self.disLA,
            self.disLB
        ])

        self.whole_model.hybridize(static_alloc=False, static_shape=False)
        """ Define Loss """
        self.L1_loss = gloss.L1Loss()
        self.MSE_loss = gloss.L2Loss(weight=2)
        self.BCE_loss = gloss.SigmoidBCELoss()
        """ Initialize Parameters"""
        params = self.whole_model.collect_params()
        block = self.whole_model
        if not self.debug:
            force_init(block.collect_params('.*?_weight'), KaimingUniform())
            force_init(block.collect_params('.*?_bias'),
                       BiasInitializer(params))
            block.collect_params('.*?_rho').initialize()
            block.collect_params('.*?_gamma').initialize()
            block.collect_params('.*?_beta').initialize()
            block.collect_params('.*?_state_.*?').initialize()
        else:
            pass
        block.collect_params().reset_ctx(self.dev)
        """ Trainer """
        self.G_params = param_dicts_merge(
            self.genA2B.collect_params(),
            self.genB2A.collect_params(),
        )
        self.G_optim = gluon.Trainer(
            self.G_params,
            'adam',
            dict(learning_rate=self.lr,
                 beta1=0.5,
                 beta2=0.999,
                 wd=self.weight_decay),
        )
        self.D_params = param_dicts_merge(self.disGA.collect_params(),
                                          self.disGB.collect_params(),
                                          self.disLA.collect_params(),
                                          self.disLB.collect_params())
        self.D_optim = gluon.Trainer(
            self.D_params,
            'adam',
            dict(learning_rate=self.lr,
                 beta1=0.5,
                 beta2=0.999,
                 wd=self.weight_decay),
        )
        """ Define Rho clipper to constraint the value of rho in AdaILN and ILN"""
        self.Rho_clipper = RhoClipper(0, 1)
Exemple #22
0
# Transform Data.
joint_transform = joint_transforms.Compose([
    joint_transforms.RandomRotate(),
    joint_transforms.Resize((args['scale'], args['scale']))
])
val_joint_transform = joint_transforms.Compose(
    [joint_transforms.Resize((args['scale'], args['scale']))])
img_transform = transforms.Compose([
    transforms.ToTensor(),
    transforms.Normalize([0.485, 0.456, 0.406],
                         [0.229, 0.224, 0.225])  # maybe can optimized.
])
target_transform = transforms.ToTensor()

# Prepare Data Set.
train_set = ImageFolder(msd_training_root, joint_transform, img_transform,
                        target_transform)
print("Train set: {}".format(train_set.__len__()))
train_loader = DataLoader(train_set,
                          batch_size=args['train_batch_size'],
                          num_workers=0,
                          shuffle=True)
val_set = ImageFolder(msd_testing_root, val_joint_transform, img_transform,
                      target_transform)
print("Validation Set: {}".format(val_set.__len__()))
val_loader = DataLoader(val_set,
                        batch_size=args['val_batch_size'],
                        num_workers=8,
                        shuffle=False)

bce = nn.BCEWithLogitsLoss().cuda(device_ids[0])
Exemple #23
0
    def build_model(self):
        """ DataLoader """
        train_transform = transforms.Compose([
            transforms.RandomHorizontalFlip(),
            transforms.Resize((self.img_size + 30, self.img_size + 30)),
            transforms.RandomCrop(self.img_size),
            transforms.ToTensor(),
            transforms.Normalize(mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5))
        ])
        test_transform = transforms.Compose([
            transforms.Resize((self.img_size, self.img_size)),
            transforms.ToTensor(),
            transforms.Normalize(mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5))
        ])

        self.trainA = ImageFolder(
            os.path.join('dataset', self.dataset, 'trainA'), train_transform)
        self.trainB = ImageFolder(
            os.path.join('dataset', self.dataset, 'trainB'), train_transform)
        self.testA = ImageFolder(
            os.path.join('dataset', self.dataset, 'testA'), test_transform)
        self.testB = ImageFolder(
            os.path.join('dataset', self.dataset, 'testB'), 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 """
        modules = {}
        self.genA2B = modules['genA2B'] = ResnetGenerator(
            input_nc=3,
            output_nc=3,
            ngf=self.ch,
            n_blocks=self.n_res,
            img_size=self.img_size,
            light=self.light,
            model_parallel=True)
        self.genB2A = modules['genB2A'] = ResnetGenerator(
            input_nc=3,
            output_nc=3,
            ngf=self.ch,
            n_blocks=self.n_res,
            img_size=self.img_size,
            light=self.light,
            model_parallel=True)
        self.disGA = modules['disGA'] = Discriminator(input_nc=3,
                                                      ndf=self.ch,
                                                      n_layers=7,
                                                      model_parallel=True)
        self.disGB = modules['disGB'] = Discriminator(input_nc=3,
                                                      ndf=self.ch,
                                                      n_layers=7,
                                                      model_parallel=True)
        self.disLA = modules['disLA'] = Discriminator(input_nc=3,
                                                      ndf=self.ch,
                                                      n_layers=5,
                                                      model_parallel=True)
        self.disLB = modules['disLB'] = Discriminator(input_nc=3,
                                                      ndf=self.ch,
                                                      n_layers=5,
                                                      model_parallel=True)
        """ Define Loss """
        self.L1_loss = modules['L1_loss'] = nn.L1Loss()
        self.MSE_loss = modules['MSE_loss'] = nn.MSELoss()
        self.BCE_loss = modules['BCE_loss'] = nn.BCEWithLogitsLoss()
        for k, module_name in enumerate(modules.keys()):
            if 'loss' in module_name:
                d = torch.device('cuda:0')  # pylint: disable=no-member
            else:
                d = torch.device('cuda:' + str(k % self.n_gpus))  # pylint: disable=no-member
            m = modules[module_name]
            m = m.to(d)
            m.cuda_device = d
            setattr(self, module_name, m)
        """ Trainer """
        self.G_optim = torch.optim.Adam(itertools.chain(
            self.genA2B.parameters(), self.genB2A.parameters()),
                                        lr=self.lr,
                                        betas=(0.5, 0.999),
                                        weight_decay=self.weight_decay)
        self.D_optim = torch.optim.Adam(itertools.chain(
            self.disGA.parameters(), self.disGB.parameters(),
            self.disLA.parameters(), self.disLB.parameters()),
                                        lr=self.lr,
                                        betas=(0.5, 0.999),
                                        weight_decay=self.weight_decay)
        """ Define Rho clipper to constraint the value of rho in AdaILN and ILN"""
        self.Rho_clipper = RhoClipper(0, 1)
Exemple #24
0
import torch.optim as optim
from dataset import ImageFolder
from models import CNNPolicy

import random

model = CNNPolicy(3)
model.load_state_dict(torch.load('checkpoint.pth.tar')['state_dict'])
model.eval()
# model.load_state_dict(torch.load('model_best.pth.tar')['state_dict'])

random.seed(123)
idxs = list(range(10100))
random.shuffle(idxs)

data_set = ImageFolder('../gym-duckietown/images', return_path=True)
test_loader = torch.utils.data.DataLoader(data_set,
                                          batch_size=1,
                                          shuffle=False,
                                          num_workers=1,
                                          sampler=idxs[10000:10100])

output_html = ''

losses = []

for i, (input, target, path) in enumerate(test_loader):

    input_var = torch.autograd.Variable(input)
    target_var = torch.autograd.Variable(target)
Exemple #25
0
    def build_model(self):
        """ DataLoader """
        train_transform = transforms.Compose([
            transforms.Resize((self.img_size, self.img_size)),
            transforms.ToTensor(),
            transforms.Normalize(mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5))
        ])
        test_transform = transforms.Compose([
            transforms.Resize((self.img_size, self.img_size)),
            transforms.ToTensor(),
            transforms.Normalize(mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5))
        ])

        self.trainA_0 = ImageFolder(
            os.path.join('dataset', self.dataset, 'trainA', '0'), 0,
            train_transform)
        self.trainA_1 = ImageFolder(
            os.path.join('dataset', self.dataset, 'trainA', '1'), 1,
            train_transform)
        self.trainA_2 = ImageFolder(
            os.path.join('dataset', self.dataset, 'trainA', '2'), 2,
            train_transform)
        self.trainA_3 = ImageFolder(
            os.path.join('dataset', self.dataset, 'trainA', '3'), 3,
            train_transform)
        self.datasetA_list = [
            self.trainA_0, self.trainA_1, self.trainA_2, self.trainA_3
        ]

        self.trainB_0 = ImageFolder(
            os.path.join('dataset', self.dataset, 'trainB', '0'), 0,
            train_transform)
        self.trainB_1 = ImageFolder(
            os.path.join('dataset', self.dataset, 'trainB', '1'), 1,
            train_transform)
        self.trainB_2 = ImageFolder(
            os.path.join('dataset', self.dataset, 'trainB', '2'), 2,
            train_transform)
        self.trainB_3 = ImageFolder(
            os.path.join('dataset', self.dataset, 'trainB', '3'), 3,
            train_transform)
        self.datasetB_list = [
            self.trainB_0, self.trainB_1, self.trainB_2, self.trainB_3
        ]

        self.testA = ImageFolder(
            os.path.join('dataset', self.dataset, 'testA'), -1, test_transform)
        self.testB = ImageFolder(
            os.path.join('dataset', self.dataset, 'testB'), -1, test_transform)

        self.trainA_0_loader = DataLoader(self.trainA_0,
                                          batch_size=self.batch_size,
                                          shuffle=True)
        self.trainA_1_loader = DataLoader(self.trainA_1,
                                          batch_size=self.batch_size,
                                          shuffle=True)
        self.trainA_2_loader = DataLoader(self.trainA_2,
                                          batch_size=self.batch_size,
                                          shuffle=True)
        self.trainA_3_loader = DataLoader(self.trainA_3,
                                          batch_size=self.batch_size,
                                          shuffle=True)
        self.loaderA_list = [
            self.trainA_0_loader, self.trainA_1_loader, self.trainA_2_loader,
            self.trainA_3_loader
        ]

        self.trainB_0_loader = DataLoader(self.trainB_0,
                                          batch_size=self.batch_size,
                                          shuffle=True)
        self.trainB_1_loader = DataLoader(self.trainB_1,
                                          batch_size=self.batch_size,
                                          shuffle=True)
        self.trainB_2_loader = DataLoader(self.trainB_2,
                                          batch_size=self.batch_size,
                                          shuffle=True)
        self.trainB_3_loader = DataLoader(self.trainB_3,
                                          batch_size=self.batch_size,
                                          shuffle=True)
        self.loaderB_list = [
            self.trainB_0_loader, self.trainB_1_loader, self.trainB_2_loader,
            self.trainB_3_loader
        ]

        self.testA_loader = DataLoader(self.testA, batch_size=1, shuffle=True)
        self.testB_loader = DataLoader(self.testB, batch_size=1, shuffle=True)
        """ Adjust dataset list and dataloader list """
        for i in range(4):
            if not (i in self.group_list):
                self.datasetA_list[i] = None
                self.datasetB_list[i] = None
                self.loaderA_list[i] = None
                self.loaderB_list[i] = None
        self.datasetA_list = removeNone(self.datasetA_list)
        self.datasetB_list = removeNone(self.datasetB_list)
        self.loaderA_list = removeNone(self.loaderA_list)
        self.loaderB_list = removeNone(self.loaderB_list)

        # Sanity check
        assert len(self.loaderA_list) == len(self.loaderB_list) == len(self.datasetA_list) == \
               len(self.datasetB_list) == len(self.group_list), 'Group num differs'
        self.iterA_list = [iter(x) for x in self.loaderA_list]
        self.iterB_list = [iter(x) for x in self.loaderB_list]
        """ Define Generator, Discriminator """
        self.genA2B = ResnetGenerator(input_nc=3,
                                      output_nc=3,
                                      ngf=self.ch,
                                      n_blocks=self.n_res,
                                      img_size=self.img_size,
                                      light=self.light).to(self.device)
        self.genB2A = ResnetGenerator(input_nc=3,
                                      output_nc=3,
                                      ngf=self.ch,
                                      n_blocks=self.n_res,
                                      img_size=self.img_size,
                                      light=self.light).to(self.device)
        #Global attention
        self.disGA = Discriminator(input_nc=3, ndf=self.ch,
                                   n_layers=7).to(self.device)
        self.disGB = Discriminator(input_nc=3, ndf=self.ch,
                                   n_layers=7).to(self.device)
        #Local attention
        self.disLA = Discriminator(input_nc=3, ndf=self.ch,
                                   n_layers=5).to(self.device)
        self.disLB = Discriminator(input_nc=3, ndf=self.ch,
                                   n_layers=5).to(self.device)
        """ Define Loss """
        self.L1_loss = nn.L1Loss().to(self.device)
        self.MSE_loss = nn.MSELoss().to(self.device)
        self.BCE_loss = nn.BCEWithLogitsLoss().to(self.device)
        self.CE_loss = nn.CrossEntropyLoss().to(self.device)
        """ Trainer """
        # Module 안에는 _parameters, _modules라는 OrderedDict가 있다.
        # _parameters 안에는 Parameters(tensor의 자식, weight, bias etc...) 객체가 들어있다.
        # _modules 안에는 하위 module들이 들어있다.
        # Module.parameters()를 호출하면, _modules를 돌면서 자신을 포함한 모든 모듈들의 _parameters 안의 Parameters 객체를 list로 반환한다.
        # 즉, optimizer는 list of Parameters(or dict)를 받는다.

        # Freeze the generator
        self.ToggleBottleNeck(freeze=True)
        # Trainable params
        genA2B_params = []
        genB2A_params = []
        for param in self.genA2B.parameters():
            if param.requires_grad == True:
                genA2B_params.append(param)
        for param in self.genB2A.parameters():
            if param.requires_grad == True:
                genB2A_params.append(param)

        #self.G_optim = torch.optim.Adam(itertools.chain(self.genA2B.parameters(), self.genB2A.parameters()), lr=self.lr, betas=(0.5, 0.999), weight_decay=self.weight_decay)
        self.G_optim = torch.optim.Adam(itertools.chain(
            genA2B_params, genB2A_params),
                                        lr=self.lr,
                                        betas=(0.5, 0.999),
                                        weight_decay=self.weight_decay)

        self.D_optim = torch.optim.Adam(itertools.chain(
            self.disGA.parameters(), self.disGB.parameters(),
            self.disLA.parameters(), self.disLB.parameters()),
                                        lr=self.lr,
                                        betas=(0.5, 0.999),
                                        weight_decay=self.weight_decay)
        """ Define Rho clipper to constraint the value of rho in AdaILN and ILN"""
        self.Rho_clipper = RhoClipper(0, 1)
    def build_model(self):
        """ DataLoader """

        # we have fifferent transformations as we have different number of channels which require
        # different arguments for Normalize function

        # commented the normalizations as the normalizations have been replaced by
        # rescaling in the image loaders

        train_transform_a = transforms.Compose([
            transforms.ToTensor(),
            #             transforms.Normalize(mean=(0.0, )*self.img_ch_a, std=(0.5, )*self.img_ch_a)   # see above
        ])  # removed horizontal flip, resize and random_crop because they use pil which doesn't suppert multichannel images above 3

        train_transform_b = transforms.Compose([
            transforms.ToTensor(),
            #             transforms.Normalize(mean=(0.0,)*self.img_ch_b, std=(0.5,)*self.img_ch_b)
        ])

        test_transform_a = transforms.Compose([
            transforms.ToTensor(),
            #             transforms.Normalize(mean=(0.5, )*self.img_ch_a, std=(0.5, )*self.img_ch_a)
        ])  # removed Resize as it uses PIL

        test_transform_b = transforms.Compose([
            transforms.ToTensor(),
            #             transforms.Normalize(mean=(0.5,)*self.img_ch_b, std=(0.5,)*self.img_ch_b)
        ])

        # thereform note: the images must be preprocessed properly!! with all the augmentation and cropping!

        self.trainA = ImageFolder(
            os.path.join('dataset', self.dataset, 'trainA'), train_transform_a)
        self.trainB = ImageFolder(
            os.path.join('dataset', self.dataset, 'trainB'), train_transform_b)
        self.testA = ImageFolder(
            os.path.join('dataset', self.dataset, 'testA'), test_transform_a)
        self.testB = ImageFolder(
            os.path.join('dataset', self.dataset, 'testB'), test_transform_b)
        self.trainA_loader = DataLoader(self.trainA,
                                        batch_size=self.batch_size,
                                        shuffle=True,
                                        pin_memory=True)
        self.trainB_loader = DataLoader(self.trainB,
                                        batch_size=self.batch_size,
                                        shuffle=True,
                                        pin_memory=True)
        self.testA_loader = DataLoader(self.testA,
                                       batch_size=1,
                                       shuffle=False,
                                       pin_memory=True)
        self.testB_loader = DataLoader(self.testB,
                                       batch_size=1,
                                       shuffle=False,
                                       pin_memory=True)
        """ Define Generator, Discriminator """
        self.gen2B = ResnetGenerator(input_nc=self.img_ch_a,
                                     output_nc=self.img_ch_b,
                                     ngf=self.ch,
                                     n_blocks=self.n_res,
                                     img_size=self.img_size,
                                     light=self.light).to(self.device)
        self.gen2A = ResnetGenerator(input_nc=self.img_ch_b,
                                     output_nc=self.img_ch_a,
                                     ngf=self.ch,
                                     n_blocks=self.n_res,
                                     img_size=self.img_size,
                                     light=self.light).to(self.device)
        self.disA = Discriminator(input_nc=self.img_ch_a,
                                  ndf=self.ch,
                                  n_layers=self.n_dis).to(self.device)
        self.disB = Discriminator(input_nc=self.img_ch_b,
                                  ndf=self.ch,
                                  n_layers=self.n_dis).to(self.device)

        print('-----------------------------------------------')
        input = torch.randn([1, self.img_ch_a, self.img_size,
                             self.img_size]).to(self.device)
        macs, params = profile(self.disA, inputs=(input, ))
        macs, params = clever_format([macs * 2, params * 2], "%.3f")
        print('[Network %s] Total number of parameters: ' % 'disA', params)
        print('[Network %s] Total number of FLOPs: ' % 'disA', macs)
        print('-----------------------------------------------')
        _, _, _, _, real_A_ae = self.disA(input)
        macs, params = profile(self.gen2B, inputs=(real_A_ae, ))
        macs, params = clever_format([macs * 2, params * 2], "%.3f")
        print('[Network %s] Total number of parameters: ' % 'gen2B', params)
        print('[Network %s] Total number of FLOPs: ' % 'gen2B', macs)
        print('-----------------------------------------------')
        """ Define Loss """
        self.L1_loss = nn.L1Loss().to(self.device)
        self.MSE_loss = nn.MSELoss().to(self.device)
        """ Trainer """
        self.G_optim = torch.optim.Adam(itertools.chain(
            self.gen2B.parameters(), self.gen2A.parameters()),
                                        lr=self.lr,
                                        betas=(0.5, 0.999),
                                        weight_decay=self.weight_decay)
        self.D_optim = torch.optim.Adam(itertools.chain(
            self.disA.parameters(), self.disB.parameters()),
                                        lr=self.lr,
                                        betas=(0.5, 0.999),
                                        weight_decay=self.weight_decay)
Exemple #27
0
    def build_model(self):
        """ DataLoader """
        train_transform = Compose([
            RandomHorizontalFlip(),
            Resize((self.img_size + 30, self.img_size + 30)),
            RandomResizedCrop(self.img_size),
            Permute(mode="CHW", to_rgb=False),
            Normalize(mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5))
        ])
        test_transform = Compose([
            Resize((self.img_size, self.img_size)),
            Permute(mode="CHW", to_rgb=False),
            Normalize(mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5))
        ])

        self.trainA = ImageFolder(
            os.path.join('dataset', self.dataset, 'trainA'), train_transform)
        self.trainB = ImageFolder(
            os.path.join('dataset', self.dataset, 'trainB'), train_transform)
        self.testA = ImageFolder(
            os.path.join('dataset', self.dataset, 'testA'), test_transform)
        self.testB = ImageFolder(
            os.path.join('dataset', self.dataset, 'testB'), 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)

        # iterator over the original dataset
        def iter_reader(dataset):
            def r():
                for sample, target in dataset:
                    yield sample

            return r

        self.trainA_loader = fluid.io.batch(fluid.io.shuffle(iter_reader(
            self.trainA),
                                                             buf_size=100),
                                            batch_size=self.batch_size,
                                            drop_last=True)
        self.trainB_loader = fluid.io.batch(fluid.io.shuffle(iter_reader(
            self.trainB),
                                                             buf_size=100),
                                            batch_size=self.batch_size,
                                            drop_last=True)
        self.testA_loader = fluid.io.batch(iter_reader(self.testA),
                                           batch_size=1)
        self.testB_loader = fluid.io.batch(iter_reader(self.testB),
                                           batch_size=1)
        """ Define Generator, Discriminator """
        # self.genA2B = ResnetGenerator(input_nc=3, output_nc=3, ngf=self.ch, n_blocks=self.n_res, img_size=self.img_size, light=self.light).to(self.device)
        # self.genB2A = ResnetGenerator(input_nc=3, output_nc=3, ngf=self.ch, n_blocks=self.n_res, img_size=self.img_size, light=self.light).to(self.device)
        # self.disGA = Discriminator(input_nc=3, ndf=self.ch, n_layers=7).to(self.device)
        # self.disGB = Discriminator(input_nc=3, ndf=self.ch, n_layers=7).to(self.device)
        # self.disLA = Discriminator(input_nc=3, ndf=self.ch, n_layers=5).to(self.device)
        # self.disLB = Discriminator(input_nc=3, ndf=self.ch, n_layers=5).to(self.device)
        self.genA2B = ResnetGenerator(input_nc=3,
                                      output_nc=3,
                                      ngf=self.ch,
                                      n_blocks=self.n_res,
                                      img_size=self.img_size,
                                      light=self.light)
        self.genB2A = ResnetGenerator(input_nc=3,
                                      output_nc=3,
                                      ngf=self.ch,
                                      n_blocks=self.n_res,
                                      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.L1Loss().to(self.device)
        # self.MSE_loss = nn.MSELoss().to(self.device)
        # self.BCE_loss = nn.BCEWithLogitsLoss().to(self.device)

        self.L1_loss = dygraph.L1Loss()
        self.MSE_loss = dygraph.MSELoss()
        self.BCE_loss = BCEWithLogitsLoss()
        """ Trainer """
        # self.G_optim = torch.optim.Adam(itertools.chain(self.genA2B.parameters(), self.genB2A.parameters()), lr=self.lr, betas=(0.5, 0.999), weight_decay=self.weight_decay)
        # self.D_optim = torch.optim.Adam(itertools.chain(self.disGA.parameters(), self.disGB.parameters(), self.disLA.parameters(), self.disLB.parameters()), lr=self.lr, betas=(0.5, 0.999), weight_decay=self.weight_decay)
        # self.G_optim = fluid.optimizer.AdamOptimizer(parameter_list=self.genA2B.parameters()+self.genB2A.parameters(), learning_rate=self.lr, beta1=0.5, beta2=0.999, regularization=fluid.regularizer.L2Decay(self.weight_decay))
        # self.D_optim = fluid.optimizer.AdamOptimizer(parameter_list=self.disGA.parameters()+self.disGB.parameters()+self.disLA.parameters()+self.disLB.parameters(), learning_rate=self.lr, beta1=0.5, beta2=0.999, regularization=fluid.regularizer.L2Decay(self.weight_decay))
        self.genA2B_opt = fluid.optimizer.AdamOptimizer(
            parameter_list=self.genA2B.parameters(),
            learning_rate=self.lr,
            beta1=0.5,
            beta2=0.999,
            regularization=fluid.regularizer.L2Decay(self.weight_decay))
        self.genB2A_opt = fluid.optimizer.AdamOptimizer(
            parameter_list=self.genB2A.parameters(),
            learning_rate=self.lr,
            beta1=0.5,
            beta2=0.999,
            regularization=fluid.regularizer.L2Decay(self.weight_decay))
        self.disGA_opt = fluid.optimizer.AdamOptimizer(
            parameter_list=self.disGA.parameters(),
            learning_rate=self.lr,
            beta1=0.5,
            beta2=0.999,
            regularization=fluid.regularizer.L2Decay(self.weight_decay))
        self.disGB_opt = fluid.optimizer.AdamOptimizer(
            parameter_list=self.disGB.parameters(),
            learning_rate=self.lr,
            beta1=0.5,
            beta2=0.999,
            regularization=fluid.regularizer.L2Decay(self.weight_decay))
        self.disLA_opt = fluid.optimizer.AdamOptimizer(
            parameter_list=self.disLA.parameters(),
            learning_rate=self.lr,
            beta1=0.5,
            beta2=0.999,
            regularization=fluid.regularizer.L2Decay(self.weight_decay))
        self.disLB_opt = fluid.optimizer.AdamOptimizer(
            parameter_list=self.disLB.parameters(),
            learning_rate=self.lr,
            beta1=0.5,
            beta2=0.999,
            regularization=fluid.regularizer.L2Decay(self.weight_decay))
        """ Define Rho clipper to constraint the value of rho in AdaILN and ILN"""
        self.Rho_clipper = RhoClipper(0, 1)
Exemple #28
0
}

joint_transform = joint_transforms.Compose([
    joint_transforms.RandomHorizontallyFlip(),
    joint_transforms.Resize((args['scale'], args['scale']))
])
val_joint_transform = joint_transforms.Compose(
    [joint_transforms.Resize((args['scale'], args['scale']))])
img_transform = transforms.Compose([
    transforms.ToTensor(),
    transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
])
target_transform = transforms.ToTensor()
to_pil = transforms.ToPILImage()

train_set = ImageFolder(sbu_training_root, joint_transform, img_transform,
                        target_transform)
train_loader = DataLoader(train_set,
                          batch_size=args['train_batch_size'],
                          num_workers=8,
                          shuffle=True)

bce_logit = nn.BCEWithLogitsLoss().cuda()
log_path = os.path.join(ckpt_path, exp_name,
                        str(datetime.datetime.now()) + '.txt')


def main():
    net = BDRAR().cuda().train()

    optimizer = optim.SGD([{
        'params': [
Exemple #29
0
def main():
    ##############################
    # Using one GPU                 args.device_ids = [0]
    ##############################
    if len(args.device_ids) == 1:
        net = MirrorNet().cuda(device_ids[0])
        optimizer = optim.Adam(net.parameters(), lr=args.lr, betas=args.betas)

        if args.load_model:
            # print(os.path.join(args.root_path + args.ckpt_path, args.exp_name, args.snapshot + '.pth'))
            print('Load snapshot {} for testing'.format(args.snapshot))
            net.load_state_dict(
                torch.load(
                    os.path.join(args.ckpt_path, args.exp_name,
                                 args.snapshot + '.pth')))
            # net.load_state_dict(torch.load(os.path.join(args.ckpt_path, args.exp_name, args.snapshot + '.pth')))
            print('Load {} succeed!'.format(
                os.path.join(args.ckpt_path, args.exp_name,
                             args.snapshot + '.pth')))

        if not args.train:
            net.eval()
            data_path = args.msd_testing_root
        else:
            data_path = args.msd_training_root
            eval_path = args.msd_eval_root
            net.train()

        if args.developer_mode:
            # To include the real images and masks
            dataset = ImageFolder(data_path,
                                  img_transform=img_transform,
                                  target_transform=mask_transform,
                                  add_real_imgs=True)
            eval_dataset = ImageFolder(eval_path,
                                       img_transform=img_transform,
                                       target_transform=mask_transform,
                                       add_real_imgs=True)
        else:
            dataset = ImageFolder(data_path,
                                  img_transform=img_transform,
                                  target_transform=mask_transform)
            eval_dataset = ImageFolder(eval_path,
                                       img_transform=img_transform,
                                       target_transform=mask_transform)

        loader = DataLoader(dataset,
                            batch_size=args.batch_size,
                            shuffle=args.shuffle_dataset)
        eval_loader = DataLoader(eval_dataset, batch_size=1, shuffle=False)

        # batch = dataset.sample(3)
        # batch["img"][0].show()
        # batch["mask"][0].show()
        # print(batch)

        if args.train:
            print("Training")

            idx = 0
            ##############################
            # Training for number of epoches
            for epoch in range(args.epochs):
                start_time = time.time()
                loss = 0

                ###############################
                ## Training
                ###############################
                # Defining the tqdm progress bar for training dataset
                pbar = tqdm(loader, desc="Processing batch number")
                idx = 0
                net.train()
                for batch in pbar:
                    batch = dataset.sample(args.batch_size)
                    inputs = batch["img"]
                    outputs = batch["mask"]
                    inputs = torch.from_numpy(inputs)
                    outputs = torch.tensor(outputs)

                    # To GPU if available
                    if args.cuda:
                        inputs = inputs.cuda(device_ids[0])
                        outputs = outputs.cuda(device_ids[0])

                    # Getting the 4 different outputs
                    inputs.requires_grad = True
                    outputs.requires_grad = True
                    f_4_gpu, f_3_gpu, f_2_gpu, f_1_gpu = net(inputs)

                    if args.developer_mode:
                        if idx == 0:
                            f_1 = f_1_gpu.data.cpu()
                            rev_size = [
                                batch["size"][0][1], batch["size"][0][0]
                            ]
                            image1_size = batch["size"][0]
                            f_1_trans = np.array(
                                transforms.Resize(rev_size)(to_pil(f_1[0])))
                            f_1_crf = crf_refine(np.array(batch["r_img"][0]),
                                                 f_1_trans)

                            new_image = Image.new(
                                'RGB', (3 * image1_size[0], image1_size[1]),
                                (250, 250, 250))
                            img_res = Image.fromarray(f_1_crf)
                            new_image.paste(batch["r_img"][0], (0, 0))
                            new_image.paste(batch["r_mask"][0],
                                            (image1_size[0], 0))
                            new_image.paste(img_res, (image1_size[0] * 2, 0))

                            new_image.save(
                                os.path.join(
                                    args.msd_results_root, "Testing",
                                    "Epoch: " + str(epoch) + " Trining.png"))
                            print("Image saved")
                        idx += 1

                    ##############################
                    # # For image processing
                    # f_4 = f_4_gpu.data.cpu()
                    # f_3 = f_3_gpu.data.cpu()
                    # f_2 = f_2_gpu.data.cpu()
                    # f_1 = f_1_gpu.data.cpu()
                    # f_4_arr = []
                    # f_3_arr = []
                    # f_2_arr = []
                    # f_1_arr = []
                    # f_1_arr_no_resize = []
                    # for i in range(args.batch_size):
                    #     rev_size = [batch["size"][i][1], batch["size"][i][0]]
                    #     f_4_arr.append(np.array(transforms.Resize(rev_size)(to_pil(f_4[i]))))
                    #     f_3_arr.append(np.array(transforms.Resize(rev_size)(to_pil(f_3[i]))))
                    #     f_2_arr.append(np.array(transforms.Resize(rev_size)(to_pil(f_2[i]))))
                    #     f_1_arr.append(np.array(transforms.Resize(rev_size)(to_pil(f_1[i]))))
                    #     f_1_arr_no_resize.append(np.array(to_pil(f_1[i])))
                    #     if args.crf and args.developer_mode:
                    #         f_1_arr[i] = crf_refine(np.array(batch["r_img"][i]), f_1_arr[i])
                    #         # img_ = np.array(batch["img"][i])
                    #         # img_ =img_.astype('uint8')
                    #         f_1_arr_no_resize[i] = crf_refine(np.array(batch["r_img"][i].resize((args.scale, args.scale))), f_1_arr_no_resize[i])

                    #     image1_size = batch["size"][i]
                    #     if args.save_images:
                    #         new_image = Image.new('RGB',(3*image1_size[0], image1_size[1]), (250,250,250))
                    #         img_res = Image.fromarray(f_1_arr[i])
                    #         new_image.paste(batch["r_img"][i],(0,0))
                    #         new_image.paste(batch["r_mask"][i],(image1_size[0],0))
                    #         new_image.paste(img_res,(image1_size[0]*2,0))

                    #         new_image.save(os.path.join(args.msd_results_root, "Testing",
                    #                                                 "MNet_" + str(idx) +".png"))
                    #         idx +=1

                    #         img_res1 = Image.fromarray(f_2_arr[i])
                    #         img_res2 = Image.fromarray(f_3_arr[i])
                    #         img_res3 = Image.fromarray(f_4_arr[i])
                    #         new_image.paste(img_res1,(0,0))
                    #         new_image.paste(img_res2,(image1_size[0],0))
                    #         new_image.paste(img_res3,(image1_size[0]*2,0))
                    #         new_image.save(os.path.join(args.msd_results_root, "Testing",
                    #                                                 "MNet_" + str(idx) +".png"))
                    #     idx +=1

                    # TODO
                    ## Achieve loss for images with crf refining
                    ## Starting Point
                    # f_1_arr = np.array(f_1_arr)
                    # f_1_arr_no_resize = np.array(f_1_arr_no_resize)
                    # f_1_arr_no_resize = torch.tensor(f_1_arr_no_resize)
                    # loss = lovasz_hinge(torch.tensor(batch["mask"]), f_1_arr_no_resize, per_image=False)

                    loss1 = lovasz_hinge(f_1_gpu, outputs,
                                         per_image=False) * args.w_losses[0]
                    loss2 = lovasz_hinge(f_2_gpu, outputs,
                                         per_image=False) * args.w_losses[1]
                    loss3 = lovasz_hinge(f_3_gpu, outputs,
                                         per_image=False) * args.w_losses[2]
                    loss4 = lovasz_hinge(f_4_gpu, outputs,
                                         per_image=False) * args.w_losses[3]
                    loss = loss1 + loss2 + loss3 + loss4
                    # outputs.requires_grad = False
                    # L2 = torch.nn.BCELoss()
                    # loss2 = L2(f_1_gpu, outputs)
                    # loss = loss2 +loss1
                    optimizer.zero_grad()
                    loss.backward()
                    optimizer.step()
                    res = loss.data.cpu()

                    text = "epoch: {}, Loss: {}".format(epoch, res)

                    text = text.ljust(40)
                    # text = text + " L_BCE: {}".format(loss2)
                    # text = text.ljust(60)
                    pbar.set_description(text)
                print("Needed time:")
                print(time.time() - start_time)

                ###############################
                ## Evaluation
                ###############################
                # Defining the tqdm progress bar for evaluation dataset
                eval_pbar = tqdm(eval_loader, desc="Processing batch number")
                idx = 0
                net.eval()
                for batch in eval_pbar:
                    batch = dataset.sample(args.batch_size)
                    inputs = batch["img"]
                    outputs = batch["mask"]
                    inputs = torch.from_numpy(inputs)
                    outputs = torch.tensor(outputs)

                    # To GPU if available
                    if args.cuda:
                        inputs = inputs.cuda(device_ids[0])
                        outputs = outputs.cuda(device_ids[0])

                    # Getting the 4 different outputs
                    inputs.requires_grad = True
                    outputs.requires_grad = True
                    f_4_gpu, f_3_gpu, f_2_gpu, f_1_gpu = net(inputs)

                    if args.developer_mode:
                        if idx == 0:
                            f_1 = f_1_gpu.data.cpu()
                            rev_size = [
                                batch["size"][0][1], batch["size"][0][0]
                            ]
                            image1_size = batch["size"][0]
                            f_1_trans = np.array(
                                transforms.Resize(rev_size)(to_pil(f_1[0])))
                            f_1_crf = crf_refine(np.array(batch["r_img"][0]),
                                                 f_1_trans)

                            new_image = Image.new(
                                'RGB', (3 * image1_size[0], image1_size[1]),
                                (250, 250, 250))
                            img_res = Image.fromarray(f_1_crf)
                            new_image.paste(batch["r_img"][0], (0, 0))
                            new_image.paste(batch["r_mask"][0],
                                            (image1_size[0], 0))
                            new_image.paste(img_res, (image1_size[0] * 2, 0))

                            new_image.save(
                                os.path.join(
                                    args.msd_results_root, "Testing",
                                    "Epoch: " + str(epoch) + " Eval.png"))
                            print("Image saved")
                        idx += 1

                    eval_loss = lovasz_hinge(f_1_gpu, outputs, per_image=False)

                    res = eval_loss.data.cpu()

                    text = "epoch: {}, Eval Loss: {}".format(epoch, res)

                    text = text.ljust(45)
                    eval_pbar.set_description(text)

    ##############################
    # Using multiple GPUs                 args.device_ids = [0, 1, ...]
    ##############################
    else:

        net = LitMirrorNet(args)
        # net = net.load_from_checkpoint(args.root_path + args.ckpt_path + "/MirrorNet-epoch=16-val_loss=3.99.ckpt")
        if args.load_model:
            net = LitMirrorNet.load_from_checkpoint(args.ckpt_path +
                                                    args.ckpt_name,
                                                    args=args)
            print('Loading {} checkpoint.'.format(args.ckpt_path +
                                                  args.ckpt_name))
            trainer = Trainer(gpus=args.device_ids,
                              fast_dev_run=args.fast_dev_run,
                              accelerator='dp',
                              max_epochs=args.epochs,
                              callbacks=[checkpoint_callback],
                              check_val_every_n_epoch=args.val_every,
                              logger=tb_logger,
                              resume_from_checkpoint=args.ckpt_path +
                              args.ckpt_name)
            print("Checkpoint loaded successfully!")
        else:
            trainer = Trainer(gpus=args.device_ids,
                              fast_dev_run=args.fast_dev_run,
                              accelerator='dp',
                              max_epochs=args.epochs,
                              callbacks=[checkpoint_callback],
                              check_val_every_n_epoch=args.val_every,
                              logger=tb_logger)
            # resume_from_checkpoint = args.root_path + args.ckpt_path + "/MirrorNet-epoch=16-val_loss=3.99.ckpt")

        if args.train:
            print("Training")

            trainer.fit(net)
            final_epoch_model_path = args.ckpt_path + "final_epoch.ckpt"
            trainer.save_checkpoint(final_epoch_model_path)

            print("Done")

        else:
            print("Testing")
            # trainer.test(model = net,
            #             ckpt_path = args.ckpt_path+args.ckpt_name)
            trainer.test(model=net)
Exemple #30
0
writer = SummaryWriter(log_dir=vis_path, comment=exp_name)

# Transform Data.
joint_transform = joint_transforms.Compose([
    joint_transforms.RandomRotate(),
    joint_transforms.Resize((args['scale'], args['scale']))
])
img_transform = transforms.Compose([
    transforms.ToTensor(),
    transforms.Normalize([0.485, 0.456, 0.406],
                         [0.229, 0.224, 0.225])  # maybe can optimized.
])
target_transform = transforms.ToTensor()

# Prepare Data Set.
train_set = ImageFolder(msd_training_root, joint_transform, img_transform,
                        target_transform)
print("Train set: {}".format(train_set.__len__()))
train_loader = DataLoader(train_set,
                          batch_size=args['train_batch_size'],
                          num_workers=0,
                          shuffle=True)


def main():
    print(args)
    print(exp_name)

    net = BASE3(backbone_path).cuda(device_ids[0]).train()
    if args['add_graph']:
        writer.add_graph(net,
                         input_to_model=torch.rand(