Exemple #1
0
def debug_model_single(debug_mode=True):
    # THIS WORKS

    in_features_1 = 540
    genotype_1 = (3e-4, 2, [12, 18], [5, 5], [0, 0], [0, 0], 0, 1, 600,
                  [0, 1, 0, 1, 2, 2], in_features_1)
    genome_1 = GO.write_genome(genotype_1)

    pv1 = ProjectVariable(debug_mode)
    pv1 = apply_same_settings(pv1)
    pv1 = apply_unique_settings(pv1, genome_1)
    pv1.experiment_number = 1000134978789
    pv1.device = 1

    results = main_file.run(pv1)

    print('asdf')
Exemple #2
0
def auto_search(lr_size,
                epochs,
                repeat_run,
                model_number,
                conv_layer_channels,
                device,
                b=1,
                e=10,
                s=2):
    file_name = date.today().strftime('%d%m') + '_' + datetime.now().strftime(
        '%H%M%S') + '.txt'
    save_path = os.path.join(PP.nas_location, file_name)

    header = 'lr,epochs,model_number,conv_layer_channels,train_acc,val_acc,have_collapsed,which_matr_ind\n'
    with open(save_path, 'a') as my_file:
        my_file.write(header)

    for cfg in range(len(conv_layer_channels)):
        out_channels = conv_layer_channels[cfg]

        for lr_incr in range(b, e, s):
            lr = lr_incr / lr_size

            for run in range(repeat_run):
                chnls = '['
                for i in range(len(out_channels)):
                    chnls = chnls + str(out_channels[i]) + ' '

                chnls = chnls[:-1] + ']'

                settings = '%f,%d,%d,%s,' % (lr, epochs, model_number, chnls)
                with open(save_path, 'a') as my_file:
                    my_file.write(settings)

                project_variable = ProjectVariable(debug_mode=True)
                train_acc, val_acc, has_collapsed, collapsed_matrix = \
                    run_single_experiment(project_variable, lr, epochs, out_channels, device, model_number)

                if has_collapsed:
                    ind = np.where(collapsed_matrix == 1)[0]
                else:
                    ind = 420

                results = '%f,%f,%s,%s\n' % (train_acc, val_acc,
                                             str(has_collapsed), str(ind))
                with open(save_path, 'a') as my_file:
                    my_file.write(results)
def resize_images():
    from relative_baseline.omg_emotion import data_loading as DL
    from relative_baseline.omg_emotion.settings import ProjectVariable
    from relative_baseline.omg_emotion import project_paths as PP

    project_variable = ProjectVariable()

    # splits = ['Training', 'Validation', 'Test']
    splits = ['Test']

    for i in splits:
        cnt = 0
        cnt_d = 0
        labels = DL.load_labels(i, project_variable)
        datapoints = len(labels[0])
        for j in range(datapoints):
            aff = True
            utterance_path = os.path.join(PP.data_path, i, PP.omg_emotion_jpg,
                                          labels[0][j][0],
                                          labels[0][j][1].split('.')[0])

            frames = os.listdir(utterance_path)

            for k in range(len(frames)):
                jpg_path = os.path.join(utterance_path, '%d.jpg' % k)
                jpg_as_arr = Image.open(jpg_path)
                if jpg_as_arr.width != 1280 or jpg_as_arr.height != 720:
                    jpg_as_arr = jpg_as_arr.resize((1280, 720))
                    jpg_as_arr.save(jpg_path)
                    cnt += 1
                    if aff:
                        cnt_d += 1
                        print('%d' % j)
                        aff = False

        print('resized in %s, frames: %d, datapoints: %d ' % (i, cnt, cnt_d))
Exemple #4
0
    project_variable.learning_rate = 1e-3
    project_variable.use_adaptive_lr = True
    project_variable.num_out_channels = [12, 22]

    main_file.run(project_variable)


def e8_3D_omgemo():
    set_init_1()
    project_variable.experiment_number = 8
    project_variable.sheet_number = 20
    project_variable.device = 0

    project_variable.learning_rate = 1e-4
    project_variable.use_adaptive_lr = True
    project_variable.num_out_channels = [12, 22]

    main_file.run(project_variable)


project_variable = ProjectVariable(debug_mode=False)

