tf-faster-rcnn icon indicating copy to clipboard operation
tf-faster-rcnn copied to clipboard

How to freeze the model?

Open lily0101 opened this issue 6 years ago • 5 comments

I have trained my data with this model, and there are some .ckpt files, but I want to generate the .pb file for the mobile. But I don't know the output and the input node name, I just print some node name when I use demo.py to test images. the output is below: Placeholder Placeholder_1 Placeholder_2 ....... vgg_16/cls_score/weights/Initializer/random_normal/shape vgg_16/cls_score/weights/Initializer/random_normal/mean vgg_16/cls_score/weights/Initializer/random_normal/stddev vgg_16/cls_score/weights/Initializer/random_normal/RandomStandardNormal vgg_16/cls_score/weights/Initializer/random_normal/mul vgg_16/cls_score/weights/Initializer/random_normal vgg_16/cls_score/weights vgg_16/cls_score/weights/Assign vgg_16/cls_score/weights/read vgg_16_3/cls_score/kernel/Regularizer/l2_regularizer/scale vgg_16_3/cls_score/kernel/Regularizer/l2_regularizer/L2Loss vgg_16_3/cls_score/kernel/Regularizer/l2_regularizer vgg_16/cls_score/biases/Initializer/Const vgg_16/cls_score/biases vgg_16/cls_score/biases/Assign vgg_16/cls_score/biases/read vgg_16_3/cls_score/MatMul vgg_16_3/cls_score/BiasAdd vgg_16_3/cls_prob vgg_16_3/cls_pred/dimension vgg_16_3/cls_pred vgg_16/bbox_pred/weights/Initializer/random_normal/shape vgg_16/bbox_pred/weights/Initializer/random_normal/mean vgg_16/bbox_pred/weights/Initializer/random_normal/stddev vgg_16/bbox_pred/weights/Initializer/random_normal/RandomStandardNormal vgg_16/bbox_pred/weights/Initializer/random_normal/mul vgg_16/bbox_pred/weights/Initializer/random_normal vgg_16/bbox_pred/weights vgg_16/bbox_pred/weights/Assign vgg_16/bbox_pred/weights/read vgg_16_3/bbox_pred/kernel/Regularizer/l2_regularizer/scale vgg_16_3/bbox_pred/kernel/Regularizer/l2_regularizer/L2Loss vgg_16_3/bbox_pred/kernel/Regularizer/l2_regularizer vgg_16/bbox_pred/biases/Initializer/Const vgg_16/bbox_pred/biases vgg_16/bbox_pred/biases/Assign vgg_16/bbox_pred/biases/read vgg_16_3/bbox_pred/MatMul vgg_16_3/bbox_pred/BiasAdd

I guess the node vgg_16_3/cls_prob and some others like this, I generate .pb file using this output node name, But when I restore this .pb file and feed the image to that graph using below code:

` sess = tf.Session()

with gfile.FastGFile(pb_file_path, 'rb') as f:
    graph_def = tf.GraphDef()
    graph_def.ParseFromString(f.read())
    sess.graph.as_default()
    tf.import_graph_def(graph_def, name='')

sess.run(tf.global_variables_initializer())

image = sess.graph.get_tensor_by_name('Placeholder:0')
image_info = sess.graph.get_tensor_by_name('Placeholder_1:0')
gt = sess.graph.get_tensor_by_name("Placeholder_2:0")

score = sess.graph.get_tensor_by_name('SCORE/vgg_16_3/cls_prob/cls_prob/scores:0')
bbox = sess.graph.get_tensor_by_name('SCORE/vgg_16_3/bbox_pred/BiasAdd/bbox_pred/scores:0')

rand_array = np.random.rand(1024, 5)

x_c = tf.constant(rand_array, dtype=tf.float32)
_, scores, bbox_pred, rois = sess.run([score,bbox],  feed_dict={image: blobs["data"], image_info: blobs["im_info"],gt: x_c})

`

There are some errors below:

File "/home/chenxingli/anaconda3/envs/python2/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 948, in _run raise TypeError('The value of a feed cannot be a tf.Tensor object. ' TypeError: The value of a feed cannot be a tf.Tensor object. Acceptable feed values include Python scalars, strings, lists, numpy ndarrays, or TensorHandles.

