Exemplo n.º 1
0
def train_cnn():

    modeltype = 'cnn'
    train_batch_size = 2
    sequence_length = 1
    directory_type = 'DPM'

    pixel = Pixel('pixel.pkl')

    train_dataloader, train_data_size, val_dataloader, val_data_size = \
     get_train_data(train_batch_size, sequence_length, directory_type, pixel)

    cnnmodel = CNN()
    if torch.cuda.is_available(): cnnmodel = cnnmodel.cuda()

    for i, batch in enumerate(train_dataloader):
        frame1 = Variable(torch.stack(batch[0])).squeeze(1)
        frame2 = Variable(torch.stack(batch[1]))
        labels = Variable(torch.LongTensor(batch[2]))

        print(frame1.size())
        print(frame2.size())
        print(labels)

        print(cnnmodel(frame1).size())
        break
Exemplo n.º 2
0
    def __init__(self, VAL_FOLD, FOLD_NAME):
        self.MODEL_SAVE = False
        self.RESTORE = False
        self.OLD_EPOCH = 0
        if not self.RESTORE:
            # Set up log directory
            self.LOG_FOLDER = './log/' + FOLD_NAME + '/'
            if not os.path.exists(self.LOG_FOLDER):
                os.makedirs(self.LOG_FOLDER)
            # Set up model directory individually
            self.SESSION_DIR = self.LOG_FOLDER + str(VAL_FOLD) + '/'
            if not os.path.exists(self.SESSION_DIR):
                os.makedirs(self.SESSION_DIR)
        else:
            self.RESTORE_DATE = '20180422'
            self.OLD_EPOCH = 2
            self.LOG_FOLDER = './log/' + self.RESTORE_DATE + '/'
            self.SESSION_DIR = self.LOG_FOLDER + str(VAL_FOLD) + '/'

        # training
        self.LEARNING_RATE = 0.001
        self.SCENES = ['scene1']
        # LSTM
        self.NUM_HIDDEN = 512
        self.NUM_LSTM = 3
        # MLP
        self.NUM_NEURON = 256
        self.NUM_MLP = 3

        # dropout
        self.OUTPUT_THRESHOLD = 0.5
        self.INPUT_KEEP_PROB = 1.0
        self.OUTPUT_KEEP_PROB = 0.9
        self.BATCH_SIZE = 70
        self.EPOCHS = 300
        self.FORGET_BIAS = 0.9
        self.TIMELENGTH = 1000
        self.MAX_GRAD_NORM = 5.0
        self.NUM_CLASSES = 13

        # Get rectangle
        self.VAL_FOLD = VAL_FOLD
        self.TRAIN_SET, self.PATHS = get_train_data(self.VAL_FOLD, self.SCENES,
                                                    self.EPOCHS,
                                                    self.TIMELENGTH)
        self.VALID_SET = get_valid_data(self.VAL_FOLD, self.SCENES, 1,
                                        self.TIMELENGTH)
        self.TOTAL_SAMPLES = len(self.PATHS)
        self.NUM_TRAIN = len(self.TRAIN_SET)
        self.NUM_TEST = len(self.VALID_SET)
        self.SET = {'train': self.TRAIN_SET, 'test': self.VALID_SET}