# e1_3D_omgemo() # run in crashed mode
# e2_3D_omgemo()
# e3_3D_omgemo()
# e4_3D_omgemo()
e5_3D_omgemo()
# e6_3D_omgemo()
# e7_3D_omgemo()
# e8_3D_omgemo()
def parallel_experiment():
    pv1 = ProjectVariable(debug_mode=True)
    p1 = same_settings(pv1)
    pv1.experiment_number = 111111111111111111
    pv1.num_out_channels = [6, 16]
    pv1.device = 0

    pv2 = ProjectVariable(debug_mode=True)
    p2 = same_settings(pv2)
    pv2.experiment_number = 222222222222222
    pv2.num_out_channels = [12, 22]
    pv2.device = 1

    pv3 = ProjectVariable(debug_mode=True)
    p3 = same_settings(pv3)
    pv3.experiment_number = 333333333333333
    pv3.num_out_channels = [8, 18]
    pv3.device = 2

    pool = Pool(processes=3)
    # pool.apply_async(main_file.run)
    results = pool.map(main_file.run, [p1, p2, p3])

    pool.join()
    pool.close()
def create_moving_mnist_png(which, frames=FRAMES):
    assert which in ['test', 'train', 'val']

    # setting up
    settings = ProjectVariable()
    settings.dataset = 'mnist'
    settings.train = False
    settings.val = False
    settings.test = False

    if which == 'train':
        settings.train = True
        SEED = 6
    elif which == 'val':
        settings.val = True
        SEED = 7
    elif which == 'test':
        settings.test = True
        SEED = 8
    else:
        SEED = None

    np.random.seed(SEED)

    mov_mnist_data_folder = os.path.join(PP.moving_mnist_png, which)

    if not os.path.exists(mov_mnist_data_folder):
        os.mkdir(mov_mnist_data_folder)

    label_file = os.path.join(PP.moving_mnist_location,
                              'labels_%s.csv' % which)
    with open(label_file, 'w') as my_file:
        pass

    # get original mnist
    mnist_all = D.load_mnist(settings)
    data = np.array(mnist_all[1][0])
    labels = np.array(mnist_all[2][0])

    # make data
    for i in tqdm.tqdm(range(data.shape[0])):
        lab = labels[i]
        image = data[i][0]
        video = None

        if lab == 0:
            video = move_horizontal(image, frames)
        elif lab == 1:
            video = move_vertical(image, frames)
        elif lab == 2:
            video = scale(image, frames)
        elif lab == 3:
            video = rotate(-1, image, frames)
        elif lab == 4:
            video = rotate(1, image, frames)
        elif lab == 5:
            video = move_in_circle(image, frames)
        elif lab == 6:
            video = scale_up_rotate_clockwise(image, frames)
        elif lab == 7:
            video = move_horizontal_rotate_counter(image, frames)
        elif lab == 8:
            video = rotate_clock_and_counter(image, frames)
        elif lab == 9:
            video = random_transformations(image, frames)
        else:
            print('Error: lab with value not expected %d' % lab)

        # save arrays as pngs
        folder = os.path.join(mov_mnist_data_folder, '%d' % i)
        if not os.path.exists(folder):
            os.mkdir(folder)

        for j in range(FRAMES):
            im = Image.fromarray(video[j].astype(DTYPE), mode=MODE)
            path = os.path.join(folder, '%d.png' % j)
            im.save(path)

        # save label
        with open(label_file, 'a') as my_file:
            my_file.write('%d\n' % lab)
import numpy as np
from PIL import Image
import h5py as h5
import tqdm

from relative_baseline.omg_emotion import data_loading as D
from relative_baseline.omg_emotion.settings import ProjectVariable
from relative_baseline.omg_emotion import project_paths as PP

FRAMES = 30
DTYPE = np.uint8
RESAMPLE = Image.BILINEAR
SIDE = 28
MODE = 'L'

PV = ProjectVariable()
PV.dataset = 'mnist'
PV.train = False
PV.val = False
PV.test = True


def get_next_x(x_current, y_current, direction, speed):
    x_next = x_current + direction * speed
    if x_next > (SIDE - 1) or x_next < 1:
        direction *= -1
        speed += 1
        x_next = x_current + direction * speed
        if x_next > (SIDE - 1) or x_next < 1:
            print('Something is wrong: x_next=%d' % x_next)
            return 0, direction, speed
