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

使用TensorFlow Slim预训练ResNet50模型推理结果异常求助

Troubleshooting Incorrect ResNet50 Inference with TensorFlow Slim

Hey there! Let's figure out why your pre-trained ResNet50 model from TF Slim isn't outputting the expected result (EnglishCockerSpaniel). I'll walk through the most common issues and fixes below:

1. Double-Check Your Preprocessing Pipeline

Even tiny mismatches in preprocessing can throw off model predictions. Since you mentioned following the "ResNet pre-processing: VGG or Inception?" scheme, let's verify these critical details:

  • Mean Subtraction: ResNet in TF Slim uses per-channel mean subtraction (RGB means: 123.68, 116.779, 103.939). Don't mix this up with Inception's [-1, 1] scaling or VGG's raw [0, 255] pixel values.
  • Image Resizing & Cropping: The model expects 224x224 inputs. You should first resize the image to 256x256 (preserving aspect ratio), then do a center crop to 224x224—stretching directly to 224x224 distorts the image and hurts accuracy.
  • Channel Order: Make sure your image is in RGB format (not BGR). TF Slim's ResNet models are trained on RGB inputs, so flipping channels will lead to wrong predictions.

2. Verify Model Loading & Inference Mode

  • Load the Full Checkpoint: Ensure you're loading the complete pre-trained checkpoint (not a truncated version). Use tf.contrib.slim.get_variables_to_restore() to make sure all layers (including the final classification head) are restored correctly.
  • Disable Training Layers: When building the model, set is_training=False for resnet_v1_50(). This turns off dropout and switches batch normalization to inference mode (using stored moving averages instead of batch stats)—this is a super common mistake that breaks predictions.

3. Confirm Label Mapping

  • Use the Correct ImageNet Labels: TF Slim's ResNet50 is trained on the 1000-class ImageNet dataset. Make sure your label file matches the exact order used during training. For reference, EnglishCockerSpaniel corresponds to index 227 in the standard ImageNet label list.

Corrected Inference Code Example

Here's a refined version of your code that addresses these points:

import tensorflow as tf
from tensorflow.contrib.slim.nets import resnet_v1
import numpy as np
from PIL import Image

# Load and preprocess the image
img_path = "your_image_path.jpg"
# Step 1: Resize to 256x256 while keeping aspect ratio
img = Image.open(img_path).resize((256, 256))
# Step 2: Center crop to 224x224
crop_offset = (256 - 224) // 2
img = img.crop((crop_offset, crop_offset, crop_offset + 224, crop_offset + 224))
# Convert to array and subtract ResNet's per-channel mean
img_array = np.array(img).astype(np.float32)
img_array -= [123.68, 116.779, 103.939]
# Add batch dimension (model expects [batch_size, height, width, channels])
img_array = np.expand_dims(img_array, axis=0)

# Build ResNet50 for inference
with tf.Graph().as_default():
    inputs = tf.placeholder(tf.float32, shape=(1, 224, 224, 3))
    with tf.contrib.slim.arg_scope(resnet_v1.resnet_arg_scope()):
        # Critical: set is_training=False to disable training-only layers
        logits, _ = resnet_v1.resnet_v1_50(inputs, num_classes=1000, is_training=False)
    probabilities = tf.nn.softmax(logits)

    # Load pre-trained checkpoint
    init_fn = tf.contrib.slim.assign_from_checkpoint_fn(
        "path/to/resnet_v1_50.ckpt",
        tf.contrib.slim.get_variables_to_restore()
    )

    # Run inference
    with tf.Session() as sess:
        init_fn(sess)
        probs = sess.run(probabilities, feed_dict={inputs: img_array})
        # Get top-1 prediction
        top1_idx = np.argmax(probs[0])
        # Load matching ImageNet labels
        with open("imagenet_labels.txt", "r") as f:
            labels = [line.strip() for line in f.readlines()]
        print(f"Top Prediction: {labels[top1_idx]}")

Quick Debugging Tips

  • Print the preprocessed image array to check for color inversion or incorrect cropping.
  • Verify your checkpoint file isn't corrupted (check file size and integrity).
  • Cross-reference the label index for EnglishCockerSpaniel to avoid mapping mix-ups.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 07:19:23