please help me with this error, I have tried my best to do this. Thank you so much!

lily0101 avatar May 17 '18 07:05 lily0101

你是在知乎上也提过这个的问题吗? 请问最后是怎么解决的

wei61457 avatar Nov 07 '18 09:11 wei61457

这个问题解决了吗?我现在遇到的是这个错误ValueError: callback pyfunc_0 is not found

Chase2816 avatar Jun 04 '19 02:06 Chase2816

I solved this problem by using the demo.py file to generate the .pb file. In demo.py , it uses the .ckpt file to test result, you can add follwing code to convert .ckpt to .pb. graph = tf.get_default_graph() input_graph_def = graph.as_graph_def() output_graph = "frozen_model.pb" output_node_names = "vgg16_3/cls_prob,vgg_16_3/bbox_pred/BiasAdd,vgg_16_1/rois/concat,vgg_16_3/cls_score/BiasAdd" output_graph_def = graph_util.convert_variables_to_constants(sess,input_graph_def,output_node_names.split(",")) with tf.gfile.GFile(output_graph,"wb") as f: f.write(output_graph_def.SerializeToString())

BTW you need to find the nodes name you need, and in my code, i reuse the .pb file to test whether or not it is right, and it will show more box in one object, so I add following code to correct it:

stds = np.tile(np.array(cfg.TRAIN.BBOX_NORMALIZE_STDS),(2)) means - np.tile(np.array(cfg.TRAIN.BBOX_NORMALIZE_MEANS),(2)) bbox_pred *= stds bbox_pred += means that's my solution.

lily0101 avatar Jul 23 '19 02:07 lily0101

