Beispiel #1
0
def data_coloration(img_dir, mode, ext, default_only=False):
    # TODO replace with random  numbers generated from uniform distrib.
    # l_mean_range = (144.048, 130.22, 135.5, 140.0)
    # l_std_range = (40.23, 35.00, 35.00, 37.5)
    l_mean_range = [144.048]
    l_std_range = [40.23]

    vectors = [
        # np.array([[148.60, 41.56], [169.30, 9.01], [105.97, 6.67]]),
        np.array([[145.473, 44.093], [161.354, 7.601], [120.180, 5.017]]),
        np.array([[154.202, 35.127], [161.124, 5.862], [119.579, 4.158]]),
        np.array([[139.806, 35.538], [174.429, 9.338], [104.025, 5.538]]),
        np.array([[113.509, 43.677], [173.726, 11.078], [100.717, 7.338]]),
    ]

    img_list = sorted(glob.glob(os.path.join(img_dir, '*.' + ext)))
    for idx, name in enumerate(img_list):
        if mode == 'feat':
            img = cv2.imread(name)
        elif mode == 'anno':
            img = cv2.imread(name, 0)
        else:
            print 'Unknown mode'
            return 0
        #/end if

        # Do the default colortarget
        # Replace the original image with the standard normalization
        # name = name.replace('.'+ext, 'c.'+ext)
        if mode == 'feat':
            img_out = cnorm.normalize(img)
            cv2.imwrite(filename=name, img=img_out)
        elif mode == 'anno':
            cv2.imwrite(filename=name, img=img)
        #/end if
        if default_only:
            continue
        #/end if
        # Do the rest of the preset color corrections
        # WARNING sometimes cnorm.nomalize is super slow. Don't know why.
        for target in vectors:
            name = name.replace('.' + ext, 'c.' + ext)
            if mode == 'feat':
                img_out = cnorm.normalize(img, target)
                cv2.imwrite(filename=name, img=img_out)
            elif mode == 'anno':
                cv2.imwrite(filename=name, img=img)
            # end if
        # /end for
        if idx % 500 == 0:
            if default_only:
                print '\tcolorizing {} of {} (default_only)'.format(
                    idx, len(img_list))
            else:
                print '\tcolorizing {} of {} (default + {})'.format(
                    idx, len(img_list), len(vectors))
        # /end if
    #/end for

    print '\tDone color augmenting images in {}'.format(img_dir)
Beispiel #2
0
def write_tile(tile, filename, writesize, normalize):
    # Needed for inference by SegNet
    tile = cv2.cvtColor(tile, cv2.COLOR_RGBA2RGB)  # ???
    tile = tile[:, :, (2, 1, 0)]  # ???

    tile = cv2.resize(
        tile, dsize=(writesize, writesize),
        interpolation=cv2.INTER_LINEAR)  # Before norm; for speed??

    if normalize:
        tile = cnorm.normalize(image=tile, target=None, verbose=False)

    cv2.imwrite(filename=filename, img=tile)
Beispiel #3
0
def preload_tiles(svs, coords, level, size, as_ndarray=False, normalize=False):
    tiles = [read_region(svs, x, y, level, size=size) for (y, x) in coords]

    # For completeness. Moved normalize outside & after resize
    if normalize:
        tiles = [cnorm.normalize(tile) for tile in tiles]

    # tiles = [cv2.resize(tile, dsize=(size,size)) for tile in tiles]

    if as_ndarray:
        tiles = [np.expand_dims(tile, axis=0) for tile in tiles]
        tiles = np.concatenate(tiles, axis=0)

    return tiles
Beispiel #4
0
def data_coloration(t):
    l_mean_range = (144.048, 130.22)
    l_std_range = (40.23, 35.00)

    img_list = glob.glob(os.path.join(t, '*.jpg'))
    for name in img_list:
        img = cv2.imread(name)
        print 'Coloration {}'.format(name)
        for LMN, LSTD in zip(l_mean_range, l_std_range):
            name = name.replace('.jpg', 'c.jpg')
            #			print name
            target = np.array([[LMN, LSTD], [169.3, 9.01], [105.97, 6.67]])
            img = cn.normalize(img, target)
            cv2.imwrite(filename=name, img=img)
    print 'Done colorizing'
