def compute_mean_ioU_val_part(preds, labels, scales, centers, rotations, num_classes, datadir, input_size=[473, 473], dataset='val', part=None): confusion_matrix = np.zeros((num_classes, num_classes)) for i in range(len(preds)): #im_name in enumerate(val_id): gt = labels[i] pred_out = preds[i] h, w = gt.shape s = scales[i] c = centers[i] if not isinstance(rotations, int): r = rotations[i] pred = transform_parsing(pred_out, c, s, r, w, h, input_size) else: pred = transform_parsing(pred_out, c, s, 0, w, h, input_size) gt = np.asarray(gt, dtype=np.int32) pred = np.asarray(pred, dtype=np.int32) rand = np.round(random.random() * 100) # cv2.imwrite('./save_dir/'+str(rand)+str(i) +'.png', pred) ignore_index = gt != 255 gt = gt[ignore_index] pred = pred[ignore_index] confusion_matrix += get_confusion_matrix(gt, pred, num_classes) pos = confusion_matrix.sum(1) res = confusion_matrix.sum(0) tp = np.diag(confusion_matrix) pixel_accuracy = (tp.sum() / pos.sum()) * 100 mean_accuracy = ((tp / np.maximum(1.0, pos)).mean()) * 100 IoU_array = (tp / np.maximum(1.0, pos + res - tp)) IoU_array = IoU_array * 100 mean_IoU = IoU_array.mean() # set_trace() # print('Pixel accuracy: %f \n' % pixel_accuracy) # print('Mean accuracy: %f \n' % mean_accuracy) # print('Mean IU: %f \n' % mean_IoU) name_value = [] for i, (label, iou) in enumerate(zip(LABELS, IoU_array)): name_value.append((label, iou)) name_value.append(('Pixel accuracy', pixel_accuracy)) name_value.append(('Mean accuracy', mean_accuracy)) name_value.append(('Mean IU', mean_IoU)) name_value = OrderedDict(name_value) return name_value
def write_logits(preds, scales, centers, datadir, dataset, result_dir, input_size=[473, 473], list_path=''): result_root = os.path.join(result_dir, dataset + '_logits/') if not os.path.exists(result_root): os.makedirs(result_root) print('Make Dir: ', result_root) id_path = os.path.join(list_path) reader = open(id_path) data_list = reader.readlines()[0:len(preds)] count = 0 for im_name, pred_out, s, c in zip(data_list, preds, scales, centers): if count % 100 == 0: print('Have Save Logits %d' % count) im_name = im_name.strip() image_path = os.path.join(datadir, dataset + '_images', im_name + '.jpg') image = cv2.imread(image_path) h, w, _ = image.shape pred = transform_parsing(pred_out, c, s, w, h, input_size) save_path = os.path.join(result_root, im_name + '.npy') np.save(save_path, pred) count = count + 1
def write_results(preds, scales, centers, datadir, dataset, result_dir, input_size=[473, 473]): palette = get_palette(20) if not os.path.exists(result_dir): os.makedirs(result_dir) json_file = os.path.join(datadir, 'annotations', dataset + '.json') with open(json_file) as data_file: data_list = json.load(data_file) data_list = data_list['root'] for item, pred_out, s, c in zip(data_list, preds, scales, centers): im_name = item['im_name'] w = item['img_width'] h = item['img_height'] pred = transform_parsing(pred_out, c, s, w, h, input_size) #pred = pred_out save_path = os.path.join(result_dir, im_name[:-4] + '.png') output_im = PILImage.fromarray(np.asarray(pred, dtype=np.uint8)) output_im.putpalette(palette) output_im.save(save_path)
def compute_mean_ioU_mhp(preds, scales, centers, num_classes, datadir, input_size=[473, 473], dataset='val', dataset_list='_new.txt'): list_path = os.path.join(datadir, dataset + dataset_list) val_id = [i_id.strip() for i_id in open(list_path)] confusion_matrix = np.zeros((num_classes, num_classes)) for i, im_name in enumerate(val_id): gt_path = os.path.join(datadir, dataset, 'single_parsing_anno_big', im_name + '.png') gt = cv2.imread(gt_path, cv2.IMREAD_GRAYSCALE) #[:,:,2] h, w = gt.shape # set_trace() pred_out = preds[i] s = scales[i] c = centers[i] pred = transform_parsing(pred_out, c, s, 0, w, h, input_size) gt = np.asarray(gt, dtype=np.int32) pred = np.asarray(pred, dtype=np.int32) ignore_index = gt != 255 gt = gt[ignore_index] pred = pred[ignore_index] confusion_matrix += get_confusion_matrix(gt, pred, num_classes) pos = confusion_matrix.sum(1) res = confusion_matrix.sum(0) tp = np.diag(confusion_matrix) pixel_accuracy = (tp.sum() / pos.sum()) * 100 mean_accuracy = ((tp / np.maximum(1.0, pos)).mean()) * 100 IoU_array = (tp / np.maximum(1.0, pos + res - tp)) IoU_array = IoU_array * 100 mean_IoU = IoU_array.mean() # set_trace() print('Pixel accuracy: %f \n' % pixel_accuracy) print('Mean accuracy: %f \n' % mean_accuracy) print('Mean IU: %f \n' % mean_IoU) name_value = [] for i, (label, iou) in enumerate(zip(LABELS, IoU_array)): name_value.append((label, iou)) name_value.append(('Pixel accuracy', pixel_accuracy)) name_value.append(('Mean accuracy', mean_accuracy)) name_value.append(('Mean IU', mean_IoU)) name_value = OrderedDict(name_value) return name_value
def transform_and_save(pred_batch, scales, centers, heights, widths, names, input_size, save_dir): for i in range(len(pred_batch)): pred_out = pred_batch[i] h, w, s, c = heights[i], widths[i], scales[i], centers[i] pred = transform_parsing(pred_out, c, s, w, h, input_size=input_size) output_im = Image.fromarray(pred) output_im.putpalette(PALETTE) output_im.save(os.path.join(save_dir, names[i] + '.png'))
def compute_mean_ioU(preds, scales, centers, num_classes, datadir, input_size=[473, 473], dataset='val', num_sample=-1): """ Args: num_sample: the number of samples for evaluate, -1 means all samples. """ list_path = os.path.join(datadir, dataset + '_id.txt') val_id = [i_id.strip() for i_id in open(list_path)] if num_sample != -1: val_id = val_id[:num_sample] confusion_matrix = np.zeros((num_classes, num_classes)) from tqdm import tqdm for i, im_name in tqdm(enumerate(val_id)): gt_path = os.path.join(datadir, dataset + '_segmentations', im_name + '.png') gt = cv2.imread(gt_path, cv2.IMREAD_GRAYSCALE) h, w = gt.shape pred_out = preds[i] s = scales[i] c = centers[i] pred = transform_parsing(pred_out, c, s, w, h, input_size) gt = np.asarray(gt, dtype=np.int32) pred = np.asarray(pred, dtype=np.int32) ignore_index = gt != 255 gt = gt[ignore_index] pred = pred[ignore_index] confusion_matrix += get_confusion_matrix(gt, pred, num_classes) pos = confusion_matrix.sum(1) res = confusion_matrix.sum(0) tp = np.diag(confusion_matrix) pixel_accuracy = (tp.sum() / pos.sum()) * 100 mean_accuracy = ((tp / np.maximum(1.0, pos)).mean()) * 100 IoU_array = (tp / np.maximum(1.0, pos + res - tp)) IoU_array = IoU_array * 100 mean_IoU = IoU_array.mean() print('Pixel accuracy: %f \n' % pixel_accuracy) print('Mean accuracy: %f \n' % mean_accuracy) print('Mean IU: %f \n' % mean_IoU) name_value = [] for i, (label, iou) in enumerate(zip(LABELS, IoU_array)): name_value.append((label, iou)) name_value.append(('Pixel accuracy', pixel_accuracy)) name_value.append(('Mean accuracy', mean_accuracy)) name_value.append(('Mean IU', mean_IoU)) name_value = OrderedDict(name_value) return name_value
def compute_mean_ioU(preds, scales, centers, num_classes, datadir, input_size, dataset='val'): list_path = os.path.join(datadir, dataset + '_id.txt') val_id = [i_id.strip() for i_id in open(list_path)] confusion_matrix = np.zeros((num_classes, num_classes)) labels_dataset = LABELS_PASCAL for i, im_name in enumerate(val_id): gt_path = os.path.join(datadir, 'pascal_person_part_gt', im_name + '.png') labels_dataset = LABELS_PASCAL gt = cv2.imread(gt_path, cv2.IMREAD_GRAYSCALE) h, w = gt.shape pred_out = preds[i] s = scales[i] c = centers[i] pred = transform_parsing(pred_out, c, s, w, h, input_size) gt = np.asarray(gt, dtype=np.int32) pred = np.asarray(pred, dtype=np.int32) ignore_index = gt != 255 gt = gt[ignore_index] pred = pred[ignore_index] confusion_matrix += get_confusion_matrix(gt, pred, num_classes) pos = confusion_matrix.sum(1) # 列之和是每类类预测的数 res = confusion_matrix.sum(0) # 行之和是每类实际上的数 tp = np.diag(confusion_matrix) # 每类预测正确的数 pixel_accuracy = (tp.sum() / pos.sum()) * 100 #预测正确的除以总数 mean_accuracy = ((tp / np.maximum(1.0, pos)).mean()) * 100 IoU_array = (tp / np.maximum(1.0, pos + res - tp)) IoU_array = IoU_array * 100 mean_IoU = IoU_array.mean() name_value = [] for i, (label, iou) in enumerate(zip(labels_dataset, IoU_array)): name_value.append((label, iou)) name_value.append(('Pixel accuracy', pixel_accuracy)) name_value.append(('Mean accuracy', mean_accuracy)) name_value.append(('Mean IoU', mean_IoU)) name_value = OrderedDict(name_value) return name_value
def compute_mean_ioU(preds, scales, centers, num_classes, datadir, input_size=[473, 473], dataset='val'): val_file = os.path.join(datadir, dataset + '_id.txt') val_id = [i_id.strip() for i_id in open(val_file)] confusion_matrix = np.zeros((num_classes, num_classes)) for i, pred_out in enumerate(preds): im_name = val_id[i] gt_path = os.path.join(datadir, dataset + '_segmentations', im_name + '.png') gt = np.array(PILImage.open(gt_path)) h, w = gt.shape s = scales[i] c = centers[i] pred = transform_parsing(pred_out, c, s, w, h, input_size) gt = np.asarray(gt, dtype=np.int32) pred = np.asarray(pred, dtype=np.int32) ignore_index = gt != 255 gt = gt[ignore_index] pred = pred[ignore_index] confusion_matrix += get_confusion_matrix(gt, pred, num_classes) pos = confusion_matrix.sum(1) res = confusion_matrix.sum(0) tp = np.diag(confusion_matrix) pixel_accuracy = (tp.sum() / pos.sum()) * 100 mean_accuracy = ((tp / np.maximum(1.0, pos)).mean()) * 100 IoU_array = (tp / np.maximum(1.0, pos + res - tp)) IoU_array = IoU_array * 100 mean_IoU = IoU_array.mean() print('Pixel accuracy: %f \n' % pixel_accuracy) print('Mean accuracy: %f \n' % mean_accuracy) print('Mean IU: %f \n' % mean_IoU) name_value = [] for i, (label, iou) in enumerate(zip(LABELS, IoU_array)): name_value.append((label, iou)) name_value.append(('Pixel accuracy', pixel_accuracy)) name_value.append(('Mean accuracy', mean_accuracy)) name_value.append(('Mean IU', mean_IoU)) name_value = OrderedDict(name_value) return name_value
def compute_mean_ioU(preds, scales, centers, num_classes, datadir, input_size=[473, 473], dataset='val'): list_path = os.path.join(datadir, dataset + '_id.txt') val_id = [i_id.strip() for i_id in open(list_path)] confusion_matrix = np.zeros((num_classes, num_classes)) for i, im_name in enumerate(val_id): gt_path = os.path.join(datadir, dataset + '_segmentations', im_name + '.png') gt = cv2.imread(gt_path, cv2.IMREAD_GRAYSCALE) h, w = gt.shape pred_out = preds[i] s = scales[i] c = centers[i] pred = transform_parsing(pred_out, c, s, w, h, input_size) # output_im = PIL.Image.fromarray(np.asarray(pred, dtype=np.uint8)) # output_im.putpalette(palette) # output_im.save('/home/jeromewan/SJTU Thesis/CE2P/CE2P-master/dataset/LIP/Predictions/'+im_name+'.png') gt = np.asarray(gt, dtype=np.int32) pred = np.asarray(pred, dtype=np.int32) ignore_index = gt != 255 gt = gt[ignore_index] pred = pred[ignore_index] confusion_matrix += get_confusion_matrix(gt, pred, num_classes) pos = confusion_matrix.sum(1) res = confusion_matrix.sum(0) tp = np.diag(confusion_matrix) pixel_accuracy = (tp.sum() / pos.sum()) * 100 mean_accuracy = ((tp / np.maximum(1.0, pos)).mean()) * 100 IoU_array = (tp / np.maximum(1.0, pos + res - tp)) IoU_array = IoU_array * 100 mean_IoU = IoU_array.mean() print('Pixel accuracy: %f \n' % pixel_accuracy) print('Mean accuracy: %f \n' % mean_accuracy) print('Mean IU: %f \n' % mean_IoU) name_value = [] #CHANGE THE LABELS NAME #THEN CHECK WHY IT ISNT WORKING for i, (label, iou) in enumerate(zip(LABELS, IoU_array)): name_value.append((label, iou)) name_value.append(('Pixel accuracy', pixel_accuracy)) name_value.append(('Mean accuracy', mean_accuracy)) name_value.append(('Mean IU', mean_IoU)) name_value = OrderedDict(name_value) return name_value
def write_results(preds, scales, centers, datadir, dataset, result_dir, input_size=[473, 473], list_path=''): if not os.path.exists(result_dir): os.makedirs(result_dir) print('Make Dir: ', result_dir) result_root = os.path.join(result_dir, dataset + '_result/') if not os.path.exists(result_root): os.makedirs(result_root) print('Make Dir: ', result_root) vis_root = os.path.join(result_dir, dataset + '_vis/') if not os.path.exists(vis_root): os.makedirs(vis_root) print('Make Dir: ', vis_root) palette = get_lip_palette() id_path = os.path.join(list_path) reader = open(id_path) data_list = reader.readlines()[0:len(preds)] count = 0 for im_name, pred_out, s, c in zip(data_list, preds, scales, centers): if count % 100 == 0: print('Have Save Result: %d' % count) im_name = im_name.strip() image_path = os.path.join(datadir, dataset + '_images', im_name + '.jpg') image = cv2.imread(image_path) h, w, _ = image.shape pred = transform_parsing(pred_out, c, s, w, h, input_size) save_path = os.path.join(result_root, im_name + '.png') output_im = PILImage.fromarray(np.asarray(pred, dtype=np.uint8)) output_im.save(save_path) save_path = os.path.join(vis_root, im_name + '.png') output_im = PILImage.fromarray(np.asarray(pred, dtype=np.uint8)) output_im.putpalette(palette) output_im.save(save_path) count = count + 1
def write_results(preds, scales, centers, datadir, dataset, result_dir, input_size=[473, 473], list_path=None): result_root = os.path.join(result_dir, dataset + '_result/') if not os.path.exists(result_root): os.makedirs(result_root) vis_root = os.path.join(result_dir, dataset + '_vis/') if not os.path.exists(vis_root): os.makedirs(vis_root) if 'coarse' in dataset: palette = get_vp_coarse_palette() elif 'fine' in dataset: palette = get_vp_fine_palette() id_path = os.path.join(datadir, dataset + '_id.txt') reader = open(id_path) data_list = reader.readlines()[0:len(preds)] dataset_name = dataset.split('_')[0] for im_name, pred_out, s, c in zip(data_list, preds, scales, centers): im_name = im_name.strip().split('.')[0] image_path = os.path.join(datadir, dataset_name + '_image', im_name + '.jpg') image = cv2.imread(image_path) h, w, _ = image.shape pred = transform_parsing(pred_out, c, s, w, h, input_size) save_path = os.path.join(result_root, im_name + '.png') output_im = PILImage.fromarray(np.asarray(pred, dtype=np.uint8)) output_im.save(save_path) save_path = os.path.join(vis_root, im_name + '.png') output_im = PILImage.fromarray(np.asarray(pred, dtype=np.uint8)) output_im.putpalette(palette) output_im.save(save_path)
def compute_mean_ioU(preds, scales, centers, num_classes, datadir, list_path, input_size=[473, 473], dataset='coarse_val'): reader = open(list_path) val_id = reader.readlines()[0:len(preds)] confusion_matrix = np.zeros((num_classes, num_classes)) dataset_name = dataset.split('_')[0] for i, im_name in enumerate(val_id): im_name = im_name.strip().split('.')[0] gt_path = os.path.join(datadir, dataset_name + '_annotation', im_name + '.png') gt = cv2.imread(gt_path, cv2.IMREAD_GRAYSCALE) if type(gt) == None: print('Error in read file ', gt_path) h, w = gt.shape pred_out = preds[i] s = scales[i] c = centers[i] pred = transform_parsing(pred_out, c, s, w, h, input_size) gt = np.asarray(gt, dtype=np.int32) pred = np.asarray(pred, dtype=np.int32) ignore_index = gt != 255 gt = gt[ignore_index] pred = pred[ignore_index] confusion_matrix += get_confusion_matrix(gt, pred, num_classes) pos = confusion_matrix.sum(1) res = confusion_matrix.sum(0) tp = np.diag(confusion_matrix) has_test = res > 1 pixel_accuracy = (tp.sum() / pos.sum()) * 100 mean_accuracy = ((tp / np.maximum(1.0, pos))[has_test].mean()) * 100 IoU_array = (tp / np.maximum(1.0, pos + res - tp)) IoU_array = IoU_array * 100 # mean_IoU = IoU_array.mean() mean_IoU = IoU_array[has_test].mean( ) # ignore classes having no test samples print('Pixel accuracy: %f \n' % pixel_accuracy) print('Mean accuracy: %f \n' % mean_accuracy) print('Mean IU: %f \n' % mean_IoU) name_value = [] no_test_name = [] if 'coarse' in dataset: LABELS = LABELS_VP_COARSE elif 'fine' in dataset: LABELS = LABELS_VP_FINE for i, (label, iou) in enumerate(zip(LABELS, IoU_array)): if has_test[i]: name_value.append((label, iou)) else: no_test_name.append(label) name_value.append(('Pixel accuracy', pixel_accuracy)) name_value.append(('Mean accuracy', mean_accuracy)) name_value.append(('Mean IU', mean_IoU)) name_value = OrderedDict(name_value) return name_value, no_test_name
def compute_mean_ioU(preds, scales, centers, num_classes, datadir, input_size=[473, 473], dataset='val', list_path=''): # val_file = os.path.join(datadir, 'annotations', dataset + '.json') # anno_file = open(val_file) # anno = json.load(anno_file) # anno = anno['root'] # val_id = [] # for i, a in enumerate(anno): # val_id.append(a['im_name'][:-4]) reader = open(list_path) val_id = reader.readlines()[0:len(preds)] confusion_matrix = np.zeros((num_classes, num_classes)) for i, im_name in enumerate(val_id): im_name = im_name.strip() gt_path = os.path.join(datadir, dataset + '_labels', im_name + '.png') gt = cv2.imread(gt_path, cv2.IMREAD_GRAYSCALE) h, w = gt.shape pred_out = preds[i] s = scales[i] c = centers[i] pred = transform_parsing(pred_out, c, s, w, h, input_size) gt = np.asarray(gt, dtype=np.int32) pred = np.asarray(pred, dtype=np.int32) ignore_index = gt != 255 gt = gt[ignore_index] pred = pred[ignore_index] confusion_matrix += get_confusion_matrix(gt, pred, num_classes) pos = confusion_matrix.sum(1) res = confusion_matrix.sum(0) tp = np.diag(confusion_matrix) has_test = res > 1 pixel_accuracy = (tp.sum() / pos.sum()) * 100 mean_accuracy = ((tp / np.maximum(1.0, pos))[has_test].mean()) * 100 IoU_array = (tp / np.maximum(1.0, pos + res - tp)) IoU_array = IoU_array * 100 # mean_IoU = IoU_array.mean() mean_IoU = IoU_array[has_test].mean( ) # ignore classes having no test samples print('Pixel accuracy: %f \n' % pixel_accuracy) print('Mean accuracy: %f \n' % mean_accuracy) print('Mean IU: %f \n' % mean_IoU) name_value = [] no_test_name = [] for i, (label, iou) in enumerate(zip(LABELS, IoU_array)): if has_test[i]: name_value.append((label, iou)) else: no_test_name.append(label) name_value.append(('Pixel accuracy', pixel_accuracy)) name_value.append(('Mean accuracy', mean_accuracy)) name_value.append(('Mean IU', mean_IoU)) name_value = OrderedDict(name_value) return name_value, no_test_name
def compute_mean_ioU(preds, scales, centers, num_classes, datadir, input_size=[473, 473], dataset='val', reverse=False): file_list_name = os.path.join(datadir, dataset + '_list.txt') val_id = [ line.split()[0][7:-4] for line in open(file_list_name).readlines() ] confusion_matrix = np.zeros((num_classes, num_classes)) label_names_file = os.path.join(datadir, 'label_names.txt') gt_label_names = pred_label_names = _read_names(label_names_file) assert gt_label_names[0] == pred_label_names[0] == 'bg' hists = [] for i, im_name in enumerate(val_id): gt_path = os.path.join(datadir, dataset, 'labels', im_name + '.png') gt = cv2.imread(gt_path, cv2.IMREAD_GRAYSCALE) h, w = gt.shape pred_out = preds[i] if scales is not None: s = scales[i] c = centers[i] else: s = None c = None pred_old = transform_parsing(pred_out, c, s, w, h, input_size) gt = np.asarray(gt, dtype=np.int32) pred = np.asarray(pred_old, dtype=np.int32) ignore_index = gt != 255 gt = gt[ignore_index] pred = pred[ignore_index] hist = fast_histogram(gt, pred, len(gt_label_names), len(pred_label_names)) hists.append(hist) confusion_matrix += get_confusion_matrix(gt, pred, num_classes) hist_sum = np.sum(np.stack(hists, axis=0), axis=0) eval_names = dict() for label_name in gt_label_names: gt_ind = gt_label_names.index(label_name) pred_ind = pred_label_names.index(label_name) eval_names[label_name] = ([gt_ind], [pred_ind]) if 'le' in eval_names and 're' in eval_names: eval_names['eyes'] = _merge(eval_names['le'], eval_names['re']) if 'lb' in eval_names and 'rb' in eval_names: eval_names['brows'] = _merge(eval_names['lb'], eval_names['rb']) if 'ulip' in eval_names and 'imouth' in eval_names and 'llip' in eval_names: eval_names['mouth'] = _merge(eval_names['ulip'], eval_names['imouth'], eval_names['llip']) # Helen if 'eyes' in eval_names and 'brows' in eval_names and 'nose' in eval_names and 'mouth' in eval_names: eval_names['overall'] = _merge(eval_names['eyes'], eval_names['brows'], eval_names['nose'], eval_names['mouth']) pos = confusion_matrix.sum(1) res = confusion_matrix.sum(0) tp = np.diag(confusion_matrix) pixel_accuracy = (tp.sum() / pos.sum()) * 100 mean_accuracy = ((tp / np.maximum(1.0, pos)).mean()) * 100 IoU_array = (tp / np.maximum(1.0, pos + res - tp)) IoU_array = IoU_array * 100 mean_IoU = IoU_array.mean() print('Pixel accuracy: %f \n' % pixel_accuracy) print('Mean accuracy: %f \n' % mean_accuracy) print('Mean IU: %f \n' % mean_IoU) mIoU_value = [] f1_value = [] mf1_value = [] for i, (label, iou) in enumerate(zip(LABELS, IoU_array)): mIoU_value.append((label, iou)) mIoU_value.append(('Pixel accuracy', pixel_accuracy)) mIoU_value.append(('Mean accuracy', mean_accuracy)) mIoU_value.append(('Mean IU', mean_IoU)) mIoU_value = OrderedDict(mIoU_value) for eval_name, (gt_inds, pred_inds) in eval_names.items(): A = hist_sum[gt_inds, :].sum() B = hist_sum[:, pred_inds].sum() intersected = hist_sum[gt_inds, :][:, pred_inds].sum() f1 = 2 * intersected / (A + B) if eval_name in gt_label_names[1:]: mf1_value.append(f1) f1_value.append((eval_name, f1)) f1_value.append(('Mean_F1', np.array(mf1_value).mean())) f1_value = OrderedDict(f1_value) return mIoU_value, f1_value
def compute_mean_ioU_atr(preds, scales, centers, num_classes, datadir, input_size=[473, 473], dataset='val', part=None, label=None, rotations=None): list_path = os.path.join(datadir, dataset + '_id.txt') val_id = [i_id.strip() for i_id in open(list_path)] # set_trace() confusion_matrix = np.zeros((num_classes, num_classes)) for i, im_name in enumerate(val_id): # for i in range(len(preds)): gt_path = os.path.join(datadir, 'SegmentationClassAug', im_name + '.png') gt_img = cv2.imread(gt_path, cv2.IMREAD_GRAYSCALE) if label is not None: gt = label[i] else: gt = gt_img h, w = gt.shape pred_out = preds[i] s = scales[i] c = centers[i] if rotations is not None: r = rotations[i] gt = transform_parsing(gt, c, s, r, w, h, input_size) else: r = 0 pred = transform_parsing(pred_out, c, s, r, w, h, input_size) # pred = pred_out gt = np.asarray(gt, dtype=np.int32) pred = np.asarray(pred, dtype=np.int32) pred_save = np.array(pred, dtype=np.uint8) # output_im = PILImage.fromarray(pred_save) # output_im.putpalette(atr_palette) # output_im.save('./output_atr/' + im_name + '.png') # cv2.imwrite('./output_atr/' + im_name + '.png', output_im) ignore_index = gt != 255 gt = gt[ignore_index] pred = pred[ignore_index] confusion_matrix += get_confusion_matrix(gt, pred, num_classes) pos = confusion_matrix.sum(1) res = confusion_matrix.sum(0) tp = np.diag(confusion_matrix) # set_trace() overall_accuracy = tp.sum() / confusion_matrix.sum() fg_accuracy = tp[1:].sum() / (confusion_matrix[1:, 0:].sum()) pixel_accuracy = (tp.sum() / pos.sum()) * 100 # pixel_accuracy = overall_accuracy mean_accuracy = ((tp / np.maximum(1.0, pos)).mean()) * 100 precision = tp / np.maximum(1.0, res) recall = tp / np.maximum(1.0, pos) # set_trace() avg_pre = (tp / np.maximum(1.0, res)).mean() avg_f1 = (2 * precision * recall / (np.maximum(1.0, (precision + recall)))).mean() IoU_array = (tp / np.maximum(1.0, pos + res - tp)) IoU_array = IoU_array * 100 mean_IoU = IoU_array.mean() #print('Overall accuracy: %f \n' % overall_accuracy) print('F.g. Acc: %f \n' % fg_accuracy) print('Avg precision: %f \n' % avg_pre) print('Avg recall: %f \n' % mean_accuracy) #print('Mean accuracy: %f \n' % mean_accuracy) print('Avg F.1 :% f \n' % avg_f1) print('Mean IU: %f \n' % mean_IoU) name_value = [] for i, (label, iou) in enumerate(zip(LABELS, IoU_array)): name_value.append((label, iou)) name_value.append(('Pixel accuracy', pixel_accuracy)) name_value.append(('Mean accuracy', mean_accuracy)) name_value.append(('Mean IU', mean_IoU)) name_value = OrderedDict(name_value) return name_value
def main(): """Create the model and start the evaluation process.""" args = get_arguments() multi_scales = [float(i) for i in args.multi_scales.split(',')] gpus = [int(i) for i in args.gpu.split(',')] assert len(gpus) == 1 if not args.gpu == 'None': os.environ["CUDA_VISIBLE_DEVICES"] = args.gpu cudnn.benchmark = True cudnn.enabled = True h, w = map(int, args.input_size.split(',')) input_size = [h, w] model = networks.init_model(args.arch, num_classes=args.num_classes, pretrained=None) IMAGE_MEAN = model.mean IMAGE_STD = model.std INPUT_SPACE = model.input_space print('image mean: {}'.format(IMAGE_MEAN)) print('image std: {}'.format(IMAGE_STD)) print('input space:{}'.format(INPUT_SPACE)) if INPUT_SPACE == 'BGR': print('BGR Transformation') transform = transforms.Compose([ transforms.ToTensor(), transforms.Normalize(mean=IMAGE_MEAN, std=IMAGE_STD), ]) if INPUT_SPACE == 'RGB': print('RGB Transformation') transform = transforms.Compose([ transforms.ToTensor(), BGR2RGB_transform(), transforms.Normalize(mean=IMAGE_MEAN, std=IMAGE_STD), ]) # Data loader lip_test_dataset = LIPDataValSet(args.data_dir, 'val', crop_size=input_size, transform=transform, flip=args.flip) num_samples = len(lip_test_dataset) print('Totoal testing sample numbers: {}'.format(num_samples)) testloader = data.DataLoader(lip_test_dataset, batch_size=args.batch_size, shuffle=False, pin_memory=True) # Load model weight state_dict = torch.load(args.model_restore)['state_dict'] from collections import OrderedDict new_state_dict = OrderedDict() for k, v in state_dict.items(): name = k[7:] # remove `module.` new_state_dict[name] = v model.load_state_dict(new_state_dict) model.cuda() model.eval() sp_results_dir = os.path.join(args.log_dir, 'sp_results') if not os.path.exists(sp_results_dir): os.makedirs(sp_results_dir) palette = get_palette(20) parsing_preds = [] scales = np.zeros((num_samples, 2), dtype=np.float32) centers = np.zeros((num_samples, 2), dtype=np.int32) with torch.no_grad(): for idx, batch in enumerate(tqdm(testloader)): image, meta = batch if (len(image.shape) > 4): image = image.squeeze() im_name = meta['name'][0] c = meta['center'].numpy()[0] s = meta['scale'].numpy()[0] w = meta['width'].numpy()[0] h = meta['height'].numpy()[0] scales[idx, :] = s centers[idx, :] = c parsing, logits = multi_scale_testing(model, image.cuda(), crop_size=input_size, flip=args.flip, multi_scales=multi_scales) if args.save_results: parsing_result = transform_parsing(parsing, c, s, w, h, input_size) parsing_result_path = os.path.join(sp_results_dir, im_name + '.png') output_im = PILImage.fromarray( np.asarray(parsing_result, dtype=np.uint8)) output_im.putpalette(palette) output_im.save(parsing_result_path) parsing_preds.append(parsing) assert len(parsing_preds) == num_samples mIoU = compute_mean_ioU(parsing_preds, scales, centers, args.num_classes, args.data_dir, input_size) print(mIoU) return
def compute_mean_ioU(preds, scales, centers, num_classes, datadir, input_size=[473, 473], dataset='val', part=None): if part != None: print("Using %s part to compute miou: " % part) list_path = os.path.join(datadir, dataset + '_id.txt') else: list_path = os.path.join(datadir, dataset + '_id.txt') val_id = [i_id.strip() for i_id in open(list_path)] confusion_matrix = np.zeros((num_classes, num_classes)) for i, im_name in enumerate(val_id): gt_path = os.path.join(datadir, dataset + '_segmentations', im_name + '.png') gt = cv2.imread(gt_path, cv2.IMREAD_GRAYSCALE) if part: gt = np.where(gt == part_dict[part], 1, 0) h, w = gt.shape pred_out = preds[i] s = scales[i] c = centers[i] pred = transform_parsing(pred_out, c, s, 0, w, h, input_size) gt = np.asarray(gt, dtype=np.int32) pred = np.asarray(pred, dtype=np.int32) pred_save = np.array(pred, dtype=np.uint8) # print(pred_save) # cv2.imwrite('./test_img/' + str(i) + '.png', pred_save) ignore_index = gt != 255 gt = gt[ignore_index] pred = pred[ignore_index] confusion_matrix += get_confusion_matrix(gt, pred, num_classes) pos = confusion_matrix.sum(1) res = confusion_matrix.sum(0) tp = np.diag(confusion_matrix) pixel_accuracy = (tp.sum() / pos.sum()) * 100 mean_accuracy = ((tp / np.maximum(1.0, pos)).mean()) * 100 IoU_array = (tp / np.maximum(1.0, pos + res - tp)) IoU_array = IoU_array * 100 mean_IoU = IoU_array.mean() # set_trace() print('Pixel accuracy: %f \n' % pixel_accuracy) print('Mean accuracy: %f \n' % mean_accuracy) print('Mean IU: %f \n' % mean_IoU) name_value = [] for i, (label, iou) in enumerate(zip(LABELS, IoU_array)): name_value.append((label, iou)) name_value.append(('Pixel accuracy', pixel_accuracy)) name_value.append(('Mean accuracy', mean_accuracy)) name_value.append(('Mean IU', mean_IoU)) name_value = OrderedDict(name_value) return name_value
def compute_mean_ioU_cihp(preds, scales, centers, num_classes, datadir, input_size=[473, 473], dataset='val', dataset_list='_add_bkg_id.txt'): list_path = os.path.join(datadir, dataset, dataset + dataset_list) # test on whole image val_id = [i_id.strip() for i_id in open(list_path)] confusion_matrix = np.zeros((num_classes, num_classes)) # set_trace() for i, im_name in enumerate(val_id): # i = 0 # for gt_fi in os.listdir(os.path.join(datadir, dataset, 'single_parsing_anno_big')): # gt_path = os.path.join(datadir, dataset, 'single_parsing_anno_big', gt_fi) gt_path = os.path.join(datadir, dataset, 'single_parsing_anno_big', im_name + '.png') gt = cv2.imread(gt_path, cv2.IMREAD_GRAYSCALE) # gt = PILImage.open(gt_path) # assert input_size[0] == input_size[1] # gt = cv2.resize(gt, (input_size[0], input_size[1]), interpolation=cv2.INTER_NEAREST) # when wrote this!!! silly me!!! try: h, w = gt.shape except: print(im_name) set_trace() continue pred_out = preds[i] s = scales[i] c = centers[i] pred = transform_parsing(pred_out, c, s, 0, w, h, input_size) gt = np.asarray(gt, dtype=np.int32) pred = np.asarray(pred, dtype=np.int32) ignore_index = gt != 255 gt = gt[ignore_index] pred = pred[ignore_index] i += 1 confusion_matrix += get_confusion_matrix(gt, pred, num_classes) #set_trace() pos = confusion_matrix.sum(1) res = confusion_matrix.sum(0) tp = np.diag(confusion_matrix) pixel_accuracy = (tp.sum() / pos.sum()) * 100 mean_accuracy = ((tp / np.maximum(1.0, pos)).mean()) * 100 IoU_array = (tp / np.maximum(1.0, pos + res - tp)) IoU_array = IoU_array * 100 mean_IoU = IoU_array.mean() # set_trace() print('Pixel accuracy: %f \n' % pixel_accuracy) print('Mean accuracy: %f \n' % mean_accuracy) print('Mean IU: %f \n' % mean_IoU) name_value = [] for i, (label, iou) in enumerate(zip(LABELS, IoU_array)): name_value.append((label, iou)) name_value.append(('Pixel accuracy', pixel_accuracy)) name_value.append(('Mean accuracy', mean_accuracy)) name_value.append(('Mean IU', mean_IoU)) name_value = OrderedDict(name_value) return name_value
def compute_mean_ioU(preds, scales, centers, num_classes, datadir, input_size=[473, 473], dataset='val', drop_factor=None): list_path = os.path.join(datadir, dataset + '_id.txt') val_id = [i_id.strip() for i_id in open(list_path)] if drop_factor is not None: val_id = val_id[::drop_factor] confusion_matrix = np.zeros((num_classes, num_classes)) palette = get_palette(num_classes) for i, im_name in enumerate(val_id): gt_path = os.path.join(datadir, dataset + '_segmentations', im_name + '.png') gt = cv2.imread(gt_path, cv2.IMREAD_GRAYSCALE) h, w = gt.shape pred_out = preds[i] s = scales[i] c = centers[i] gt_output = PILImage.fromarray(np.asarray(pred_out, dtype=np.uint8)) gt_output.putpalette(palette) gt_output.save(os.path.join('output_val_pred_not_tr', im_name + '.png')) pred = transform_parsing(pred_out, c, s, w, h, input_size) gt = np.asarray(gt, dtype=np.int32) pred = np.asarray(pred, dtype=np.int32) # print('before ',gt.shape, pred.shape) gt_output = PILImage.fromarray(np.asarray(gt, dtype=np.uint8)) gt_output.putpalette(palette) gt_output.save(os.path.join('output_val_gt', im_name +'.png')) pred_output = PILImage.fromarray(np.asarray(pred, dtype=np.uint8)) pred_output.putpalette(palette) pred_output.save(os.path.join('output_val_pred', im_name +'.png')) ignore_index = gt != 255 gt = gt[ignore_index] pred = pred[ignore_index] # print('after ', gt.shape, pred.shape) confusion_matrix += get_confusion_matrix(gt, pred, num_classes) pos = confusion_matrix.sum(1) res = confusion_matrix.sum(0) tp = np.diag(confusion_matrix) pixel_accuracy = (tp.sum() / pos.sum()) * 100 mean_accuracy = ((tp / np.maximum(1.0, pos)).mean()) * 100 IoU_array = (tp / np.maximum(1.0, pos + res - tp)) IoU_array = IoU_array * 100 mean_IoU = IoU_array.mean() print('Pixel accuracy: %f \n' % pixel_accuracy) print('Mean accuracy: %f \n' % mean_accuracy) print('Mean IU: %f \n' % mean_IoU) name_value = [] for i, (label, iou) in enumerate(zip(LABELS, IoU_array)): name_value.append((label, iou)) name_value.append(('Pixel accuracy', pixel_accuracy)) name_value.append(('Mean accuracy', mean_accuracy)) name_value.append(('Mean IU', mean_IoU)) name_value = OrderedDict(name_value) return name_value
def compute_mean_ioU(preds, scales, centers, num_classes, datadir, input_size=[473, 473], dataset='val'): list_path = os.path.join(datadir, dataset + '_id.txt') val_id = [i_id.strip() for i_id in open(list_path)] confusion_matrix = np.zeros((num_classes, num_classes)) palette = get_lip_palette() for i, im_name in enumerate(val_id): gt_path = os.path.join(datadir, dataset + '_segmentations', im_name + '.png') gt = cv2.imread(gt_path, cv2.IMREAD_GRAYSCALE) h, w = gt.shape pred_out = preds[i] s = scales[i] c = centers[i] pred = transform_parsing(pred_out, c, s, w, h, input_size) ################### Save the presiction results """output_im = PILImage.fromarray(np.asarray(pred, dtype=np.uint8)) output_im.putpalette(palette) output_im.save('./output_cihp_3edge/'+im_name+'.png')""" ################### gt = np.asarray(gt, dtype=np.int32) pred = np.asarray(pred, dtype=np.int32) ignore_index = gt != 255 gt = gt[ignore_index] pred = pred[ignore_index] confusion_matrix += get_confusion_matrix(gt, pred, num_classes) pos = confusion_matrix.sum(1) res = confusion_matrix.sum(0) tp = np.diag(confusion_matrix) pixel_accuracy = (tp.sum() / pos.sum()) * 100 mean_accuracy = ((tp / np.maximum(1.0, pos)).mean()) * 100 IoU_array = (tp / np.maximum(1.0, pos + res - tp)) IoU_array = IoU_array * 100 #print(IoU_array) mean_IoU = IoU_array.mean() print('Pixel accuracy: %f \n' % pixel_accuracy) print('Mean accuracy: %f \n' % mean_accuracy) print('Mean IU: %f \n' % mean_IoU) name_value = [] for i, (label, iou) in enumerate(zip(LABELS_LIP, IoU_array)): name_value.append((label, iou)) name_value.append(('Pixel accuracy', pixel_accuracy)) name_value.append(('Mean accuracy', mean_accuracy)) name_value.append(('Mean IU', mean_IoU)) name_value = OrderedDict(name_value) return name_value