예제 #1
0
def run_channel_extraction(ds, images=[], colorspace='rgb',
                           methods=['gabor', 'gaussian', 'sobel'],
                           methods_params=None, overwrite=False,
                           cfg=None):
    logger.info('Channel extraction started...')

    stats = [{'max': 0., 'min': 255.}
             for i in range(channels.get_number_channels(methods_params=methods_params,
                                                         methods=methods))]

    for i, im in iterate_images(ds, images, overwrite, ['channels']):

        img = filesys.read_image_file(ds, im)

        # img is now [i,j,rgb]:                   grayscale channels   extra_channels
        # img becomes [i,j, channels] channels -> gray      (r g b)    (gabor sigmadiff)
        img = channels.add_channels(
            img, colorspace, methods=methods,
            methods_params=methods_params)
        filesys.write_export_file(ds, im, 'channels', img)

        stats = [{'max': np.max([stats[j]['max'], img[:,:,j+4].max()]),
                  'min': np.min([stats[j]['min'], img[:,:,j+4].min()])}
                 for j in range(img.shape[-1] - 4)]
    
    filesys.write_log_file(ds, {'channelstats': stats})

    logger.info('Channel extraction finished.')
예제 #2
0
def run_segmentation(ds, images=[], method='slic', method_params={},
                     extract_contours=False, remove_disjoint=True,
                     overwrite=False, cfg=None, roi=None):

    logger.info('Segmentation started...')

    for i, im in iterate_images(ds, images, overwrite=overwrite, ext='segments'):

        img = filesys.read_image_file(ds, im)

        if roi is not None:
            cnum = int(re.findall('(?<=\.c)[0-9](?=\.)',im)[0]) # Temporary hack to link camera number to roi...
            roi = filesys.read_roi_file(ds,iroi=cnum-2)

        segments, contours = seg.superpixels.get_segmentation(
            img, method=method, method_params=method_params,
            extract_contours=extract_contours,
            remove_disjoint=remove_disjoint,roi=roi)

        nx, ny = seg.superpixels.get_superpixel_grid(
            segments, img.shape[:2])
        err = not seg.superpixels.check_segmentation(segments, nx, ny)

        meta = {'image_resolution_cropped': img.shape,
                'superpixel_grid': (nx, ny),
                'superpixel_grid_error': err,
                'last_segmented': time.strftime('%d-%b-%Y %H:%M')}

        filesys.write_export_file(ds, im, 'meta', meta, append=True)
        filesys.write_export_file(ds, im, 'segments', segments)
        filesys.write_export_file(ds, im, 'contours', contours)

    logger.info('Segmentation finished.')
예제 #3
0
 def test_add_channels(self):
     """test if we extend the channels"""
     dirname = 'argusnl'
     images = filesys.get_image_list(dirname)
     filename = images[0]
     img = filesys.read_image_file(dirname, filename)
     all_channels = channels.add_channels(img, colorspace='rgb')
     self.assertGreater(all_channels.shape[2], 10)
예제 #4
0
 def test_add_channels(self):
     """test if we extend the channels"""
     dirname = 'argusnl'
     images = filesys.get_image_list(dirname)
     filename = images[0]
     img = filesys.read_image_file(dirname, filename)
     all_channels = channels.add_channels(img, colorspace='rgb')
     self.assertGreater(all_channels.shape[2], 10)
