-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataset.py
41 lines (35 loc) · 1.32 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
import numpy as np
import timm
import math
from fastai.vision.all import *
import torch, torchvision
import torch.nn.functional as F
import torch.nn as nn
import torchvision.transforms as transforms
from torch.utils.data import Dataset, DataLoader
from torch.utils.data import DataLoader
from torchvision.io import read_image
from sklearn import preprocessing
class ClassificationDataset(torch.utils.data.Dataset):
def __init__(self, df, is_valid=False, transform=None, target_transform=None):
self.df = df
self.transform = transform
self.target_transform = target_transform
self.basic_transforms = transforms.Compose([
transforms.ToTensor(),
transforms.ConvertImageDtype(torch.float),
transforms.Resize((224,224)),
])
def __len__(self):
return len(self.df)
def __getitem__(self, idx):
img_path = self.df['file_path'].iloc[idx]
image = Image.open(img_path).convert("RGB")
image = self.basic_transforms(image)
label = self.df['class_num'].iloc[idx]-1 # make it start from 0
label = torch.Tensor([label]).long().squeeze()
if self.transform:
image = self.transform(image)
if self.target_transform:
label = self.target_transform(label)
return TensorImage(image), label