ms-swift icon indicating copy to clipboard operation
ms-swift copied to clipboard

Image augmentation is a bit tricky, tied to models, and hard to do for training data only

Open sliedes opened this issue 3 months ago • 3 comments

Checklist / 检查清单

  • [x] I have searched existing issues, and this is a new question or discussion topic. / 我已经搜索过现有的 issues,确认这是一个新的问题与讨论。

Question Description / 问题描述

I wrote an image augmentation plugin (code below). It does what I want, but there are a few things I find a bit tricky here, and I wonder if they could be done better:

  1. I only want to augment the training dataset, not the validation one. I think the template doesn't get the information of which dataset is being used. I solve this by adding an extra field _augment_image to my dataset, and then I also need to pass --remove_unused_columns false on the command line. Of course this also means I couldn't use it with non-JSONL datasets.
  2. I think the current approach of overriding a template couples more things than it ideally should. In my plugin, I define augmentation for Qwen 3.5. But, it seems to me that image augmentation should have nothing to do with the model used. As I understand it, currently there's very little ways to write an image augmentation plugin that works with any multimodal model.

Here's what I use; I have a separate Python package swiftwrap installed in the same venv as swift:

swiftwrap/_image_augmentation.py:

import numpy as np
import swift.utils
from PIL import Image as PILImage
from swift.template import StdTemplateInputs, Template
from swift.template.templates.qwen import Qwen3_5Template
from torchvision.transforms import v2 as tv_v2

# The logic:
# - If the image has more pixels than _MIN_PIXELS, scale it randomly between _MIN_PIXELS and _MAX_PIXELS
# - If the image has fewer pixels than _MIN_PIXELS, scale it anywhere between its original size and _MAX_PIXELS
#
# That is, _MIN_PIXELS is not our absolute minimum pixels; it's the threshold below which we do not scale down
# images larger than _MIN_PIXELS.

DEBUG = False

_MAX_PIXELS = 2880**2
_MIN_PIXELS = 800**2

logger = swift.utils.get_logger()


def _augment_image(image: PILImage.Image) -> PILImage.Image:
    im = tv_v2.functional.to_image(image)
    channels, width, height = im.size()

    if DEBUG:
        logger.info(
            "Got image of size: %dx%d, %d channels; min=%f, max=%f",
            width,
            height,
            channels,
            im.min().item(),
            im.max().item(),
        )
    transforms = tv_v2.Compose(
        [
            tv_v2.RandomRotation(
                degrees=15,
                expand=True,
                fill=im.max().item(),
                interpolation=tv_v2.InterpolationMode.BILINEAR,
            ),
            tv_v2.ColorJitter(brightness=0.5, contrast=0.5, saturation=0.5, hue=0.5),
        ]
    )
    im = transforms(im)

    # torchvision Resize supports maximum edge sizes but not maximum pixel counts, so no Compose
    num_pixels = width * height
    # compute max and min scale factors based on pixel constraints
    max_scale = (_MAX_PIXELS / num_pixels) ** 0.5 if num_pixels > _MAX_PIXELS else 1.0
    # We allow original size even if it's below _MIN_PIXELS
    min_scale = 1.0 if num_pixels <= _MIN_PIXELS else (_MIN_PIXELS / num_pixels) ** 0.5
    scale = np.random.uniform(min_scale, max_scale)
    im = tv_v2.functional.resize(
        im,
        [int(height * scale), int(width * scale)],
        interpolation=tv_v2.InterpolationMode.BILINEAR,
    )
    image = tv_v2.functional.to_pil_image(im)
    if DEBUG:
        logger.info(
            "Returning augmented image of size: %dx%d", image.width, image.height
        )
    return image


class _ImageAugmentationMixin(Template):
    def _preprocess_inputs(self, inputs: StdTemplateInputs) -> None:
        super()._preprocess_inputs(inputs)

        augment = inputs.extra_kwargs.get("_augment_image")
        if not isinstance(augment, bool):
            raise TypeError(
                f"Every sample must contain boolean '_augment_image'; got {augment!r}. "
                " Did you forget to run with --remove_unused_columns false?"
            )

        if not augment:
            return

        for i, image in enumerate(inputs.images):
            if not isinstance(image, PILImage.Image):
                raise TypeError(
                    f"Expected preprocessing to produce PIL.Image.Image, "
                    f"got {type(image)!r}"
                )
            inputs.images[i] = _augment_image(image)