Exemple #8
0
def debug_model_parallel(debug_mode=True):
    in_features_1 = 540
    #               0   1   2        3        4       5      6  7  8    9                    10
    genotype_1 = (3e-4, 2, [12, 18], [5, 5], [0, 0], [0, 0], 0, 1, 600,
                  [0, 1, 0, 1, 2, 2], in_features_1)
    genome_1 = GO.write_genome(genotype_1)

    in_features_2 = 336
    genotype_2 = (3e-4, 3, [16, 32, 32], [3, 5, 5], [0, 0, 0], [0, 0, 0], 0, 1,
                  496, [0, 1, 0, 0, 1, 2, 2], in_features_2)
    genome_2 = GO.write_genome(genotype_2)

    in_features_3 = 182
    genotype_3 = (3e-4, 3, [16, 32, 48], [5, 5, 5], [0, 0, 0], [0, 0, 0], 0, 1,
                  256, [0, 1, 0, 0, 1, 2, 2], in_features_3)
    genome_3 = GO.write_genome(genotype_3)

    pv1 = ProjectVariable(debug_mode)
    pv1 = apply_same_settings(pv1)
    pv1 = apply_unique_settings(pv1, genome_1)
    pv1.experiment_number = 10001
    pv1.individual_number = 1
    pv1.device = 0

    pv2 = ProjectVariable(debug_mode)
    pv2 = apply_same_settings(pv2)
    pv2 = apply_unique_settings(pv2, genome_2)
    pv2.experiment_number = 10002
    pv2.individual_number = 2
    pv2.device = 1

    pv3 = ProjectVariable(debug_mode)
    pv3 = apply_same_settings(pv3)
    pv3 = apply_unique_settings(pv3, genome_3)
    pv3.experiment_number = 10003
    pv3.individual_number = 3
    pv3.device = 2

    pool = Pool(processes=3)
    results = pool.map(main_file.run, [pv1, pv2, pv3])

    col, val, train = process_results(results)

    print('asdf')
