-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataset.py
More file actions
85 lines (69 loc) · 2.86 KB
/
dataset.py
File metadata and controls
85 lines (69 loc) · 2.86 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import torch
import pickle
import torchvision.transforms as transforms
from torch.utils import data
from PIL import Image
from PIL import ImageFile
ImageFile.LOAD_TRUNCATED_IMAGES = True
IMAGE_NET_MEAN = [0.485, 0.456, 0.406]
IMAGE_NET_STD = [0.229, 0.224, 0.225]
class AVADataset(data.Dataset):
"""AVA dataset
Args:
pickle_file: a 4-column pickle_file
column 0 contains the id of the image
column 1 contains the name of the image
column 2 contains the mean of scores of the image
column 3 contains the std of scores of the image
root_dir: directory to the images (Path object)
transform: preprocessing and augmentation of the training images
"""
def __init__(self, pickle_file, root_dir, wrap_size=224):
self.root_dir = root_dir
with open(root_dir / pickle_file, 'rb') as handle:
self.annotations = pickle.load(handle)
normalize = transforms.Normalize(mean=IMAGE_NET_MEAN, std=IMAGE_NET_STD)
self.transform = transforms.Compose([
transforms.Resize((wrap_size, wrap_size)),
transforms.ToTensor(),
normalize,
])
def __len__(self):
return len(self.annotations)
def __getitem__(self, idx):
img_path = str(self.root_dir / 'images' /
f'{int(self.annotations[idx][1])}.jpg')
img = Image.open(img_path).convert('RGB')
img_tensor = self.transform(img)
ground_truth = torch.tensor(self.annotations[idx][2:])
return img_tensor, ground_truth
class AVADatasetEmp(data.Dataset):
"""AVA dataset
Args:
pickle_file: a 4-column pickle_file
column 0 contains the id of the image
column 1 contains the name of the image
column 2 contains the mean of scores of the image
column 3 contains the std of scores of the image
root_dir: directory to the images (Path object)
transform: preprocessing and augmentation of the training images
"""
def __init__(self, pickle_file, root_dir, wrap_size=224):
self.root_dir = root_dir
with open(root_dir / pickle_file, 'rb') as handle:
self.annotations = pickle.load(handle)
normalize = transforms.Normalize(mean=IMAGE_NET_MEAN, std=IMAGE_NET_STD)
self.transform = transforms.Compose([
transforms.Resize((wrap_size, wrap_size)),
transforms.ToTensor(),
normalize,
])
def __len__(self):
return len(self.annotations)
def __getitem__(self, idx):
img_path = str(self.root_dir / 'images' /
f'{int(self.annotations[idx][1])}.jpg')
img = Image.open(img_path).convert('RGB')
img_tensor = self.transform(img)
ground_truth = torch.tensor(self.annotations[idx][2:12])
return img_tensor, ground_truth