InterDigitalInc / CompressAI

A PyTorch library and evaluation platform for end-to-end compression research
https://interdigitalinc.github.io/CompressAI/
BSD 3-Clause Clear License
1.19k stars 232 forks source link

Errors while using traditional VTM codec with CompressAI #91

Closed HAIIISE closed 3 years ago

HAIIISE commented 3 years ago

Hi, I am using your CompressAI to test the VTM codec and I got some problems.

I have build VTM and try to run the command python3 -m compressai.utils.bench vtm -b 'VTM build dir' -c 'config dir' dataset to use VTM codec to encode some images. But I got some errors:

Traceback (most recent call last): File "/data/anaconda3/envs/ctof/lib/python3.6/runpy.py", line 193, in _run_module_asmain "\_main", mod_spec) File "/data/anaconda3/envs/ctof/lib/python3.6/runpy.py", line 85, in _run_code exec(code, runglobals) File "/data/anaconda3/envs/ctof/lib/python3.6/site-packages/compressai/utils/bench/\_main.py", line 168, in main(sys.argv[1:]) File "/data/anaconda3/envs/ctof/lib/python3.6/site-packages/compressai/utils/bench/__main.py", line 155, in main args.numjobs, File "/data/anaconda3/envs/ctof/lib/python3.6/site-packages/utils/bench/\_main.py", line 82, in collect rv = list(starmap(func, args)) File "/data/anaconda3/envs/ctof/lib/python3.6/site-packages/compressai/utils/bench/__main__.py", line 48, in func rv = codec.run(*args) File "/data/anaconda3/envs/ctof/lib/python3.6/site-packages/compressai/utils/bench/codecs.py", line 169, in run info, rec = self._run_impl(img, quality) File "/data/anaconda3/envs/ctof/lib/python3.6/site-packages/compressai/utils/bench/codecs.py", line 528, in _run_impl arr = np.asarray(read_image(img)) File "/data/anaconda3/envs/ctof/lib/python3.6/site-packages/compressai/utils/bench/codecs.py", line 58, in read_image if not os.path.isfile(filepath): File "/data/anaconda3/envs/ctof/lib/python3.6/genericpath.py", line 30, in isfile st = os.stat(path) TypeError: stat: path should be string, bytes, os.PathLike or integer, not Image

It seems that the read_image() function got an Image input, so I tried to trace the error code. And I found that In codecs.py file, the run() of class Codec runs the _load_img(img) in line 168,

line 154: def _load_img(self, img): 
        return read_image(os.path.abspath(img))

line 168: img = self._load_img(img)

which will run the read_image() function and convert an image file into PIL Image. After then the function _run_impl() line at 169 rec = self._run_impl(img, quality) will be implemented at class VTM.

And I found this _run_impl() function in class VTM have rerun the read_image() again in line 528,

    def _run_impl(self, img, quality):
        if not 0 <= quality <= 63:
            raise ValueError(f"Invalid quality value: {quality} (0,63)")

        # Taking 8bit input for now
        bitdepth = 8

        # Convert input image to yuv 444 file
        arr = np.asarray(read_image(img)) # call read_image() again
        fd, yuv_path = mkstemp(suffix=".yuv")
        out_filepath = os.path.splitext(yuv_path)[0] + ".bin"

        arr = arr.transpose((2, 0, 1))  # color channel first

namely it calls the read_image() for a PIL Image object, thus causing the above mentioned error. So I tried to switch the line 168 & 169, just like:

        info, rec = self._run_impl(img, quality)
        img = self._load_img(img)
        info.update(compute_metrics(rec, img, metrics))

I thought the program have run successfully.

I'm not sure the above changes are correct so maybe you can check for it. Thank you.

fracape commented 3 years ago

Hi @HAIIISE, thanks for spotting this, have been using different scripts for a while for running VTM. I just pushed some results and updates, including a fix for this 70368e86e75651f6b21e054a3326cebd26d94cba. Please let me know how it goes on your end.