I have a dataset of grayscale images, like this one below:
Now, I open my dataset with the following class:
"""Tabular and Image dataset."""
def __init__(self, excel_file, image_dir):
self.image_dir = image_dir
self.excel_file = excel_file
self.tabular = pd.read_excel(excel_file)
def __len__(self):
return len(self.tabular)
def __getitem__(self, idx):
if torch.is_tensor(idx):
idx = idx.tolist()
tabular = self.tabular.iloc[idx, 0:]
y = tabular["Prognosis"]
image = PIL.Image.open(f"{self.image_dir}/{tabular['ImageFile']}")
image = np.array(image)
#image = image[..., :3]
image = transforms.functional.to_tensor(image)
return image, y
If I check the tensors of the image, then I have this:
tensor([[[160, 160, 192, ..., 52, 40, 40],
[176, 208, 320, ..., 96, 80, 16],
[176, 240, 368, ..., 160, 160, 52],
...,
[576, 608, 560, ..., 16, 16, 16],
[624, 592, 544, ..., 16, 16, 16],
[624, 624, 576, ..., 16, 16, 16]]], dtype=torch.int32)
Now, they should be between 0 and 1, right? Because it is grayscale, or 0-255 in RGB, but there are those big values that I have no idea from where they are coming (indeed imshow
shows images with strange distorted colors like yellow and blue rather then grayscale).
However, this is the size of the images torch.Size([1, 2350, 2866])
; I want to resize to (1,224,224) for example
This is my function:
def resize_images(images: List[str]):
for i in images:
image = PIL.Image.open(f"{data_path}TrainSet/{i}")
new_image = image.resize((224, 224))
new_image.save(f"{data_path}TrainImgs/{i}")
resize_images(list(df["ImageFile"]))
However, this code returns all images that are 224x244 but they are all black. All images are completely black!