def model_vote_help(train_map, q_labels, majority_vote, new_train): new_model = ObjectClassifier(NZ = False) indcs = np.where(q_labels>-1) new_model.fit(train_map, (q_labels == majority_vote), indcs) probs = new_model.predict_proba_segs(train_map, new_train) probs[np.where(q_labels[new_train]==-2)] = 0 return probs
def rf_uncertainty(self): """ Selcts [self.batch_size] new segments to label based on their distance from [self.thresh] First trains classifier on all labeled data, then predicts probability of all other segments being damage and chooses segments closest to [self.thresh] Returns ------- ndarray All indices of unlabeled data sorted in decreasing uncertainty """ model = ObjectClassifier(NZ = 0, verbose = 0) #train and predict segs of classifier training_sample = model.sample(self.training_labels, EVEN = 2) model.fit(self.train_map, self.training_labels, training_sample) proba_segs = model.predict_proba_segs(self.train_map) #If show, save figures of heatmap from prediction if self.show: self.show_selected() fig = plt.figure() n_labeled = np.where(self.training_labels != -1)[0].shape[0] img = self.train_map.seg_convert(self.seg, proba_segs) plt.imshow(img, cmap = 'seismic', norm = plt.Normalize(0,1)) fig.savefig('{}test_{}{}.png'.format(self.path, n_labeled, self.postfix), format='png') plt.close(fig) #choose indices whose predictions minus thresh were closest to zero unknown_indcs = np.where(self.training_labels == -1)[0] uncertainties = 1-np.abs(proba_segs-self.thresh) return self._uncertain_order(uncertainties.ravel(), unknown_indcs)
def model_start(self, train_map, indcs, truth = None): predictions = np.zeros((self.labels.shape[0], train_map.unique_segs(20).shape[0])) agreement = (self.labels == self.majority_vote())[:,train_map.unique_segs(20)] if truth is not None: for i in range(agreement.shape[0]): print agreement[i,indcs].astype('int') - (self.labels[i,train_map.unique_segs(20)[indcs]] == truth).astype('int') agreement[:,indcs] = self.labels[:,train_map.unique_segs(20)[indcs]] == truth print "HERE" for i in range(agreement.shape[0]): print agreement[i,indcs].astype('int') - (self.labels[i,train_map.unique_segs(20)[indcs]] == truth).astype('int') for i in range(self.labels.shape[0]): new_model = ObjectClassifier(NZ = False) new_model.fit(train_map, agreement[i], indcs) probs = new_model.predict_proba_segs(train_map) predictions[i] = probs print predictions best_labelers = np.argmax(predictions, axis = 0) print best_labelers np.save('predictions.npy', predictions) np.save('best.npy',best_labelers) assert(best_labelers.shape[0] == train_map.unique_segs(20).shape[0]) self.model_labels = self.labels[best_labelers,train_map.unique_segs(20)] np.save('vote.npy', self.model_labels)