Beispiel #5
0
def run_for_segmentation(m0in, m1in):
    m0out = '/home/nathan/mzmo/data/seg/m0'
    m1out = '/home/nathan/mzmo/data/seg/m1'

    listm0 = os.path.join(m0out, 'list.txt')
    listm1 = os.path.join(m1out, 'list.txt')

    sources = [m0in, m1in]
    destinations = [m0out, m1out]
    listfiles = [listm0, listm1]

    for src, dst, lst in zip(sources, destinations, listfiles):
        # List out contents
        if not os.path.exists(dst):
            os.makedirs(dst)

        search = os.path.join(src, '*.tif')
        files = glob.glob(search)

        # Create the blank dummy file
        dummy = np.zeros(shape=(256, 256))
        dummy_name = os.path.join(dst, 'dummy.png')
        cv2.imwrite(dummy_name, dummy)

        # Copy files over; open the list file
        with open(lst, 'w') as lf:
            for f in files:
                base = os.path.basename(f)
                base = base.replace(' ', '_')
                base = base.replace('.tif', '.jpg')
                img = cv2.imread(f)
                # Color normalize
                img = cn.normalize(img)

                # Resize directly from 1k --> 256
                img = cv2.resize(img, dsize=(256, 256))
                img_name = os.path.join(dst, base)
                print img_name
                cv2.imwrite(img_name, img)
                lf.write('{} {}\n'.format(img_name, dummy_name))
Beispiel #6
0
def data_coloration(img_dir, mode, ext):
    '''
    LOL
    '''
    # TODO replace with random  numbers generated from uniform distrib.
    # l_mean_range = (144.048, 130.22, 135.5, 140.0)
    # l_std_range = (40.23, 35.00, 35.00, 37.5)
    l_mean_range = [144.048]
    l_std_range = [40.23]

    img_list = sorted(glob.glob(os.path.join(img_dir, '*.' + ext)))
    for idx, name in enumerate(img_list):
        if mode == 'feat':
            img = cv2.imread(name)
        elif mode == 'anno':
            img = cv2.imread(name, 0)
        else:
            print 'Unknown mode'
            return 0
        #/end if
        for LMN, LSTD in zip(l_mean_range, l_std_range):
            target = np.array([[LMN, LSTD], [169.3, 9.01], [105.97, 6.67]])
            name = name.replace('.'+ext, 'c.'+ext)
            if mode == 'feat':
                img_out = cnorm.normalize(img, target)
                cv2.imwrite(filename=name, img=img_out)
            elif mode == 'anno':
                cv2.imwrite(filename=name, img=img)
            # end if
        # /end for
        if idx % 500 == 0:
            print '\tcolorizing {} of {}'.format(idx, len(img_list))
        # /end if
    #/end for

    print '\tDone color augmenting images in {}'.format(img_dir)
