Exemplo n.º 1
0
 def load(self, shuffle=False):
     if not self.is_valid_path:
         return
     # Load train/val files ('data_batch_X' -> X={1,2,3,4,5})
     files = [
         os.path.join(self.data_dir, 'data_batch_%d' % (i + 1))
         for i in range(5)
     ]
     labels_train_val, raw_images_train_val = self._load_images_labels(
         files)
     # Load test file (test_batch)
     files = [os.path.join(self.data_dir, 'test_batch')]
     labels_test, raw_images_test = self._load_images_labels(files)
     # Split training and validation sets
     X_train, X_val, y_train, y_val = train_val_split(
         raw_images_train_val,
         labels_train_val,
         proportion_train=0.75,
         random_state=self.random_seed,
         shuffle=shuffle)
     train_set = {'labels': y_train, 'data': X_train}
     val_set = {'labels': y_val, 'data': X_val}
     # Create test set
     test_set = {'labels': labels_test, 'data': raw_images_test}
     # Now the dataset is lodaded, lets load the mean and std of the train/val data
     self._load_mean_std({"train": train_set, "validation": val_set})
     return {"train": train_set, "validation": val_set, "test": test_set}
Exemplo n.º 2
0
def data():
    data_root = '/nosave/lange/cu-ssp/data/netsurfp/'
    file_train = 'train_700'
    file_test = ['cb513_700', 'ts115_700', 'casp12_700']

    X_test = np.load(data_root + file_test[0] + '_input.npy')
    profiles = np.load(data_root + file_test[0] + '_hmm.npy')
    mean = np.mean(profiles)
    std = np.std(profiles)
    X_aug_test = (profiles - mean) / std
    X_test_aug = np.concatenate((X_test, X_aug_test), axis=2)
    y_test = np.load(data_root + file_test[0] + '_q8.npy')

    X_train = np.load(data_root + file_train + '_input.npy')
    profiles = np.load(data_root + file_train + '_hmm.npy')
    mean = np.mean(profiles)
    std = np.std(profiles)
    X_aug_train = (profiles - mean) / std
    X_train_aug = [X_train, X_aug_train]
    y_train = np.load(data_root + file_train + '_q8.npy')

    X_train_aug, y_train, X_val_aug, y_val = train_val_split(
        X_train_aug, y_train)

    return X_train_aug, y_train, X_val_aug, y_val, X_test_aug, y_test
def train():
    print("Starting!")
    model = VRNN(2, 256, 196, 1)
    model.to(device)
    optimizer = torch.optim.Adam(model.parameters(), lr=1e-3)

    p1_trajectories = preprocess(1, 32)[:, :, 0:2]
    p2_trajectories = preprocess(2, 32)[:, :, 0:2]
    trajectories = np.concatenate((p1_trajectories, p2_trajectories))
    np.random.shuffle(trajectories)  # randomize trajectories for train/val
    trajectories, _max, _min = normalize(trajectories)

    train, test = train_val_split(trajectories, val_split=0.15)
    train = torch.from_numpy(train).float().to(device)
    test = torch.from_numpy(test).float().to(device)

    __max = torch.tensor(_max).float().to(device)
    __min = torch.tensor(_min).float().to(device)

    print("Max", __max, "Min", __min)

    training_generator = data.DataLoader(train,
                                         batch_size=32,
                                         shuffle=True,
                                         drop_last=True)
    validation_generator = data.DataLoader(
        test, batch_size=32, shuffle=False,
        drop_last=True)  # for computing overall loss

    val_loss = float("inf")

    for epoch in range(1, 2001):
        for batch in training_generator:
            optimizer.zero_grad()
            kld_loss, mse_loss = model.forward(batch, __max, __min)
            loss = kld_loss + mse_loss
            loss.backward()
            optimizer.step()

        kld_loss = 0
        mse_loss = 0
        with torch.no_grad():
            for batch in validation_generator:
                _kld_loss, _mse_loss = model.forward(batch, __max, __min)
                kld_loss += _kld_loss
                mse_loss += _mse_loss
            kld_loss /= len(validation_generator)
            mse_loss /= len(validation_generator)
            print("Epoch {} kld_loss={}, mse_loss={}".format(
                epoch, kld_loss, mse_loss))

            if kld_loss + mse_loss < val_loss:
                print("Val_loss lowered from {} to {}. Saving model...".format(
                    val_loss, kld_loss + mse_loss))
                torch.save(model.state_dict(),
                           os.path.join(WEIGHTS_DIR, "32_p1-2_vrnn.pth"))
                val_loss = kld_loss + mse_loss
