def colorcube_analysis(): # m = 'densenet121' for m in models: test_data = dt.get_data('cifar10', (50000, 60000)) top_n = 2000 model_name0 = mt.weight_file_name(m, 'cifar10-2-5', 50, False) # model_name0 = mt.weight_file_name(m, 'cifar10-2-5', 50, False, suffix='ft20ep-exp') model = mt.load_by_name(model_name0, test_data[0].shape[1:], h5_path+model_name0) # y_predicted = model.predict(np.array(test_data[0])) y_predicted = t_log.load_predictions(model_name0, file_path=csv_path) true_classes = [int(k) for k in test_data[1]] scores = metrics.prediction_ratings(y_predicted, true_classes) score_sorted_ids = np.argsort(scores) cc_high = metrics_color.ColorDensityCube(resolution=4) for img_id in score_sorted_ids[-top_n:]: cc_high.feed(test_data[0][img_id]) cc_high.normalize() cc_high.plot_cube() cc_low = metrics_color.ColorDensityCube(resolution=4) for img_id in score_sorted_ids[:top_n]: cc_low.feed(test_data[0][img_id]) cc_low.normalize() cc_diff = cc_high.substract(cc_low, 'value') cc_low.plot_cube() cc_diff.normalize() cc_diff.plot_cube(title='Color cube analysis difference (' + str(top_n) + ' images/series)', normalize=True, save=True)
def car_example(): test_data = dt.get_data('cifar10', (50000, 60000)) cars = [6983, 3678, 3170, 1591] cc0 = metrics_color.ColorDensityCube(resolution=4) cc0.feed(test_data[0][cars[0]]) plotting.imshow(test_data[0][cars[0]]) cc0.plot_cube() cc0 = metrics_color.ColorDensityCube(resolution=4) cc0.feed(test_data[0][cars[1]]) plotting.imshow(test_data[0][cars[1]]) cc0.plot_cube()
def cc_grant(): img_name = '/Users/user/Desktop/grant.jpg' cc = metrics_color.ColorDensityCube(64) img = cv2.imread(img_name) cc.feed(img) cc.normalize() cc.plot_cube()
def show_distribution(): images_cube = dt.cifar10_maxcolor_domains(granularity=4, data_range=(50000, 60000)) region_sizes = dt.cube_cardinals(images_cube) cc = metrics_color.ColorDensityCube(resolution=4, cube=region_sizes) cc.normalize() cc.plot_cube()
def show_imgs(id_list, title, dataset, tag_list=None, showColorCube=False, resolution=16): # list of img list n_images = min(10, len(id_list)) if n_images >= 1: fig, axes = plt.subplots(1 + showColorCube, n_images, squeeze=False, figsize=(n_images, 12), subplot_kw={'xticks': (), 'yticks': ()}) if showColorCube: cc = metrics_color.ColorDensityCube(resolution=resolution) for i in xrange(n_images): ax = axes[0][i] img_id = id_list[i] cc.feed(dataset[img_id]) ax.imshow(dataset[img_id], vmin=0, vmax=1) if tag_list: ax.set_title(tag_list[i]) # ax.set_title('label #' + str(id_list) + ' (' + str(i) + '/' + str(len(id_list)) + ' images)') plot_cube(cc, fig) else: for i in xrange(n_images): ax = axes[0][i] img_id = id_list[i] ax.imshow(dataset[img_id], vmin=0, vmax=1) if tag_list: ax.set_title(tag_list[i]) # ax.set_title('label #' + str(id_list) + ' (' + str(i) + '/' + str(len(id_list)) + ' images)') if title: fig.suptitle(title + ' - label #' + str(id_list)) # fig.subplots_adjust() plt.show() plt.close()
def bdd100k_cc_analysis(): model_files = ['densenet121_bdd100k_cl0-500k_20ep_woda_ep20_vl0.22.hdf5', ] # 'resnet50_bdd100k_cl0-500k_20ep_woda_ep13_vl0.27.hdf5', # 'mobilenet_bdd100k_cl0-500k_20ep_woda_ep15_vl0.24.hdf5', # 'mobilenetv2_bdd100k_cl0-500k_20ep_woda_ep17_vl0.22.hdf5', # 'nasnet_bdd100k_cl0-500k_20ep_woda_ep17_vl0.24.hdf5'] class_map_file = bu.class_mapping(input_json=val_json, output_csv=labels_path + 'class_mapping.csv') # Dataset for analysis val_partition, val_labels = bu.get_ids_labels(val_labels_csv, class_map_file) for m in model_files: # test_subset creation pr_file = '.'.join(m.split('.')[:-1]) + '_predictions.csv' predictions, y_scores, img_ids = dt.get_scores_from_file(csv_path + pr_file, val_labels) top_n_args, bot_n_args = dt.get_topbot_n_args(20000, y_scores) cc_high = metrics_color.ColorDensityCube(resolution=4) for arg in top_n_args: cc_high.feed(cv2.imread(img_ids[arg])) print('high sum', np.sum(cc_high.get_cube().flatten())) cc_high.normalize() cc_high.plot_cube() cc_low = metrics_color.ColorDensityCube(resolution=4) for arg in bot_n_args: cc_low.feed(cv2.imread(img_ids[arg])) print('low sum', np.sum(cc_low.get_cube().flatten())) cc_low.normalize() cc_low.plot_cube() cc_diff = cc_high.substract(cc_low, 'value') print('diff mean', np.sum(cc_diff.get_cube().flatten())) print('diff mean', np.mean(cc_diff.get_cube().flatten())) cc_diff.normalize() # cc_diff.plot_cube() # cc_diff.normalize() cc_diff.plot_cube(title='Color cube analysis difference (' + str(20000) + ' images/series)', normalize=True, save=True)
def pr_on_fair_distribution(models=['densenet121'], top_n=100, res=4): test_data = dt.get_data('cifar10', (50000, 60000)) # Add every image's cube in densities densities = [] for img in test_data[0]: cc = metrics_color.ColorDensityCube(res) cc.feed(img) densities.append(cc.get_cube()) # ccf = np.array(cc.get_cube()).flatten() # Shape densities (list of cubes) to make a list per color densities_lists = np.swapaxes(np.swapaxes(np.swapaxes(densities, 0, 3), 0, 2), 0, 1) # print(densities_lists.shape) densities_cube = np.empty((res, res, res), dtype=object) # For each color keep the ids of the top_n most dense images in this color (same image can be in 2 colors) for i in xrange(res): for j in xrange(res): for k in xrange(res): # pr_most_dense = [] density_list = densities_lists[i][j][k].tolist() args_most_dense = np.argsort(density_list)[-top_n:] densities_cube[i][j][k] = args_most_dense # print(densities_cube.shape) # Per model analysis for m in models: # Load model predictions and ground_truth values model_name0 = mt.weight_file_name(m, 'cifar10-2-5', 50, False) y_predicted = t_log.load_predictions(model_name0, file_path=csv_path) true_classes = [int(k) for k in test_data[1]] pr = metrics.prediction_ratings(y_predicted, true_classes) # For each color get prediction score of the top_n images score_cube = np.zeros((res, res, res)) global_cc = metrics_color.ColorDensityCube(resolution=res) args_most_dense_all = [] for i in xrange(res): for j in xrange(res): for k in xrange(res): pr_most_dense = [] densities_args = densities_cube[i][j][k].tolist() # args_most_dense = np.argsort(density_list)[-topn:] ijk_cc = metrics_color.ColorDensityCube(res) for a in densities_cube[i][j][k].tolist(): pr_most_dense.append(pr[a]) ijk_cc.feed(test_data[0][a]) global_cc.feed(test_data[0][a]) ijk_cc.normalize() ttl = 'color = (' + str(float(i/res)) + ', ' + str(float(j/res)) + ', ' + str(float(k/res)) + ')' # ijk_cc.plot_cube() score_cube[i][j][k] = np.mean(pr_most_dense) print(np.mean(pr_most_dense)) # args_most_dense_all.append(args_most_dense) ttl = 'color = (' + str(float(i/res)) + ', ' + str(float(j/res)) + ', ' + str(float(k/res)) + ')' # plotting.show_imgs(densities_args[:10], ttl, test_data[0], showColorCube=True, resolution=4) global_cc.normalize() global_cc.plot_cube(title='Fair distributed dataset ColorCube') sc = metrics_color.ColorDensityCube(resolution=res, cube=score_cube) sc.normalize() sc.plot_cube(title='Scores per color for ' + m)