Exemple #9
0
def evolutionary_search(debug_mode=True):
    # generations = 100
    generations = 50
    genetic_search_path = os.path.join(PP.jester_location,
                                       'genetic_search_log.txt')
    if not os.path.exists(genetic_search_path):
        # delimiter = ';'
        with open(genetic_search_path, 'a') as my_file:
            header = 'generation;exp_num;collapsed;val_acc;train_acc;' \
                     'lr;num_conv_layers;num_channels;kernels;padding;conv_type;pooling_a_conv;' \
                     'pooling_final;fc_layer;arch_order;in_features\n'
            my_file.write(header)

    results = None

    genotype_1, genotype_2, genotype_3 = None, None, None

    for gen in range(generations):
        if gen == 0:
            # manually set first values
            # in_features_1 = 1*6*12
            ##                0   1   2                  3                  4       5            6  7  8      9                          10
            # genotype_1 = (3e-4, 4, [12, 18, 24, 32], [3, 5, 5, 5], [0, 0, 0, 0], [0, 0, 0, 0], 0, 1, 600, [0, 1, 0, 0, 0, 1, 2, 2], in_features_1)

            genotype_1 = (5e-5, 4, [10, 26, 32, 32], [3, 5, 5,
                                                      5], [0, 0, 0, 0],
                          [0, 0, 0, 0], 0, 1, 1200, [0, 1, 0, 0, 0, 1, 2,
                                                     2], 72)
            genome_1 = GO.write_genome(genotype_1)

            genotype_2 = (5e-5, 4, [16, 20, 32, 32], [3, 3, 5,
                                                      3], [0, 0, 0, 0],
                          [0, 0, 0, 0], 1, 1, 1968, [0, 1, 0, 0, 0, 1, 2,
                                                     2], 336)
            genome_2 = GO.write_genome(genotype_2)

            genotype_3 = (5e-5, 4, [16, 20, 38, 38], [3, 3, 5,
                                                      5], [0, 0, 0, 0],
                          [0, 0, 0, 0], 0, 1, 1840, [0, 1, 0, 0, 0, 1, 2,
                                                     2], 182)
            genome_3 = GO.write_genome(genotype_3)

        else:
            assert results is not None

            new_genotypes = GO.generate_genotype(
                results, [genotype_1, genotype_2, genotype_3])

            genotype_1, genotype_2, genotype_3 = new_genotypes

            genome_1 = GO.write_genome(genotype_1)
            genome_2 = GO.write_genome(genotype_2)
            genome_3 = GO.write_genome(genotype_3)

        # run models
        pv1 = ProjectVariable(debug_mode)
        pv1 = apply_same_settings(pv1)
        pv1 = apply_unique_settings(pv1, genome_1)
        pv1.experiment_number = 10001
        pv1.individual_number = 1
        pv1.device = 0

        pv2 = ProjectVariable(debug_mode)
        pv2 = apply_same_settings(pv2)
        pv2 = apply_unique_settings(pv2, genome_2)
        pv2.experiment_number = 10002
        pv2.individual_number = 2
        pv2.device = 1

        pv3 = ProjectVariable(debug_mode)
        pv3 = apply_same_settings(pv3)
        pv3 = apply_unique_settings(pv3, genome_3)
        pv3.experiment_number = 10003
        pv3.individual_number = 3
        pv3.device = 2

        pool = Pool(processes=3)
        results = pool.map(main_file.run, [pv1, pv2, pv3])

        # results = [(1, False, 0.0, 0.0),
        #            (2, False, 0.0, 0.037037037037037035),
        #            (3, False, 0.0, 0.07407407407407407)]

        pool.close()
        pool.join()

        col, val, train = process_results(results)
        results = col, val, train

        for k in col.keys():
            # keys are 1, 2, 3
            if k == '1':
                pv = pv1
            elif k == '2':
                pv = pv2
            else:
                pv = pv3

            line = '%d;%d;%s;%f;%f;%f;%d;%s;%s;%s;%s;%s;%s;%d;%s;%d\n' % \
                   (gen, pv.individual_number, str(col[k]), val[k], train[k], pv.learning_rate, pv.genome['num_conv_layers'],
                    str(pv.num_out_channels), str(pv.genome['kernel_size_per_layer']), str(pv.genome['padding']),
                    str(pv.genome['conv_layer_type']), pv.genome['pooling_after_conv'], pv.genome['pooling_final'],
                    pv.genome['fc_layer'], str(pv.genome['architecture_order']), pv.genome['in_features'])

            with open(genetic_search_path, 'a') as my_file:
                my_file.write(line)

        del pv1, pv2, pv3
    project_variable.theta_init = None
    project_variable.srxy_init = 'eye'
    project_variable.weight_transform = 'seq'

    project_variable.repeat_experiments = 1

    project_variable.experiment_number = 777656767
    project_variable.sheet_number = 21
    project_variable.device = 0

    project_variable.model_number = 20
    project_variable.data_points = [2 * 14, 2 * 14, 1 * 14]
    project_variable.batch_size = 2 * 14

    project_variable.optimizer = 'adam'
    project_variable.learning_rate = 1e-4
    project_variable.use_adaptive_lr = True
    project_variable.num_out_channels = [6, 16]

    main_file.run(project_variable)


project_variable = ProjectVariable(debug_mode=True)

# e71_3D_dhg()
# e72_3DTTN_dhg()

e_test_3DTTN_dhg()

# thisistest()
Exemple #11
0
import torch
from torch.utils.data import DataLoader, Dataset
import numpy as np
import os

from relative_baseline.omg_emotion.settings import ProjectVariable
from relative_baseline.omg_emotion import data_loading as D

project_variable = ProjectVariable(debug_mode=True)

project_variable.dataset = 'omg_emotion'
project_variable.data_points = [10 * 7, 0, 0]
project_variable.train = True
project_variable.test = False
project_variable.val = False
project_variable.label_type = 'categories'

all_data = D.load_data(project_variable, seed=42)

data = all_data[1][0]
labels = all_data[2][0]

print('gabi')


