I am working on an AI related issue, where I need to track several human bodyparts on videos. I create a DataLoader with my images and i make several transforms when calling my Dataset class .
Here is a code sample :
transform = transforms.Compose(
[
transforms.Resize(img_size),
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
]
)
dataset = NamedClassDataset(annotation_folder_path=path, transform=transform, img_size=img_size, normalized=normalize)
train_set, validation_set = torch.utils.data.random_split(dataset, get_train_test_size(dataset,train_percent))
train_loader = DataLoader(dataset=train_set, shuffle=shuffle, batch_size=batch_size,num_workers=num_workers,pin_memory=pin_memory)
validation_loader = DataLoader(dataset=validation_set, shuffle=shuffle, batch_size=batch_size,num_workers=num_workers, pin_memory=pin_memory)
The problem is : after running my model, I display images with the predicted points in order to see their quality. But since images are resized and normalized, I cannot retrieve their original quality and color. I would like to display points on the original images instead of the transformed images and I want to know what is the usual way to do this.
I already have thought of two solutions with their respective disadvantages :
- Reverting transformations, but impossible when resize is called since we loose information
- Returning an index as a third argument in the
__getitem__method of the NamedClassDataset (along with the image and labels). But pytorch methods expects only two outputs when using__getitem__which are (image, associated labels).
EDIT : Here is the getitem of my NamedClassDataset class :
def __getitem__(self, index):
(img_path, coords) = self.annotations.iloc[index].values
img = Image.open(img_path).convert("RGB")
w,h = img.size
# Normalize by img size
if self.img_size is not None:
if self.normalized:
coords = coords/(w,h) # Normalized
else:
n_h,n_w = self.img_size
coords = coords/(w,h)*(n_w,n_h) # Not normalized
y_coords = torch.flatten(torch.tensor(coords)).float() # Flatten outputs and convert from double to float32
if self.transform is not None:
img = self.transform(img)
return (img, y_coords)