-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatasetReader.py
More file actions
57 lines (42 loc) · 1.75 KB
/
Copy pathDatasetReader.py
File metadata and controls
57 lines (42 loc) · 1.75 KB
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
import os
import numpy as np
from torch.utils.data import Dataset
from torchvision import transforms
from PIL import Image
class CUBDataset(Dataset):
def __init__(self, split, id_file_path, bound_file_path='', is_training=False, transform=None):
self.images = []
self.isTraining = is_training
with open(id_file_path) as idFile:
idFiles = idFile.readlines()
if is_training:
with open(bound_file_path) as boxFile:
boundingBoxes = boxFile.readlines();
for ind in range(len(idFiles)):
imageFileName = idFiles[ind].strip()
currentDir = os.path.dirname(__file__)
imageFileName = os.path.join(currentDir, r'images', imageFileName)
if is_training:
self.images.append((imageFileName, np.array(boundingBoxes[ind].strip().split(split), dtype=np.float32)))
else:
self.images.append(imageFileName)
if transform is None:
self.transform = transforms.Compose([
transforms.Resize((224, 224)),
transforms.ToTensor(),
])
def __getitem__(self, index):
if self.isTraining:
imagePath, boundingBox = self.images[index]
image = Image.open(imagePath).convert('RGB')
imageSize = np.array(image.size, dtype=np.float32)
image = self.transform(image)
return image, boundingBox, imageSize
else:
imagePath = self.images[index]
image = Image.open(imagePath).convert('RGB')
imageSize = np.array(image.size, dtype=np.float32)
image = self.transform(image)
return image, imageSize
def __len__(self):
return len(self.images)