非常感谢您的问题,通过输出节点,成功生成了pb文件,但是在sess传参的时候运行了五分钟,我分配了动态内存管理,没有作用,请问您遇见了吗?运行的警告如下: @@@@@@@@@@@@@@@@@@@@start_time 2019-11-15 14:51:20.998786: W tensorflow/core/framework/allocator.cc:124] Allocation of 411041792 exceeds 10% of system memory. 2019-11-15 14:52:40.981364: W tensorflow/core/framework/allocator.cc:124] Allocation of 411041792 exceeds 10% of system memory. 2019-11-15 14:52:41.303845: W tensorflow/core/framework/allocator.cc:124] Allocation of 411041792 exceeds 10% of system memory. 2019-11-15 14:52:44.509332: W tensorflow/core/framework/allocator.cc:124] Allocation of 411041792 exceeds 10% of system memory. 2019-11-15 14:52:46.302226: W tensorflow/core/framework/allocator.cc:124] Allocation of 411041792 exceeds 10% of system memory. 2019-11-15 14:52:54.346989: W .\tensorflow/core/grappler/optimizers/graph_optimizer_stage.h:241] Failed to run optimizer ArithmeticOptimizer, stage RemoveStackStridedSliceSameAxis node vgg_16_1/rois/strided_slice_12. Error: Pack node (vgg_16_1/rois/stack) axis attribute is out of bounds: 1 2019-11-15 14:52:54.347390: W .\tensorflow/core/grappler/optimizers/graph_optimizer_stage.h:241] Failed to run optimizer ArithmeticOptimizer, stage RemoveStackStridedSliceSameAxis node vgg_16_1/rois/strided_slice_14. Error: Pack node (vgg_16_1/rois/stack) axis attribute is out of bounds: 1 2019-11-15 14:52:54.347785: W .\tensorflow/core/grappler/optimizers/graph_optimizer_stage.h:241] Failed to run optimizer ArithmeticOptimizer, stage RemoveStackStridedSliceSameAxis node vgg_16_1/rois/strided_slice_16. Error: Pack node (vgg_16_1/rois/stack) axis attribute is out of bounds: 1 2019-11-15 14:52:54.348243: W .\tensorflow/core/grappler/optimizers/graph_optimizer_stage.h:241] Failed to run optimizer ArithmeticOptimizer, stage RemoveStackStridedSliceSameAxis node vgg_16_1/rois/strided_slice_18. Error: Pack node (vgg_16_1/rois/stack) axis attribute is out of bounds: 1 2019-11-15 14:55:13.729777: W .\tensorflow/core/grappler/optimizers/graph_optimizer_stage.h:241] Failed to run optimizer ArithmeticOptimizer, stage RemoveStackStridedSliceSameAxis node vgg_16_1/rois/strided_slice_12. Error: Pack node (vgg_16_1/rois/stack) axis attribute is out of bounds: 1 2019-11-15 14:55:14.138125: W .\tensorflow/core/grappler/optimizers/graph_optimizer_stage.h:241] Failed to run optimizer ArithmeticOptimizer, stage RemoveStackStridedSliceSameAxis node vgg_16_1/rois/strided_slice_14. Error: Pack node (vgg_16_1/rois/stack) axis attribute is out of bounds: 1 2019-11-15 14:55:14.138541: W .\tensorflow/core/grappler/optimizers/graph_optimizer_stage.h:241] Failed to run optimizer ArithmeticOptimizer, stage RemoveStackStridedSliceSameAxis node vgg_16_1/rois/strided_slice_16. Error: Pack node (vgg_16_1/rois/stack) axis attribute is out of bounds: 1 2019-11-15 14:55:14.198133: W .\tensorflow/core/grappler/optimizers/graph_optimizer_stage.h:241] Failed to run optimizer ArithmeticOptimizer, stage RemoveStackStridedSliceSameAxis node vgg_16_1/rois/strided_slice_18. Error: Pack node (vgg_16_1/rois/stack) axis attribute is out of bounds: 1 2019-11-15 14:56:02.263001: I tensorflow/stream_executor/dso_loader.cc:152] successfully opened CUDA library cublas64_100.dll locally 2019-11-15 14:56:20.213774: E tensorflow/stream_executor/cuda/cuda_driver.cc:806] failed to allocate 3.11G (3335344384 bytes) from device: CUDA_ERROR_OUT_OF_MEMORY: out of memory 2019-11-15 14:56:20.214025: W tensorflow/core/common_runtime/bfc_allocator.cc:211] Allocator (GPU_0_bfc) ran out of memory trying to allocate 3.09GiB. The caller indicates that this is not a failure, but may mean that there could be performance gains if more memory were available. 2019-11-15 14:56:20.215159: E tensorflow/stream_executor/cuda/cuda_driver.cc:806] failed to allocate 3.11G (3335344384 bytes) from device: CUDA_ERROR_OUT_OF_MEMORY: out of memory 2019-11-15 14:56:20.215407: W tensorflow/core/common_runtime/bfc_allocator.cc:211] Allocator (GPU_0_bfc) ran out of memory trying to allocate 3.09GiB. The caller indicates that this is not a failure, but may mean that there could be performance gains if more memory were available. 2019-11-15 14:56:20.525097: E tensorflow/stream_executor/cuda/cuda_driver.cc:806] failed to allocate 3.11G (3335344384 bytes) from device: CUDA_ERROR_OUT_OF_MEMORY: out of memory 2019-11-15 14:56:20.525345: W tensorflow/core/common_runtime/bfc_allocator.cc:211] Allocator (GPU_0_bfc) ran out of memory trying to allocate 2.10GiB. The caller indicates that this is not a failure, but may mean that there could be performance gains if more memory were available. 2019-11-15 14:56:20.525889: E tensorflow/stream_executor/cuda/cuda_driver.cc:806] failed to allocate 3.11G (3335344384 bytes) from device: CUDA_ERROR_OUT_OF_MEMORY: out of memory 2019-11-15 14:56:20.526181: W tensorflow/core/common_runtime/bfc_allocator.cc:211] Allocator (GPU_0_bfc) ran out of memory trying to allocate 2.10GiB. The caller indicates that this is not a failure, but may mean that there could be performance gains if more memory were available. @@@@@@@@@@@@@@@ end_time:307.0676922798157

Chase2816 avatar Nov 15 '19 07:11 Chase2816

这个问题解决了吗?我现在遇到的是这个错误ValueError: callback pyfunc_0 is not found

碰到了同样的问题,重新编译了.so文件仍没有解决,想问下你后面是怎么解决了这个问题?

gfyulx avatar Jan 13 '20 10:01 gfyulx