Tensorboard parsing metadata or fetching sprite images takes forever - tensorflow

I ran this code snippet:
import os
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
from tensorflow.contrib.tensorboard.plugins import projector
LOG_DIR = 'logs'
metadata = os.path.join(LOG_DIR, 'metadata.tsv')
mnist = input_data.read_data_sets('MNIST_data')
input_1 = mnist.train.next_batch(10)
images = tf.Variable(input_1[0], name='images')
with open(metadata, 'w') as metadata_file:
for row in input_1[1]:
metadata_file.write('%d\n' % row)
with tf.Session() as sess:
saver = tf.train.Saver([images])
sess.run(images.initializer)
saver.save(sess, os.path.join(LOG_DIR, 'images.ckpt'))
config = projector.ProjectorConfig()
# One can add multiple embeddings.
# Link this tensor to its metadata file (e.g. labels).
embedding = config.embeddings.add()
embedding.tensor_name = images.name
embedding.metadata_path = metadata
# Saves a config file that TensorBoard will read during startup.
projector.visualize_embeddings(tf.summary.FileWriter(LOG_DIR), config)
And after this, I opened tensorboard embedding tab and it showed parsing metadata. However, it kept on loading that way endlessly. I tried another code and in that case, it kept loading on fetching spite Image. Is there something wrong with my tensorboard?

The problem is that TensorBoard couldn't find your metadata file, because it looks for the metadata file relative to the directory that you have specified with your '--logdir' parameter of the 'tensorboard' command.
So if you are opening TensorBoard with 'tensorboard --logdir logs', it will look for the metadata file in 'logs/logs/metadata.tsv'.
A possible fix for your code is to replace this line
embedding.metadata_path = metadata
with this one:
embedding.metadata_path = 'metadata.tsv'
In general, in order to debug errors TensorBoard you have to look at the response of the error messages in your browser console when looking at TensorBoard.

Related

TensorFlow Embedding Projector for Visualization of Latent Space Images Not Working?

Can someone please help me with why Tensorflow embedding projector is not working? I am training an autoencoder and am now trying to visualize the latent space. I followed this very useful tutorial: https://github.com/anujshah1003/Tensorboard-own-image-data-image-features-embedding-visualization
I have been rechecking my work, and I can't find any errors. The projector starts and ends up at this screen as shown in the attached image. It just says the points and dimensions are loading, but no points actually get loaded. The image I'm using and the code I have is below. Any pointers greatly appreciated. Thank you very much!
I am using Tensorflow 1.9.0 with Keras 2.1.6 and Python 2.7. I was using Tensorboard 1.9.0 but downgraded to 1.5.1 though it didn't do anything.
image_list = load_crops(num_positive,num_negative,h5_file,only_positives)
LOG_DIR = os.getcwd() + '/embedding-logs'
#now get the feature vectors by creating the encoder and running images through
embedding = encoder.predict(image_list)
features = tf.Variable(embedding, name='features')
#obtain the labels and name them
n_classes = 2
num_of_samples = embedding.shape[0]
num_of_samples_each_class = num_of_samples/n_classes
y = np.ones((num_of_samples,), dtype = 'int64')
y[:num_of_samples_each_class] = 0
y[num_of_samples_each_class:num_of_samples_each_class*2] = 1
names = ['CD3+','FOXP3+']
#generate metadata file that says which features belong to which label
#metadata allows to assign labels to each point in embedded space. label will be the name and the number we assign
metadata_file = open(os.path.join(LOG_DIR, 'metadata_2_classes.tsv'), 'a+')
metadata_file.write('Class\tName\n')
k=num_of_samples_each_class
j=0
for i in range(num_of_samples):
c = names[y[i]]
if i%k==0:
j=j+1
metadata_file.write('{}\t{}\n'.format(j,c))
metadata_file.close()
#we have to generate sprite image if we want to see the images in the visualization
sprite = images_to_sprite(image_list)
cv2.imwrite(os.path.join(LOG_DIR, 'sprite_2_classes.png'), sprite)
#run session
with tf.Session() as sess:
img_data = image_list
saver = tf.train.Saver([features])
sess.run(features.initializer)
saver.save(sess, os.path.join(LOG_DIR, 'images_2_classes.ckpt'))
config = projector.ProjectorConfig()
embedding = config.embeddings.add()
embedding.tensor_name = features.name
# Link this tensor to its metadata file (e.g. labels).
embedding.metadata_path = os.path.join(LOG_DIR, 'metadata_2_classes.tsv')
# Comment out if you don't want sprites
embedding.sprite.image_path = os.path.join(LOG_DIR, 'sprite_2_classes.png')
embedding.sprite.single_image_dim.extend([img_data.shape[1], img_data.shape[1]])
# Saves a config file that TensorBoard will read during startup.
projector.visualize_embeddings(tf.summary.FileWriter(LOG_DIR), config)
What shows up on Tensorboard
I figured it out. You have to apply np.squeeze on the output of the encoder. The output of the encoder being that the embedding projector has to be an array of points). It can't plot with that extra one so when it is removed, it works.

