-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatasets.py
64 lines (60 loc) · 2.45 KB
/
datasets.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
62
63
import os
from PIL import Image
import torch
from torch.utils import data
from torchvision import transforms
device=' cuda'if torch.cuda.is_available() else'cpu'
class DogCat(data.Dataset):
def __init__(self,root='./data/dogCat/',mode='train'):
super(DogCat, self).__init__()
self.mode=mode
self.train_path=root+'train/'
self.test_path = root + 'test/'
self.val_path = root + 'val/'
normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
if mode=='train':
self.train_images=[os.path.join(root+'/train/',i) for i in os.listdir(self.train_path)][:]
self.train_images.sort()
self.transforms = transforms.Compose([
transforms.Resize(size=(224, 224)),
transforms.RandomRotation(20),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
normalize])
elif mode=='test':
self.test_images = [os.path.join(root+ 'test/', i) for i in os.listdir(self.test_path)]
self.test_images.sort()
self.transforms = transforms.Compose([
transforms.Resize(size=(224, 224)),
transforms.ToTensor(),
normalize])
elif mode=='val':
self.val_images = [os.path.join(root+ 'val/', i) for i in os.listdir(self.val_path)]
self.val_images.sort()
self.transforms = transforms.Compose([
transforms.Resize(size=(224, 224)),
transforms.ToTensor(),
normalize])
def __getitem__(self, index):
if self.mode=='train':
_path=self.train_images[index]
image=Image.open(_path)
label=1 if 'dog' in _path.split('/')[-1] else 0
image=self.transforms(image)
return image,label
elif self.mode=='val':
_path = self.val_images[index]
image = Image.open(_path)
label = 1 if 'dog' in _path.split('/')[-1] else 0
image = self.transforms(image)
return image, label
elif self.mode=='test':
_path = self.test_images[index]
image = Image.open(_path)
image = self.transforms(image)
return image
def __len__(self):
if self.mode=='train':
return len(self.train_images)
elif self.mode == 'val':
return len(self.val_images)