In [108]:
import numpy as np
import matplotlib.pyplot as plt
import time
import tools.my_io as my_io
%matplotlib inline

import caffe

# Set the right path to your model definition file, pretrained model weights,
# and the image you would like to classify.
MODEL_FILE = '/media/raid_arr/data/ndsb/config/deploy_cnn_v3_maxout_supersparse.prototxt'
PRETRAINED = '/media/raid_arr/data/ndsb/models/zoomed_out_vanilla_smallmaxout/simple_fold0_iter_3000.caffemodel'

MEAN_VALUE = 23
IMAGE_FILE = '/afs/ee.cooper.edu/user/t/a/tam8/data/ndsb/train/acantharia_protist/100224.jpg'
VALIDATION_DB = '/media/raid_arr/tmp/test0_norm_lmdb'


---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-108-56676cfc6f3b> in <module>()
      2 import matplotlib.pyplot as plt
      3 import time
----> 4 import tools.my_io as my_io
      5 get_ipython().magic(u'matplotlib inline')
      6 

ImportError: No module named tools.my_io

In [120]:
import numpy as np
# DUMMY DATA
n_obs = 615
# 0 8
# 1 59
# 2 14
# 3 33
# 4 7

fine_preds = {
    0: np.random.uniform(size=(n_obs, 8)),
    1: np.random.uniform(size=(n_obs, 59)),
    2: np.random.uniform(size=(n_obs, 14)),
    3: np.random.uniform(size=(n_obs, 33)),
    4: np.random.uniform(size=(n_obs, 7)),
}
for k, v in fine_preds.items():
    fine_preds[k] = v/np.tile(v.sum(axis=1), (v.shape[1], 1)).T

coarse_pred = np.random.uniform(size=(n_obs, 5))
coarse_pred = coarse_pred/np.tile(coarse_pred.sum(axis=1), 
                                  (coarse_pred.shape[1], 1)).T

In [135]:
# each column scales the corresponding fine prediction
# final_pred = np.zeros((n_obs, 121))

def aggregate_fine_pred(coarse_pred, fine_pred_d):
    """
    fine_pred_d should have keys corresponding to coarse weight index
    """
    pred_list = []
    for col in range(coarse_pred.shape[1]):
        pred_list.append(coarse_pred[:, col][:, None] * fine_preds[col])
    final_pred = np.concatenate(pred_list, axis=1)
    return final_pred
    

final_pred = aggregate_fine_pred(coarse_pred, fine_preds)

In [123]:
import specialism as sp
from le import le
import itertools
# Sort by the column labels
zz = [sp.coarse_to_fine[k] for k in sorted(sp.coarse_to_fine.keys())]
qq = list(itertools.chain(*zz))

In [124]:
le.inverse_transform(qq)


Out[124]:
array(['acantharia_protist', 'acantharia_protist_big_center',
       'acantharia_protist_halo', 'copepod_calanoid_large',
       'copepod_calanoid_octomoms', 'hydromedusae_narco_dark',
       'protist_dark_center', 'trochophore_larvae', 'amphipods',
       'appendicularian_fritillaridae', 'appendicularian_s_shape',
       'appendicularian_slight_curve', 'appendicularian_straight',
       'chaetognath_other', 'chordate_type1', 'copepod_calanoid',
       'copepod_calanoid_eggs', 'copepod_calanoid_eucalanus',
       'copepod_calanoid_flatheads', 'copepod_calanoid_frillyAntennae',
       'copepod_calanoid_large_side_antennatucked',
       'copepod_calanoid_small_longantennae', 'copepod_cyclopoid_copilia',
       'copepod_cyclopoid_oithona', 'copepod_cyclopoid_oithona_eggs',
       'copepod_other', 'crustacean_other', 'decapods', 'detritus_blob',
       'detritus_other', 'echinoderm_larva_pluteus_early',
       'echinoderm_larva_pluteus_typeC', 'echinoderm_larva_pluteus_urchin',
       'echinoderm_larva_seastar_bipinnaria',
       'echinoderm_larva_seastar_brachiolaria', 'echinopluteus', 'ephyra',
       'euphausiids', 'euphausiids_young', 'fecal_pellet',
       'fish_larvae_deep_body', 'fish_larvae_leptocephali',
       'fish_larvae_medium_body', 'fish_larvae_myctophids',
       'fish_larvae_thin_body', 'fish_larvae_very_thin_body',
       'hydromedusae_typeE', 'hydromedusae_typeF',
       'invertebrate_larvae_other_A', 'protist_fuzzy_olive',
       'protist_noctiluca', 'protist_star', 'pteropod_butterfly',
       'pteropod_theco_dev_seq', 'pteropod_triangle', 'shrimp-like_other',
       'shrimp_caridean', 'shrimp_zoea', 'stomatopod',
       'tornaria_acorn_worm_larvae', 'trichodesmium_bowtie',
       'trichodesmium_multiple', 'trichodesmium_puff',
       'trichodesmium_tuft', 'unknown_blobs_and_smudges', 'unknown_sticks',
       'unknown_unclassified', 'ctenophore_cydippid_no_tentacles',
       'ctenophore_cydippid_tentacles', 'hydromedusae_h15',
       'hydromedusae_haliscera_small_sideview', 'hydromedusae_shapeA',
       'hydromedusae_shapeA_sideview_small', 'hydromedusae_shapeB',
       'protist_other', 'siphonophore_calycophoran_rocketship_young',
       'siphonophore_calycophoran_sphaeronectes',
       'siphonophore_calycophoran_sphaeronectes_stem',
       'siphonophore_calycophoran_sphaeronectes_young',
       'siphonophore_other_parts', 'siphonophore_physonect_young',
       'artifacts', 'chaetognath_non_sagitta', 'chaetognath_sagitta',
       'ctenophore_cestid', 'ctenophore_lobate', 'detritus_filamentous',
       'diatom_chain_tube', 'echinoderm_larva_pluteus_brittlestar',
       'echinoderm_seacucumber_auricularia_larva', 'hydromedusae_aglaura',
       'hydromedusae_bell_and_tentacles', 'hydromedusae_haliscera',
       'hydromedusae_liriope', 'hydromedusae_narco_young',
       'hydromedusae_narcomedusae', 'hydromedusae_other',
       'hydromedusae_sideview_big', 'hydromedusae_solmaris',
       'hydromedusae_solmundella', 'hydromedusae_typeD',
       'hydromedusae_typeD_bell_and_tentacles',
       'invertebrate_larvae_other_B', 'jellies_tentacles', 'polychaete',
       'radiolarian_chain', 'radiolarian_colony', 'shrimp_sergestidae',
       'siphonophore_calycophoran_abylidae',
       'siphonophore_calycophoran_rocketship_adult',
       'siphonophore_physonect', 'tunicate_doliolid_nurse',
       'tunicate_salp', 'tunicate_salp_chains', 'artifacts_edge',
       'diatom_chain_string', 'heteropod', 'hydromedusae_partial_dark',
       'siphonophore_partial', 'tunicate_doliolid', 'tunicate_partial'], 
      dtype='|S45')

In [125]:
ordered = []

for col in qq:
    ordered.append(final_pred[:, col])

In [127]:
ordered_pred = np.array(ordered).T

In [129]:
ordered_pred.shape


Out[129]:
(615, 121)