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 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.
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 loc = (x_current, 0, x_current + SIDE, SIDE) return loc, x_next, y_current, direction, speed
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)