How can I use the Tensorflow .pb file?

I have a Tensorflow file AlexNet.pb. I am trying to load it then classify an image that I have. I can't find a way to load it then classify an image.
No-one seems to have a simple example of loading and running the .pb file.
It depends on how the protobuf file has been created.
If the .pb file is the result of:
# Create a builder to export the model
builder = tf.saved_model.builder.SavedModelBuilder("export")
# Tag the model in order to be capable of restoring it specifying the tag set
builder.add_meta_graph_and_variables(sess, ["tag"])
builder.save()
You have to know how that model has been tagged and use the tf.saved_model.loader.load method to load the saved graph in the current, empty, graph.
If the model instead has been frozen you have to load the binary file in memory manually:
with tf.gfile.GFile(frozen_graph_filename, "rb") as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
graph = tf.get_default_graph()
tf.import_graph_def(graph_def, name="prefix")
In both cases, you have to know the name of the input tensor and the name of the node you want to execute:
If, for example, your input tensor is a placeholder named batch_ and the node you want to execute is the node named dense/BiasAdd:0 you have to
batch = graph.get_tensor_by_name('batch:0')
prediction = restored_graph.get_tensor_by_name('dense/BiasAdd:0')
values = sess.run(prediction, feed_dict={
batch: your_input_batch,
})
You can use opencv to load .pb models,
eg.
net = cv2.dnn.readNet("model.pb")
Make sure you are using specific version of opencv - OpenCV 3.4.2 or OpenCV 4

How can I invoke a SageMaker model, trained with TensorFlow, using a csv file in the body of the call? [duplicate]

This question already has answers here:
How do I load python modules which are not available in Sagemaker?
(2 answers)
Closed 3 years ago.
I have deployed a TensorFlow model on AWS SageMaker, and I want to be able to invoke it using a csv file as the body of the call. The documentation says about creating a serving_input_function like the one below:
def serving_input_fn(hyperparameters):
# Logic to the following:
# 1. Defines placeholders that TensorFlow serving will feed with inference requests
# 2. Preprocess input data
# 3. Returns a tf.estimator.export.ServingInputReceiver or tf.estimator.export.TensorServingInputReceiver,
# which packages the placeholders and the resulting feature Tensors together.
In step 2, where it says preprocess input data, how do I get a handle on input data to process them?
I had the same problem but I wanted to handle jpeg requests.
Once you have your model_data ready, you can deploy it with the following lines.
from sagemaker.tensorflow.model import TensorFlowModel
sagemaker_model = TensorFlowModel(
model_data = 's3://path/to/model/model.tar.gz',
role = role,
framework_version = '1.12',
entry_point = 'train.py',
source_dir='my_src',
env={'SAGEMAKER_REQUIREMENTS': 'requirements.txt'}
)
predictor = sagemaker_model.deploy(
initial_instance_count=1,
instance_type='ml.m4.xlarge',
endpoint_name='resnet-tensorflow-classifier'
)
Your notebook should have a my_src directory which contains a file train.py and a requirements.txt file. The train.py file should have a function input_fn defined. For me, that function handled image/jpeg content, but the pattern is the same.
import io
import numpy as np
from PIL import Image
from keras.applications.resnet50 import preprocess_input
from keras.preprocessing import image
JPEG_CONTENT_TYPE = 'image/jpeg'
CSV_CONTENT_TYPE = 'text/csv'
# Deserialize the Invoke request body into an object we can perform prediction on
def input_fn(request_body, content_type=JPEG_CONTENT_TYPE):
# process an image uploaded to the endpoint
if content_type == JPEG_CONTENT_TYPE:
img = Image.open(io.BytesIO(request_body)).resize((300, 300))
img_array = np.array(img)
expanded_img_array = np.expand_dims(img_array, axis=0)
x = preprocess_input(expanded_img_array)
return x
# you would have something like this:
if content_type == CSV_CONTENT_TYPE:
# handle input
return handled_input
else:
raise errors.UnsupportedFormatError(content_type)
If yourtrain.py code imports some modules, you must supply requirements.txt defining those dependencies (that was the part I had trouble finding in the docs).
Hope this helps someone in the future.
You can preprocess input data by adding an input_fn which will be invoked every time u invoke and endpoint. It receives the input data and the content type of the data.
def input_fn(data, content_type):
// do some data preprocessing.
return preprocessed_data
This article explains it in more depth:
https://docs.aws.amazon.com/sagemaker/latest/dg/tf-training-inference-code-template.html

