Exemplo n.º 1
0
stack_size = 2  #args.stack_size
rfield_size = int(np.sum([2**i for i in range(0, layer_size)] * stack_size))

data_dir_g1 = '/home/jn1664/DGM/DiscoGAN/data_piano'
data_dir_g2 = '/home/jn1664/DGM/DiscoGAN/data_glock'
seed_dir_g1 = data_dir_g1 + '/A_Grand.wav'
seed_dir_g2 = data_dir_g2 + '/A_Glockenspiel.wav'
model_file_path = 'model/'
source_name = 'piano_grand'
target_name = 'glockenspiel'

sample_size = 10000
train_steps = 1000000
##################################################################################

train_loader = datasets.DataLoader(data_dir_g1, data_dir_g2, rfield_size,
                                   sample_size)

g1 = WaveNet(in_out_size, res_size, stack_size, layer_size)  #P->G
#g2 = WaveNet(in_out_size, res_size, stack_size, layer_size)#G->P
if torch.cuda.is_available():
    g1.cuda()
    #g2.cuda()
CEloss = nn.CrossEntropyLoss()

d1 = Discriminator(in_out_size, 1)  #G
d2 = Discriminator(in_out_size, 1)  #P
if torch.cuda.is_available():
    d1.cuda()
    d2.cuda()

criterion = nn.BCELoss()
Exemplo n.º 2
0
def train_fc(learning_rate=1e-4,
             num_epochs=50,
             decay_every=50,
             gamma=0.1,
             batch_size=256,
             num_batches=100,
             display_info=True,
             hidden_width=1536):
    TRAIN_SET_SIZE = batch_size * num_batches

    file_name = "data/cifar-10-batches-py/data_batch_1"
    data = f.unpickle(file_name)

    ims = f.open_data_ims(50000)
    x, y = f.seperate_xy_ims(ims[0:TRAIN_SET_SIZE])

    # figure our device here
    device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
    if torch.cuda.is_available():
        torch.set_default_tensor_type('torch.cuda.FloatTensor')
    print("Using device:{}, cuda:{}, pytorch:{}".format(
        device, torch.version.cuda, torch.__version__))

    m = fc.FC_1(1536, hidden_width, 1536)
    if torch.cuda.is_available():
        m = m.cuda()  # transfer model to cuda as needed
    lossFunction = nn.L1Loss()
    optimizer = torch.optim.Adam(m.parameters(), lr=learning_rate)
    scheduler = torch.optim.lr_scheduler.StepLR(optimizer,
                                                step_size=decay_every,
                                                gamma=gamma)

    x = torch.tensor(x, dtype=torch.float)
    y = torch.tensor(y, dtype=torch.float)

    x, y = x / 256.0, y / 256.0  # regularize

    # introduce a dataset class
    train_dataset = datasets.VectorizedDataset(x, y)
    train_loader = datasets.DataLoader(dataset=train_dataset,
                                       batch_size=batch_size,
                                       shuffle=True,
                                       num_workers=0)

    # remember loss graph
    losses = np.zeros(num_epochs, dtype=np.float)

    print("Started training.")
    train_start_time = time.time()

    for epoch in range(num_epochs):
        if epoch != 0 and epoch % int(num_epochs / 10) == 0:
            print("{}% done! loss:{}".format(epoch / num_epochs * 100.0,
                                             losses[epoch - 1]))

        epoch_losses = 0.0

        # perform MINI-BATCH grad_descent here
        for i, data in enumerate(train_loader, 0):
            x_batch, y_batch = data

            y_pred = m.forward(x_batch)
            loss = lossFunction(y_pred, y_batch)
            epoch_losses += loss

            optimizer.zero_grad()
            loss.backward()
            optimizer.step()

        losses[epoch] = epoch_losses / num_batches

    print("Training took {} seconds!".format(time.time() - train_start_time))

    if display_info:
        # try to write this first layer to a video
        v = m.linear1.weight[:, 0:512].detach().cpu().numpy()
        num_frames = v.shape[0]
        v = v.reshape(num_frames, 16, 32)
        v = np.absolute(v)
        v = v * (1.0 / v.max()) * 255.0  # normailize 0->255
        f.save_video(v)

        # print loss graph
        print(losses)
        plt.plot(losses)
        plt.show()

        # print results
        y_pred = m.forward(x)
        x, y_pred = x.cpu().detach().numpy(), y_pred.detach().cpu().numpy()
        # x, y_pred = np.asarray(x.data), np.asarray(y_pred.data)
        x, y_pred = x * 256.0, y_pred * 256.0
        x, y_pred = x[0:50], y_pred[0:50]
        fuse_xy_pred = f.combine_xy_ims(x, y_pred)
        big_im = f.comb_ims(fuse_xy_pred, 32, 32)
        f.show_im_std(big_im)

        # print image of the last FIRST layer weights
        s = m.linear1.weight[0, 0:512].detach().cpu().numpy()
        s = s.reshape(16, 32)
        s = np.absolute(s)
        s *= (1.0 / s.max())  # normalize 0->1
        plt.imshow(s, interpolation='nearest')
        plt.show()

    return losses[num_epochs - 1]