class AugmentedQwen35Template(
    _ImageAugmentationMixin,
    Qwen3_5Template,
):
    pass

image_augmentation_plugin.py:

from copy import deepcopy

from swift.template import TEMPLATE_MAPPING, register_template

from swiftwrap._image_augmentation import AugmentedQwen35Template

meta = deepcopy(TEMPLATE_MAPPING["qwen3_5"])
meta.template_type = "qwen3_5_augmented"
meta.template_cls = AugmentedQwen35Template

register_template(meta)

Then I add "augment_image": true to all rows in my training dataset and "augment_image": false to all rows in my validation dataset and run swift with --remove_unused_columns false --external_plugins image_augmentation_plugin.py.

(I also wish --external_plugins would take Python module names, but that's not a major hurdle here!)

sliedes avatar Aug 31 '26 09:08 sliedes

This looks like a useful framework-level gap. I would like to implement a small, model-agnostic first version if the maintainers agree with the ownership boundary below.

Proposed first-PR boundary

  • Add an optional training-media transform hook owned by the dataset/preprocessing pipeline, before a model-specific template converts media into model inputs.
  • Let an external plugin register/provide the transform callable; ms-swift would define the invocation contract but would not depend on torchvision or ship augmentation policies.
  • Apply the hook only to training data through pipeline context, without requiring a per-row marker and without requiring --remove_unused_columns false.
  • Support one or multiple images while preserving their order and cardinality.
  • Keep validation/evaluation data unchanged by default.
  • Make random behavior reproducible from the existing training/data-worker seed contract; the transform itself remains user-owned.

The hook should receive normalized image objects plus minimal sample context, but should not require subclassing Qwen3_5Template or any other model template.

Explicit non-goals

  • Built-in augmentation recipes, model-specific transforms, validation-time/test-time augmentation, video/audio transforms, changes to template tokenization, or changes to --external_plugins module-name handling. Those can remain separate follow-ups.

Validation plan

  • Unit tests proving the hook runs exactly once for training samples and never for validation samples.
  • Cover JSONL and a non-JSON dataset path, with normal unused-column removal enabled.
  • Cover multi-image samples, transform exceptions, no-hook backward compatibility, and deterministic behavior under fixed data-worker seeds.
  • Exercise the same recording transform through two existing multimodal templates to prove the contract is model-agnostic.
  • Run a one-step VLM training smoke test with an external plugin and verify that evaluation inputs remain byte/pixel-equivalent to the untransformed path.
  • Run the repository pre-commit hooks and focused dataset/template tests.

Would maintainers prefer this hook to live in the dataset preprocessor or in the template input-normalization layer? I favor the dataset/preprocessing owner because it naturally knows the train/eval split, while the model template should remain unaware of augmentation policy.

WenzheWang avatar Sep 02 '26 14:09 WenzheWang

Thanks for raising this — the limitation does exist in the current version. We're working on a full refactor of swift, which we expect to complete in about a month(more or less). For now, you can keep using your current approach as a workaround; once the refactor lands, this will be properly resolved. You can follow the progress on this branch: https://github.com/modelscope/ms-swift/tree/dev-swift-v5/swift/dev

tastelikefeet avatar Sep 05 '26 02:09 tastelikefeet

There is also a concrete issue in the augmentation function: to_image() produces (C, H, W), but the code unpacks it as channels, width, height. A 640×360 image therefore gets resized to 360×640 even with zero rotation and scale 1.

Use channels, height, width = im.shape for the initial logging. Then recompute the dimensions after RandomRotation(expand=True), since expansion changes the pixel count:

im = transforms(im)
_, height, width = im.shape
num_pixels = height * width

min_scale = (min(_MIN_PIXELS, num_pixels) / num_pixels) ** 0.5
max_scale = (_MAX_PIXELS / num_pixels) ** 0.5
scale = np.random.uniform(min_scale, max_scale)

im = tv_v2.functional.resize(
    im,
    [max(1, int(height * scale)), max(1, int(width * scale))],
    interpolation=tv_v2.InterpolationMode.BILINEAR,
)

This also follows the stated policy of allowing upscaling toward _MAX_PIXELS; the original max_scale was capped at 1 for every image below that limit. The training/evaluation hook limitation is separate from these shape issues.

yyh-sjtu avatar Nov 02 '26 18:11 yyh-sjtu