def compare(k): N_INDCS = -1 map_test, map_train = map_overlay.basic_setup([100, 400], 50, label_name = "Jared") clf = ObjectClassifier() damage_labels = np.loadtxt('damagelabels50/Jared-3-3.csv', delimiter = ',').astype('int') building_labels = np.loadtxt('damagelabels50/all_rooftops-3-3.csv', delimiter =',').astype('int') segs = map_train.segmentations[50].ravel() n_segs = int(np.max(segs))+1 all_labels = np.zeros(n_segs) #Nothing = Green all_labels[damage_labels] = 1 #Damage = Red all_labels[building_labels] = 2 #Building = Blue all_data, y = clf._get_X_y(map_train, 'Jared') clf.fit(map_train, label_name = 'Jared') #<-this one changesthe training for the importance order names = clf.feat_names print names importances = clf.model.feature_importances_ indices = np.argsort(importances)[::-1] print names[indices][:N_INDCS] plt.figure() std = np.std([tree.feature_importances_ for tree in clf.model.estimators_], axis=0) plt.title("Feature importances") plt.bar(range(names.shape[0]), importances[indices], color="r", yerr=std[indices], align="center") plt.xticks(range(names.shape[0]), names[indices]) plt.xlim([-1, names.shape[0]]) all_histos(all_data[damage_labels], all_data[building_labels], all_data[np.where(all_labels == 0)[0]], names, indices[:N_INDCS]) #plt.show() return names, indices '''
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 look_at_features(names, indices): N_INDCS = -1 map_test, map_train = map_overlay.basic_setup([100, 400], 50, label_name = "Jared") clf = ObjectClassifier() damage_labels = np.loadtxt('damagelabels50/Jared-3-3.csv', delimiter = ',').astype('int') building_labels = np.loadtxt('damagelabels50/all_rooftops-3-3.csv', delimiter =',').astype('int') segs = map_train.segmentations[50] n_segs = int(np.max(segs.ravel()))+1 all_labels = np.zeros(n_segs) #Nothing = Green all_labels[damage_labels] = 1 #Damage = Red all_labels[building_labels] = 2 #Building = Blue all_data, y = clf._get_X_y(map_train, 'Jared') ''' clf.fit(map_train, label_name = 'Jared') #<-this one changesthe training for the importance order names = clf.feat_names print names importances = clf.model.feature_importances_ indices = np.argsort(importances)[::-1]''' pbar = custom_progress() j = names.shape[0] for i in pbar(indices[::-1]): j-=1 fig = plt.figure(names[i]) plt.imshow(all_data[:,i][segs.astype('int')], cmap = 'gray') fig.savefig('/Users/jsfrank/Desktop/features/{}-{}'.format(j, names[i]), format='png') plt.close(fig)
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 transition(): N_INDCS = -1 map_test, map_train = map_overlay.basic_setup([100], 50, label_name = "Jared") clf = ObjectClassifier() damage_labels = np.loadtxt('damagelabels50/Jared-3-3.csv', delimiter = ',').astype('int') building_labels = np.loadtxt('damagelabels50/all_buildings-3-3.csv', delimiter =',').astype('int') segs = map_train.segmentations[50].ravel() n_segs = int(np.max(segs))+1 all_labels = np.zeros(n_segs) #Nothing = Green all_labels[damage_labels] = 1 #Damage = Red all_labels[building_labels] = 2 #Building = Blue all_data, y = clf._get_X_y(map_train, 'Jared') clf.fit(map_train, label_name = 'Jared') names = clf.feat_names for i in range(50): new_damage = np.concatenate((all_data[damage_labels], all_data[building_labels[:i*10]]), axis = 0) print new_damage.shape all_histos(new_damage, all_data[building_labels], all_data[np.where(all_labels == 0)[0]], names, [17], prefix = "{} ".format(i*10)) plt.show()
def test_progress(self): """Evaluates progress of active learning run by looking at results tested on testing map""" model = ObjectClassifier(NZ = 0, verbose = 0) #Pulls all training data thats been labeled and samples evenly between the classes training_sample = model.sample(self.training_labels, EVEN = 2) #Trains on training data and tests on test map model.fit(self.train_map, self.training_labels, training_sample) proba = model.predict_proba(self.test_map) #Uses majority vote as ground truth g_truth = self.labelers.majority_vote()[self.test_map.segmentations[self.seg]] n_labeled = np.where(self.training_labels > -1)[0].shape[0] #If show is true, saves the ROC curve if self.show: fig, AUC = analyze_results.ROC(g_truth.ravel(), proba.ravel(), 'Haiti Test')[:2] fig.savefig('{}ROC_{}{}.png'.format(self.path, n_labeled, self.postfix), format='png') plt.close(fig) #Evaluates progress by finding FPR at self.FNR and adding it to the fprs list FPR, thresh = analyze_results.FPR_from_FNR(g_truth.ravel(), proba.ravel(), TPR = self.TPR) self.fprs.append(FPR) #saves all fprs to document every iteration so results are not loss and progress can be seen mid-run np.save('{}fprs{}.npy'.format(self.path, self.postfix), self.fprs)
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)