Exemple #1
0
def labeler_train_ibeis_cnn(ibs,
                            species_list=None,
                            species_mapping=None,
                            viewpoint_mapping=None,
                            **kwargs):
    from ibeis_cnn.ingest_ibeis import get_cnn_labeler_training_images
    from ibeis_cnn.process import numpy_processed_directory2
    from ibeis_cnn.models.labeler import train_labeler
    from ibeis_cnn.utils import save_model
    data_path = join(ibs.get_cachedir(), 'extracted')
    extracted_path = get_cnn_labeler_training_images(
        ibs,
        data_path,
        category_list=species_list,
        category_mapping=species_mapping,
        viewpoint_mapping=viewpoint_mapping,
        **kwargs)
    id_file, X_file, y_file = numpy_processed_directory2(extracted_path)
    output_path = join(ibs.get_cachedir(), 'training', 'labeler')
    model_path = train_labeler(output_path, X_file, y_file)
    # Add the species_list to the model
    model_state = ut.load_cPkl(model_path)
    assert 'category_list' not in model_state
    model_state['category_list'] = species_list
    assert 'viewpoint_mapping' not in model_state
    model_state['viewpoint_mapping'] = viewpoint_mapping
    save_model(model_state, model_path)
    return model_path
Exemple #2
0
def background_train(ibs,
                     species,
                     train_gid_set=None,
                     global_limit=500000,
                     **kwargs):
    from ibeis_cnn.ingest_ibeis import get_background_training_patches2
    from ibeis_cnn.process import numpy_processed_directory2
    from ibeis_cnn.models.background import train_background
    from ibeis_cnn.utils import save_model
    data_path = join(ibs.get_cachedir(), 'extracted')
    extracted_path = get_background_training_patches2(
        ibs,
        species,
        data_path,
        patch_size=50,
        train_gid_set=train_gid_set,
        global_limit=global_limit,
        **kwargs)
    id_file, X_file, y_file = numpy_processed_directory2(extracted_path)
    output_path = join(ibs.get_cachedir(), 'training', 'background')
    model_path = train_background(output_path, X_file, y_file)
    model_state = ut.load_cPkl(model_path)
    assert 'species' not in model_state
    model_state['species'] = species
    save_model(model_state, model_path)
    return model_path
Exemple #3
0
def _init_config(ibs):
    r"""
    Loads the database's algorithm configuration

    TODO: per-species config
    """
    #####
    # <GENERAL CONFIG>
    config_fpath = ut.unixjoin(ibs.get_dbdir(), 'general_config.cPkl')
    try:
        general_config = ut.load_cPkl(config_fpath, verbose=ut.VERBOSE)
    except IOError as ex:
        logger.error('*** failed to load general config', exc_info=ex)
        general_config = {}
        ut.save_cPkl(config_fpath, general_config, verbose=ut.VERBOSE)
    current_species = general_config.get('current_species', None)
    logger.info('[_init_config] general_config.current_species = %r' %
                (current_species, ))
    # </GENERAL CONFIG>
    #####
    # species_list = ibs.get_database_species()
    # if current_species is None:
    #     # species_list = ibs.get_database_species()
    #     # species_list[0] if len(species_list) == 1 else None
    #     primary_species = ibs.get_primary_database_species()
    #     current_species = primary_species
    cfgname = 'cfg' if current_species is None else current_species
    if ut.VERBOSE and ut.NOT_QUIET:
        # logger.info('[_init_config] Loading database with species_list = %r ' % (species_list,))
        logger.info('[_init_config] Using cfgname=%r' % (cfgname, ))
    # try to be intelligent about the default speceis
    ibs._load_named_config(cfgname)
Exemple #4
0
def _init_config(ibs):
    r"""
    Loads the database's algorithm configuration

    TODO: per-species config
    """
    #####
    # <GENERAL CONFIG>
    config_fpath = ut.unixjoin(ibs.get_dbdir(), 'general_config.cPkl')
    try:
        general_config = ut.load_cPkl(config_fpath)
    except IOError:
        general_config = {}
    current_species = general_config.get('current_species', None)
    if ut.VERBOSE and ut.NOT_QUIET:
        print('[_init_config] general_config.current_species = %r' % (current_species,))
    # </GENERAL CONFIG>
    #####
    species_list = ibs.get_database_species()
    if current_species is None:
        species_list = ibs.get_database_species()
        current_species = species_list[0] if len(species_list) == 1 else None
    cfgname = 'cfg' if current_species is None else current_species
    if ut.VERBOSE and ut.NOT_QUIET:
        print('[_init_config] Loading database with species_list = %r ' % (species_list,))
        print('[_init_config] Using cfgname=%r' % (cfgname,))
    # try to be intelligent about the default speceis
    ibs._load_named_config(cfgname)
Exemple #5
0
def aoi2_train(ibs,
               species_list=None,
               train_gid_list=None,
               purge=True,
               cache=False):
    from wbia_cnn.ingest_wbia import get_aoi2_training_data
    from wbia_cnn.process import numpy_processed_directory5
    from wbia_cnn.models.aoi2 import train_aoi2
    from wbia_cnn.utils import save_model

    data_path = join(ibs.get_cachedir(), 'extracted')
    extracted_path = get_aoi2_training_data(
        ibs,
        dest_path=data_path,
        target_species_list=species_list,
        train_gid_list=train_gid_list,
        purge=purge,
        cache=cache,
    )
    id_file, X_file, y_file = numpy_processed_directory5(extracted_path)
    output_path = join(ibs.get_cachedir(), 'training', 'aoi2')
    model_path = train_aoi2(output_path, X_file, y_file)
    model_state = ut.load_cPkl(model_path)
    assert 'species_list' not in model_state
    model_state['species_list'] = species_list
    save_model(model_state, model_path)
    return model_path
