def test_padding(): # create test 2D array R = 5 C = 10 x = np.array(range(R * C)).reshape(R, C) # array([[ 0, 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]]) # split into blocks block_slices, blocks, xout = pymg.block_split(x, nblocks=(1, 4), pad_width=(2, 3), mode='constant', constant_values=0) # check number of blocks assert (1 * 4 == len(blocks)) # check that array has been padded assert ((np.pad(x, pad_width=(2, 3), mode='constant') == xout).all()) # load ground truth expected_blocks = example_padded_2D_array_blocks() # check that all blocks were computed as expected for eb, b in zip(expected_blocks, blocks): assert ((eb == b).all())
def test_3d_array(): S = 3 R = 7 C = 5 # create 3D array x = np.array(range(R * C * S)).reshape(S, R, C) # array([[[ 0, 1, 2], # [ 3, 4, 5], # [ 6, 7, 8], # [ 9, 10, 11], # [ 12, 13, 14]], # # ... # # [[ 90, 91, 92], # [ 93, 94, 95], # [ 96, 97, 98], # [ 99, 100, 101], # [102, 103, 104]]]) # split into blocks block_slices, blocks, _ = pymg.block_split(x, nblocks=(3, 3, 2), by_reference=True) # load ground truth expected_blocks = example_3D_array_blocks() # check that all blocks were computed as expected for eb, b in zip(expected_blocks, blocks): assert ((eb == b).all())
def test_blocks_by_value(): # create test 2D array R = 5 C = 10 x = np.array(range(R * C)).reshape(R, C) # array([[ 0, 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]]) # split into blocks block_slices, blocks, xout = pymg.block_split(x, nblocks=(2, 3), by_reference=False) # check number of blocks assert (2 * 3 == len(blocks)) # check that array hasn't changed assert ((x == xout).all()) # load ground truth expected_blocks = example_2D_array_blocks() # check that all blocks were computed as expected for eb, b in zip(expected_blocks, blocks): assert ((eb == b).all()) # set one block to zeros, and check that the original array does not change blocks[3][...] = 0 assert ((x[block_slices[3]] == expected_blocks[3]).all())
def aux_split_stack(R, C, nblocks, S=1, pad_width=0, mode='constant', constant_values=0): if (S == 1): x = np.array(range(R * C)).reshape(R, C) else: x = np.array(range(S * R * C)).reshape(S, R, C) # array([[ 0, 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]]) # split into blocks block_slices, blocks, xout = pymg.block_split( x, nblocks=nblocks, pad_width=pad_width, mode=mode, constant_values=constant_values) # check number of blocks assert (np.prod(nblocks) == len(blocks)) # check that array has been padded assert ((np.pad(x, pad_width=pad_width, mode='constant') == xout).all()) # stack blocks x2, block_slices2 = pymg.block_stack(blocks, block_slices, pad_width=pad_width) # check that the array is correctly recovered assert ((x == x2).all())
def test_no_reference_with_padding(): # create test 2D array R = 5 C = 10 x = np.array(range(R * C)).reshape(R, C) # array([[ 0, 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]]) # split into blocks try: block_slices, blocks, xout = pymg.block_split(x, nblocks=(2, 3), pad_width=3, by_reference=True) except Exception: pass else: raise Exception('Exception not raised')
pad_xy = 30 # create image im = np.ones(shape=(1, 2, 200, 200)) im[:, :, 0:50, 0:50] = 2 im[:, :, 100:150, 100:150] = 2 # apply DeepCell's preprocessing im[0, 0, :, :] = deepcell.process_image(im[0, 0, :, :], 30, 30) im[0, 1, :, :] = deepcell.process_image(im[0, 1, :, :], 30, 30) # split into blocks block_slices, blocks, im_padded = pymproc.block_split( im, nblocks=(1, 1, 1, 2), pad_width=((0, 0), (0, 0), (pad_xy, pad_xy), (pad_xy, pad_xy)), mode='reflect') # load a model j = 0 # model index model = deepcell_models.sparse_bn_feature_net_61x61(batch_input_shape=im.shape) model = deepcell.set_weights( model, os.path.join(netdir, model_dir, model_weights_file + str(j) + '.h5')) # process image and blocks im_out = model.predict(im) im_out = np.pad(im_out, pad_width=((0, 0), (0, 0), (pad_xy, pad_xy), (pad_xy, pad_xy)), mode='constant',
plt.ion() plt.clf() plt.imshow(im_crop) plt.draw() plt.show() plt.pause(0.1) # remove alpha channel im_crop = np.array(im_crop) im_crop = im_crop[:, :, 0:3] # split image into overlapping blocks im_crop_slices, im_crop_blocks, xout = pystoim.block_split( im_crop, (10, 10, 1), pad_width=((100, 100), (100, 100), (0, 0)), mode='reflect', reflect_type='even') # init memory and correct slices for the predictor output (input images are RGB, and predictions have 4 channels) im_crop_blocks_predicted = im_crop_blocks.copy() im_crop_slices_predicted = im_crop_slices.copy() for i, sl in enumerate(im_crop_slices_predicted): sl[2] = slice(0, 4, 1) im_crop_slices_predicted[i] = sl # apply trained model to the cropped image for i, block in enumerate(im_crop_blocks): if DEBUG: print("Processing block " + str(i) + "/" + str(len(im_crop_blocks))) aux = model.predict(block.reshape((1, ) + block.shape))
''' for fold_i, model_file in enumerate(model_files): # select test data (data not used for training) idx_test = idx_test_all[fold_i] im_test = im[idx_test, ...] dmap_test = dmap[idx_test, ...] mask_test = mask[idx_test, ...] # split data into blocks dmap_test = dmap_test[:, 0:-1, 0:-1, :] mask_test = mask_test[:, 0:-1, 0:-1, :] im_test = im_test[:, 0:-1, 0:-1, :] _, dmap_test, _ = pystoim.block_split(dmap_test, nblocks=(1, 2, 2, 1)) _, im_test, _ = pystoim.block_split(im_test, nblocks=(1, 2, 2, 1)) _, mask_test, _ = pystoim.block_split(mask_test, nblocks=(1, 2, 2, 1)) dmap_test = np.concatenate(dmap_test, axis=0) im_test = np.concatenate(im_test, axis=0) mask_test = np.concatenate(mask_test, axis=0) # load model model = cytometer.models.fcn_sherrah2016_regression( input_shape=im_test.shape[1:]) model.load_weights(model_file) # visualise results if DEBUG: for i in range(im_test.shape[0]):
''' for fold_i, model_file in enumerate(model_files): # select test data (data not used for training) idx_test = idx_test_all[fold_i] im_test = im[idx_test, ...] seg_test = seg[idx_test, ...] mask_test = mask[idx_test, ...] # split data into blocks seg_test = seg_test[:, 0:-1, 0:-1, :] mask_test = mask_test[:, 0:-1, 0:-1, :] im_test = im_test[:, 0:-1, 0:-1, :] _, seg_test, _ = pystoim.block_split(seg_test, nblocks=(1, 2, 2, 1)) _, im_test, _ = pystoim.block_split(im_test, nblocks=(1, 2, 2, 1)) _, mask_test, _ = pystoim.block_split(mask_test, nblocks=(1, 2, 2, 1)) seg_test = np.concatenate(seg_test, axis=0) im_test = np.concatenate(im_test, axis=0) mask_test = np.concatenate(mask_test, axis=0) # load model model = cytometer.models.fcn_sherrah2016_contour( input_shape=im_test.shape[1:]) model.load_weights(model_file) # visualise results if DEBUG: for i in range(im_test.shape[0]):