Exemplo n.º 4
0
def main():
    # downloading the dataset
    transform = torchvision.transforms.Compose([
        torchvision.transforms.ToTensor(),
        torchvision.transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
    ])

    trainset = torchvision.datasets.CIFAR10(root="/data",
                                            train=True,
                                            download=True,
                                            transform=transform)

    testset = torchvision.datasets.CIFAR10(root="/data",
                                           train=False,
                                           download=True,
                                           transform=transform)

    train_set, train_set_label, validation_set, validation_set_label = ut.train_val_split(
        trainset)
    flat_test, labels = ut.process_test_set(testset)

    print("The splits are: ")
    print(train_set.shape, train_set_label.shape)
    print(validation_set.shape)

    #training
    myNN = NeuralNetwork(OUTPUTS, IMAGESIZE, BATCHSIZE, LEARNINGRATE, LAYERS)

    # # starting the training
    myNN.train(train_set, train_set_label, validation_set,
               validation_set_label, ITERATIONS)
    myNN.save()
    myNN.clean()

    # testing
    myNNtest = NeuralNetwork(OUTPUTS, IMAGESIZE, BATCHSIZE, LEARNINGRATE,
                             LAYERS)
    myNNtest.load(wFile, bFile)
    print("validation accuracy for current",
          myNNtest.check(validation_set, validation_set_label))
    print("train accuracy for current",
          myNNtest.check(train_set, train_set_label))

    images, label, predictions = myNNtest.pred(flat_test.T, labels)

    classes = [
        'plane', 'car', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship',
        'truck'
    ]
    for x in range(10):
        ut.imshow(testset[x][0])
        print("Ground Truth: ", label[x], classes[label[x]])
        print("prediction: ", predictions[x], classes[predictions[x]])
def run_twolayer(out_file, title):
    net = TwoLayerNet(n_input=in_seq_len, n_hidden=10, n_output=out_seq_len)
    net.compile("adam", "mse")

    train_data, val_data = train_val_split(data_points, val_fraction=0.5)
    train_x, train_y = generate_sequences(train_data, in_seq_len, out_seq_len)
    val_x, val_y = generate_sequences(val_data, in_seq_len, out_seq_len)

    history = net.train(train_x, train_y, val_x, val_y, epochs=epochs, verbose=0)

    train_prediction = net.predict(train_x)
    val_prediction = net.predict(val_x)

    # plot results
    plot_prediction(data_points, train_prediction, val_prediction, in_seq_len,
                    out_file,
                    title)
    return history
def run_cnn(out_file, title):
    net = CNNPredictor(n_hidden, out_seq_len)
    net.compile("adam", "mse")

    train_data, val_data = train_val_split(data_points, val_fraction=0.5)
    train_x, train_y = generate_sequences(train_data, in_seq_len, out_seq_len)
    val_x, val_y = generate_sequences(val_data, in_seq_len, out_seq_len)

    train_x = train_x.reshape(*train_x.shape, 1)
    val_x = val_x.reshape(*val_x.shape, 1)

    history = net.train(train_x, train_y, val_x, val_y, epochs=epochs, verbose=0)

    train_prediction = net.predict(train_x)
    val_prediction = net.predict(val_x)

    # plot results
    plot_prediction(data_points, train_prediction, val_prediction, in_seq_len,
                    out_file, title)
    return history
Exemplo n.º 7
0
def default_loader():
    """ default loading Kuzushiji19 dataset using default transform and default validation set size
    Returns:
        train, val, test (three dataloaders)
    """
    transform = transforms.Compose([
        transforms.ToTensor(),
        transforms.Normalize([0.5],[0.5])
    ])

    datasets = []
    train_dataset = Kuzhushiji49Dataset(root, transform = transform, train=True)
    train_dataset, val_dataset = train_val_split(train_dataset)
    test_dataset = Kuzhushiji49Dataset(root, transform =transform, train=False)
    datasets = [train_dataset, val_dataset, test_dataset]

    dataloaders = []
    for dataset in datasets:
        dataloaders.append(DataLoader(dataset, shuffle=True, num_workers=2, batch_size=32))

    return dataloaders
def create_patches_dataset_iam(data_path,
                               new_path,
                               height=100,
                               width=100,
                               num_patches=10,
                               seed=None,
                               binary=False,
                               stride=1):
    if not exists(new_path):
        makedirs(new_path)

    d = get_labels_iam()
    for label, imgs in tqdm(d.items()):
        train_label_dir = join(new_path, 'train', str(label))
        validation_label_dir = join(new_path, 'validation', str(label))
        test_label_dir = join(new_path, 'test', str(label))

        if not exists(train_label_dir):
            makedirs(train_label_dir)

        if not exists(validation_label_dir):
            makedirs(validation_label_dir)

        if not exists(test_label_dir):
            makedirs(test_label_dir)

        if len(imgs) == 1:
            if binary:
                img = imread(data_path + imgs[0], as_gray=True, plugin='pil')
                otsu = threshold_otsu(img)
                img = img < otsu
            else:
                img = Image.open(data_path + imgs[0])

            img = asarray(img)
            train_img, test_img = halve_image(img)
            train_img = text_padding3(train_img,
                                      max_width=2350,
                                      max_height=2195)
            test_img = text_padding3(test_img, max_width=2350, max_height=2195)

        else:
            if binary:
                train_img = text_padding_numpy(data_path + imgs[0])
                otsu = threshold_otsu(train_img)
                train_img = train_img < otsu
                test_img = text_padding_numpy(data_path + imgs[1])
                otsu = threshold_otsu(test_img)
                test_img = test_img < otsu
            else:
                train_img = text_padding2(data_path + imgs[0],
                                          max_width=2350,
                                          max_height=2195)
                test_img = text_padding2(data_path + imgs[1],
                                         max_width=2350,
                                         max_height=2195)

        train_img = asarray(train_img)
        test_img = asarray(test_img)

        train_and_val_patches = extract_patches_2d(train_img, (height, width),
                                                   num_patches,
                                                   random_state=seed,
                                                   stride=stride,
                                                   th=1000)
        train_patches, val_patches = utils.train_val_split(
            train_and_val_patches, train_ratio=0.9)
        test_patches = extract_patches_2d(test_img, (height, width),
                                          random_state=seed,
                                          stride=(height, width),
                                          th=1000)

        a = 1
        for p in range(len(train_patches)):
            patch = train_patches[p]
            img = fromarray(patch)
            img_dir = join(train_label_dir,
                           imgs[0].split('.')[0] + '_patch_' + str(a) + '.jpg')

            if binary:
                img.save(img_dir, mode=1, optimize=True)
            else:
                img.save(img_dir)
            a = a + 1

        b = 1
        for p in range(len(val_patches)):
            patch = val_patches[p]
            img = fromarray(patch)
            img_dir = join(validation_label_dir,
                           imgs[0].split('.')[0] + '_patch_' + str(b) + '.jpg')

            if binary:
                img.save(img_dir, mode=1, optimize=True)
            else:
                img.save(img_dir)
            b = b + 1

        c = 1
        for p in range(len(test_patches)):
            patch = test_patches[p]
            img = fromarray(patch)
            try:
                img_dir = join(
                    test_label_dir,
                    imgs[1].split('.')[0] + '_patch_' + str(c) + '.jpg')
            except IndexError:
                img_dir = join(
                    test_label_dir,
                    imgs[0].split('.')[0] + '_patch_' + str(c) + '.jpg')

            if binary:
                img.save(img_dir, mode=1, optimize=True)
            else:
                img.save(img_dir)
            c = c + 1
def create_patches_dataset_firemaker(train_path,
                                     test_path,
                                     new_path,
                                     height=256,
                                     width=256,
                                     num_patches=100,
                                     seed=None,
                                     binary=False,
                                     stride=1):
    if not exists(new_path):
        makedirs(new_path)

    train_file_names = sorted(listdir(train_path))
    test_file_names = sorted(listdir(test_path))
    for t in tqdm(range(len(train_file_names))):

        train_label_dir = join(new_path, 'train', str(t))
        validation_label_dir = join(new_path, 'validation', str(t))
        test_label_dir = join(new_path, 'test', str(t))

        if not exists(train_label_dir):
            makedirs(train_label_dir)

        if not exists(validation_label_dir):
            makedirs(validation_label_dir)

        if not exists(test_label_dir):
            makedirs(test_label_dir)

        if binary:
            train_img = text_padding_numpy(train_path + train_file_names[t])
            otsu = threshold_otsu(train_img)
            train_img = train_img < otsu
            test_img = text_padding_numpy(test_path + test_file_names[t])
            otsu = threshold_otsu(test_img)
            test_img = test_img < otsu
        else:
            train_img = text_padding2(train_path + train_file_names[t],
                                      max_width=2480,
                                      max_height=2100)
            test_img = text_padding2(test_path + test_file_names[t],
                                     max_width=2480,
                                     max_height=2100)

        train_img = asarray(train_img)
        test_img = asarray(test_img)

        train_and_val_patches = extract_patches_2d(train_img, (height, width),
                                                   num_patches,
                                                   random_state=seed,
                                                   stride=stride)
        train_patches, val_patches = utils.train_val_split(
            train_and_val_patches, train_ratio=0.9)
        test_patches = extract_patches_2d(test_img, (height, width),
                                          random_state=seed,
                                          stride=(height, width))

        a = 1
        for p in range(len(train_patches)):
            patch = train_patches[p]
            img = fromarray(patch)
            img_dir = join(
                train_label_dir, train_file_names[t].split('.')[0] +
                '_patch_' + str(a) + '.jpg')

            if binary:
                img.save(img_dir, mode=1, optimize=True)
            else:
                img.save(img_dir)
            a = a + 1

        b = 1
        for p in range(len(val_patches)):
            patch = val_patches[p]
            img = fromarray(patch)
            img_dir = join(
                validation_label_dir, train_file_names[t].split('.')[0] +
                '_patch_' + str(b) + '.jpg')

            if binary:
                img.save(img_dir, mode=1, optimize=True)
            else:
                img.save(img_dir)
            b = b + 1

        c = 1
        for p in range(len(test_patches)):
            patch = test_patches[p]
            img = fromarray(patch)
            img_dir = join(
                test_label_dir,
                test_file_names[t].split('.')[0] + '_patch_' + str(c) + '.jpg')

            if binary:
                img.save(img_dir, mode=1, optimize=True)
            else:
                img.save(img_dir)
            c = c + 1
def train_geometric_matching():
    trans_params = {
        'rotation': (0, 0),
        'offset': (0, 0),
        'flip': (False, False),
        'shear': (0., 0.),
        'stretch': (1. / 2, 2),
    }

    print('building model')
    layers = vgg16.build_model((None, 3, 227, 227))

    # file to store the learned weights
    weightsfile = join('weights', 'weights.pickle')

    # initialize the feature extraction layers
    pretrainfile = join('weights', 'vgg16.pkl')
    print('initializing feature extraction layers from %s' % (pretrainfile))
    with open(pretrainfile, 'rb') as f:
        data = pickle.load(f)
    # weights are tied, no need to initialize a and b
    set_all_param_values(layers['pool4a'], data['param values'][0:20])

    # used to initialize from learned weights
    #with open(weightsfile, 'rb') as f:
    #    param_values = pickle.load(f)
    #set_all_param_values(layers['trans'], param_values)

    mean = data['mean value']

    max_epochs = 5000
    batch_size = 16
    sample_every = 25  # visualizes network output every n epochs
    sample_dir = join('data', 'samples')

    # set this to point to the root of Pascal VOC-2011
    voc_fpath = '/media/hdd/hendrik/datasets/pascal-2011'
    train_fpaths, valid_fpaths = utils.train_val_split(voc_fpath)

    print('compiling theano functions for training')
    train_func = theano_funcs.create_train_func(layers)
    print('compiling theano functions for validation')
    valid_func = theano_funcs.create_valid_func(layers)

    try:
        for epoch in range(1, max_epochs + 1):
            print('epoch %d' % (epoch))
            train_losses = []
            num_train_idx = (len(train_fpaths) + batch_size - 1) / batch_size
            train_iter = utils.get_batch_idx(len(train_fpaths), batch_size)
            for i, idx in tqdm(train_iter, total=num_train_idx, leave=False):
                X_crop_train, X_warp_train, M_train =\
                    utils.prepare_synth_batch(train_fpaths[idx], mean,
                                              trans_params)
                M, train_loss = train_func(X_crop_train, X_warp_train, M_train)
                train_losses.append(train_loss)
                if epoch % sample_every == 0:
                    utils.plot_samples(X_crop_train,
                                       X_warp_train,
                                       M,
                                       mean,
                                       prefix=join(sample_dir, 'train_%d' % i))
            print(' train loss = %.6f' % (np.mean(train_losses)))

            valid_losses = []
            num_valid_idx = (len(valid_fpaths) + batch_size - 1) / batch_size
            valid_iter = utils.get_batch_idx(len(valid_fpaths), batch_size)
            for i, idx in tqdm(valid_iter, total=num_valid_idx, leave=False):
                X_crop_valid, X_warp_valid, M_valid =\
                    utils.prepare_synth_batch(valid_fpaths[idx], mean,
                                              trans_params)
                M, valid_loss = valid_func(X_crop_valid, X_warp_valid, M_valid)
                valid_losses.append(valid_loss)
                if epoch % sample_every == 0:
                    utils.plot_samples(X_crop_valid,
                                       X_warp_valid,
                                       M,
                                       mean,
                                       prefix=join(sample_dir, 'valid_%d' % i))
            print(' valid loss = %.6f' % (np.mean(valid_losses)))
    except KeyboardInterrupt:
        print('caught ctrl-c, stopped training')

    print('saving weights to %s' % (weightsfile))
    weights = get_all_param_values(layers['trans'])
    with open(weightsfile, 'wb') as f:
        pickle.dump(weights, f, protocol=pickle.HIGHEST_PROTOCOL)
Exemplo n.º 11
0
from utils import load_data, train_val_split
from model import UnetBrain

from keras.callbacks import EarlyStopping, ModelCheckpoint, ReduceLROnPlateau

import sys
import matplotlib.pyplot as plt
plt.style.use("ggplot")

train_data = load_data('./machine_learning_challenge/train_data.csv')
train_label = load_data('./machine_learning_challenge/train_label.csv')
test_data = load_data('./machine_learning_challenge/test_data.csv')
test_label = load_data('./machine_learning_challenge/test_label.csv')

X_train, y_train, X_val, y_val = train_val_split(train_data,
                                                 train_label,
                                                 val_size=0.20)

X_train.shape

fig, axes = plt.subplots(nrows=1, ncols=2)
ax = axes.ravel()
ax[0].imshow(np.squeeze(X_train[100]))
ax[0].set_title("image")
ax[1].imshow(np.squeeze(y_train[100]))
ax[1].set_title("label")

plt.show()

# input parameters
params = {}
Exemplo n.º 12
0
from NeuralNetwork import *
from Layer import *

#mnist.init()
X_train, Y_train, X_test, Y_test = mnist.load()

# Pre-process data. For computers with less RAM, we must slice the training set
X_train = X_train[0:50000]
Y_train = Y_train[0:50000]

X_train, X_test = (X_train / 127.5) - 1, (X_test / 127.5) - 1
X_train = utils.bias_trick(X_train)
X_test = utils.bias_trick(X_test)
Y_train, Y_test = utils.onehot_encode(Y_train), utils.onehot_encode(Y_test)

X_train, Y_train, X_val, Y_val = utils.train_val_split(X_train, Y_train, 0.1)

#Add Layers
hidden_layer = Layer(num_input=X_train.shape[1],
                     num_neurons=64,
                     activation_func=utils.relu,
                     activation_func_der=utils.relu_der)
