Open NitishJaiswal opened 3 years ago
in infer.py, change Line 148 from
Image.fromarray(final).save(save_path)
to
Image.fromarray(final.squeeze()).save(save_path)
Thanks for sharing comment @Rothfeld , np.squeeze resolves this issue
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
@NitishJaiswal it was working in #13
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
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?
Output