def plot_img_reconstructions(
    root_path: str,
    name_of_best_exp: str,
    path_to_plot: str,
    baseline: bool = False,
    epoch: int = 49999,
):
    dataset = MNISTDataset()
    ae = AutoEncoder(30, False, False, 0.001, "test")
    ae.load_state_dict(
        torch.load(
            os.path.join(
                root_path,
                name_of_best_exp,
                f"params/step_{epoch}/rank_0/{'A' if not baseline else 'baseline'}.pt",
            ),
            map_location=torch.device("cpu"),
        )
    )
    digits: torch.Tensor = dataset.sample(50)

    _, axes = plt.subplots(
        nrows=10,
        ncols=10,
        figsize=(10, 8),
        gridspec_kw=dict(
            wspace=0.0, hspace=0.0, top=0.95, bottom=0.05, left=0.17, right=0.845
        ),
    )
    axes = axes.reshape(50, 2)

    for digit, ax_column in zip(digits, axes):
        ax_column[0].imshow(digit.squeeze().detach())
        ax_column[0].set_axis_off()
        rec = ae(digit.reshape(1, 1, 28, 28))
        ax_column[1].imshow(rec.squeeze().detach())
        ax_column[1].set_axis_off()
    plt.show()
    exit(1)
    plt_path = f"plots/{path_to_plot}/reconstructions_baseline_{baseline}"
    plt.savefig(plt_path + ".pdf")
    plt.savefig(plt_path + ".svg")
    plt.close()
Ejemplo n.º 2
0
def main(args):
    model = AutoEncoder()
    use_gpu = torch.cuda.is_available()
    if use_gpu:
        print('cuda is available!')
        model.cuda()

    weight_file = args.path_weight_file

    model = AutoEncoder()
    model.load_state_dict(
        torch.load(weight_file, map_location=lambda storage, loc: storage))

    test_dataset = dataloader('dogs_cats', 'test')
    test_loader = torch.utils.data.DataLoader(
        test_dataset, batch_size=args.batch_size, shuffle=False)

    images, _ = iter(test_loader).next()
    images = Variable(images, volatile=True)

    imshow(torchvision.utils.make_grid(images.data[:25], nrow=5))
    outputs = model(images)
    imshow(torchvision.utils.make_grid(outputs.data[:25], nrow=5))
Ejemplo n.º 3
0
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))
Ejemplo n.º 4
0
try:
    pre_train_path = args.load_weight_dir
    pretrained_dict = torch.load(pre_train_path)  #load pre train model
    pretrained_dict = {
        k: v
        for k, v in pretrained_dict.items() if k in model_dict
    }  #load the layer only same with the target model
    model_dict.update(pretrained_dict)
    print('===================================')
    print('load pre_train weight successfully')
    print('===================================')
except:
    print('===================================')
    print('       random init the weight      ')
    print('===================================')
autoencoder.load_state_dict(model_dict)
autoencoder.cuda()
autoencoder.train()
'''opt setting'''
optimizer = torch.optim.Adam(
    autoencoder.parameters(),
    lr=args.learning_rate)  # optimize all cnn parameters
loss_func = nn.MSELoss()
'''folder for saving  weight and loss history'''
save_path = args.save_weight_dir
'''training code'''
for epoch in range(EPOCH):
    loss_iter = 0
    for step, (x, b_label) in enumerate(train_data):

        x_in = torch.tensor(x).cuda()
