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

重新训练MobileNetV1后运行optimize_for_inference.py遇解码错误的解决办法

Fixing google.protobuf.message.DecodeError with optimize_for_inference.py in TensorFlow 1.4.0

Let's break down the likely causes and fixes for this error—this is a common pain point when optimizing MobileNet models for Android deployment, so I’ve got a few targeted solutions for you:

1. Fix the command parameter syntax (most likely culprit)

Your command has a critical syntax mistake: you shouldn’t include spaces around the = signs in the flags. When you write --input = model_output/graph_v1.pb, the script interprets = as part of the input path, meaning it’s trying to read a non-existent or invalid file. This is almost certainly why you’re getting the decode error.

Rewrite your command without spaces around = like this:

!python tensorflow/tensorflow/python/tools/optimize_for_inference.py \
 --input=model_output/graph_v1.pb \
 --output=model_output/optimized_graph_v1.pb \
 --input_names=input \
 --output_names=MobilenetV1/Predictions/Reshape_1

2. Verify your input .pb file is valid

If fixing the syntax doesn’t work, your graph_v1.pb might be corrupted, incomplete, or not a proper frozen graph. Here’s how to check:

Option A: Use TensorFlow’s summarize_graph tool

Run this command to inspect the graph structure (it’s included with TensorFlow 1.x):

python tensorflow/tensorflow/tools/graph_transforms/summarize_graph.py --in_graph=model_output/graph_v1.pb

If this throws an error, your .pb file is definitely invalid.

Option B: Test parsing with Python code

Create a small script to load the graph directly and confirm it’s readable:

import tensorflow as tf

try:
    with tf.gfile.GFile('model_output/graph_v1.pb', 'rb') as f:
        graph_def = tf.GraphDef()
        graph_def.ParseFromString(f.read())
        print("✅ Graph file parsed successfully!")
        print(f"Input nodes found: {[node.name for node in graph_def.node if node.op == 'Placeholder']}")
        print(f"Target output node present: {'MobilenetV1/Predictions/Reshape_1' in [node.name for node in graph_def.node]}")
except Exception as e:
    print(f"❌ Failed to parse graph: {str(e)}")

If this fails, you’ll need to re-export your model correctly.

3. Ensure you’re using a frozen graph

If you only used tf.train.write_graph to export your model, you probably have a file with just the graph structure—no trained weights attached. You need to freeze the graph first to combine structure and weights into a single, usable .pb file.

Use TensorFlow’s freeze_graph.py tool (included with TensorFlow 1.x):

python tensorflow/tensorflow/python/tools/freeze_graph.py \
 --input_graph=model_output/graph_v1.pb \
 --input_checkpoint=path/to/your/training/checkpoint.ckpt \
 --input_binary=true \
 --output_graph=model_output/frozen_graph_v1.pb \
 --output_node_names=MobilenetV1/Predictions/Reshape_1

Then use the frozen_graph_v1.pb as the input to optimize_for_inference.py.

4. Match Protobuf version to TensorFlow 1.4.0

TensorFlow 1.4.0 requires a specific Protobuf version (3.3.0) to avoid compatibility issues. Check your current version with:

pip show protobuf

If it’s not 3.3.0, reinstall the correct version:

pip uninstall protobuf -y
pip install protobuf==3.3.0

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.11 09:15:03