-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCustomDataSet.py
43 lines (31 loc) · 1.53 KB
/
CustomDataSet.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
import os
import numpy as np
import pandas as pd
import torch
from torch.utils.data.dataset import Dataset
from skimage import io
# import PIL.Image as Image
from PIL import Image as Image
def ZeroCenter(img):
for i in range(3):
mu = np.ones_like(img[i]) * img[i].mean()
img[i] = img[i] - mu
return img
# root_dir = 'C:/Users/or8be/OneDrive/Desktop/Electrical Engineering B.Sc/Deep Learning/Final Project/Custom_Data'
# csv_file = 'C:/Users/or8be/OneDrive/Desktop/Electrical Engineering B.Sc/Deep Learning/Final Project/csv_style.csv'
class PaintingDataset(Dataset):
def __init__(self, csv_file, root_dir, transforms=None):
self.annotations = pd.read_csv(csv_file, encoding='latin1') # array of paths & labels
# self.annotations = open(csv_file,"r").read()
self.root_dir = root_dir # path to dataset directory
self.transforms = transforms # transform
def __len__(self):
return len(self.annotations) # num of images
def __getitem__(self, index):
img_path = self.annotations.iloc[index, 0]
image = io.imread(img_path) # load image from source
label = torch.tensor(int(self.annotations.iloc[index, 1])) # get label from file as tensor
# image = ZeroCenter(pic) # zero-centering the image
if self.transforms:
image = self.transforms(image)
return image, label