def _parse_action(self, action):
move_type, y, x = np.unravel_index(action, (8, self.map_height, self.map_width))
start = y * self.map_width + x
index = move_type % 4
if index == 0:
end = start + self.map_width
elif index == 1:
end = start + 1
elif index == 2:
end = start - self.map_width
elif index == 3:
end = start - 1
else:
raise("invalid index")
is_50 = True if move_type >= 4 else False
return {'start': start, 'end': end, 'is50': is_50}
python类unravel_index()的实例源码
def is_valid_move(self, start, end, player_index):
start_label = self.label_map.flat[start]
if end < len(self.label_map.flat) and end >= 0:
end_label = self.label_map.flat[end]
else:
return False
index = start_label - 1
if player_index != None and (player_index != index):
return False
if self.army_map.flat[start] == 0:
return False
start_x, start_y = np.unravel_index(start, (self.map_height, self.map_width))
end_x, end_y = np.unravel_index(end, (self.map_height, self.map_width))
if abs(start_x - end_x) + abs(start_y - end_y) != 1:
return False
return True
def _sample_cond_single(rng, marginal_pmf, n_group, out, eps):
"""Single sample from conditional probab. (call :func:`self.sample`)"""
n_sites = len(marginal_pmf[-1])
# Probability of the incomplete output. Empty output has unit probab.
out_p = 1.0
# `n_out` sites of the output have been sampled. We will add
# at most `n_group` sites to the output at a time.
for n_out in range(0, n_sites, n_group):
# Select marginal probability distribution on (at most)
# `n_out + n_group` sites.
p = marginal_pmf[min(n_sites, n_out + n_group)]
# Obtain conditional probab. from joint `p` and marginal `out_p`
p = p.get(tuple(out[:n_out]) + (slice(None),) * (len(p) - n_out))
p = project_pmf(mp.prune(p).to_array() / out_p, eps, eps)
# Sample from conditional probab. for next `n_group` sites
choice = rng.choice(p.size, p=p.flat)
out[n_out:n_out + n_group] = np.unravel_index(choice, p.shape)
# Update probability of the partial output
out_p *= np.prod(p.flat[choice])
# Verify we have the correct partial output probability
p = marginal_pmf[-1].get(tuple(out)).to_array()
assert abs(p - out_p) <= eps
def unpack_samples(self, samples):
"""Unpack samples into several integers per sample
Inverse of :func:`MPPovm.pack_samples`. Example:
>>> p = pauli_mpp(nr_sites=2, local_dim=2)
>>> p.outdims
(6, 6)
>>> p.unpack_samples(np.array([0, 6, 7, 12]))
array([[0, 0],
[1, 0],
[1, 1],
[2, 0]], dtype=uint8)
"""
assert samples.ndim == 1
assert all(dim <= 255 for dim in self.outdims)
return np.array(np.unravel_index(samples, self.nsoutdims)) \
.T.astype(np.uint8)
def batch_works(k):
if k == n_processes - 1:
paths = all_paths[k * int(len(all_paths) / n_processes) : ]
else:
paths = all_paths[k * int(len(all_paths) / n_processes) : (k + 1) * int(len(all_paths) / n_processes)]
for path in paths:
o_path = os.path.join(output_path, os.path.basename(path))
if not os.path.exists(o_path):
os.makedirs(o_path)
x, y, z = perturb_patch_locations(base_locs, patch_size / 16)
probs = generate_patch_probs(path, (x, y, z), patch_size, image_size)
selections = np.random.choice(range(len(probs)), size=patches_per_image, replace=False, p=probs)
image = read_image(path)
for num, sel in enumerate(selections):
i, j, k = np.unravel_index(sel, (len(x), len(y), len(z)))
patch = image[int(x[i] - patch_size / 2) : int(x[i] + patch_size / 2),
int(y[j] - patch_size / 2) : int(y[j] + patch_size / 2),
int(z[k] - patch_size / 2) : int(z[k] + patch_size / 2), :]
f = os.path.join(o_path, str(num))
np.save(f, patch)
def __process_path__(self, path, next_move_only=True):
if len(path) != 0:
path = path[:-1]
print("[INFO] Received path: %s" % (path))
path_list = []
for a in path.split('.'):
path_list.append(int(a))
path_list = np.unravel_index(path_list, self.imsize)
solution_list = []
for i in xrange(path_list[0].shape[0]):
solution_list.append((path_list[0][i],
path_list[1][i]))
if next_move_only:
return False, solution_list[1]
else:
return False, solution_list
else:
print("[ERROR] Errors found while running dstar algorithm.")
return True
def __init__(self):
self.shape = (4, 12)
nS = np.prod(self.shape)
nA = 4
# Cliff Location
self._cliff = np.zeros(self.shape, dtype=np.bool)
self._cliff[3, 1:-1] = True
# Calculate transition probabilities
P = {}
for s in range(nS):
position = np.unravel_index(s, self.shape)
P[s] = { a : [] for a in range(nA) }
P[s][UP] = self._calculate_transition_prob(position, [-1, 0])
P[s][RIGHT] = self._calculate_transition_prob(position, [0, 1])
P[s][DOWN] = self._calculate_transition_prob(position, [1, 0])
P[s][LEFT] = self._calculate_transition_prob(position, [0, -1])
# We always start in state (3, 0)
isd = np.zeros(nS)
isd[np.ravel_multi_index((3,0), self.shape)] = 1.0
super(CliffWalkingEnv, self).__init__(nS, nA, P, isd)
def _render(self, mode='human', close=False):
if close:
return
outfile = StringIO() if mode == 'ansi' else sys.stdout
for s in range(self.nS):
position = np.unravel_index(s, self.shape)
# print(self.s)
if self.s == s:
output = " x "
elif position == (3,7):
output = " T "
else:
output = " o "
if position[1] == 0:
output = output.lstrip()
if position[1] == self.shape[1] - 1:
output = output.rstrip()
output += "\n"
outfile.write(output)
outfile.write("\n")
def joints_pred_numpy(self, img, coord = 'hm', thresh = 0.2, sess = None):
""" Create Tensor for joint position prediction
NON TRAINABLE
TO CALL AFTER GENERATING GRAPH
Notes:
Not more efficient than Numpy, prefer Numpy for such operation!
"""
if sess is None:
hm = self.HG.Session.run(self.HG.pred_sigmoid , feed_dict = {self.HG.img: img})
else:
hm = sess.run(self.HG.pred_sigmoid , feed_dict = {self.HG.img: img})
joints = -1*np.ones(shape = (self.params['num_joints'], 2))
for i in range(self.params['num_joints']):
index = np.unravel_index(hm[0,:,:,i].argmax(), (self.params['hm_size'],self.params['hm_size']))
if hm[0,index[0], index[1],i] > thresh:
if coord == 'hm':
joints[i] = np.array(index)
elif coord == 'img':
joints[i] = np.array(index) * self.params['img_size'] / self.params['hm_size']
return joints
def get_i_j(lats, lons, lat, lon):
"""
Finds the nearest neighbour in a lat lon grid. If the point is outside the grid, the nearest
point within the grid is still returned.
Arguments:
lats (np.array): 2D array of latitudes
lons (np.array): 2D array of longitude
lat (float): Loopup latitude
lon (float): Loopup longitude
Returns:
I (int): First index into lats/lons arrays
J (int): Second index into lats/lons arrays
"""
dist = distance(lat, lon, lats, lons)
indices = np.unravel_index(dist.argmin(), dist.shape)
X = lats.shape[0]
Y = lats.shape[1]
I = indices[0]
J = indices[1]
if(indices[0] == 0 or indices[0] >= X-1 or indices[1] == 0 or indices[1] >= Y-1):
debug("Lat/lon %g,%g outside grid" % (lat, lon))
return I, J
def find_beam_position_blur(z, sigma=30):
"""Estimate direct beam position by blurring the image with a large
Gaussian kernel and finding the maximum.
Parameters
----------
sigma : float
Sigma value for Gaussian blurring kernel.
Returns
-------
center : np.array
np.array containing indices of estimated direct beam positon.
"""
blurred = ndi.gaussian_filter(z, sigma)
center = np.unravel_index(blurred.argmax(), blurred.shape)
return np.array(center)
def smallest_k(matrix: np.ndarray, k: int,
only_first_row: bool = False) -> Tuple[Tuple[np.ndarray, np.ndarray], np.ndarray]:
"""
Find the smallest elements in a numpy matrix.
:param matrix: Any matrix.
:param k: The number of smallest elements to return.
:param only_first_row: If true the search is constrained to the first row of the matrix.
:return: The row indices, column indices and values of the k smallest items in matrix.
"""
if only_first_row:
flatten = matrix[:1, :].flatten()
else:
flatten = matrix.flatten()
# args are the indices in flatten of the k smallest elements
args = np.argpartition(flatten, k)[:k]
# args are the indices in flatten of the sorted k smallest elements
args = args[np.argsort(flatten[args])]
# flatten[args] are the values for args
return np.unravel_index(args, matrix.shape), flatten[args]
def smallest_k_mx(matrix: mx.nd.NDArray, k: int,
only_first_row: bool = False) -> Tuple[Tuple[np.ndarray, np.ndarray], np.ndarray]:
"""
Find the smallest elements in a NDarray.
:param matrix: Any matrix.
:param k: The number of smallest elements to return.
:param only_first_row: If True the search is constrained to the first row of the matrix.
:return: The row indices, column indices and values of the k smallest items in matrix.
"""
if only_first_row:
matrix = mx.nd.reshape(matrix[0], shape=(1, -1))
# pylint: disable=unbalanced-tuple-unpacking
values, indices = mx.nd.topk(matrix, axis=None, k=k, ret_typ='both', is_ascend=True)
return np.unravel_index(indices.astype(np.int32).asnumpy(), matrix.shape), values
def partition_index_2d(self, axis):
if not self._distributed:
return False, self.index.grid_collection(self.center,
self.index.grids)
xax = self.ds.coordinates.x_axis[axis]
yax = self.ds.coordinates.y_axis[axis]
cc = MPI.Compute_dims(self.comm.size, 2)
mi = self.comm.rank
cx, cy = np.unravel_index(mi, cc)
x = np.mgrid[0:1:(cc[0]+1)*1j][cx:cx+2]
y = np.mgrid[0:1:(cc[1]+1)*1j][cy:cy+2]
DLE, DRE = self.ds.domain_left_edge.copy(), self.ds.domain_right_edge.copy()
LE = np.ones(3, dtype='float64') * DLE
RE = np.ones(3, dtype='float64') * DRE
LE[xax] = x[0] * (DRE[xax]-DLE[xax]) + DLE[xax]
RE[xax] = x[1] * (DRE[xax]-DLE[xax]) + DLE[xax]
LE[yax] = y[0] * (DRE[yax]-DLE[yax]) + DLE[yax]
RE[yax] = y[1] * (DRE[yax]-DLE[yax]) + DLE[yax]
mylog.debug("Dimensions: %s %s", LE, RE)
reg = self.ds.region(self.center, LE, RE)
return True, reg
def _unravel_flat_index(self, i):
"""Unravels a flat index i to multi-indexes.
:param i: Flat index
:type i: int or slice
:rtype: Tuple of np.array
:raises IndexError: If the index is out of bounds
"""
# i is int => make sure we deal with negative i properly
# i is slice => use i.indices to compute the actual indices
total = len(self)
if isinstance(i, int):
indexes = [i] if i >= 0 else [total + i]
else:
indexes = list(range(*i.indices(total)))
# Convert to multi-indexes
try:
unraveled = np.unravel_index(indexes, self.controlpoints.shape[:-1], order='F')
except ValueError:
raise IndexError
return unraveled
def estimateBackgroundLevel(img, image_is_artefact_free=False,
min_rel_size=0.05, max_abs_size=11):
'''
estimate background level through finding the most homogeneous area
and take its average
min_size - relative size of the examined area
'''
s0,s1 = img.shape[:2]
s = min(max_abs_size, int(max(s0,s1)*min_rel_size))
arr = np.zeros(shape=(s0-2*s, s1-2*s), dtype=img.dtype)
#fill arr:
_spatialStd(img, arr, s)
#most homogeneous area:
i,j = np.unravel_index(arr.argmin(), arr.shape)
sub = img[int(i+0.5*s):int(i+s*1.5),
int(j+s*0.5):int(j+s*1.5)]
return np.median(sub)
def _pixel_select(self, event):
x, y = event.xdata, event.ydata
# get index by assuming even spacing
# TODO use kdtree?
diff = np.hypot((self.x_pos - x), (self.y_pos - y))
y_ind, x_ind = np.unravel_index(np.argmin(diff), diff.shape)
# get the spectrum for this point
new_y_data = self.counts[y_ind, x_ind, :]
self.mask = np.zeros(self.x_pos.shape, dtype='bool')
self.mask[y_ind, x_ind] = True
self.mask_im.set_data(self._overlay_image)
self._pixel_txt.set_text(
'pixel: [{:d}, {:d}] ({:.3g}, {:.3g})'.format(
y_ind, x_ind,
self.x_pos[y_ind, x_ind],
self.y_pos[y_ind, x_ind]))
self.spec.set_ydata(new_y_data)
self.ax_spec.relim()
self.ax_spec.autoscale(True, axis='y')
self.fig.canvas.draw_idle()
def all_out_attack(self):
cells_to_consider_moving = []
for square in itertools.chain.from_iterable(self.squares):
# Do we risk undoing a multi-move capture if we move a piece that's "STILL"?
if square.owner == self.my_id and (square.move == -1 or square.move == STILL) and square.strength > (square.production * late_game_buildup_multiplier):
cells_to_consider_moving.append(square)
for square in cells_to_consider_moving:
# # Find an enemy square to attack!
# #if (square.x + square.y) % 2 == self.frame % 2:
# value_map = numpy.zeros((self.width, self.height))
# #value_map += numpy.multiply(numpy.divide(self.influence_enemy_strength_map[3], self.dij_prod_distance_map[square.x, square.y]), self.combat_zone_map)
# value_map += numpy.multiply(self.distance_map_no_decay[square.x, square.y], self.is_enemy_map)
# tx, ty = numpy.unravel_index(value_map.argmax(), (self.width, self.height))
# target = self.squares[tx, ty]
# square.move_to_target(target, False)
self.find_nearest_non_npc_enemy_direction(square)
def update_value_maps(self):
self.base_value_map = np.divide(self.production_map_01, self.strength_map_1) * (self.is_neutral_map - self.combat_zone_map)
# Each neutral cell gets assigned to the closest border non-combat cell
global_targets_indices = np.transpose(np.nonzero(self.is_neutral_map - self.combat_zone_map))
global_targets = [self.squares[c[0], c[1]] for c in global_targets_indices]
self.global_border_map = np.zeros((self.w, self.h))
for g in global_targets:
# Find the closest border square that routes to g
gb_map = self.dij_recov_distance_map[g.x, g.y] * (self.border_map - self.combat_zone_map)
gb_map[gb_map == 0] = 9999
tx, ty = np.unravel_index(gb_map.argmin(), (self.w, self.h))
self.global_border_map[tx, ty] += self.base_value_map[g.x, g.y] / self.dij_recov_distance_map[g.x, g.y, tx, ty]
self.value_map = 1 / np.maximum(self.base_value_map + self.global_border_map * 1, 0.001)
print_map(self.global_border_map, "global_border_")
print_map(self.base_value_map, "base_value_")
print_map(self.value_map, "value_map_")
def update_value_maps(self):
base_value_map = np.divide(self.production_map_01, self.strength_map_1) * (self.is_neutral_map - self.combat_zone_map)
# Each neutral cell gets assigned to the closest border non-combat cell
global_targets_indices = np.transpose(np.nonzero(self.is_neutral_map - self.combat_zone_map))
global_targets = [self.squares[c[0], c[1]] for c in global_targets_indices]
# border_squares_indices = np.transpose(np.nonzero(self.border_map - self.combat_zone_map))
# border_squares = [self.squares[c[0], c[1]] for c in border_squares_indices]
global_border_map = np.zeros((self.w, self.h))
for g in global_targets:
# Find the closest border square that routes to g
gb_map = self.dij_recov_distance_map[g.x, g.y] * (self.border_map - self.combat_zone_map)
gb_map[gb_map == 0] = 9999
tx, ty = np.unravel_index(gb_map.argmin(), (self.w, self.h))
global_border_map[tx, ty] += base_value_map[g.x, g.y] / self.dij_recov_distance_map[g.x, g.y, tx, ty]
self.value_map = 1 / np.maximum(base_value_map + global_border_map * 1, 0.001)
print_map(global_border_map, "global_border_")
print_map(base_value_map, "base_value_")
print_map(self.value_map, "value_map_")
def update_value_production_map(self):
self.value_production_map = (self.border_map - self.combat_zone_map * (self.enemy_strength_map[1] == 0)) * self.recover_wtd_map
#self.value_production_map = (self.border_map - self.combat_zone_map) * self.recover_wtd_map
self.value_production_map[self.value_production_map == 0] = 9999
turns_left = self.max_turns - self.frame
recover_threshold = turns_left * 0.6
self.value_production_map[self.value_production_map > recover_threshold] == 9999
bx, by = np.unravel_index(self.value_production_map.argmin(), (self.width, self.height))
best_cell_value = self.value_production_map[bx, by]
avg_recov_threshold = 2
avg_map_recovery = np.sum(self.strength_map * self.border_map) / np.sum(self.production_map * self.border_map)
self.value_production_map[self.value_production_map > (avg_recov_threshold * avg_map_recovery)] = 9999
if self.frame > 5 and self.my_production_sum / self.next_highest_production_sum > 1.1 and np.sum(self.combat_zone_map) > 2:
self.value_production_map = np.ones((self.width, self.height)) * 9999
def update_value_maps(self):
self.base_value_map = np.divide(self.strength_map, self.production_map_01) * (self.is_neutral_map - self.combat_zone_map)
self.value_map = np.zeros((self.width, self.height))
cells_out = 5
num_cells = cells_out * (cells_out + 1) * 2 + 1
for x in range(self.width):
for y in range(self.height):
if self.is_neutral_map[x, y]:
self.value_map += (self.distance_map_no_decay[x, y] + self.base_value_map[x, y]) * (self.distance_map_no_decay[x, y] <= cells_out)
else:
self.value_map += (self.distance_map_no_decay[x, y] + 100) * (self.distance_map_no_decay[x, y] <= cells_out)
# Add in the cost to get to each square.
self.global_search_map = np.copy(self.value_map)
self.value_map /= num_cells
for x in range(self.width):
for y in range(self.height):
temp_map = self.dij_recov_distance_map[x, y] * (self.is_owned_map == 1)
temp_map[temp_map == 0] = 9999
tx, ty = np.unravel_index(temp_map.argmin(), (self.width, self.height))
self.global_search_map[x, y] += self.dij_recov_distance_map[x, y, tx, ty]
print_map(self.value_map, "value_map_")
print_map(self.global_search_map, "global_search_map_")
def update_value_maps(self):
self.base_value_map = np.divide(self.production_map_01, self.strength_map_1) * (self.is_neutral_map - self.combat_zone_map)
# Each neutral cell gets assigned to the closest border non-combat cell
global_targets_indices = np.transpose(np.nonzero(self.is_neutral_map - self.combat_zone_map))
global_targets = [self.squares[c[0], c[1]] for c in global_targets_indices]
# border_squares_indices = np.transpose(np.nonzero(self.border_map - self.combat_zone_map))
# border_squares = [self.squares[c[0], c[1]] for c in border_squares_indices]
self.global_border_map = np.zeros((self.w, self.h))
for g in global_targets:
# Find the closest border square that routes to g
gb_map = self.dij_recov_distance_map[g.x, g.y] * (self.border_map - self.combat_zone_map)
gb_map[gb_map == 0] = 9999
tx, ty = np.unravel_index(gb_map.argmin(), (self.w, self.h))
self.global_border_map[tx, ty] += self.base_value_map[g.x, g.y] / self.dij_recov_distance_map[g.x, g.y, tx, ty]
self.value_map = 1 / np.maximum(self.base_value_map + self.global_border_map * 1, 0.001)
print_map(self.global_border_map, "global_border_")
print_map(self.base_value_map, "base_value_")
print_map(self.value_map, "value_map_")
def update_value_maps(self):
base_value_map = np.divide(self.production_map_01, self.strength_map_1) * (self.is_neutral_map - self.combat_zone_map)
# Each neutral cell gets assigned to the closest border non-combat cell
global_targets_indices = np.transpose(np.nonzero(self.is_neutral_map - self.combat_zone_map))
global_targets = [self.squares[c[0], c[1]] for c in global_targets_indices]
# border_squares_indices = np.transpose(np.nonzero(self.border_map - self.combat_zone_map))
# border_squares = [self.squares[c[0], c[1]] for c in border_squares_indices]
global_border_map = np.zeros((self.w, self.h))
for g in global_targets:
# Find the closest border square that routes to g
gb_map = self.dij_recov_distance_map[g.x, g.y] * (self.border_map - self.combat_zone_map)
gb_map[gb_map == 0] = 9999
tx, ty = np.unravel_index(gb_map.argmin(), (self.w, self.h))
global_border_map[tx, ty] += base_value_map[g.x, g.y] / self.dij_recov_distance_map[g.x, g.y, tx, ty]
self.value_map = 1 / np.maximum(base_value_map + global_border_map * 1, 0.001)
print_map(global_border_map, "global_border_")
print_map(base_value_map, "base_value_")
print_map(self.value_map, "value_map_")
def update_value_maps(self):
self.base_value_map = np.divide(self.production_map_01, self.strength_map_1) * (self.is_neutral_map - self.combat_zone_map)
# Each neutral cell gets assigned to the closest border non-combat cell
global_targets_indices = np.transpose(np.nonzero(self.is_neutral_map - self.combat_zone_map))
global_targets = [self.squares[c[0], c[1]] for c in global_targets_indices]
self.global_border_map = np.zeros((self.w, self.h))
for g in global_targets:
# Find the closest border square that routes to g
gb_map = self.dij_recov_distance_map[g.x, g.y] * (self.border_map - self.combat_zone_map)
gb_map[gb_map == 0] = 9999
tx, ty = np.unravel_index(gb_map.argmin(), (self.w, self.h))
self.global_border_map[tx, ty] += self.base_value_map[g.x, g.y] / self.dij_recov_distance_map[g.x, g.y, tx, ty]
self.value_map = 1 / np.maximum(self.base_value_map + self.global_border_map * 1, 0.001)
print_map(self.global_border_map, "global_border_")
print_map(self.base_value_map, "base_value_")
print_map(self.value_map, "value_map_")
def process_batch(self, batch):
"""
Execution of an update step, infer cg_id from selectors, and pick
corresponding computational graph, and apply batch to the CG.
"""
cg_id = self.get_cg_id_from_selectors(batch['src_selector'][0],
batch['trg_selector'][0])
# Apply input replacement with <UNK> if necessary
if self.drop_input[cg_id] > 0.0:
num_els = numpy.prod(batch['source'].shape)
num_reps = max(1, int(num_els * self.drop_input[cg_id]))
replace_idx = numpy.random.choice(num_els, num_reps, replace=False)
# TODO: set it according to unk_id in config
batch['source'][numpy.unravel_index(
replace_idx, batch['source'].shape)] = 1
ordered_batch = [batch[v.name] for v in self.algorithms[cg_id].inputs]
# To save memory, we may combine f_update and f_grad_shared
if self.f_grad_shareds[cg_id] is None:
inps = [self.learning_rate] + ordered_batch
cost = self.f_updates[cg_id](*inps)
self._cost = ('cost_' + cg_id, cost)
else:
cost = self.f_grad_shareds[cg_id](*ordered_batch)
self._cost = ('cost_' + cg_id, cost)
self.f_updates[cg_id](self.learning_rate)
cpm_utils.py 文件源码
项目:convolutional-pose-machines-tensorflow
作者: timctho
项目源码
文件源码
阅读 34
收藏 0
点赞 0
评论 0
def make_gaussian_batch(heatmaps, size, fwhm):
""" Make a square gaussian kernel.
size is the length of a side of the square
fwhm is full-width-half-maximum, which
can be thought of as an effective radius.
"""
stride = heatmaps.shape[1] // size
batch_datum = np.zeros(shape=(heatmaps.shape[0], size, size, heatmaps.shape[3]))
for data_num in range(heatmaps.shape[0]):
for joint_num in range(heatmaps.shape[3] - 1):
heatmap = heatmaps[data_num, :, :, joint_num]
center = np.unravel_index(np.argmax(heatmap), (heatmap.shape[0], heatmap.shape[1]))
x = np.arange(0, size, 1, float)
y = x[:, np.newaxis]
if center is None:
x0 = y0 = size * stride // 2
else:
x0 = center[1]
y0 = center[0]
batch_datum[data_num, :, :, joint_num] = np.exp(
-((x * stride - x0) ** 2 + (y * stride - y0) ** 2) / 2.0 / fwhm / fwhm)
batch_datum[data_num, :, :, heatmaps.shape[3] - 1] = np.ones((size, size)) - np.amax(
batch_datum[data_num, :, :, 0:heatmaps.shape[3] - 1], axis=2)
return batch_datum
def backward(self, pre_grad, *args, **kwargs):
new_h, new_w = self.out_shape[-2:]
pool_h, pool_w = self.pool_size
layer_grads = _zero(self.input_shape)
if np.ndim(pre_grad) == 4:
nb_batch, nb_axis, _, _ = pre_grad.shape
for a in np.arange(nb_batch):
for b in np.arange(nb_axis):
for h in np.arange(new_h):
for w in np.arange(new_w):
patch = self.last_input[a, b, h:h + pool_h, w:w + pool_w]
max_idx = np.unravel_index(patch.argmax(), patch.shape)
h_shift, w_shift = h * pool_h + max_idx[0], w * pool_w + max_idx[1]
layer_grads[a, b, h_shift, w_shift] = pre_grad[a, b, a, w]
elif np.ndim(pre_grad) == 3:
nb_batch, _, _ = pre_grad.shape
for a in np.arange(nb_batch):
for h in np.arange(new_h):
for w in np.arange(new_w):
patch = self.last_input[a, h:h + pool_h, w:w + pool_w]
max_idx = np.unravel_index(patch.argmax(), patch.shape)
h_shift, w_shift = h * pool_h + max_idx[0], w * pool_w + max_idx[1]
layer_grads[a, h_shift, w_shift] = pre_grad[a, a, w]
else:
raise ValueError()
return layer_grads
def riess_reject(n_c_ch, app_mag_err_c, sig_int_c, res, threshold = 2.7):
res_scaled = np.zeros(res.shape)
for i in range(0, len(n_c_ch)):
res_scaled[i, 0: n_c_ch[i]] = np.abs(res[i, 0: n_c_ch[i]]) / \
np.sqrt(app_mag_err_c[i, 0: n_c_ch[i]] ** 2 +
sig_int_c ** 2)
to_rej = np.unravel_index(np.argmax(res_scaled), res.shape)
if res_scaled[to_rej] > threshold:
return to_rej
else:
return None
def gen_valid_move(move_index, label_map, army_map, dims):
"""Generate the top valid move given an output from network"""
x1, y1, x2, y2 = 0, 0, 0, 0
move_half = False
for i in range(moves.shape[0]):
move = moves[i]
if action_mask[move] == 0:
break
move_type, y1, x1 = np.unravel_index(move, (8, dims[0], dims[1]))
index = move_type % 4
if index == 0:
x2, y2 = x1, y1 + 1
elif index == 1:
x2, y2 = x1 + 1, y1
elif index == 2:
x2, y2 = x1, y1 - 1
elif index == 3:
x2, y2 = x1 - 1, y1
move_half = True if move_type >= 4 else False
if y2 < 0 or y2 >= dims[0] or x2 < 0 or x2 >= dims[1]:
continue
if not (
label_map[
y2,
x2] == generals.MOUNTAIN) and (
army_map[
y1,
x1] > 1):
break
return x1, y1, x2, y2, move_half