Exemple #6
0
    def load_old_weights_kw2(model, old_weights_fpath):
        print('[model] loading old model state from: %s' %
              (old_weights_fpath, ))

        oldkw = ut.load_cPkl(old_weights_fpath, n=None)
        #output_dims = model.best_results['weights'][-1][0]

        # Model architecture and weight params
        if model.output_dims is None:
            #model.output_dims = output_dims
            #ut.depth_profile(oldkw['best_weights'])
            model.output_dims = oldkw['best_weights'][-1].shape[0]

        # Set class attributes
        model.data_params = {
            'center_mean': oldkw['data_whiten_mean'],
            'center_std': oldkw['data_whiten_std'],
        }
        model._fix_center_mean_std()
        model.best_results = {
            'epoch': oldkw['best_epoch'],
            'test_accuracy': oldkw['best_valid_accuracy'],
            'learn_loss': oldkw['best_train_loss'],
            'valid_accuracy': oldkw['best_valid_accuracy'],
            'valid_loss': oldkw['best_valid_loss'],
            'weights': oldkw['best_fit_weights']
        }

        # Need to build architecture first
        model.init_arch()
        model.encoder = oldkw.get('data_label_encoder', None)
        model.batch_size = oldkw['train_batch_size']

        # Set architecture weights
        model.set_all_param_values(model.best_results['weights'])
Exemple #7
0
def preproc_has_tips(depc, aid_list, config=None):
    r"""
    HACK TO FIND ONLY ANNOTS THAT HAVE TIPS

    Args:
        depc (DependencyCache):
        aid_list (list):  list of annotation rowids
        config (dict): (default = {})

    Yields:
        tuple: (np.ndarray, np.ndarray, np.ndarray)

    CommandLine:
        python -m ibeis_flukematch.plugin --exec-preproc_has_tips --db testdb1
        python -m ibeis_flukematch.plugin --exec-preproc_has_tips --dbdir /home/zach/data/IBEIS/humpbacks --no-cnn
        python -m ibeis_flukematch.plugin --exec-preproc_has_tips --dbdir /home/zach/data/IBEIS/humpbacks --no-cnn --clear-all-depcache
        python -m ibeis_flukematch.plugin --exec-preproc_has_tips --db humpbacks --no-cnn
        python -m ibeis_flukematch.plugin --exec-preproc_has_tips --db humpbacks --no-cnn --clear-all-depcache

    Example:
        >>> # DISABLE_DOCTEST
        >>> from ibeis_flukematch.plugin import *  # NOQA
        >>> ibs = ibeis.opendb(defaultdb='humpbacks')
        >>> aid_list = ibs.get_valid_aids()
        >>> config = {}
        >>> propgen = preproc_has_tips(ibs.depc, aid_list, config)
        >>> result = list(propgen)
        >>> hasnotch_list = ut.take_column(result, 0)
        >>> num_with = sum(hasnotch_list)
        >>> valid_aids = ut.compress(aid_list, hasnotch_list)
        >>> ibs.append_annot_case_tags(valid_aids, ['hasnotch'] * len(valid_aids))
        >>> print(ibs.get_annot_info(valid_aids[2], default=True))
        >>> print('%r / %r annots have notches' % (num_with, len(aid_list)))
    """
    print('Preprocess Has_Notch')
    print(config)

    config = config.copy()
    ibs = depc.controller
    fn = join(ibs.get_dbdir(), 'fluke_image_points.pkl')
    if not exists(fn):
        print('[fluke-module] ERROR: Could not find image points file')
        raise NotImplementedError('Could not find image points file')

    # this is a dict of img: dict of left/right/notch to the xy-point
    img_points_map = ut.load_cPkl(fn)

    img_names = ibs.get_annot_image_names(aid_list)

    for imgn in ut.ProgIter(img_names, lbl='Checking Has_Notch'):
        try:
            (
                img_points_map[imgn]['notch'],
                img_points_map[imgn]['left'],
                img_points_map[imgn]['right'],
            )
        except KeyError:
            yield (False, )
        else:
            yield (True, )
Exemple #8
0
def classifier2_train(ibs,
                      species_list=None,
                      species_mapping={},
                      train_gid_set=None,
                      **kwargs):
    from ibeis_cnn.ingest_ibeis import get_cnn_classifier2_training_images
    from ibeis_cnn.process import numpy_processed_directory3
    from ibeis_cnn.models.classifier2 import train_classifier2
    from ibeis_cnn.utils import save_model
    if species_list is not None:
        species_list = sorted(species_list)
    data_path = join(ibs.get_cachedir(), 'extracted')
    values = get_cnn_classifier2_training_images(
        ibs,
        species_list,
        category_mapping=species_mapping,
        train_gid_set=train_gid_set,
        dest_path=data_path,
        **kwargs)
    extracted_path, category_list = values
    id_file, X_file, y_file = numpy_processed_directory3(extracted_path)
    output_path = join(ibs.get_cachedir(), 'training', 'classifier2')
    model_path = train_classifier2(output_path, X_file, y_file, purge=True)
    # Add the species_list to the model
    model_state = ut.load_cPkl(model_path)
    assert 'category_list' not in model_state
    model_state['category_list'] = category_list
    save_model(model_state, model_path)
    # Return model path
    return model_path
