-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.py
40 lines (35 loc) · 1.14 KB
/
Main.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
import glob
import cv2
import numpy as np
import torch
from torch.utils.data import Dataset, DataLoader
class CustomDataset(Dataset):
def __init__(self):
self.imgs_path = "Dog_Cat_Dataset/"
file_list = glob.glob(self.imgs_path + "*")
print(file_list)
self.data = []
for class_path in file_list:
class_name = class_path.split("/")[-1]
for img_path in glob.glob(class_path + "/*.jpeg"):
self.data.append([img_path, class_name])
print(self.data)
self.class_map = {"dogs" : 0, "cats": 1}
self.img_dim = (416, 416)
def __len__(self):
return len(self.data)
def __getitem__(self, idx):
img_path, class_name = self.data[idx]
img = cv2.imread(img_path)
img = cv2.resize(img, self.img_dim)
class_id = self.class_map[class_name]
img_tensor = torch.from_numpy(img)
img_tensor = img_tensor.permute(2, 0, 1)
class_id = torch.tensor([class_id])
return img_tensor, class_id
if __name__ == "__main__":
dataset = CustomDataset()
data_loader = DataLoader(dataset, batch_size=4, shuffle=True)
for imgs, labels in data_loader:
print("Batch of images has shape: ",imgs.shape)
print("Batch of labels has shape: ", labels.shape)