Models: IndexError: list index out of range freezing a keras model

Created on 26 Sep 2018  路  7Comments  路  Source: tensorflow/models


System information

  • What is the top-level directory of the model you are using: /models/official/resnet
  • Have I written custom code (as opposed to using a stock example script provided in TensorFlow):No
  • OS Platform and Distribution (e.g., Linux Ubuntu 16.04): Ubuntu 16.04.5 LTS (GNU/Linux 4.4.0-128-generic x86_64)
  • TensorFlow installed from (source or binary): binary
  • TensorFlow version (use command below): ('v1.10.0-0-g656e7a2b34', '1.10.0')
  • Bazel version (if compiling from source): n/a
  • CUDA/cuDNN version: CUDA Version 9.0.176 / 7.1.3
  • GPU model and memory: Tesla P4
  • Exact command to reproduce:

Describe the problem

I have trained a resnet50 network using transfer learning to create a classifier with my small data set using Keras in the TensorFlow implementation (not Keras API natively with TensorFlow backend) . I have saved the keras model as .h5 file and need to freeze it as .pb file, but I got error the IndexError: list index out of range when I tried to freeze the model with freeze_graph as shown below:

Source code / logs

from tensorflow.keras.models import load_model
#from keras.models import load_model
import tensorflow.keras.backend as K
from tensorflow.python.framework import graph_io
from tensorflow.python.tools import freeze_graph
from tensorflow.core.protobuf import saver_pb2
from tensorflow.python.training import saver as saver_lib

def convert_kerasmodel_to_frozengraph():
  keras_model = 'kr_tf_implementation_model.h5'
  models_dir = '/workspace/'
  model_filename = 'frozen_graph.pb'
  output_name = 'dense_1_1/Sigmoid'
  model = load_model(keras_model)
  model.summary()
  K.set_learning_phase(0)
  sess = K.get_session()
  saver = saver_lib.Saver(write_version=saver_pb2.SaverDef.V2)
  checkpoint_path = saver.save(sess, './saved_ckpt', global_step=0, latest_filename='checkpoint_state')
  graph_io.write_graph(sess.graph, '.', 'tmp.pb')
  freeze_graph.freeze_graph('./tmp.pb', '',
                            False, checkpoint_path, output_name,
                            "save/restore_all", "save/Const:0",      
                            models_dir+model_filename, False, "")

Logs

Traceback (most recent call last): File "multiclass_kr-tf_implementation_frozengraph_trtengine.py", line 268, in <module> main() File "multiclass_kr-tf_implementation_frozengraph_trtengine.py", line 263, in main convert_kerasmodel_to_frozengraph() File "multiclass_kr-tf_implementation_frozengraph_trtengine.py", line 176, in convert_kerasmodel_to_frozengraph models_dir+model_filename, False, "") File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/tools/freeze_graph.py", line 254, in freeze_graph checkpoint_version=checkpoint_version) File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/tools/freeze_graph.py", line 128, in freeze_graph_ with_def_protos var_list=var_list, write_version=checkpoint_version) File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/training/saver.py", line 1281, in __init__ self.build() File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/training/saver.py", line 1293, in build self._build(self._filename, build_save=True, build_restore=True) File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/training/saver.py", line 1330, in _build build_save=build_save, build_restore=build_restore) File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/training/saver.py", line 756, in _build_internal saveables = self._ValidateAndSliceInputs(names_to_saveables) File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/training/saver.py", line 663, in _ValidateAndSlice Inputs for converted_saveable_object in self.SaveableObjectsForOp(op, name): File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/training/saver.py", line 637, in SaveableObjectsFo rOp variable, "", name) File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/training/saver.py", line 121, in __init__ self.handle_op = var.op.inputs[0] File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 2001, in __getitem__ return self._inputs[i] IndexError: list index out of range

Most helpful comment

hi @harshini-gadige, thanks. I was able to freeze the keras model via command line with the flag python3 freeze_graph.py --input_saved_model_dir=/home/serving_model/ --output_graph=frozen_graph.pb --output_node_names=dense_1_1/Sigmoid --clear_devices

Now, I am trying to convert it to a TensorRT engine but I got this error ValueError: Node 'import/conv1_1/Conv2D' has an _output_shapes attribute inconsistent with the GraphDef for output #0: Dimension 1 in both shapes must be equal, but are 112 and 128. Shapes are [8,112,112,64] and [?,128,128,64].

Some tips or recommendations?

All 7 comments

@vilmara Can you try using "saved model" and use input_saved_model_dir (to input the path of the directory where your model is saved) as an input to the freeze_graph.

hi @harshini-gadige, thanks. I was able to freeze the keras model via command line with the flag python3 freeze_graph.py --input_saved_model_dir=/home/serving_model/ --output_graph=frozen_graph.pb --output_node_names=dense_1_1/Sigmoid --clear_devices

Now, I am trying to convert it to a TensorRT engine but I got this error ValueError: Node 'import/conv1_1/Conv2D' has an _output_shapes attribute inconsistent with the GraphDef for output #0: Dimension 1 in both shapes must be equal, but are 112 and 128. Shapes are [8,112,112,64] and [?,128,128,64].

Some tips or recommendations?

@vilmara It could be due to the size of the input data set you are using for training(not sure but I think so). You may try changing the size of the input data set.

thanks @harshini-gadige, I will try it

