decompress: reduce memory usage for single tile images
Trying to decompress a relatively big image, that was (slowly) compressed with grk_compress (or openJpeg) there is a bug, with following error message: [2021-12-16 10:57:56.359] [error] Failed to allocate aligned memory buffer of dimensions 25024 x 25000 [2021-12-16 10:57:56.376] [error] Not enough memory for tile data [2021-12-16 10:57:56.376] [error] Failed to decompress tile 0/1 [2021-12-16 10:57:56.377] [error] Failed to decompress JP2 file
Memory allocation seems to work with grk_compress, not with grk_decompress on big images. If necessary, I amy provide such a test image (but don't attach it here due to its volume 1.8 Gb in tiff uncompressed)
Can you please share your :
- OS
- Available system RAM memory
- compress and decompress command line parameters
Thanks!
Also, can you make the test image available ?
Can you please share your :
OS: Win10 64 bits Available system RAM memory 8 Gb compress and decompress command line parameters grk_decompress -i 935000-6565000-E100.jp2 -o test.tif [2021-12-16 15:20:13.000] [error] Failed to allocate aligned memory buffer of dimensions 25024 x 25000 [2021-12-16 15:20:13.013] [error] Not enough memory for tile data [2021-12-16 15:20:13.018] [error] Failed to decompress tile 0/1 [2021-12-16 15:20:13.019] [error] Failed to decompress JP2 file Thanks!
ok I can try uploading this file (935000-6565000-E100.jp2 756 Mb compressed lossless. but it does not work. I guess you may use any jp2 big file on the web, e.g. https://www.l3harrisgeospatial.com/Data-Imagery/Satellite-Imagery/High-Resolution/Pleiades If necessary, we may arrange some alternate way so as to be able to provide you this sample.
Thanks, @EmDevys . Would you mind verifying that you get the same error on the Pleiades image you shared ? Then I can test that one.
By the way, if you compress the original using tiles, you will probably not run into this error.
For example:
grk_compress -i Foo.tif -o Foo.jp2 -t 2048,2048 -v
@boxerab I did check the error occurs on the Pleiades image. Please download it at https://l3harrisgeospatial-webcontent.s3.amazonaws.com/MM_Samples/Pleiades_ORTHO_UTM_BUNDLE.zip (about 490 Mb). Then unzip it and select the IMG_PHR1A_P_001 folder where you'll find the IMG_PHR1A_P_201202250025599_ORT_IPU_20120504_1772-002_R1C1.JP2 file (about 390 Mb). on my machine (Win10 64 bits, 8Bg memory), I get the following message: grk_decompress -i IMG_PHR1A_P_201202250025599_ORT_IPU_20120504_1772-002_R1C1.JP2 -o test.tif [2021-12-20 14:26:24.201] [error] Failed to allocate aligned memory buffer of dimensions 64 x 64 [2021-12-20 14:26:24.201] [error] Failed to allocate aligned memory buffer of dimensions 64 x 64 [2021-12-20 14:26:24.201] [error] Failed to allocate aligned memory buffer of dimensions 64 x 64 [2021-12-20 14:26:24.201] [error] Failed to allocate aligned memory buffer of dimensions 64 x 64 [2021-12-20 14:26:24.201] [error] Failed to allocate aligned memory buffer of dimensions 64 x 64 [2021-12-20 14:26:24.201] [error] Failed to allocate aligned memory buffer of dimensions 2048 x 2048 [2021-12-20 14:26:24.201] [error] Failed to allocate aligned memory buffer of dimensions 64 x 64 [2021-12-20 14:26:24.287] [error] Failed to allocate aligned memory buffer of dimensions 2048 x 2048 [2021-12-20 14:26:25.014] [error] Not enough memory for tile data [2021-12-20 14:26:25.015] [error] Failed to decompress tile 144/273 [2021-12-20 14:26:24.695] [error] Failed to allocate aligned memory buffer of dimensions 2048 x 2048 [2021-12-20 14:26:25.015] [error] Not enough memory for tile data [2021-12-20 14:26:25.015] [error] Failed to decompress tile 164/273 [2021-12-20 14:26:24.695] [error] Failed to allocate aligned memory buffer of dimensions 2048 x 2048 [2021-12-20 14:26:25.016] [error] Not enough memory for tile data [2021-12-20 14:26:25.016] [error] Failed to decompress tile 163/273 [2021-12-20 14:26:24.985] [error] Not enough memory for tile data [2021-12-20 14:26:24.479] [error] Failed to allocate aligned memory buffer of dimensions 2048 x 2048 [2021-12-20 14:26:25.054] [error] Not enough memory for tile data [2021-12-20 14:26:25.054] [error] Failed to decompress tile 145/273 [2021-12-20 14:26:25.054] [error] Failed to decompress tile 143/273
Note: this file is tiled 2048x2048 Hoping this helps
Thanks. I'm pretty sure the issue is with the Grok tile cache: all decompressed tiles are cached, and then composited into a single buffer which is then written out to disk. There are a couple of fixes possible to reduce memory consumption:
- store tile data in native bit depth such as 8 or 12, rather than current 32 bit depth
- store composited data in native bit depth
- write tiles directly to disk without intermediate composite
All of these tasks are non-trivial, can't say when they will be implemented. In the mean time, you could either decompress a smaller region of the image, or run on a system with 16 GB RAM.
@boxerab Aaron, thanks for echo and advice. It seems it occurs on other "big" images 8 bits depth. But on the same laptop, with only 8 Gb. Other j2k drivers such as Kakadu or OpenJpeg work on such images, on such limted memory laptops, therefore in some future, I guess it would be of interest to fix this, when time allows. Otherwise the level of performance of Grok seems quite good.
@EmDevys yes, reducing memory usage is on the road map for the project. I have made a new change that reduces memory usage for tiled images. If you are able to test the change on the Pleiades image on your system and let me know if it works, I would appreciate it.
@boxerab Thanks for this effort. I would gladly test this updated version, by any chance would you have the binaries for win64 (Win 10) available. Would be great. If necessary, my email may be used.
@EmDevys I have issued a new release with the memory reduction
https://github.com/GrokImageCompression/grok/releases/tag/v9.6.0
There is a windows binary attached to the release.
@boxerab I tried on both the Pleiades JPEG 2000 and on my other big file (8 bits depth). Neither works on my win64 win10 8 Gb memory. For the Pleiades file, I get the following error message: grk_decompress -i IMG_PHR1A_P_201202250025599_ORT_IPU_20120504_1772-002_R1C1.JP2 -o test.tif [2021-12-29 18:50:55.058] [error] Failed to allocate aligned memory buffer of dimensions 64 x 64 [2021-12-29 18:50:55.058] [error] Failed to allocate aligned memory buffer of dimensions 64 x 64 [2021-12-29 18:50:55.058] [error] Failed to allocate aligned memory buffer of dimensions 64 x 64 [2021-12-29 18:50:55.058] [error] Failed to allocate aligned memory buffer of dimensions 64 x 64 [2021-12-29 18:50:55.058] [error] Failed to allocate aligned memory buffer of dimensions 64 x 64 [2021-12-29 18:50:55.058] [error] Failed to allocate aligned memory buffer of dimensions 64 x 64
- a system error message as follows (seems ratehr dangerous, with display being stopped during one or 2 seconds): The instruction at 0x00007FFC2CAA3D3A uses the memory address 0x0000000000001D1C. Yhe memory state can't be written. Click on OK to terminate the program. Click on Cancel in order to debug the program.
For my other test file, I get the following error message (but no system error): grk_decompress -i 935000-6565000-E100.jp2 -o test.tif [2021-12-29 18:47:56.354] [error] Failed to allocate aligned memory buffer of dimensions 64 x 64 [2021-12-29 18:47:56.378] [error] Failed to allocate aligned memory buffer of dimensions 64 x 64 [2021-12-29 18:47:56.379] [error] Failed to allocate aligned memory buffer of dimensions 64 x 64 [2021-12-29 18:47:56.405] [error] Failed to allocate aligned memory buffer of dimensions 64 x 64 [2021-12-29 18:47:56.416] [error] Failed to allocate aligned memory buffer of dimensions 64 x 64 [2021-12-29 18:47:56.416] [error] Failed to allocate aligned memory buffer of dimensions 64 x 64
It seems there is a severe memory issue with this version on the Pleiades image, I don't know whether you face a similar error on your machine (or not). Hoping this helps
@EmDevys thank - you are right - there were some problems with that release. There is a new windows binary for version 9.6.0 - it has even lower memory usage, and it should be faster. From my tests, it uses only ~4.5 GB of memory for the Pleiades image. Let me know how it works on your system, when you have time.
@boxerab good news, it now works on the pleiades image. Unfortunately, still an issue with my other test sample: 25024 x 25000 pixels, RGB 8bits per band (24 bits per pixel) grk_decompress -i 935000-6565000-E100.jp2 -o test.tif [2021-12-30 15:18:30.567] [error] Failed to allocate aligned memory buffer of dimensions 25024 x 25000 [2021-12-30 15:18:30.596] [error] Not enough memory for tile data [2021-12-30 15:18:30.596] [error] Failed to decompress tile 0/1 [2021-12-30 15:18:30.597] [error] Failed to decompress JP2 file.
I added this image under my google drive. the link is https://drive.google.com/file/d/1Pu-xHKmprWLDA91gtEXInfDTGL-TxTNe/view?usp=sharing. If you have some email, I can allow you access to this test data.
@EmDevys thanks, good to hear. Your RGB test image is twice as large (uncompressed) as the Peiades image, that's why you are running out of memory. It's possible to drop memory for this image as well, but it will take some time. In the mean time, are you able to run kakadu, grok and openjpeg decompress on the Pleiades image and time the results ? I'm curious about performance on windows.
@boxerab Yes, I'm aware this is really a huge image. However both kakadu and openjpeg are able to decompress it. I did the evaluation of time performance on my very basic laptop (HP Probook 8 Gb memory) with the pleiades image. The results are: measure-command { grk_decompress -i IMG_PHR1A_P_201202250025599_ORT_IPU_20120504_1772-002_R1C1.JP2 -o test.tif} => TotalSeconds : 46,840678 measure-command {kdu_expand -i IMG_PHR1A_P_201202250025599_ORT_IPU_20120504_1772-002_R1C1.JP2 -o test_kdu.tif} => TotalSeconds : 30,9305125 (latest 8.2 version) measure-command {opj_decompress -i IMG_PHR1A_P_201202250025599_ORT_IPU_20120504_1772-002_R1C1.JP2 -o test_jph.tif} => TotalSeconds : 114,5239408 So Grok is about 1.5 slower than kakadu (which seems to be a good performance) and 2.5 faster than Openjpeg.
@EmDevys thanks for the timings. Depending on how many threads you have on your system, you should set the number of threads to use for openjpeg:
If you have 8 threads, then the command is:
opj_decompress -i IMG_PHR1A_P_201202250025599_ORT_IPU_20120504_1772-002_R1C1.JP2 -o test_jph.tif -threads 8 -quiet
This should give a big boost in performance for that library.
@boxerab if i set the number of threads, I get the following results:
- kakadu : measure-command {kdu_expand -i IMG_PHR1A_P_201202250025599_ORT_IPU_20120504_1772-002_R1C1.JP2 -o test_kdu.tif -num_threads 8} => TotalSeconds : 12,629738
- grok : measure-command {grk_decompress -i IMG_PHR1A_P_201202250025599_ORT_IPU_20120504_1772-002_R1C1.JP2 -o test.tif -num_threads 8 } => TotalSeconds : 38,3554907
- openjpeg : measure-command {opj_decompress -i IMG_PHR1A_P_201202250025599_ORT_IPU_20120504_1772-002_R1C1.JP2 -o test_jph.tif -threads 8 -quiet} => TotalSeconds : 47,2045008
so Kakadu is 3 times faster than grok, and 3.74 faster than openjpeg
Cheers for new year eve
Thanks, @EmDevys. Those results look a bit odd. If number of threads is not specified for kakadu, then it will use the maximum number of threads available. So, setting num_threads to 8 shouldn't make it any faster :) How many cores do you have on your laptop, and do you have hyperthreading ?
Also, can you verify that all three output files are identical ? You can use Graphics Magick for this:
gm compare FOO.tif BAR.tif --metric=PSNR
you should get INF for all outputs.
Also, Happy New Year to you !
@EmDevys when you have time, please try out the latest windows release binary. I have significantly dropped memory usage so you should now be able to decompress that 25024 x 25000 RGB image on your laptop.
@boxerab I did the comparison tests, as follows: grok vs kdu, then kdu vs openjpeg gm compare test.tif test_kdu.tif -metric PSNR -verbose Image Difference (PeakSignalToNoiseRatio): PSNR ====== Red: 94.86 Green: 94.86 Blue: 94.86 Total: 94.86 gm compare test_jph.tif test_kdu.tif -metric PSNR -verbose Image Difference (PeakSignalToNoiseRatio): PSNR ====== Red: 94.86 Green: 94.86 Blue: 94.86 Total: 94.86 By the way the images look exactly the same.
Unfortunately, still an arror message as follows: grk_decompress -i 935000-6565000-E100.jp2 -o test.tif [2022-01-01 15:07:31.409] [error] Failed to allocate aligned memory buffer of dimensions 25024 x 25000 [2022-01-01 15:07:31.434] [error] Not enough memory for tile data [2022-01-01 15:07:31.437] [error] Failed to decompress tile 0/1 [2022-01-01 15:07:31.437] [error] Failed to decompress JP2 file
grk_decompress -i 935000-6565000-E100.jp2 -o test.tif -version grk_decompress version: 9.6.0
@EmDevys thanks - I just realized that the image is lossy compressed, so you shouldn't expect them to be identical for different j2k libraries.
I guess I should take a look at the actual file that still causes error - you can share it with me using the email:
boxerab at protonmail dot com
@boxerab just shared the file on my google drive. Hoping this helps to check and fix the issue.
@EmDevys thanks very much for the file. I see it is a single tile image, so my recent memory reduction work won't apply to this format. This will take a bit longer to fix - I will let you know when I have something.
@boxerab I agree it is probably not the best sample, but it is a valid jp2 file. Thanks for the effort, waiting till there is some news