def get_data(ds, images=[], feature_blocks=[]): X = [] Y = [] X_rlp = [] if type(feature_blocks) is dict: feature_blocks = feature_blocks.keys() logger.info('Preparation of features and labels started...') for i, im in iterate_images(ds, images): meta = filesys.read_export_file(ds, im, 'meta') if meta is not None: if meta.has_key('superpixel grid error'): if meta['superpixel grid error']: continue # read feature blocks X.append(filesys.read_feature_files( ds, im, feature_blocks, ext='normalized')[0]) X_rlp_i = filesys.read_feature_files( ds, im, ['relloc'], ext='normalized')[0] if X_rlp_i is not None: X_rlp.append(X_rlp_i) # load classes and append as array classes = filesys.read_export_file(ds, im, 'classes') Y.append(np.asarray(classes)) logger.info('Preparation of features and labels finished.') return X, Y, X_rlp
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.')
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)
def run_relative_location_mapping(ds, n=100, sigma=2, class_aggregation=None, cfg=None): logger.info('Computing relative location maps started...') # get image list images = filesys.get_image_list(ds) # loop over training samples maplist = [] for i, im in iterate_images(ds, images): if not filesys.is_classified(ds, im): logging.warning( 'Image %s not annotated, skipped' % im) continue annotations = 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] if not len(annotations) == nx * ny: logging.warning( 'Size mismatch for image %s, skipped' % im) continue centroids = filesys.read_feature_files( ds, im, ['pixel'])[0].ix[:, 'centroid'] annotations = cls.utils.aggregate_classes( np.asarray(annotations), class_aggregation) maplist.append(relativelocation.compute_prior(annotations, centroids, (nm, nn), (nx, ny), n=n)) maps = relativelocation.aggregate_maps(maplist) maps = relativelocation.smooth_maps(maps, sigma=sigma) maps = relativelocation.panel_to_dict(maps) filesys.write_export_file(ds, None, 'relative_location_maps', maps) l = {'last relative location prior computation': time.strftime('%d-%b-%Y %H:%M')} filesys.write_log_file(ds, l) logger.info('Computing relative location maps finished.')
def split_data(ds, images, images_train, images_test, X, Y, X_rlp=[]): X_train = [] Y_train = [] X_test = [] Y_test = [] X_train_prior = [] X_test_prior = [] for i, im in enumerate(images): meta = filesys.read_export_file(ds, im, 'meta') shp = meta['superpixel_grid'] if not np.prod(shp) == np.prod(np.asarray(Y[i]).shape) or X[i] is None: continue Xi = np.asarray(X[i]).reshape((shp[0], shp[1], -1)) Yi = np.asarray(Y[i]).reshape(shp) if im in images_train: X_train.append(Xi) Y_train.append(Yi) if im in images_test: X_test.append(Xi) Y_test.append(Yi) if len(X_rlp) > 0: Xi_rlp = np.asarray(X_rlp[i]).reshape((shp[0], shp[1], -1)) if im in images_train: X_train_prior.append(Xi_rlp) if im in images_test: X_test_prior.append(Xi_rlp) return X_train, X_test, Y_train, Y_test, X_train_prior, X_test_prior
def resolve_indices(ds, Y, meta, agg): eqinds = np.empty((len(Y),2)) for i in range(len(Y)): for j in range(len(meta['images_test'])): Y = filesys.read_export_file(ds, meta['images_test'][j],'classes') if Y: metim = filesys.read_export_file(ds, meta['images_test'][j],'meta') Ya = np.array(Y) if np.prod(metim['superpixel_grid']) == len(Ya): Yr = Ya.reshape((metim['superpixel_grid'])) Ya = utils.aggregate_classes(Yr,agg) if np.prod(Yr.shape) == np.prod(Ytest[i].shape): if np.all(Yr == Ytest[i]): eqinds[i,:] = np.array([i,j]) break return eqinds
def run_feature_normalization(ds, images=[], feature_blocks=[], model_dataset=None, feature_stats=None, overwrite=False, cfg=None): logger.info('Normalizing features started...') if not overwrite: feature_stats = filesys.read_log_file(ds, 'stats') if feature_stats is None: logger.info('Aggregate feature statistics') if model_dataset is not None and model_dataset != ds: images_model = filesys.get_image_list(model_dataset) else: images_model = images allstats = [] for im in images_model: meta = filesys.read_export_file(ds, im, 'meta') if meta.has_key('stats'): stats = meta['stats'] allstats.append(stats) feature_stats = \ cls.features.normalize.aggregate_feature_stats(allstats) l = {'stats': feature_stats, 'last stats computation': time.strftime('%d-%b-%Y %H:%M')} filesys.write_log_file(ds, l) # create feature block list feature_blocks = create_feature_list(feature_blocks) for i, im in iterate_images(ds, images, overwrite, ['features.normalized.%s' % re.sub('^extract_blocks_', '', k) for k in feature_blocks.keys()]): feature_stats = filesys.read_log_file(ds, keys='stats') features, features_in_block = filesys.read_feature_files( ds, im, feature_blocks.keys() + ['relloc'], ext='linear') if features is not None: features = cls.features.normalize.normalize_features( features, feature_stats) filesys.write_feature_files( ds, im, features, features_in_block, ext='normalized') meta = {'last normalized': time.strftime('%d-%b-%Y %H:%M')} filesys.write_export_file(ds, im, 'meta', meta, append=True) logger.info('Normalizing features finished.')
def run_channel_normalization(ds, images=[], model_dataset=None, methods=['gabor', 'gaussian', 'sobel'], methods_params=None, overwrite=False, cfg=None): logger.info('Channel normalization started...') stats = filesys.read_log_file( model_dataset if model_dataset is not None else ds, 'channelstats') if not stats: logger.info( 'Using theoretical channel boundaries for normalization.') stats = channels.get_channel_bounds(methods=methods, methods_params=methods_params) for i, im in iterate_images(ds, images, overwrite, 'channels.normalized'): if filesys.check_export_file(ds, im, 'channels'): img = filesys.read_export_file(ds, im, 'channels') for j in range(4, img.shape[-1]): img[...,j] = channels.normalize_channel(img[...,j], stats[i-4]) filesys.write_export_file(ds, im, 'channels.normalized', img) logger.info('Channel normalization finished.')
def initialize_models(ds, images='all', feature_blocks='all', model_type='LR', class_aggregation=None, partitions='all', C=1.0): # create image list images = create_image_list(ds, images) # create feature block list feature_blocks = create_feature_list(feature_blocks) # retreive train test partitions dslog = filesys.read_log_file( ds, keys=['training images', 'testing images']) if not dslog: msg = 'Train and test partitions not found' logger.error(msg) raise ValueError(msg) images_train = dslog['training images'] images_test = dslog['testing images'] # get data X, Y, X_rlp = get_data(ds, images, feature_blocks=feature_blocks) # aggregate classes if class_aggregation is not None: logger.info('Aggregate classes...') Y = cls.utils.aggregate_classes(Y, class_aggregation) # create category list classes = cls.utils.get_classes(Y) # read relative location data if filesys.check_export_file(ds, None, 'relative_location_maps'): rlp_maps = filesys.read_export_file( ds, None, 'relative_location_maps') rlp_stats = filesys.read_log_file(ds, keys='stats') else: rlp_maps = None rlp_stats = None # number of features n_features = len(X[0].columns) # create partitions list partitions = create_partitions_list(partitions, len(images_train)) # construct models if not type(model_type) is list: model_type = [model_type] models = [cls.models.get_model(model_type=m, n_states=len(classes), n_features=n_features, rlp_maps=rlp_maps, rlp_stats=rlp_stats, C=C) for m in model_type] # construct data arrays from dataframes and partitions annotated = [] for im in images: if filesys.is_classified(ds, im): annotated.append(im) train_sets, test_sets, prior_sets = \ features_to_input(ds, annotated, images_train, images_test, X, Y, X_rlp, partitions=partitions) # collect meta information meta = [[{'dataset': ds, 'images': list(annotated), 'images_train': list(images_train[i]), 'images_test':list(images_test[i]), 'feature_blocks':[re.sub('^extract_blocks_', '', x) for x in feature_blocks.keys()], 'features':list(X[0].columns), 'classes':list(classes), 'model_type':m} for i in partitions] for m in model_type] return models, meta, train_sets, test_sets, prior_sets
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.')