def train_fc(learning_rate=1e-4,
             num_epochs=50,
             decay_every=50,
             gamma=0.1,
             batch_size=1024 * 20,
             num_batches=20,
             display_info=True,
             hidden_width=512,
             num_x_rows=3,
             num_y_rows=1,
             x_train_filename="data/custom/x_train.npy",
             y_train_filename="data/custom/y_train.npy",
             shuffle_data=True):
    TRAIN_SET_SIZE = batch_size * num_batches
    ROW_WIDTH = 32
    COL_HEIGHT = 32
    COLOR_CHANNELS = 3

    try:
        print("Attempting to load x, y data from disk.")
        x = np.load(x_train_filename)
        y = np.load(y_train_filename)
    except:
        # create the dataset that we will be training on
        print("x,y data disk load failure. Generating from scratch...")
        ims = f.open_data_ims(50000)
        x, y = f.generate_custom_data(num_x_rows, num_y_rows,
                                      ims[0:TRAIN_SET_SIZE], 32, 32)
        np.save(x_train_filename, x)
        np.save(y_train_filename, y)
    assert x.shape[0] == y.shape[0]  # ensure x,y are of equal lengths

    # recombined = f.combine_xy_ims(x,y)
    # big_im = f.comb_ims(recombined, im_width=32, im_height=num_x_rows+num_y_rows)
    # f.show_im_std(big_im)

    # now begin setting up the model
    input_width = ROW_WIDTH * num_x_rows * COLOR_CHANNELS
    output_width = ROW_WIDTH * num_y_rows * COLOR_CHANNELS

    # set up devices
    device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
    if torch.cuda.is_available():
        torch.set_default_tensor_type('torch.cuda.FloatTensor')
    print("Using device:{}, cuda:{}, pytorch:{}".format(
        device, torch.version.cuda, torch.__version__))

    # set up model, optimizer, scheduler
    m = fc.FC_4(input_width, hidden_width, output_width)
    if torch.cuda.is_available():
        m = m.cuda()  # transfer model to cuda as needed
    lossFunction = nn.L1Loss()
    optimizer = torch.optim.Adam(m.parameters(), lr=learning_rate)
    scheduler = torch.optim.lr_scheduler.StepLR(optimizer,
                                                step_size=decay_every,
                                                gamma=gamma)

    # ensure x, y are of correct form
    x = torch.tensor(x, dtype=torch.float)
    y = torch.tensor(y, dtype=torch.float)
    x, y = x / 256.0, y / 256.0  # regularize

    print("Data set size:{}".format(x.shape[0]))

    train_dataset = datasets.VectorizedDataset(x, y)
    train_loader = datasets.DataLoader(dataset=train_dataset,
                                       batch_size=batch_size,
                                       shuffle=shuffle_data,
                                       num_workers=0)

    # remember loss graph
    losses = np.zeros(num_epochs, dtype=np.float)

    print("Started training.")
    train_start_time = time.time()

    for epoch in range(num_epochs):
        if num_epochs >= 10 and epoch != 0 and epoch % int(
                num_epochs / 10) == 0:
            print("{}% done! loss:{}".format(epoch / num_epochs * 100.0,
                                             losses[epoch - 1]))

        epoch_losses = 0.0

        # perform MINI-BATCH grad_descent here
        for i, data in enumerate(train_loader, 0):
            x_batch, y_batch = data

            y_pred = m.forward(x_batch)
            loss = lossFunction(y_pred, y_batch)
            epoch_losses += loss

            optimizer.zero_grad()
            loss.backward()
            optimizer.step()

        losses[epoch] = epoch_losses / num_batches

    print("Training took {} seconds!".format(time.time() - train_start_time))

    # our model is now trained. now let us attempt to reconstruct samples from our train set
    VAL_SET_SIZE = 100
    ims = f.open_data_ims(10000)[0:VAL_SET_SIZE]

    # lets try to start predicting from halfway down
    # ims = np.reshape(ims,(VAL_SET_SIZE, ROW_WIDTH, COL_HEIGHT, COLOR_CHANNELS)) # to STD form
    YS = 16
    # ims = torch.tensor(ims, dtype=torch.float)
    for im in range(0, VAL_SET_SIZE):
        for i in range(YS, COL_HEIGHT + 1 - num_y_rows):
            ys = i  # y row start
            ye = ys + num_y_rows  # y row end (noninclusive)
            xs = ys - num_x_rows  # x row start
            xe = ys  # x row end (noninclusive)

            x = ims[im]
            x = f.extract_vec_rows(xs, xe, 32, x)
            x = torch.tensor(x, dtype=torch.float)

            y_pred = m.forward(x)
            y_pred = y_pred.cpu().detach().numpy()
            ims[im] = f.paste_over_rows(ys, ye, 32, ims[im], y_pred)

    ims = np.reshape(ims,
                     (VAL_SET_SIZE, ROW_WIDTH * COL_HEIGHT * COLOR_CHANNELS))
    big_im = f.comb_ims(ims, 32, 32)

    if display_info:
        title = "LR:{} decay:{} epochs:{} batch:{} num_batch:{} hidden:{}".format(
            learning_rate, decay_every, num_epochs, batch_size,
            train_dataset.__sizeof__(), hidden_width)
        f.show_im_std(big_im, title=title)
