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 comparepx(k): N_INDCS = -1 map_test, map_train = map_overlay.basic_setup([100], 50, label_name = "all_buildings") segs = map_train.segmentations[50].ravel().astype('int') clf = PxClassifier(85,-1) 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') 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_labels = all_labels[segs].ravel() #plt.imshow(all_labels.reshape(map_train.rows, map_train.cols), cmap = 'gray') indcs = np.random.choice(np.arange(map_train.rows*map_train.cols), 1000000, replace = False).astype('int') #print np.where(all_labels == 0)[0][indcs].shape #print np.where(all_labels == 1)[0][indcs].shape #print np.where(all_labels == 2)[0][indcs].shape all_data, names = clf.gen_features(map_train) #print all_data[:,:3].shape, map_train.rows, map_train.cols #plt.imshow(all_data[:,:3].reshape(map_train.rows, map_train.cols, 3)) #plt.show() #print all_data[np.where(all_labels == 1)[0]][indcs], all_data[np.where(all_labels[indcs] == 1)[0]].shape '''clf.fit(map_train) clf.feature_importance() plt.show() names = clf.feat_names importances = clf.model.feature_importances_ indices = np.argsort(importances)[::-1]''' #print names[indices][:N_INDCS] all_histos(all_data[indcs][np.where(all_labels[indcs] == 1)[0]], all_data[indcs][np.where(all_labels[indcs] == 2)[0]], all_data[indcs][np.where(all_labels[indcs] == 0)[0]], names, np.arange(names.shape[0]), prefix = 'px ')
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 main_NZ(): model = ObjectClassifier() map_2, map_3 = map_overlay.basic_setup() labels = np.zeros(map_2.unique_segs(50).shape[0]) labels[np.loadtxt('damagelabels50/Jared-3-2.csv', delimiter = ',').astype('int')] = 1 probs = model.fit_and_predict(map_2, map_3, labels) np.save('object_probs.npy', probs)
def _setup_map(self, random): self.maps = [0,0,0,0] self.tr, self.te = 2,3 self.maps[2:4] = list(map_overlay.basic_setup([100], 50)) if random: self.building_rando = np.loadtxt('damagelabels50/non_damage_random-3-{}.csv'.format(self.tr)).astype('int') else: self.building_rando = np.loadtxt('damagelabels50/all_rooftops_random-3-{}.csv'.format(self.tr)).astype('int') self.real_damage = np.loadtxt('damagelabels50/Jared-3-{}.csv'.format(self.tr), delimiter = ',').astype('int') test_damage = np.loadtxt('damagelabels50/Jared-3-{}.csv'.format(self.te), delimiter = ',').astype('int') test_segs = self.maps[self.te].segmentations[50].ravel().astype('int') segs = self.maps[self.te].segmentations[50] self.damage_ground = indcs2bools(test_damage, test_segs)[segs].ravel()
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()
importances = self.model.feature_importances_ std = np.std([tree.feature_importances_ for tree in self.model.estimators_], axis=0) indices = np.argsort(importances)[::-1] v_print("Feature ranking:", self.verbose) for f in range(len(self.feat_names)): v_print("{}. feature {}: ({})".format(f + 1, self.feat_names[indices[f]], importances[indices[f]]), self.verbose) plt.figure() plt.title("Feature importances") plt.bar(range(len(self.feat_names)), importances[indices], color="r", yerr=std[indices], align="center") plt.xticks(range(len(self.feat_names)), self.feat_names[indices]) plt.xlim([-1, len(self.feat_names)]) if __name__ == "__main__": map_2, map_3 = map_overlay.basic_setup() model = PxClassifier() real_damage = np.loadtxt('damagelabels50/Jared-3-3.csv', delimiter = ',').astype('int') labels = map_3.mask_segments_by_indx(real_damage, 50, False) labels = labels.reshape(map_3.shape[:2]) plt.imshow(labels) plt.show() probs = model.fit_and_predict(map_3, map_2, labels.ravel()) plt.imshow(probs.reshape(map_2.shape[:2])) plt.show()
def visualize_noise(my_map): img_num = my_map.name[-1] for i in range(5): noise_level = 10*i img_name = 'damagelabels50/Sim_{}-3-{}.csv'.format(noise_level, img_num) #plt.figure(img_name) next_label = np.loadtxt(img_name, delimiter = ',') next_img = my_map.mask_segments_by_indx(next_label, 50, with_img = True) show_img(next_img) img_name = 'damagelabels50/Joe-3-{}.csv'.format(img_num) plt.figure(img_name) next_label = np.loadtxt(img_name, delimiter = ',') next_img = my_map.mask_segments_by_indx(next_label, 50, with_img = True) plt.imshow(next_img) img_name = 'damagelabels50/Jared-3-{}.csv'.format(img_num) plt.figure(img_name) next_label = np.loadtxt(img_name, delimiter = ',') next_img = my_map.mask_segments_by_indx(next_label, 50, with_img = True) plt.imshow(next_img) plt.show() if __name__ == '__main__': map_train, map_test = map_overlay.basic_setup([100], 50, label_name = "Jared") #main(map_test) #main(map_train) #main2(map_test) #calc_percents() visualize_noise(map_test)