forked from ZhiangChen/image_classification
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata.py
More file actions
106 lines (89 loc) · 2.98 KB
/
data.py
File metadata and controls
106 lines (89 loc) · 2.98 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
"""
data.py
Zhiang Chen, June 2020
Custom dataset for Eureka image classification (https://pytorch.org/tutorials/beginner/data_loading_tutorial.html)
"""
from __future__ import print_function, division
import os
import json
import torch
from skimage import io, transform
import numpy as np
import matplotlib.pyplot as plt
from torch.utils.data import Dataset, DataLoader
from torchvision import transforms, utils
from PIL import Image
normalize = transforms.Normalize(mean=[0.44, 0.50, 0.43],
std=[0.26, 0.25, 0.26])
data_transform = transforms.Compose([
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
normalize,])
class EurekaDataset(Dataset):
def __init__(self, root_dir, json_file, transform=None):
assert os.path.isdir(root_dir)
assert os.path.isfile(json_file)
self.root_dir = root_dir
self.classes = ['non_damage','light_damage','severe_damage']
self.images = os.listdir(root_dir)
self.dataset = []
self.__readJson(json_file)
self.transform = transform
def __readJson(self, json_file):
with open(json_file, 'r') as f:
labels = json.load(f)
for label in labels:
if label['Label']:
if label['Label'] == 'Skip':
continue
dataset_name = label['Dataset Name']
image_name = label['External ID']
image_file = dataset_name + '_' + image_name
if image_file in self.images:
#print(label['Label'])
if 'tile_damage' in label['Label']:
data = {}
image_path = os.path.join(self.root_dir, image_file)
clas = self.classes.index(label['Label']['tile_damage'])
data['image_path'] = image_path
data['label'] = clas
self.dataset.append(data)
if clas == 2:
#print(image_file)
for i in range(1,8):
data = {}
image_file = str(i) + '_' + image_file
if image_file in self.images:
image_path = os.path.join(self.root_dir, image_file)
data['image_path'] = image_path
data['label'] = clas
self.dataset.append(data)
def __len__(self):
return len(self.dataset)
def __getitem__(self, idx):
image = io.imread(self.dataset[idx]['image_path'])
label = self.dataset[idx]['label']
image = Image.fromarray(image)
image = image.resize((600,600))
if self.transform:
image = self.transform(image)
#if label==2:
# label=1
return image, label
def show(self, idx):
image, label = self.__getitem__(idx)
print(self.classes[label])
image.show()
def stats(self):
images = np.empty((0,3), float)
indice = list(range(len(self.dataset)))
import random
random.shuffle(indice)
for i in indice[:100]:
img, _ = self.__getitem__(i)
img = img.reshape(-1,3)/255.0
images = np.append(images, img, axis=0)
return np.mean(images, axis=0).tolist(), np.std(images, axis=0).tolist(), \
np.max(images, axis=0).tolist(), np.min(images, axis=0).tolist()
if __name__ == "__main__":
ds = EurekaDataset('./datasets/Eureka/images/','./datasets/Eureka/class.json', data_transform)