Exemplo n.º 4
0
def run_experiment(args, seed):
    if args.train is not None:
        train_dl = MTCDataLoader(args.train)
        if args.dev is None:
            train_data, dev_data = train_dl.train_test_split(
                test_size=args.devsize)
        else:
            dev_dl = MTCDataLoader(args.dev)
            train_data = list(train_dl.sequences())
            dev_data = list(dev_dl.sequences())
        if args.test is not None:
            test_dl = MTCDataLoader(args.test)
            test_data = list(test_dl.sequences())
        else:
            test_data = []
        print(
            f'Train: {len(train_data)}, Dev: {len(dev_data)}, Test: {len(test_data)}'
        )

    elif args.dataconf:
        if args.dataconf not in CONFIGS:
            print(f"Error. {args.dataconf} is not a valid data configuration.",
                  file=sys.stderr)
            print(f"Choose one of: {' '.join(DataConf.confs.keys())}",
                  file=sys.stderr)
            raise SystemExit
        train_data, dev_data, test_data = DataConf().getData(
            args.dataconf, args.devsize, args.testsize, args.cross_class_size)
        if args.test:
            print("Warning. Command line argument --test_data ignored.",
                  file=sys.stderr)

    if args.savetrain:
        MTCDataLoader.writeJSON(args.savetrain, train_data)
    if args.savedev:
        MTCDataLoader.writeJSON(args.savedev, dev_data)
    if args.savetest:
        MTCDataLoader.writeJSON(args.savetest, test_data)

    cat_encoders = [CategoricalEncoder(f) for f in args.categorical_features]
    scaler = (StandardScaler if args.scaler == 'zscore' else
              MinMaxScaler if args.scaler == 'minmax' else IdentityScaler)
    cont_encoders = [
        ContinuousEncoder(f, scaler=scaler) for f in args.continuous_features
    ]
    encoders = cat_encoders + cont_encoders

    train_selector, dev_selector = None, None
    if args.precompute_examples:
        if args.example_type == 'pairs':
            train_selector = samplers.PairSelector(
                pos_neg_ratio=args.pn_ratio, random_sample=args.sample_ratio)
            dev_selector = samplers.PairSelector(pos_neg_ratio=args.pn_ratio)
        else:
            train_selector = samplers.TripletSelector(
                sample_ratio=args.sample_ratio)
            dev_selector = samplers.TripletSelector(
                sample_ratio=args.sample_ratio)

    dataset_constructor = (datasets.Dataset if args.online_sampler else
                           datasets.DupletDataset if args.example_type
                           == 'pairs' else datasets.TripletDataset)

    train = dataset_constructor(train_data,
                                *encoders,
                                batch_size=args.batch_size,
                                selector=train_selector,
                                label='tunefamily',
                                train=True).fit()

    dev = dataset_constructor(dev_data,
                              *encoders,
                              batch_size=args.batch_size,
                              selector=dev_selector,
                              label='tunefamily',
                              train=False).fit()

    if args.precompute_examples:
        print(train_selector)
        print(dev_selector)

    collate_fn = datasets.collate_fn

    if args.balanced_batch_sampler:
        train_batch_sampler = datasets.BalancedBatchSampler(
            train.labels, n_classes=args.n_classes, n_samples=args.n_samples)
        dev_batch_sampler = datasets.BalancedBatchSampler(
            dev.labels, n_classes=args.n_classes, n_samples=args.n_samples)
        train_loader = DataLoader(train,
                                  batch_sampler=train_batch_sampler,
                                  collate_fn=collate_fn,
                                  num_workers=args.n_workers)
        dev_loader = DataLoader(dev,
                                batch_sampler=dev_batch_sampler,
                                collate_fn=collate_fn,
                                num_workers=args.n_workers)
    elif not args.precompute_examples:
        train_loader = DataLoader(train,
                                  batch_size=args.batch_size,
                                  shuffle=True,
                                  collate_fn=collate_fn,
                                  num_workers=args.n_workers)
        dev_loader = DataLoader(dev,
                                batch_size=args.batch_size,
                                collate_fn=collate_fn,
                                num_workers=args.n_workers)
    else:
        train_loader, dev_loader = datasets.DataLoader(
            train), datasets.DataLoader(dev)

    device = 'cuda' if args.cuda else 'cpu'

    emb_dims = [(encoder.size(), args.emb_dim) for encoder in cat_encoders]
    if args.model.lower() == 'rnn':
        network = models.RNN(emb_dims,
                             args.hid_dim,
                             cont_features=len(cont_encoders),
                             n_layers=args.n_layers,
                             cell=args.cell,
                             dropout=args.dropout,
                             bidirectional=args.bidirectional)
    elif args.model.lower() == 'cnn':
        network = models.CNN(emb_dims,
                             cont_features=len(cont_encoders),
                             kernel_sizes=tuple(args.kernel_sizes),
                             highway_layers=args.highway_layers,
                             out_channels=args.out_channels,
                             dropout=args.dropout)
    else:
        network = models.CNNRNN(emb_dims,
                                cont_features=len(cont_encoders),
                                kernel_sizes=tuple(args.kernel_sizes),
                                highway_layers=args.highway_layers,
                                out_channels=args.out_channels,
                                dropout=args.dropout,
                                cell=args.cell,
                                bidirectional=args.bidirectional,
                                n_layers=args.n_layers)

    if args.example_type == 'pairs':
        if not args.online_sampler:
            if args.loss == 'cosine':
                loss_fn = models.CosinePairLoss(weight=args.weight,
                                                margin=args.margin)
            else:
                loss_fn = models.EuclideanPairLoss(margin=args.margin)
            model = models.TwinNetwork(network,
                                       loss_fn).to(device)  # margin 0.16, 0.4
        else:
            if args.loss == 'cosine':
                loss_fn = models.OnlineCosinePairLoss(
                    samplers.HardNegativePairSelector(),
                    weight=args.weight,
                    margin=args.margin,
                    cutoff=args.cutoff_cosine)
            else:
                loss_fn = models.OnlineEuclideanPairLoss(
                    samplers.HardNegativePairSelector(), margin=args.margin)
            model = models.Network(network, loss_fn).to(device)
    else:
        if not args.online_sampler:
            if args.loss == 'cosine':
                loss_fn = models.CosineTripletLoss(margin=args.margin)
            else:
                loss_fn = models.EuclidianTripletLoss(margin=args.margin)
            model = models.TripletNetwork(network, loss_fn).to(device)
        else:
            if args.loss == 'cosine':
                loss_fn = models.OnlineCosineTripletLoss(
                    samplers.NegativeTripletSelector(
                        method=args.negative_pair_selector,
                        margin=args.margin),
                    margin=args.margin)
            else:
                loss_fn = models.OnlineEuclideanTripletLoss(
                    samplers.NegativeTripletSelector(
                        method=args.negative_pair_selector,
                        margin=args.margin),
                    margin=args.margin)
            model = models.Network(network, loss_fn).to(device)

    print(model)

    for embedding in model.network.embs:
        embedding.to(device)

    optimizer = torch.optim.Adam(model.parameters(), lr=args.lr)
    scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(
        optimizer,
        'min',
        verbose=True,
        patience=args.lr_scheduler,
        threshold=1e-4,
        cooldown=5)
    print(
        f'Number of parameters: {sum(p.nelement() for p in model.parameters())}'
    )

    try:
        early_stop = fit_model(train_loader,
                               dev_loader,
                               model,
                               optimizer,
                               scheduler,
                               args.epochs,
                               args.log_interval,
                               plot=False,
                               patience=args.patience,
                               early_stop_score=args.early_stop_score,
                               eval_metric=args.loss)
        best_score = early_stop['best']
        fails = early_stop['fails']
        best_params = early_stop['best_params']
        val_scores = early_stop['val_scores']
    except EarlyStopException as e:
        print("Early stopping training")
        best_score = e.best
        fails = e.fails
        best_params = e.best_params
        val_scores = e.val_scores

    model.load_state_dict(best_params)
    model.eval()
    # serialize model if necessary

    print("Best", args.early_stop_score, best_score)

    if args.save_encodings is not None and args.dev is not None:
        utils.save_encodings(dev, model, args.save_encodings, 'dev')

    if args.test is not None:
        train_label_set = list(set(train_loader.dataset.labels))
        test = dataset_constructor(test_data,
                                   *encoders,
                                   batch_size=args.batch_size,
                                   label='tunefamily',
                                   train=False).fit()
        test_scores = metrics.evaluate_ranking(model,
                                               test,
                                               train_label_set=train_label_set,
                                               metric=args.loss)
        message = 'Testing:\n'
        message += f'  silouhette: {test_scores["silhouette"]:.3f}\n'
        message += f'  MAP: {test_scores["MAP"]:.3f}\n'
        message += f'  MAP (seen): {test_scores["MAP seen labels"]:.3f}\n'
        message += f'  MAP (unseen): {test_scores["MAP unseen labels"]:.3f}\n'
        message += f'  Margin: {test_scores["margin_score"]:.3f}'
        print(message)

    with open(f'{args.results_dir}/{args.results_path}', 'a+') as f:
        f.write(
            json.dumps(
                {
                    "params": vars(args),
                    "dev_score": float(best_score),
                    "val_scores": val_scores,
                    "test_scores":
                    test_scores if args.test is not None else {},
                    "fails": fails,
                    "seed": seed,
                    "now": str(datetime.now())
                },
                cls=NumpyEncoder) + '\n')

    if args.save_encodings is not None and args.test is not None:
        utils.save_encodings(test, model, args.save_encodings, 'test')

    return model