Exemple #9
0
def classify_helper(weight_filepath, vector_list, index_list=None,
                    verbose=VERBOSE_SVM):
    if index_list is None:
        index_list = list(range(len(vector_list)))
    # Init score and class holders
    score_dict = { index: [] for index in index_list }
    class_dict = { index: [] for index in index_list }
    # Load models
    model_tup = ut.load_cPkl(weight_filepath, verbose=verbose)
    model, scaler = model_tup
    # Normalize
    vector_list = scaler.transform(vector_list)
    # calculate decisions and predictions
    # score_list = model.decision_function(vector_list)
    score_list = model.predict_proba(vector_list)
    # Take only the positive probability
    score_list = score_list[:, 1]
    class_list = model.predict(vector_list)
    # Zip together results
    zipped = zip(index_list, score_list, class_list)
    for index, score_, class_ in zipped:
        score_dict[index].append(score_)
        class_dict[index].append(class_)
    # Return scores and classes
    return score_dict, class_dict
Exemple #10
0
def save_dataset(dataset_path, train, val, test, load_norm_from=None, norms=None, grey=False):
    if exists(dataset_path):
        print("Overwriting %s y/n" % dataset_path)
        confirm = raw_input().rstrip()
        if confirm != 'y':
            return
    else:
        mkdir(dataset_path)
    # assume train[0] is the dataset
    # figure out normalization constants
    if (load_norm_from is None) and (norms is None):
        mean, std = get_img_norm_consts(train[0], grey=grey)
    elif norms is not None:
        mean, std = norms
    elif load_norm_from is not None:
        try:
            mean, std = ut.load_cPkl(join(load_norm_from, 'meanstd.pkl'))
        except IOError:
            print("Couldn't find mean and std in %s" % load_norm_from)
            mean, std = get_img_norm_consts(train[0], grey=grey)

    tic = time.time()

    ut.save_cPkl(join(dataset_path, 'train.pkl'), train)
    ut.save_cPkl(join(dataset_path, 'val.pkl'), val)
    ut.save_cPkl(join(dataset_path, 'test.pkl'), test)
    ut.save_cPkl(join(dataset_path, 'meanstd.pkl'), (mean, std))

    toc = time.time() - tic
    print("Took %0.2f seconds" % toc)
Exemple #11
0
def _init_config(ibs):
    r"""
    Loads the database's algorithm configuration

    TODO: per-species config
    """
    #####
    # <GENERAL CONFIG>
    config_fpath = ut.unixjoin(ibs.get_dbdir(), 'general_config.cPkl')
    try:
        general_config = ut.load_cPkl(config_fpath, verbose=ut.VERBOSE)
    except IOError as ex:
        if ut.VERBOSE:
            ut.printex(ex, 'failed to genral load config', iswarning=True)
        general_config = {}
    current_species = general_config.get('current_species', None)
    if ut.VERBOSE and ut.NOT_QUIET:
        print('[_init_config] general_config.current_species = %r' %
              (current_species, ))
    # </GENERAL CONFIG>
    #####
    #species_list = ibs.get_database_species()
    if current_species is None:
        #species_list = ibs.get_database_species()
        #species_list[0] if len(species_list) == 1 else None
        primary_species = ibs.get_primary_database_species()
        current_species = primary_species
    cfgname = 'cfg' if current_species is None else current_species
    if ut.VERBOSE and ut.NOT_QUIET:
        #print('[_init_config] Loading database with species_list = %r ' % (species_list,))
        print('[_init_config] Using cfgname=%r' % (cfgname, ))
    # try to be intelligent about the default speceis
    ibs._load_named_config(cfgname)
def preproc_has_tips(depc, aid_list, config=None):
    r"""
    HACK TO FIND ONLY ANNOTS THAT HAVE TIPS

    Args:
        depc (DependencyCache):
        aid_list (list):  list of annotation rowids
        config (dict): (default = {})

    Yields:
        tuple: (np.ndarray, np.ndarray, np.ndarray)

    CommandLine:
        python -m ibeis_flukematch.plugin --exec-preproc_has_tips --db testdb1
        python -m ibeis_flukematch.plugin --exec-preproc_has_tips --dbdir /home/zach/data/IBEIS/humpbacks --no-cnn
        python -m ibeis_flukematch.plugin --exec-preproc_has_tips --dbdir /home/zach/data/IBEIS/humpbacks --no-cnn --clear-all-depcache
        python -m ibeis_flukematch.plugin --exec-preproc_has_tips --db humpbacks --no-cnn
        python -m ibeis_flukematch.plugin --exec-preproc_has_tips --db humpbacks --no-cnn --clear-all-depcache

    Example:
        >>> # DISABLE_DOCTEST
        >>> from ibeis_flukematch.plugin import *  # NOQA
        >>> ibs = ibeis.opendb(defaultdb='humpbacks')
        >>> aid_list = ibs.get_valid_aids()
        >>> config = {}
        >>> propgen = preproc_has_tips(ibs.depc, aid_list, config)
        >>> result = list(propgen)
        >>> hasnotch_list = ut.take_column(result, 0)
        >>> num_with = sum(hasnotch_list)
        >>> valid_aids = ut.compress(aid_list, hasnotch_list)
        >>> ibs.append_annot_case_tags(valid_aids, ['hasnotch'] * len(valid_aids))
        >>> print(ibs.get_annot_info(valid_aids[2], default=True))
        >>> print('%r / %r annots have notches' % (num_with, len(aid_list)))
    """
    print('Preprocess Has_Notch')
    print(config)

    config = config.copy()
    ibs = depc.controller
    fn = join(ibs.get_dbdir(), 'fluke_image_points.pkl')
    if not exists(fn):
        print('[fluke-module] ERROR: Could not find image points file')
        raise NotImplementedError('Could not find image points file')

    # this is a dict of img: dict of left/right/notch to the xy-point
    img_points_map = ut.load_cPkl(fn)

    img_names = ibs.get_annot_image_names(aid_list)

    for imgn in ut.ProgIter(img_names, lbl='Checking Has_Notch'):
        try:
            (img_points_map[imgn]['notch'], img_points_map[imgn]['left'],
             img_points_map[imgn]['right'],)
        except KeyError:
            yield (False,)
        else:
            yield (True,)
