-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_generator.py
58 lines (44 loc) · 1.9 KB
/
data_generator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# -*- coding: utf-8 -*-
"""
Created on Sun Dec 3 22:14:33 2023
@author: SABARI
"""
import torch
import numpy as np
import cv2
from torchvision import transforms
from torch.utils.data import Dataset
class ClassificationDataset(Dataset):
def __init__(self, image_paths, targets, resize=None, augmentations=None, mean=None, std=None):
self.image_paths = image_paths
self.targets = targets
self.resize = resize
self.augmentations = augmentations
self.mean = mean
self.std = std
def __len__(self):
return len(self.image_paths)
def __getitem__(self, item):
image = cv2.imread(self.image_paths[item])
# Make sure image is in RGB format (3 channels)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
targets = self.targets[item]
if self.resize is not None:
image = cv2.resize(image, (self.resize[1], self.resize[0]))
# Normalize using mean and std
mean = (0.485, 0.456, 0.406)
std = (0.229, 0.224, 0.225)
normalized_img = (image / 255.0 - mean) / std
if self.augmentations is not None:
augmented = self.augmentations(image=image)
image = augmented["image"]
# Convert the NumPy array to a PyTorch tensor
#image = transforms.ToTensor()(image)
# Normalize the image using mean and std
#if self.mean is not None and self.std is not None:
# image = transforms.functional.normalize(image, mean=self.mean, std=self.std)
# You can use transforms.ToTensor() to convert the image to a PyTorch tensor
image = transforms.ToTensor()(normalized_img)
# Note that for classification tasks, targets should be of type long
targets = torch.tensor(targets, dtype=torch.long)
return {"image": image, "targets": targets}