-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataset.py
102 lines (79 loc) · 3.79 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
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
import os
import six
import cv2
import random
import chainer
from PIL import Image
from chainer.dataset import dataset_mixin
class ImageDataset(dataset_mixin.DatasetMixin):
def __init__(self, dataPaths, pathRoot, resizeTo):
if isinstance(dataPaths, six.string_types):
with open(dataPaths) as paths:
dataPaths = [path.strip() for path in paths]
self._dataPaths = dataPaths
self._pathRoot = pathRoot
self._resizeTo = resizeTo
def __len__(self):
return len(self._dataPaths)
def get_example(self, i):
# Open image at specified index
path = os.path.join(self._pathRoot, self._dataPaths[i])
image = Image.open(path)
# Resize if applicable and return
if not self._resizeTo is None:
return image.resize(self._resizeTo)
else:
return image
class ResizedImageDataset(dataset_mixin.DatasetMixin):
def __init__(self, xp, dataPaths, pathRoot, resizeTo):
self._xp = xp
self.images = ImageDataset(dataPaths=dataPaths, pathRoot=pathRoot, resizeTo=resizeTo)
def __len__(self):
return len(self.images)
def get_example(self, i):
# Get example at specified index
image = self._xp.array(self.images[i])
# If monochrome, duplicate single channel 3 times to get RGB
if len(image.shape) == 2:
image = self._xp.dstack((image, image, image))
# Transform data from (x, y, channels) to (channels, x, y)
image_data = image.transpose(2, 0, 1)
# If alpha channel present, trim it out
if image_data.shape[0] == 4:
image_data = image_data[:3]
return image_data
class PreprocessedImageDataset(dataset_mixin.DatasetMixin):
def __init__(self, xp, useGpu, jpegQuality, dataPaths, pathRoot=".", targetSize=96, resizeTo=None):
self._xp = xp
self._useGpu = useGpu
self._jpegQuality = jpegQuality
self.resizedImages = ResizedImageDataset(xp=xp, dataPaths=dataPaths, pathRoot=pathRoot, resizeTo=resizeTo)
self._dtype = xp.float32
self.targetSize = targetSize
def __len__(self):
return len(self.resizedImages)
def get_example(self, i):
# Get indicated image
originalImage = self.resizedImages[i]
# Determine random bounds to crop image at
cropStartX = random.randint(0, originalImage.shape[1] - self.targetSize)
cropEndX = cropStartX + self.targetSize
cropStartY = random.randint(0, originalImage.shape[2] - self.targetSize)
cropEndY = cropStartY + self.targetSize
# Crop image
croppedOriginal = originalImage[:, cropStartX:cropEndX, cropStartY:cropEndY]
# Compress image to JPEG
if self._useGpu >= 0:
croppedOriginalCpu = chainer.cuda.to_cpu(croppedOriginal)
result, encodedImage = cv2.imencode('.jpg', croppedOriginalCpu.transpose(1, 2, 0), [int(cv2.IMWRITE_JPEG_QUALITY), self._jpegQuality])
decodedImage = cv2.imdecode(encodedImage, 1)
croppedCompressedCpu = decodedImage.transpose(2, 0, 1)
croppedCompressed = chainer.cuda.to_gpu(croppedCompressedCpu, device=0)
else:
result, encodedImage = cv2.imencode('.jpg', croppedOriginal.transpose(1, 2, 0), [int(cv2.IMWRITE_JPEG_QUALITY), self._jpegQuality])
decodedImage = cv2.imdecode(encodedImage, 1)
croppedCompressed = decodedImage.transpose(2, 0, 1)
# Convert to desired data type and return the example pairing
croppedOriginal = self._xp.asarray(croppedOriginal, dtype=self._dtype)
croppedCompressed = self._xp.asarray(croppedCompressed, dtype=self._dtype)
return croppedCompressed, croppedOriginal