airctic / icevision

An Agnostic Computer Vision Framework - Pluggable to any Training Library: Fastai, Pytorch-Lightning with more to come
https://airctic.github.io/icevision/
Apache License 2.0
845 stars 149 forks source link

TypeError: image must be numpy array type #867

Closed talhaanwarch closed 3 years ago

talhaanwarch commented 3 years ago

albumentation version: 1.0.0
torch version: 1.9.0+cu102

code Data loader


class DataReader(Dataset):
  def __init__(self,df,transform=None):
    super(DataReader,self).__init__()
    self.df=df
    self.transform=transform

  def __len__(self):
    return len(self.df)

  def __getitem__(self,index):
    image_path=self.df.image[index]
    image_label=self.df.label[index]

    #read data
    image = dicom.dcmread(image_path).pixel_array
    image=(x-x.min())/(x.max()-x.min())

    if self.transform:
      image=self.transform(image=image)['image']
    return np.asarray(image),image_label

Augmentation function

def aug():
    """albumentations image augmentation"""
    return Compose([
            Resize(224, 224),
            HorizontalFlip(p=0.5),
            VerticalFlip(p=0.5),
            ShiftScaleRotate(0.05,0.05,5), 
            OpticalDistortion(),
            Cutout(p=0.5),
            ToTensorV2(p=1.0),
        ], p=
1.)

testing it

batch=next(iter(DataLoader(DataReader(train_df,aug()), batch_size = 8, num_workers=1,shuffle=True)))
print(batch[0][0].shape)
grid_img=torchvision.utils.make_grid(batch[0],4,4)
plt.imshow(grid_img.permute(1, 2, 0))

Every thing work well till here. Model initialize

model=densenet121()
model(x)

run above test code again and got following error

TypeError: Caught TypeError in DataLoader worker process 0.
Original Traceback (most recent call last):
  File "/usr/local/lib/python3.7/dist-packages/torch/utils/data/_utils/worker.py", line 287, in _worker_loop
    data = fetcher.fetch(index)
  File "/usr/local/lib/python3.7/dist-packages/torch/utils/data/_utils/fetch.py", line 44, in fetch
    data = [self.dataset[idx] for idx in possibly_batched_index]
  File "/usr/local/lib/python3.7/dist-packages/torch/utils/data/_utils/fetch.py", line 44, in <listcomp>
    data = [self.dataset[idx] for idx in possibly_batched_index]
  File "<ipython-input-41-a0a6888cb06e>", line 21, in __getitem__
    image=self.transform(image=image)['image']
  File "/usr/local/lib/python3.7/dist-packages/albumentations/core/composition.py", line 166, in __call__
    self._check_args(**data)
  File "/usr/local/lib/python3.7/dist-packages/albumentations/core/composition.py", line 237, in _check_args
    raise TypeError("{} must be numpy array type".format(data_name))
TypeError: image must be numpy array type
rsomani95 commented 3 years ago

@talhaanwarch can you show how precisely you're creating this DataReader object in your code?

In the above sample, you're passing x to the model but I don't see where that's defined. I also don't know where densenet is coming from. It appears you're using a plain PyTorch dataset, not the icevision Record API or the provided Dataset object. That means you won't be able to use any of the library's models.

As for your specific error, it looks like you've used albumentations transforms (but I see torchvision transforms in your sample above...), which only works with NumPy arrays, not PIL images

talhaanwarch commented 3 years ago

DataReader is torch custom data loader, model is PyTorch densenet model with input channel change from 3 to 1. No I am not using torchvision augmentation technique. x is just a random tensor to check whether my model is in correct or not. I defined x as x=torch.randn(3,1,224,224)

As I am passing aug which is albumentations function created, the self.transform in DataReader is this aug function. I think this is confusing you

lgvaz commented 3 years ago

@talhaanwarch Maybe I'm missing something, but this does not seem to be an error related to icevision?

talhaanwarch commented 3 years ago

@lgvaz @rsomani95 I am really sorry, I have no clue, how this issue gets posted here, I was supposed to open in albumentations. Really no clue how I appear on this repo because before you mentioning icevision, I never heard it before.