def train(x, y, m, x_test=None, y_test=None,
          learning_rate = 1e-4,
          decay_every = 50,
          gamma = 0.1,
          batch_size = 256,
          num_epochs = 100):
    # figure our device here
    device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
    if torch.cuda.is_available(): torch.set_default_tensor_type('torch.cuda.FloatTensor')
    print("Using device:{}, cuda:{}, pytorch:{}".format(device, torch.version.cuda, torch.__version__))

    if torch.cuda.is_available(): m = m.cuda()  # transfer model to cuda as needed
    lossFunction = nn.L1Loss()
    # lossFunction = nn.MSELoss()
    optimizer = torch.optim.Adam(m.parameters(), lr=learning_rate)
    scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=decay_every, gamma=gamma)

    # prep data
    x = torch.tensor(x, dtype=torch.float)
    y = torch.tensor(y, dtype=torch.float)
    x_test = torch.tensor(x_test, dtype=torch.float)
    y_test = torch.tensor(y_test, dtype=torch.float)

    train_dataset = datasets.StdDataset(x, y)
    train_loader = datasets.DataLoader(dataset=train_dataset,
                                       batch_size=batch_size,
                                       shuffle=True,
                                       num_workers=0)

    # remember loss graph
    train_losses = np.zeros(num_epochs, dtype=np.float)
    test_losses = np.zeros(num_epochs, dtype=np.float)
    test_size = x_test.shape[0]

    print("Started training.")
    train_start_time = time.time()

    for epoch in range(num_epochs):
        if num_epochs >= 10 and epoch != 0 and epoch % int(num_epochs/10) == 0:
            print("{}% done! lossTrain:{} lossTest:{}".format(epoch / num_epochs * 100.0,
                                                              train_losses[epoch-1],
                                                              test_losses[epoch-1]))

        epoch_losses = 0.0

        # perform MINI-BATCH grad_descent here
        z = 0
        for i, data in enumerate(train_loader, 0):
            x_batch, y_batch = data

            y_pred = m.forward(x_batch)
            loss = lossFunction(y_pred, y_batch)
            epoch_losses += loss

            optimizer.zero_grad()
            loss.backward()
            optimizer.step()

            z += 1


        train_losses[epoch] = epoch_losses / z
        y_pred_test = m.forward(x_test)
        test_losses[epoch] = lossFunction(y_pred_test, y_test)

    print("Training took {} seconds!".format(time.time() - train_start_time))

    plt.plot(train_losses, label="Train Loss")
    plt.plot(test_losses, label="Test Loss")
    plt.legend()
    plt.show()

    return m