Exemple #13
0
def load_dataset(dataset_path, normalize_method='zscore'):
    print("Loading %s" % dataset_path)
    tic = time.time()
    dset = {}
    mean, std = ut.load_cPkl(join(dataset_path, 'meanstd.pkl'))
    dset['mean'] = mean
    dset['std'] = std
    if normalize_method == 'zscore':
        normalize = lambda x: (normalize_patch(x[0], mean=dset['mean'], std=dset['std']),) + (x[1:])
    elif normalize_method == 'meansub':
        normalize = lambda x: (x[0].astype(np.float32) - dset['mean'],) + (x[1:])
    elif normalize_method is None:
        normalize = lambda x: x

    dset['train'] = normalize(ut.load_cPkl(join(dataset_path, 'train.pkl')))
    dset['valid'] = normalize(ut.load_cPkl(join(dataset_path, 'val.pkl')))
    dset['test'] = normalize(ut.load_cPkl(join(dataset_path, 'test.pkl')))
    toc = time.time() - tic
    print("Took %0.2f seconds" % toc)
    return dset
Exemple #14
0
    def view_vocab(fpath):
        # QUANTIZED AND FLOATING POINT STATS
        centroids = ut.load_cPkl(fpath)
        print('viewing vocat fpath=%r' % (fpath,))
        smk_debug.vector_stats(centroids, 'centroids')
        #centroids_float = centroids.astype(np.float64) / 255.0
        centroids_float = centroids.astype(np.float64) / 512.0
        smk_debug.vector_stats(centroids_float, 'centroids_float')

        fig = clustertool.plot_centroids(centroids, centroids, labels='centroids',
                                         fnum=1, prefix='centroid vecs\n', **kwd)
        fig.show()
Exemple #15
0
 def __init__(self, model_key=None, show_network=False):
     from ibeis_cnn._plugin_grabmodels import ensure_model
     self.model_key = model_key
     weights_path = ensure_model(model_key)
     try:
         self.pretrained_weights = ut.load_cPkl(weights_path)
     except Exception:
         raise IOError('The specified model was not found: %r' %
                       (weights_path, ))
     if show_network:
         net_strs.print_pretrained_weights(
             self.pretrained_weights, weights_path)
def setup_kp_network(network_str):
    fn = KP_NETWORK_OPTIONS[network_str]['url']
    file_url = join('https://lev.cs.rpi.edu/public/models/', fn)
    network_params_path = ut.grab_file_url(file_url, appname='ibeis')
    network_params = ut.load_cPkl(network_params_path)
    # network_params also includes normalization constants needed for the dataset, and is assumed to be a dictionary
    # with keys mean, std, and params
    network_exp = KP_NETWORK_OPTIONS[network_str]['exp']()
    ll.set_all_param_values(network_exp, network_params['params'])
    X = T.tensor4()
    network_fn = tfn([X], ll.get_output(network_exp, X, deterministic=True))
    return {'mean': network_params['mean'], 'std': network_params['std'], 'networkfn': network_fn,
            'input_size': KP_NETWORK_OPTIONS[network_str]['size']}
Exemple #17
0
    def view_vocab(fpath):
        # QUANTIZED AND FLOATING POINT STATS
        centroids = ut.load_cPkl(fpath)
        print('viewing vocat fpath=%r' % (fpath, ))
        smk_debug.vector_stats(centroids, 'centroids')
        #centroids_float = centroids.astype(np.float64) / 255.0
        centroids_float = centroids.astype(np.float64) / 512.0
        smk_debug.vector_stats(centroids_float, 'centroids_float')

        fig = clustertool.plot_centroids(centroids,
                                         centroids,
                                         labels='centroids',
                                         fnum=1,
                                         prefix='centroid vecs\n',
                                         **kwd)
        fig.show()
Exemple #18
0
def setup_kp_network(network_str):
    fn = KP_NETWORK_OPTIONS[network_str]['url']
    file_url = join('https://lev.cs.rpi.edu/public/models/', fn)
    network_params_path = ut.grab_file_url(file_url, appname='ibeis')
    network_params = ut.load_cPkl(network_params_path)
    # network_params also includes normalization constants needed for the dataset, and is assumed to be a dictionary
    # with keys mean, std, and params
    network_exp = KP_NETWORK_OPTIONS[network_str]['exp']()
    ll.set_all_param_values(network_exp, network_params['params'])
    X = T.tensor4()
    network_fn = tfn([X], ll.get_output(network_exp, X, deterministic=True))
    return {
        'mean': network_params['mean'],
        'std': network_params['std'],
        'networkfn': network_fn,
        'input_size': KP_NETWORK_OPTIONS[network_str]['size']
    }
Exemple #19
0
def aoi_train(ibs, species_list=None):
    from ibeis_cnn.ingest_ibeis import get_aoi_training_data
    from ibeis_cnn.process import numpy_processed_directory4
    from ibeis_cnn.models.aoi import train_aoi
    from ibeis_cnn.utils import save_model
    data_path = join(ibs.get_cachedir(), 'extracted')
    extracted_path = get_aoi_training_data(ibs,
                                           data_path,
                                           target_species_list=species_list)
    id_file, X_file, y_file = numpy_processed_directory4(extracted_path)
    output_path = join(ibs.get_cachedir(), 'training', 'aoi')
    model_path = train_aoi(output_path, X_file, y_file)
    model_state = ut.load_cPkl(model_path)
    assert 'species_list' not in model_state
    model_state['species_list'] = species_list
    save_model(model_state, model_path)
    return model_path