generating pb file in tensorflow

I'm trying to generate a pb file using the method given in this tutorial,
http://cv-tricks.com/how-to/freeze-tensorflow-models/
import tensorflow as tf
saver = tf.train.import_meta_graph('/Users/pr/tensorflow/dogs-cats-model.meta', clear_devices=True)
graph = tf.get_default_graph()
input_graph_def = graph.as_graph_def()
sess = tf.Session()
saver.restore(sess, "./dogs-cats-model")
When I try to run this code I get this error -
DataLossError (see above for traceback): Unable to open table file ./dogs-cats-model: Data loss: file is too short to be an sstable: perhaps your file is in a different file format and you need to use a different restore operator?
WHen I googled this error most of them recommend to generate the meta file using version 2 format? Is that the right approach?
Tensorflow version used -
1.3.0
Apparently, you are using both '/Users/pr/tensorflow/dogs-cats-model.meta' and './dogs-cats-model.meta'. Are you sure they point to the same file?
The following code works well on my machine:
import tensorflow as tf
saver = tf.train.import_meta_graph('./dogs-cats-model.meta', clear_devices=True)
graph = tf.get_default_graph()
input_graph_def = graph.as_graph_def()
sess = tf.Session()
saver.restore(sess, "./dogs-cats-model")

Embedding feature vectors in Tensorflow

In text processing there is embedding to show up (if I understood it correctly) the database words as vector (after dimension reduction).
now, I am wondering, is there any method like this to show extracted features via CNN?
for example: consider we have a CNN and train and test sets. we want to train the CNN with train set and meanwhile see the extracted features (from dense layer) corresponding class labels via CNN in the embedding section of tensorboard.
the purpose of this work is seeing the features of input data in every batch and understand how close or far are they from together. and finally, in the trained model, we can find out accuracy of our classifier (like softmax or etc.).
thank you in advance for your help.
I have taken help of Tensorflow documentation.
For in depth information on how to run TensorBoard and make sure you are logging all the necessary information, see TensorBoard: Visualizing Learning.
To visualize your embeddings, there are 3 things you need to do:
1) Setup a 2D tensor that holds your embedding(s).
embedding_var = tf.get_variable(....)
2) Periodically save your model variables in a checkpoint in LOG_DIR.
saver = tf.train.Saver()
saver.save(session, os.path.join(LOG_DIR, "model.ckpt"), step)
3) (Optional) Associate metadata with your embedding.
If you have any metadata (labels, images) associated with your embedding, you can tell TensorBoard about it either by directly storing a projector_config.pbtxt in the LOG_DIR, or use our python API.
For instance, the following projector_config.ptxt associates the word_embedding tensor with metadata stored in $LOG_DIR/metadata.tsv:
embeddings {
tensor_name: 'word_embedding'
metadata_path: '$LOG_DIR/metadata.tsv'
}
The same config can be produced programmatically using the following code snippet:
from tensorflow.contrib.tensorboard.plugins import projector
# Create randomly initialized embedding weights which will be trained.
vocabulary_size = 10000
embedding_size = 200
embedding_var = tf.get_variable('word_embedding', [vocabulary_size,
embedding_size])
# Format: tensorflow/tensorboard/plugins/projector/projector_config.proto
config = projector.ProjectorConfig()
# You can add multiple embeddings. Here we add only one.
embedding = config.embeddings.add()
embedding.tensor_name = embedding_var.name
# Link this tensor to its metadata file (e.g. labels).
embedding.metadata_path = os.path.join(LOG_DIR, 'metadata.tsv')
#Use the same LOG_DIR where you stored your checkpoint.
summary_writer = tf.summary.FileWriter(LOG_DIR)
# The next line writes a projector_config.pbtxt in the LOG_DIR. TensorBoard will
# read this file during startup.
projector.visualize_embeddings(summary_writer, config)