def compute_error_vs_iterations(suffix, num_images, dataset): # assemble truth d = Dataset(dataset) truth = d.get_cls_ground_truth().arr truth = np.random.permutation(truth)[:num_images, :] num_classes = truth.shape[1] tt = ut.TicToc() lbp_times = [0] + [10**x for x in range(3)] lbp_times += [1000 + 1000 * x for x in range(10)] lbp_times += [10**x for x in [5]] #lbp_times = [3000] all_scores = np.zeros((num_classes, len(lbp_times), num_classes)) all_times = np.zeros((num_classes, len(lbp_times))) counter = 0 # do inference for itdex in range(len(lbp_times)): fm = FastinfModel(d, 'perfect', num_classes, lbp_time=lbp_times[itdex]) for rowdex in range(comm_rank, truth.shape[0], comm_size): # parallel obs = truth[rowdex, :].astype(int) taken = np.zeros(num_classes).astype(int) for num_obser in range(num_classes): counter += 1 taken[np.argmax(fm.p_c - taken)] = 1 tt.tic() fm.update_with_observations(taken, obs) utime = tt.toc(quiet=True) curr_score = compute_sq_error(obs, fm.p_c) all_scores[num_obser, itdex, :] = np.add(all_scores[num_obser, itdex, :], curr_score) all_times[num_obser, itdex] += utime print '%d is at %d / %d :'%(comm_rank, counter, len(lbp_times)* \ num_classes*num_images/float(comm_size)),curr_score all_scores /= num_images all_times /= num_images safebarrier(comm) all_scores = comm.reduce(all_scores) all_times = comm.reduce(all_times) if comm_rank == 0: #parallel outfile = open('all_scores_' + suffix, 'w') cPickle.dump(all_scores, outfile) outfile.close() outfile = open('all_times_' + suffix, 'w') cPickle.dump(all_times, outfile) outfile.close()
def test(): dataset = Dataset('full_pascal_trainval') fm = FastinfModel(dataset, 'perfect', 20) # NOTE: just took values from a run of the thing prior_correct = [ float(x) for x in "0.050543 0.053053 0.073697 0.038331 0.050954 0.041879 0.16149\ 0.068721 0.10296 0.026837 0.043779 0.087683 0.063447 0.052205\ 0.41049 0.051664 0.014211 0.068361 0.056969 0.05046".split() ] np.testing.assert_almost_equal(fm.p_c, prior_correct, 4) observations = np.zeros(20) taken = np.zeros(20) fm.update_with_observations(taken, observations) np.testing.assert_almost_equal(fm.p_c, prior_correct, 4) observations[5] = 1 taken[5] = 1 fm.update_with_observations(taken, observations) print fm.p_c correct = [ float(x) for x in "0.027355 0.11855 0.027593 0.026851 0.012569 0.98999 0.52232\ 0.017783 0.010806 0.015199 0.0044641 0.02389 0.033602 0.089089\ 0.50297 0.0083272 0.0088274 0.0098522 0.034259 0.0086298".split() ] np.testing.assert_almost_equal(fm.p_c, correct, 4) observations[15] = 0 taken[15] = 1 fm.update_with_observations(taken, observations) correct = [ float(x) for x in "2.73590000e-02 1.19030000e-01 2.75500000e-02 2.68760000e-02 \ 1.23920000e-02 9.90200000e-01 5.25320000e-01 1.76120000e-02 \ 1.05030000e-02 1.52130000e-02 4.26410000e-03 2.38250000e-02 \ 3.36870000e-02 8.96450000e-02 5.04300000e-01 8.71880000e-05 \ 8.82630000e-03 9.55290000e-03 3.43240000e-02 8.44510000e-03".split() ] np.testing.assert_almost_equal(fm.p_c, correct) # reinit_marginals fm.reset() np.testing.assert_equal(fm.p_c, prior_correct) print(fm.cache)
def __init__(self,dataset,actions,mode='fixed_order',bounds=None,model=None,fastinf_model_name='perfect'): assert(mode in self.accepted_modes) self.mode = mode self.dataset = dataset self.actions = actions self.bounds = bounds self.fastinf_model_name = fastinf_model_name # Is GIST in the actions? Need to behave differently if so. self.gist_mode = ('gist' in [action.name for action in self.actions]) self.num_obs_vars = len(self.actions) if self.gist_mode: assert(self.actions[0].name == 'gist') self.num_obs_vars = len(self.actions)-1+len(self.dataset.classes) if mode == 'random': if model: assert(isinstance(model,RandomModel)) self.model = model else: self.model = RandomModel(len(self.dataset.classes)) elif mode=='no_smooth' or mode=='backoff': if model: assert(isinstance(model,NGramModel)) self.model = model else: self.model = NGramModel(dataset,mode) elif mode=='fixed_order': if model: assert(isinstance(model,FixedOrderModel)) self.model = model else: self.model = FixedOrderModel(dataset) elif mode=='fastinf': if model: assert(isinstance(model,FastinfModel)) self.model = model else: self.model = FastinfModel(dataset, self.fastinf_model_name, self.num_obs_vars) else: raise RuntimeError("Unknown mode") self.reset() self.orig_p_c = self.get_p_c()