def setup_te_network(network_str):
    fn = TE_NETWORK_OPTIONS[network_str]['url']
    file_url = join('https://lev.cs.rpi.edu/public/models/', fn)
    network_params_path = ut.grab_file_url(file_url, appname='ibeis')
    network_params = ut.load_cPkl(network_params_path)
    # network_params also includes normalization constants needed for the dataset, and is assumed to be a dictionary
    # with keys mean, std, and params
    network_exp = TE_NETWORK_OPTIONS[network_str]['exp']()
    ll.set_all_param_values(network_exp, network_params['params'])
    X = T.tensor4()
    network_fn = tfn([X], ll.get_output(
        network_exp[-1], X, deterministic=True))
    retdict = {'mean': network_params['mean'], 'std': network_params[
        'std'], 'networkfn': network_fn}
    if any([i in network_str for i in ('upsample', 'jet')]):
        retdict['mod_acc'] = 8
    return retdict
Exemple #21
0
def classifier_binary_train(ibs, species_list, **kwargs):
    from ibeis_cnn.ingest_ibeis import get_cnn_classifier_binary_training_images
    from ibeis_cnn.process import numpy_processed_directory2
    from ibeis_cnn.models.classifier import train_classifier
    from ibeis_cnn.utils import save_model
    data_path = join(ibs.get_cachedir(), 'extracted')
    extracted_path = get_cnn_classifier_binary_training_images(
        ibs, species_list, dest_path=data_path, **kwargs)
    id_file, X_file, y_file = numpy_processed_directory2(extracted_path)
    output_path = join(ibs.get_cachedir(), 'training', 'classifier-binary')
    model_path = train_classifier(output_path, X_file, y_file)
    # Add the species_list to the model
    model_state = ut.load_cPkl(model_path)
    assert 'species_list' not in model_state
    model_state['species_list'] = species_list
    save_model(model_state, model_path)
    # Return model path
    return model_path
Exemple #22
0
def setup_te_network(network_str):
    fn = TE_NETWORK_OPTIONS[network_str]['url']
    file_url = join('https://lev.cs.rpi.edu/public/models/', fn)
    network_params_path = ut.grab_file_url(file_url, appname='ibeis')
    network_params = ut.load_cPkl(network_params_path)
    # network_params also includes normalization constants needed for the dataset, and is assumed to be a dictionary
    # with keys mean, std, and params
    network_exp = TE_NETWORK_OPTIONS[network_str]['exp']()
    ll.set_all_param_values(network_exp, network_params['params'])
    X = T.tensor4()
    network_fn = tfn([X], ll.get_output(network_exp[-1], X,
                                        deterministic=True))
    retdict = {
        'mean': network_params['mean'],
        'std': network_params['std'],
        'networkfn': network_fn
    }
    if any([i in network_str for i in ('upsample', 'jet')]):
        retdict['mod_acc'] = 8
    return retdict
Exemple #23
0
def background_train(ibs,
                     species,
                     train_gid_set=None,
                     global_limit=500000,
                     **kwargs):
    """
    Example:
    >>> values = output_path, X_file, y_file
    >>> print(values)
    >>> output_path, X_file, y_file = values
    >>> from ibeis_cnn.models.background import train_background
    >>> values = (
    >>>     '/data/ibeis/IMS_Master/_ibsdb/_ibeis_cache/training/background',
    >>>     '/data/ibeis/IMS_Master/_ibsdb/_ibeis_cache/extracted/background/raw/X.npy',
    >>>     '/data/ibeis/IMS_Master/_ibsdb/_ibeis_cache/extracted/background/labels/y.npy'
    >>> )
    >>> output_path, X_file, y_file = values
    """
    from wbia_cnn.ingest_wbia import get_background_training_patches2
    from wbia_cnn.process import numpy_processed_directory2
    from wbia_cnn.models.background import train_background
    from wbia_cnn.utils import save_model

    data_path = join(ibs.get_cachedir(), 'extracted')
    extracted_path = get_background_training_patches2(
        ibs,
        species,
        data_path,
        patch_size=50,
        train_gid_set=train_gid_set,
        global_limit=global_limit,
        **kwargs,
    )
    id_file, X_file, y_file = numpy_processed_directory2(extracted_path)
    output_path = join(ibs.get_cachedir(), 'training', 'background')
    model_path = train_background(output_path, X_file, y_file)
    model_state = ut.load_cPkl(model_path)
    assert 'species' not in model_state
    model_state['species'] = species
    save_model(model_state, model_path)
    return model_path
Exemple #24
0
    def load_old_weights_kw(model, old_weights_fpath):
        print('[model] loading old model state from: %s' %
              (old_weights_fpath, ))
        oldkw = ut.load_cPkl(old_weights_fpath)
        # Model architecture and weight params
        data_shape = oldkw['model_shape'][1:]
        input_shape = (None, data_shape[2], data_shape[0], data_shape[1])
        output_dims = oldkw['output_dims']

        if model.output_dims is None:
            model.output_dims = output_dims

        # Perform checks
        assert input_shape[1:] == model.input_shape[1:], (
            'architecture disagreement')
        assert output_dims == model.output_dims, ('architecture disagreement')

        model.data_params = {
            'center_mean': oldkw['center_mean'],
            'center_std': oldkw['center_std'],
        }
        model._fix_center_mean_std()
        # Set class attributes
        model.best_results = {
            'epoch': oldkw['best_epoch'],
            'test_accuracy': oldkw['best_test_accuracy'],
            'learn_loss': oldkw['best_learn_loss'],
            'valid_accuracy': oldkw['best_valid_accuracy'],
            'valid_loss': oldkw['best_valid_loss'],
            'weights': oldkw['best_weights'],
        }

        # Need to build architecture first
        model.init_arch()

        model.encoder = oldkw.get('encoder', None)

        # Set architecture weights
        weights_list = model.best_results['weights']
        model.set_all_param_values(weights_list)
