TensorFlow迁移学习:使用MobileNet替代Inception时遇NotFoundError问题
Hey there! I totally get how frustrating this error is— I’ve dealt with this exact checkpoint loading issue when switching models for transfer learning. Let’s break down why this is happening and how to fix it step by step:
Don’t specify the full checkpoint file name (only the prefix!)
TensorFlow’s checkpoint loader expects you to use the shared prefix of the three files, not the individual.data,.index, or.metafiles. For your setup, that means instead of passingmobilenet_v1_0.5_128.ckpt.data-00000-of-00001(or any of the other two) to your restore function, you should use justmobilenet_v1_0.5_128.ckpt.Example of the correct code (for TF1.x):
saver = tf.train.Saver() with tf.Session() as sess: saver.restore(sess, './path/to/mobilenet_v1_0.5_128.ckpt') # No file extension!Double-check your file path
It’s easy to mix up relative vs absolute paths, or miss that your checkpoint files are nested in a subfolder.- Run
import os; print(os.getcwd())to confirm your current working directory. - Make sure the checkpoint prefix path points directly to where the three files live. If you unzipped the checkpoint into a folder named
mobilenet_checkpoints, your path would be./mobilenet_checkpoints/mobilenet_v1_0.5_128.ckpt.
- Run
Verify exact filename matches
Typos happen! Check that the prefix in your code matches the actual file names perfectly— no missing characters, wrong version numbers (like0.5vs05), or case mismatches (critical on Linux/Mac systems).Switch to TF2.x’s built-in MobileNet API (if possible)
If you’re using TensorFlow 2.x, you don’t need to mess with manual checkpoint loading at all. The Keras API has a pre-built MobileNet implementation that handles weights automatically:from tensorflow.keras.applications import MobileNetV1 # Load pre-trained MobileNet with your input shape (128x128 here) base_model = MobileNetV1(weights='imagenet', input_shape=(128, 128, 3), include_top=False) # Freeze layers for transfer learning base_model.trainable = FalseThis is way more reliable and avoids checkpoint path headaches entirely.
内容的提问来源于stack exchange,提问作者nassima dif