Ejemplo n.º 5
0
def evaluate_experiment(path_dti: str, path_mtm: str, data_x: Tensor,
                        data_y: Tensor) -> pd.DataFrame:
    result_df_container = []

    for path_dti in glob.glob(f"{path_dti}/*"):
        for i in range(2):
            ae = AutoEncoder(30,
                             False,
                             False,
                             0.001,
                             "bruh",
                             pre_latent_dim=49)
            repres = ae.encode(data_x).detach()
            results = evaluate_representations(repres, data_y, 10, (30, ),
                                               args, "Random features")
            results["Agent"] = i
            result_df_container.append(results)

        for i in range(2):
            ae = AutoEncoder(30,
                             False,
                             False,
                             0.001,
                             "bruh",
                             pre_latent_dim=49)
            ae.load_state_dict(
                torch.load(path_dti +
                           ("/baseline.pt" if i == 0 else "/baseline_2.pt")))
            repres = ae.encode(data_x).detach()
            results = evaluate_representations(repres, data_y, 10, (30, ),
                                               args, "AE")
            results["Agent"] = i
            result_df_container.append(results)

        for i in range(3):
            ae = AutoEncoder(30,
                             False,
                             False,
                             0.001,
                             "bruh",
                             pre_latent_dim=49)
            ae.load_state_dict(
                torch.load(path_dti + f"/{string.ascii_uppercase[i]}.pt"))
            repres = ae.encode(data_x).detach()
            results = evaluate_representations(repres, data_y, 10, (30, ),
                                               args, "DTI")
            results["Agent"] = i
            result_df_container.append(results)

    for path_mtm in glob.glob(f"{path_mtm}/*"):
        for i in range(3):
            ae = AutoEncoder(30,
                             False,
                             False,
                             0.001,
                             "bruh",
                             pre_latent_dim=49)
            ae.load_state_dict(
                torch.load(path_mtm + f"/{string.ascii_uppercase[i]}.pt"))
            repres = ae.encode(data_x).detach()
            results = evaluate_representations(repres, data_y, 10, (30, ),
                                               args, "AE+MTM")
            results["Agent"] = i
            result_df_container.append(results)

        results = pd.concat(result_df_container)
        return results
