Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dimension mismatch with loading a lazily loaded image to memory #177

Closed
RpfR2000 opened this issue Dec 6, 2024 · 19 comments · Fixed by #179
Closed

Dimension mismatch with loading a lazily loaded image to memory #177

RpfR2000 opened this issue Dec 6, 2024 · 19 comments · Fixed by #179

Comments

@RpfR2000
Copy link

RpfR2000 commented Dec 6, 2024

I have loaded in a large image (26 GB) lazily with the lazyio=true flag and am now trying to load a subset of that image to memory by indexing as follows:

chunk = Array{Gray{N0f16}, 3}(undef, y_end-y_start+1, x_end-x_start+1, slices)
for k in 1:slices
    chunk[:,:,k] .= image[y_start:y_end, x_start:x_end, k]
end

This gives the following error: LoadError: DimensionMismatch: parent has 18596 elements, which is incompatible with size (256, 256). The reason I am loading in a loop like this is because I couldn't even @show the size of a 3D chunk of the image without the same error being thrown. I can @show the size of a slice, though (not sure why). I have verified that the size and eltype of the lazily loaded image is correct. Any ideas what is going on?

@tlnagy
Copy link
Owner

tlnagy commented Dec 6, 2024

Does image[y_start:y_end, x_start:x_end, k] give you the dimensions and eltype of what you expect? Have you tried loading the image with mmap=true instead of lazyio?

@RpfR2000
Copy link
Author

RpfR2000 commented Dec 6, 2024

Yes, image[y_start:y_end, x_start:x_end, k] gives the correct eltype and dimensions. I just tried mmap=true and got LoadError: BoundsError: attempt to access 0-element Vector{TiffImages.Tag} at index [1].

@tlnagy
Copy link
Owner

tlnagy commented Dec 6, 2024

Interesting. Can you give me the output of first(ifds(img))?

@RpfR2000
Copy link
Author

RpfR2000 commented Dec 6, 2024

first(ifds(image)) = IFD, with tags:
Tag(SUBFILETYPE, 2)
Tag(IMAGEWIDTH, 10138)
Tag(IMAGELENGTH, 10138)
Tag(BITSPERSAMPLE, 16)
Tag(COMPRESSION, COMPRESSION_NONE)
Tag(PHOTOMETRIC, 1)
Tag(IMAGEDESCRIPTION, "<?xml version="1.0" ...")
Tag(SAMPLESPERPIXEL, 1)
Tag(SOFTWARE, "OME Files (C++) 0.6....")
Tag(TILEWIDTH, 256)
Tag(TILELENGTH, 256)
Tag(TILEOFFSETS, UInt64[16, 131088, 262160, 393232, 524304, ...])
Tag(TILEBYTECOUNTS, UInt64[131072, 131072, 131072, 131072, 131072, ...])
Tag(SAMPLEFORMAT, 1)
Tag(UNKNOWN(65325), 1329.7020542975515)
Tag(UNKNOWN(65326), 0.065)
Tag(UNKNOWN(65327), 1.0)
Tag(UNKNOWN(65328), 1.0)
Tag(UNKNOWN(65329), Float64[-13174.5, -14112.900000000001, 4663.08, 1329.7020542975515])

@tlnagy
Copy link
Owner

tlnagy commented Dec 9, 2024

Ah, the image is tiled. mmap doesn't support tiled images if I remember properly, but lazyio should, so let's see if we can diagnose this.

This gives the following error: LoadError: DimensionMismatch: parent has 18596 elements, which is incompatible with size (256, 256).

This error now makes more sense, since the tile sizes are 256x256. Do you mind providing the full stacktrace (all of the output) for lazyio=true error? I'm curious where it's failing.

@tlnagy
Copy link
Owner

tlnagy commented Dec 9, 2024

I suspect the error is being triggered because it's unaware that the image is tiled. Actually it looks like it should set the cache size properly

TiffImages.jl/src/layout.jl

Lines 138 to 141 in f4d285b

if istiled(ifd)
tile_width = tilecols(ifd)
tile_height = tilerows(ifd)
return Array{pixeltype}(undef, cld(ncols(ifd), tile_width) * tile_width, cld(nrows(ifd), tile_height) * tile_height)

If you lazyio load the image, what's the output of size(img.cache) and size(img)

@RpfR2000
Copy link
Author

RpfR2000 commented Dec 9, 2024

size(img.cache) doesn't work, I get type DenseTaggedImage has no field cache.

size(image) = (10138, 10138, 135)

Full stacktrace:

ERROR: LoadError: DimensionMismatch: parent has 18596 elements, which is incompatible with size (256, 256)
Stacktrace:
  [1] _throw_dmrs(n::Int64, str::String, dims::Tuple{Int64, Int64})
    @ Base ./reshapedarray.jl:182
  [2] _reshape
    @ ./reshapedarray.jl:177 [inlined]
  [3] reshape
    @ ./reshapedarray.jl:112 [inlined]
  [4] reshape
    @ ./reshapedarray.jl:117 [inlined]
  [5] _broadcast_getindex_evalf
    @ ./broadcast.jl:709 [inlined]
  [6] _broadcast_getindex
    @ ./broadcast.jl:682 [inlined]
  [7] _getindex
    @ ./broadcast.jl:706 [inlined]
  [8] _broadcast_getindex
    @ ./broadcast.jl:681 [inlined]
  [9] getindex
    @ ./broadcast.jl:636 [inlined]
 [10] macro expansion
    @ ./broadcast.jl:1004 [inlined]
 [11] macro expansion
    @ ./simdloop.jl:77 [inlined]
 [12] copyto!
    @ ./broadcast.jl:1003 [inlined]
 [13] copyto!
    @ ./broadcast.jl:956 [inlined]
 [14] copy
    @ ./broadcast.jl:928 [inlined]
 [15] materialize
    @ ./broadcast.jl:903 [inlined]
 [16] tile(cache::Matrix{Gray{N0f16}}, ifd::TiffImages.IFD{UInt64})
    @ TiffImages ~/.julia/packages/TiffImages/Xrmi4/src/load.jl:151
 [17] transform
    @ ~/.julia/packages/TiffImages/Xrmi4/src/load.jl:139 [inlined]
 [18] getindex(A::LazyBufferedTIFF{Gray{N0f16}, UInt64, Matrix{Gray{N0f16}}}, i1::Int64, i2::Int64, i::Int64)
    @ TiffImages ~/.julia/packages/TiffImages/Xrmi4/src/types/lazy.jl:129
 [19] macro expansion
    @ ./multidimensional.jl:917 [inlined]
 [20] macro expansion
    @ ./cartesian.jl:64 [inlined]
 [21] macro expansion
    @ ./multidimensional.jl:912 [inlined]
 [22] _unsafe_getindex!
    @ ./multidimensional.jl:925 [inlined]
 [23] _unsafe_getindex(::IndexCartesian, ::LazyBufferedTIFF{Gray{N0f16}, UInt64, Matrix{Gray{N0f16}}}, ::UnitRange{Int64}, ::UnitRange{Int64}, ::Int64)
    @ Base ./multidimensional.jl:903
 [24] _getindex
    @ ./multidimensional.jl:889 [inlined]
 [25] getindex
    @ ./abstractarray.jl:1291 [inlined]
 [26] getindex
    @ ~/.julia/packages/TiffImages/Xrmi4/src/types/dense.jl:67 [inlined]

@tlnagy
Copy link
Owner

tlnagy commented Dec 10, 2024

size(img.cache) doesn't work, I get type DenseTaggedImage has no field cache.

That means that you weren't loading using with lazyio=true, do you mind trying with that flag?

@RpfR2000
Copy link
Author

This was the code I ran:

image = TiffImages.load(path*filename; lazyio=true) 
@show size(image.cache)
@show size(image)

@tlnagy
Copy link
Owner

tlnagy commented Dec 10, 2024

Ah, my bad. I forgot that we wrap with a DenseTaggedImage, could you give me the output of size(img.data.cache)?

@RpfR2000
Copy link
Author

size(image.data.cache) = (10240, 10240)

@RpfR2000
Copy link
Author

@tlnagy perhaps I could also share the image with you if that would be easier? It is quite large though.

@tlnagy
Copy link
Owner

tlnagy commented Dec 12, 2024

Yeah, do you mind uploading it to dropbox/similar place and I can download it? It'll be easier to diagnose that way then going back and forth here.

You can send the link to my email listed on my github profile page.

@RpfR2000
Copy link
Author

Sent

@tlnagy
Copy link
Owner

tlnagy commented Dec 12, 2024

Do you mind sharing it as a public link? It's requiring me to log in to access it.

@RpfR2000
Copy link
Author

Sorry, just sent a link. Let me know if that works.

@chrstphrbrns
Copy link
Contributor

I'm preparing a PR for this

@RpfR2000
Copy link
Author

Does this require a PR?

@tlnagy
Copy link
Owner

tlnagy commented Dec 16, 2024

Thanks to @chrstphrbrns' quick work this should be fixed in the newest release v0.11.2, give it a hour or so for the version to propagate through CI etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants