-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utils.py
225 lines (191 loc) · 6.02 KB
/
Utils.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import tempfile, os
import numpy as np
import cv2
class UArray(object):
@staticmethod
def ToDoubleArray(arr, sourceIndex=0, length=None):
if length is None:
length = len(arr)
return np.array(arr, dtype=np.float64)[sourceIndex:sourceIndex+length]
@staticmethod
def ToFloatArray(arr):
return np.array(arr, dtype=np.float32)
@staticmethod
def ToByteArray(arr):
return np.array(arr, dtype=np.uint8)
@staticmethod
def ToIntArray(arr):
return np.array(arr, dtype=np.uint64)
@staticmethod
def ToRGBArray(arr, scale, offset):
arr = np.array(arr)
return np.array(scale * arr + offset, dtype=np.uint8)
@staticmethod
def InPlaceRoundDoubleArray(arr):
return np.around(arr)
@staticmethod
def ComputeRoundIdenticals(arr1, arr2):
a1 = np.around(arr1)
a2 = np.around(arr2)
return (a1 == a2).sum()
class UMath(object):
@staticmethod
def SoftMax(x):
"""Compute softmax values for each sets of scores in x."""
return np.exp(x) / np.sum(np.exp(x), axis=0)
@staticmethod
def EnsureInt(value):
""" <summary>
Rounds a double and ensures it was an integer
</summary>
<param name="value">The double to be converted</param>
<returns>The integer represented by the double</returns>
"""
intValue = np.around(value)
if value != intValue:
raise ValueError("Invalid integer: " + value)
return intValue
@staticmethod
def EnsureIntArray(arr):
""" <summary>
Converts an entire array to integers, ensuring their format
</summary>
"""
a = np.around(a)
if np.all(np.array(arr) == a):
return a
raise ValueError("Invalid integer in array!")
@staticmethod
def Max(output):
i = np.argmax(output)
return output[i], i
@staticmethod
def MaxExcluding(idx, output):
tmp = np.ma.array(output, mask=False)
tmp.mask[idx] = True
return UMath.Max(tmp)
@staticmethod
def Clamp(value, minimum, maximum):
return np.clip(value, minimum, maximum)
@staticmethod
def ClampArray(values, minimum, maximum):
return np.clip(values, minimum, maximum)
@staticmethod
def LInfinityDistance(point1, point2):
""" <summary>
Calculates the LInfinity distance between two points in Rn
</summary>
"""
p1 = np.array(point1)
p2 = np.array(point2)
if p1.shape != p2.shape:
raise ValueError("Invalid inputs!")
return np.max(np.abs(p1 - p2))
@staticmethod
def L1Distance(point1, point2):
p1 = np.array(point1)
p2 = np.array(point2)
if p1.shape != p2.shape:
raise ValueError("Invalid inputs!")
return np.sum(np.abs(p1 - p2))
class URand(object):
""" <summary>
Various functions that utilize randomness
</summary>
"""
@staticmethod
def NextGaussian(rand):
""" <summary>
Returns a double drawn from a Gaussian distribution(0,1)
</summary>
"""
assert isinstance(rand, random.Random), "Wrong Type!"
return rand.gauss(0, 1)
@staticmethod
def NextRandomImage(rand, size):
assert isinstance(rand, random.Random), "Wrong Type!"
arr = [rand.randint(0, 255) for i in range]
return np.array(arr, dtype=np.uint8)
@staticmethod
def NextGaussian(mean, sd, rand):
""" <summary>
Draws a double from a Gaussian distribution weith a specific mean and deviation
</summary>
<returns></returns>
"""
return sd * URand.NextGaussian(rand) + mean
@staticmethod
def NextPermutation(random, length):
""" <summary>
Standard Fisher-Yates random permutation
</summary>
<param name="random"></param>
<param name="length"></param>
<returns></returns>
"""
lst = [None] * length
i = 0
while i < length:
lst[i] = i
i += 1
n = length
i = length - 1
while i > 0:
# swap randomly with element in (i, length]
k = rand.randint(i + 1, length)
bucket = lst[k]
lst[k] = lst[i]
lst[i] = bucket
i -= 1
return lst
@staticmethod
def GetNoisyPoint(point, addedNoiseSD, rand):
addedNoise = URand.NextGaussian(0.0, addedNoiseSD, rand)
pt = np.array(point)
return np.clip(pt + addedNoise, 0.0, 255.0)
class UDraw(object):
""" <summary>
Displaying images
</summary>
"""
#########TODO: CHECK!###########
@staticmethod
def DrawGrayscalePixels(pixels, numRows, numCols, isRowOrder):
image = np.array(pixels, dtype=np.uint8)
order = 'C' if isRowOrder else 'F'
image = np.reshape(image, (numRows, numRows), order=order)
return image
#########TODO: CHECK!##########
@staticmethod
def DrawRGBPixels(pixels, numRows, numCols):
image = np.array(pixels, dtype=np.uint8)
order = 'C' if isRowOrder else 'F'
image = np.reshape(image, (3, numRows, numRows), order=order)
return image
@staticmethod
def DisplayImageAndPause(imagePixels, numRows, numCols, isColor, isRowOrder):
image = UDraw.DrawRGBPixels(imagePixels, numRows, numCols, isRowOrder) if isColor
else UDraw.DrawGrayscalePixels(imagePixels, numRows, numCols, isRowOrder)
cv2.imshow("Image Visualization", image)
cv2.waitKey(0)
cv2.destroyAllWindows()
@staticmethod
def Rotate(imagePixels, numRows, numCols, isColor, degrees, isRowOrder):
image_0 = UDraw.DrawRGBPixels(imagePixels, numRows, numCols, isRowOrder) if isColor
else UDraw.DrawGrayscalePixels(imagePixels, numRows, numCols, isRowOrder)
curr = imutils.rotate_bound(image_0, degrees)
return UDraw.FromBitmap(curr, numRows, numCols, isColor, isRowOrder)
@staticmethod
def FromBitmap(m, numRows, numCols, isColor, isRowOrder):
return m.reshape(m.size)
@staticmethod
def LossyJPGAndBack(imagePixels, numRows, numCols, isColor, photoquality, isRowOrder):
image = UDraw.DrawRGBPixels(imagePixels, numRows, numCols, isRowOrder) if isColor
else UDraw.DrawGrayscalePixels(imagePixels, numRows, numCols, isRowOrder)
assert 0 <= photoquality <= 50, "0 <= photoquality <= 50; provided: %d"%photoquality
tmpDir = tempfile.mkdtemp()
tmpImage = os.path.append(tmpDir, 'tmp.jpg')
cv2.imwrite( , image, [int(cv2.IMWRITE_JPEG_QUALITY), photoquality])
newImage = cv2.imread(tmpImage)
newImagePixels = UDraw.FromBitmap(newImage, *newImage.shape, isColor, isRowOrder)
return newImagePixels