编译Proto文件后出现ImportError:无法导入string_int_label_map_pb2
Hey, I've run into this exact issue before when working with the TensorFlow Object Detection API—let's walk through the fixes step by step:
1. Double-check where you ran the protoc command
The command protoc object_detection/protos/*.proto --python_out=. must be executed from the models/research directory. This ensures the generated .py files end up directly in the object_detection/protos folder (right next to their original .proto files). If you ran it elsewhere, the string_int_label_map_pb2.py file is probably in the wrong place.
Head to ~/Documents/imgmlreport/inception/classification_inception/models/research/object_detection/protos and confirm the string_int_label_map_pb2.py exists. If not, cd back to models/research and re-run the protoc command.
2. Add the required paths to your Python environment
Even if the .py file exists, Python might not be able to find it because the models/research and models/research/slim directories aren't in its default search path. You have two easy fixes here:
Option 1: Set paths temporarily in the terminal
Before running your script, run these commands (replace the paths with your actual username):
export PYTHONPATH=$PYTHONPATH:/home/your-username/Documents/imgmlreport/inception/classification_inception/models/research export PYTHONPATH=$PYTHONPATH:/home/your-username/Documents/imgmlreport/inception/classification_inception/models/research/slim
Option 2: Hardcode paths in your script (recommended)
Add these lines at the very top of your Python script to automatically add the paths every time you run it:
import sys sys.path.append('/home/your-username/Documents/imgmlreport/inception/classification_inception/models/research') sys.path.append('/home/your-username/Documents/imgmlreport/inception/classification_inception/models/research/slim')
3. Verify your protoc version is compatible
Different versions of the Object Detection API require specific protoc versions. For example:
- TensorFlow 1.x works best with protoc 3.0.0–3.3.0
- TensorFlow 2.x supports newer versions (like 3.19.0+)
Run protoc --version to check your current version. If it's too old or new, download the matching version and replace your current installation.
4. Ensure all .proto files were compiled successfully
Sometimes the wildcard *.proto might miss some files (due to permissions or corrupted files). Check that every .proto file in object_detection/protos has a corresponding .py file. If string_int_label_map.proto is missing its .py counterpart, compile it individually:
protoc object_detection/protos/string_int_label_map.proto --python_out=.
Try these steps one by one—this should resolve the import error!
内容的提问来源于stack exchange,提问作者송준석

