def test_observation_data(self): """ Test observation data """ transform = transforms.ToTensor() dataset = RolloutObservationDataset('datasets/carracing', transform) loader = torch.utils.data.DataLoader(dataset, batch_size=32, shuffle=True, num_workers=8) dataset.load_next_buffer() init_time = time.time() for i, data in enumerate(loader): if i == 150: self.assertEqual(data.size(), torch.Size([32, 3, 96, 96])) break end_time = time.time() print("WallTime: {}s".format(end_time - init_time))
def make_vae_dataset(rollout_dir): step_log('2-1. make_vae_dataset START!!') transform_train = transforms.Compose([ transforms.ToPILImage(), transforms.Resize((RED_SIZE, RED_SIZE)), transforms.RandomHorizontalFlip(), transforms.ToTensor(), ]) transform_test = transforms.Compose([ transforms.ToPILImage(), transforms.Resize((RED_SIZE, RED_SIZE)), transforms.ToTensor(), ]) dataset_train = RolloutObservationDataset(rollout_dir, transform_train, train=True) dataset_test = RolloutObservationDataset(rollout_dir, transform_test, train=False) return dataset_train, dataset_test
optimizer_p.load_state_dict(state['optimizer']) print('prediction load success') state = torch.load('/home/ld/gym-car/log/vae/vae_checkpoint_52.pkl') model.load_state_dict(state['state_dict']) optimizer.load_state_dict(state['optimizer']) trained=state['epoch'] print('vae load success') trained=0 cur_best = None all_data=6000 sample_data=1000 sample_buff=all_data/sample_data sample_count=0 training_sample=0 for epoch in range(trained+1, args.epochs + 1): dataset_train = RolloutObservationDataset('/data/result/',transform_train, train=True,sample_data=sample_data,sample_count=sample_count) #dataset_test = RolloutObservationDataset('/data/result/',transform_test, train=False,sample_data=sample_data,sample_count=sample_count) sample_count+=1 if sample_count==sample_buff: sample_count=0 train_loader = torch.utils.data.DataLoader( dataset_train, batch_size=args.batch_size, shuffle=True, num_workers=32,drop_last=True) # test_loader = torch.utils.data.DataLoader( # dataset_test, batch_size=args.batch_size, shuffle=False, num_workers=32,drop_last=True) training_sample=train(epoch,vae_dir,training_sample) #exit() #test_loss = test() # scheduler.step(test_loss) # earlystopping.step(test_loss) # checkpointing
transform_train = transforms.Compose([ transforms.ToPILImage(), transforms.Resize((RED_SIZE, RED_SIZE)), transforms.RandomHorizontalFlip(), transforms.ToTensor(), ]) transform_test = transforms.Compose([ transforms.ToPILImage(), transforms.Resize((RED_SIZE, RED_SIZE)), transforms.ToTensor(), ]) dataset_train = RolloutObservationDataset('datasets/pacman', transform_train, train=True) dataset_test = RolloutObservationDataset('datasets/pacman', transform_test, train=False) train_loader = torch.utils.data.DataLoader(dataset_train, batch_size=args.batch_size, shuffle=True, num_workers=2) test_loader = torch.utils.data.DataLoader(dataset_test, batch_size=args.batch_size, shuffle=True, num_workers=2) model = VAE(3, LSIZE).to(device)
transform_train = transforms.Compose([ transforms.ToPILImage(), transforms.Resize((RED_SIZE, RED_SIZE)), transforms.RandomHorizontalFlip(), transforms.ToTensor(), ]) transform_test = transforms.Compose([ transforms.ToPILImage(), transforms.Resize((RED_SIZE, RED_SIZE)), transforms.ToTensor(), ]) dataset_train = RolloutObservationDataset('datasets/carracing', transform_train, train=True) dataset_test = RolloutObservationDataset('datasets/carracing', transform_test, train=False) train_loader = torch.utils.data.DataLoader(dataset_train, batch_size=args.batch_size, shuffle=True, num_workers=args.max_workers) test_loader = torch.utils.data.DataLoader(dataset_test, batch_size=args.batch_size, shuffle=True, num_workers=args.max_workers) model = VAE(3, LSIZE).to(device) optimizer = optim.Adam(model.parameters())
transform_train = transforms.Compose([ transforms.ToPILImage(), transforms.Resize((RED_SIZE, RED_SIZE)), # transforms.RandomHorizontalFlip(), transforms.ToTensor(), ]) transform_val = transforms.Compose([ transforms.ToPILImage(), transforms.Resize((RED_SIZE, RED_SIZE)), transforms.ToTensor(), ]) dataset_train = RolloutObservationDataset( 'datasets/carnav', transform_train, train=True, num_val_rollouts=args.num_val_rollouts) dataset_val = RolloutObservationDataset('datasets/carnav', transform_val, train=False, num_val_rollouts=args.num_val_rollouts) train_loader = torch.utils.data.DataLoader(dataset_train, batch_size=args.batch_size, shuffle=True, num_workers=2) val_loader = torch.utils.data.DataLoader(dataset_val, batch_size=args.batch_size, shuffle=True, num_workers=2)
def train_vae(logdir, traindir, epochs=100, testdir=None): print('Training VAE using traindir', traindir) batch_size = 100 # maybe should change this back to their initial one of 32 noreload = False #Best model is not reloaded if specified nosamples = False #'Does not save samples during training if specified' testdir = testdir if testdir else traindir torch.manual_seed(123) device = torch.device("cuda" if torch.cuda.is_available() else "cpu") transform_train = transforms.Compose([ transforms.ToPILImage(), transforms.Resize((RED_SIZE, RED_SIZE)), transforms.RandomHorizontalFlip(), transforms.ToTensor(), ]) transform_test = transforms.Compose([ transforms.ToPILImage(), transforms.Resize((RED_SIZE, RED_SIZE)), transforms.ToTensor(), ]) dataset_train = RolloutObservationDataset(traindir, transform_train, train=True) dataset_test = RolloutObservationDataset(testdir, transform_test, train=False) train_loader = torch.utils.data.DataLoader(dataset_train, batch_size=batch_size, shuffle=True, num_workers=2) test_loader = torch.utils.data.DataLoader(dataset_test, batch_size=batch_size, shuffle=True, num_workers=2) model = VAE(3, LSIZE).to(device) optimizer = optim.Adam(model.parameters()) scheduler = ReduceLROnPlateau(optimizer, 'min', factor=0.5, patience=5) earlystopping = EarlyStopping('min', patience=30) # Reconstruction + KL divergence losses summed over all elements and batch def loss_function(recon_x, x, mu, logsigma): """ VAE loss function """ BCE = F.mse_loss(recon_x, x, size_average=False) # see Appendix B from VAE paper: # Kingma and Welling. Auto-Encoding Variational Bayes. ICLR, 2014 # https://arxiv.org/abs/1312.6114 # 0.5 * sum(1 + log(sigma^2) - mu^2 - sigma^2) KLD = -0.5 * torch.sum(1 + 2 * logsigma - mu.pow(2) - (2 * logsigma).exp()) return BCE + KLD def train(epoch): """ One training epoch """ model.train() dataset_train.load_next_buffer() train_loss = 0 for batch_idx, data in enumerate(train_loader): data = data.to(device) optimizer.zero_grad() recon_batch, mu, logvar = model(data) loss = loss_function(recon_batch, data, mu, logvar) loss.backward() train_loss += loss.item() optimizer.step() if batch_idx % 20 == 0: print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format( epoch, batch_idx * len(data), len(train_loader.dataset), 100. * batch_idx / len(train_loader), loss.item() / len(data))) print('====> Epoch: {} Average loss: {:.4f}'.format( epoch, train_loss / len(train_loader.dataset))) def test(): """ One test epoch """ model.eval() dataset_test.load_next_buffer() test_loss = 0 with torch.no_grad(): for data in test_loader: data = data.to(device) recon_batch, mu, logvar = model(data) test_loss += loss_function(recon_batch, data, mu, logvar).item() test_loss /= len(test_loader.dataset) print('====> Test set loss: {:.4f}'.format(test_loss)) return test_loss # check vae dir exists, if not, create it vae_dir = join(logdir, 'vae') if not exists(vae_dir): mkdir(vae_dir) mkdir(join(vae_dir, 'samples')) reload_file = join(vae_dir, 'best.tar') if not noreload and exists(reload_file): state = torch.load(reload_file) print("Reloading model at epoch {}" ", with test error {}".format(state['epoch'], state['precision'])) model.load_state_dict(state['state_dict']) optimizer.load_state_dict(state['optimizer']) scheduler.load_state_dict(state['scheduler']) earlystopping.load_state_dict(state['earlystopping']) cur_best = None for epoch in range(1, epochs + 1): train(epoch) test_loss = test() scheduler.step(test_loss) earlystopping.step(test_loss) # checkpointing best_filename = join(vae_dir, 'best.tar') filename = join(vae_dir, 'checkpoint.tar') is_best = not cur_best or test_loss < cur_best if is_best: cur_best = test_loss save_checkpoint( { 'epoch': epoch, 'state_dict': model.state_dict(), 'precision': test_loss, 'optimizer': optimizer.state_dict(), 'scheduler': scheduler.state_dict(), 'earlystopping': earlystopping.state_dict() }, is_best, filename, best_filename) if not nosamples: with torch.no_grad(): sample = torch.randn(RED_SIZE, LSIZE).to(device) sample = model.decoder(sample).cpu() save_image( sample.view(64, 3, RED_SIZE, RED_SIZE), join(vae_dir, 'samples/sample_' + str(epoch) + '.png')) if earlystopping.stop: print( "End of Training because of early stopping at epoch {}".format( epoch)) break
transform_train = transforms.Compose([ transforms.ToPILImage(), transforms.Resize((RED_SIZE, RED_SIZE)), # transforms.RandomHorizontalFlip(), transforms.ToTensor(), ]) transform_test = transforms.Compose([ transforms.ToPILImage(), transforms.Resize((RED_SIZE, RED_SIZE)), transforms.ToTensor(), ]) dataset_train = RolloutObservationDataset('./datasets/control', transform_train, train=True) dataset_test = RolloutObservationDataset('./datasets/control', transform_test, train=False) train_loader = torch.utils.data.DataLoader(dataset_train, batch_size=args.batch_size, shuffle=True, num_workers=8, drop_last=True) test_loader = torch.utils.data.DataLoader(dataset_test, batch_size=args.batch_size, shuffle=True, num_workers=8, drop_last=True)
transforms.RandomHorizontalFlip(), transforms.ToTensor(), ]) transform_test = transforms.Compose([ transforms.ToPILImage(), transforms.Resize((RED_SIZE, RED_SIZE)), transforms.ToTensor(), ]) dataset_dir = args.dataset_dir if args.iteration_num is not None: dataset_dir = join(args.dataset_dir, 'iter_{}'.format(args.iteration_num)) dataset_train = RolloutObservationDataset(dataset_dir, transform_train, train=True) dataset_test = RolloutObservationDataset(dataset_dir, transform_test, train=False) train_loader = torch.utils.data.DataLoader(dataset_train, batch_size=args.batch_size, shuffle=True, num_workers=2) test_loader = torch.utils.data.DataLoader(dataset_test, batch_size=args.batch_size, shuffle=True, num_workers=2) model = VAE(3, LSIZE).to(device) optimizer = optim.Adam(model.parameters())
transform_train = transforms.Compose([ transforms.ToPILImage(), transforms.Resize((RED_SIZE, RED_SIZE)), transforms.RandomHorizontalFlip(), transforms.ToTensor(), ]) transform_test = transforms.Compose([ transforms.ToPILImage(), transforms.Resize((RED_SIZE, RED_SIZE)), transforms.ToTensor(), ]) dataset_train = RolloutObservationDataset(root=args.datadir, transform=transform_train, train_size=60, train=True) dataset_test = RolloutObservationDataset(root=args.datadir, transform=transform_test, train_size=60, train=False) train_loader = torch.utils.data.DataLoader(dataset_train, batch_size=args.batch_size, shuffle=True, num_workers=2) test_loader = torch.utils.data.DataLoader(dataset_test, batch_size=args.batch_size, shuffle=True, num_workers=2) model = VAE(3, LSIZE).to(device)
transform_train = transforms.Compose([ transforms.ToPILImage(), transforms.Resize((RED_SIZE, RED_SIZE)), transforms.RandomHorizontalFlip(), transforms.ToTensor(), ]) transform_test = transforms.Compose([ transforms.ToPILImage(), transforms.Resize((RED_SIZE, RED_SIZE)), transforms.ToTensor(), ]) dataset_train = RolloutObservationDataset(join(args.datasets, 'carracing'), transform_train, train=True) dataset_test = RolloutObservationDataset(join(args.datasets, 'carracing'), transform_test, train=False) train_loader = torch.utils.data.DataLoader(dataset_train, batch_size=args.batch_size, shuffle=True, num_workers=2) test_loader = torch.utils.data.DataLoader(dataset_test, batch_size=args.batch_size, shuffle=True, num_workers=2) model = VAE(3, LSIZE).to(device) optimizer = optim.Adam(model.parameters())