@Could you please list more detailed information about how to use freeze_graph to convert keras model to frozen pb file please.

python freeze.py --input_graph=graph.pbtxt --input_checkpoint=model.ckpt-292 --output_graph=return_visit.pb --output_node_names=softmax --clear_devices
/data1/tf/anaconda3/lib/python3.6/site-packages/h5py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from float to np.floating is deprecated. In future, it will be treated as np.float64 == np.dtype(float).type.
from ._conv import register_converters as _register_converters
2019-01-07 10:06:56.618717: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
Traceback (most recent call last):
File "freeze.py", line 452, in
run_main()
File "freeze.py", line 449, in run_main
app.run(main=my_main, argv=[sys.argv[0]] + unparsed)
File "/data1/tf/anaconda3/lib/python3.6/site-packages/tensorflow/python/platform/app.py", line 125, in run
_sys.exit(main(argv))
File "freeze.py", line 448, in
my_main = lambda unused_args: main(unused_args, flags)
File "freeze.py", line 342, in main
flags.saved_model_tags, checkpoint_version)
File "freeze.py", line 324, in freeze_graph
checkpoint_version=checkpoint_version)
File "freeze.py", line 152, in freeze_graph_with_def_protos
var_list=var_list, write_version=checkpoint_version)
File "/data1/tf/anaconda3/lib/python3.6/site-packages/tensorflow/python/training/saver.py", line 1102, in __init__
self.build()
File "/data1/tf/anaconda3/lib/python3.6/site-packages/tensorflow/python/training/saver.py", line 1114, in build
self._build(self._filename, build_save=True, build_restore=True)
File "/data1/tf/anaconda3/lib/python3.6/site-packages/tensorflow/python/training/saver.py", line 1151, in _build
build_save=build_save, build_restore=build_restore)
File "/data1/tf/anaconda3/lib/python3.6/site-packages/tensorflow/python/training/saver.py", line 773, in _build_internal
saveables = self._ValidateAndSliceInputs(names_to_saveables)
File "/data1/tf/anaconda3/lib/python3.6/site-packages/tensorflow/python/training/saver.py", line 680, in _ValidateAndSliceInputs
for converted_saveable_object in self.SaveableObjectsForOp(op, name):
File "/data1/tf/anaconda3/lib/python3.6/site-packages/tensorflow/python/training/saver.py", line 654, in SaveableObjectsForOp
variable, "", name)
File "/data1/tf/anaconda3/lib/python3.6/site-packages/tensorflow/python/training/saver.py", line 128, in __init__
self.handle_op = var.op.inputs[0]
File "/data1/tf/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 2128, in __getitem__
return self._inputs[i]
IndexError: list index out of range

python freeze.py --input_graph=graph.pbtxt --input_checkpoint=model.ckpt-292 --output_graph=return_visit.pb --output_node_names=softmax --clear_devices
/data1/tf/anaconda3/lib/python3.6/site-packages/h5py/init.py:36: FutureWarning: Conversion of the second argument of issubdtype from float to np.floating is deprecated. In future, it will be treated as np.float64 == np.dtype(float).type.
from ._conv import register_converters as _register_converters
2019-01-07 10:06:56.618717: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
Traceback (most recent call last):
File "freeze.py", line 452, in
run_main()
File "freeze.py", line 449, in run_main
app.run(main=my_main, argv=[sys.argv[0]] + unparsed)
File "/data1/tf/anaconda3/lib/python3.6/site-packages/tensorflow/python/platform/app.py", line 125, in run
_sys.exit(main(argv))
File "freeze.py", line 448, in
my_main = lambda unused_args: main(unused_args, flags)
File "freeze.py", line 342, in main
flags.saved_model_tags, checkpoint_version)
File "freeze.py", line 324, in freeze_graph
checkpoint_version=checkpoint_version)
File "freeze.py", line 152, in freeze_graph_with_def_protos
var_list=var_list, write_version=checkpoint_version)
File "/data1/tf/anaconda3/lib/python3.6/site-packages/tensorflow/python/training/saver.py", line 1102, in init
self.build()
File "/data1/tf/anaconda3/lib/python3.6/site-packages/tensorflow/python/training/saver.py", line 1114, in build
self._build(self._filename, build_save=True, build_restore=True)
File "/data1/tf/anaconda3/lib/python3.6/site-packages/tensorflow/python/training/saver.py", line 1151, in _build
build_save=build_save, build_restore=build_restore)
File "/data1/tf/anaconda3/lib/python3.6/site-packages/tensorflow/python/training/saver.py", line 773, in _build_internal
saveables = self._ValidateAndSliceInputs(names_to_saveables)
File "/data1/tf/anaconda3/lib/python3.6/site-packages/tensorflow/python/training/saver.py", line 680, in _ValidateAndSliceInputs
for converted_saveable_object in self.SaveableObjectsForOp(op, name):
File "/data1/tf/anaconda3/lib/python3.6/site-packages/tensorflow/python/training/saver.py", line 654, in SaveableObjectsForOp
variable, "", name)
File "/data1/tf/anaconda3/lib/python3.6/site-packages/tensorflow/python/training/saver.py", line 128, in init
self.handle_op = var.op.inputs[0]
File "/data1/tf/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 2128, in getitem
return self._inputs[i]
IndexError: list index out of range

Have you solved this problem?

Was this page helpful?
0 / 5 - 0 ratings