models icon indicating copy to clipboard operation
models copied to clipboard

Cannot export inference graph for custom architecture

Open wrkgm opened this issue 6 years ago • 2 comments

System information

OS Platform and Distribution (e.g., Linux Ubuntu 16.04): Windows 10 TensorFlow installed from (source or binary): source TensorFlow version (use command below): 1.12.0 Bazel version (if compiling from source): CUDA/cuDNN version: 9.0 GPU model and memory: GTX 1070 ti Exact command to reproduce: python train.py --train_dir=training --pipeline_config_path=configs/lstm_ssd_mobilenet_v1_imagenet.config

I'm trying to modify the faster rcnn architecture provided in the object detection api. I've modified the data decoder and loading scripts to make it take a sequence of n images, rather than a single image. To make the dimensions fit with the architecture, I used ConvLSTM2D to transform this sequence back to correct dimensionality. I've added this layer as follows: self._lstm_reduce = tf.keras.Sequential([ tf.keras.layers.ConvLSTM2D( self._lstm_size, kernel_size=[self._lstm_kernel, self._lstm_kernel], padding='SAME', name='LSTMReduce', return_sequences=False, **conv_hyperparams.params()), conv_hyperparams.build_batch_norm( (self._is_training and not freeze_batchnorm), name='LSTMReduceBatchNorm'), tf.keras.layers.Lambda( tf.nn.relu6, name='LSTMReduceActivation') ], name='LSTMReduceFeatures')

The layer is applied to the extracted features as followed: features = self._lstm_reduce(features)

This works fine for training. In fact, the model trains for a while and converges nicely. I can stop training and restart, and it loads from the previous checkpoint correctly.

Now I want to make an inference graph. I initially encountered some errors with the inference script because of the different dimensions (my inputs are [batch, time, w, h, c], it expects [batch, w, h, c]). I modified the inference graph code to fix these issues. Now I get the following error:

Key LSTMReduceFeatures/Variable not found in checkpoint [[Node: save/RestoreV2_1 ….

I know for a fact this variable is in the checkpoint. I examined the checkpoint with tensorflow checkpoint utils, and found this variable was present. I even looked at the raw checkpoint.meta files and found this variable listed. In addition, it restores successfully if I stop it during training! But when I try to restore it for inference graph, it fails.

The line it fails on is: https://github.com/tensorflow/models/blob/a76bc1256ffd04bdccb73442986858a0675c9c31/research/object_detection/exporter.py#L345

When it tries to restore the inference graph.

Thanks!

wrkgm avatar Jan 11 '20 02:01 wrkgm

Thank you for your post. We noticed you have not filled out the following field in the issue template. Could you update them if they are relevant in your case, or leave them as N/A? Thanks. What is the top-level directory of the model you are using Have I written custom code

tensorflowbutler avatar Jan 12 '20 00:01 tensorflowbutler

Just updating this for anyone who happens to have the same problem.

My issue was, in the export script, another branch of the model had gotten defined, responsible for providing specially formatted "detection features" as an output. This branch did not get defined during training, but it used the same lstm layer as part of it's new layers. This meant a new "lstm_reduce" was getting defined in this second branch, and there was no match for it in the training graph, which didn't use the second branch.

I didn't need detection features, so I just removed that code. If you did need it, you'd need to make sure that layer/weights got shared/reused.

Closing this

wrkgm avatar Jan 16 '20 00:01 wrkgm