model = Transporter(encodings, DISTR, FOLDER, BATCH_SIZE_GEN) elif MODEL == 'generator': model = Generator(encodings, DISTR, FOLDER, BATCH_SIZE_GEN) # I Could try L2 Loss instead of L1? else: raise NotImplementedError # Train the Latent Space Model if GEN_LOAD: model.load_weights(MODEL) else: model.train(STEPS, lr=0.001) # I should try adjusting the learning rate? #model.train(STEPS//2, lr=0.0003) #model.train(STEPS//2, lr=0.0001) # Display Results fake_distr = model.generate(batches=1) fake_img = ae.decode(fake_distr) fake_img = np.reshape(fake_img, ((BATCH_SIZE_GEN,) + shape)) save_image(torch.Tensor(fake_img[0:64]), os.path.join(FOLDER, "final.png")) # Save Images in a file for later evaluation eval_distr = model.generate(batches=10000//BATCH_SIZE_GEN) eval_img = ae.decode(eval_distr) np.save("{}/distribution.npy".format(FOLDER), eval_distr) np.save("{}/images.npy".format(FOLDER), eval_img) #channels_last = np.rollaxis(fake_img[0:16], 1, 4) #display_img(channels_last, columns=4)
y = np.array(labels) horse_indices = np.where(y == 7)[0] horse_x = x[horse_indices] print(np.shape(horse_x)) # (5000, 3072) # Train the autoencoder on images of horses input_dim = np.shape(horse_x)[1] hidden_dim = 100 ae = Autoencoder(input_dim, hidden_dim) ae.train(horse_x) # Test the autoencoder on other images test_data = unpickle('../cifar-10-batches-py/test_batch') test_x = grayscale(test_data['data']) test_labels = np.array(test_data['labels']) encodings = ae.classify(test_x, test_labels) # Decode images numberOfImages = 4 plt.rcParams['figure.figsize'] = (100, 100) plt.figure() for i in range(numberOfImages): plt.subplot(numberOfImages, 2, i * 2 + 1) original_img = np.reshape(test_x[i, :], (32, 32)) plt.imshow(original_img, cmap='Greys_r') plt.subplot(numberOfImages, 2, i * 2 + 2) reconstructed_img = ae.decode([encodings[i]]) plt.imshow(reconstructed_img, cmap='Greys_r') plt.show()
from tqdm import tqdm from autoencoder import Autoencoder device = torch.device("cuda" if torch.cuda.is_available() else "cpu") AUTOENCODER_FILENAME = 'trained_models/autoencoder.to' from image_loader import ImageDataset dataset = ImageDataset(return_hashes=True) data_loader = DataLoader(dataset, batch_size=1, shuffle=True, num_workers=4) autoencoder = Autoencoder() autoencoder.load_state_dict(torch.load(AUTOENCODER_FILENAME)) autoencoder.eval() with torch.no_grad(): for sample in tqdm(data_loader): image, hash = sample hash = hash[0] output = autoencoder.decode( autoencoder.encode(image.to(device).unsqueeze(0))) result = torch.zeros((3, 128, 256)) result[:, :, :128] = image.cpu() result[:, :, 128:] = output utils.save_image(result, 'data/test/{:s}.jpg'.format(hash))
plt.imshow(view_data[i].squeeze()) plt.gray() ax.get_xaxis().set_visible(False) ax.get_yaxis().set_visible(False) # reconstructed image ax = plt.subplot(2 * N_ROWS, N_COLS, 2 * r * N_COLS + c + N_COLS) x = Variable(view_data[i]) _, y = encoder_net(x.view(1, -1)) plt.imshow(y.detach().squeeze().numpy().reshape(28, 28)) ax.get_xaxis().set_visible(False) ax.get_yaxis().set_visible(False) elif args.vis == "gmmn": print("Images generated by GMMN") gmm_net = GMMN(NOISE_SIZE, ENCODED_SIZE) gmm_net.load_state_dict(torch.load(GMMN_SAVE_PATH)) for r in range(N_ROWS): for c in range(N_COLS): ax = plt.subplot(N_ROWS, N_COLS, r * N_COLS + c + 1) noise = torch.rand((1, NOISE_SIZE)) * 2 - 1 encoded_x = gmm_net(Variable(noise)) y = encoder_net.decode(encoded_x) plt.imshow(y.detach().squeeze().numpy().reshape(28, 28)) ax.get_xaxis().set_visible(False) ax.get_yaxis().set_visible(False) plt.show()
horse_x = x[horse_indices] print(np.shape(horse_x)) # (5000, 3072) input_dim = np.shape(horse_x)[1] hidden_dim = 100 ae = Autoencoder(input_dim, hidden_dim) ae.train(horse_x) test_data = unpickle('./cifar-10-batches-py/test_batch') test_x = grayscale(test_data['data']) test_labels = np.array(test_data['labels']) encoding = ae.classify(test_x, test_labels) encoding = np.matrix(encoding) from matplotlib import pyplot as plt # encoding = np.matrix(np.random.choice([0, 1], size=(hidden_dim,))) original_img = np.reshape(test_x[7, :], (32, 32)) plt.imshow(original_img, cmap='Greys_r') plt.show() print(np.size(encoding)) while (True): img = ae.decode(encoding) plt.imshow(img, cmap='Greys_r') plt.show() rand_idx = np.random.randint(np.size(encoding)) encoding[0, rand_idx] = np.random.randint(2)
SAMPLE_SIZE = 400 indices = [int(i / SAMPLE_SIZE * len(dataset)) for i in range(SAMPLE_SIZE)] dataset.hashes = [dataset.hashes[i] for i in indices] data_loader = DataLoader(dataset, batch_size=1, shuffle=True, num_workers=4) autoencoder = Autoencoder(is_variational=USE_VARIATIONAL_AUTOENCODER) autoencoder.load_state_dict(torch.load(AUTOENCODER_FILENAME)) autoencoder.eval() STEPS = 5 with torch.no_grad(): for sample in tqdm(data_loader): image, hash = sample hash = hash[0] latent_code = autoencoder.encode(image.to(device)).unsqueeze(0) result = torch.zeros((3, 128, 128 * (STEPS + 1))) result[:, :, :128] = image.cpu() for i in range(STEPS): output = autoencoder.decode(latent_code * (1.0 - i / (STEPS - 1))) result[:, :, 128 * (i + 1):128 * (i + 2)] = output result = (torch.clamp(result, 0, 1).numpy() * 255).astype( np.uint8).transpose((1, 2, 0)) io.imsave('data/test/{:s}.jpg'.format(hash), result, quality=99)