You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修改TensorFlow.pb模型输入形状以适配Movidius神经计算棒

Fixing Dynamic Input Shape for Movidius NCS Deployment

Hey, let's tackle this dynamic input shape issue so you can get your pose estimation model deployed to the Movidius Neural Compute Stick smoothly. The [Error 5] Toolkit Error you're hitting is because mvNCCompile refuses to work with uncertain input dimensions—we need to lock in that fixed (1,368,656,3) shape properly. Your initial approach was on the right track, but there were a few key details missing that kept it from working.

Step 1: Verify the Original Model's Input Node Name

First, make sure you're targeting the correct input node in the original graph. Sometimes node names can be tricky, so run this quick check to confirm:

import tensorflow as tf

graph_path = '/home/bk/Documents/OPSLim/Pose/graph_models/mobilenet_thin/graph_opt.pb'
with tf.gfile.GFile(graph_path, 'rb') as f:
    graph_def = tf.GraphDef()
    graph_def.ParseFromString(f.read())

# Print all placeholder nodes (inputs)
for node in graph_def.node:
    if node.op == 'Placeholder':
        print(f"Input node name: {node.name}, shape: {node.attr['shape'].shape}")

This should output the exact input node name (like image without the :0 suffix—important for the next step).

Step 2: Replace Input and Export a Fixed-Shape Model

Your earlier code only imported the modified graph but didn't save the changes to a new .pb file. Here's the corrected code, with fixes for the input shape mismatch and proper model export:

import tensorflow as tf

# Paths for original and new model
original_graph = '/home/bk/Documents/OPSLim/Pose/graph_models/mobilenet_thin/graph_opt.pb'
fixed_shape_graph = '/home/bk/Documents/OPSLim/Pose/graph_models/mobilenet_thin/graph_fixed_shape.pb'

with tf.Session() as sess:
    # Load the original frozen graph
    with tf.gfile.GFile(original_graph, 'rb') as f:
        graph_def = tf.GraphDef()
        graph_def.ParseFromString(f.read())
    
    # Create a new placeholder with your fixed input shape (corrected from 368x368 to 368x656)
    fixed_input = tf.placeholder(shape=(1, 368, 656, 3), dtype=tf.float32, name='fixed_image')
    
    # Import the original graph, replacing the dynamic input with our fixed one
    # Note: Use the node name without :0 for input_map
    tf.import_graph_def(graph_def, name='', input_map={"image": fixed_input})
    
    # Locate the model's output node (replace with your actual output node name from TensorBoard)
    # For the mobilenet_thin model, it's typically 'Openpose/concat_stage7'
    output_node_name = 'Openpose/concat_stage7'
    output_tensor = sess.graph.get_tensor_by_name(f'{output_node_name}:0')
    
    # Freeze the modified graph (convert variables to constants)
    frozen_graph_def = tf.graph_util.convert_variables_to_constants(
        sess,
        sess.graph.as_graph_def(),
        [output_node_name]
    )
    
    # Save the fixed-shape model
    with tf.gfile.GFile(fixed_shape_graph, 'wb') as f:
        f.write(frozen_graph_def.SerializeToString())

Key fixes here:

  • Corrected the input shape to match your actual deployment scenario (368x656 instead of 368x368)
  • Used the correct node name format for input_map (no :0 suffix)
  • Added code to freeze and save the modified graph permanently

Step 3: Validate the Fixed-Shape Model

Before compiling, double-check that the input shape is now static:

with tf.Session() as sess:
    with tf.gfile.GFile(fixed_shape_graph, 'rb') as f:
        graph_def = tf.GraphDef()
        graph_def.ParseFromString(f.read())
        tf.import_graph_def(graph_def, name='')
        input_tensor = sess.graph.get_tensor_by_name('fixed_image:0')
        print(f"Fixed input shape: {input_tensor.shape}")

You should see Fixed input shape: (1, 368, 656, 3) if everything worked.

Step 4: Compile with mvNCCompile

Now run the compile command with your new fixed-shape model:

mvNCCompile /home/bk/Documents/OPSLim/Pose/graph_models/mobilenet_thin/graph_fixed_shape.pb -in fixed_image -on Openpose/concat_stage7 -o pose_model.graph
  • -in: Specifies the new fixed input node name (fixed_image)
  • -on: Matches the output node name you used earlier
  • -o: Sets the output graph filename for the NCS

内容的提问来源于stack exchange,提问作者klopfer384

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 07:58:20