def build_programs(clctx, config): sigmas = [s['diffusion'] for s in config['signals'][:4]] colony = genome.genome_cl(config['genome']) colony += convolution.gaussian_cl(sigmas) colony = cl.Program(clctx.ctx, colony).build() feats = cl.Program(clctx.ctx, features.features_cl()).build() sigma2, sigma4 = [np.sqrt(2.0)] * 4, [2.0] * 4 blurs2 = cl.Program(clctx.ctx, convolution.gaussian_cl(sigma2)).build() blurs4 = cl.Program(clctx.ctx, convolution.gaussian_cl(sigma4)).build() rdctns = cl.Program(clctx.ctx, reduction.reduction_sum_cl()).build() colours = cl.Program(clctx.ctx, colour.colour_cl()).build() return colony, feats, blurs2, blurs4, rdctns, colours
def profile(): import time from iib.simulation import CLContext from skimage import io, data, transform gs, wgs = 256, 16 # Load some test data r = transform.resize sigs = np.empty((gs, gs, 4), np.float32) sigs[:, :, 0] = r(data.coins().astype(np.float32) / 255.0, (gs, gs)) sigs[:, :, 1] = r(data.camera().astype(np.float32) / 255.0, (gs, gs)) sigs[:, :, 2] = r(data.text().astype(np.float32) / 255.0, (gs, gs)) sigs[:, :, 3] = r(data.checkerboard().astype(np.float32) / 255.0, (gs, gs)) sigs[:, :, 2] = r(io.imread("../scoring/corpus/rds/turing_001.png", as_grey=True), (gs, gs)) sigs[:, :, 3] = io.imread("../scoring/corpus/synthetic/blobs.png", as_grey=True) sigs = sigs.reshape(gs*gs*4) # Set up OpenCL ctx = cl.create_some_context(interactive=False) queue = cl.CommandQueue(ctx) mf = cl.mem_flags ifmt_f = cl.ImageFormat(cl.channel_order.RGBA, cl.channel_type.FLOAT) bufi = cl.Image(ctx, mf.READ_ONLY, ifmt_f, (gs, gs)) cl.enqueue_copy(queue, bufi, sigs, origin=(0, 0), region=(gs, gs)) clctx = CLContext(ctx, queue, ifmt_f, gs, wgs) # Compile the kernels feats = cl.Program(ctx, features_cl()).build() rdctn = cl.Program(ctx, reduction.reduction_sum_cl()).build() blur2 = cl.Program(ctx, convolution.gaussian_cl([np.sqrt(2.0)]*4)).build() blur4 = cl.Program(ctx, convolution.gaussian_cl([np.sqrt(4.0)]*4)).build() iters = 500 t0 = time.time() for i in range(iters): get_features(clctx, feats, rdctn, blur2, blur4, bufi) print((time.time() - t0)/iters)
def test(): import matplotlib.pyplot as plt from skimage import io, data, transform from iib.simulation import CLContext gs, wgs = 256, 16 # Load some test data r = transform.resize sigs = np.empty((gs, gs, 4), np.float32) sigs[:, :, 0] = r(data.coins().astype(np.float32) / 255.0, (gs, gs)) sigs[:, :, 1] = r(data.camera().astype(np.float32) / 255.0, (gs, gs)) sigs[:, :, 2] = r(data.text().astype(np.float32) / 255.0, (gs, gs)) sigs[:, :, 3] = r(data.checkerboard().astype(np.float32) / 255.0, (gs, gs)) sigs[:, :, 2] = r(io.imread("../scoring/corpus/rds/turing_001.png", as_grey=True), (gs, gs)) sigs[:, :, 3] = io.imread("../scoring/corpus/synthetic/blobs.png", as_grey=True) #sq = np.arange(256).astype(np.float32).reshape((16, 16)) / 255.0 #sigs[:, :, 0] = np.tile(sq, (16, 16)) sigs = sigs.reshape(gs*gs*4) # Set up OpenCL ctx = cl.create_some_context(interactive=False) queue = cl.CommandQueue(ctx) mf = cl.mem_flags ifmt_f = cl.ImageFormat(cl.channel_order.RGBA, cl.channel_type.FLOAT) bufi = cl.Image(ctx, mf.READ_ONLY, ifmt_f, (gs, gs)) cl.enqueue_copy(queue, bufi, sigs, origin=(0, 0), region=(gs, gs)) clctx = CLContext(ctx, queue, ifmt_f, gs, wgs) # Compile the kernels feats = cl.Program(ctx, features_cl()).build() rdctn = cl.Program(ctx, reduction.reduction_sum_cl()).build() blur2 = cl.Program(ctx, convolution.gaussian_cl([np.sqrt(2.0)]*4)).build() blur4 = cl.Program(ctx, convolution.gaussian_cl([np.sqrt(4.0)]*4)).build() entropy = get_entropy(clctx, feats, rdctn, bufi) print("Average entropy:", entropy) variance = get_variance(clctx, feats, rdctn, bufi) print("Variance:", variance) edges = get_edges(clctx, feats, rdctn, blur4, bufi, summarise=False) edge_counts = get_edges(clctx, feats, rdctn, blur4, bufi) print("Edge pixel counts:", edge_counts) blobs = get_blobs(clctx, feats, rdctn, blur2, bufi, summarise=False) features = get_features(clctx, feats, rdctn, blur2, blur4, bufi) print("Feature vector:") print(features) # Plot the edges and blobs for i in range(4): plt.subplot(4, 3, i*3+1) img = sigs.reshape((gs, gs, 4))[:, :, i] plt.imshow(img, cmap="gray") plt.xticks([]) plt.yticks([]) plt.subplot(4, 3, i*3+2) img = edges.reshape((gs, gs, 4))[:, :, i] plt.imshow(img, cmap="gray") plt.xticks([]) plt.yticks([]) ax = plt.subplot(4, 3, i*3+3) img = sigs.reshape((gs, gs, 4))[:, :, i] plt.imshow(img, cmap="gray") plt.xticks([]) plt.yticks([]) for j in range(len(blobs)): sblobs = blobs[j] s = 2**(j+1) r = np.sqrt(2.0) * s im = sblobs[:, :, i] posns = np.transpose(im.nonzero()) * 2**(j+1) for xy in posns: circ = plt.Circle((xy[1], xy[0]), r, color="green", fill=False) ax.add_patch(circ) plt.show()