Ejemplo n.º 6
0
def train(block=200,
          data_name="bookcorpus",
          downsample=-1,
          dropout_rate=0.2,
          history=None,
          device="cuda:0",
          params=None):
    # version 1 - tfidf as feature
    if data_name == "bookcorpus":
        if history is None:
            x_train, y_train = load_tfidf("train",
                                          block,
                                          verbose=True,
                                          redo=False)
            x_test, y_test = load_tfidf("test",
                                        block,
                                        verbose=True,
                                        redo=False)
            x_valid, y_valid = load_tfidf("valid",
                                          block,
                                          verbose=True,
                                          redo=False)
        else:
            x_train, y_train = load_tfidf_long("train",
                                               block,
                                               verbose=True,
                                               redo=False,
                                               history=history)
            x_test, y_test = load_tfidf_long("test",
                                             block,
                                             verbose=True,
                                             redo=False,
                                             history=history)
            x_valid, y_valid = load_tfidf_long("valid",
                                               block,
                                               verbose=True,
                                               redo=False,
                                               history=history)

    elif data_name == "coda19":
        x_train, y_train = coda_load_tfidf("train",
                                           block,
                                           verbose=True,
                                           redo=False)
        x_test, y_test = coda_load_tfidf("test",
                                         block,
                                         verbose=True,
                                         redo=False)
        x_valid, y_valid = coda_load_tfidf("valid",
                                           block,
                                           verbose=True,
                                           redo=False)
    else:
        print("Not supported yet")
        quit()

    if downsample != -1:
        random_index = np.random.RandomState(5516).permutation(
            x_train.shape[0])[:downsample]
        x_train, y_train = x_train[random_index], y_train[random_index]

    # parameter setting
    vocab_size = x_train.shape[1]
    output_size = y_train.shape[1]
    hidden_size = 512 if params is None else params.hidden_size
    epoch_num = 2000 if params is None else params.epoch_num
    batch_size = 512 if params is None else params.batch_size
    layer_num = 5 if params is None else params.layer_num
    learning_rate = 1e-4 if params is None else params.learning_rate
    early_stop_epoch = 20 if params is None else params.early_stop
    device = device

    if downsample == -1:
        note = f"cosine - auto2 - {dropout_rate}"
    else:
        note = f"cosine - auto2 - {dropout_rate} - downsample"

    # build dataset
    training_dataset = TFIDF_Dataset(x_train, y_train)
    training = data.DataLoader(training_dataset,
                               batch_size=batch_size,
                               shuffle=True,
                               num_workers=2)

    testing_dataset = TFIDF_Dataset(x_test, y_test)
    testing = data.DataLoader(testing_dataset,
                              batch_size=batch_size,
                              shuffle=False,
                              num_workers=2)

    valid_dataset = TFIDF_Dataset(x_valid, y_valid)
    valid = data.DataLoader(valid_dataset,
                            batch_size=batch_size,
                            shuffle=False,
                            num_workers=2)

    # build model
    model = AutoEncoder(
        vocab_size=vocab_size,
        hidden_size=hidden_size,
        output_size=output_size,
        dropout_rate=dropout_rate,
        device=device,
        layer_num=layer_num,
    ).to(device)
    optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)
    loss_function = lambda y_pred, y_batch: 1 - F.cosine_similarity(
        y_pred, y_batch).mean()

    # first evaluation
    evaluate(model, valid, loss_function=loss_function)

    best_epoch = 0
    best_cosine = 0
    best_model = copy.deepcopy(model.state_dict())
    stopper = EarlyStop(mode="max", history=early_stop_epoch)

    # train model
    for epoch in range(1, epoch_num + 1):
        # train
        model.train()
        total_loss = 0
        total_count = np.ceil(x_train.shape[0] / batch_size)
        total_cosine = 0

        for count, (x_batch, y_batch) in enumerate(training, 1):
            x_batch = x_batch.squeeze()
            y_batch = y_batch.squeeze()

            x_batch = x_batch.to(device)
            y_batch = y_batch.to(device)

            optimizer.zero_grad()
            y_pred = model(x_batch)
            loss = loss_function(y_pred, y_batch)
            loss.backward()
            optimizer.step()
            total_loss += loss.item()

            cosine = F.cosine_similarity(y_batch, y_pred)
            total_cosine += cosine.mean().item()

            print(
                "\x1b[2K\rEpoch: {} / {} [{:.2f}%] Loss: {:.4f} Cosine: {:.4f}"
                .format(epoch, epoch_num, 100.0 * count / total_count,
                        total_loss / count, total_cosine / count),
                end="")
        print()

        # valid
        if epoch % 1 == 0 or epoch == epoch_num:
            cosine, _ = evaluate(model, valid, loss_function=loss_function)
            if cosine > best_cosine:
                best_model = copy.deepcopy(model.state_dict())
                best_epoch = epoch
                best_cosine = cosine

            # check early stopping
            if stopper.check(cosine):
                print("Early Stopping at Epoch = ", epoch)
                break

    # load best model & test & save
    print("loading model from epoch {}".format(best_epoch))
    torch.save(
        best_model,
        os.path.join(model_dir, data_name,
                     "{}_autoencoder_{}.pt".format(note, block)))
    model.load_state_dict(best_model)
    cosine, y_pred = evaluate(model,
                              testing,
                              device=device,
                              loss_function=loss_function)
    print("testing cosine:", cosine)

    # config filename
    if history is None:
        filename = os.path.join(result_dir, f"{data_name}_dl_baseline.json")
        prediction_filename = os.path.join(
            predict_dir, "bookcorpus",
            f"block{block}_autoencoder_{note.replace(' ', '')}.h5")
    else:
        filename = os.path.join(result_dir,
                                f"history_exp_{data_name}_dl_baseline.json")
        prediction_filename = os.path.join(
            predict_dir, "bookcorpus",
            f"history_block{block}_autoencoder_{note.replace(' ', '')}.h5")

    print_tfidf_metric(
        {
            "cosine": float(cosine),
            "block": block,
            "model": "autoencoder",
            "note": "clean - autoencoder - tfidf - deep - {}".format(note)
        },
        filename=filename)

    save_prediction(prediction_filename, y_pred)
Ejemplo n.º 7
0
                    type    = int,
                    help    = 'GPU device ids (CUDA_VISIBLE_DEVICES)')

global args
args = parser.parse_args()

'''set the training gpu''' 
os.environ["CUDA_VISIBLE_DEVICES"] = str(args.gpuid)

'''load_data'''
Load_data = load_data()
test_data,test_gt = Load_data.test()

