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 [136]:
# 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 [137]:
final_pred


Out[137]:
array([[ 0.00942457,  0.00589951,  0.06700011, ...,  0.00769107,
         0.01991315,  0.01610762],
       [ 0.01317672,  0.00365908,  0.03249153, ...,  0.02474018,
         0.0079774 ,  0.00507571],
       [ 0.00068839,  0.05821988,  0.00196748, ...,  0.00401103,
         0.00222921,  0.00206912],
       ..., 
       [ 0.00339755,  0.00816817,  0.00137667, ...,  0.00429999,
         0.0069915 ,  0.00611117],
       [ 0.09395989,  0.03621835,  0.02804596, ...,  0.08307418,
         0.07553701,  0.02040919],
       [ 0.07117556,  0.01101161,  0.05339076, ...,  0.06099452,
         0.07806459,  0.00720237]])

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)