Example #1
0
def test_input_fn(data_dir, params):
    """Test input function for the MNIST dataset.

    Args:
        data_dir: (string) path to the data directory
        params: (Params) contains hyperparameters of the model (ex: `params.num_epochs`)
    """
    dataset = mnist_dataset.test(data_dir)
    dataset = dataset.batch(params.batch_size)
    dataset = dataset.prefetch(
        1)  # make sure you always have one batch ready to serve
    return dataset
def __predict__(args, params):
    path = os.path.join(args.model_dir, "centroids_embeddings.npy")
    centrois = np.load(path)
    n_clusters = centrois.shape[1]  # remember to get it before reshape
    # print("centrois.shape=", centrois.shape)
    centrois = centrois.reshape(-1, centrois.shape[2])  # flatten it for knn
    # print("centrois.shape=", centrois.shape)

    # Define the model
    tf.logging.info("Creating the model...")
    config = tf.estimator.RunConfig(
        tf_random_seed=230,
        model_dir=args.model_dir,
        save_summary_steps=params.save_summary_steps)
    estimator = tf.estimator.Estimator(model_fn, params=params, config=config)

    # Compute embeddings on the test set
    tf.logging.info("Predicting")
    predictions = estimator.predict(
        lambda: test_input_fn(args.data_dir, params))

    embeddings = np.zeros((params.eval_size, params.embedding_size))
    for i, p in enumerate(
            predictions):  # i:enumerate_id, p:{'embeddings':array(64)}
        embeddings[i] = p['embeddings']

    labels = np.zeros(shape=[params.num_labels * n_clusters], dtype=np.int)
    for i in range(1, params.num_labels):
        for j in range(n_clusters):
            labels[n_clusters * i + j] = i

    knn = KNeighborsClassifier(n_neighbors=3)

    knn.fit(centrois, labels)
    y_predicted = knn.predict(embeddings)

    with tf.Session() as sess:
        dataset = mnist_dataset.test(args.data_dir)
        dataset = dataset.map(lambda img, lab: lab)
        dataset = dataset.batch(params.eval_size)
        labels_tensor = dataset.make_one_shot_iterator().get_next()
        y_true = sess.run(labels_tensor)

    print("Accuracy: " +
          str(metrics.accuracy_score(y_true, y_predicted) * 100) + "%")
Example #3
0
    config = projector.ProjectorConfig()
    embedding = config.embeddings.add()
    embedding.tensor_name = embedding_var.name

    # Specify where you find the sprite (we will create this later)
    # Copy the embedding sprite image to the eval directory
    shutil.copy2(args.sprite_filename, eval_dir)
    embedding.sprite.image_path = pathlib.Path(args.sprite_filename).name
    embedding.sprite.single_image_dim.extend(
        [params.image_size, params.image_size])
    # embedding.sprite.single_image_dim.extend([28, 28])

    with tf.Session() as sess:
        # TODO (@omoindrot): remove the hard-coded 10000
        # Obtain the test labels
        dataset = mnist_dataset.test(args.data_dir)
        dataset = dataset.map(lambda img, lab: lab)
        # dataset = dataset.batch(10000)
        dataset = dataset.batch(params.eval_size)
        labels_tensor = dataset.make_one_shot_iterator().get_next()
        labels = sess.run(labels_tensor)

    # Specify where you find the metadata
    # Save the metadata file needed for Tensorboard projector
    metadata_filename = "mnist_metadata.tsv"
    with open(os.path.join(eval_dir, metadata_filename), 'w') as f:
        for i in range(params.eval_size):
            c = labels[i]
            f.write('{}\n'.format(c))
    embedding.metadata_path = metadata_filename