def train_AE(X_train, X_test, encoder_size, decoder_size, epochs, lr): model = autoencoder.AutoEncoder(encoder_size, decoder_size).to(device) autoencoder.train(model, dataset=X_train, num_epochs=epochs, batch_size=batch_size, lr=lr) # Calculate training error AE_reconstruction = model(X_train) training_error = np.mean( ((X_train - AE_reconstruction[0])**2).cpu().detach().numpy()) # Calculate test error AE_reconstruction = model(X_test) test_error = np.mean( ((X_test - AE_reconstruction[0])**2).cpu().detach().numpy()) return model, training_error, test_error
def main(): parser = argparse.ArgumentParser() parser.add_argument( "--dataset_ratio", type=float, default=1, help="the purcentage of data used in the dataset (default: 1)") parser.add_argument("--image_size", type=int, default=512, help="size of the input image (default: 512)") parser.add_argument("--depth", type=int, default=6, help="depth of the autoencoder (default: 6)") parser.add_argument( "--num_block", type=int, default=3, help="number of blocks of the autoencoder (default: 3)") parser.add_argument("--epoch", type=int, default=1, help="number of epoch (default: 1)") parser.add_argument("--batch", type=int, default=100, help="number of batch (default: 100)") parser.add_argument("--valpct", type=float, default=0.2, help="proportion of test data (default: 0.2)") parser.add_argument("--num_threads", type=int, default=1, help="number of thread used (default: 1)") parser.add_argument("--create_csv", type=bool, default=False, help="create or not csv file (default: False)") parser.add_argument("--log", default=False, action='store_true', help="Write log or not (default: False)") parser.add_argument("--l2_reg", type=int, default=0.001, help="L2 regularisation (default: 0.001)") args = parser.parse_args() # if args.create_csv: # g_csv.generate_csv(DATA_PATH + CSV_NAME, args.num_var, # args.num_const, args.num_prob) valid_ratio = args.valpct # Going to use 80%/20% split for train/valid data_transforms = transforms.Compose([ToTensor(), Normalize()]) full_dataset = ImageLoader(csv_file_path=LABEL_FILE_PATH, image_directory=IMAGE_FOLDER_PATH, mask_directory=MASK_FOLDER_PATH, dataset_size=int(args.dataset_ratio * DATASET_SIZE), image_size=args.image_size, transform=data_transforms) nb_train = int((1.0 - valid_ratio) * len(full_dataset)) nb_test = len(full_dataset) - nb_train print("Size of full data set: ", len(full_dataset)) print("Size of training data: ", nb_train) print("Size of testing data: ", nb_test) train_dataset, test_dataset = torch.utils.data.dataset.random_split( full_dataset, [nb_train, nb_test]) train_loader = DataLoader(dataset=train_dataset, batch_size=args.batch, shuffle=True, num_workers=args.num_threads) test_loader = DataLoader(dataset=test_dataset, batch_size=args.batch, shuffle=True, num_workers=args.num_threads) # TODO params # num_param = args.num_var + args.num_const + (args.num_var*args.num_const) model = AutoEncoder(num_block=args.num_block, depth=args.depth) # print("Network architechture:\n", model) use_gpu = torch.cuda.is_available() if use_gpu: device = torch.device('cuda') else: device = torch.device('cpu') model.to(device) # f_loss = nn.BCEWithLogitsLoss() f_loss = loss.Custom_loss() # define optimizer optimizer = torch.optim.Adam(model.parameters(), weight_decay=args.l2_reg) # Make run directory run_name = "run-" LogManager = lw.LogManager(LOG_DIR, run_name) run_dir_path, num_run = LogManager.generate_unique_dir() # setup model checkpoint path_model_check_point = run_dir_path + MODEL_DIR if not os.path.exists(path_model_check_point): os.mkdir(path_model_check_point) model_checkpoint = ModelCheckpoint(path_model_check_point + BEST_MODELE, model) # top_logdir = LOG_DIR + FC1 # if not os.path.exists(top_logdir): # os.mkdir(top_logdir) # model_checkpoint = ModelCheckpoint(top_logdir + BEST_MODELE, model) if args.log: print("Writing log") # generate unique folder for new run tensorboard_writer = SummaryWriter(log_dir=run_dir_path, filename_suffix=".log") LogManager.set_tensorboard_writer(tensorboard_writer) LogManager.summary_writer(model, optimizer) # write short description of the run run_desc = "Epoch{}".format(args.epoch) log_file_path = LOG_DIR + run_desc + "Run{}".format(num_run) + ".log" # LogManager.summary_writer(model, optimizer) # write short description of the run # run_desc = "Epoch{}Reg{}Var{}Const{}CLoss{}Dlayer{}Alpha{}".format( # args.num_epoch, # args.l2_reg, # args.num_var, # args.num_const, # args.custom_loss, # args.num_deep_layer, # args.alpha) # log_file_path = LOG_DIR + "Run{}".format(num_run) + run_desc + ".log" # log_file_path = lw.generate_unique_logpath(LOG_DIR, "Linear") with tqdm(total=args.epoch) as pbar: for t in range(args.epoch): pbar.update(1) pbar.set_description("Epoch {}".format(t)) # print(DIEZ + "Epoch Number: {}".format(t) + DIEZ) train_loss, train_acc = train(model, train_loader, f_loss, optimizer, device, LogManager) progress(train_loss, train_acc) # time.sleep(0.5) val_loss, val_acc = test(model, test_loader, f_loss, device, log_manager=LogManager) print(" Validation : Loss : {:.4f}, Acc : {:.4f}".format( val_loss, val_acc)) model_checkpoint.update(val_loss) # lw.write_log(log_file_path, val_acc, val_loss, train_acc, train_loss) if args.log: tensorboard_writer.add_scalars("Loss/", { 'train_loss': train_loss, 'val_loss': val_loss }, t) # tensorboard_writer.add_scalar(METRICS + 'train_loss', train_loss, t) # tensorboard_writer.add_scalar(METRICS + 'train_acc', train_acc, t) # tensorboard_writer.add_scalar(METRICS + 'val_loss', val_loss, t) # tensorboard_writer.add_scalar(METRICS + 'val_acc', val_acc, t) LogManager.write_log(log_file_path, val_acc, val_loss, train_acc, train_loss) model.load_state_dict(torch.load(path_model_check_point + BEST_MODELE)) print(DIEZ + " Final Test " + DIEZ) _, _ = test(model, train_loader, f_loss, device, final_test=True, log_manager=LogManager, txt="training") test_loss, test_acc = test(model, test_loader, f_loss, device, log_manager=LogManager, final_test=True) print(" Test : Loss : {:.4f}, Acc : {:.4f}".format( test_loss, test_acc))
# Build matrix keep_data = [] for row in data: keep_row = {} for j in keep_header_js: h = headers[j] if j < len(row) and row[j] == '' and args.zero: keep_row[h] = 0.0 elif j < len(row) and row[j]: keep_row[h] = float(row[j]) if keep_row: keep_data.append(keep_row) keep_headers = [headers[j] for j in keep_header_js] # Compress json output json.encoder.FLOAT_REPR = lambda f: ("%.4f" % f) json.encoder.c_make_encoder = None print 'training' for model in autoencoder.train(keep_headers, keep_data, bins=args.bins, n_hidden_units=args.n_hidden_units, n_hidden_layers=args.n_hidden_layers): js = 'var data = %s;' % json.dumps(model) f = open(args.output, 'w') f.write(js) f.close()
weight_lambda = 1e-5 training_dataset = tf.data.Dataset.from_tensor_slices(bops).batch(batch_size) autoencoder = ae.Autoencoder(original_dim=original_dim, code_dim=code_dim, hidden_dim=hidden_dim, weight_lambda=weight_lambda) #opt = tf.optimizers.SGD(learning_rate=learning_rate, momentum=momentum) opt = tf.optimizers.Adam(learning_rate=learning_rate) with open("loss_%s" % input_filename, "w") as loss_file: real_step = 0 for epoch in range(epochs): for step, batch_features in enumerate(training_dataset): ae.train(ae.loss, autoencoder, opt, batch_features) loss_value = ae.loss(autoencoder, batch_features) real_step += 1 print("%d %f" % (real_step, loss_value), file=loss_file) encoded_bops = autoencoder.encoder(tf.constant(bops)) # FIND THE GAUSSIAN MIXTURE MODEL THAT BEST APPROXIMATES THE DATA THROUGH A BIC CRITERIUM from sklearn import mixture import itertools lowest_bic = np.infty bic = [] n_components_range = range(1, 10) for n_components in n_components_range:
training_data, validation_data, test_data = mnist_loader.load_data_wrapper() # only get x inputs training_data, test_data = np.array(list(training_data))[:, 0], np.array( list(test_data))[:, 0] training_data, test_data = np.array( list(map(lambda x: np.squeeze(x), training_data))), np.array( list(map(lambda x: np.squeeze(x), test_data))) print("processed data") # [ 784, 400, L50, 400, 784] autoencoder = autoencoder.Autoencoder([784, 400], [400, 784], 50) print("finished building autoencoder") autoencoder.train(training_data, test_data) print("finished training!") '''= # Creates filename and prepares network properties(performance and parameters) for saving # name follows naming scheme: inputsize_layer1size_layer2size_...outputsize file_name = str(net.sizes).replace("[","").replace("]","").replace(" ","").replace(",","_")+'.pkl' new_net_properties = [net.performance, net.weights, net.biases, ] Saves network properties (learned parameters and performance). If the shape had been previously trained, it will override previous saved file if new performance is better. If it is a new shape, it will save to a new file try: with open('best_networks/{0}'.format(file_name), 'rb') as f: old_net_properties = pickle.load(f)
import image_preprocessing as img import numpy as np import matplotlib.pyplot as plt import autoencoder as A X = img.blur_images(100) Y = img.pre_process(100) X = X / X.max() Y = Y / Y.max() ip = np.prod(np.array(X[0].shape)) print(X.shape) print(Y.shape) model = A.train(X, Y, ip, 1) predictions = model.predict(X) plt.imshow(predictions[0].reshape(100, 100), cmap='gray') plt.show()