-
Notifications
You must be signed in to change notification settings - Fork 3
/
Inria.py
154 lines (127 loc) · 5.33 KB
/
Inria.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/usr/bin/env python
from __future__ import absolute_import
from __future__ import print_function, division
import os
from os import path
import numpy as np
import pickle as p
import cv2
from Utils import ProgressBar
import sys
from Reader import INRIATXT, FLICKRTXT, ReadFlickrandSave, ReadINRIAandSave
import json
from Utils import RandInt
import math
py3 = sys.version_info >= (3, 4)
# INRIA holiday Dataset description
# The Holidays dataset is a set of images which mainly contains some of our personal holidays photos.
# The remaining ones were taken on purpose to test the robustness to various attacks:
# rotations, viewpoint and illumination changes, blurring, etc.
# The dataset includes a very large variety of scene types (natural, man-made, water and fire effects, etc)
# and images are in high resolution. The dataset contains 500 image groups,
# each of which represents a distinct scene or object.
# The first image of each group is the query image and the correct retrieval results are the other images of the group.
NUM_CLASSES = 501
class Inria(object):
"""docstring for Inria."""
def __init__(self, mode, resizeWidth, resizeHeight, smallWidth, smallHeight):
print(mode)
if (mode != "database" and mode != "train" and mode != "test" and mode != "all"):
raise AttributeError("Argument of mode is invalid.")
self._mode = mode
self._width = resizeWidth
self._height = resizeHeight
self._smallWidth = smallWidth
self._smallHeight = smallHeight
self.preparePaths()
self._counts = self.X.shape[0]
def preparePaths(self):
if not path.exists(INRIATXT):
ReadINRIAandSave()
if not path.exists(FLICKRTXT):
ReadFlickrandSave()
with open(INRIATXT, 'r') as fp:
inria = json.load(fp)
with open(FLICKRTXT, 'r') as fp:
flickr = json.load(fp)
flickr_id = [500] * len(flickr)
if self._mode == "all":
self.X = np.array(inria['querys'] + inria['database'] + flickr)
self.Y = np.array(inria['query_id'] + inria['ids'] + flickr_id)
self.DataNum = self.X.shape[0]
self.ClassNum = NUM_CLASSES
self.n_samples = self.DataNum
self.Onehot()
print("Label shape:", self.Y.shape)
print("Data shape:", self.X.shape)
return
if self._mode == "database":
self.X = np.array(inria['database'] + flickr)
self.Y = np.array(inria['ids'] + flickr_id)
self.DataNum = self.X.shape[0]
self.ClassNum = NUM_CLASSES
self.n_samples = self.DataNum
self.Onehot()
print("Label shape:", self.Y.shape)
print("Data shape:", self.X.shape)
return
if self._mode == "train":
self.X = np.array(inria['database'] + flickr)
self.Y = np.array(inria['ids'] + flickr_id)
np.random.seed(541)
choice = np.random.permutation(self.X.shape[0])
self.X = self.X[choice[:5000]]
self.Y = self.Y[choice[:5000]]
self.DataNum = self.X.shape[0]
self.ClassNum = NUM_CLASSES
self.n_samples = self.DataNum
self.Onehot()
print("Label shape:", self.Y.shape)
print("Data shape:", self.X.shape)
else:
self.X = np.array(inria['querys'])
self.Y = np.array(inria['query_id'])
self.DataNum = self.X.shape[0]
self.ClassNum = NUM_CLASSES
self.n_samples = self.DataNum
self.Onehot()
print("Label shape:", self.Y.shape)
print("Data shape:", self.X.shape)
def Onehot(self):
# one-hot encoding
y = np.zeros((self.X.shape[0], NUM_CLASSES), dtype=int)
y[range(self.X.shape[0]), self.Y] = 1
self.Y = y
@staticmethod
def resizeX(image, width, height):
# Resize img, scale with same ratio to the smalle scale, then crop to w, h
# for example, a 1024*768 image resize to 640*640, if scale down to 640*480,
# it will be cropped, so firstly scale down to 853.33333 * 640, then crop to 640*640
h, w, c = image.shape
hRatio = height / h
wRatio = width / w
scale = max(hRatio, wRatio)
# print(hRatio, wRatio)
reshaped = cv2.resize(image, (int(round(h * scale)), int(round(w * scale))))
nh, nw, c = reshaped.shape
# print(reshaped.shape)
hRemain = RandInt(nh - height)
wRemain = RandInt(nw - width)
# print(hRemain, hRemain + height)
return reshaped[hRemain:(hRemain + height), wRemain:(wRemain + width)]
# normalize [0~255] to [-1, 1]]
def normalize(self, inp):
return inp / 127.5 - 1.0
def Get(self, index):
paths = self.X[index]
results = np.zeros((index.shape[0], self._height, self._width, 3))
resultsSmall = np.zeros((index.shape[0], self._smallHeight, self._smallWidth, 3))
j = 0
for i in paths:
x = cv2.imread(i)
results[j] = self.resizeX(x, self._width, self._height)
resultsSmall[j] = self.resizeX(x, self._smallWidth, self._smallHeight)
return results, self.normalize(resultsSmall), self.Y[index]
@property
def SamplesCount(self):
return self._counts