def generate_dif_mat_single(predictions, labels, plot_flg=False, sub_set=""): """ Generates diffusion matrix for single output :param predictions: :param labels: :param plot_flg: :param sub_set: :return: """ matrices = [] # single case for i in range(1): s = (np.shape(predictions)[-1], np.shape(predictions)[-1]) matrices.append(np.zeros(shape=s)) for pred, lab in zip(predictions, labels): p = np.argmax(pred) matrices[0][p][lab] += 1 if plot_flg: for i in range(1): plot_matrix(matrices[i], str(i) + "_" + sub_set + "_", get_cat_attributes_names()) return matrices
def generate_dif_mat(predictions, labels, plot_flg=False, sub_set=""): matrices = [] att_cnt = len(predictions) #todo add iff predictions empty for i in range(att_cnt): l = len(predictions[i][0]) s = (l, l) print("Shape:", s) matrices.append(np.zeros(shape=s)) for att_pred, att_lab, i in zip(predictions, labels, range(att_cnt)): for pred, lab in zip(att_pred, att_lab): if lab != MASK_VALUE: # categorical p = np.argmax(pred) # l = np.argmax(lab) l = lab # matrices[i][p][l] += 1 # sparse matrices[i][p][l] += 1 if plot_flg: for i in range(att_cnt): plot_matrix(matrices[i], str(i) + "_" + sub_set + "_", get_cat_attributes_names()) return matrices
def plot_diff_matrices(matrices, split_name, ind=None): """ Plots percentage diffusion matrices and saves them as .png files into figures/confusion folder :param matrices: :param split_name: train/test/validation, it will be used as prefix for saved images :return: """ if ind is None: names = get_cat_attributes_names() categories = get_category_names() else: names = [get_cat_attributes_names()[ind]] categories = [get_category_names()[ind]] for matrix, alpha, cat in zip(matrices, names, categories): print("-----------------", split_name) plot_matrix(convert_to_percentage_mat(matrix), split_name + "_" + cat, alpha)
def run_err_stats(): """ Computes error rate in percentage per category. Requires diff_dict.npy to load data. :return: """ d_d = load_dictionary("diff_dict.npy") alphas = get_cat_attributes_names() for key in d_d.keys(): print("----- ", key, "-------") for matrix, alpha in zip(d_d[key], alphas): print_errs(convert_to_percentage_mat(matrix), alpha)
def plot_diff_matrices_m(matrices, split_name): """ Plots percentage diffusion matrices and saves them as .png files into figures/confusion folder :param matrices: :param split_name: train/test/validation, it will be used as prefix for saved images :return: """ names = get_cat_attributes_names() categories = get_category_names() names.append(["Adolescent", "Adult", "Middle aged", "Retired", "Old"]) categories.append("Age") for matrix, alpha, cat in zip(matrices, names, categories): plot_matrix(convert_to_percentage_mat(matrix), split_name + "_" + cat, alpha)