Windows正常运行的TensorFlow代码在Ubuntu报错求调试帮助
Hey there! Let's work through this issue step by step—since your code runs smoothly on Windows but throws errors on Ubuntu, the problem is almost certainly tied to environment differences or platform-specific quirks. Here's a structured approach to debug it:
1. First, Confirm Your TensorFlow Version
You mentioned that Mobilenet inference requires at least TF 1.5, so let's start here. On your Ubuntu machine, run this command in your terminal to check the installed version:
python -c "import tensorflow as tf; print(tf.__version__)"
- If the version is lower than 1.5, install the required version with:
Note: TensorFlow 1.x has specific Python version compatibility (TF 1.5 works best with Python 3.5-3.6). Make sure your Ubuntu Python version matches this.pip install tensorflow==1.5
2. Check Environment Dependencies & GPU/CPU Compatibility
Ubuntu often requires additional system-level dependencies that Windows handles automatically:
- If you're using the GPU version of TensorFlow:
- Verify that CUDA (v9.0) and cuDNN (v7.0) are installed—these are the versions compatible with TF 1.5. Mismatched CUDA/cuDNN versions are a common source of cross-platform errors.
- If you're using the CPU version:
- Install essential system libraries with:
sudo apt-get update && sudo apt-get install libatlas-base-dev libprotobuf-dev
- Install essential system libraries with:
3. Fix File Path & Permission Issues
Windows and Ubuntu use different path conventions, and file permissions can trip you up:
- Replace any Windows-style path separators (
\) in your code with Unix-style (/), or use Python'sos.path.join()to make paths cross-platform. - Check if your Ubuntu user has read access to the Mobilenet model files. Run this to verify:
If permissions are restricted, fix them with:ls -l /path/to/your/mobilenet/modelchmod +r /path/to/your/model/files/*
4. Capture & Analyze the Exact Error Message
Without the specific error traceback, it's hard to pinpoint the issue. Run your script in Ubuntu with this command to save the full error log:
python your_inference_script.py 2>&1 | tee error_log.txt
Look for key phrases like:
NotFoundError: Likely a missing model file or incorrect path.InvalidArgumentError: Tensor shape mismatches or incompatible input data.ImportError: Missing Python dependencies.
5. Test with a Minimal Reproducible Example
To rule out code-specific issues, run this minimal Mobilenet inference script on Ubuntu:
import tensorflow as tf # Load pre-trained Mobilenet (uses default weights from TensorFlow's repository) model = tf.keras.applications.MobileNet(weights='imagenet', input_shape=(224, 224, 3)) # Test with a dummy input image dummy_input = tf.random.normal((1, 224, 224, 3)) predictions = model.predict(dummy_input) print("Inference completed successfully!")
If this works, the problem is in your specific code (e.g., data preprocessing, custom model loading logic). If it fails, the issue is with your Ubuntu TensorFlow environment.
Once you have the exact error message, share it and we can narrow down the problem further! Also, double-check if you're using the same type of Python environment (virtualenv/conda) on both systems—environment inconsistencies are a frequent culprit.
内容的提问来源于stack exchange,提问作者Tujamo

