def set_model(args): mlp = model.AE(args.in_features, args.latent_size) pytorch_total_params = sum(p.numel() for p in mlp.parameters() if p.requires_grad) print("Number of traninable parameter: {0}".format(pytorch_total_params)) if args.loss == "mae": loss_function = torch.nn.functional.l1_loss #torch.nn.L1Loss() elif args.loss == "mse": loss_function = torch.nn.functional.mse_loss #torch.nn.MSELoss() elif args.loss == "mink": loss_function = minkowski_error elif args.loss == "huber": loss_function = torch.nn.functional.smooth_l1_loss optimizer, scheduler = configure_optimizers(mlp) if args.warm_start is not None: # Load the save model checkpoint = torch.load(args.locations['model_loc'] + '/' + args.warm_start, map_location=args.device) mlp.load_state_dict(checkpoint['model_state_dict']) mlp.to(args.device) optimizer.load_state_dict(checkpoint['optimizer_state_dict']) loss = checkpoint['training_loss'] epoch = checkpoint['epoch'] args.model_name = args.model_name.replace('.tar', '_{0}.tar'.format("cont")) else: mlp.to(args.device) print("Model's state_dict:") for param_tensor in mlp.state_dict(): print(param_tensor, "\t", mlp.state_dict()[param_tensor].size()) return mlp, loss_function, optimizer, scheduler
def set_model(model_file, args): # Define the Model # n_inputs,n_outputs=140,70 print(args.xvars) args.region = args.data_region # in_features = (args.nlevs*(len(args.xvars)-3)+3) in_features = (args.nlevs * (len(args.xvars))) print(in_features) # if not args.train_on_y2: # nb_classes = (args.nlevs*(len(args.yvars))) # # nb_classes = 1 #(args.nlevs*(len(args.yvars))) # else: # nb_classes = (args.nlevs*(len(args.yvars2))) nb_classes = (args.nlevs * (len(args.yvars))) # in_features, nb_classes=(args.nlevs*4+3),(args.nlevs*2) # hidden_size = 512 hidden_size = int(1. * in_features + nb_classes) # mlp = nn_model.MLP(in_features, nb_classes, args.nb_hidden_layers, hidden_size) # mlp = nn_model.MLPSkip(in_features, nb_classes, args.nb_hidden_layers, hidden_size) # mlp = nn_model.VAE(in_features) # mlp = nn_model.AE(in_features) mlp = nn_model.AE(args.in_features, args.latent_size) # mlp = nn_model.MLPDrop(in_features, nb_classes, args.nb_hidden_layers, hidden_size) # Load the save model print("Loading PyTorch model: {0}".format(model_file)) checkpoint = torch.load(model_file, map_location=torch.device('cpu')) mlp.load_state_dict(checkpoint['model_state_dict']) mlp.eval() print("Model's state_dict:") for param_tensor in mlp.state_dict(): print(param_tensor, "\t", mlp.state_dict()[param_tensor].size()) return mlp
from dataset import load_movielens_100k, load_movielens_1m import model import tensorflow as tf slim = tf.contrib.slim flags = tf.app.flags FLAGS = flags.FLAGS if __name__ == '__main__': with tf.Graph().as_default(): tf.logging.set_verbosity(tf.logging.INFO) trainset, testset = load_movielens_1m() X, y = model.convert_to_tensors(trainset) logits = model.AE(X) loss = model.loss(logits, y) train_op = model.train(loss) final_loss = slim.learning.train(train_op, logdir='trainAE.log', number_of_steps=1000, save_summaries_secs=5)
train_epochs = [5000, 8000, 50000, 50000, 50000, 50000] use_partial = [False, False, False, False, False, False] num_samples = [4000, 2000, 2000, 1000, 1000, 1000] sparsity_lambda = [3, 0, 3, 3, 3, 3] weight_decay = [0, 0, 0, 3e-4, 3e-4, 3e-4] epsilon = [-1, -1, -1, -1, -1, -1] filter_shapes = [[100, 1, 10, 10], [200, 100, 10, 10], [400, 200, 10, 10], [500, 400, 10, 10], [500, 500, 10, 10], [500, 500, 10, 10]] filter_strides = [2, 2, 1, 1, 1, 1] nets = [] for filter_shape in filter_shapes: nets.append(m.AE(np.prod(filter_shape[1:4]), filter_shape[0]).to(device)) spectro_dir = '../../Data/TIMIT/' root_dir = '../../intermDat/FHSAE' + str(iteration) + '/' #root_dir = '../../intermDat/FHSAE1/' #spectro_dir for first layer, Conv1-5 for layers 2-6 data_dirs = [spectro_dir] + [root_dir + 'Conv' + str(i) for i in range(1, 6)] data_funcs = [s.get_from_spectro] + [s.get for i in range(1, 6)] out_dirs = data_dirs[1:] + [root_dir + 'Conv6'] for i in range(len(nets)): print("===== NET #%d =====" % (i + 1)) if train_net[i]:
from torchvision.utils import save_image import torch.optim as optim import gc from torch.autograd import Variable import matplotlib.pyplot as plt import matplotlib.colors as colors import matplotlib as mpl from mpl_toolkits.mplot3d import Axes3D device = torch.device("cuda") image_size = 240 dimz = 50 # test codes model = models.AE(dimz, 3).to(device) # model load pretrained params # model.load_weights_from_file('../Disentangling/params/ClothV2_env_all_dimZ_100alpha_0.05')BlocksV3_env_all_dimZ_100alpha_0.03 model.load_weights_from_file('params/AE_Cloth_prev_human12_dimZ_50') mixedHR = uts.load_images_sequence( '../../Experiments/Dataset/Cloth_prev/mixedHR', 200, image_size) finalImages = uts.load_images_sequence( '../../Experiments/Dataset/Cloth_prev/final', 20, image_size) plot_r = pr.plot_task_map('../../Experiments/Dataset/Cloth_prev', None, device) plot_r.save_results_41(model, "AE_mixedHR", mixedHR, "mixedHR") plot_r.save_results_41(model, "AE_final_states", finalImages, "final states") # plot_r.save_results_4(model, 'save_test') # plot_r.compare_exp(model, 'another_test', 144) input("Press Enter to continue...")
for j in range(len(data_set)): #data_set level data_set_name = data_set[j] sample_size_t = sample_size[j] # load data image_folder = '../../Experiments/Dataset/' + task_name + '/' + data_set_name # train_loader, test_loader = uts.wrap_data_loader_images(image_folder, args.image_size, kwargs, 0, args.batch_size) train_loader = uts.wrap_data_loader_images(image_folder, args.image_size, kwargs, 0, args.batch_size) for m in range(len(dim_Zs)): # dim Z level dim_Zt = dim_Zs[m] torch.manual_seed( args.seed ) # manually set random seed for CPU random number generation. # construct model and optimizer model = models.AE(dim_Zt, args.input_channel).to(device) # initialize an optimizer optimizer = optim.Adam(model.parameters(), lr=5e-4) plot_r = pr.plot_task_map( '../../Experiments/Dataset/' + task_name, sample_sequence, device) train_losss = [] for epoch in range(1, args.epochs + 1): trainLoss = train(epoch, train_loader, model, optimizer, task_name, data_set_name, dim_Zt, plot_r) train_losss.append(trainLoss) # test(epoch, test_loader, model, alpha, beta, task_name, data_set_name, dim_Zt, plot_r) # save the model model.save("AE_" + task_name + "_" + data_set_name + "_dimZ_" + str(dim_Zt)) np.save(
from dataset import load_movielens_100k, load_movielens_1m import model import tensorflow as tf slim = tf.contrib.slim flags = tf.app.flags FLAGS = flags.FLAGS if __name__ == '__main__': with tf.Graph().as_default(): tf.logging.set_verbosity(tf.logging.INFO) trainset, testset = load_movielens_1m() X, y = model.convert_to_tensors(testset) logits = model.AE(X, is_training=False) names_to_values, names_to_updates = model.error_metrics(logits, y) logdir = 'trainAE.log' checkpoint_path = tf.train.latest_checkpoint(logdir) metric_values = slim.evaluation.evaluate_once( master='', checkpoint_path=checkpoint_path, logdir=logdir, eval_op=list(names_to_updates.values()), final_op=list(names_to_values.values())) names_to_values = dict(zip(list(names_to_values.keys()), metric_values)) for name in names_to_values: print('%s: %f' % (name, names_to_values[name]))
import model as m import samples as s import torch data_dir = 'D:/Data/TIMIT' samples = s.get_from_spectro(data_dir, num_samples=100, partial=False) #samples = s.PCA_whitening(samples) net = m.AE(100, 64) net.train_lbfgs(1, samples, 2000, './', decov=0, weight_decay=3e-4, sparsity_lambda=3) import matplotlib.pyplot as plt weights = list(net.parameters())[0].detach().to('cpu') fig = plt.figure(figsize=(40, 40)) for i in range(weights.shape[0]): filt = torch.reshape(weights[i, :], [10, 10]) # filt = filt / torch.sqrt(torch.sum(filt**2)) ax = fig.add_subplot(8, 8, i + 1) ax.imshow(filt, cmap='jet', interpolation='nearest') fig.savefig('./filtersTest64.png') #fig.savefig('../../LAE/filtersGreyWTAFacesDeconv' + str(patch_size) + 'in-batch' + str(batch_size) + '.png') plt.close(fig)
def scm_diff_enc(qmodel, tmodel, encmodelfile, datasetfile, qargs, targs, subdomain): """ SCM type run with two models one model for q prediction and nother for theta perdiction """ # aemodel = nn_model.AE(qargs.nlevs) aemodel = nn_model.AE(qargs.nlevs, qargs.latent_size) print(aemodel) print("Loading PyTorch model: {0}".format(encmodelfile)) checkpoint = torch.load(encmodelfile, map_location=torch.device('cpu')) for param_tensor in checkpoint['model_state_dict']: print(param_tensor, "\t", checkpoint['model_state_dict'][param_tensor].size()) # print(checkpoint['model_state_dict']) aemodel.load_state_dict(checkpoint['model_state_dict']) aemodel.eval() print("Model's state_dict:") for param_tensor in aemodel.state_dict(): print(param_tensor, "\t", aemodel.state_dict()[param_tensor].size()) qnn_data = data_io.Data_IO_validation( qargs.region, qargs.nlevs, datasetfile, qargs.locations['normaliser_loc'], xvars=qargs.xvars, yvars=qargs.yvars, # yvars2=qargs.yvars2, add_adv=False, no_norm=False, fmin=0, fmax=100) tnn_data = data_io.Data_IO_validation( targs.region, targs.nlevs, datasetfile, targs.locations['normaliser_loc'], xvars=targs.xvars, yvars=targs.yvars, # yvars2=targs.yvars2, add_adv=False, no_norm=False, fmin=0, fmax=100) # mplier = targs.xvar_multiplier # mplier[0] = 4. # targs.xvar_multiplier = mplier print("Q model x: ", qargs.xvars) print("Q model xmul", qargs.xvar_multiplier) print("Q Model y: ", qargs.yvars) print("Q Model ymul: ", qargs.yvar_multiplier) print("T Model x: ", targs.xvars) print("T Model xmul: ", targs.xvar_multiplier) print("T Model y: ", targs.yvars) print("T Model y: ", targs.yvar_multiplier) qx, qy, qy2, qxmean, qxstd, qymean, qystd, qymean2, qystd2 = qnn_data.get_data( ) tx, ty, ty2, txmean, txstd, tymean, tystd, tymean2, tystd2 = tnn_data.get_data( ) # model = set_model(args) qx_split = qnn_data.split_data(qx, xyz='x') qyt_split = qnn_data.split_data(qy, xyz='y') tx_split = tnn_data.split_data(tx, xyz='x') tyt_split = tnn_data.split_data(ty, xyz='y') # yt_inverse = nn_data._inverse_transform(y,ymean,ystd) # qyt_inverse = qy qyt_inverse = qnn_data._inverse_transform(qy, qymean, qystd) qyt_inverse_split = qnn_data.split_data(qyt_inverse, xyz='y') # tyt_inverse = ty tyt_inverse = tnn_data._inverse_transform(ty, tymean, tystd) tyt_inverse_split = tnn_data.split_data(tyt_inverse, xyz='y') qnext = qyt_split['qtot'][:-1] qnext_inv = qyt_inverse_split['qtot'][:-1] tnext = tyt_split['theta'][:-1] tnext_inv = tyt_inverse_split['theta'][:-1] tx_inv_split = tnn_data._inverse_transform_split(tx_split, txmean, txstd, xyz='x') qx_inv_split = qnn_data._inverse_transform_split(qx_split, qxmean, qxstd, xyz='x') # qx_inv_split = qnn_data.split_data(qx,xyz='x') # tx_inv_split = tnn_data.split_data(tx,xyz='x') qtot_inv = tx_inv_split['qtot'][:-1] theta_inv = qx_inv_split['theta'][:-1] qoutput = [] qoutput.append(qnext[0]) # qoutput.append(qnext[1]) toutput = [] toutput.append(tnext[0]) # toutput.append(tnext[1]) qdifflist = [] for v, m in zip(qargs.xvars, qargs.xvar_multiplier): qvdiff = (qx_split[v][1:] - qx_split[v][:-1]) * m qdifflist.append(qvdiff[:-1]) tdifflist = [] for v, m in zip(targs.xvars, targs.xvar_multiplier): tvdiff = (tx_split[v][1:] - tx_split[v][:-1]) * m tdifflist.append(tvdiff[:-1]) qxcopy = torch.cat(qdifflist, dim=1) txcopy = torch.cat(tdifflist, dim=1) qdiff_output = [] qdiff = tdifflist[0] qdiff_output.append((qdiff[0])) tdiff_output = [] tdiff = qdifflist[0] tdiff_output.append((tdiff[0])) # plotX(txcopy, qargs) # sys.exit(0) for t in range(1, len(qdiff) - 1, 1): typ_ = tmodel(txcopy[t - 1].reshape(1, -1)) if t > 0: qxcopy[t - 1, :qargs.nlevs] = typ_ # qxcopy[t-1,:qargs.nlevs] = qdiff_output[t-2] # qxcopy[t-1,qargs.nlevs:qargs.nlevs*2] = typ_ # true_enc = aemodel.encoder(txcopy[t-1][:qargs.nlevs]) qyp_enc = qmodel(qxcopy[t - 1].reshape(1, -1)) # print("True", true_enc[:]) # print("Model", qyp_enc[:]) qyp_ = aemodel.decoder(qyp_enc) qxcopy[t, :qargs.nlevs] = qyp_ if t > 0: txcopy[t, :targs.nlevs] = qyp_ if t > 0: qnew = (qoutput[t - 1] + qyp_[:qargs.nlevs] / torch.Tensor( qargs.yvar_multiplier)[:]) * 1.0 + (qoutput[t - 2] * 0.0) tnew = (toutput[t - 1] + typ_[:targs.nlevs] / torch.Tensor( targs.yvar_multiplier)[:]) * 1.0 + (toutput[t - 2] * 0.0) else: qnew = qoutput[t - 1] + qyp_[:qargs.nlevs] / torch.Tensor( qargs.yvar_multiplier)[:] tnew = toutput[t - 1] + typ_[:targs.nlevs] / torch.Tensor( targs.yvar_multiplier)[:] negative_values = torch.where(qnew < 0.) if len(negative_values[0]) > 0: qnew[negative_values] = 1.e-6 #output[t-1][negative_values] # yp_[negative_values] = diff_output[t-1][negative_values] # qoutput.append(qnew) qoutput.append(qnew.reshape(-1)) qdiff_output.append(qyp_) toutput.append(tnew.reshape(-1)) tdiff_output.append(typ_[:targs.nlevs]) # yp = torch.from_numpy(np.concatenate([qnext_ml, tnext_ml], axis=1)) qyp = torch.stack(qoutput) typ = torch.stack(toutput) qnext_ml_inv = qnn_data._inverse_transform(qyp, qymean, qystd) tnext_ml_inv = tnn_data._inverse_transform(typ, tymean, tystd) output = { 'qtot_next': qnext_inv.data.numpy(), 'qtot_next_ml': qnext_ml_inv.data.numpy(), 'qtot': qtot_inv.data.numpy(), 'theta': theta_inv.data.numpy(), 'theta_next': tnext_inv.data.numpy(), 'theta_next_ml': tnext_ml_inv.data.numpy() } hfilename = qargs.model_name.replace( '.tar', '_scm_2m_{0}.hdf5'.format(str(subdomain).zfill(3))) with h5py.File(hfilename, 'w') as hfile: for k, v in output.items(): hfile.create_dataset(k, data=v)