Exemple #25
0
def build_vgg16_class():
    net = {}
    net['input'] = ll.InputLayer((None, 3, 224, 224), name='inp')
    net['conv1_1'] = ll.Conv2DLayer(net['input'], 64, 3, pad='same', name='conv1')
    net['drop1'] = ll.DropoutLayer(net['conv1_1'], p=0.5)
    net['conv1_2'] = ll.Conv2DLayer(net['drop1'], 64, 3, pad='same', name='conv2')
    net['pool1'] = ll.Pool2DLayer(net['conv1_2'], 2)
    net['conv2_1'] = ll.Conv2DLayer(net['pool1'], 128, 3, pad='same')
    net['drop2'] = ll.DropoutLayer(net['conv2_1'], p=0.5)
    net['conv2_2'] = ll.Conv2DLayer(net['drop2'], 128, 3, pad='same')
    net['pool2'] = ll.Pool2DLayer(net['conv2_2'], 2)
    net['conv3_1'] = ll.Conv2DLayer(net['pool2'], 256, 3, pad='same')
    net['drop3'] = ll.DropoutLayer(net['conv3_1'], p=0.5)
    net['conv3_2'] = ll.Conv2DLayer(net['drop3'], 256, 3, pad='same')
    net['conv3_3'] = ll.Conv2DLayer(net['conv3_2'], 256, 3, pad='same')
    net['drop4'] = ll.DropoutLayer(net['conv3_3'], p=0.5)
    net['pool3'] = ll.Pool2DLayer(net['drop4'], 2)
    net['conv4_1'] = ll.Conv2DLayer(net['pool3'], 512, 3, pad='same')
    net['conv4_2'] = ll.Conv2DLayer(net['conv4_1'], 512, 3, pad='same')
    net['drop5'] = ll.DropoutLayer(net['conv4_2'], p=0.5)
    net['conv4_3'] = ll.Conv2DLayer(net['drop5'], 512, 3, pad='same')
    net['pool4'] = ll.Pool2DLayer(net['conv4_3'], 2)
    net['conv5_1'] = ll.Conv2DLayer(net['pool4'], 512, 3, pad='same')
    net['conv5_2'] = ll.Conv2DLayer(net['conv5_1'], 512, 3, pad='same')
    net['conv5_3'] = ll.Conv2DLayer(net['conv5_2'], 512, 3, pad='same')
    net['pool5'] = ll.Pool2DLayer(net['conv5_3'], 2)
    net['fc6'] = ll.DenseLayer(net['pool5'], num_units=4096)
    net['fc7'] = ll.DenseLayer(net['fc6'], num_units=4096)
    net['fc8'] = ll.DenseLayer(net['fc7'], num_units=1000, nonlinearity=None)
    net['prob'] = ll.NonlinearityLayer(net['fc8'], nonlinearity=softmax)

    # load parameters
    param_file = join(dataset_loc, "vgg16.pkl")
    params = ut.load_cPkl(param_file)

    ll.set_all_param_values(net['fc8'], params['param values'])
    return net
