def generate_encodings(): data, labels = get_mnist_data() data = data.reshape(-1, 784) / 255. # Settings. model_dir = 'out/model@10000' img_height, img_width = 28, 28 input_dim = 784 latent_dim = 10 num_classes = 10 batch_size = 100 output_dir = 'vae_encodings' # Model. vae_model = VAE(vae, input_dim, latent_dim) vae_model.load_model(model_dir) # Perform encoding. encoded_data = [] for i in range(0, len(data), batch_size): start, end = i, min(i + batch_size, len(data)) X_batch = data[start:end] encoded_inputs = vae_model.encode(X_batch) encoded_data.append(encoded_inputs) encoded_data = np.concatenate(encoded_data, axis=0) # Save data. rgb_data = np.stack([data.reshape(-1, 28, 28)] * 3, axis=-1) rgb_data = np.array([img_as_ubyte(resize(x, (14, 14))) for x in rgb_data]) if not os.path.exists(output_dir): os.mkdir(output_dir) np.save(output_dir + '/features.npy', encoded_data) np.save(output_dir + '/data.npy', rgb_data) np.save(output_dir + '/labels.npy', labels)
def interpolate_between_classes(): data, labels = get_mnist_data() data = data.reshape(-1, 784) / 255. # Settings. model_dir = 'out/model@10000' img_height, img_width = 28, 28 input_dim = 784 latent_dim = 10 num_classes = 10 num_steps = 10 output_path = 'interpolate_classes.png' # Model. vae_model = VAE(vae, input_dim, latent_dim) vae_model.load_model(model_dir) # Data to examine. class_order = np.arange(num_classes) np.random.shuffle(class_order) class_idx = [np.random.choice(np.where(labels == c)[0]) \ for c in range(num_classes)] xs = vae_model.encode(data[class_idx]) # Create visualization. output_buffer = np.zeros( (img_height * (num_classes - 1), img_width * num_steps)) for c in range(num_classes - 1): step = (xs[c + 1] - xs[c]) / float(num_steps) X = np.expand_dims(xs[c], axis=0) for i in range(num_steps): decoded_X = vae_model.decode(X + i * step)[0] decoded_X = decoded_X.reshape(img_height, img_width) output_buffer[c * img_height:(c + 1) * img_height, i * img_width:(i + 1) * img_width] = decoded_X skio.imsave(output_path, output_buffer)
def compare_mean_vs_samples(): data, labels = get_mnist_data() data = data.reshape(-1, 784) / 255. # Settings. model_dir = 'out/model@10000' img_height, img_width = 28, 28 input_dim = 784 latent_dim = 10 num_classes = 10 num_samples = 10 output_path = 'mean_vs_samples.png' # Model. vae_model = VAE(vae, input_dim, latent_dim) vae_model.load_model(model_dir) # Create visualization. output_buffer = np.zeros( (img_height * num_classes, img_width * (num_samples + 2))) for c in range(num_classes): idx = np.random.choice(np.where(labels == c)[0]) X = np.expand_dims(data[idx], axis=0) X_square = X.reshape(img_height, img_width) encoded_X_mean = vae_model.encode(X) decoded_X_mean = vae_model.decode(encoded_X_mean)[0] decoded_X_mean = decoded_X_mean.reshape(img_height, img_width) # Draw. output_buffer[c * img_height:(c + 1) * img_height, :img_width] = X_square output_buffer[c * img_height:(c + 1) * img_height, img_width:2 * img_width] = decoded_X_mean for j in range(num_samples): decoded_X_sample = vae_model.encode_decode_sample(X)[0] decoded_X_sample = decoded_X_sample.reshape(img_height, img_width) output_buffer[c * img_height:(c + 1) * img_height, (j + 2) * img_width:(j + 3) * img_width] = decoded_X_sample skio.imsave(output_path, output_buffer)
def run_class_conditional(): data, labels = get_mnist_data() data = data.reshape(-1, 784) / 255. # data, labels = get_olivetti_data() # data = data.reshape(-1, 64, 64, 1) num_classes = 10 batch_size = 100 num_iters = 10000 img_height, img_width = 28, 28 # img_height, img_width = 64, 64 # Declare model. latent_dim = 10 vae_model = ConditionalVAE(vae_class_conditional, 784, latent_dim, num_classes) # vae_model = ConditionalVAE(vae_faces_class_conditional, [64, 64, 1], latent_dim, num_classes) # Visualization points. num_viz, num_cols = 20, 20 rows, row_labels = [], [] for i in range(num_viz): xs = np.random.multivariate_normal(np.zeros(latent_dim), np.eye(latent_dim), 2) step = (xs[1] - xs[0]) / float(num_cols) rows.append([xs[0] + step * j for j in range(num_cols)]) row_labels += [i // 2] * num_cols rows = np.array(rows).reshape(-1, latent_dim) row_labels = np.diag(np.arange(num_classes))[row_labels] output_dir = 'out6' if not os.path.exists(output_dir): os.mkdir(output_dir) # Train. for t in range(num_iters): idx = np.random.choice(np.arange(len(data)), batch_size, replace=False) X_batch, y_batch = data[idx], labels[idx] y_batch_one_hot = np.diag(np.arange(num_classes))[y_batch.astype( np.int32)] batch_loss = vae_model.train(X_batch, y_batch_one_hot) if t % 200 == 0: print(t, np.mean(batch_loss)) decoded_outputs = vae_model.decode(rows, row_labels) decoded_outputs = decoded_outputs.reshape(num_viz, num_cols, img_height, img_width) output_buffer = np.zeros( (num_viz * img_height, num_cols * img_width)) for i in range(num_viz): for j in range(num_cols): output_buffer[i * img_height:(i + 1) * img_height, j * img_width:(j + 1) * img_width] = decoded_outputs[i, j] skio.imsave(output_dir + '/%d.png' % t, output_buffer) vae_model.save_model(output_dir + '/model@%d' % num_iters)
def run(): data, labels = get_mnist_data() data = data.reshape(-1, 784) / 255. # data = data.reshape(-1, 28, 28, 1) / 255. # data, labels = get_olivetti_data() # data = data.reshape(-1, 64, 64, 1) # data, labels = get_train_data() batch_size = 100 num_iters = 10000 img_height, img_width = 28, 28 # img_height, img_width = 64, 64 # Declare model. latent_dim = 10 vae_model = VAE(vae, 784, latent_dim) # vae_model = VAE(vae_mnist_conv, [28, 28, 1], latent_dim) # vae_model = VAE(vae_face_conv, [64, 64, 3], latent_dim) # Visualization points. num_viz, num_cols = 21, 20 xs = np.random.multivariate_normal(np.zeros(latent_dim), np.eye(latent_dim), num_viz) rows = [] for i in range(num_viz - 1): step = (xs[i + 1] - xs[i]) / float(num_cols) rows.append([xs[i] + step * j for j in range(num_cols)]) rows = np.array(rows).reshape(-1, latent_dim) output_dir = 'out2' if not os.path.exists(output_dir): os.mkdir(output_dir) # Train. for t in range(num_iters): idx = np.random.choice(np.arange(len(data)), batch_size, replace=False) X_batch = data[idx] batch_loss = vae_model.train(X_batch) if t % 200 == 0: print(t, batch_loss) decoded_outputs = vae_model.decode(rows) decoded_outputs = decoded_outputs.reshape(num_viz - 1, num_cols, img_height, img_width) output_buffer = np.zeros( ((num_viz - 1) * img_height, num_cols * img_width)) for i in range(num_viz - 1): for j in range(num_cols): output_buffer[i * img_height:(i + 1) * img_height, j * img_width:(j + 1) * img_width] = decoded_outputs[i, j] skio.imsave(output_dir + '/%d.png' % t, output_buffer) vae_model.save_model(output_dir + '/model@%d' % num_iters)
def train_mnist_simple_cnn(): loss = 'categorical_crossentropy' optimizer = 'adam' metrics = ['accuracy'] train_x, train_y, test_x, test_y = get_mnist_data() train_x = train_x.reshape(-1, 28, 28, 1) test_x = test_x.reshape(-1, 28, 28, 1) train_x, test_x = train_x / 255., test_x / 255. train_y, test_y = to_categorical(train_y), to_categorical(test_y) model = simple_mnist_cnn() model.compile(loss=loss, optimizer=optimizer, metrics=metrics) model.fit(train_x, train_y, epochs=20, batch_size=128) print('=================') print(model.evaluate(test_x, test_y, batch_size=128)) model.save('mnist_simple_cnn.h5')
def train_mnist_mlp(): layer_n_units = [784, 200, 10] activations = ['tanh', 'softmax'] loss = 'categorical_crossentropy' optimizer = 'adam' metrics = ['accuracy'] train_x, train_y, test_x, test_y = get_mnist_data() train_y, test_y = to_categorical(train_y), to_categorical(test_y) train_x, test_x = train_x.reshape(-1, 784), test_x.reshape(-1, 784) train_x, test_x = train_x / 255., test_x / 255. model = simple_mlp(layer_n_units, activations) model.compile(loss=loss, optimizer=optimizer, metrics=metrics) model.fit(train_x, train_y, epochs=20, batch_size=128) print('=================') print(model.evaluate(test_x, test_y, batch_size=128)) model.save('mnist_mlp.h5')
def load_and_process_fmnist_data(): # Make sure that fashion-mnist/*.gz files is in data/ train_x, train_y, val_x, val_y, test_x, test_y = get_mnist_data() num_train = train_x.shape[0] num_val = val_x.shape[0] num_test = test_x.shape[0] # Convert label lists to one-hot (one-of-k) encoding train_y = create_one_hot(train_y) val_y = create_one_hot(val_y) test_y = create_one_hot(test_y) # Normalize our data train_x, val_x, test_x = normalize(train_x, val_x, test_x) # Pad 1 as the last feature of train_x and test_x train_x = add_one(train_x) val_x = add_one(val_x) test_x = add_one(test_x) return train_x, train_y, val_x, val_y, test_x, test_y
def classification_accuracy(): from sklearn.svm import LinearSVC from sklearn.neural_network import MLPClassifier from sklearn.metrics import accuracy_score data, labels = get_mnist_data() data = data.reshape(-1, 784) / 255. # Settings. model_dir = 'out/model@10000' img_height, img_width = 28, 28 input_dim = 784 latent_dim = 10 num_classes = 10 batch_size = 100 output_dir = 'vae_encodings' # Model. vae_model = VAE(vae, input_dim, latent_dim) vae_model.load_model(model_dir) # Perform encoding. encoded_data = [] for i in range(0, len(data), batch_size): start, end = i, min(i + batch_size, len(data)) X_batch = data[start:end] encoded_inputs = vae_model.encode(X_batch) encoded_data.append(encoded_inputs) encoded_data = np.concatenate(encoded_data, axis=0) # Test. idx = np.arange(len(data)) np.random.shuffle(idx) train_idx, test_idx = idx[:60000], idx[60000:] # model = LinearSVC(verbose=2) model = MLPClassifier(hidden_layer_sizes=[10, 10, 10, 10], verbose=2) X_train, y_train = encoded_data[train_idx], labels[train_idx] X_test, y_test = encoded_data[test_idx], labels[test_idx] model.fit(X_train, y_train) preds = model.predict(X_test) print(accuracy_score(preds, y_test))
confusion_mat[test_y[i], :] = confusion_mat[test_y[i]] + y_hat[i] s = np.sum(confusion_mat, axis=1, keepdims=True) confusion_mat = confusion_mat / s np.set_printoptions(precision=2) print('Confusion matrix:') print(confusion_mat) print('Diagonal values:') print(confusion_mat.flatten()[0::11]) if __name__ == "__main__": np.random.seed(2018) # Load data from file # Make sure that fashion-mnist/*.gz files is in data/ train_x, train_y, val_x, val_y, test_x, test_y = get_mnist_data() num_train = train_x.shape[0] num_val = val_x.shape[0] num_test = test_x.shape[0] # generate_unit_testcase(train_x.copy(), train_y.copy()) # Convert label lists to one-hot (one-of-k) encoding train_y = create_one_hot(train_y) val_y = create_one_hot(val_y) test_y = create_one_hot(test_y) # Normalize our data train_x, val_x, test_x = normalize(train_x, val_x, test_x) # Pad 1 as the last feature of train_x and test_x