def __init__(self, arguments): self.arguments = arguments input_dims = 28, 28, 1 self.ae = AutoEncoder( input_dims=input_dims, encoder_filters=arguments.encoder_filters, encoder_conv_kernels=arguments.encoder_conv_kernels, encoder_conv_strides=arguments.encoder_conv_strides, decoder_filters=arguments.decoder_filters, decoder_conv_kernels=arguments.decoder_conv_kernels, decoder_conv_strides=arguments.decoder_conv_strides, latent_dim=arguments.latent_dim, use_batch_norm=arguments.use_batch_norm, use_dropout=arguments.use_dropout).to(device) self.ae.train() self.criterion = nn.MSELoss() self.encoder_optimizer = opt.Adam(params=self.ae.encoder.parameters(), lr=arguments.learning_rate) self.decoder_optimizer = opt.Adam(params=self.ae.decoder.parameters(), lr=arguments.learning_rate) self.writer = SummaryWriter( logdir=os.path.join(self.arguments.log_dir, self.arguments.data), comment='epoch_{0:03d}_batch_size_{1:03d}_lr_{2:.03f}'.format( self.arguments.epochs - 1, self.arguments.batch_size, self.arguments.learning_rate))
def run(data_obj, training_size): data_obj.split_dataset(training_size) data_obj.preprocess() #-------------------------------- Autoencoder model ae_model = AutoEncoder(data_obj.x_train_scaled.shape[1], training_size, data_obj.name) ae_model.train(data_obj.x_train_scaled, data_obj.x_val_scaled) #-------------------------------- Encoded representation x_train_encoded, x_val_encoded, x_test_encoded = ae_model.encoded_data( data_obj.x_train_scaled, data_obj.x_val_scaled, data_obj.x_test_scaled) #-------------------------------- Neural Network model nn_model = NeuralNetwork( data_obj.x_train_scaled.shape[1], data_obj.y_train.shape[1], training_size, data_obj.name) nn_model.train( x_train_encoded, data_obj.y_train, x_val_encoded, data_obj.y_val) nn_model.evaluate(x_test_encoded, data_obj.y_test) #-------------------------------- reset data from memory data_obj.reset_scalar() return nn_model.result()
class trainAE(object): def __init__(self, path, k, lr= 0.1, batch_size=1, loss='bce', n_epochs=100): ''' Arguments: path : path to training data k : hidde unit's dimension lr : learning rate batch_size : batch_size for training, currently set to 1 loss : loss function (bce, rmse) to train AutoEncoder n_epochs : number of epochs for training ''' self.AE = AutoEncoder(path, k) # Definne the autoencoder model #self.AE.model() self.AE.model_batch() self.epochs = n_epochs def train(self): T = self.AE.T # COnverting to csr format for indexing T = T.tocsr() #pdb.set_trace() nonzero_indices = T.nonzero() for epoch in xrange(self.epochs): print("Running epoch %d"%(epoch)) for i in np.unique(nonzero_indices[0]): # get indices of observed values from the user 'i' 's vector indices = T[i, :].nonzero()[1] #print indices #indices = indices.reshape(indices.shape[0],) # Get correspoding ratings ratings = T[i, indices].toarray() #print ratings ratings = ratings.reshape(ratings.shape[1],) # Convert inputs to theano datatype indices = indices.astype(np.int32) ratings = ratings.astype(np.int32) #pdb.set_trace() loss = self.AE.ae(indices, ratings) print("Loss at epoch %d is %f"%(epoch, loss)) # Batch training method def train_batch(self, batch_size): T = self.AE.T T = T.tocsr() nonzero_indices = T.nonzero() #pdb.set_trace() n_users = len(np.unique(nonzero_indices[0])) indices = np.unique(nonzero_indices[0]) for epoch in xrange(self.epochs): for ind, i in enumerate(xrange(0, n_users, batch_size)): ratings = T[indices[i:(i + batch_size)], :].toarray().astype(np.float32) #print ratings #pdb.set_trace() loss = self.AE.ae_batch(ratings) #loss = self.AE.debug(ratings) print loss
def main(opt): device = 'cuda' if torch.cuda.is_available() else 'cpu' # Dataset print('Dataset....') transform = transforms.Compose([ transforms.Resize((600, 600)), transforms.Grayscale(3), transforms.ToTensor() ]) train_set = myDataset(image_path=opt.train_path, transform=transform) val_set = myDataset(image_path=opt.val_path, transform=transform) train_loader = DataLoader(train_set, batch_size=opt.train_batch_size) val_loader = DataLoader(val_set, batch_size=opt.val_batch_size) # Model print('Model....') model = AutoEncoder() model.to(device) # Optimizer optimizer = optim.Adam(model.parameters(), lr=opt.lr) loss_func = nn.MSELoss() # Train print('Training....') train_epoch_loss = [] val_epoch_loss = [] train_iter_losses = [] val_iter_losses = [] for e in range(opt.epoch): train_iter_loss = train(opt, model, train_loader, optimizer, loss_func, device, e) train_iter_losses += train_iter_loss train_epoch_loss.append(sum(train_iter_loss)) val_iter_loss = val(opt, model, val_loader, loss_func, device, e) val_iter_losses += val_iter_loss val_epoch_loss.append(sum(val_iter_loss)) # save model best = 10000 if val_epoch_loss[-1] < best: print('Saving Model....') torch.save(model, 'weights/AutoEncoder_try1.pth') best = val_epoch_loss[-1] print('Saving Result') plt.figure(figsize=(10, 10)) plt.plot(train_iter_losses) plt.plot(val_iter_losses) plt.legend(['Train_loss', 'Val_loss']) plt.savefig('Result.jpg')
def main(): use_cuda = args.use_cuda train_data = UnlabeledContact(data=args.data_dir) print('Number of samples: {}'.format(len(train_data))) trainloader = DataLoader(train_data, batch_size=args.batch_size) # Contact matrices are 21x21 input_size = 441 img_height = 21 img_width = 21 vae = AutoEncoder(code_size=20, imgsize=input_size, height=img_height, width=img_width) criterion = nn.BCEWithLogitsLoss() if use_cuda: #vae = nn.DataParallel(vae) vae = vae.cuda() #.half() criterion = criterion.cuda() optimizer = optim.SGD(vae.parameters(), lr=0.01) clock = AverageMeter(name='clock32single', rank=0) epoch_loss = 0 total_loss = 0 end = time.time() for epoch in range(15): for batch_idx, data in enumerate(trainloader): inputs = data['cont_matrix'] inputs = inputs.resize_(args.batch_size, 1, 21, 21) inputs = inputs.float() if use_cuda: inputs = inputs.cuda() #.half() inputs = Variable(inputs) optimizer.zero_grad() output, code = vae(inputs) loss = criterion(output, inputs) loss.backward() optimizer.step() epoch_loss += loss.data[0] clock.update(time.time() - end) end = time.time() if batch_idx % args.log_interval == 0: print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format( epoch, batch_idx * len(data), len(trainloader.dataset), 100. * batch_idx / len(trainloader), loss.data[0])) clock.save( path= '/home/ygx/libraries/mds/molecules/molecules/conv_autoencoder/runtimes' )
def train(output_filename, model_type, hidden_size, loss_type, norm_type, sigma_noise): train_data = torchvision.datasets.MNIST( root='datasets/mnist/', train=True, transform=torchvision.transforms.ToTensor(), download=False, ) train_loader = Data.DataLoader(dataset=train_data, batch_size=BATCH_SIZE, shuffle=True) if loss_type == 'l2': loss_func = nn.MSELoss() elif loss_type == 'cross_entropy': loss_func = F.binary_cross_entropy if model_type == 'AE': model = AutoEncoder(hidden_size).cuda() elif model_type == 'LTAE': model = LatentAutoEncoder(hidden_size, norm_type, sigma=sigma_noise).cuda() model.set_device() elif model_type == 'VAE': model = VariationalAE(hidden_size).cuda() optimizer = torch.optim.Adam(model.parameters(), lr=0.001) model.train() for epoch in range(EPOCH): for step, (x, _) in enumerate(train_loader): optimizer.zero_grad() x_batch = x.view(-1, 28 * 28).cuda() y_batch = x.view(-1, 28 * 28).cuda() if model_type == 'AE': _, decoded = model(x_batch) loss = loss_func(decoded, y_batch) elif model_type == 'LTAE': _, latent, transformed, decoded = model(x_batch) loss = loss_func(decoded, y_batch) loss += torch.nn.functional.mse_loss(transformed, latent) elif model_type == 'VAE': decoded, mu, logvar = model(x_batch) loss = loss_func_vae(decoded, x_batch, mu, logvar, loss_type) loss.backward() optimizer.step() if epoch % 10 == 0: print('Epoch: ', epoch, '| train loss: %.4f' % loss.detach().cpu()) torch.save({'state_dict': model.state_dict()}, f'./saved_models/{output_filename}')
def __init__(self, path, k, lr= 0.1, batch_size=1, loss='bce', n_epochs=50): ''' Arguments: path : path to training data k : hidde unit's dimension lr : learning rate batch_size : batch_size for training, currently set to 1 loss : loss function (bce, rmse) to train AutoEncoder n_epochs : number of epochs for training ''' self.AE = AutoEncoder(path, k) # Definne the autoencoder model #self.AE.model() self.AE.model_batch() self.epochs = n_epochs
def run_test(): model = AutoEncoder(n=2, dim_x=50, dim_hidden=2000) device_id = 0 model_file = 'model/dae.pth' net = load_pretrained_model(model, model_file, device_id) x_test = pickle.load(open('data/manifold_test.pkl', 'rb')) x_true = x_test[:] noise_shape = x_true.shape[1:] n_dim = np.prod(noise_shape) sigma_train = 0.01 sigma_proposal = 0.01 sigma_noise_hat_init = 0.01 for noise in [0.01, 0.02, 0.03, 0.04]: sigma_noise = [noise] * n_dim sigma_noise = np.array(sigma_noise[:n_dim]).reshape(noise_shape) rmse_mean, rmse_std, noise_mean, noise_std, variance_noise_hat_em = \ solve_agem(net=net, x_true=x_true, sigma_noise=sigma_noise, sigma_train=sigma_train, sigma_noise_hat_init=sigma_noise_hat_init, sigma_proposal=sigma_proposal, type_proposal='mala', candidate='mean', em_epochs=10, sample_epochs=1000) print('[AGEM] noise_gt: %.2f | rmse %.4f (%.4f), noise_est: %.4f (%.4f)' % ( noise, rmse_mean, rmse_std, noise_mean, noise_std ))
def build_net(self): # define network self.net = AutoEncoder(self.in_channel, self.image_size, self.hidden_dim, self.output_dim) if self.config.mode == 'test' and self.config.training_path == '': print("[*] Enter model path!") exit() # if training model exists if self.config.training_path != '': self.net.load_state_dict( torch.load(self.config.training_path, map_location=lambda storage, loc: storage)) print("[*] Load weight from {}!".format(self.config.training_path)) self.net.to(self.device)
def generate(model_path,model_name, generate_path, generate_name, piece): """Synthesize audio from an array of embeddings. Args: encodings: Numpy array with shape [batch_size, time, dim]. save_paths: Iterable of output file names. checkpoint_path: Location of the pretrained model. [model.ckpt-200000] samples_per_save: Save files after every amount of generated samples. """ # Create directory for encoding if os.path.exists(generate_path) is False: os.makedirs(generate_path) net = AutoEncoder() net = load_model(net,model_path,model_name) cuda_available = torch.cuda.is_available() if cuda_available is True: net = net.cuda() net.eval() # Load audio for encoding piece = audio.load_wav(piece) spec = audio.spectrogram(piece).astype(np.float32) spec = torch.from_numpy(spec.T) spec = torch.FloatTensor(spec) spec = torch.unsqueeze(spec, 0) spec = Variable(spec, volatile=True).contiguous() if cuda_available is True: spec = spec.cuda() generated_spec = net(spec) generated_spec = generated_spec.data.cpu().numpy() generated_spec = np.squeeze(generated_spec) waveform = audio.inv_spectrogram(generated_spec.T) wav_name = generate_path + generate_name + '.wav' audio.save_wav(waveform , wav_name)
def get_network(args_hiddens, logger=None): """ get autoencoder :param args_hiddens: args.hiddens (comma separated) :param logger: logger :return: autoencoder model """ hidden_dims = [int(h) for h in args_hiddens.split(',')] return AutoEncoder(hidden_dims, logger=logger)
def run(args): # Create AutoEncoder autoencoder = AutoEncoder(args['input_shape'], args['z_dim'], args['c_dim'], learning_rate=args['learning_rate']) # train autoencoder.train(args['train_dir'], args['val_dir'], args['epochs'], args['batch_size'], args['output_dir']) # plot x = autoencoder.sample_data() plot_original(x, save_dir=args['output_dir']) plot_reconstruction(x, autoencoder, save_dir=args['output_dir']) plot_zvariation(x, autoencoder, save_dir=args['output_dir']) plot_cvariation(x, autoencoder, save_dir=args['output_dir']) plot_zsemireconstructed(x, autoencoder, save_dir=args['output_dir']) plot_csemireconstructed(x, autoencoder, save_dir=args['output_dir'])
def gen_pic(ckpt, epoch): device = torch.device('cuda') model = AutoEncoder().to(device) model.load_state_dict(torch.load(ckpt)) model.eval() sample = torch.randn(64, 20).to(device) sample = model.decoder(sample).cpu() save_image(sample.view(64, 1, 28, 28), 'results/sample_e{:02}.png'.format(epoch))
def main(_): create_dirs([FLAGS.summary_dir, FLAGS.checkpoint_dir]) config = auto_encoder_config() model = AutoEncoder(config) sess = tf.Session() trainer = AutoEncoderTrainer(sess, model, FLAGS) trainer.train()
def __init__(self, args): self.device = torch.device( 'cuda' if torch.cuda.is_available() else 'cpu') self.id = args.id self.dataset_path = args.dataset model = AutoEncoder(args.E, args.D) if args.load: model.load_state_dict(torch.load(args.load)) self.model = model.to(self.device) self.optimizer = optim.Adam(self.model.parameters(), lr=args.lr) self.metric = nn.MSELoss() self.epoch_num = args.epoch self.batch_size = args.bs self.cluster_num = args.cluster_num self.save = args.save self.testcase = args.testcase self.csv = args.csv self.record_file = None self.best = {'epoch': 0, 'loss': 999} self.test_images = get_test_image(args.dataset)
def main(): torch.manual_seed(1618) device = 'cuda' if torch.cuda.is_available() else 'cpu' print('Using PyTorch Device : {}'.format(device.upper())) n_epochs = 800 LOGDIR = './runs/' + datetime.now().strftime('%Y%m%d_%H%M%S') logger = Logger(log_dir=LOGDIR) criterion = nn.MSELoss().to(device) lr = 1e-4 model = AutoEncoder(connections=128).to(device) optim = torch.optim.Adam(itertools.chain(model.encoder.parameters(), model.decoder.parameters()), lr=lr) #model = AE_3D_200().to(device) #optim = torch.optim.Adam(itertools.chain(model.encoder.parameters(), model.decoder.parameters()), lr=lr, weight_decay=1e-6) train_batch, val_batch, test_batch = get_data_batches(device=device, frac=1.0) print(train_batch.size()) print(val_batch.size()) print(test_batch.size()) worst_case_loss = torch.FloatTensor([float('Inf')]).to(device) pbar = tqdm(range(n_epochs), leave=True) for e in pbar: new_lr = lr * (0.2**((e + 1) // 100)) for param_group in optim.param_groups: param_group['lr'] = new_lr optim.zero_grad() recon_batch = model(train_batch) loss = criterion(recon_batch, train_batch) loss.backward() optim.step() model.eval() recon_val = model(val_batch) val_loss = nn.MSELoss()(recon_val, val_batch) recon_test = model(test_batch) test_loss = nn.MSELoss()(recon_test, test_batch) model.train() info = { 'train_loss': loss.item(), 'val_loss': val_loss.item(), 'test_loss': test_loss.item() } for tag, value in info.items(): logger.scalar_summary(tag, value, e) torch.save(model.encoder.state_dict(), LOGDIR + '/encoder_epoch_{}.pt'.format(e)) torch.save(model.decoder.state_dict(), LOGDIR + '/decoder_epoch_{}.pt'.format(e)) pbar.set_description( 'train_loss: {:.4f}, val_loss: {:.4f}, test_loss: {:.4f}'.format( loss.item(), val_loss.item(), test_loss.item()))
def train(dataloader, parameters, device): model = AutoEncoder(input_dim=1900, nlayers=parameters.get('nlayers', 5), latent=100) model = model.to(device) model.train() train_loss = 0 optimizer = torch.optim.Adam(model.parameters(), lr=parameters.get('lr', 1e-5), weight_decay=parameters.get( 'weight_decay', 0.)) loss_func = torch.nn.MSELoss() for epoch in range(parameters.get('epochs', 1000)): for index, (data, ) in enumerate(dataloader, 1): optimizer.zero_grad() output = model(data) loss = loss_func(output, data) train_loss += loss.item() loss.backward() optimizer.step() return model
def main(dataset, net_config, _run): # Add all of the config into the helper class for key in net_config: setattr(a, key, net_config[key]) setattr(a, 'EXP_OUT', EXP_OUT) setattr(a, 'RUN_id', _run._id) output_dir = create_directories(_run._id, ex) gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.9) with tf.Session(config=tf.ConfigProto(gpu_options=gpu_options)) as sess: # load the dataset class data = get_dataset(dataset['name']) data = data(**dataset) model = AutoEncoder(sess, image_size=a.input_image_size, batch_size=a.batch_size, output_size=a.input_image_size, dataset_name=dataset['name'], checkpoint_dir=output_dir, data=data, momentum=a.batch_momentum, aef_dim=a.naef, noise_std_dev=a.noise_std_dev) if a.mode == 'train': tmp = model.train(a) _run.info['predictions'] = tmp _run.info['mean_predictions'] = np.mean(tmp, axis=0) elif a.mode == 'valid': tmp = model.validate(a) _run.info['predictions'] = tmp _run.info['mean_predictions'] = np.mean(tmp, axis=0) else: model.test(a)
def get_model(nNeuron, nLayer, in_dim, out_dim): nNeuron = nNeuron + in_dim mid1 = int((nNeuron + in_dim) / 2) mid2 = int((nNeuron + out_dim) / 2) mid = [nNeuron] * (nLayer - 2) return ae.AutoEncoder( encode_dim=[in_dim, mid1, *mid, mid2, out_dim], decode_dim=[out_dim, mid2, mid1, in_dim], activation=nn.LeakyReLU)
def main(args): device = torch.device( 'cuda' if torch.cuda.is_available() and not args.cpu else 'cpu') print('Using %s device.' % device) world_size = int( os.environ[args.env_size]) if args.env_size in os.environ else 1 local_rank = int( os.environ[args.env_rank]) if args.env_rank in os.environ else 0 if local_rank == 0: print(vars(args)) if world_size > 1: print('rank: {}/{}'.format(local_rank + 1, world_size)) torch.distributed.init_process_group(backend='gloo', init_method='file://%s' % args.tmpname, rank=local_rank, world_size=world_size) train_dataloader, test_dataloader = load_dataset(args, device, world_size) net = AutoEncoder(input_dim=1900, nlayers=args.nlayers, latent=100).to(device) if world_size > 1: net = torch.nn.parallel.DistributedDataParallel(net) if args.modelfile: net.load_state_dict(torch.load(args.modelfile)) # define our optimizer and loss function optimizer = torch.optim.Adam(net.parameters(), lr=args.lr, weight_decay=args.weight_decay) loss_func = nn.MSELoss(reduction='mean') test_losses = [] for epoch in range(args.epochs): epoch_start = timeit.default_timer() train(train_dataloader, net, optimizer, loss_func, epoch) test_loss = test(test_dataloader, net, loss_func) print(' %5.2f sec' % (timeit.default_timer() - epoch_start)) test_losses.append(test_loss) if test_loss <= min(test_losses): torch.save(net.state_dict(), 'model/%5.3f.pth' % min(test_losses))
def main(): args = parser.parse_args() filenames = glob.glob(os.path.join(args.dataset, '*.tfrecord')) dataset = dataset_builder.build(filenames) model = AutoEncoder() optimizer = tf.keras.optimizers.Adam() loss_object = tf.keras.losses.Huber( reduction=tf.keras.losses.Reduction.NONE) train_loss = tf.keras.metrics.Mean(name='train_loss') ckpt = tf.train.Checkpoint(model=model) ckpt_mgr = tf.train.CheckpointManager(ckpt, args.logdir, max_to_keep=5) def train_step(origin, speaker_one_hot, target): with tf.GradientTape() as tape: convert = model(origin, speaker_one_hot) loss = tf.reduce_mean(loss_object(convert, target)) gradients = tape.gradient(loss, model.trainable_variables) optimizer.apply_gradients(zip(gradients, model.trainable_variables)) train_loss(loss) EPOCHS = 1000 for epoch in range(EPOCHS): # Reset the metrics at the start of the next epoch train_loss.reset_states() for feature in dataset: speaker_id = feature['speaker_id'] speaker_one_hot = tf.one_hot(speaker_id, 2) mel = feature['mel'] mel = tf.expand_dims(mel, axis=-1) train_step(mel, speaker_one_hot, mel) break ckpt_mgr.save() template = 'Epoch {}, Loss: {}' print(template.format(epoch + 1, train_loss.result())) ckpt_mgr.save()
def decode(model_name, encoding, decoder_name): """Synthesize audio from an array of embeddings. Args: encodings: Numpy array with shape [batch_size, time, dim]. save_paths: Iterable of output file names. checkpoint_path: Location of the pretrained model. [model.ckpt-200000] samples_per_save: Save files after every amount of generated samples. """ decoder_path = './decoding/' model_path = './restore/' # Create directory for encoding if os.path.exists(decoder_path) is False: os.makedirs(decoder_path) net = AutoEncoder() net = load_model(net,model_path,model_name) cuda_available = torch.cuda.is_available() if cuda_available is True: net = net.cuda() net.eval() # Load Encoding encoding_ndarray = np.load(encoding) encoding = torch.from_numpy(encoding_ndarray).float() encoding = Variable(encoding, volatile=True) generated_spec = net.decoder(encoding) generated_spec = generated_spec.data.cpu().numpy() generated_spec = np.squeeze(generated_spec) dec_name = decoder_path + decoder_name np.save(dec_name , generated_spec)
def encode(spec): model_path = './restore/' model_name = 'Autoencoder1.model' net = AutoEncoder() net = load_model(net, model_path, model_name) cuda_available = torch.cuda.is_available() if cuda_available is True: net = net.cuda() net.eval() spec = torch.FloatTensor(torch.from_numpy(spec)) spec = torch.unsqueeze(spec, 0) spec = Variable(spec, volatile=True).contiguous() if cuda_available is True: spec = spec.cuda() # Pass input audio to net forward pass out = net.encoder(spec) out = out.data.cpu().numpy() out = np.squeeze(out) return out
def encode(model_name, piece, encoding_name): model_path = './restore/' encoding_path = './encoding/' # Create directory for encoding if os.path.exists(encoding_path) is False: os.makedirs(encoding_path) net = AutoEncoder() net = load_model(net,model_path,model_name) cuda_available = torch.cuda.is_available() if cuda_available is True: net = net.cuda() net.eval() # Load audio for encoding piece = audio.load_wav(piece) spec = audio.spectrogram(piece).astype(np.float32) spec = torch.from_numpy(spec.T) spec = torch.FloatTensor(spec) spec = torch.unsqueeze(spec, 0) spec = Variable(spec, volatile=True).contiguous() if cuda_available is True: spec = spec.cuda() # Pass input audio to net forward pass encoding = net.encoder(spec) encoding = encoding.data.cpu().numpy() #encoding = np.squeeze(encoding) encoding_ndarray = encoding_path + encoding_name+ '.npy' np.save(encoding_ndarray, encoding)
if __name__ == '__main__': opts = get_args() if not os.path.exists(opts.outDir): os.makedirs(opts.outDir) # mnist digits dataset transforms_ = torchvision.transforms.ToTensor() traindata = torchvision.datasets.MNIST(root=opts.root, train=True, transform=transforms_, download=True) trainset = DataLoader(dataset=traindata, batch_size=opts.bs, shuffle=False) # getting the structure of your autoencoder autoencoder = AutoEncoder(opts.nz) # useful when you have a GPU (link pytorch to CUDA) if torch.cuda.is_available(): autoencoder.cuda() print("test the data") test_dataset(traindata=traindata, trainset=trainset) print("begin training ...") train(autoencoder=autoencoder, outDir=opts.outDir, trainset=trainset, traindata=traindata) print("visualize latent space representation") visualize(autoencoder=autoencoder, outDir=opts.outDir, trainset=trainset, traindata=traindata)
def main(eval_args): # ensures that weight initializations are all the same logging = utils.Logger(eval_args.local_rank, eval_args.save) # load a checkpoint logging.info('loading the model at:') logging.info(eval_args.checkpoint) checkpoint = torch.load(eval_args.checkpoint, map_location='cpu') args = checkpoint['args'] if not hasattr(args, 'ada_groups'): logging.info('old model, no ada groups was found.') args.ada_groups = False if not hasattr(args, 'min_groups_per_scale'): logging.info('old model, no min_groups_per_scale was found.') args.min_groups_per_scale = 1 if not hasattr(args, 'num_mixture_dec'): logging.info('old model, no num_mixture_dec was found.') args.num_mixture_dec = 10 logging.info('loaded the model at epoch %d', checkpoint['epoch']) arch_instance = utils.get_arch_cells(args.arch_instance) model = AutoEncoder(args, None, arch_instance) # Loading is not strict because of self.weight_normalized in Conv2D class in neural_operations. This variable # is only used for computing the spectral normalization and it is safe not to load it. Some of our earlier models # did not have this variable. model.load_state_dict(checkpoint['state_dict'], strict=False) model = model.cuda() logging.info('args = %s', args) logging.info('num conv layers: %d', len(model.all_conv_layers)) logging.info('param size = %fM ', utils.count_parameters_in_M(model)) if eval_args.eval_mode == 'evaluate': # load train valid queue args.data = eval_args.data train_queue, valid_queue, num_classes = datasets.get_loaders(args) if eval_args.eval_on_train: logging.info('Using the training data for eval.') valid_queue = train_queue # get number of bits num_output = utils.num_output(args.dataset) bpd_coeff = 1. / np.log(2.) / num_output valid_neg_log_p, valid_nelbo = test( valid_queue, model, num_samples=eval_args.num_iw_samples, args=args, logging=logging) logging.info('final valid nelbo %f', valid_nelbo) logging.info('final valid neg log p %f', valid_neg_log_p) logging.info('final valid nelbo in bpd %f', valid_nelbo * bpd_coeff) logging.info('final valid neg log p in bpd %f', valid_neg_log_p * bpd_coeff) else: bn_eval_mode = not eval_args.readjust_bn num_samples = 16 with torch.no_grad(): n = int(np.floor(np.sqrt(num_samples))) set_bn(model, bn_eval_mode, num_samples=36, t=eval_args.temp, iter=500) for ind in range(10): # sampling is repeated. torch.cuda.synchronize() start = time() with autocast(): logits = model.sample(num_samples, eval_args.temp) output = model.decoder_output(logits) output_img = output.mean if isinstance(output, torch.distributions.bernoulli.Bernoulli) \ else output.sample() torch.cuda.synchronize() end = time() output_tiled = utils.tile_image(output_img, n).cpu().numpy().transpose( 1, 2, 0) logging.info('sampling time per batch: %0.3f sec', (end - start)) output_tiled = np.asarray(output_tiled * 255, dtype=np.uint8) output_tiled = np.squeeze(output_tiled) plt.imshow(output_tiled) plt.show()
torch.backends.cudnn.deterministic = DEBUG torch.backends.cudnn.benchmark = not DEBUG print(f'Using device {DEVICE}') # Data loaders train_ds = PPFDataset(**TRAIN_DS_ARGS) train_dl = DataLoader(train_ds, **TRAIN_DL_ARGS) val_ds = PPFDataset(**VAL_DS_ARGS) val_dl = DataLoader(val_ds, **VAL_DL_ARGS) print('Training set: {} Validation set: {}\n'.format( train_ds.__len__(), val_ds.__len__() )) # Model model = AutoEncoder(NUM_PTS_PER_PATCH) model.apply(init_weights).to(DEVICE) loss_func = ChamferLoss() optimizer = Adam(model.parameters(), LR) scheduler = OneCycleLR( optimizer, MAX_LR, total_steps=len(train_dl)*TRAINER_ARGS.num_epochs ) Path(TRAINER_ARGS.checkpoint_path).parent.mkdir(parents=True, exist_ok=True) # Training train(model, loss_func, optimizer, scheduler, (train_dl, val_dl), TRAINER_ARGS)
class trainAE(object): def __init__(self, path, k, lr=0.01, batch_size=1, loss='bce', n_epochs=500): ''' Arguments: path : path to training data k : hidde unit's dimension lr : learning rate batch_size : batch_size for training, currently set to 1 loss : loss function (bce, rmse) to train AutoEncoder n_epochs : number of epochs for training ''' self.AE = AutoEncoder(path, k) # Definne the autoencoder model #self.AE.model() self.AE.model_batch() self.epochs = n_epochs def sigmoid(self, x): return 1.0 / (1.0 + np.exp(-x)) def train(self): T = self.AE.T # COnverting to csr format for indexing T = T.tocsr() #pdb.set_trace() nonzero_indices = T.nonzero() for epoch in xrange(self.epochs): print("Running epoch %d" % (epoch)) for i in np.unique(nonzero_indices[0]): # get indices of observed values from the user 'i' 's vector indices = T[i, :].nonzero()[1] #print indices #indices = indices.reshape(indices.shape[0],) # Get correspoding ratings ratings = T[i, indices].toarray() #print ratings ratings = ratings.reshape(ratings.shape[1], ) # Convert inputs to theano datatype indices = indices.astype(np.int32) ratings = ratings.astype(np.int32) #pdb.set_trace() loss = self.AE.ae(indices, ratings) print("Loss at epoch %d is %f" % (epoch, loss)) print("RMSE after one epoch is %f" % (self.RMSE())) # Batch training method def train_batch(self, batch_size): T = self.AE.T T = T.tocsr() nonzero_indices = T.nonzero() #pdb.set_trace() n_users = len(np.unique(nonzero_indices[0])) indices = np.unique(nonzero_indices[0]) for epoch in xrange(self.epochs): l = [] for ind, i in enumerate(xrange(0, n_users, batch_size)): # CHECK : SEEMS BUGGY. #------------------------ #ratings = T[indices[i:(i + batch_size)], :].toarray().astype(np.float32) ratings = T[indices[i:(i + batch_size)], :].toarray().astype( np.float32) #------------------------ #print ratings #pdb.set_trace() loss = self.AE.ae_batch(ratings) #loss = self.AE.debug(ratings) #print loss #pdb.set_trace() l.append(loss) m = np.mean(np.array(l)) print("mean Loss for epoch %d batch %d is %f" % (epoch, ind, m)) #rmse = self.RMSE_sparse() rmse = self.RMSE() print("RMSE after one epoch is %f" % (rmse)) f.write(str(rmse) + '\n') def RMSE(self): W, V, b, mu = self.AE.get_params() print("testing process starts") test = self.AE.test_ind[:5000] rat = [] pred = [] rmse = 0 #try: # Rt = self.AE.T[test[:,0]].todense() # p = expit(np.dot(W, expit(np.dot(V, Rt) + mu)) + b) # P = p[:, test[:,1]] # pdb.set_trace() #except: # print "Exceptoin in batch test" # pdb.set_trace() for i, j in test: Rt = self.AE.T[i, :].todense() Rt1 = np.zeros(Rt.shape[1]) Rt1[:] = Rt[:] #pdb.set_trace() p = (np.dot(W, expit(np.dot(V, Rt1) + mu)) + b) #p = np.tanh(np.dot(W, np.tanh(np.dot(V, Rt1) + mu)) + b) p = p[j] pred.append(p) rat.append(self.AE.t[i, j]) try: rat = np.array(rat) pred = np.array(pred) rmse = np.sqrt(np.mean((pred - rat)**2)) except: print "exception" pdb.set_trace() np.save('test', test) np.save('W', W) np.save('V', V) np.save('mu', mu) np.save('b', b) return rmse #pdb.set_trace() def RMSE_sparse(self): W, V, b, mu = self.AE.get_params() #pdb.set_trace() mu = mu.reshape(100, 1) print("testing process starts") test = self.AE.test_ind rat = [] pred = [] rmse = 0 for i, j in test: #pdb.set_trace() Rt = np.array(self.AE.T[i, :].todense().tolist()).astype( np.float16) #pdb.set_trace() ind = np.where(Rt > 0)[1] #pdb.set_trace() Rt = Rt.T #pdb.set_trace() temp1 = V[:, ind] temp2 = Rt[ind] temp = expit(np.dot(temp1, temp2) + mu) #del temp1 #del temp2 p = expit(np.dot(W, temp) + b) #pdb.set_trace() p = p[0, j] pred.append(p) rat.append(self.AE.t[i, j]) #gc.collect() try: rat = np.array(rat) pred = np.array(pred) rmse = np.sqrt(np.mean((pred - rat)**2)) except: print "exception" pdb.set_trace() np.save('test', test) np.save('W', W) np.save('V', V) np.save('mu', mu) np.save('b', b) return rmse
from torch.utils.data import TensorDataset, DataLoader from model import AutoEncoder import torch.nn as nn import torch import numpy as np from tqdm import tqdm model = AutoEncoder().cuda() checkpoint = torch.load("autoencoder.pth") criterion = nn.L1Loss() model.load_state_dict(checkpoint) model.eval() with open("../agent_code/rule_based_agent_auto_encode/states.npy", "rb") as f: data = np.load(f, allow_pickle=False) data = data[:2_500_000] data_t = torch.Tensor(data) data_t = data_t.cuda() dataset = TensorDataset(data_t) train_len = int(0.7 * len(dataset)) valid_len = (len(dataset) - train_len) // 2 test_len = len(dataset) - train_len - valid_len train_dataset, valid_dataset, test_dataset = torch.utils.data.random_split( dataset, [train_len, valid_len, test_len], generator=torch.Generator().manual_seed(42)) batch_size = 32
help='Field of view angle in radians') parser.add_argument('-l', '--light', nargs='+', type=float, default=[.5, 30], help='Light intensity from poseur') parser.add_argument('-oc', '--num_occlusions', default=2, type=int, help='Number of occlusions') parser.add_argument('-sa', '--save_path', default=os.path.join( os.path.expanduser('~'), '.keras/paz/models'), type=str, help='Path for writing model weights and logs') args = parser.parse_args() # setting optimizer and compiling model latent_dimension = args.latent_dimension model = AutoEncoder((args.image_size, args.image_size, 3), latent_dimension) optimizer = Adam(args.learning_rate, amsgrad=True) model.compile(optimizer, args.loss, metrics=['mse']) model.summary() # setting scene renderer = SingleView( args.obj_path, (args.image_size, args.image_size), args.y_fov, args.depth, args.sphere, args.roll, args.translation, args.shift, args.light) # creating sequencer image_paths = glob.glob(os.path.join(args.images_directory, '*.png')) processor = DomainRandomization( renderer, args.image_size, image_paths, args.num_occlusions) sequence = GeneratingSequence(processor, args.batch_size, args.steps_per_epoch)
def main(args): # ensures that weight initializations are all the same torch.manual_seed(args.seed) np.random.seed(args.seed) torch.cuda.manual_seed(args.seed) torch.cuda.manual_seed_all(args.seed) logging = utils.Logger(args.global_rank, args.save) writer = utils.Writer(args.global_rank, args.save) # Get data loaders. train_queue, valid_queue, num_classes, _ = datasets.get_loaders(args) args.num_total_iter = len(train_queue) * args.epochs warmup_iters = len(train_queue) * args.warmup_epochs swa_start = len(train_queue) * (args.epochs - 1) arch_instance = utils.get_arch_cells(args.arch_instance) model = AutoEncoder(args, writer, arch_instance) model = model.cuda() logging.info('args = %s', args) logging.info('param size = %fM ', utils.count_parameters_in_M(model)) logging.info('groups per scale: %s, total_groups: %d', model.groups_per_scale, sum(model.groups_per_scale)) if args.fast_adamax: # Fast adamax has the same functionality as torch.optim.Adamax, except it is faster. cnn_optimizer = Adamax(model.parameters(), args.learning_rate, weight_decay=args.weight_decay, eps=1e-3) else: cnn_optimizer = torch.optim.Adamax(model.parameters(), args.learning_rate, weight_decay=args.weight_decay, eps=1e-3) cnn_scheduler = torch.optim.lr_scheduler.CosineAnnealingLR( cnn_optimizer, float(args.epochs - args.warmup_epochs - 1), eta_min=args.learning_rate_min) grad_scalar = GradScaler(2**10) num_output = utils.num_output(args.dataset, args) bpd_coeff = 1. / np.log(2.) / num_output # if load checkpoint_file = os.path.join(args.save, 'checkpoint.pt') if args.cont_training: logging.info('loading the model.') checkpoint = torch.load(checkpoint_file, map_location='cpu') init_epoch = checkpoint['epoch'] model.load_state_dict(checkpoint['state_dict']) model = model.cuda() cnn_optimizer.load_state_dict(checkpoint['optimizer']) grad_scalar.load_state_dict(checkpoint['grad_scalar']) cnn_scheduler.load_state_dict(checkpoint['scheduler']) global_step = checkpoint['global_step'] else: global_step, init_epoch = 0, 0 for epoch in range(init_epoch, args.epochs): # update lrs. if args.distributed: train_queue.sampler.set_epoch(global_step + args.seed) valid_queue.sampler.set_epoch(0) if epoch > args.warmup_epochs: cnn_scheduler.step() # Logging. logging.info('epoch %d', epoch) # Training. train_nelbo, global_step = train(train_queue, model, cnn_optimizer, grad_scalar, global_step, warmup_iters, writer, logging) logging.info('train_nelbo %f', train_nelbo) writer.add_scalar('train/nelbo', train_nelbo, global_step) model.eval() # generate samples less frequently eval_freq = 1 if args.epochs <= 50 else 20 if epoch % eval_freq == 0 or epoch == (args.epochs - 1): with torch.no_grad(): num_samples = 16 n = int(np.floor(np.sqrt(num_samples))) for t in [0.7, 0.8, 0.9, 1.0]: logits = model.sample(num_samples, t) output = model.decoder_output(logits) output_img = output.mean if isinstance( output, torch.distributions.bernoulli.Bernoulli ) else output.sample(t) output_tiled = utils.tile_image(output_img, n) writer.add_image('generated_%0.1f' % t, output_tiled, global_step) valid_neg_log_p, valid_nelbo = test(valid_queue, model, num_samples=10, args=args, logging=logging) logging.info('valid_nelbo %f', valid_nelbo) logging.info('valid neg log p %f', valid_neg_log_p) logging.info('valid bpd elbo %f', valid_nelbo * bpd_coeff) logging.info('valid bpd log p %f', valid_neg_log_p * bpd_coeff) writer.add_scalar('val/neg_log_p', valid_neg_log_p, epoch) writer.add_scalar('val/nelbo', valid_nelbo, epoch) writer.add_scalar('val/bpd_log_p', valid_neg_log_p * bpd_coeff, epoch) writer.add_scalar('val/bpd_elbo', valid_nelbo * bpd_coeff, epoch) save_freq = int(np.ceil(args.epochs / 100)) if epoch % save_freq == 0 or epoch == (args.epochs - 1): if args.global_rank == 0: logging.info('saving the model.') torch.save( { 'epoch': epoch + 1, 'state_dict': model.state_dict(), 'optimizer': cnn_optimizer.state_dict(), 'global_step': global_step, 'args': args, 'arch_instance': arch_instance, 'scheduler': cnn_scheduler.state_dict(), 'grad_scalar': grad_scalar.state_dict() }, checkpoint_file) # Final validation valid_neg_log_p, valid_nelbo = test(valid_queue, model, num_samples=1000, args=args, logging=logging) logging.info('final valid nelbo %f', valid_nelbo) logging.info('final valid neg log p %f', valid_neg_log_p) writer.add_scalar('val/neg_log_p', valid_neg_log_p, epoch + 1) writer.add_scalar('val/nelbo', valid_nelbo, epoch + 1) writer.add_scalar('val/bpd_log_p', valid_neg_log_p * bpd_coeff, epoch + 1) writer.add_scalar('val/bpd_elbo', valid_nelbo * bpd_coeff, epoch + 1) writer.close()
class trainAE(object): def __init__(self, path, k, lr= 0.1, batch_size=1, loss='bce', n_epochs=50): ''' Arguments: path : path to training data k : hidde unit's dimension lr : learning rate batch_size : batch_size for training, currently set to 1 loss : loss function (bce, rmse) to train AutoEncoder n_epochs : number of epochs for training ''' self.AE = AutoEncoder(path, k) # Definne the autoencoder model #self.AE.model() self.AE.model_batch() self.epochs = n_epochs def sigmoid(self, x): return 1.0 / (1.0 + np.exp(-x)) def train(self): T = self.AE.T # COnverting to csr format for indexing T = T.tocsr() #pdb.set_trace() nonzero_indices = T.nonzero() for epoch in xrange(self.epochs): print("Running epoch %d"%(epoch)) for i in np.unique(nonzero_indices[0]): # get indices of observed values from the user 'i' 's vector indices = T[i, :].nonzero()[1] #print indices #indices = indices.reshape(indices.shape[0],) # Get correspoding ratings ratings = T[i, indices].toarray() #print ratings ratings = ratings.reshape(ratings.shape[1],) # Convert inputs to theano datatype indices = indices.astype(np.int32) ratings = ratings.astype(np.int32) #pdb.set_trace() loss = self.AE.ae(indices, ratings) print("Loss at epoch %d is %f"%(epoch, loss)) print("RMSE after one epoch is %f"%(self.RMSE())) # Batch training method def train_batch(self, batch_size): T = self.AE.T T = T.tocsr() nonzero_indices = T.nonzero() #pdb.set_trace() n_users = len(np.unique(nonzero_indices[0])) indices = np.unique(nonzero_indices[0]) for epoch in xrange(self.epochs): for ind, i in enumerate(xrange(0, n_users, batch_size)): # CHECK : SEEMS BUGGY. #------------------------ #ratings = T[indices[i:(i + batch_size)], :].toarray().astype(np.float32) ratings = T[indices[i:(i + batch_size)], :].toarray().astype(np.float32) #------------------------ #print ratings #pdb.set_trace() loss = self.AE.ae_batch(ratings) #loss = self.AE.debug(ratings) #print loss #pdb.set_trace() print("Loss for epoch %d batch %d is %f"%(epoch, ind, loss)) print("RMSE after one epoch is %f"%(self.RMSE())) def RMSE(self): W, V, b, mu = self.AE.get_params() print("testing process starts") test = self.AE.test_ind rat = [] pred = [] rmse = 0 for i,j in test: Rt = self.AE.T[i, :].todense() Rt1 = np.zeros(Rt.shape[1] +1) Rt1[:6541] = Rt #pdb.set_trace() p = expit(np.dot(W, expit(np.dot(V, Rt1) + mu)) + b) #p = np.tanh(np.dot(W, np.tanh(np.dot(V, Rt1) + mu)) + b) p = p[j] pred.append(p) rat.append(self.AE.t[i, j]) try: rat = np.array(rat) pred = np.array(pred) rmse = np.sqrt(np.mean((pred-rat)**2)) except: print "exception" pdb.set_trace() np.save('test', test) np.save('W', W) np.save('V', V) np.save('mu', mu) np.save('b', b) return rmse #pdb.set_trace() def RMSE_sparse(self): W, V, mu, b = self.AE.get_params() print("testing process starts") test = self.AE.test_ind rat = [] pred = [] for i,j in test: Rt = self.AE.T[i, :].todense() #pdb.set_trace() ind = self.AE.T[i, :].nonzero()[1] #pdb.set_trace() Rt = Rt.T temp = np.tanh(np.dot(V[:, ind], Rt[ind]) + mu.reshape(100,1)) p = np.tanh(np.dot(W, temp) + b) p = p[j] pred.append(p) rat.append(self.AE.t[i, j]) try: rat = np.array(rat) pred = np.array(pred) print np.sqrt(np.mean((pred-rat)*2)) except: print "exception" pdb.set_trace() np.save('test', test) np.save('W', W) np.save('V', V) np.save('mu', mu) np.save('b', b) pdb.set_trace()