Exemple #26
0
def preproc_notch_tips(depc, cid_list, config=None):
    r"""
    Args:
        depc (DependencyCache):
        aid_list (list):  list of annotation rowids
        config (dict): (default = {})

    Yields:
        tuple: (np.ndarray, np.ndarray, np.ndarray)

    CommandLine:
        python -m ibeis_flukematch.plugin --exec-preproc_notch_tips
        python -m ibeis_flukematch.plugin --exec-preproc_notch_tips --db humpbacks --no-cnn --show
        python -m ibeis_flukematch.plugin --exec-preproc_notch_tips --db humpbacks --show --manual_extract=False
        python -m ibeis_flukematch.plugin --exec-preproc_notch_tips --db humpbacks --show --manual_extract=True
        python -m ibeis_flukematch.plugin --exec-preproc_notch_tips --db humpbacks --no-cnn --clear-all-depcache

    Example:
        >>> # DISABLE_DOCTEST
        >>> from ibeis_flukematch.plugin import *  # NOQA
        >>> ibs = ibeis.opendb(defaultdb='humpbacks')
        >>> all_aids = ibs.get_valid_aids()
        >>> isvalid = ibs.depc.get('Has_Notch', all_aids, 'flag')
        >>> aid_list = ut.compress(all_aids, isvalid)
        >>> aid_list = aid_list[0:10]
        >>> #config = dict(dim_size=None)
        >>> config = NotchTipConfig.from_argv_dict()
        >>> depc = ibs.depc
        >>> config['dim_size'] = 480
        >>> cid_list = depc.get_rowids('chips', aid_list, config)
        >>> notch_tips = list(preproc_notch_tips(depc, cid_list, config))
        >>> result = ut.depth_profile(notch_tips)
        >>> print('depth_profile(notch_tips) = %r' % (result,))
        >>> ut.quit_if_noshow()
        >>> chip_list1 = depc.get_native_property('chips', cid_list, 'img')
        >>> chip_list2 = depc.get('chips', aid_list, 'img', config=config)
        >>> assert np.all(chip_list2[0] == chip_list1[0])
        >>> chip_list = chip_list2
        >>> import plottool as pt
        >>> ut.ensure_pylab_qt4()
        >>> overlay_chips = [overlay_fluke_feats(chip, tips=tips) for chip,  tips in zip(chip_list, notch_tips)]
        >>> iteract_obj = pt.interact_multi_image.MultiImageInteraction(overlay_chips, nPerPage=4, autostart=True)
        >>> ut.show_if_requested()
    """
    print('Preprocess Notch_Tips')
    print(config)

    config = config.copy()

    ibs = depc.controller

    aid_list = depc.get_root_rowids(const.CHIP_TABLE, cid_list)
    img_names = ibs.get_annot_image_names(aid_list)

    M_list = ibs.depc.get_native_property(const.CHIP_TABLE, cid_list, 'M')
    size_list = ibs.depc.get_native_property(const.CHIP_TABLE, cid_list,
                                             ('width', 'height'))

    if config['manual_extract']:
        # TODO: Implement manual annotation options
        # HACK: Read in a file that associates image names w/these annotations, and
        #   try to associate these w/the image names
        # HACK: hardcode this filename relative to the IBEIS directory

        # this is a dict of img: dict of left/right/notch to the corresponding
        # point
        fn = join(ibs.get_dbdir(), 'fluke_image_points.pkl')
        img_points_map = ut.load_cPkl(fn)
    else:
        network_data = setup_kp_network(config['kp_net'])
        # process all the points at once
        # TODO: Is this the best way to do this? Or should we do it in the main
        # loop? Another preproc node?
        img_paths = depc.get_native_property(const.CHIP_TABLE,
                                             cid_list,
                                             'img',
                                             read_extern=False)
        # assume infer_kp handles the bounds checking / snapping
        # TODO: Add config for batch size and image size
        networkfn = network_data['networkfn']
        mean = network_data['mean']
        std = network_data['std']
        pt_preds = infer_kp(img_paths,
                            networkfn,
                            mean,
                            std,
                            input_size=network_data['input_size'])
        img_points_map = {
            img_name: pt_pred
            for img_name, pt_pred in zip(img_names, pt_preds)
        }

    def inbounds(size, point):
        return (point[0] >= 0 and point[0] < size[0]) and (point[1] >= 0 and
                                                           point[1] < size[1])

    for aid, imgn, M, size in ut.ProgIter(zip(aid_list, img_names, M_list,
                                              size_list),
                                          lbl='Reading Notch_Tips'):
        try:
            # Need to scale notch tips as they are
            # specified relative to the image, not the chip.
            ptdict = img_points_map[imgn]
            notch, left, right = ut.dict_take(ptdict,
                                              ['notch', 'left', 'right'])

            if config['manual_extract']:
                notch_ = bound_point(M[0:2].T.dot(notch)[0:2], size)
                left_ = bound_point(M[0:2].T.dot(left)[0:2], size)
                right_ = bound_point(M[0:2].T.dot(right)[0:2], size)
            else:
                notch_ = notch
                left_ = left
                right_ = right

            # verify that the notch / left / right are within the bounds specified by size
            assert (inbounds(size, notch_) and inbounds(size, left_)
                    and inbounds(size, right_))

            yield (notch_, left_, right_)
        except KeyError:
            print(
                '[fluke-module] ERROR: aid=%r does not have points associated'
                % (aid, ))
            # yield None
            raise NotImplementedError(
                'ERROR: aid=%r does not have points associated' % (aid, ))
        except AssertionError:
            print(
                '[fluke-module] ERROR: aid=%r has associated points that are out of bounds'
                % (aid, ))
            print(
                '[fluke-module] ERROR: Points: Notch: %s, Left: %s, Right: %s -- Chip Size: %s'
                % (notch_, left_, right_, size))
            raise NotImplementedError(
                'ERROR: aid=%r has associated points that are out of bounds' %
                (aid, ))
Exemple #27
0
 def load_from_fpath(cls, fpath, verbose=ut.VERBOSE):
     state_dict = ut.load_cPkl(fpath, verbose=verbose)
     self = cls()
     self.__setstate__(state_dict)
     return self
Exemple #28
0
        print("Invalid loss type: %s" % options.loss_type)
        sys.exit(1)
    print("Loading dataset")
    dset = load_dataset(join(dataset_loc, "Flukes/patches/%s" % dset_name), normalize_method='zscore')
    print("Figuring out triplet candidates and pairs")
    tic = time.time()
    dset = {section:preproc_dataset(dset[section]) for section in ['train', 'valid', 'test']}
    # load_dataset normalizes
    toc = time.time() - tic
    print("Took %0.2f seconds" % toc)
    epoch_losses = []
    batch_losses = []
    embedder = build_embedder()
    model_path = join(dataset_loc, "Flukes/patches/%s/model%s.pkl" % (dset_name, options.loss_type))
    if options.resume and exists(model_path):
        params = ut.load_cPkl(model_path)
        ll.set_all_param_values(embedder, params)

    print("Compiling network for embedding")
    simple_embedder_fn = create_network_fn(embedder[-1])
    #iter_funcs = loss_iter(embedder, update_params={'learning_rate':.01})
    lr = theano.shared(np.array(0.010, dtype=np.float32))
    momentum_params = {'l_r':lr, 'momentum':0.9}

    iter_funcs = FUNCTIONS[options.loss_type]['loss_iter'](embedder, update_params=momentum_params)
    best_params = ll.get_all_param_values(embedder)
    best_val_loss = np.inf
    layer_names = [p.name for p in ll.get_all_params(embedder, trainable=True)]
    save_model = True
    active_triplets = {'train':None, 'valid':None}
    try:
