Hi everyone,
I have created a custom dataset for some medical images and wanted to use an ImageNet-based pre-trained model like vgg16 for feature extraction. Then, I will design a custom fully-connected network and use Sklearn StratifiedKfold cross-validation to train and test the model.
Here is the code for the dataset:
def __init__ (self, transform, kfold_index, img_path = '/content/drive/MyDrive/projection images',
img_data = pd.read_csv ('/content/drive/MyDrive/projection images/images.csv'),
img_files = os.listdir ('/content/drive/MyDrive/projection images /projection images)):
super ().__init__ ()
self.transform = transform
self.img_path = img_path
self.img_label = [img_data.iloc [kfold_index [i], 7] for i in range (len (kfold_index))]
self.img_files = [img_files [kfold_index [i]] for i in range (len (kfold_index))]
def __len__ (self):
return len (self.img_files)
def __getitem__(self, index):
sample_path = os.path.join (self.img_path, self.img_files [index])
img = Image.open (sample_path)
img = self.transform (img)
return img, torch.tensor (self.img_label [index])
Also, the torchvision transformations for train and test datasets are as follows:
test_transform = transforms.Compose ([transforms.Resize (256),
transforms.CenterCrop (224),
transforms.Grayscale (num_output_channels= 3)])
transforms.ToTensor ()])
train_transform = transforms.Compose ([transforms.RandomResizedCrop (256, scale = (0.8, 1)),
transforms.ColorJitter (),
transforms.CenterCrop (224),
transforms.RandomAffine (15),
transforms.RandomHorizontalFlip (),
transforms.Grayscale (num_output_channels= 3)])
transforms.ToTensor ()])
However, in previous works of my colleagues on the same dataset, the images were not normalized/standardized in the classic ways, i.e., all the images were just divided by a number (claculated based on the range of image values), not using min-max normalization or not standardizing by making the mean to zero and std to 1. In those studies, the models worked very well, and thus, I want to use the same approach in my work.
The problem is that when using transforms.ToTensor (), Pytorch automatically normalizes the images. My question is that what is the best way to prevent such normalization?
should I change transforms.ToTensor with transforms.PILToTensor ()?
when I do that, this error is raised:
RuntimeError: Input type (torch.cuda.ByteTensor) and weight type (torch.cuda.FloatTensor) should be the same
or should I delete transforms.ToTensor from the transforms.Compose and instead, in the “return” part of the custom dataset, I change :
return img, torch.tensor (self.img_label [index])
with
1)
return torch.tensor (img), torch.tensor (self.img_label [index])```
or
2)
return torch.Tensor (img), torch.tensor (self.img_label [index])
Actually, other errors were again raised when I did these changes:
Could not infer dtype of Image
new(): data must be a sequence (got Image)
I would be grateful if you could tell me what is the best approach in my case.