Skip to content

OneHot and ImageToTensor produce different dimension orders #145

Backlog

Description

@CarloLucibello

ImageToTensor swaps height and width, while OneHot keeps the dimensions of the mask. For an image and a mask of the same size, the two outputs therefore do not line up:

using DataAugmentation, Images

image = Image(rand(RGB{N0f8}, 4, 6))        # 4 rows, 6 columns
mask = MaskMulti(rand(1:3, 4, 6), 1:3)

size(itemdata(apply(ImageToTensor(), image)))  # (6, 4, 3), WHC
size(itemdata(apply(OneHot(), mask)))          # (4, 6, 3), HWC

A segmentation pipeline for Flux needs OneHot() |> PermuteDims(2, 1, 3) to get a target with the layout of the input, and a mask used as class indices needs a permutedims. This is easy to miss, because a square image gives matching sizes but a transposed target. Julia 1.13.4, DataAugmentation master (e389a2f).

Proposed outcome

OneHot swaps the first two dimensions of the mask, as ImageToTensor does for images, and puts the classes last: a (height, width) mask becomes (width, height, classes), and a 3D mask (width, height, depth, classes). The one-hot target then has the layout of the input tensor, Flux's WHCN convention once batched. The mask item itself, MaskMulti, keeps the layout of the image it belongs to.

This is breaking: code that relies on (height, width, classes), or permutes the result as #142 does, has to change.

🧔‍♂️🤖 Assisted by AI

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    breakingenhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions