-
Notifications
You must be signed in to change notification settings - Fork 8
/
GameMoves.py
executable file
·193 lines (163 loc) · 6.98 KB
/
GameMoves.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
#!/usr/bin/env python
"""
author: Xiaowei Huang
revised by: Min Wu ([email protected])
"""
import sys
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import cv2
import numpy as np
from keras import backend as K
from scipy.stats import truncnorm, norm
from basics import *
from FeatureExtraction import *
import collections
############################################################
#
# initialise possible moves for a two-player game
#
################################################################
class GameMoves:
def __init__(self, data_set, model, image, tau, image_index):
self.data_set = data_set
self.model = model
self.image = image
self.tau = tau
self.image_index = image_index
self.maxVal = np.max(image)
self.minVal = np.min(image)
feature_extraction = FeatureExtraction(pattern='grey-box')
kps = feature_extraction.get_key_points(self.image, num_partition=10)
partitions = feature_extraction.get_partitions(self.image, self.model, num_partition=10)
# path = "%s_pic/%s_Saliency_(%s).png" % (self.data_set, self.image_index, feature_extraction.PATTERN)
# feature_extraction.plot_saliency_map(self.image, partitions=partitions, path=path)
img_enlarge_ratio = 1
image1 = copy.deepcopy(self.image)
# if np.max(image1) <= 1:
# image1 = (image1 * 255).astype(np.uint8)
# else:
# image1 = image1.astype(np.uint8)
#
# if max(image1.shape) < 100:
# # for small images, sift works by enlarging the images
# image1 = cv2.resize(image1, (0, 0), fx=img_enlarge_ratio, fy=img_enlarge_ratio)
# kps = self.SIFT_Filtered_twoPlayer(image1)
# for i in range(len(kps)):
# oldpt = (kps[i].pt[0], kps[i].pt[1])
# kps[i].pt = (int(oldpt[0] / img_enlarge_ratio), int(oldpt[1] / img_enlarge_ratio))
# else:
# kps = self.SIFT_Filtered_twoPlayer(image1)
#
# print("%s keypoints are found. " % (len(kps)))
actions = dict()
actions[0] = kps
s = 1
kp2 = []
if len(image1.shape) == 2:
image0 = np.zeros(image1.shape)
else:
image0 = np.zeros(image1.shape[:2])
# to compute a partition of the pixels, for an image classification task
# partitions = self.getPartition(image1, kps)
print("The pixels are partitioned with respect to keypoints.")
# construct moves according to the obtained the partitions
num_of_manipulations = 0
for k, blocks in partitions.items():
all_atomic_manipulations = []
for i in range(len(blocks)):
x = blocks[i][0]
y = blocks[i][1]
(_, _, chl) = image1.shape
# + tau
if image0[x][y] == 0:
atomic_manipulation = dict()
for j in range(chl):
atomic_manipulation[(x, y, j)] = self.tau
all_atomic_manipulations.append(atomic_manipulation)
atomic_manipulation = dict()
for j in range(chl):
atomic_manipulation[(x, y, j)] = -1 * self.tau
all_atomic_manipulations.append(atomic_manipulation)
image0[x][y] = 1
# actions[k] = all_atomic_manipulations
actions[s] = all_atomic_manipulations
kp2.append(kps[s - 1])
s += 1
# print("%s manipulations have been initialised for keypoint (%s,%s), whose response is %s."
# % (len(all_atomic_manipulations), int(kps[k - 1].pt[0] / img_enlarge_ratio),
# int(kps[k - 1].pt[1] / img_enlarge_ratio), kps[k - 1].response))
num_of_manipulations += len(all_atomic_manipulations)
# index-0 keeps the keypoints, actual actions start from 1
actions[0] = kp2
print("the number of all manipulations initialised: %s\n" % num_of_manipulations)
self.moves = actions
def applyManipulation(self, image, manipulation):
# apply a specific manipulation to have a manipulated input
#image1 = copy.deepcopy(image)
#maxVal = np.max(image1)
#minVal = np.min(image1)
#for elt in list(manipulation.keys()):
# (fst, snd, thd) = elt
# image1[fst][snd][thd] += manipulation[elt]
# if image1[fst][snd][thd] < minVal:
# image1[fst][snd][thd] = minVal
# elif image1[fst][snd][thd] > maxVal:
# image1[fst][snd][thd] = maxVal
#return image1
image1 = copy.deepcopy(image)
if len(manipulation.keys())>0:
a = np.array(list(manipulation.keys()))
image1[a[:,0],a[:,1],a[:,2]]=np.clip(image1[a[:,0],a[:,1],a[:,2]]
+np.array(list(manipulation.values()))
,self.maxVal,self.minVal)
return image1
else:
return image1
"""
def SIFT_Filtered_twoPlayer(self, image): # threshold=0.0):
sift = cv2.xfeatures2d.SIFT_create() # cv2.SIFT() # cv2.SURF(400) #
kp, des = sift.detectAndCompute(image, None)
return kp
def getPartition(self, image, kps):
# get partition by keypoints
import operator
import random
max_num_of_pixels_per_key_point = 1000000
blocks = {}
if self.data_set != "imageNet":
for x in range(max(image.shape)):
for y in range(max(image.shape)):
ps = 0
maxk = -1
for i in range(1, len(kps) + 1):
k = kps[i - 1]
dist2 = np.linalg.norm(np.array([x, y]) - np.array([k.pt[0], k.pt[1]]))
ps2 = norm.pdf(dist2, loc=0.0, scale=k.size)
if ps2 > ps:
ps = ps2
maxk = i
if maxk in blocks.keys():
blocks[maxk].append((x, y))
else:
blocks[maxk] = [(x, y)]
if max_num_of_pixels_per_key_point > 0:
for mk in blocks.keys():
begining_num = len(blocks[mk])
for i in range(begining_num - max_num_of_pixels_per_key_point):
blocks[mk].remove(random.choice(blocks[mk]))
return blocks
else:
kps = kps[:200]
eachNum = max(image.shape) ** 2 / len(kps)
maxk = 1
blocks[maxk] = []
for x in range(max(image.shape)):
for y in range(max(image.shape)):
if len(blocks[maxk]) <= eachNum:
blocks[maxk].append((x, y))
else:
maxk += 1
blocks[maxk] = [(x, y)]
return blocks
"""