def main(): # load MNIST images images, labels = dataset.load_test_images() # config config = aae.config num_scatter = len(images) x, _, label_ids = dataset.sample_labeled_data(images, labels, num_scatter, config.ndim_x, config.ndim_y) z = aae.to_numpy(aae.encode_x_z(x, test=True)) visualizer.plot_labeled_z(z, label_ids, dir=args.plot_dir)
def main(): # load MNIST images images, labels = dataset.load_test_images() # config config = aae.config # settings num_analogies = 10 pylab.gray() # generate style vector z x = dataset.sample_unlabeled_data(images, num_analogies, config.ndim_x, binarize=False) z = aae.to_numpy(aae.encode_x_z(x)) # plot original image on the left for m in xrange(num_analogies): pylab.subplot(num_analogies, config.ndim_y + 2, m * 12 + 1) pylab.imshow(x[m].reshape((28, 28)), interpolation="none") pylab.axis("off") all_y = np.identity(config.ndim_y, dtype=np.float32) for m in xrange(num_analogies): # copy z as many as the number of classes fixed_z = np.repeat(z[m].reshape(1, -1), config.ndim_y, axis=0) gen_x = aae.to_numpy(aae.decode_yz_x(all_y, fixed_z)) # plot images generated from each label for n in xrange(config.ndim_y): pylab.subplot(num_analogies, config.ndim_y + 2, m * 12 + 3 + n) pylab.imshow(gen_x[n].reshape((28, 28)), interpolation="none") pylab.axis("off") fig = pylab.gcf() fig.set_size_inches(num_analogies, config.ndim_y) pylab.savefig("{}/analogy.png".format(args.plot_dir))
def main(): # load MNIST images train_images, train_labels = dataset.load_train_images() # config config = aae.config # settings # _l -> labeled # _u -> unlabeled max_epoch = 1000 num_trains_per_epoch = 5000 batchsize = 100 alpha = 1 # seed np.random.seed(args.seed) if args.gpu_device != -1: cuda.cupy.random.seed(args.seed) # classification # 0 -> true sample # 1 -> generated sample class_true = aae.to_variable(np.zeros(batchsize, dtype=np.int32)) class_fake = aae.to_variable(np.ones(batchsize, dtype=np.int32)) # training progress = Progress() for epoch in xrange(1, max_epoch): progress.start_epoch(epoch, max_epoch) sum_loss_reconstruction = 0 sum_loss_discriminator = 0 sum_loss_generator = 0 for t in xrange(num_trains_per_epoch): # sample from data distribution images_l, label_onehot_l, label_ids_l = dataset.sample_labeled_data( train_images, train_labels, batchsize) # reconstruction phase z_l = aae.encode_x_z(images_l) reconstruction_l = aae.decode_yz_x(label_onehot_l, z_l) loss_reconstruction = F.mean_squared_error( aae.to_variable(images_l), reconstruction_l) aae.backprop_generator(loss_reconstruction) aae.backprop_decoder(loss_reconstruction) # adversarial phase images_l = dataset.sample_labeled_data(train_images, train_labels, batchsize)[0] z_fake_l = aae.encode_x_z(images_l) z_true_l = sampler.gaussian(batchsize, config.ndim_z, mean=0, var=1) dz_true = aae.discriminate_z(z_true_l, apply_softmax=False) dz_fake = aae.discriminate_z(z_fake_l, apply_softmax=False) loss_discriminator = F.softmax_cross_entropy( dz_true, class_true) + F.softmax_cross_entropy( dz_fake, class_fake) aae.backprop_discriminator(loss_discriminator) # adversarial phase images_l = dataset.sample_labeled_data(train_images, train_labels, batchsize)[0] z_fake_l = aae.encode_x_z(images_l) dz_fake = aae.discriminate_z(z_fake_l, apply_softmax=False) loss_generator = F.softmax_cross_entropy(dz_fake, class_true) aae.backprop_generator(loss_generator) sum_loss_reconstruction += float(loss_reconstruction.data) sum_loss_discriminator += float(loss_discriminator.data) sum_loss_generator += float(loss_generator.data) if t % 10 == 0: progress.show(t, num_trains_per_epoch, {}) aae.save(args.model_dir) progress.show( num_trains_per_epoch, num_trains_per_epoch, { "loss_r": sum_loss_reconstruction / num_trains_per_epoch, "loss_d": sum_loss_discriminator / num_trains_per_epoch, "loss_g": sum_loss_generator / num_trains_per_epoch, })
def main(): # load MNIST images images, labels = dataset.load_train_images() # config config = aae.config # settings # _l -> labeled # _u -> unlabeled max_epoch = 1000 num_trains_per_epoch = 5000 batchsize_l = 100 batchsize_u = 100 alpha = 1 # seed np.random.seed(args.seed) if args.gpu_device != -1: cuda.cupy.random.seed(args.seed) # create semi-supervised split num_labeled_data = 10000 num_types_of_label = 11 # additional label corresponds to unlabeled data training_images_l, training_labels_l, training_images_u, _, _ = dataset.create_semisupervised( images, labels, 0, num_labeled_data, num_types_of_label) # classification # 0 -> true sample # 1 -> generated sample class_true = aae.to_variable(np.zeros(batchsize_u, dtype=np.int32)) class_fake = aae.to_variable(np.ones(batchsize_u, dtype=np.int32)) # training progress = Progress() for epoch in xrange(1, max_epoch): progress.start_epoch(epoch, max_epoch) sum_loss_reconstruction = 0 sum_loss_supervised = 0 sum_loss_discriminator = 0 sum_loss_generator = 0 for t in xrange(num_trains_per_epoch): # sample from data distribution images_l, label_onehot_l, label_ids_l = dataset.sample_labeled_data( training_images_l, training_labels_l, batchsize_l, ndim_y=num_types_of_label) images_u = dataset.sample_unlabeled_data(training_images_u, batchsize_u) # reconstruction phase z_u = aae.encode_x_z(images_u) reconstruction_u = aae.decode_z_x(z_u) loss_reconstruction = F.mean_squared_error( aae.to_variable(images_u), reconstruction_u) aae.backprop_generator(loss_reconstruction) aae.backprop_decoder(loss_reconstruction) # adversarial phase z_fake_u = aae.encode_x_z(images_u) z_fake_l = aae.encode_x_z(images_l) onehot = np.zeros((1, num_types_of_label), dtype=np.float32) onehot[0, -1] = 1 # turn on the extra class label_onehot_u = np.repeat(onehot, batchsize_u, axis=0) z_true_l = sampler.supervised_gaussian_mixture( batchsize_l, config.ndim_z, label_ids_l, num_types_of_label - 1) z_true_u = sampler.gaussian_mixture(batchsize_u, config.ndim_z, num_types_of_label - 1) dz_true_l = aae.discriminate_z(label_onehot_l, z_true_l, apply_softmax=False) dz_true_u = aae.discriminate_z(label_onehot_u, z_true_u, apply_softmax=False) dz_fake_l = aae.discriminate_z(label_onehot_l, z_fake_l, apply_softmax=False) dz_fake_u = aae.discriminate_z(label_onehot_u, z_fake_u, apply_softmax=False) loss_discriminator = F.softmax_cross_entropy( dz_true_l, class_true) + F.softmax_cross_entropy( dz_true_u, class_true) + F.softmax_cross_entropy( dz_fake_l, class_fake) + F.softmax_cross_entropy( dz_fake_u, class_fake) aae.backprop_discriminator(loss_discriminator) # adversarial phase z_fake_u = aae.encode_x_z(images_u) z_fake_l = aae.encode_x_z(images_l) dz_fake_l = aae.discriminate_z(label_onehot_l, z_fake_l, apply_softmax=False) dz_fake_u = aae.discriminate_z(label_onehot_u, z_fake_u, apply_softmax=False) loss_generator = F.softmax_cross_entropy( dz_fake_l, class_true) + F.softmax_cross_entropy( dz_fake_u, class_true) aae.backprop_generator(loss_generator) sum_loss_reconstruction += float(loss_reconstruction.data) sum_loss_discriminator += float(loss_discriminator.data) sum_loss_generator += float(loss_generator.data) if t % 10 == 0: progress.show(t, num_trains_per_epoch, {}) aae.save(args.model_dir) progress.show( num_trains_per_epoch, num_trains_per_epoch, { "loss_r": sum_loss_reconstruction / num_trains_per_epoch, "loss_d": sum_loss_discriminator / num_trains_per_epoch, "loss_g": sum_loss_generator / num_trains_per_epoch, })
def main(): images, labels = dataset.load_test_images() num_scatter = len(images) x, _, label_ids = dataset.sample_labeled_data(images, labels, num_scatter) z = aae.to_numpy(aae.encode_x_z(x, test=True)) plot.scatter_labeled_z(z, label_ids, dir=args.plot_dir)