Nastaran
Nastaran

Reputation: 91

How to save an operation node's output in a graph trained by Tensorflow?

I have trained a deep learning model by Tensorflow. The model is saved as a saved_model.pb and also there are variables and checkpoints saved during training. The model's task is regression. The output is a single scalar. I use the saved model on some new data to test the model accuracy. Now, I want to use the saved model to save the vector just before the last layer which do regression. I'm going to use that vector by different regressors to take and improve the best result. I have also visualized the graph by Tensorboard, and I realized the vector I'm looking for is the output of an operation node in Tensorflow. I attach the screenshot of the desired part of graph. How it is possible to use a saved model to do that? I mean to save the output vector just before the regression layer. https://drive.google.com/file/d/1tn8kGO6KBYSaD-Yz5Xp5OYQmdAniY84d/view?usp=sharing Furthermore, I have some codes to get the predicted single scalar.

my_predictor = predictor.from_saved_model(export_dir)

mae = []
for output in read_fn(file_references=file_names,
                      mode=tf.estimator.ModeKeys.EVAL,
                      params=READER_PARAMS):

    img = output['features']['x']
    lbl = output['labels']['y']
    test_id = output['img_id']

    num_crop_predictions = 4
    crop_batch = extract_random_example_array(
        image_list=img,
        example_size=[32, 32, 32],
        n_examples=num_crop_predictions)

    y_ = my_predictor.session.run(
        fetches=my_predictor._fetch_tensors['logits'],
        feed_dict={my_predictor.feed_tensors['x']: crop_batch})

    y_ = np.mean(y_)

    mae.append(np.abs(y_ - lbl))

I have changed the part

y_ = my_predictor.session.run(
        fetches=my_predictor._fetch_tensors['logits'],
        feed_dict={my_predictor.feed_tensors['x']: crop_batch})

to

y_ = my_predictor.session.run(
        fetches=my_predictor.graph,
        feed_dict={my_predictor.feed_tensors['x']: crop_batch})

due to feed the 3D inputs, and fetch the desired node of graph. But, the output of an operation node (which is what I'm looking for (pool/global_avg_pool)) is not in the graph.

Upvotes: 1

Views: 380

Answers (1)

Dev Khadka
Dev Khadka

Reputation: 5451

This is how I did it on a DNN learning transfer.

Save Trained Model

self._saver.save(self._session, self._get_save_path(name))

Restore Model

## restore the graph
imported_meta = tf.train.import_meta_graph("%s.meta"%self._get_save_path(name))
graph = tf.get_default_graph()

## get reference of output of operation DNN/hidden5_out and set it to variable
## here ":0" indicates the output
self._frozen_out = self._graph.get_tensor_by_name("DNN/hidden5_out:0)

## restore the variables in session
self._session = tf.Session(graph=graph)
imported_meta.restore(self._session, self._get_save_path(name))

Calculate Frozen Output Once

I need to do this because it needs for placeholders to calculate frozen output. I think it is not necessary if you don't need to recalculate tensor value by feeding feed_dict

tensor = sess.eval(self._frozen_out, feed_dict=feed_dict)

Reuse the Frozen Output

## here dnn is an operation which uses output of operation "DNN/hidden5_out"
## I am passing the value of it directly as feed_dict so it will not go down the 
## graph to calculate it
sess.eval(dnn, feed_dict={self._frozen_out: tensor})

Hope this helps, comment if it is not clear.

Upvotes: 1

Related Questions