'''init model'''
autoencoder = AutoEncoder()
autoencoder.load_state_dict(torch.load(args.load_weight_dir)) #load pre-train
autoencoder.cuda()
autoencoder.eval()

loss_func = nn.L1Loss() 
loss = 0

with torch.no_grad(): #it can save the memory,prevent it allocated,we dont need to keep the grad during the evualation
    for index in range(0,test_data.size()[0],50):
        x_in = torch.tensor(test_data[index:index+49,:,:,:], dtype=torch.float32).cuda() 
        decoded = autoencoder(x_in)
        loss = loss+loss_func(decoded, x_in)   # L1 loss 
        '''pick some sample to check vision performance'''
        plt.title('autoencoder input')
        plt.imshow(x_in[0,0,:,:].data.cpu().numpy(), cmap='gray')
        plt.show()         
Ejemplo n.º 8
0
def to_img(img):
    '''
    Re-normalise the image
    '''
    mean = [114, 108, 100]
    std = [46, 52, 51]
    img = img * std + mean
    return img


CUDA = torch.cuda.is_available()

model = AutoEncoder()

if args.load_model == 1:
    model.load_state_dict(torch.load('conv_autoencoder_weight.pt'))
    optimizer = torch.optim.Adam(model.parameters(),
                                 lr=args.learning_rate,
                                 weight_decay=1e-5)
    optimizer.load_state_dict(torch.load('conv_autoencoder_optimizer.pt'))

if CUDA:
    model = AutoEncoder().cuda()
criterion = nn.MSELoss()
if args.load_model == 0:
    optimizer = torch.optim.Adam(model.parameters(),
                                 lr=learning_rate,
                                 weight_decay=1e-5)
print('Running')
done = False
for epoch in (range(num_epochs)):
if data_name == "eeg":
    X_raw, y_raw = load_eeg_raw("{}data/raw/".format(base_path))
elif data_name == "syn":
    X_raw, y_raw = load_one_syn_raw("{}data/raw/".format(base_path), data_idx)
elif data_name == "har":
    X_raw, y_raw = load_har_raw("{}data/raw/".format(base_path))
else:
    assert(False)
print("y_raw: {}".format(y_raw.shape))
X_sliding = sliding_window(X_raw, args["window_size"])
X_variable = Variable(torch.Tensor(X_sliding), requires_grad=False).to(device)
auto_encoder = AutoEncoder(input_dim=X_sliding.shape[1],
                           hidden_sizes=args["hidden_sizes"],
                           latent_dim=args["latent_dim"],
                           ).to(device)
auto_encoder.load_state_dict(torch.load(checkpoint_path, map_location=device))
z = auto_encoder.encode(X_variable).detach().numpy()
print(z.shape)


def find_peaks(z):
    dists = np.sqrt(np.sum(np.diff(z, axis=0) ** 2, axis=1))
    print(dists.shape)

    def mean(xs):
        return sum(xs) * 1. / len(xs)

    # inspect width, i.e. for t we inspect [t-d, t+d]
    d = 50
    indices = [
        i

data = []
print('Reading Images')
for i in tqdm(range(len(os.listdir(image_filepath)[0:3]))):
    img_name = os.listdir(image_filepath)[i]
    img_name = image_filepath + img_name
    img = plt.imread(img_name)
    data.append(img)

CUDA = torch.cuda.is_available()

model = AutoEncoder()
model.eval()

model.load_state_dict(
    torch.load('conv_autoencoder_weight.pt', map_location='cpu'))
# optimizer = torch.optim.Adam(model.parameters(), lr=args.learning_rate, weight_decay =1e-5)
# optimizer.load_state_dict(torch.load('conv_autoencoder_optimizer.pt'))

if CUDA:
    model = AutoEncoder().cuda()

for epoch in (range(num_epochs)):
    dataset = random.sample(data, batch_size)
    for image in dataset:
        image, orig_im, dim, w, h = prep_image(image, 256)
        image = Variable(torch.tensor(image))
        img = image.view(1, image.shape[2], image.shape[0], image.shape[1])
        if CUDA:
            img = img.cuda()
        # ===================forward=====================