Skip to content

Results of Buffered transformations are overwritten by the next call, which corrupts batches in collating loaders #148

Done

Description

@CarloLucibello

The result of apply with a Buffered or BufferedThreadsafe transformation is the buffer itself, which the next call overwrites (documented on the Buffering page). A data loader that stacks the observations of a batch only after loading all of them, like DataLoader(...; collate = true) from MLUtils.jl, then receives the same array for every sample. The batch contains the last sample repeated, without any error or warning.

using DataAugmentation, Images, MLUtils

images = [fill(Gray{Float32}(i), 16, 16) for i in 1:64]   # sample i has the value i
labels = collect(1:64)

tfm = CenterCrop((8, 8)) |> PinOrigin() |> ImageToTensor()
btfm = BufferedThreadsafe(tfm; n = Threads.maxthreadid())   # n avoids #143

data = mapobs(((img, y),) -> (itemdata(apply(btfm, Image(img))), y), (images, labels))

wrong = 0
for (x, y) in DataLoader(data; batchsize = 16, collate = true)
    wrong += count(j -> x[1, 1, 1, j] != y[j], 1:length(y))
end
wrong  # 60 of the 64 samples

parallel = true gives the same result, and wrapping the buffered result in copy gives 0 wrong samples. Julia 1.13.4, DataAugmentation master (e389a2f), MLUtils 0.4.16.

Possible changes:

On the value of buffering with a loader: in one quick benchmark (256×256 images, RandomResizeCrop((224, 224)), brightness, ImageToTensor, Normalize; a sequential loop over the samples), BufferedThreadsafe plus copy took 1.07 ms per sample against 1.02 ms without buffering, while allocating 599 KiB instead of 1510 KiB. So the saving is in allocations, not in time, at least for this pipeline.

🧔‍♂️🤖 Assisted by AI

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

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions