ct_segnet/seg_utils.py
def seg_chunk(self, p, max_patches = None, overlap = None,\
                  nprocs = None, arr_split = 1):
        """Segment a volume of shape (nslices, ny, nx). The 2D keras model passes\
        along nslices, segmenting images (ny, nx) with a patch size defined by input \
        to the model
        max_patches   : tuple, (my, mx) are # of patches along Y, X in image (ny, nx)
        overlap       : tuple or int, number of overlapping pixels between patches
        nprocs        : number of CPU processors for multiprocessing Pool
        arr_split     : breakdown chunk into arr_split number of smaller chunks
        """
        
        # Handle patching parameter inputs
        patch_size = self.model.output_shape[1:-1]
        if type(max_patches) is not tuple:
            max_patches = (max_patches, max_patches)
            
        if type(overlap) is not tuple:
            overlap = (overlap, overlap)
        overlap = (0 if max_patches[0] == 1 else overlap[0],\
                   0 if max_patches[1] == 1 else overlap[1])
    
        # Resize images
        orig_shape = p[0].shape
        p = np.asarray([cv2.resize(p[ii], (max_patches[1]*patch_size[1] - overlap[1],\
                                           max_patches[0]*patch_size[0] - overlap[0]))\
                        for ii in range(p.shape[0])])
        
        # Make patches
        message("Making patches...")
        message("\tCurrent d shape:" + str(np.shape(p)))
        downres_shape = p[0].shape
        steps = PM.get_stepsize(downres_shape, patch_size)
        p = Parallelize(p, PM.get_patches, procs = nprocs, \
                           patch_size = patch_size, steps = steps)
        p = np.asarray(p)
        
        # The dataset now has shape: (nslices, ny, nx, py, px),
        # where ny, nx are # of patches, and py, px is patch_shape.
        # Reshape this dataset into (n, py, px) where n = nslices*ny*nx.
        dataset_shape = p.shape
        p = p.reshape((-1,) + patch_size)
        
        # Predict using the model.
        message("Running predictions using model...")
        message("\tCurrent d shape:" + str(np.shape(p)))
        p = self.model.predict(p[...,np.newaxis])
        p = p[...,0]
        
        # Now, reshape the data back...
        p = p.reshape(dataset_shape)
        p = [p[ii] for ii in range(p.shape[0])]
        
        # Reconstruct from patches...
        message("Reconstructing from patches...")
        message("\tCurrent d shape:" + str(np.shape(p)))
        p = np.array_split(p, arr_split)
        
        p = [np.asarray(Parallelize(p[ii], PM.recon_patches,\
                                    img_shape = downres_shape,\
                                    steps = steps, procs = nprocs\
                                   )) for ii in range(arr_split)]
        p = np.concatenate(p, axis = 0)
        
        # Finally, resize the images to the original shape of slices... This will result in some loss of resolution...
        message("Resizing images to original slice size...")
        message("\tCurrent d shape:" + str(np.shape(p)))
        p = np.asarray([cv2.resize(p[ii], (orig_shape[1], orig_shape[0]))\
                        for ii in range(p.shape[0])])
        return np.asarray(np.round(p)).astype(np.uint8)
Segment a volume of shape (nslices, ny, nx). The 2D keras model passes\
        along nslices, segmenting images (ny, nx) with a patch size defined by input \
        to the model
        max_patches   : tuple, (my, mx) are # of patches along Y, X in image (ny, nx)
        overlap       : tuple or int, number of overlapping pixels between patches
        nprocs        : number of CPU processors for multiprocessing Pool
        arr_split     : breakdown chunk into arr_split number of smaller chunks
        
Segment a volume of shape (nslices, ny, nx). The 2D keras model passes\
along nslices, segmenting images (ny, nx) with a patch size defined by input \
to the model
max_patches   : tuple, (my, mx) are # of patches along Y, X in image (ny, nx)
overlap       : tuple or int, number of overlapping pixels between patches
nprocs        : number of CPU processors for multiprocessing Pool
arr_split     : breakdown chunk into arr_split number of smaller chunks
Segment a volume of shape ( nslices ny nx ) . The 2D keras model passes \ along nslices segmenting images ( ny nx ) with a patch size defined by input \ to the model max_patches : tuple ( my mx ) are # of patches along Y X in image ( ny nx ) overlap : tuple or int number of overlapping pixels between patches nprocs : number of CPU processors for multiprocessing Pool arr_split : breakdown chunk into arr_split number of smaller chunks
def seg_chunk(self, p, max_patches = None, overlap = None,\
                  nprocs = None, arr_split = 1):
        patch_size = self.model.output_shape[1:-1]
        if type(max_patches) is not tuple:
            max_patches = (max_patches, max_patches)
        if type(overlap) is not tuple:
            overlap = (overlap, overlap)
        overlap = (0 if max_patches[0] == 1 else overlap[0],\
                   0 if max_patches[1] == 1 else overlap[1])
        orig_shape = p[0].shape
        p = np.asarray([cv2.resize(p[ii], (max_patches[1]*patch_size[1] - overlap[1],\
                                           max_patches[0]*patch_size[0] - overlap[0]))\
                        for ii in range(p.shape[0])])
        message("Making patches...")
        message("\tCurrent d shape:" + str(np.shape(p)))
        downres_shape = p[0].shape
        steps = PM.get_stepsize(downres_shape, patch_size)
        p = Parallelize(p, PM.get_patches, procs = nprocs, \
                           patch_size = patch_size, steps = steps)
        p = np.asarray(p)
        dataset_shape = p.shape
        p = p.reshape((-1,) + patch_size)
        message("Running predictions using model...")
        message("\tCurrent d shape:" + str(np.shape(p)))
        p = self.model.predict(p[...,np.newaxis])
        p = p[...,0]
        p = p.reshape(dataset_shape)
        p = [p[ii] for ii in range(p.shape[0])]
        message("Reconstructing from patches...")
        message("\tCurrent d shape:" + str(np.shape(p)))
        p = np.array_split(p, arr_split)
        p = [np.asarray(Parallelize(p[ii], PM.recon_patches,\
                                    img_shape = downres_shape,\
                                    steps = steps, procs = nprocs\
                                   )) for ii in range(arr_split)]
        p = np.concatenate(p, axis = 0)
        message("Resizing images to original slice size...")
        message("\tCurrent d shape:" + str(np.shape(p)))
        p = np.asarray([cv2.resize(p[ii], (orig_shape[1], orig_shape[0]))\
                        for ii in range(p.shape[0])])
        return np.asarray(np.round(p)).astype(np.uint8)
def seg_chunk ( self , p , max_patches = None , overlap = None , nprocs = None , arr_split = 1 ) : patch_size = self . model . output_shape [ 1 : - 1 ] if type ( max_patches ) is not tuple : max_patches = ( max_patches , max_patches ) if type ( overlap ) is not tuple : overlap = ( overlap , overlap ) overlap = ( 0 if max_patches [ 0 ] == 1 else overlap [ 0 ] , 0 if max_patches [ 1 ] == 1 else overlap [ 1 ] ) orig_shape = p [ 0 ] . shape p = np . asarray ( [ cv2 . resize ( p [ ii ] , ( max_patches [ 1 ] * patch_size [ 1 ] - overlap [ 1 ] , max_patches [ 0 ] * patch_size [ 0 ] - overlap [ 0 ] ) ) for ii in range ( p . shape [ 0 ] ) ] ) message ( "Making patches..." ) message ( "\tCurrent d shape:" + str ( np . shape ( p ) ) ) downres_shape = p [ 0 ] . shape steps = PM . get_stepsize ( downres_shape , patch_size ) p = Parallelize ( p , PM . get_patches , procs = nprocs , patch_size = patch_size , steps = steps ) p = np . asarray ( p ) dataset_shape = p . shape p = p . reshape ( ( - 1 , ) + patch_size ) message ( "Running predictions using model..." ) message ( "\tCurrent d shape:" + str ( np . shape ( p ) ) ) p = self . model . predict ( p [ ... , np . newaxis ] ) p = p [ ... , 0 ] p = p . reshape ( dataset_shape ) p = [ p [ ii ] for ii in range ( p . shape [ 0 ] ) ] message ( "Reconstructing from patches..." ) message ( "\tCurrent d shape:" + str ( np . shape ( p ) ) ) p = np . array_split ( p , arr_split ) p = [ np . asarray ( Parallelize ( p [ ii ] , PM . recon_patches , img_shape = downres_shape , steps = steps , procs = nprocs ) ) for ii in range ( arr_split ) ] p = np . concatenate ( p , axis = 0 ) message ( "Resizing images to original slice size..." ) message ( "\tCurrent d shape:" + str ( np . shape ( p ) ) ) p = np . asarray ( [ cv2 . resize ( p [ ii ] , ( orig_shape [ 1 ] , orig_shape [ 0 ] ) ) for ii in range ( p . shape [ 0 ] ) ] ) return np . asarray ( np . round ( p ) ) . astype ( np . uint8 )
Segment a volume of shape (nslices, ny, nx).
Segment a volume of shape ( nslices ny nx ) .
"""Segment a volume of shape (nslices, ny, nx). The 2D keras model passes\
        along nslices, segmenting images (ny, nx) with a patch size defined by input \
        to the model
        max_patches   : tuple, (my, mx) are # of patches along Y, X in image (ny, nx)
        overlap       : tuple or int, number of overlapping pixels between patches
        nprocs        : number of CPU processors for multiprocessing Pool
        arr_split     : breakdown chunk into arr_split number of smaller chunks
        """ # Handle patching parameter inputs # Resize images # Make patches # The dataset now has shape: (nslices, ny, nx, py, px), # where ny, nx are # of patches, and py, px is patch_shape. # Reshape this dataset into (n, py, px) where n = nslices*ny*nx. # Predict using the model. # Now, reshape the data back... # Reconstruct from patches... # Finally, resize the images to the original shape of slices... This will result in some loss of resolution...
self
p
max_patches
overlap
nprocs
arr_split