def __pad(image, px, py): if px == 0 and py == 0: return image d = image.dims() pad_x = d[0]+2*px pad_y = d[1]+2*py return af.moddims(af.unwrap(image, pad_x, pad_y, pad_x, pad_y, px, py), pad_x, pad_y, d[2])
def pool(image, w, s): #TODO: Remove assumption of image.dims()[3] == 1 d0, d1, d2 = image.dims() #TODO: remove reorder and moddims call image = af.moddims(af.reorder(image, 2, 0, 1), 1, d2, d0, d1) d0, d1, d2, d3 = image.dims() h_o = (d2 - w)/s + 1 w_o = (d3 - w)/s + 1 tiles = af.unwrap(af.reorder(image, 2, 3, 1, 0), w, w, s, s) return af.reorder(af.reorder(af.moddims(af.max(tiles, 0), d0, h_o, w_o, d1), 0, 3, 1, 2), 2, 3, 1, 0)
def conv(weights, biases, image, wx, wy, sx = 1, sy = 1, px = 0, py = 0, groups = 1): image = __pad(image, px, py) batch = util.num_input(image) n_filters = util.num_filters(weights) n_channel = util.num_channels(weights) w_i = image.dims()[0] h_i = image.dims()[1] w_o = (w_i - wx) / sx + 1 h_o = (h_i - wy) / sy + 1 tiles = af.unwrap(image, wx, wy, sx, sy) weights = af.moddims(weights, wx*wy, n_channel, n_filters) out = af.constant(0, batch, n_filters, w_o, h_o) if groups > 1: out = af.constant(0, w_o, h_o, n_filters, batch) split_in = util.num_channels(image) / groups s_i = split_in split_out = n_filters / groups s_o = split_out for i in xrange(groups): weights_slice = af.moddims(weights[:,:,i*s_o:(i+1)*s_o], wx, wy, n_channel, split_out) biases_slice = biases[i*s_o:(i+1)*s_o] image_slice = image[:,:,i*s_i:(i+1)*s_i] out[:,:,i*s_o:(i+1)*s_o] = conv(weights_slice, biases_slice, image_slice, wx, wy, sx, sy, 0, 0, 1) # out[:,i*s_o:(i+1)*s_o] = conv(weights_slice, # biases_slice, # image_slice, # wx, wy, sx, sy, 0, 0, 1) return out #TODO: Speedup this section for f in xrange(n_filters): for d in xrange(n_channel): tile_d = af.reorder(tiles[:,:,d],1,0) weight_d = weights[:,d,f] out[0,f] += af.moddims(af.matmul(tile_d, weight_d), 1,1, w_o, h_o) out[0,f] += biases[f].to_list()[0] return af.reorder(out, 2, 3, 1, 0)
def lrn(image, size, alpha, beta): #TODO: Remove assumption of image.dims()[3] == 1 d0, d1, d2 = image.dims() #TODO: remove reorder and moddims call image = af.moddims(af.reorder(image, 2, 0, 1), 1, d2, d0, d1) d0, d1, d2, d3 = image.dims() pad = (size - 1) / 2 padded_size = d1+2*pad out = af.constant(0,d1, d2, d3) image = af.moddims(image, d1, d2, d3) padded = af.moddims(af.unwrap(image, padded_size, d2, padded_size, d2, pad,0), padded_size, d2, d2) padded = padded*padded for i in xrange(size): out += padded[i:i+d1] out = 1 + (float(alpha)/size) * out out = af.pow(out,beta) #TODO: remove reorder call return af.reorder(af.moddims(image/out, d0,d1,d2,d3), 2, 3, 1, 0)
def simple_image(verbose=False): display_func = _util.display_func(verbose) print_func = _util.print_func(verbose) a = 10 * af.randu(6, 6) a3 = 10 * af.randu(5, 5, 3) dx, dy = af.gradient(a) display_func(dx) display_func(dy) display_func(af.resize(a, scale=0.5)) display_func(af.resize(a, odim0=8, odim1=8)) t = af.randu(3, 2) display_func(af.transform(a, t)) display_func(af.rotate(a, 3.14)) display_func(af.translate(a, 1, 1)) display_func(af.scale(a, 1.2, 1.2, 7, 7)) display_func(af.skew(a, 0.02, 0.02)) h = af.histogram(a, 3) display_func(h) display_func(af.hist_equal(a, h)) display_func(af.dilate(a)) display_func(af.erode(a)) display_func(af.dilate3(a3)) display_func(af.erode3(a3)) display_func(af.bilateral(a, 1, 2)) display_func(af.mean_shift(a, 1, 2, 3)) display_func(af.medfilt(a)) display_func(af.minfilt(a)) display_func(af.maxfilt(a)) display_func(af.regions(af.round(a) > 3)) dx, dy = af.sobel_derivatives(a) display_func(dx) display_func(dy) display_func(af.sobel_filter(a)) display_func(af.gaussian_kernel(3, 3)) display_func(af.gaussian_kernel(3, 3, 1, 1)) ac = af.gray2rgb(a) display_func(ac) display_func(af.rgb2gray(ac)) ah = af.rgb2hsv(ac) display_func(ah) display_func(af.hsv2rgb(ah)) display_func(af.color_space(a, af.CSPACE.RGB, af.CSPACE.GRAY)) a = af.randu(6, 6) b = af.unwrap(a, 2, 2, 2, 2) c = af.wrap(b, 6, 6, 2, 2, 2, 2) display_func(a) display_func(b) display_func(c) display_func(af.sat(a)) a = af.randu(10, 10, 3) display_func(af.rgb2ycbcr(a)) display_func(af.ycbcr2rgb(a))
def simple_image(verbose = False): display_func = _util.display_func(verbose) print_func = _util.print_func(verbose) a = 10 * af.randu(6, 6) a3 = 10 * af.randu(5,5,3) dx,dy = af.gradient(a) display_func(dx) display_func(dy) display_func(af.resize(a, scale=0.5)) display_func(af.resize(a, odim0=8, odim1=8)) t = af.randu(3,2) display_func(af.transform(a, t)) display_func(af.rotate(a, 3.14)) display_func(af.translate(a, 1, 1)) display_func(af.scale(a, 1.2, 1.2, 7, 7)) display_func(af.skew(a, 0.02, 0.02)) h = af.histogram(a, 3) display_func(h) display_func(af.hist_equal(a, h)) display_func(af.dilate(a)) display_func(af.erode(a)) display_func(af.dilate3(a3)) display_func(af.erode3(a3)) display_func(af.bilateral(a, 1, 2)) display_func(af.mean_shift(a, 1, 2, 3)) display_func(af.medfilt(a)) display_func(af.minfilt(a)) display_func(af.maxfilt(a)) display_func(af.regions(af.round(a) > 3)) dx,dy = af.sobel_derivatives(a) display_func(dx) display_func(dy) display_func(af.sobel_filter(a)) display_func(af.gaussian_kernel(3, 3)) display_func(af.gaussian_kernel(3, 3, 1, 1)) ac = af.gray2rgb(a) display_func(ac) display_func(af.rgb2gray(ac)) ah = af.rgb2hsv(ac) display_func(ah) display_func(af.hsv2rgb(ah)) display_func(af.color_space(a, af.CSPACE.RGB, af.CSPACE.GRAY)) a = af.randu(6,6) b = af.unwrap(a, 2, 2, 2, 2) c = af.wrap(b, 6, 6, 2, 2, 2, 2) display_func(a) display_func(b) display_func(c) display_func(af.sat(a)) a = af.randu(10,10,3) display_func(af.rgb2ycbcr(a)) display_func(af.ycbcr2rgb(a)) a = af.randu(10, 10) b = af.canny(a, low_threshold = 0.2, high_threshold = 0.8) display_func(af.anisotropic_diffusion(a, 0.125, 1.0, 64, af.FLUX.QUADRATIC, af.DIFFUSION.GRAD))
def simple_image(verbose=False): display_func = _util.display_func(verbose) print_func = _util.print_func(verbose) a = 10 * af.randu(6, 6) a3 = 10 * af.randu(5, 5, 3) dx, dy = af.gradient(a) display_func(dx) display_func(dy) display_func(af.resize(a, scale=0.5)) display_func(af.resize(a, odim0=8, odim1=8)) t = af.randu(3, 2) display_func(af.transform(a, t)) display_func(af.rotate(a, 3.14)) display_func(af.translate(a, 1, 1)) display_func(af.scale(a, 1.2, 1.2, 7, 7)) display_func(af.skew(a, 0.02, 0.02)) h = af.histogram(a, 3) display_func(h) display_func(af.hist_equal(a, h)) display_func(af.dilate(a)) display_func(af.erode(a)) display_func(af.dilate3(a3)) display_func(af.erode3(a3)) display_func(af.bilateral(a, 1, 2)) display_func(af.mean_shift(a, 1, 2, 3)) display_func(af.medfilt(a)) display_func(af.minfilt(a)) display_func(af.maxfilt(a)) display_func(af.regions(af.round(a) > 3)) dx, dy = af.sobel_derivatives(a) display_func(dx) display_func(dy) display_func(af.sobel_filter(a)) ac = af.gray2rgb(a) display_func(ac) display_func(af.rgb2gray(ac)) ah = af.rgb2hsv(ac) display_func(ah) display_func(af.hsv2rgb(ah)) display_func(af.color_space(a, af.CSPACE.RGB, af.CSPACE.GRAY)) a = af.randu(6, 6) b = af.unwrap(a, 2, 2, 2, 2) c = af.wrap(b, 6, 6, 2, 2, 2, 2) display_func(a) display_func(b) display_func(c) display_func(af.sat(a)) a = af.randu(10, 10, 3) display_func(af.rgb2ycbcr(a)) display_func(af.ycbcr2rgb(a))
def simple_image(verbose=False): display_func = _util.display_func(verbose) a = 10 * af.randu(6, 6) a3 = 10 * af.randu(5, 5, 3) dx, dy = af.gradient(a) display_func(dx) display_func(dy) display_func(af.resize(a, scale=0.5)) display_func(af.resize(a, odim0=8, odim1=8)) t = af.randu(3, 2) display_func(af.transform(a, t)) display_func(af.rotate(a, 3.14)) display_func(af.translate(a, 1, 1)) display_func(af.scale(a, 1.2, 1.2, 7, 7)) display_func(af.skew(a, 0.02, 0.02)) h = af.histogram(a, 3) display_func(h) display_func(af.hist_equal(a, h)) display_func(af.dilate(a)) display_func(af.erode(a)) display_func(af.dilate3(a3)) display_func(af.erode3(a3)) display_func(af.bilateral(a, 1, 2)) display_func(af.mean_shift(a, 1, 2, 3)) display_func(af.medfilt(a)) display_func(af.minfilt(a)) display_func(af.maxfilt(a)) display_func(af.regions(af.round(a) > 3)) display_func( af.confidenceCC(af.randu(10, 10), (af.randu(2) * 9).as_type(af.Dtype.u32), (af.randu(2) * 9).as_type(af.Dtype.u32), 3, 3, 10, 0.1)) dx, dy = af.sobel_derivatives(a) display_func(dx) display_func(dy) display_func(af.sobel_filter(a)) display_func(af.gaussian_kernel(3, 3)) display_func(af.gaussian_kernel(3, 3, 1, 1)) ac = af.gray2rgb(a) display_func(ac) display_func(af.rgb2gray(ac)) ah = af.rgb2hsv(ac) display_func(ah) display_func(af.hsv2rgb(ah)) display_func(af.color_space(a, af.CSPACE.RGB, af.CSPACE.GRAY)) a = af.randu(6, 6) b = af.unwrap(a, 2, 2, 2, 2) c = af.wrap(b, 6, 6, 2, 2, 2, 2) display_func(a) display_func(b) display_func(c) display_func(af.sat(a)) a = af.randu(10, 10, 3) display_func(af.rgb2ycbcr(a)) display_func(af.ycbcr2rgb(a)) a = af.randu(10, 10) b = af.canny(a, low_threshold=0.2, high_threshold=0.8) display_func( af.anisotropic_diffusion(a, 0.125, 1.0, 64, af.FLUX.QUADRATIC, af.DIFFUSION.GRAD)) a = af.randu(10, 10) psf = af.gaussian_kernel(3, 3) cimg = af.convolve(a, psf) display_func( af.iterativeDeconv(cimg, psf, 100, 0.5, af.ITERATIVE_DECONV.LANDWEBER)) display_func( af.iterativeDeconv(cimg, psf, 100, 0.5, af.ITERATIVE_DECONV.RICHARDSONLUCY)) display_func(af.inverseDeconv(cimg, psf, 1.0, af.INVERSE_DECONV.TIKHONOV))