def process_svs(svs, prob_maps, coordinates, net, settings):
    # Check inputs
    overlap = settings['overlap']
    scales = settings['scales']
    # weights = settings['weights']
    # netproto = settings['deploy_proto']
    gpumode = settings['gpumode']
    # cnnlayer = settings['cnnlayer']
    n_classes = settings['n_classes']
    prefetch = settings['prefetch']
    do_normalize = settings['do_normalize']
    debug_mode = settings['DEBUGGING']
    bayesian = settings['bayesian']
    samples = settings['samples']

    ## Set the processing funciton up front to avoid if-else hell
    print 'Setting up for bayesian inference mode with {} samples'.format(
        samples)

    def process_fn(tile_in, samples=samples):
        tile_mean, tile_var, _ = net.bayesian_inference(tile_in,
                                                        samples=samples,
                                                        ret_all=True)
        return tile_mean, tile_var

    svs_info = {}
    svs_info = data_utils.pull_svs_stats(svs, svs_info)
    lvl20_index = svs_info['20x_lvl']
    mult_5x = svs_info['20x_to_5x']

    ## Loop over scales
    pmap_out = []
    varmap_out = []
    for coords, scale in zip(coordinates, scales):
        pmap_scale = np.copy(
            prob_maps)  ## use this line to enforce default class
        varmap_scale = np.zeros_like(pmap_scale)
        h, w = pmap_scale.shape[:2]
        processed_mean = np.zeros((h, w), dtype=np.bool)
        processed_vars = np.zeros((h, w), dtype=np.bool)

        print 'Processing {}'.format(scale)
        print 'Using {} tile coordinates'.format(len(coords))

        load_size, place_size, proc_size = get_load_place_proc_size(
            scale, settings)
        failed_count = 0

        ## Leftover from debugging
        if settings['DEBUGGING']:
            print 'Shuffling coordinates'
            random.shuffle(coords)
            indices = np.random.choice(range(len(coords)), 50)
            coords = [coords[index] for index in indices]
            print 'Subsetted {} coordinates '.format(len(coords))

        ## Divide the set into n chunks
        if len(coords) < prefetch:
            print 'Coordinates ({}) < prefetch ({})'.format(
                len(coords), prefetch)
            coord_split = [coords]
            n_splits = 1
        else:
            n_splits = len(coords) / prefetch
            coord_split = np.array_split(coords, n_splits)

        for nindx, coord_prefetch in enumerate(coord_split):
            print '[{:02d}/{:02d}]'.format(nindx + 1, n_splits),

            preload_start = time.time()
            tiles = data_utils.preload_tiles(svs,
                                             coord_prefetch,
                                             size=(load_size, load_size),
                                             level=lvl20_index)
            tiles = [
                cv2.resize(tile, dsize=(proc_size, proc_size))
                for tile in tiles
            ]
            preload_delta_t = time.time() - preload_start
            print '{} Tiles preloaded in {:03.3f}s'.format(
                len(tiles), preload_delta_t),

            if do_normalize:
                norm_start = time.time()
                tiles = [cnorm.normalize(tile) for tile in tiles]
                norm_delta_t = time.time() - norm_start
                print 'Normalizing done in {:03.3f}s'.format(norm_delta_t),
            #/end if

            ## Processing here
            # tiles = [cv2.cvtColor(tile, cv2.COLOR_RGB2GRAY) for tile in tiles]
            cnn_start = time.time()
            tiles = [np.expand_dims(tile, 0) for tile in tiles]
            tiles = [tile * (2 / 255.0) - 1 for tile in tiles
                     ]  ## Recenter to [-1,1] for SELU activations
            tile_means = []
            tile_vars = []
            for tile in tiles:
                t_mean, t_var = process_fn(tile)
                tile_means.append(t_mean)
                tile_vars.append(t_var)
            # tiles = [process_fn(tile) for tile in tiles]
            cnn_delta_t = time.time() - cnn_start
            print 'CNN finished in {:03.3f}s'.format(cnn_delta_t)

            # Resize to fit
            placing_start = time.time()
            ## -------------------- place mean
            # pmap_scale = place_tiles_into(tile_means, pmap_scale, processed, place_size, coord_prefetch, overlap, mult_5x)
            tile_means = [np.squeeze(tile) for tile in tile_means]
            tile_means = [
                cv2.resize(tile, dsize=(place_size, place_size))
                for tile in tile_means
            ]
            coord_prefetch = [(int(x * mult_5x), int(y * mult_5x))
                              for (x, y) in coord_prefetch]
            #
            ## x, y are w.r.t. 20X
            if overlap < 1 and overlap > 0:
                overlap = load_size * overlap
            ovp = int(overlap * mult_5x)
            inner = [ovp, place_size - ovp]
            in_out = np.zeros((place_size, place_size), dtype=np.bool)
            in_out[inner[0]:inner[1], inner[0]:inner[1]] = True
            for tile, (row, col) in zip(tile_means, coord_prefetch):
                # try:
                placeholder = pmap_scale[row:row + place_size,
                                         col:col + place_size, :]
                processed_pl = processed_mean[row:row + place_size,
                                              col:col + place_size]
                if (processed_pl).sum() > 0:
                    ## we've already placed some of this tile
                    placeholder[in_out] = tile[in_out]
                    tile_out = tile[in_out == 0]
                    #
                    ## Take a dirty average
                    placeholder[in_out == 0] += tile_out
                    placeholder[in_out == 0] /= 2
                    pmap_scale[row:row + place_size,
                               col:col + place_size, :] = placeholder
                else:
                    ## We haven't placed any part of this tile; place in the whole thing.
                    pmap_scale[row:row + place_size,
                               col:col + place_size, :] = tile
                processed_mean[row:row + place_size,
                               col:col + place_size] = True

            ## -------------------- place variance
            # varmap_scale = place_tiles_into(tile_vars, varmap_scale, processed, place_size, coord_prefetch, overlap, mult_5x)
            tile_vars = [np.squeeze(tile) for tile in tile_vars]
            tile_vars = [
                cv2.resize(tile, dsize=(place_size, place_size))
                for tile in tile_vars
            ]
            #coord_prefetch = [(int(x * mult_5x), int(y * mult_5x)) for (x,y) in coord_prefetch]
            #
            ## x, y are w.r.t. 20X
            #if overlap < 1 and overlap > 0:
            #    overlap = load_size * overlap
            #ovp = int(overlap * mult_5x)
            inner = [ovp, place_size - ovp]
            in_out = np.zeros((place_size, place_size), dtype=np.bool)
            in_out[inner[0]:inner[1], inner[0]:inner[1]] = True
            for tile, (row, col) in zip(tile_vars, coord_prefetch):
                # try:
                placeholder = varmap_scale[row:row + place_size,
                                           col:col + place_size, :]
                processed_pl = processed_vars[row:row + place_size,
                                              col:col + place_size]
                if (processed_pl).sum() > 0:
                    ## we've already placed some of this tile
                    placeholder[in_out] = tile[in_out]
                    tile_out = tile[in_out == 0]
                    #
                    ## Take a dirty average
                    placeholder[in_out == 0] += tile_out
                    placeholder[in_out == 0] /= 2
                    varmap_scale[row:row + place_size,
                                 col:col + place_size, :] = placeholder
                else:
                    ## We haven't placed any part of this tile; place in the whole thing.
                    varmap_scale[row:row + place_size,
                                 col:col + place_size, :] = tile
                processed_vars[row:row + place_size,
                               col:col + place_size] = True

            placing_delta_t = time.time() - placing_start
            print 'Done placing tiles in {}s'.format(placing_delta_t)
        print 'Failed: {}'.format(failed_count)

        pmap_out.append(pmap_scale)
        varmap_out.append(varmap_scale)

    return pmap_out, varmap_out
