mingyuliutw / UNIT

Unsupervised Image-to-Image Translation
Other
1.98k stars 360 forks source link

OpenCV Error: Assertion failed (scn == 3 || scn == 4) in cvtColor #45

Closed christopher-beckham closed 6 years ago

christopher-beckham commented 6 years ago

I seem to be getting this error. AFAIK I correctly did the CelebA pre-processing by first extracting the original CelebA data and running the resize/crop script which generates the img_align_crop_resize_celeba directory.

Here is the error:

$ python cocogan_train.py --config ../exps/unit/blondhair.yaml --log ../logs
self.image_display_iterations=100
self.image_save_iterations=2500
self.snapshot_save_iterations=5000
self.snapshot_prefix='../outputs/unit/celeba/blondhair/blondhair'
self.hyperparameters={'trainer': 'COCOGANTrainer', 'kl_cycle_link_w': 0.1, 'gan_w': 10, 'll_cycle_link_w': 100, 'batch_size': 1, 'll_direct_link_w': 100, 'lr': 0.0001, 'kl_direct_link_w': 0.1, 'max_iterations': 2000000, 'gen': {'ch': 64, 'name': 'COCOResGen2', 'n_gen_front_blk': 3, 'n_enc_front_blk': 3, 'input_dim_a': 3, 'n_enc_shared_blk': 1, 'input_dim_b': 3, 'n_gen_res_blk': 3, 'n_enc_res_blk': 3, 'n_gen_shared_blk': 1}, 'dis': {'n_front_layer': 2, 'n_shared_layer': 4, 'ch': 64, 'name': 'COCOSharedDis', 'input_dim_a': 3, 'input_dim_b': 3}}
self.datasets={'train_a': {'channels': 3, 'scale': 1.0, 'class_name': 'dataset_celeba', 'folder': 'img_align_crop_resize_celeba/', 'crop_image_size': 128, 'root': '../datasets/celeba/', 'list_name': 'lists/Blond_Hair_ON.txt'}, 'train_b': {'channels': 3, 'scale': 1.0, 'class_name': 'dataset_celeba', 'folder': 'img_align_crop_resize_celeba/', 'crop_image_size': 128, 'root': '../datasets/celeba/', 'list_name': 'lists/Blond_Hair_OFF.txt'}}
self.display=1
dataset=dataset_celeba(conf)
dataset=dataset_celeba(conf)
OpenCV Error: Assertion failed (scn == 3 || scn == 4) in cvtColor, file /home/travis/miniconda/conda-bld/work/opencv-2.4.11/modules/imgproc/src/color.cpp, line 3650
OpenCV Error: Assertion failed (scn == 3 || scn == 4) in cvtColor, file /home/travis/miniconda/conda-bld/work/opencv-2.4.11/modules/imgproc/src/color.cpp, line 3650
OpenCV Error: Assertion failed (scn == 3 || scn == 4) in cvtColor, file /home/travis/miniconda/conda-bld/work/opencv-2.4.11/modules/imgproc/src/color.cpp, line 3650
OpenCV Error: Assertion failed (scn == 3 || scn == 4) in cvtColor, file /home/travis/miniconda/conda-bld/work/opencv-2.4.11/modules/imgproc/src/color.cpp, line 3650
OpenCV Error: Assertion failed (scn == 3 || scn == 4) in cvtColor, file /home/travis/miniconda/conda-bld/work/opencv-2.4.11/modules/imgproc/src/color.cpp, line 3650
OpenCV Error: Assertion failed (scn == 3 || scn == 4) in cvtColor, file /home/travis/miniconda/conda-bld/work/opencv-2.4.11/modules/imgproc/src/color.cpp, line 3650
OpenCV Error: Assertion failed (scn == 3 || scn == 4) in cvtColor, file /home/travis/miniconda/conda-bld/work/opencv-2.4.11/modules/imgproc/src/color.cpp, line 3650
OpenCV Error: Assertion failed (scn == 3 || scn == 4) in cvtColor, file /home/travis/miniconda/conda-bld/work/opencv-2.4.11/modules/imgproc/src/color.cpp, line 3650
Iteration: 00000001/02000000
Iteration: 00000002/02000000
Traceback (most recent call last):
  File "cocogan_train.py", line 88, in <module>
    main(sys.argv)
  File "cocogan_train.py", line 56, in main
    for it, (images_a, images_b) in enumerate(izip(train_loader_a,train_loader_b)):
  File "/u/beckhamc/.conda/envs/pytorch-env/lib/python2.7/site-packages/torch/utils/data/dataloader.py", line 187, in __next__
    return self._process_next_batch(batch)
  File "/u/beckhamc/.conda/envs/pytorch-env/lib/python2.7/site-packages/torch/utils/data/dataloader.py", line 221, in _process_next_batch
    raise batch.exc_type(batch.exc_msg)
cv2.error: Traceback (most recent call last):
  File "/u/beckhamc/.conda/envs/pytorch-env/lib/python2.7/site-packages/torch/utils/data/dataloader.py", line 40, in _worker_loop
    samples = collate_fn([dataset[i] for i in batch_indices])
  File "/data/work/beckhamc/UNIT/src/datasets/dataset_celeba.py", line 28, in __getitem__
    crop_img = self._load_one_image(self.images[index])
  File "/data/work/beckhamc/UNIT/src/datasets/dataset_celeba.py", line 34, in _load_one_image
    img = cv2.cvtColor(cv2.imread(img_name), cv2.COLOR_BGR2RGB)
error: /home/travis/miniconda/conda-bld/work/opencv-2.4.11/modules/imgproc/src/color.cpp:3650: error: (-215) scn == 3 || scn == 4 in function cvtColor

I think I know what the error means: it seems to be expecting 3/4 channels for a particular image (is it trying to convert a b/w image to RGB??), but I'm just wondering why I'm getting this issue in the first place since nobody else has raised it here!

I'm using the OpenCV suggested in USAGE.md: conda install -y -c menpo opencv

mingyuliutw commented 6 years ago

This is strange. After the conversion, the image should be still 3 channel RGB image. Have you checked if the image is read correctly?

christopher-beckham commented 6 years ago

I ran a script that read all the images in and verified their # channels was == 3, and it worked. I used this exact same line: cv2.cvtColor(imread(file), cv2.COLOR_BGR2RGB). Indeed a mystery.

EDIT: it turns out not all the images were converted. :O I'm running the convert script again and making sure this time it processes everything.

christopher-beckham commented 6 years ago

Never mind, turns out that the problem.

Thanks!