output_layer = Layer(
    num_input=64, num_neurons=Y_train.shape[1], activation_func=utils.softmax
)  #The derivation of the activation function of the output layer is not used

#Create network
model = NeuralNetwork(max_epochs=20,
                      learning_rate=0.01,
                      should_gradient_check=False,
                      batch_size=256,
Exemplo n.º 13
0
    patches_img = np.array(patches_img)
    patches_mask = np.array(patches_mask)
    return patches_img, patches_mask

##########
## MAIN ##
##########

def parse_args():
    parser = argparse.ArgumentParser(description="Arguments for training")
    parser.add_argument("-i", "--images_folder", dest="images_folder", help="Path to images (and mask) folder", required=True)
    parser.add_argument("-o", "--output_folder", dest="output_folder", help="Path to the output folder (for patches)", required=True)
    parser.add_argument("-ps", "--patch_size", dest="patch_size", help="Patch size", default=constants.PATCH_SIZE, type=int)
    parser.add_argument("-p", "--train_prop", dest="train_prop", help="Proportion of train set", default=constants.TRAIN_PROPORTION, type=float)
    return parser.parse_args()

if __name__ == "__main__":
    args = parse_args()
    img_ids = utils.get_img_ids(args.images_folder)
    train_img_ids, val_img_ids = utils.train_val_split(img_ids, args.train_prop)
    for img_id in tqdm(train_img_ids):
        patches_img, patches_mask = extract_patches(img_id, args.images_folder, args.patch_size)
        for k in range(len(patches_img)):
            cv2.imwrite("{}/train/images/{}_{}.jpg".format(args.output_folder, img_id, k), patches_img[k])
            cv2.imwrite("{}/train/masks/{}_{}.jpg".format(args.output_folder, img_id, k), patches_mask[k])
    for img_id in tqdm(val_img_ids):
        patches_img, patches_mask = extract_patches(img_id, args.images_folder, args.patch_size)
        for k in range(len(patches_img)):
            cv2.imwrite("{}/val/images/{}_{}.jpg".format(args.output_folder, img_id, k), patches_img[k])
            cv2.imwrite("{}/val/masks/{}_{}.jpg".format(args.output_folder, img_id, k), patches_mask[k])
Exemplo n.º 14
0
import dataloader
import settings
import utils

K = settings.folds
cnn = model.CNN()
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(cnn.parameters(),
                      lr=settings.lr,
                      momentum=settings.momentum)
trainset = dataloader.trainset

start = time.time()
for k in range(K):
    train_loss, val_loss = [], []
    trainloader, valloader = utils.train_val_split(trainset, K)
    print("data loaded")
    for epoch in range(settings.epochs):
        for i, data in enumerate(trainloader):
            inputs, labels = data
            optimizer.zero_grad()
            outputs = cnn(inputs)
            loss = criterion(outputs, labels)
            loss.backward()
            optimizer.step()
            train_loss.append(loss.item())
        for i, data in enumerate(valloader):
            inputs, labels = data
            outputs = cnn(inputs)
            loss = criterion(outputs, labels)
            val_loss.append(loss.item())
Exemplo n.º 15
0
        NB_AS = X_train_aug.shape[2]
    print("y train shape: ", y_train.shape)

    time_data = time.time() - start_time
    save_results = True

    if cross_validate:

        cv_scores, model_history = crossValidation(load_file, X_train_aug,
                                                   y_train)
        test_accs = save_cv(weights_file, cv_scores, file_scores,
                            file_scores_mean, N_FOLDS)
        test_acc = test_accs[file_test[0] + '_mean']

    else:
        X_train_aug, y_train, X_val_aug, y_val = train_val_split(
            hmm, X_train_aug, y_train, tv_perc, embedding)
        model, history = build_and_train(X_train_aug,
                                         y_train,
                                         X_val_aug,
                                         y_val,
                                         epochs=epochs)
        test_acc = evaluate_model(model=model,
                                  load_file=load_file,
                                  file_test=file_test,
                                  hmm=hmm,
                                  normalize=normalize,
                                  standardize=standardize,
                                  embedding=embedding)

time_end = time.time() - start_time
m, s = divmod(time_end, 60)
Exemplo n.º 16
0
def main():
    data_name = "augmented_1"
    data_path = os.path.join("./data", data_name)
    csv_name = data_name + ".csv"
    train_df = pd.read_csv(os.path.join(data_path, csv_name))

    keypoint_names = list(
        map(lambda x: x[:-2],
            train_df.columns.to_list()[1::2]))
    keypoint_flip_map = [
        ("left_eye", "right_eye"),
        ("left_ear", "right_ear"),
        ("left_shoulder", "right_shoulder"),
        ("left_elbow", "right_elbow"),
        ("left_wrist", "right_wrist"),
        ("left_hip", "right_hip"),
        ("left_knee", "right_knee"),
        ("left_ankle", "right_ankle"),
        ("left_palm", "right_palm"),
        ("left_instep", "right_instep"),
    ]

    image_list = train_df.iloc[:, 0].to_numpy()
    keypoints_list = train_df.iloc[:, 1:].to_numpy()
    train_imgs, valid_imgs, train_keypoints, valid_keypoints = train_val_split(
        image_list, keypoints_list, random_state=42)

    image_set = {"train": train_imgs, "valid": valid_imgs}
    keypoints_set = {"train": train_keypoints, "valid": valid_keypoints}

    hyper_params = {
        "augmented_ver": data_name,
        "learning_rate": 0.001,
        "num_epochs": 10000,
        "batch_size": 256,
        "description": "Final training"
    }

    for phase in ["train", "valid"]:
        DatasetCatalog.register(
            "keypoints_" + phase,
            lambda phase=phase: get_data_dicts(data_path, image_set[phase],
                                               keypoints_set[phase]))
        MetadataCatalog.get("keypoints_" + phase).set(thing_classes=["human"])
        MetadataCatalog.get("keypoints_" +
                            phase).set(keypoint_names=keypoint_names)
        MetadataCatalog.get("keypoints_" +
                            phase).set(keypoint_flip_map=keypoint_flip_map)
        MetadataCatalog.get("keypoints_" + phase).set(evaluator_type="coco")

    cfg = get_cfg()
    cfg.merge_from_file(
        model_zoo.get_config_file(
            "COCO-Keypoints/keypoint_rcnn_X_101_32x8d_FPN_3x.yaml"))
    cfg.DATASETS.TRAIN = ("keypoints_train", )
    cfg.DATASETS.TEST = ("keypoints_valid", )
    cfg.DATALOADER.NUM_WORKERS = 16  # On Windows environment, this value must be 0.
    cfg.SOLVER.IMS_PER_BATCH = 2  # mini batch size would be (SOLVER.IMS_PER_BATCH) * (ROI_HEADS.BATCH_SIZE_PER_IMAGE).
    cfg.SOLVER.BASE_LR = hyper_params["learning_rate"]  # Learning Rate.
    cfg.SOLVER.MAX_ITER = hyper_params["num_epochs"]  # Max iteration.
    cfg.SOLVER.GAMMA = 0.8
    cfg.SOLVER.STEPS = [
        3000, 4000, 5000, 6000, 7000, 8000
    ]  # The iteration number to decrease learning rate by GAMMA.
    cfg.MODEL.WEIGHTS = model_zoo.get_checkpoint_url(
        "COCO-Keypoints/keypoint_rcnn_X_101_32x8d_FPN_3x.yaml")
    cfg.MODEL.ROI_HEADS.BATCH_SIZE_PER_IMAGE = hyper_params[
        "batch_size"]  # Use to calculate RPN loss.
    cfg.MODEL.ROI_HEADS.NUM_CLASSES = 1
    cfg.MODEL.ROI_KEYPOINT_HEAD.NUM_KEYPOINTS = 24
    cfg.TEST.KEYPOINT_OKS_SIGMAS = np.ones((24, 1), dtype=float).tolist()
    cfg.TEST.EVAL_PERIOD = 5000  # Evaluation would occur for every cfg.TEST.EVAL_PERIOD value.
    cfg.OUTPUT_DIR = os.path.join("./output", data_name)

    os.makedirs(cfg.OUTPUT_DIR, exist_ok=True)
    trainer = Trainer(cfg)
    trainer.resume_or_load(resume=False)
    trainer.train()

    # Inference should use the config with parameters that are used in training
    # cfg now already contains everything we've set previously. We changed it a little bit for inference:
    cfg.MODEL.WEIGHTS = os.path.join(
        cfg.OUTPUT_DIR, "model_final.pth")  # path to the model we just trained
    cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.7  # set a custom testing threshold
    predictor = DefaultPredictor(cfg)

    test_dir = os.path.join("data", "test_imgs")
    test_list = os.listdir(test_dir)
    test_list.sort()
    except_list = []

    files = []
    preds = []
    for file in tqdm(test_list):
        filepath = os.path.join(test_dir, file)
        im = cv2.imread(filepath)
        outputs = predictor(im)
        outputs = outputs["instances"].to("cpu").get("pred_keypoints").numpy()
        files.append(file)
        pred = []
        try:
            for out in outputs[0]:
                pred.extend([float(e) for e in out[:2]])
        except IndexError:
            pred.extend([0] * 48)
            except_list.append(filepath)
        preds.append(pred)

    df_sub = pd.read_csv("./data/sample_submission.csv")
    df = pd.DataFrame(columns=df_sub.columns)
    df["image"] = files
    df.iloc[:, 1:] = preds

    df.to_csv(os.path.join(cfg.OUTPUT_DIR, f"{data_name}_submission.csv"),
              index=False)
    if except_list:
        print(
            "The following images are not detected keypoints. The row corresponding that images names would be filled with 0 value."
        )
        print(*except_list)
    save_samples(cfg.OUTPUT_DIR,
                 test_dir,
                 os.path.join(cfg.OUTPUT_DIR, f"{data_name}_submission.csv"),
                 mode="random",
                 size=5)
Exemplo n.º 17
0
print()
print('+------------------------------------+')
print('|              PRODUVIA              |')
print('+------------------------------------+')
print()

PATH = Path(kvargs['data_path'])
DEVICE = kvargs['device']
padding = partial(dynamic_ws_padding, max_sent=kvargs['max_sent'], max_words=kvargs['max_sent_words'])

# create mapping text <-> sent
data = imdb_extraction(PATH/'train')
test = imdb_extraction(PATH/'test')

# split data into train & validation
train, valid = train_val_split(data, train_ptg=.8)

# create vocabulary
t2i, i2t = create_vocab(train)

# create datasets
train_ds = ha_dataset(train, t2i)
valid_ds = ha_dataset(valid, t2i)

# create dataloaders
train_dl = DataLoader(train_ds, shuffle=True, batch_size=kvargs['bs'], 
                    collate_fn=padding, drop_last=True)
valid_dl = DataLoader(valid_ds, shuffle=False, batch_size=kvargs['bs'], 
                    collate_fn=padding, drop_last=True)

# define architecutre
Exemplo n.º 18
0
    images_per_video = 250
    num_training_images = 200
    img_height = 288
    img_width = 160
    frames_to_mix = 3

    input_shape = (3, img_height, img_width)
    remake_images = False


    ImageMaker = VideoToBlurredImgConverter(images_per_video, frames_to_mix, img_height,
                                   img_width, vid_dir = './data/', rebuild_target_dir=False)
    if remake_images:
        ImageMaker.make_and_save_images()

    train_fnames, val_fnames = train_val_split('./data', num_training_images, images_per_video)
    data_feeder = DataFeeder(batch_size=20, gen_only_batch_size=20, fnames=train_fnames)

    gen_model, disc_model, gen_disc_model = make_models(input_shape,
                                                        n_filters_in_res_blocks=[64 for _ in range(3)],
                                                        gen_filter_size=3,
                                                        layers_in_res_blocks=2,
                                                        res_block_subsample=(2, 2),
                                                        filters_in_deconv=[32 for _ in range(3)],
                                                        deconv_filter_size=3,
                                                        n_disc_filters=[64, 32, 32])

    trainer = Trainer(gen_model, disc_model, gen_disc_model, data_feeder, report_freq=10)
    trainer.train(n_steps=1000)
    gen_model, disc_model, gen_disc_model = trainer.get_models()
    save_predicted_images(gen_model, val_fnames)