Beispiel #8
0
def process_svs(svs, prob_maps, coordinates, settings):
    # Check inputs
    overlap = settings['overlap']
    scales = settings['scales']
    weights = settings['weights']
    netproto = settings['deploy_proto']
    gpumode = settings['gpumode']
    cnnlayer = settings['cnnlayer']
    n_classes = settings['n_classes']
    prefetch = settings['prefetch']
    do_normalize = settings['do_normalize']
    debug_mode = settings['DEBUGGING']
    rotate = settings['rotate']
    caffe_root = settings['caffe_root']

    svs_info = {}
    svs_info = data_utils.pull_svs_stats(svs, svs_info)
    lvl20_index = svs_info['20x_lvl']
    mult_5x = svs_info['20x_to_5x']


    ## Loop over scales
    pmap_out = []
    for coords, scale, weight in zip(
        coordinates, scales, weights):
        pmap_scale = np.copy(prob_maps)  ## use this line to enforce default class
        # pmap_scale = np.zeros_like(prob_maps)
        # for enforcing the border
        h,w = pmap_scale.shape[:2]
        processed = np.zeros((h,w), dtype=np.bool)

        net = init_net(netproto, weight, caffe_root, gpumode=gpumode)

        print 'Processing {}'.format(scale)
        print 'Shuffling coordinates'
        random.shuffle(coords)
        print 'Using {} tile coordinates'.format(len(coords))

        if scale == '20x':
            mult = 1
            place_mult = 0.25**2
        elif scale == '10x':
            mult = 2
            place_mult = 1/8.
        elif scale == '5x':
            mult = 4
            place_mult = 0.25
        #/end if

        ## A subset for speed
        # indices = np.random.choice(range(len(coords)), 250)
        # coords = [coords[index] for index in indices]
        # print 'Subsetted {} coordinates '.format(len(coords))

        failed_count = 0
        load_size = proc_size = settings['proc_size']
        load_size *= mult
        place_size = proc_size * place_mult
        place_size = int(place_size)

        # print 'load_size:', load_size
        # print 'place_size:', place_size

        ## Divide the set into n chunks
        if len(coords) < prefetch:
            print 'Coordinates ({}) < prefetch ({})'.format(
                len(coords), prefetch
            )
            coord_split = [coords]
            n_splits = 1
        else:
            n_splits = len(coords) / prefetch
            coord_split = np.array_split(coords, n_splits)
        #/end if

        print 'n splits: ', n_splits

        for nindx, coord_prefetch in enumerate(coord_split):
            ## tile preloading
            preload_start = time.time()
            # random.shuffle(coord_prefetch)
            tiles = data_utils.preload_tiles(svs, coord_prefetch,
                    size=(load_size, load_size), level=lvl20_index)
            tiles = [cv2.resize(tile, dsize=(proc_size, proc_size)) for tile in tiles]

            if do_normalize:
                tiles = [cnorm.normalize(tile) for tile in tiles]
            #/end if

            print '{} Tiles preloaded in {:3.3f}s'.format(len(tiles), time.time() - preload_start),

            ## Processing here
            # tiles = [cv2.cvtColor(tile, cv2.COLOR_RGB2GRAY) for tile in tiles]
            cnn_start = time.time()
            tiles = [run_net(net, tile, rotate=rotate, layer=cnnlayer) for tile in tiles]
            print 'CNN finished in {:3.3f}s'.format(time.time() - cnn_start),

            # Resize to fit
            placing_start = time.time()
            tiles = [cv2.resize(tile, dsize=(place_size, place_size)) for tile in tiles]
            coord_prefetch = [(int(x * mult_5x), int(y * mult_5x)) for (x,y) in coord_prefetch]

            # if overlap > 0:
            #     ovp = int(overlap * mult_5x)
            #     place_size_crop = place_size - ovp
            #     bbox = [ovp,
            #             place_size_crop,
            #             ovp,
            #             place_size_crop]
            #     # print 'ovp:', ovp
            #     # print 'bbox:', bbox
            #     # place_size_crop -= ovp
            #     # print 'place_size_crop:', place_size_crop
            #
            #     tiles = [tile[bbox[0]:bbox[1], bbox[2]:bbox[3], :] for tile in tiles]
            #     tiles = [cv2.resize(tile, dsize=(place_size, place_size)) for tile in tiles]
            # #/end if

            ## x, y are w.r.t. 20X
            if overlap < 1 and overlap > 0:
                overlap = load_size * overlap
            ovp = int(overlap * mult_5x)
            inner = [ovp, place_size-ovp]
            in_out = np.zeros((place_size, place_size), dtype=np.bool)
            in_out[inner[0]:inner[1], inner[0]:inner[1]] = True
            for tile, (row, col) in zip(tiles, coord_prefetch):
                # try:
                placeholder = pmap_scale[row:row+place_size, col:col+place_size, :]
                processed_pl = processed[row:row+place_size, col:col+place_size]
                if (processed_pl).sum() > 0:
                    ## we've already placed some of this tile
                    placeholder[in_out] = tile[in_out]
                    tile_out = tile[in_out==0]
                    # placeholder_out = placeholder[in_out==0]
                    # border = np.mean([tile_out, placeholder_out])

                    ## Take a dirty average
                    placeholder[in_out==0] += tile_out
                    placeholder[in_out==0] /= 2
                    pmap_scale[row:row+place_size, col:col+place_size, :] = placeholder
                else:
                    ## We haven't placed any part of this tile; place in the whole thing.
                    pmap_scale[row:row+place_size, col:col+place_size, :] = tile
                #/end if
                processed[row:row+place_size, col:col+place_size] = True
                # except:
                #     print 'failed {} <-- {}'.format((row, col), tile.shape)
                #     failed_count += 1
                #/end try
            #/end for tile, (row,col)
            print 'Placing done in {:3.3f}s'.format(time.time() - placing_start)
        #/end for nindx, coord_prefetch

        print 'Failed: {}'.format(failed_count)

        pmap_out.append(pmap_scale)
    #/end for coords, scale, overlap, weight

    return pmap_out