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

如何导入MNIST图片而非数据框?Keras中ResNet50运行MNIST遇阻

Hey there! I totally get where you're stuck right now. The flow_from_directory method is perfect when you have image files organized in folders by class, but Keras' built-in MNIST dataset comes as numpy arrays (not individual image files on your disk), which throws a wrench into that workflow. Let's walk through two ways to fix this—one that's efficient (and recommended) and another if you really want to work with actual image files.


You don't need to convert MNIST into physical image files to use ImageDataGenerator. The library has a flow() method designed specifically for working with in-memory numpy arrays, which is way faster and avoids unnecessary disk I/O. Here's how to set it up for ResNet50:

Step 1: Load and Preprocess MNIST for ResNet50

ResNet50 expects 3-channel RGB images (224x224 size) and normalized input values, but MNIST is 1-channel grayscale (28x28). We'll fix those differences first:

import numpy as np
from keras.datasets import mnist
from keras.applications.resnet50 import preprocess_input
from keras.utils import to_categorical
from keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array

# Load raw MNIST data
(x_train, y_train), (x_test, y_test) = mnist.load_data()

# Convert single-channel grayscale to 3-channel RGB by repeating the channel
x_train = np.repeat(x_train[..., np.newaxis], 3, axis=-1)
x_test = np.repeat(x_test[..., np.newaxis], 3, axis=-1)

# Resize images to ResNet50's default input size (224x224)
x_train = np.array([img_to_array(array_to_img(img).resize((224, 224))) for img in x_train])
x_test = np.array([img_to_array(array_to_img(img).resize((224, 224))) for img in x_test])

# Apply ResNet50's required preprocessing
x_train = preprocess_input(x_train)
x_test = preprocess_input(x_test)

# One-hot encode labels for categorical classification
y_train = to_categorical(y_train, 10)
y_test = to_categorical(y_test, 10)

Step 2: Use flow() to Generate Augmented Data

Now you can hook up your preprocessed arrays to ImageDataGenerator with flow():

# Initialize your data generator with augmentation settings
datagen = ImageDataGenerator(
    rotation_range=10,
    width_shift_range=0.1,
    height_shift_range=0.1,
    zoom_range=0.1,
    horizontal_flip=False  # MNIST digits don't make sense flipped horizontally
)

# Create generators for training and testing data
train_generator = datagen.flow(x_train, y_train, batch_size=32)
test_generator = datagen.flow(x_test, y_test, batch_size=32)

You can now use these generators directly with your ResNet50 model (whether you're fine-tuning or using it as a feature extractor).


Alternative: Save MNIST Arrays as Image Files First

If you really want to use flow_from_directory (e.g., to match a folder-based workflow), you can save each MNIST array as a physical image file organized by class. Note that this is slower and uses disk space, but here's how to do it:

Step 1: Create Folder Structure

First, set up a directory hierarchy where each class (0-9) has its own subfolder for training and test data:

import os
from PIL import Image

# Base directory to store MNIST images
base_dir = "mnist_image_dataset"
train_dir = os.path.join(base_dir, "train")
test_dir = os.path.join(base_dir, "test")

# Create directories if they don't exist
for split_dir in [train_dir, test_dir]:
    os.makedirs(split_dir, exist_ok=True)
    for digit in range(10):
        os.makedirs(os.path.join(split_dir, str(digit)), exist_ok=True)

Step 2: Save Arrays as Images

Loop through the raw MNIST data and save each image to its corresponding class folder:

# Reload raw MNIST (we don't want preprocessed data here)
(x_train_raw, y_train_raw), (x_test_raw, y_test_raw) = mnist.load_data()

# Save training images
for idx, (img, label) in enumerate(zip(x_train_raw, y_train_raw)):
    img_path = os.path.join(train_dir, str(label), f"train_{idx}.png")
    Image.fromarray(img).save(img_path)

# Save test images
for idx, (img, label) in enumerate(zip(x_test_raw, y_test_raw)):
    img_path = os.path.join(test_dir, str(label), f"test_{idx}.png")
    Image.fromarray(img).save(img_path)

Step 3: Use flow_from_directory

Now you can use the standard flow_from_directory method to load the images:

datagen = ImageDataGenerator(
    preprocessing_function=preprocess_input,
    rotation_range=10,
    width_shift_range=0.1,
    height_shift_range=0.1,
    zoom_range=0.1
)

train_generator = datagen.flow_from_directory(
    train_dir,
    target_size=(224, 224),
    color_mode="rgb",
    batch_size=32,
    class_mode="categorical"
)

test_generator = datagen.flow_from_directory(
    test_dir,
    target_size=(224, 224),
    color_mode="rgb",
    batch_size=32,
    class_mode="categorical"
)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 04:02:31