forked from KaviiSuri/GradCamProjectRepo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSkinMnistDataset.py
61 lines (52 loc) · 1.83 KB
/
SkinMnistDataset.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
59
60
61
from torch.utils.data import Dataset
from torchvision import transforms
import torch
import os
from PIL import Image
import pandas as pd
import numpy as np
class SkinMnistDataset(Dataset):
'''
csv_file is the csv_file containing the information
root_dir is the parent directory/folder containing the images
transforms specifies the transformation to be applied on the images
'''
def __init__(self, csv_file, root_dir, transforms=None):
self.csv_file = pd.read_csv(csv_file)
self.root_dir = root_dir
self.transforms = transforms
def __len__(self):
'''
:return: returns the length of the dataset
'''
return len(self.csv_file)
def __getitem__(self, item):
"""
:param item:it is the the index of the data item to be fetched.
:return: it returns the specified index dataitem and its corresponding label
"""
image_path = os.path.join(
self.root_dir, self.csv_file['image_path'][item])
image = Image.open(image_path)
row = self.csv_file.iloc[item, -3:]
label = torch.tensor(row)
if self.transforms:
image = self.transforms(image)
return (image, label)
data_transforms = {
'train': transforms.Compose([
transforms.RandomResizedCrop(224),
transforms.RandomHorizontalFlip(),
transforms.RandomRotation(90),
transforms.ToTensor(),
transforms.Normalize([0.6373545, 0.44605875, 0.46191868], [
0.27236816, 0.22500427, 0.24329403])
]),
'test': transforms.Compose([
transforms.Resize(226),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize([0.6373545, 0.44605875, 0.46191868], [
0.27236816, 0.22500427, 0.24329403])
]),
}