def is_max(pos): def is_smaller_neighbor(offset): neighbor_idx = tuple( mode(p, o - w / 2, w) for (p, o, w) in zip(pos, offset, wsize)) return data[neighbor_idx] <= data[pos] return np.all(parakeet.imap(is_smaller_neighbor, wsize))
def local_maxima(data, wsize, mode=wrap): def is_max(pos): def is_smaller_neighbor(offset): neighbor_idx = tuple(mode(p, o-w/2, w) for (p, o, w) in zip(pos, offset, wsize)) return data[neighbor_idx] <= data[pos] return np.all(parakeet.imap(is_smaller_neighbor, wsize)) return parakeet.imap(is_max, data.shape)
def test_identity(): x = np.ones((1, 1, 2, 1)) def get_idx(idx): return x[idx] y = parakeet.imap(get_idx, x.shape) assert eq(x, y)
def conv_3x3_imap(image, weights): def compute((i,j)): total = np.float32(0.0) for ii in xrange(3): for jj in xrange(3): total += image[i+ii-1, j + jj - 1] * weights[ii, jj] return total w,h = image.shape return parakeet.imap(compute, (w-2,h-2))
def parakeet_local_maxima(data, wsize, mode=wrap): def is_max(pos): def is_smaller_neighbor(offset): neighbor_idx = tuple( mode(p, o - w / 2, w) for (p, o, w) in zip(pos, offset, wsize)) return data[neighbor_idx] <= data[pos] return np.all(parakeet.imap(is_smaller_neighbor, wsize)) return parakeet.imap(is_max, data.shape)
def conv_3x3_imap(image, weights): def compute((i, j)): total = np.float32(0.0) for ii in xrange(3): for jj in xrange(3): total += image[i + ii - 1, j + jj - 1] * weights[ii, jj] return total w, h = image.shape return parakeet.imap(compute, (w - 2, h - 2))
def maxpool(array, pool_size): n, w, h = array.shape def _inner(args): img, x, y = args max_val = -1e12 for i in xrange(0, pool_size): for j in xrange(0, pool_size): if x + i < w and y + j < h: max_val = max(array[img, x + i, y + j]) return max_val return parakeet.imap(_inner, (n, w, h))
def _convolve(local_image, local_filters): num_images, n_color, w, h = local_image.shape num_filts, f_col, fw, fh = local_filters.shape def _inner(args): iid, fid, x, y = args image = local_image[iid] f = local_filters[fid] out = 0 for c in xrange(n_color): for i in xrange(fw): for j in xrange(fh): if x + i < w and y + j < h: out += image[c, x + i, y + j] * f[c, i, j] return out return parakeet.imap(_inner, (num_images, num_filts, w, h))
def growcut_par(image, state, window_radius): height = image.shape[0] width = image.shape[1] def attack((i,j)): winning_colony = state[i, j, 0] defense_strength = state[i, j, 1] for jj in xrange(max(j-window_radius,0), min(j+window_radius+1, width)): for ii in xrange(max(i-window_radius, 0), min(i+window_radius+1, height)): if ii != i or jj != j: d = image[i, j, :] - image[ii, jj, :] s = np.sum(d**2) gval = 1.0 - np.sqrt(s) / np.sqrt(3) attack_strength = gval * state[ii, jj, 1] if attack_strength > defense_strength: defense_strength = attack_strength winning_colony = state[ii, jj, 0] return [winning_colony, defense_strength] return parakeet.imap(attack, (height, width))
def growcut_par(image, state, window_radius): height = image.shape[0] width = image.shape[1] def attack((i, j)): winning_colony = state[i, j, 0] defense_strength = state[i, j, 1] for jj in xrange(max(j - window_radius, 0), min(j + window_radius + 1, width)): for ii in xrange(max(i - window_radius, 0), min(i + window_radius + 1, height)): if ii != i or jj != j: d = image[i, j, :] - image[ii, jj, :] s = np.sum(d**2) gval = 1.0 - np.sqrt(s) / np.sqrt(3) attack_strength = gval * state[ii, jj, 1] if attack_strength > defense_strength: defense_strength = attack_strength winning_colony = state[ii, jj, 0] return [winning_colony, defense_strength] return parakeet.imap(attack, (height, width))
def is_max(pos): def is_smaller_neighbor(offset): neighbor_idx = tuple(mode(p, o-w/2, w) for (p, o, w) in zip(pos, offset, wsize)) return data[neighbor_idx] <= data[pos] return np.all(parakeet.imap(is_smaller_neighbor, wsize))
def fill_with_const(a, k): def return_const(idx): return k return parakeet.imap(return_const, a.shape)
def test_identity(): x = np.ones((1,1,2,1)) def get_idx(idx): return x[idx] y = parakeet.imap(get_idx, x.shape) assert eq(x,y)
def test_identity(): x = np.random.randn(10,20,3,2) def get_idx(idx): return x[idx] y = parakeet.imap(get_idx, x.shape) assert eq(x,y)