def preproc_notch_tips(depc, cid_list, config=None):
    r"""
    Args:
        depc (DependencyCache):
        aid_list (list):  list of annotation rowids
        config (dict): (default = {})

    Yields:
        tuple: (np.ndarray, np.ndarray, np.ndarray)

    CommandLine:
        python -m ibeis_flukematch.plugin --exec-preproc_notch_tips
        python -m ibeis_flukematch.plugin --exec-preproc_notch_tips --db humpbacks --no-cnn --show
        python -m ibeis_flukematch.plugin --exec-preproc_notch_tips --db humpbacks --show --manual_extract=False
        python -m ibeis_flukematch.plugin --exec-preproc_notch_tips --db humpbacks --show --manual_extract=True
        python -m ibeis_flukematch.plugin --exec-preproc_notch_tips --db humpbacks --no-cnn --clear-all-depcache

    Example:
        >>> # DISABLE_DOCTEST
        >>> from ibeis_flukematch.plugin import *  # NOQA
        >>> ibs = ibeis.opendb(defaultdb='humpbacks')
        >>> all_aids = ibs.get_valid_aids()
        >>> isvalid = ibs.depc.get('Has_Notch', all_aids, 'flag')
        >>> aid_list = ut.compress(all_aids, isvalid)
        >>> aid_list = aid_list[0:10]
        >>> #config = dict(dim_size=None)
        >>> config = NotchTipConfig.from_argv_dict()
        >>> depc = ibs.depc
        >>> config['dim_size'] = 480
        >>> cid_list = depc.get_rowids('chips', aid_list, config)
        >>> notch_tips = list(preproc_notch_tips(depc, cid_list, config))
        >>> result = ut.depth_profile(notch_tips)
        >>> print('depth_profile(notch_tips) = %r' % (result,))
        >>> ut.quit_if_noshow()
        >>> chip_list1 = depc.get_native_property('chips', cid_list, 'img')
        >>> chip_list2 = depc.get('chips', aid_list, 'img', config=config)
        >>> assert np.all(chip_list2[0] == chip_list1[0])
        >>> chip_list = chip_list2
        >>> import plottool as pt
        >>> ut.ensure_pylab_qt4()
        >>> overlay_chips = [overlay_fluke_feats(chip, tips=tips) for chip,  tips in zip(chip_list, notch_tips)]
        >>> iteract_obj = pt.interact_multi_image.MultiImageInteraction(overlay_chips, nPerPage=4, autostart=True)
        >>> ut.show_if_requested()
    """
    print('Preprocess Notch_Tips')
    print(config)

    config = config.copy()

    ibs = depc.controller

    aid_list = depc.get_root_rowids(const.CHIP_TABLE, cid_list)
    img_names = ibs.get_annot_image_names(aid_list)

    M_list = ibs.depc.get_native_property(const.CHIP_TABLE, cid_list, 'M')
    size_list = ibs.depc.get_native_property(const.CHIP_TABLE, cid_list, ('width', 'height'))

    if config['manual_extract']:
        # TODO: Implement manual annotation options
        # HACK: Read in a file that associates image names w/these annotations, and
        #   try to associate these w/the image names
        # HACK: hardcode this filename relative to the IBEIS directory

        # this is a dict of img: dict of left/right/notch to the corresponding
        # point
        fn = join(ibs.get_dbdir(), 'fluke_image_points.pkl')
        img_points_map = ut.load_cPkl(fn)
    else:
        network_data = setup_kp_network(config['kp_net'])
        # process all the points at once
        # TODO: Is this the best way to do this? Or should we do it in the main
        # loop? Another preproc node?
        img_paths = depc.get_native_property(const.CHIP_TABLE, cid_list, 'img',
                                             read_extern=False)
        # assume infer_kp handles the bounds checking / snapping
        # TODO: Add config for batch size and image size
        networkfn = network_data['networkfn']
        mean = network_data['mean']
        std = network_data['std']
        pt_preds = infer_kp(img_paths, networkfn, mean, std, input_size=network_data['input_size'])
        img_points_map = {img_name: pt_pred for img_name, pt_pred in zip(img_names, pt_preds)}

    def inbounds(size, point):
        return (point[0] >= 0 and point[0] < size[0]) and (point[1] >= 0 and point[1] < size[1])

    for aid, imgn, M, size in ut.ProgIter(zip(aid_list, img_names, M_list, size_list),
                                          lbl='Reading Notch_Tips'):
        try:
            # Need to scale notch tips as they are
            # specified relative to the image, not the chip.
            ptdict = img_points_map[imgn]
            notch, left, right = ut.dict_take(ptdict, ['notch', 'left', 'right'])

            if config['manual_extract']:
                notch_ = bound_point(M[0:2].T.dot(notch)[0:2], size)
                left_  = bound_point(M[0:2].T.dot(left)[0:2], size)
                right_ = bound_point(M[0:2].T.dot(right)[0:2], size)
            else:
                notch_ = notch
                left_  = left
                right_ = right

            # verify that the notch / left / right are within the bounds specified by size
            assert(inbounds(size, notch_) and inbounds(size, left_) and inbounds(size, right_))

            yield (notch_, left_, right_)
        except KeyError:
            print(
                '[fluke-module] ERROR: aid=%r does not have points associated' % (aid,))
            # yield None
            raise NotImplementedError(
                'ERROR: aid=%r does not have points associated' % (aid,))
        except AssertionError:
            print(
                '[fluke-module] ERROR: aid=%r has associated points that are out of bounds' % (aid,))
            print(
                '[fluke-module] ERROR: Points: Notch: %s, Left: %s, Right: %s -- Chip Size: %s' % (notch_, left_, right_, size))
            raise NotImplementedError(
                'ERROR: aid=%r has associated points that are out of bounds' % (aid,))
Exemple #30
0
 def load_from_fpath(cls, fpath, verbose=ut.VERBOSE):
     state_dict = ut.load_cPkl(fpath, verbose=verbose)
     self = cls()
     self.__setstate__(state_dict)
     return self