예제 #5
0
def plot_predictions(ds, model, meta, test_sets, part=0, class_aggregation=None):
 
    if model is list:
        model = model[part]
    elif part > 0:
        raise IOError

    classlist = filesys.read_default_categories(ds)
    classlist = cls.utils.aggregate_classes(np.array(classlist), class_aggregation)
    classlist = list(np.unique(classlist))

    eqinds = resolve_indices(ds, test_sets[0][1], meta, class_aggregation)

    n = np.shape(test_sets)[-1]

    fig,axs = plt.subplots(n,3,figsize=(20,10 * round(n / 2)))

    cdict = {
        'red'  :  ((0., .5, .5), (.2, .5, 1.), (.4, 1., .66),
                   (.6, .66, .13), (.8, .13, .02), (1., .02, .02)),
        'green':  ((0., .5, .5), (.2, .5, .95), (.4, .95, 1.),
                   (.6, 1., .69), (.8, .69, .24), (1., .24, .24)),
        'blue' :  ((0., .5, .5), (.2, .5, 0.), (.4, 0., 1.),
                   (.6, 1., .30), (.8, .30, .75), (1., .75, .75))
        }
    cmap_argus = matplotlib.colors.LinearSegmentedColormap('argus_classes', cdict, 5)

    for i, fname in enumerate(meta['images_test'][:-1]):
        if any(eqinds[:,1] == i):
            gind = np.where(eqinds[:,1] == i)[0][0]
            grnd = test_sets[part][1][gind]
            pred = model.predict([test_sets[part][0][gind]])[0]
            score = float(np.sum(pred == grnd)) / np.prod(grnd.shape) * 100
        
            img = filesys.read_image_file(ds,fname)
            axs[i,0].imshow(img)

            plot.plot_prediction(ds,
                                 fname,
                                 grnd,
                                 cm=cmap_argus,
                                 clist=classlist,
                                 axs=axs[i,1])

            plot.plot_prediction(ds,
                                 fname,
                                 pred,
                                 cm=cmap_argus,
                                 clist=classlist,
                                 axs=axs[i,2])
        
            axs[i,0].set_title(fname)
            axs[i,1].set_title('groundtruth')
            axs[i,2].set_title('prediction (%0.1f%%)' % score)

    return fig,axs
예제 #6
0
def run_feature_extraction(ds, images=[], feature_blocks=[],
                           colorspace='rgb', model_dataset=None,
                           overwrite=False, image_slice=1,
                           blocks_params={}, cfg=None):

    logger.info('Feature extraction started...')

    # create feature block list
    feature_blocks = create_feature_list(feature_blocks)

    for i, im in iterate_images(ds, images, overwrite,
                                ['features.%s' % re.sub('^extract_blocks_', '', k)
                                 for k in feature_blocks.keys()]):

        segments = filesys.read_export_file(ds, im, 'segments')

        if segments is None:
            logging.warning(
                'No segmentation found for image: %s' % im)
            continue

        meta = filesys.read_export_file(ds, im, 'meta')
        if meta['superpixel_grid_error']:
            logging.warning(
                'Invalid segmentation found for image: %s' % im)
            continue

        # load image
        img = filesys.read_export_file(ds, im, 'channels.normalized')
        if img is None:
            img = filesys.read_export_file(ds, im, 'channels')
        if img is None:
            img = filesys.read_image_file(ds, im)

        # extract features
        features, features_in_block = \
            cls.features.blocks.extract_blocks(img[::image_slice,::image_slice,:],
                                               segments[::image_slice,::image_slice],
                                               colorspace=colorspace,
                                               blocks=feature_blocks,
                                               blocks_params=blocks_params)

        # remove too large features
        features = cls.features.remove_large_features(features)

        # write features to disk
        filesys.write_feature_files(
            ds, im, features, features_in_block)

        meta = {
            'last feature extraction': time.strftime('%d-%b-%Y %H:%M')}
        filesys.write_export_file(ds, im, 'meta', meta, append=True)

    logger.info('Feature extraction finished.')
