def main(args): train, valid, _ = load_cifar() whiten, color = pca(train) feat = args.features or int(np.sqrt(4 * K)) e = theanets.Experiment( theanets.Autoencoder, layers=(K, feat ** 2, K), ) e.train(whiten(train), whiten(valid), input_noise=1) plot_layers([ color(e.network.find(1, 0).get_value().T).T, color(e.network.find('out', 0).get_value())], channels=3) plt.tight_layout() plt.show() valid = whiten(valid[:100]) plot_images(color(valid), 121, 'Sample data', channels=3) plot_images(color(e.network.predict(valid)), 122, 'Reconstructed data', channels=3) plt.tight_layout() plt.show()
def info(self, show_img=False, use_logging=True, smooth=True): if use_logging: self.logger.info("Size of:") self.logger.info("- Training-set:\t\t{}".format(self.num_train)) self.logger.info("- Validation-set:\t\t{}".format(self.num_val)) self.logger.info("- Test-set:\t\t{}".format(self.num_test)) self.logger.info("- image_size_flat: \t{}".format( self.img_size_flat)) self.logger.info("- image_size: \t\t{}".format(self.img_shape)) self.logger.info("- num_classes:\t\t{}".format(self.num_classes)) self.logger.info("- CIFAR-10 class names: {}".format( self.class_names)) else: print("Size of:") print("- Training-set:\t\t{}".format(self.num_train)) print("- Validation-set:\t{}".format(self.num_val)) print("- Test-set:\t\t{}".format(self.num_test)) print("- image_size_flat: \t{}".format(self.img_size_flat)) print("- image_size: \t\t{}".format(self.images_train[0].shape)) print("- num_classes:\t\t{}".format(self.num_classes)) print("- CIFAR-10 class names: {}".format(self.class_names)) if show_img: index = np.random.choice(self.num_test, size=9, replace=False) plot_images(images=self.x_test[index], cls_true=self.x_test_cls[index], dataset='cifar10', class_names=self.class_names, smooth=smooth)
def test_model(self): with torch.no_grad(): # self.G.eval() #FIXME: Why is .eval causing the image to be black/all_zero? start_time = time.time() basetitle = "Fake Image" for j in range(self.label_dim + 1): feat = (self.fixed_feats.clone()).int() if j: # Skip for first round to save original fake images. for i in range(feat.size(0)): feat[i, j - 1, :, :] ^= 1 fake = self.G(feat.float()).detach().cpu() fake = F.interpolate(fake, size=(2 * config.image_size, 2 * config.image_size), mode='nearest') result = vutils.make_grid(fake, padding=2, normalize=True) if j: title = basetitle + "\nToggled Feature : {}".format( self.test_loader.dataset.selected_attr[j - 1]) else: title = basetitle plot_images(title, result, self.args.run_id, j, mode='test') end_time = time.time() min_time = (end_time - start_time) // 60 sec_time = (end_time - start_time) - (min_time * 60) print('Test Completed. Time: %dm%ds' % (min_time, sec_time)) return result
def main(args): train, valid, _ = load_cifar() whiten, color = pca(train) feat = args.features or int(np.sqrt(4 * K)) e = theanets.Experiment( theanets.Autoencoder, layers=(K, feat**2, K), ) e.train(whiten(train), whiten(valid), input_noise=1) plot_layers([ color(e.network.find(1, 0).get_value().T).T, color(e.network.find('out', 0).get_value()) ], channels=3) plt.tight_layout() plt.show() valid = whiten(valid[:100]) plot_images(color(valid), 121, 'Sample data', channels=3) plot_images(color(e.network.predict(valid)), 122, 'Reconstructed data', channels=3) plt.tight_layout() plt.show()
def explain(args, dm, net): from interpret import Explainer model = net.load_from_checkpoint(checkpoint_path=args.ckpt_path, num_channel=dm.n_channels, num_class=dm.n_classes) dm.config["batch_size"] = 1 dm.prepare_data() dm.setup() print("Generating explanation using GradCam...") data = next(iter(dm.train_dataloader())) sample = utils.preprocess_signals(data["signal"]) label = data["label"].numpy().argmax() GradCamExplainer = Explainer("GradCam", model=model.network, feature_module=model.network[:9], target_layer_names=["8"]) cam_mask = GradCamExplainer.explain_instance(sample) utils.show_cam_on_image(sample=sample, mask=cam_mask, figure_path="./figures/gradcam.jpg") print("Generating explanation using LIME...") LimeExplainer = Explainer("LIME", model=model) explanation = LimeExplainer.explain_instance(data["signal"], labels=[label]) sample, mask = explanation.get_instance_and_mask(label) masked_sample = (sample * mask)[0].transpose() utils.plot_images(sample=masked_sample, figure_path="./figures/lime_label{}.jpg".format(label))
def train_loaders(batch_size, random_seed, augment=False, valid_size=0.2, shuffle=True, show_sample=False): """ Utility function for loading and returning train and valid multi-process iterators over the MNIST dataset. A sample 9x9 grid of the images can be optionally displayed. If using CUDA, num_workers should be set to 1 and pin_memory to True. """ train_transform, valid_transform = _transform(augment) # load the dataset train_dataset = MNIST(root='..\\data', train=True, download=True, transform=train_transform) valid_dataset = MNIST(root='..\\data', train=True, download=True, transform=valid_transform) num_train = len(train_dataset) indices = list(range(num_train)) split = int(np.floor(valid_size * num_train)) if shuffle: np.random.seed(random_seed) np.random.shuffle(indices) train_idx, valid_idx = indices[split:], indices[:split] train_sampler = SubsetRandomSampler(train_idx) valid_sampler = SubsetRandomSampler(valid_idx) train_loader = DataLoader(train_dataset, batch_size=batch_size, sampler=train_sampler) valid_loader = DataLoader(valid_dataset, batch_size=batch_size, sampler=valid_sampler) # visualize some images if show_sample: sample_loader = DataLoader(train_dataset, batch_size=9, shuffle=shuffle) data_iter = iter(sample_loader) images, labels = data_iter.next() X = images.numpy() plot_images(X, [int(i) for i in labels]) return train_loader, valid_loader
def display_samples(self, dataset_type): sample_loader = torch.utils.data.DataLoader( self.image_datasets[dataset_type], batch_size=9, shuffle=True, num_workers=cfg.CONST.NUM_WORKERS, pin_memory=cfg.CONST.PIN_MEMORY, ) data_iter = iter(sample_loader) images, labels = data_iter.next() X = images.numpy().transpose([0, 2, 3, 1]) plot_images(X, labels, self.label_names)
def main(perform_exp_n_times: int, iters_per_experiment: int, show_plots: bool): print('TensorFlow version %s' % str(tf.__version__)) print('PyTorch version %s' % str(torch.__version__)) # allowing soft growth of GPU in tensorflow gpus = tf.config.experimental.list_physical_devices('GPU') for gpu in gpus: tf.config.experimental.set_memory_growth(gpu, True) # create an image of circular masks g1 = create_circular_mask(21, 21, radius=4) g2 = create_circular_mask(21, 21, radius=5) g3 = create_circular_mask(21, 21, [3, 3], radius=3) image = np.stack([g1, g2, g3], axis=-1)[None] # create kernel to convolve image with gauss_kernel = np.stack([make_gaussian(5, 1)] * 3, axis=-1)[..., None] # plot image and kernel plot_images([image[0, ...], gauss_kernel[..., 0]], [ 'Image shape %s' % str(image.shape), 'Kernel shape %s' % str(gauss_kernel.shape) ], 'Image to process', show_plots) convolved_tf = cnn2d_tf(image, gauss_kernel) convolved_torch = cnn2d_torch(image, gauss_kernel) # plot tf and torch convolutions plot_images([convolved_tf[0, ..., 0], convolved_torch[0, ..., 0]], [ 'TF2 Conv shape %s' % str(convolved_tf.shape), 'PyTorch Conv shape %s' % str(convolved_torch.shape) ], 'Difference between convolutions %.2f' % np.mean(convolved_tf - convolved_torch), show_plots) delta_time_list = [] for i in range(perform_exp_n_times): start_time = time.time() for i in range(iters_per_experiment): cnn2d_tf(image, gauss_kernel) delta_time_list.append(time.time() - start_time) print("Time usage conv2d TF2: %s +/- %s" % (str(timer(np.mean(delta_time_list))), str(timer(np.std(delta_time_list), True)))) delta_time_list = [] for i in range(perform_exp_n_times): start_time = time.time() for i in range(iters_per_experiment): cnn2d_torch(image, gauss_kernel) delta_time_list.append(time.time() - start_time) print("Time usage conv2d PyTorch: %s +/- %s" % (str(timer(np.mean(delta_time_list))), str(timer(np.std(delta_time_list), True))))
def trans_attributes(vae, image_file_name, attribute_vectors_file, attrs): attribute_vectors = np.load(attribute_vectors_file).item() image = read_image('CelebA/img_align_celeba/' + image_file_name) image = np.expand_dims(image, axis=0) z = vae.get_layer('encoder').predict(image)[0] z_v = [z[0] + attribute_vectors[attr] for attr in attrs] z_v2 = [z[0] - attribute_vectors[attr] for attr in attrs] z_v.extend(z_v2) z_v = np.asarray(z_v, dtype=np.float32) modified_images = vae.get_layer('decoder').predict(z_v) images = np.append(modified_images, image, axis=0) plot_images(img_renorm(images))
def train_loaders(batch_size, random_seed, valid_size=0.2, shuffle=True, show_sample=False): train_transform, valid_transform = _transform() # load the dataset train_dataset = CIFAR10(root='..\\data', train=True, download=True, transform=train_transform) valid_dataset = CIFAR10(root='..\\data', train=True, download=True, transform=valid_transform) num_train = len(train_dataset) indices = list(range(num_train)) split = int(np.floor(valid_size * num_train)) if shuffle: np.random.seed(random_seed) np.random.shuffle(indices) train_idx, valid_idx = indices[split:], indices[:split] train_sampler = SubsetRandomSampler(train_idx) valid_sampler = SubsetRandomSampler(valid_idx) train_loader = DataLoader(train_dataset, batch_size=batch_size, sampler=train_sampler) valid_loader = DataLoader(valid_dataset, batch_size=batch_size, sampler=valid_sampler) # visualize some images if show_sample: sample_loader = DataLoader(train_dataset, batch_size=9, shuffle=shuffle) data_iter = iter(sample_loader) images, labels = data_iter.next() X = images.numpy() plot_images(X, [classes[label] for label in labels]) return train_loader, valid_loader
def train_sampler(self, z, _labels=None, train_time=None, samp_dir='samp'): if not train_time: train_time = time.strftime('%Y-%m-%d-%H-%M-%S', time.localtime(time.time())) samp_name = 'g_{0}.png'.format(train_time) else: epoch = train_time[0] batch = train_time[1] samp_name = 'g_{0}_{1}.png'.format( str(epoch + 1).zfill(3), str(batch).zfill(4)) samples, decoded = self.sess.run( [self.G, self.decoder], feed_dict={ self.z: z, self.y: _labels, self.encoded: _labels, self.is_training: False }) fig = utils.plot_images(samples, image_dim=self.data.h_dim, n_categories=self.data.n_categories, labels=decoded) file_name = os.path.join(samp_dir, samp_name) plt.savefig(file_name, bbox_inches='tight') plt.close(fig) print("Sample: {file_name}".format(file_name=file_name))
def stereo_rectify_test(): image_name = 'test/180602_015140124' dataset = data.ApolloScape(scale=1.0, use_stereo=True) images = OrderedDict([]) image_size = dataset._data_config['image_size'] for cam_name in ['Camera_5', 'Camera_6']: images[cam_name] = cv2.imread('%s_%s.jpg' % (image_name, cam_name)) images[cam_name] = cv2.resize(images[cam_name], (image_size[1], image_size[0])) images[cam_name] = dataset.stereo_rectify(images[cam_name], cam_name) for cam_name in ['Camera_5', 'Camera_6']: images[cam_name + '_crop'] = uts.crop_image( images[cam_name], dataset._data_config['stereo_crop']) uts.plot_images(images, layout=[2, 2])
def main(args): train, valid, _ = load_mnist() e = theanets.Experiment(theanets.Autoencoder, layers=(784, args.features**2, 784)) e.train(train, valid) plot_layers([e.network.find(1, 0), e.network.find(2, 0)]) plt.tight_layout() plt.show() v = valid[:100] plot_images(v, 121, 'Sample data') plot_images(e.network.predict(v), 122, 'Reconstructed data') plt.tight_layout() plt.show()
def display_weights(self, img_weights): (X_train, y, y_hat, w) = img_weights y_hat = self.predict(y_hat) w_ordered_inds = np.argsort(-w.float().numpy()) max_weights = w_ordered_inds[:5] min_weights = w_ordered_inds[-5:] inds = np.concatenate((max_weights, min_weights)) imgs = np.squeeze(X_train.numpy()[inds, :, :, :].transpose( [0, 2, 3, 1]), axis=3) cls_pred = np.array(self.classes)[y_hat.int().numpy()[inds]] cls_true = np.array(self.classes)[y.int().numpy()[inds]] weights = w.numpy()[inds] plot_images(imgs, cls_true, cls_pred=cls_pred, weight=weights)
def save_images(self, X_source, X_target, images_dir, nb_images=64): images_dir = os.path.join(self.output_dir, "generated-images") if not os.path.exists(images_dir): os.mkdir(images_dir) X_s2s = unnormalize(self.sess.run(self.G_s2s, feed_dict={self.ipt_source: X_source[:nb_images]})) X_t2t = unnormalize(self.sess.run(self.G_t2t, feed_dict={self.ipt_target: X_target[:nb_images]})) X_s2t = unnormalize(self.sess.run(self.G_s2t, feed_dict={self.ipt_source: X_source[:nb_images]})) X_t2s = unnormalize(self.sess.run(self.G_t2s, feed_dict={self.ipt_target: X_target[:nb_images]})) X_cycle_s2s = unnormalize(self.sess.run(self.G_cycle_s2s, feed_dict={self.ipt_source: X_source[:nb_images]})) X_cycle_t2t = unnormalize(self.sess.run(self.G_cycle_t2t, feed_dict={self.ipt_target: X_target[:nb_images]})) Y_source_predict = self.sess.run(self.D_source_predict, feed_dict={self.ipt_source: X_source[:nb_images]}) Y_target_predict = self.sess.run(self.DG_t2s_predict, feed_dict={self.ipt_target: X_target[:nb_images]}) for index in range(len(X_s2s)): plot_images(index, X_source, X_target, X_s2s, X_t2t, X_s2t, X_t2s, X_cycle_s2s, X_cycle_t2t, Y_source_predict, Y_target_predict) print("Saving 'generated-images/iter_{:05d}_image_{:02d}.png'".format(self.iter, index), end="\r") plt.savefig(os.path.join(images_dir, "iter_{:05d}_image_{:02d}.png".format(self.iter, index)))
def main(args): train, valid, _ = load_mnist() e = theanets.Experiment( theanets.Autoencoder, layers=(784, args.features ** 2, 784)) e.train(train, valid, min_improvement=0.1) plot_layers([e.network.find('hid1', 'w'), e.network.find('out', 'w')]) plt.tight_layout() plt.show() v = valid[:100] plot_images(v, 121, 'Sample data') plot_images(e.network.predict(v), 122, 'Reconstructed data') plt.tight_layout() plt.show()
def main(args): train, valid, _ = load_cifar() e = theanets.Experiment( theanets.Autoencoder, layers=(3072, args.features ** 2, 3072)) e.train(train, valid) plot_layers(e.network.weights, channels=3) plt.tight_layout() plt.show() valid = valid[:100] plot_images(valid, 121, 'Sample data', channels=3) plot_images(e.network.predict(valid), 122, 'Reconstructed data', channels=3) plt.tight_layout() plt.show()
def main(args): train, valid, _ = load_mnist() e = theanets.Experiment( theanets.Autoencoder, layers=(784, args.features ** 2, 784)) e.train(train, valid) plot_layers(e.network.weights) plt.tight_layout() plt.show() v = valid[:100] plot_images(v, 121, 'Sample data') plot_images(e.network.predict(v), 122, 'Reconstructed data') plt.tight_layout() plt.show()
def merge_2(vae, image_file_name1, image_file_name2): image1 = read_image(image_file_name1) image1 = np.expand_dims(image1, axis=0) z1 = vae.get_layer('encoder').predict(image1)[0] image2 = read_image(image_file_name2) image2 = np.expand_dims(image2, axis=0) z2 = vae.get_layer('encoder').predict(image2)[0] z_v = [] for i in range(9): z = (z2[0] * i + z1[0] * (8 - i)) / 8 z_v.append(z) z_v = np.asarray(z_v, dtype=np.float32) images = vae.get_layer('decoder').predict(z_v) plot_images(img_renorm(images))
def save_plot(examples, epoch, n=10): """Save sample images to file """ fig = utils.plot_images(examples, grid_shape=(n, n), rescale=True) # generate plot # Save plot to file img_fname = utils.name_inter_img_file(epoch) plt.savefig(img_fname) plt.close()
def main(args): train, valid, _ = load_cifar() e = theanets.Experiment(theanets.Autoencoder, layers=(3072, args.features**2, 3072)) e.train(train, valid) plot_layers(e.network.weights, channels=3) plt.tight_layout() plt.show() valid = valid[:100] plot_images(valid, 121, 'Sample data', channels=3) plot_images(e.network.predict(valid), 122, 'Reconstructed data', channels=3) plt.tight_layout() plt.show()
def finetuning(self, task, tune_epochs, number, numbers, name): train_x = task.to(self.device) test_x = task.to(self.device) model_copy = copy.deepcopy(self.model) weights = list(model_copy.parameters()) for epoch in range(tune_epochs): if epoch==0: x_sample, z_mu, z_var = model_copy(train_x, weights) train_loss, BCE, KLD = utils.loss_function(x_sample, train_x, z_mu, z_var, self.beta) grad = torch.autograd.grad(train_loss, weights) # gradient clipping for w, g in zip(weights, grad): w.grad = g torch.nn.utils.clip_grad_norm_(weights, self.clip_value) temp_weights = [w - self.inner_lr * g for w, g in zip(weights, grad)] else: x_sample, z_mu, z_var = model_copy(train_x, temp_weights) train_loss, BCE, KLD = utils.loss_function(x_sample, train_x, z_mu, z_var, self.beta) grad = torch.autograd.grad(train_loss, temp_weights) # gradient clipping for w, g in zip(temp_weights, grad): w.grad = g torch.nn.utils.clip_grad_norm_(temp_weights, self.clip_value) temp_weights = [w - self.inner_lr * g for w, g in zip(temp_weights, grad)] with open("results/logs//tune_log.txt", 'a+') as f: print("inner iteration: {}, Recon_loss = {}, KLD_loss = {}".format(epoch, BCE, KLD), file=f) x_sample, z_mu, z_var = model_copy(test_x, temp_weights) task_loss, BCE, KLD = utils.loss_function(x_sample, test_x, z_mu, z_var, self.beta) if number in numbers: utils.plot_images(model_copy, temp_weights, test_x, BCE, name=name) with open('transfer/tune_log.txt', 'a+') as f: print('Task test loss: {}'.format(task_loss), file=f) return task_loss.item()
def main(args): # load up the MNIST digit dataset. train, valid, _ = load_mnist() net = theanets.Autoencoder([784, args.features ** 2, 784]) net.train(train, valid, input_noise=0.1, weight_l2=0.0001, algo='rmsprop', momentum=0.9, min_improvement=0.1) plot_layers([net.find('hid1', 'w'), net.find('out', 'w')]) plt.tight_layout() plt.show() v = valid[:100] plot_images(v, 121, 'Sample data') plot_images(net.predict(v), 122, 'Reconstructed data') plt.tight_layout() plt.show()
def main(features): train, valid, _ = load_cifar() whiten, color = pca(train[0]) feat = features or int(np.sqrt(2 * K)) n = theanets.Autoencoder([K, feat ** 2, K]) n.train(whiten(train), whiten(valid), input_noise=1, train_batches=313) plot_layers([ color(n.find('hid1', 'w').get_value().T).T, color(n.find('out', 'w').get_value())], channels=3) plt.tight_layout() plt.show() valid = whiten(valid[:100]) plot_images(color(valid), 121, 'Sample data', channels=3) plot_images(color(n.predict(valid)), 122, 'Reconstructed data', channels=3) plt.tight_layout() plt.show()
def info(self, show_img=False, use_logging=True): if use_logging: logger.info("Size of:") logger.info("- Training-set:\t\t{}".format(self.num_train)) logger.info("- Validataion-set:\t\t{}".format(self.num_val)) logger.info("- Test-set:\t\t{}".format(self.num_test)) img_size_flat = self.img_size_flat logger.info("- img_size_flat:\t{}".format(img_size_flat)) img_shape = self.img_shape logger.info("- img_shape:\t\t{}".format(img_shape)) num_classes = self.num_classes logger.info("- num_classes:\t\t{}".format(num_classes)) # Plot the images using our helper-function above. if show_img: index = np.random.choice(self.num_test, size=9, replace=False) plot_images(images=self.x_test[index], cls_true=self.y_test_cls[index], smooth=False)
def test_gan(self, epoch): for part in ('train', 'val', 'test'): ds = dataset.load_celeba('CelebA', batch_size, part=part, consumer='translator', smallbatch=10) element = ds.make_one_shot_iterator().get_next() sess = K.get_session() imgs, labels = sess.run(element) labels = 1 - labels print(labels) rec_imgs = self.generator.predict([imgs, labels]) src_real, _, cls_real = self.discriminator.predict(imgs) src_fake, _, cls_fake = self.discriminator.predict(rec_imgs) for r, f, sr, cr, sf, cf in zip(imgs, rec_imgs, src_real, cls_real, src_fake, cls_fake): plot_images([img_renorm(r), img_renorm(f)]) print('real: ' + str(sr) + ' cls ' + str(cr) + ' fake: ' + str(sf) + ' cls ' + str(cf))
def main(args): # load up the MNIST digit dataset. train, valid, _ = load_mnist() net = theanets.Autoencoder([784, args.features**2, 784]) net.train(train, valid, input_noise=0.1, weight_l2=0.0001, algo='rmsprop', momentum=0.9, min_improvement=0.1) plot_layers([net.find('hid1', 'w'), net.find('out', 'w')]) plt.tight_layout() plt.show() v = valid[:100] plot_images(v, 121, 'Sample data') plot_images(net.predict(v), 122, 'Reconstructed data') plt.tight_layout() plt.show()
def main(features): train, valid, _ = load_cifar() whiten, color = pca(train[0]) feat = features or int(np.sqrt(2 * K)) n = theanets.Autoencoder([K, feat**2, K]) n.train(whiten(train), whiten(valid), input_noise=1, train_batches=313) plot_layers([ color(n.find('hid1', 'w').get_value().T).T, color(n.find('out', 'w').get_value()) ], channels=3) plt.tight_layout() plt.show() valid = whiten(valid[:100]) plot_images(color(valid), 121, 'Sample data', channels=3) plot_images(color(n.predict(valid)), 122, 'Reconstructed data', channels=3) plt.tight_layout() plt.show()
def plot_representative_img_set(img_set, label=None, annot_avg=True): """Plots selected representative images - assumes average is present """ fig = utils.plot_images(img_set, grid_shape=(1, len(img_set)), rescale=True, figsize=(12, 3)) axes = fig.axes if annot_avg: axes[-1].text(0.5, 0, 'avg', transform=axes[-1].transAxes, ha='center', va='top') if label is not None: axes[0].text(0, 0.5, label, transform=axes[0].transAxes, ha='right', va='center', rotation=90) return fig
(28, 28, 1)))) return Dataset.batch(BATCH_SIZE) def generator(LATENT_DIM): while True: (x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data() images = (np.expand_dims(x_train, axis=-1)) / 255. images = images.astype(np.float32) noise = np.random.randn(60000, LATENT_DIM).reshape(60000, LATENT_DIM) idx = np.random.permutation(60000) noise = noise[idx] images = images[idx] for i in range(60000): yield (noise[i], images[i]) for loop in range(0, 200): gan_estimator.train( lambda: batched_dataset(BATCH_SIZE, LATENT_DIM, generator), steps=ITER) result = gan_estimator.predict( lambda: batched_dataset(BATCH_SIZE, LATENT_DIM, generator)) images = [] for i, image in enumerate(result): images.append(image * 255.) if i == 15: images = np.array(images) break plot_images(images, fname=dir + "/images_%.3i.png" % loop)
def color(z): return np.dot(z, np.dot(np.diag(vals), vecs.T)) # now train our model on the whitened dataset. N = 16 e = theanets.Experiment( RICA, layers=(K, N * N, K), activation='linear', hidden_l1=0.2, no_learn_biases=True, tied_weights=True, train_batches=100, weight_inverse=0.01, ) e.run(whiten(train), whiten(valid)) # color the network weights so they are viewable as digits. plot_layers([color(e.network.weights[0].get_value().T).T], tied_weights=True) plt.tight_layout() plt.show() plot_images(valid[:N * N], 121, 'Sample data') plot_images(color(e.network.predict(whiten(valid[:N * N]))), 122, 'Reconstructed data') plt.tight_layout() plt.show()
def run_experiment(params, dropout, bn): tf.reset_default_graph() figname_suffix = "a_" dropout_value = 1.0 if dropout: dropout_value = params['dropout'] figname_suffix += "dropout" if bn: figname_suffix += "bn" train_step, \ cost, \ accuracy, \ y_pred, \ y_pred_cls, \ y_true_cls, \ placeholders = create_network( params['img_size'], params['num_channels'], params['num_classes'], params['shape1'], params['shape2'], params['num_fc_layer1_output'], params['num_fc_layer2_output'], params['learning_rate'], bn ) saver = tf.train.Saver() if not os.path.exists(params['save_dir']): os.makedirs(params['save_dir']) (train_acc, cost, test_acc) = train_network(params['data'], train_step, cost, accuracy, params['num_iterations'], params['train_batch_size'], dropout_value, placeholders, saver, params['save_dir'], params['plot_dir'], params['log_dir'], params['display_step'], figname_suffix) cls_true, cls_pred, acc, y_pred_probs, x_all, y_all\ = test_network(params['test_batch_size'], placeholders, 1.0, saver, params['save_dir'], accuracy, y_pred, y_pred_cls, y_true_cls, params['data']) plot_confusion_matrix(cls_true=cls_true, cls_pred=cls_pred, output_dir=plot_dir, fig_name="confusion_matrix" + "_" + figname_suffix) plot_images(output_dir=plot_dir, fig_name="images" + "_" + figname_suffix, img_shape=(params['img_size'], params['img_size'], params['num_channels']), images=x_all, cls_true=y_all, cls_pred=cls_pred, prob_pred=y_pred_probs, logits=None, y_pred=None) return train_acc, cost, test_acc, figname_suffix, acc
#!/usr/bin/env python import matplotlib.pyplot as plt import theanets from utils import load_mnist, plot_layers, plot_images train, valid, _ = load_mnist() e = theanets.Experiment( theanets.Autoencoder, layers=(784, 256, 100, 64, ('tied', 100), ('tied', 256), ('tied', 784)), ) e.train(train, valid, algorithm='layerwise', patience=1, min_improvement=0.05, train_batches=100) e.train(train, valid, min_improvment=0.01, train_batches=100) plot_layers([e.network.find(i, 'w') for i in (1, 2, 3)], tied_weights=True) plt.tight_layout() plt.show() valid = valid[:16*16] plot_images(valid, 121, 'Sample data') plot_images(e.network.predict(valid), 122, 'Reconstructed data') plt.tight_layout() plt.show()
correct = 0 total = 0 plot_img, plot_lab = [], [] with torch.no_grad(): for (images, labels) in val_loader: images = images.to(device) labels = labels.to(device) outputs = net(images).to(device) _, predicted = torch.max(outputs.data, 1) total += labels.size(0) correct += (predicted == labels).sum().item() while len(plot_img) < 9: plot_img = images[0:9].cpu().numpy() plot_lab = predicted[0:9].data plot_images(plot_img, plot_lab) print('Accuracy of the network on the %d test images: %f %%' % (total, 100 * correct / total)) # plotting plt.plot(losses) # save model to path PATH = '../pretrained/{}_{}.pth'.format(dataSet, modelName) torch.save(net.state_dict(), PATH) print(f"Training time: {time.time() - start_ts}s") plt.savefig("../plots/{}_{}_losses".format(dataSet, modelName)) plt.title(dataSet + "_" + modelName)
nsamples = x_train.shape[0] images = 2 * (x_train / 255. - 0.5) images = images.astype(np.float32) noise = np.random.randn(nsamples, LATENT_DIM).reshape(nsamples, LATENT_DIM) idx = np.random.permutation(nsamples) noise = noise[idx] images = images[idx] for i in range(nsamples): yield (noise[i], images[i]) import itertools images = np.array(list(itertools.islice(generator(LATENT_DIM), 16)))[:, 1] images = 255. * (images / 2. + 0.5) plot_images(images, fname=dir + "/original_images.png") for loop in range(0, 600): gan_estimator.train( lambda: batched_dataset(BATCH_SIZE, LATENT_DIM, generator), steps=ITER) result = gan_estimator.predict( lambda: batched_dataset(BATCH_SIZE, LATENT_DIM, generator)) images = [] for i, image in enumerate(result): images.append(255. * (image / 2. + 0.5)) if i == 15: images = np.array(images) break plot_images(images, fname=dir + "/images_%.3i.png" % loop)
def whiten(x): return np.dot(x, np.dot(vecs, np.diag(1. / vals))) def color(z): return np.dot(z, np.dot(np.diag(vals), vecs.T)) # now train our model on the whitened dataset. N = 20 net = RICA([K, (N * N, 'linear'), (K, 'tied')]) net.train(whiten(train), whiten(valid), hidden_l1=0.001, weight_inverse=1e-6, train_batches=300, monitors={'hid1:out': (-0.9, -0.1, 0.1, 0.9)}) # color the network weights so they are viewable as digits. plot_layers([color(net.find('hid1', 'w').get_value().T).T], tied_weights=True) plt.tight_layout() plt.show() plot_images(valid[:N*N], 121, 'Sample data') plot_images(color(net.predict(whiten(valid[:N*N]))), 122, 'Reconstructed data') plt.tight_layout() plt.show()
# Load validation data X_valid = dataset['X_valid'].value y_valid = dataset['y_valid'].value # Load test data X_test = dataset['X_test'].value y_test = dataset['y_test'].value # pick randomly 16 images from training data random_choices = np.random.choice(np.arange(X_train.shape[0]), size=16, replace=False) X_sampled = X_train[random_choices] y_samples = y_train[random_choices] # start plotting plt.figure() _ = plot_images(X_sampled) plt.show() print(y_samples) # start plotting plt.figure() plt.subplot(1, 3, 1) plt.title("Training set statistics") plt.hist(y_train, bins=10) plt.subplot(1, 3, 2) plt.title("Validation set statistics") plt.hist(y_valid, bins=10) _ = plt.subplot(1, 3, 3) plt.title("Test set statistics")
def get_train_valid_loader(data_dir, batch_size, augment, random_seed, valid_size=0.1, shuffle=True, show_sample=False, num_workers=4, pin_memory=False): """ Utility function for loading and returning train and valid multi-process iterators over the CIFAR-10 dataset. A sample 9x9 grid of the images can be optionally displayed. If using CUDA, num_workers should be set to 1 and pin_memory to True. Params ------ - data_dir: path directory to the dataset. - batch_size: how many samples per batch to load. - augment: whether to apply the data augmentation scheme mentioned in the paper. Only applied on the train split. - random_seed: fix seed for reproducibility. - valid_size: percentage split of the training set used for the validation set. Should be a float in the range [0, 1]. - shuffle: whether to shuffle the train/validation indices. - show_sample: plot 9x9 sample grid of the dataset. - num_workers: number of subprocesses to use when loading the dataset. - pin_memory: whether to copy tensors into CUDA pinned memory. Set it to True if using GPU. Returns ------- - train_loader: training set iterator. - valid_loader: validation set iterator. """ error_msg = "[!] valid_size should be in the range [0, 1]." assert ((valid_size >= 0) and (valid_size <= 1)), error_msg normalize = transforms.Normalize( mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225], ) # define transforms valid_transform = transforms.Compose([ transforms.ToTensor(), normalize, ]) if augment: train_transform = transforms.Compose([ transforms.RandomCrop(32, padding=4), transforms.RandomHorizontalFlip(), transforms.ToTensor(), normalize, ]) else: train_transform = transforms.Compose([ transforms.ToTensor(), normalize, ]) # load the dataset train_dataset = datasets.CIFAR10( root=data_dir, train=True, download=True, transform=train_transform, ) valid_dataset = datasets.CIFAR10( root=data_dir, train=True, download=True, transform=valid_transform, ) num_train = len(train_dataset) indices = list(range(num_train)) split = int(np.floor(valid_size * num_train)) if shuffle: np.random.seed(random_seed) np.random.shuffle(indices) train_idx, valid_idx = indices[split:], indices[:split] train_sampler = SubsetRandomSampler(train_idx) valid_sampler = SubsetRandomSampler(valid_idx) train_loader = torch.utils.data.DataLoader( train_dataset, batch_size=batch_size, sampler=train_sampler, num_workers=num_workers, pin_memory=pin_memory, ) valid_loader = torch.utils.data.DataLoader( valid_dataset, batch_size=batch_size, sampler=valid_sampler, num_workers=num_workers, pin_memory=pin_memory, ) # visualize some images if show_sample: sample_loader = torch.utils.data.DataLoader( train_dataset, batch_size=128, shuffle=shuffle, num_workers=num_workers, pin_memory=pin_memory, ) data_iter = iter(sample_loader) images, labels = data_iter.next() X = images.numpy().transpose([0, 2, 3, 1]) plot_images(X, labels) return (train_loader, valid_loader)