def train_motion(use_pickle=True):

    modeltype = 'motion'
    train_batch_size = 64
    sequence_length = 6
    directory_type = 'DPM'

    pixel = Pixel('pixel.pkl')
    if use_pickle: pixel.load()
    pretrained = PreTrainedResnet({'intermediate_layers': ['fc']})
    if torch.cuda.is_available():
        pretrained = pretrained.cuda()

    train_dataloader, train_data_size, val_dataloader, val_data_size = \
     get_train_data(train_batch_size, sequence_length, directory_type, pixel, pretrained)

    save_dir = 'models/{}/'.format(modeltype)
    valfile = open('val.txt', 'a')

    dict_args = {
        'input_dim': 4,
        'rnn_hdim': 200,
        'rnn_type': 'LSTM',
        'feature_dim': 100,
    }

    motmodel = MotionModel(dict_args)

    num_epochs = 300
    learning_rate = 1.0
    criterion = nn.NLLLoss()
    optimizer = optim.Adadelta(motmodel.parameters(),
                               lr=learning_rate,
                               rho=0.95,
                               eps=1e-06,
                               weight_decay=0)

    if torch.cuda.is_available():
        motmodel = motmodel.cuda()
        criterion = criterion.cuda()

    for epoch in range(num_epochs):
        for i, batch in enumerate(train_dataloader):
            trackcoords = Variable(torch.stack(batch[3]))
            detectioncoord = Variable(torch.stack(batch[4]))
            labels = Variable(torch.LongTensor(batch[2]))

            if torch.cuda.is_available():
                trackcoords = trackcoords.cuda()
                detectioncoord = detectioncoord.cuda()
                labels = labels.cuda()

            motmodel = motmodel.train()

            output, _ = motmodel(trackcoords, detectioncoord)
            output = functional.log_softmax(output, dim=-1)
            loss = criterion(output, labels)

            optimizer.zero_grad()
            loss.backward()
            optimizer.step()
            nn.utils.clip_grad_norm(motmodel.parameters(), 5.0)

            if ((i + 1) % 10 == 0):
                print('Epoch: [{0}/{1}], Step: [{2}/{3}], Loss: {4}'.format( \
                   epoch+1, num_epochs, i+1, train_data_size//train_batch_size, loss.data[0]))

        #Saving the model
        if (epoch % 1 == 0):
            if not os.path.isdir(
                    os.path.join(save_dir, "epoch{}".format(epoch))):
                os.makedirs(os.path.join(save_dir, "epoch{}".format(epoch)))
            filename = modeltype + '.pth'
            file = open(
                os.path.join(save_dir, "epoch{}".format(epoch), filename),
                'wb')
            torch.save(
                {
                    'state_dict': motmodel.state_dict(),
                    'dict_args': dict_args
                }, file)
            print('Saving the model to {}'.format(save_dir +
                                                  "epoch{}".format(epoch)))
            file.close()

        #Validation accuracy
        if (epoch % 1 == 0):
            num_correct = 0.0
            num_total = 0.0
            for j, batch in enumerate(val_dataloader):

                trackcoords = Variable(torch.stack(batch[3]))
                detectioncoord = Variable(torch.stack(batch[4]))
                labels = Variable(torch.LongTensor(batch[2]))

                if torch.cuda.is_available():
                    trackcoords = trackcoords.cuda()
                    detectioncoord = detectioncoord.cuda()
                    labels = labels.cuda()

                motmodel = motmodel.eval()

                output, _ = motmodel(trackcoords, detectioncoord)

                predictions = output.max(dim=-1)[1]
                num_correct += (predictions == labels).sum().data[0]
                num_total += len(labels)

            accuracy = float(num_correct / num_total)
            print('Epoch {} Accuracy {} \n'.format(epoch, accuracy))
            valfile.write('Epoch {} Accuracy {} \n'.format(epoch, accuracy))

    valfile.close()
Exemplo n.º 4
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--num_lights', type=int, default=1053)
    parser.add_argument('--num_samples', type=int, default=5)
    parser.add_argument('--degree', type=int, default=45, choices=[45, 90])
    parser.add_argument(
        '--sampler',
        type=str,
        default='original',
        choices=['original', 'gaussian', 'categorical', 'basicAdaptive'])
    parser.add_argument('--methods',
                        type=str,
                        default='sum',
                        choices=['sum', 'sample'])
    parser.add_argument('--lr', type=float, default=0.0001)
    parser.add_argument('--num_workers', type=int, default=2)
    parser.add_argument('--num_scene', type=int, default=4)
    parser.add_argument('--num_gt', type=int, default=18)
    parser.add_argument('--path', type=str, default='exp')
    parser.add_argument('--num_epochs', type=int, default=16)
    parser.add_argument('--dataset_path',
                        type=str,
                        default='/home/hza/data/rendering/cropped.hdf5')
    args = parser.parse_args()

    if args.degree is not None:
        degree = args.degree / 180. * np.pi
        args.num_lights = (angles <= degree).sum()
        global Light
        Light = Light[np.where(angles <= degree)]
    print('NUM_LIGHT:', args.num_lights)

    net = RelightNetwork(args.num_lights,
                         args.num_samples,
                         type_sampler=args.sampler,
                         sample_methods=args.methods,
                         Light=Light).cuda()
    optim = torch.optim.Adam(net.parameters(), args.lr)

    def zipper(net, optim):
        return {'optim': optim, 'net': net}

    resume_if_exists(args.path, zipper(net, optim))
    ## todo: tensorboard for relightling effects
    data = get_train_data(num_workers=args.num_workers,
                          batch_size=args.num_scene,
                          path=args.dataset_path,
                          degree=args.degree,
                          num_split=args.num_gt)
    viewer = Visualizer(args.path)

    for epochs in range(args.num_epochs):
        b = data.__iter__()

        num_batchs = len(b)
        for j in tqdm.trange(num_batchs):
            ids = (epochs + float(j) / num_batchs)
            T = 1 + 5 * (ids + 0.0)**2.0
            #T = 1 + 1000 * (ids+ 0.0) ** 2.0

            tmp = next(b)
            inps = tmp[0].cuda()
            inps = (inps.permute(0, 1, 4, 2, 3).float() - 127.5) / 127.5
            ws = []
            gts = []

            light_ids = tmp[1].numpy()
            for scene_id in range(len(inps)):
                w = []
                gt = []
                for k in range(args.num_gt):
                    #sampled = np.random.randint(args.num_lights)
                    sampled = light_ids[scene_id][k]
                    w.append(Light[sampled])
                    gt.append(inps[scene_id][sampled])
                ws.append(torch.stack(w, dim=0))
                gts.append(torch.stack(gt, dim=0))

            gts = torch.stack(gts, dim=0)
            ws = torch.stack(ws, dim=0)

            optim.zero_grad()
            out, logp = net(inps, ws, alpha=T)

            gts = gts.view(gts.size(0) * gts.size(1), *gts.size()[2:])
            loss = ((out - gts)**2).mean(dim=3).mean(dim=2).mean(
                dim=1)  #nn.functional.mse_loss(out, gts)

            adv = loss.detach().reshape(len(inps), -1).sum(dim=1)
            prob_loss = (adv * logp).mean()
            total_loss = loss.mean() + prob_loss

            total_loss.backward()
            optim.step()

            if j % 100 == 0:
                toshow = torch.cat((gts, out),
                                   dim=3).detach().cpu().detach().numpy()
                toshow = np.minimum(toshow, 1)
                toshow = np.maximum(toshow, -1)
                toshow = (toshow * 127.5 + 127.5).astype(np.uint8)

                #if j % 3000 == 0:
                if args.sampler == 'original':
                    mask = [
                        nn.functional.softmax(net.sample.mask * T,
                                              dim=0).cpu().detach().numpy()
                    ]
                else:
                    mask = net.sample.extra_output

                imgs = []
                for mask in mask:
                    #inds = mask.argmax(axis=0)
                    #values = [mask[inds[i], i] for i in range(inds.shape[0])]
                    tmp = []
                    for j in range(mask.shape[1]):
                        tmp.append(viewDirection(mask[:, j], args.degree, 31))
                    tmp = np.concatenate(tmp, axis=1)
                    imgs.append(tmp)
                imgs = np.float32(np.concatenate(imgs))

                viewer({
                    'img': toshow,
                    'loss': loss.mean().cpu().detach().numpy(),
                    'prob_loss': prob_loss.cpu().detach().numpy(),
                    'attention': imgs[None, None, :]
                })

        dict = zipper(net, optim)
        save(args.path, dict, epochs)
Exemplo n.º 5
0
def train_driver(model_name,
                 pretrained,
                 load_choice,
                 models_path,
                 train_data_path,
                 val_data_path,
                 num_epochs,
                 batch_size,
                 image_size,
                 num_workers,
                 use_num_worker_mult=True):
    """Loads and trains the selected model.
    
    Attributes:
        model                   (models): The model to be trained.
        model_epoch             (int): The last epoch of the loaded model.
        dataloaders             (dict): Dictionary of {train:training dataloader, val:validation dataloader}
        loss_function           (loss): The loss function.
        optimizer               (optim): The optimizer.
        is_inception            (bool): True if the model is the Inception network.
        device                  (device): The GPU device to train on.

    Args:
        model_name              (str): The name of the model.
        pretrained              (bool): True to load a pretrained model, False to load an untrained model.
        load_choice             (int): The model version to be loaded. -1 for a new model.
        models_path              (str): The folder path where saved models are stored.
        train_data_path         (str): The folder path where training images are stored.
        val_data_path           (str): The folder path where validation images are stored.
        num_epochs              (int): The number of epochs to train for.
        batch_size              (int): The number of images per batch.
        image_size              (int): The size for all images to be resized to.
        num_workers             (int): The number of workers for DataLoader.
        use_num_worker_mult     (bool): If False, number of workers = num_workers. 
                                            If True, number of workers = num_workers times number of CPU cores.
    """
    if use_num_worker_mult:
        cpu_cores = int(multiprocessing.cpu_count())
        num_workers = num_workers * cpu_cores
        print(f"Number of CPU cores: {cpu_cores}")
    print(f"Number of workers: {num_workers}")

    train_loader, val_loader, num_classes = dataloader.get_train_data(
        train_data_path,
        val_data_path,
        batch_size=batch_size,
        image_size=image_size,
        num_workers=num_workers)
    dataloaders = {'train': train_loader, 'val': val_loader}

    model, model_epoch = get_model(model_name, pretrained, load_choice,
                                   num_classes, models_path)
    print(f"Previous epoch: {model_epoch}")

    if model is not None:
        device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
        if device != "cpu":
            if torch.cuda.device_count() > 1:
                print(f"{torch.cuda.device_count()}GPUs detected.")
                model = torch.nn.DataParallel(model)
        model = model.to(device)
        print(f"Device: {device}")

        loss_function = torch.nn.CrossEntropyLoss()

        optimizer = torch.optim.SGD(model.parameters(), lr=0.001, momentum=0.9)
        train_model(model,
                    model_name,
                    models_path,
                    model_epoch,
                    dataloaders,
                    loss_function,
                    optimizer,
                    num_epochs,
                    device,
                    is_inception=(model_name == "inception"))