shariqfarooq123 / AdaBins

Official implementation of Adabins: Depth Estimation using adaptive bins
GNU General Public License v3.0
725 stars 156 forks source link

TypeError: Cannot handle this data type: (1, 1, 480, 640), <u2 #3

Open NitishJaiswal opened 3 years ago

NitishJaiswal commented 3 years ago

I'm trying to implement it on my jupyter notebook, following Readme file, downloaded pretrained weights and saved it in a directory named pretrained. While running the code for predicting depth for a single pillow image (I used the test image given in the test_imgs), the predicted_depth is a numpy array with shape =[1,1,480,640]. I didn't understand why predicted_depth is 4 dimentional array. Moving further, I tried to run the code for predicting depths of all images from a directory so I again used the test_imgs directory containing classroom image and specified a target directory for storing 16-bit output. I'm getting type error, Can't handle this data type (1,1, 480, 640), <u2. Shouldn't the dimensions of the predicted depth output be a 2-dimentional array instead of 4?

from infer import InferenceHelper
infer_helper = InferenceHelper(dataset='nyu')
infer_helper.predict_dir("test_imgs/", "test_imgs_results/")

Output

Loading base model ()...
Using cache found in C:\Users\Hp/.cache\torch\hub\rwightman_gen-efficientnet-pytorch_master
Done.
Removing last two layers (global_pool & classifier).
Building Encoder-Decoder model..Done.
  0%|                                                                                            | 0/1 [00:04<?, ?it/s]
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
~\anaconda3\lib\site-packages\PIL\Image.py in fromarray(obj, mode)
   2679         try:
-> 2680             mode, rawmode = _fromarray_typemap[typekey]
   2681         except KeyError:

KeyError: ((1, 1, 480, 640), '<u2')

During handling of the above exception, another exception occurred:

TypeError                                 Traceback (most recent call last)
<ipython-input-1-8cfe96e3a7b5> in <module>
      6 #bin_centers, predicted_depth = infer_helper.predict_pil(img)
      7 # predict depths of images stored in a directory and store the predictions in 16-bit format in a given separate dir
----> 8 infer_helper.predict_dir("test_imgs/", "test_imgs_results/")

~\anaconda3\lib\site-packages\torch\autograd\grad_mode.py in decorate_context(*args, **kwargs)
     13         def decorate_context(*args, **kwargs):
     14             with self:
---> 15                 return func(*args, **kwargs)
     16         return decorate_context
     17 

<personal_directories>\AdaBins\infer.py in predict_dir(self, test_dir, out_dir)
    146             save_path = os.path.join(out_dir, basename + ".png")
    147 
--> 148             Image.fromarray(final).save(save_path)
    149 
    150 

~\anaconda3\lib\site-packages\PIL\Image.py in fromarray(obj, mode)
   2680             mode, rawmode = _fromarray_typemap[typekey]
   2681         except KeyError:
-> 2682             raise TypeError("Cannot handle this data type: %s, %s" % typekey)
   2683     else:
   2684         rawmode = mode

TypeError: Cannot handle this data type: (1, 1, 480, 640), <u2
Rothfeld commented 3 years ago

in infer.py, change Line 148 from Image.fromarray(final).save(save_path)

to Image.fromarray(final.squeeze()).save(save_path)

NitishJaiswal commented 3 years ago

Thanks for sharing comment @Rothfeld , np.squeeze resolves this issue

NitishJaiswal commented 3 years ago

I used .squeeze() function earlier for the same issue on a single PIL image and it worked perfectly and I assumed it should resolve when trying to do for the whole image directory. But, unexpectedly, .squeeze() function doesn't resolve the issue for that part, any ideas? Input

# predict depths of images stored in a directory and store the predictions in 16-bit format in a given separate dir
infer_helper.predict_dir("test_imgs", "test_imgs_results")  #test_imgs_results is a new directory to store output

Output

KeyError                                  Traceback (most recent call last)
D:\anaconda3\lib\site-packages\PIL\Image.py in fromarray(obj, mode)
   2750         try:
-> 2751             mode, rawmode = _fromarray_typemap[typekey]
   2752         except KeyError as e:

KeyError: ((1, 1, 480, 640), '<u2')

The above exception was the direct cause of the following exception:

TypeError                                 Traceback (most recent call last)
<ipython-input-7-36b5885687fe> in <module>
      1 # predict depths of images stored in a directory and store the predictions in 16-bit format in a given separate dir
----> 2 infer_helper.predict_dir("test_imgs", "test_imgs_results")

D:\anaconda3\lib\site-packages\torch\autograd\grad_mode.py in decorate_context(*args, **kwargs)
     24         def decorate_context(*args, **kwargs):
     25             with self.__class__():
---> 26                 return func(*args, **kwargs)
     27         return cast(F, decorate_context)
     28 

<private_repo>\infer.py in predict_dir(self, test_dir, out_dir)
    146             save_path = os.path.join(out_dir, basename + ".png")
    147 
--> 148             Image.fromarray(final.squeeze()).save(save_path)  #added .squeeze() to shrink size from 1,1,480,640 to 480,640
    149 
    150 

D:\anaconda3\lib\site-packages\PIL\Image.py in fromarray(obj, mode)
   2751             mode, rawmode = _fromarray_typemap[typekey]
   2752         except KeyError as e:
-> 2753             raise TypeError("Cannot handle this data type: %s, %s" % typekey) from e
   2754     else:
   2755         rawmode = mode

TypeError: Cannot handle this data type: (1, 1, 480, 640), <u2
INF800 commented 3 years ago

@NitishJaiswal it was working in #13

younas-aziz commented 1 year ago

Need Help: def convert_nifti_to_png(input_path, output_path): img = nib.load(input_path) data = img.get_fdata() data_normalized = (data - np.min(data)) / (np.max(data) - np.min(data)) data_scaled = (data_normalized * 255).astype(np.uint8) data_3d = np.squeeze(data_scaled) data_uint8 = data_scaled.astype(np.uint8) image = Image.fromarray(data_uint8) image.save(output_path, 'PNG')

KeyError Traceback (most recent call last) /usr/local/lib/python3.10/dist-packages/PIL/Image.py in fromarray(obj, mode) 2834 try: -> 2835 mode, rawmode = _fromarray_typemap[typekey] 2836 except KeyError as e:

KeyError: ((1, 1, 10, 30), '|u1')

The above exception was the direct cause of the following exception:

TypeError Traceback (most recent call last) 3 frames /usr/local/lib/python3.10/dist-packages/PIL/Image.py in fromarray(obj, mode) 2835 mode, rawmode = _fromarray_typemap[typekey] 2836 except KeyError as e: -> 2837 raise TypeError("Cannot handle this data type: %s, %s" % typekey) from e 2838 else: 2839 rawmode = mode

TypeError: Cannot handle this data type: (1, 1, 10, 30), |u1