-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataset.py
53 lines (43 loc) · 1.93 KB
/
dataset.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
import pandas as pd
from torchvision.io import read_image
import torch
from torch.utils.data import Dataset, DataLoader
from torchvision import transforms
import os
from PIL import Image
class ProductDataset(Dataset):
def __init__(self, target_transform=None):
super().__init__()
csv_file = "training_data.csv"
img_dir = "C:/Users/USER/OneDrive - Teesside University/Documents/Ai Core/MyProject/Facebook Marketplace's Recommendation Ranking System/newcleaned_images/"
self.img_labels = pd.read_csv(csv_file)
self.img_dir = img_dir
self.transform = transforms.Compose([
transforms.ToTensor()
])
self.target_transform = target_transform
categories = ['Home & Garden', 'Baby & Kids Stuff', 'DIY Tools & Materials',
'Music, Films, Books & Games', 'Phones, Mobile Phones & Telecoms',
'Clothes, Footwear & Accessories', 'Other Goods',
'Health & Beauty', 'Sports, Leisure & Travel', 'Appliances',
'Computers & Software', 'Office Furniture & Equipment',
'Video Games & Consoles']
self.encoder = {category: label for label, category in enumerate(categories)}
self.decoder = {label: category for category, label in self.encoder.items()}
def __getitem__(self, index):
img_path = (self.img_dir + self.img_labels.iloc[index, 0] + ".jpg")
image = Image.open(img_path)
label = self.img_labels.iloc[index, 1]
#label = self.decoder[label]
if self.transform:
image = self.transform(image)
if self.target_transform:
label = self.target_transform(label)
return image, label
def __len__(self):
#print((self.img_labels).dtypes)
return len(self.img_labels)
if __name__ == '__main__':
dataset = ProductDataset()
print(len(dataset))
print(dataset[786])