예제 #7
0
def run_prediction(ds, images='all', model=None,
                   model_dataset=None, colorspace='rgb',
                   feature_blocks='all', overwrite=False, cfg=None):

    if model_dataset is None:
        model_dataset = ds

    if model is None:
        model = filesys.get_model_list(model_dataset)[-1]
    if type(model) is str:
        logging.info('Using model %s' % model)
        model = filesys.read_model_file(model_dataset, model)[0]
    if not hasattr(model,'predict'):
        raise IOError('Invalid model input type') 

    # create image list
    images = create_image_list(ds, images)

    # create block list
    blocks = create_feature_list(feature_blocks)

    # read feature data
    X = get_data(ds,
                 images,
                 feature_blocks=blocks)[0]

    for i, im in iterate_images(ds, images, overwrite, 'predict'):
        if X[i] is None:
            continue

        shp = filesys.read_export_file(
            ds, im, 'meta')['superpixel_grid']
        X[i] = np.asarray(X[i]).reshape((shp[0], shp[1], -1))

        # run prediction
        try:
            classes = model.predict([X[i]])[0]
        except:
            logger.error('Error predicting %s' % im)
            continue

        # save raw data
        filesys.write_export_file(ds, im, 'predict', classes)

        # save plot
        img = filesys.read_image_file(ds, im)
        seg = filesys.read_export_file(ds, im, 'segments')
        cls = list(set(classes.flatten()))
        for i, c in enumerate(classes.flatten()):
            ix = cls.index(c)
            img[seg==i,ix-1] += .1
        img = np.minimum(1., img) * 255.
        fdir, fname = os.path.split(im)
        cv2.imwrite(os.path.join(fdir, 'predictions', os.path.splitext(fname)[0] + '.classes.png'), img)
예제 #8
0
def run_feature_update(ds, images=[], feature_blocks=[],
                       class_aggregation=None,
                       relative_location_prior=False,
                       overwrite=False, cfg=None):
    logger.info('Updating extracted features started...')

    # create feature block list
    feature_blocks = create_feature_list(feature_blocks)

    if relative_location_prior:
        maps = filesys.read_export_file(
            ds, None, 'relative_location_maps')

    for i, im in iterate_images(ds, images, overwrite,
                                ['features.linear.%s' % re.sub('^extract_blocks_', '', k)
                                 for k in feature_blocks.keys()]):

        # load image and features
        img = filesys.read_image_file(ds, im)
        features, features_in_block = filesys.read_feature_files(
            ds, im, feature_blocks.keys())

        if features is None:
            continue

        # include relative location feature if requested
        if relative_location_prior:
            try:
                logger.info('Add relative location votes')

                Iann = filesys.read_export_file(ds, im, 'classes')
                meta = filesys.read_export_file(ds, im, 'meta')
                nx, ny = meta['superpixel_grid']
                nm, nn = meta['image_resolution_cropped'][:-1]
                Iann = np.reshape(Iann, meta['superpixel_grid'])

                centroids = filesys.read_feature_files(
                    ds, im, ['pixel'])[0].ix[:, 'centroid']
                Iann = cls.utils.aggregate_classes(
                    np.asarray(Iann), class_aggregation)

                votes = relativelocation.vote_image(
                    Iann, maps, centroids, (nm, nn))[0]

                features, features_in_block = \
                    relativelocation.add_features(
                    votes, features, features_in_block)
                filesys.write_feature_files(
                    ds, im, features, features_in_block)
            except:
                logging.warning(
                    'Adding relative location votes failed, using zeros')
                features = relativelocation.remove_features(
                    features, maps.keys())
                features_in_block['relloc'] = [
                    'prob_%s' % c for c in maps.keys()]

            meta = {'last relative location voting':
                    time.strftime('%d-%b-%Y %H:%M')}
            filesys.write_export_file(
                ds, im, 'meta', meta, append=True)

        # make features scale invariant
        logger.info('Make features scale invariant')
        features = cls.features.scaleinvariant.scale_features(
            img, features)
        filesys.write_feature_files(
            ds, im, features, features_in_block, ext='invariant')

        # linearize features
        logger.info('Linearize features')
        features = cls.features.linearize(features)
        features_in_block = cls.features.extend_feature_blocks(
            features, features_in_block)
        filesys.write_feature_files(
            ds, im, features, features_in_block, ext='linear')

        # get feature stats for image
        logger.info('Compute feature statistics')
        imstats = cls.features.normalize.compute_feature_stats(features)

        meta = {'stats': imstats,
                'last stats computation': time.strftime('%d-%b-%Y %H:%M')}
        filesys.write_export_file(ds, im, 'meta', meta, append=True)

    logger.info('Updating extracted features finished.')