class FaceLandmarksDataset(Dataset):
    """Face Landmarks dataset."""
    def __init__(self, csv_file, root_dir, transform=None):
        """
        Args:
            csv_file (string): Path to the csv file with annotations.
Exemple #12
0
def random_sampling():
    project_variable = ProjectVariable(debug_mode=True)
    project_variable.dataset = 'mnist'
    project_variable.train = False
    project_variable.val = True
    project_variable.test = False

    how_many = 200
    num_params = 4
    # num_params = 6

    # get  MNIST images
    splits, data, labels = D.load_mnist(project_variable)
    data = data[0].numpy()[0:how_many].reshape((how_many, 28, 28))

    transformed_mnist = np.zeros((data.shape[0], 28, 28))

    if num_params == 6:
        params = np.zeros((data.shape[0], 2, 3))
    else:
        params = np.zeros((data.shape[0], 4))

    for i in range(data.shape[0]):
        # make transformation grids
        out_channels = 1
        # 1: theta = torch.nn.init.normal(torch.nn.Parameter(torch.zeros((out_channels, 2, 3))))
        # 2: theta = torch.eye(3)[:2] + torch.nn.init.normal(torch.nn.Parameter(torch.zeros((out_channels, 2, 3))), std=0.1)
        # 3: theta = torch.eye(3)[:2] + torch.nn.init.normal(torch.nn.Parameter(torch.zeros((out_channels, 2, 3))), std=0.5)
        # 4: theta = torch.eye(3)[:2] + torch.nn.init.normal(torch.nn.Parameter(torch.zeros((out_channels, 2, 3))), std=0.5)
        #    theta[0][0][2] = 0
        #    theta[0][1][2] = 0
        # 5:
        scale = torch.nn.init.normal(torch.nn.Parameter(torch.zeros(1)))
        rotate = torch.nn.init.normal(torch.nn.Parameter(torch.zeros(1)))
        translate_x = torch.nn.init.normal(torch.nn.Parameter(torch.zeros(1)))
        translate_y = torch.nn.init.normal(torch.nn.Parameter(torch.zeros(1)))
        theta = make_theta_matrix(scale, rotate, translate_x, translate_y)

        # theta = torch.zeros((out_channels, 2, 3))
        # theta[0][0][0] = scale * torch.cos(rotate)
        # theta[0][0][1] = -scale * torch.sin(rotate)
        # theta[0][0][2] = translate_x * scale * torch.cos(rotate) - translate_y * scale * torch.sin(rotate)
        # theta[0][1][0] = scale * torch.sin(rotate)
        # theta[0][1][1] = -scale * torch.cos(rotate)
        # theta[0][1][2] = translate_x * scale * torch.sin(rotate) + translate_y * scale * torch.cos(rotate)
        grid = F.affine_grid(theta, torch.Size([out_channels, 1, 28, 28]))

        # apply transformations to MNIST
        d = np.expand_dims(data[i], 0)
        d = np.expand_dims(d, 0)
        transformed_mnist[i] = F.grid_sample(torch.Tensor(d),
                                             grid).detach().numpy().reshape(
                                                 (28, 28))

        if num_params == 6:
            params[i] = theta.detach().numpy().reshape((2, 3))
        else:
            params[i] = float(scale), float(rotate), float(translate_x), float(
                translate_y)

    # save data & visualize
    save_location_images = '/scratch/users/gabras/data/convttn3d_project/sanity_check_affine/images'
    save_location_log = '/scratch/users/gabras/data/convttn3d_project/sanity_check_affine/log.txt'

    if os.path.exists(save_location_log):
        os.remove(save_location_log)

    for i in range(data.shape[0]):
        # make image, before and after
        the_mode = 'L'
        canvas = Image.new(mode=the_mode, size=(28 + 5 + 28, 28))
        im1 = Image.fromarray(data[i].astype(np.uint8), mode=the_mode)
        im2 = Image.fromarray(transformed_mnist[i].astype(np.uint8),
                              mode=the_mode)
        canvas.paste(im1, (0, 0))
        canvas.paste(im2, (28 + 5, 0))
        canvas_path = os.path.join(save_location_images, '%d.jpg' % i)
        canvas.save(canvas_path)

        # write line
        if num_params == 6:
            p = params[i].flatten()
            line = '%d,%f,%f,%f,%f,%f,%f\n' % (i, p[0], p[1], p[2], p[3], p[4],
                                               p[5])
            with open(save_location_log, 'a') as my_file:
                my_file.write(line)
        else:
            p = params[i]
            line = '%d,%f,%f,%f,%f\n' % (i, p[0], p[1], p[2], p[3])
            with open(save_location_log, 'a') as my_file:
                my_file.write(line)