kears库中对样本图片resize的原理(target_size)

本文详细解析了如何使用Keras库中的load_img方法进行图片预处理,包括调整图片大小以适应模型输入需求,以及在预测阶段如何重复相同的预处理步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

调用keras库去训练样本的时候,经常会用target_size吧图片resize自己想要的形状继续训练,可是需要预测的时候想单张把图片放进去预测,需要重复相同的resize步骤,于是看了源码,在image.py中有找到了方法

def load_img(path, grayscale=False, color_mode='rgb', target_size=None,
             interpolation='nearest'):
    """Loads an image into PIL format.

    # Arguments
        path: Path to image file.
        color_mode: One of "grayscale", "rbg", "rgba". Default: "rgb".
            The desired image format.
        target_size: Either `None` (default to original size)
            or tuple of ints `(img_height, img_width)`.
        interpolation: Interpolation method used to resample the image if the
            target size is different from that of the loaded image.
            Supported methods are "nearest", "bilinear", and "bicubic".
            If PIL version 1.1.3 or newer is installed, "lanczos" is also
            supported. If PIL version 3.4.0 or newer is installed, "box" and
            "hamming" are also supported. By default, "nearest" is used.

    # Returns
        A PIL Image instance.

    # Raises
        ImportError: if PIL is not available.
        ValueError: if interpolation method is not supported.
    """
    if grayscale is True:
        warnings.warn('grayscale is deprecated. Please use '
                      'color_mode = "grayscale"')
        color_mode = 'grayscale'
    if pil_image is None:
        raise ImportError('Could not import PIL.Image. '
                          'The use of `array_to_img` requires PIL.')
    img = pil_image.open(path)
    if color_mode == 'grayscale':
        if img.mode != 'L':
            img = img.convert('L')
    elif color_mode == 'rgba':
        if img.mode != 'RGBA':
            img = img.convert('RGBA')
    elif color_mode == 'rgb':
        if img.mode != 'RGB':
            img = img.convert('RGB')
    else:
        raise ValueError('color_mode must be "grayscale", "rbg", or "rgba"')
    if target_size is not None:
        width_height_tuple = (target_size[1], target_size[0])
        if img.size != width_height_tuple:
            if interpolation not in _PIL_INTERPOLATION_METHODS:
                raise ValueError(
                    'Invalid interpolation method {} specified. Supported '
                    'methods are {}'.format(
                        interpolation,
                        ", ".join(_PIL_INTERPOLATION_METHODS.keys())))
            resample = _PIL_INTERPOLATION_METHODS[interpolation]
            img = img.resize(width_height_tuple, resample)
    return img

可以看出,是用pil中nearest(默认)的模式resize的,所以只需要按照相同的方法resize就OK了

``` import numpy as np import tensorflow as tf from tensorflow.keras.preprocessing.image import ImageDataGenerator from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout import matplotlib.pyplot as plt import cv2 tf.random.set_seed(42) # 定义路径 train_dir = r'C:\Users\29930\Desktop\结构参数图' validation_dir = r'C:\Users\29930\Desktop\测试集路径' # 需要实际路径 # 图像参数 img_width, img_height = 150, 150 batch_size = 32 # 数据生成器 train_datagen = ImageDataGenerator( rescale=1./255, shear_range=0.2, zoom_range=0.2, horizontal_flip=True) test_datagen = ImageDataGenerator(rescale=1./255) train_generator = train_datagen.flow_from_directory( train_dir, # 修改为实际路径 target_size=(img_width, img_height), batch_size=batch_size, class_mode='binary') validation_generator = test_datagen.flow_from_directory( validation_dir, # 修改为实际路径 target_size=(img_width, img_height), batch_size=batch_size, class_mode='binary') # 模型构建 model = Sequential([ Conv2D(32, (3,3), activation='relu', input_shape=(img_width, img_height, 3)), MaxPooling2D(2,2), Conv2D(64, (3,3), activation='relu'), MaxPooling2D(2,2), Flatten(), Dense(128, activation='relu'), Dropout(0.5), Dense(1, activation='sigmoid') ]) model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy']) # 训练模型 history = model.fit( train_generator, steps_per_epoch=len(train_generator), epochs=20, validation_data=validation_generator, validation_steps=len(validation_generator)) # Grad-CAM函数修正版 def generate_grad_cam(model, img_array, layer_name): # 创建梯度模型 grad_model = tf.keras.models.Model( inputs=[model.inputs], outputs=[model.get_layer(layer_name).output, model.output] ) # 计算梯度 with tf.GradientTape() as tape: conv_outputs, predictions = grad_model(img_array) loss = predictions[:, 0] # 获取梯度 grads = tape.gradient(loss, conv_outputs) pooled_grads = tf.reduce_mean(grads, axis=(0, 1, 2)) # 生成热力图 conv_outputs = conv_outputs[0] heatmap = tf.reduce_sum(conv_outputs * pooled_grads, axis=-1) # 归一化处理 heatmap = np.maximum(heatmap, 0) heatmap /= np.max(heatmap) return heatmap # 可视化部分 X, y = next(validation_generator) sample_image = X[0] img_array = np.expand_dims(sample_image, axis=0) # 获取最后一个卷积层(根据模型结构调整索引) last_conv_layer = model.layers[2] # 第二个Conv2D层 heatmap = generate_grad_cam(model, img_array, last_conv_layer.name) # 调整热力图尺寸 heatmap = cv2.resize(heatmap, (img_width, img_height)) heatmap = np.uint8(255 * heatmap) # 颜色映射 jet = plt.colormaps.get_cmap('jet') jet_colors = jet(np.arange(256))[:, :3] jet_heatmap = jet_colors[heatmap] # 叠加显示 superimposed_img = jet_heatmap * 0.4 + sample_image superimposed_img = np.clip(superimposed_img, 0, 1) # 绘制结果 plt.figure(figsize=(12, 4)) plt.subplot(131) plt.imshow(sample_image) plt.title('原始图像') plt.axis('off') plt.subplot(132) plt.imshow(heatmap, cmap='jet') plt.title('热力图') plt.axis('off') plt.subplot(133) plt.imshow(superimposed_img) plt.title('叠加效果') plt.axis('off') plt.tight_layout() plt.show()```完善代码使在构建模型后,调用模型初始化各层形状,随后指定输入形状并通过实际数据前向传播来触发形状推断.输出完整代码
03-24
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值