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

使用Imgaug进行图像增强时遭遇Dtype错误的技术求助

Fixing Imgaug's AdditiveGaussianNoise dtype Error

Hey there! I’ve run into this exact issue before—Imgaug’s AdditiveGaussianNoise is pretty picky about image data types, unlike the other augmentations you’re using. Let’s break down what’s happening and how to fix it.

Why the Error Happens

The error message tells you that float64 is a forbidden dtype for this augmentation. Most Imgaug operations (like flips or rotations) are flexible with dtypes, but AdditiveGaussianNoise involves low-level numerical operations that only support uint8 or float32 by default. Your training images are currently in float64, which triggers this restriction.

Step-by-Step Fix

Let’s adjust your data generator to convert images to a supported dtype before applying the augmentation:

  1. Verify your image dtype (optional but helpful)
    Add a quick print statement in your create_augmented_batch function to confirm the current dtype of your images:

    def create_augmented_batch(self, index):
        image_list, labels_batch = self._create_balanced_batch(index)
        print("Image dtype before augmentation:", image_list.dtype) # Check current dtype
        if self.augmentation is not None:
            image_list = self.augmentation.augment(images=image_list)
        return image_list, labels_batch
    
  2. Convert to a supported dtype
    Modify the function to cast your images to float32 (ideal if your images are normalized to 0-1) or uint8 (if they’re in the 0-255 pixel range) right before running the augmentation:

    def create_augmented_batch(self, index):
        # call in the training images
        image_list, labels_batch = self._create_balanced_batch(index)
        if self.augmentation is not None:
            # Convert to float32 (switch to uint8 if your images use 0-255 range)
            image_list = image_list.astype(np.float32)
            image_list = self.augmentation.augment(images=image_list)
        return image_list, labels_batch
    

Quick Note on Noise Parameters

If you switch to uint8 images (0-255 pixel range), adjust your AdditiveGaussianNoise scale to match that range. Your current scale=(0.0, 0.2) works for normalized 0-1 images, but for uint8 you’d want something like scale=(0, 51) (since 0.2 * 255 ≈ 51) to avoid under/overflow issues with pixel values.

That should resolve the dtype error while keeping all your other augmentations working as expected!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.29 11:32:39