def get_images_and_latents(dir, dataset, batch): ae, ds = utils.load_ae(dir, dataset, batch, all_aes.ALL_AES, return_dataset=True) with utils.HookReport.disable(): ae.eval_mode() if dataset == 'celeba32': images, latents, labels, shape = get_latents_and_labels(ae.eval_sess, ae.eval_ops, ds.test, batches=batch) elif dataset == 'lines2': images, latents, labels, shape = get_latents_and_labels(ae.eval_sess, ae.eval_ops, ds.test, batches=batch) else: images, latents, labels, shape = get_latents_and_labels(ae.eval_sess, ae.eval_ops, ds.train_once, batches=None) return images, latents, labels, shape
def main(argv): del argv # Unused. batch = FLAGS.batch dataset = data.get_dataset(FLAGS.dataset, dict(batch_size=batch)) ae, ae_model = utils.load_ae(FLAGS.ae_dir, FLAGS.dataset, FLAGS.batch, all_aes.ALL_AES) with utils.HookReport.disable(): ae.eval_mode() model = XLatentAE(dataset, FLAGS.train_dir) model.train_dir = os.path.join(model.train_dir, ae_model) model.ae = ae model.train()
def main(argv): del argv # Unused. ae, ds = utils.load_ae(FLAGS.ae_dir, FLAGS.dataset, FLAGS.batch, all_aes.ALL_AES, return_dataset=True) with utils.HookReport.disable(): ae.eval_mode() # Convert all test samples to latents and get the labels test_latents, test_labels = get_latents_and_labels(ae.eval_sess, ae.eval_ops, ds.test) print('Shape of test_labels = {}'.format(np.shape(test_labels))) print('Shape of test_latents = {}'.format(np.shape(test_latents))) train_latents, train_labels = get_latents_and_labels(ae.eval_sess, ae.eval_ops, ds.train_once) print('Shape of train_labels = {}'.format(np.shape(train_labels))) print('Shape of train_latents = {}'.format(np.shape(train_latents))) with open('mnist256_features.pkl', 'wb') as file: pkl.dump({'test_labels': test_labels, 'test_latents': test_latents, 'train_labels': train_labels, 'train_latents': train_latents}, file) if not FLAGS.use_svd: acc = cluster(train_latents, train_labels, test_latents, test_labels) print('classification acc = {}'.format(acc)) return if 0: # use PCA print('PCA') pca = sklearn.decomposition.PCA(train_latents.shape[1], whiten=True) pca.fit(train_latents) train_latents = pca.transform(train_latents) test_latents = pca.transform(test_latents) else: print('SVD') mean = train_latents.mean(axis=0) train_latents -= mean test_latents -= mean s, vt = la.svd(train_latents, full_matrices=False)[-2:] train_latents = (train_latents.dot(vt.T) / (s + 1e-5)) test_latents = (test_latents.dot(vt.T) / (s + 1e-5)) rank = train_latents.shape[1] for x in range(FLAGS.n_try): acc = cluster(train_latents[:, :rank].copy(), train_labels[:, :rank].copy(), test_latents[:, :rank].copy(), test_labels[:, :rank].copy()) print('Rank %3d Inits %4d Accuracy %.2f' % (rank, FLAGS.n_init, 100 * acc))
def main(argv): del argv # Unused. batch = FLAGS.batch dataset = data.get_dataset(FLAGS.dataset, dict(batch_size=batch)) ae, ae_model = utils.load_ae(FLAGS.ae_dir, FLAGS.dataset, FLAGS.batch, all_aes.ALL_AES) with utils.HookReport.disable(): ae.eval_mode() model = XLatentAE( dataset, FLAGS.train_dir) model.train_dir = os.path.join(model.train_dir, ae_model) model.ae = ae model.train()
def main(argv): del argv # Unused. ae, ds = utils.load_ae(FLAGS.ae_dir, FLAGS.dataset, FLAGS.batch, all_aes.ALL_AES, return_dataset=True) with utils.HookReport.disable(): ae.eval_mode() # Convert all test samples to latents and get the labels test_latents, test_labels = get_latents_and_labels(ae.eval_sess, ae.eval_ops, ds.test) print('Shape of test_labels = {}'.format(np.shape(test_labels))) print('Shape of test_latents = {}'.format(np.shape(test_latents))) train_latents, train_labels = get_latents_and_labels(ae.eval_sess, ae.eval_ops, ds.train_once, 60000) print('Shape of train_labels = {}'.format(np.shape(train_labels))) print('Shape of train_latents = {}'.format(np.shape(train_latents))) if not FLAGS.use_svd: acc = cluster(train_latents, train_labels, test_latents, test_labels) print('classification acc = {}'.format(acc)) return if 0: # use PCA print('PCA') pca = sklearn.decomposition.PCA(train_latents.shape[1], whiten=True) pca.fit(train_latents) train_latents = pca.transform(train_latents) test_latents = pca.transform(test_latents) else: print('SVD') mean = train_latents.mean(axis=0) train_latents -= mean test_latents -= mean s, vt = la.svd(train_latents, full_matrices=False)[-2:] print('SVD Sigma', s) train_latents = (train_latents.dot(vt.T) / (s + 1e-5)) test_latents = (test_latents.dot(vt.T) / (s + 1e-5)) rank = train_latents.shape[1] for x in range(FLAGS.n_try): acc = cluster(train_latents[:, :rank].copy(), train_labels[:, :rank].copy(), test_latents[:, :rank].copy(), test_labels[:, :rank].copy()) print('Rank %3d Inits %4d Accuracy %.2f' % (rank, FLAGS.n_init, 100 * acc))
def main(argv): del argv # Unused. ae, ds = utils.load_ae(FLAGS.ae_dir, FLAGS.dataset, FLAGS.batch, all_aes.ALL_AES, return_dataset=True) with utils.HookReport.disable(): ae.eval_mode() # Convert all test samples to latents and get the labels val_images, val_latents, val_labels = get_images_and_latents_and_labels( ae.eval_sess, ae.eval_ops, ds.val) print('Shape of val_labels = {}'.format(np.shape(val_labels))) print('Shape of val_latents = {}'.format(np.shape(val_latents))) test_images, test_latents, test_labels = get_images_and_latents_and_labels( ae.eval_sess, ae.eval_ops, ds.test) print('Shape of test_labels = {}'.format(np.shape(test_labels))) print('Shape of test_latents = {}'.format(np.shape(test_latents))) train_images, train_latents, train_labels = get_images_and_latents_and_labels( ae.eval_sess, ae.eval_ops, ds.train_once) print('Shape of train_labels = {}'.format(np.shape(train_labels))) print('Shape of train_latents = {}'.format(np.shape(train_latents))) train_val_test_images = [train_images, val_images, test_images] train_val_test_labels = [train_labels, val_labels, test_labels] train_val_test_latents = [train_latents, val_latents, test_latents] # train_val_test_images = map(lambda x: unshift_and_unscale_all(x), train_val_test_images) # if FLAGS.dataset == 'mnist32' or FLAGS.dataset == 'omniglot32': # train_val_test_images = map(lambda x: unpad(x), train_val_test_images) # if FLAGS.dataset == 'mnist32': # train_val_test_labels = map(lambda y: np.argmax(y, axis=-1), train_val_test_labels) ipdb.set_trace() for (split, X, Y, Z) in zip(['train', 'val', 'test'], train_val_test_images, train_val_test_labels, train_val_test_latents): save_split(split, X, Y, Z, ae)