def img_to_label_list_not_for_change_type(GT_path, PRED_path, label_values_RGB): GT_img = cv.cvtColor(cv.imread(GT_path, 1), cv.COLOR_BGR2RGB) PRED_img = cv.cvtColor(cv.imread(PRED_path, 1), cv.COLOR_BGR2RGB) GT_precode = util.reverse_one_hot(util.one_hot_it(GT_img, label_values_RGB)) PRED_precode = util.reverse_one_hot( util.one_hot_it(PRED_img, label_values_RGB)) GT_flat = GT_precode.flatten() PRED_flat = PRED_precode.flatten() return GT_flat, PRED_flat
def compute_one(img_path,gt_path): out = load_image(img_path) # gt = load_image(gt_path) # 不要用interpolation=cv.INTER_NEAREST,不然结果不一样,估计opencv bug # 上面说法错了,要用interpolation=cv.INTER_NEAREST gt = cv.resize(load_image(gt_path),(512,256),interpolation=cv.INTER_NEAREST) # val_gt_erode paired with [0,0,0]label value # label order: R G B # num_classes = len(label_values) gt = util.reverse_one_hot(util.one_hot_it(gt, label_values)) output_image = util.reverse_one_hot(util.one_hot_it(out, label_values)) running_metrics_val.update(gt, output_image)
def val(args, model, dataloader, logger): with torch.no_grad(): model.eval() num = int(len(dataloader)) precision_record = [] # hist = np.zeros((args.num_classes, args.num_classes)) for i, (data, label) in enumerate(dataloader): if torch.cuda.is_available() and args.use_gpu: data = data.cuda() label = label.cuda() # get RGB predict image # label = label.squeeze(1) predict = model(data) # for i in range(predicts.size(0)): # predict = predicts[i] # predict = reverse_one_hot(predict) # get RGB label image # label = labels[i] if args.losstype == 'dice': label = reverse_one_hot(label) # label = np.array(label) # compute per pixel accuracy precision = compute_global_accuracy(predict, label) # hist += fast_hist(label.flatten(), predict.flatten(), args.num_classes) precision_record.append(precision) # precision_record[0,i] = precision_record.cpu().squeeze() precision_record = torch.Tensor(precision_record) precision = torch.mean(precision_record) # miou_list = per_class_iu(hist)[:-1] # miou = np.mean(miou_list) logger.info('precision per pixel for test: %.6f' % precision.cpu()) # logger.info('mIoU for validation: %.3f' % miou) return precision.cpu()
def img_to_label_list(GT_path,PRED_path): GT_img = cv.cvtColor(cv.imread(GT_path, 1), cv.COLOR_BGR2RGB) PRED_img = cv.cvtColor(cv.imread(PRED_path, 1), cv.COLOR_BGR2RGB) label_values_RGB = [(0, 0, 0), (255, 250, 250), (248, 248, 255), (211, 211, 211), (255, 99, 71), (255, 250, 240), (139, 69, 19), (250, 240, 230), (0, 206, 209), (255, 215, 0), (205, 92, 92), (255, 228, 196), (255, 218, 185), (255, 222, 173), (175, 238, 238), (255, 248, 220), (47, 79, 79), (255, 250, 205), (255, 245, 238), (240, 255, 240), (245, 255, 250), (240, 255, 255), (240, 248, 255), (230, 230, 250), (255, 240, 245), (255, 228, 225), (255, 255, 240), (105, 105, 105), (112, 128, 144), (190, 190, 190), (245, 245, 245), (100, 149, 237), (65, 105, 225), (0, 191, 255), (135, 206, 250), (70, 130, 180), (255, 228, 181), (250, 235, 215), (95, 158, 160), (0, 250, 154), (255, 255, 0), (255, 239, 213), (255, 235, 205)] GT_precode = util.reverse_one_hot(util.one_hot_it(GT_img, label_values_RGB)) PRED_precode = util.reverse_one_hot(util.one_hot_it(PRED_img, label_values_RGB)) GT_flat = GT_precode.flatten() PRED_flat = PRED_precode.flatten() return GT_flat,PRED_flat
def compute_one(img_path): label_values_RGB_SCPA_WC = [[0,0,0], [128,0,0],[0,128,0],[128,128,0],[0,0,128],[128,0,128],[0,128,128]] gt = load_image(img_path) out = util.reverse_one_hot(util.one_hot_it(gt, label_values_RGB_SCPA_WC)) out_str = 'out.png' cv.imwrite(out_str,out) image = Image.open(out_str) a = image.getcolors() a.sort(key=takesecond) print(a) print("color kind: %d" % len(a)) return a
(0, 206, 209), (255, 215, 0), (205, 92, 92), (255, 228, 196), (255, 218, 185), (255, 222, 173), (175, 238, 238), (255, 248, 220), (47, 79, 79), (255, 250, 205), (255, 245, 238), (240, 255, 240), (245, 255, 250), (240, 255, 255), (240, 248, 255), (230, 230, 250), (255, 240, 245), (255, 228, 225), (255, 255, 240), (105, 105, 105), (112, 128, 144), (190, 190, 190), (245, 245, 245), (100, 149, 237), (65, 105, 225), (0, 191, 255), (135, 206, 250), (70, 130, 180), (255, 228, 181), (250, 235, 215), (95, 158, 160), (0, 250, 154), (255, 255, 0), (255, 239, 213), (255, 235, 205)] for k in tqdm(range(len(GT_Str))): gt = load_image(GT_Str[k]) # out=util.reverse_one_hot(util.one_hot_it(gt,label_values_RGB_SCPA_WC)) out = util.reverse_one_hot(util.one_hot_it(gt, candy_color_list)) out_str = out_prefix + Path(GT_Str[k]).name cv.imwrite(out_str, out) # print("kk") image = Image.open(out_str) a = image.getcolors() a.sort(key=takefirst, reverse=True) print(a) print("color kind: %d" % len(a)) image = Image.open(GT_Str[0]) b = image.getcolors() b.sort(key=takefirst, reverse=True) print(b)
import cv2 as cv import util import helpers out = cv.imread("center_morph.png") gt = cv.imread("center_gt.png") label_values = [[0, 0, 0], [255, 255, 255]] num_classes = len(label_values) gt = util.reverse_one_hot(util.one_hot_it(gt, label_values)) # out = cv.cvtColor(out, cv.COLOR_GRAY2BGR) output_image = util.reverse_one_hot(util.one_hot_it(out, label_values)) # out_vis_image = helpers.colour_code_segmentation(output_image, label_values) accuracy, class_accuracies, prec, rec, f1, iou = util.evaluate_segmentation( pred=output_image, label=gt, num_classes=num_classes) print("precision: %f" % prec) print("recall: %f" % rec) print("F1: %f " % f1) print("mIoU: %f " % iou) # print("specimen: SAR01-00673")
(175, 238, 238), (255, 248, 220), (47, 79, 79), (255, 250, 205), (255, 245, 238), (240, 255, 240), (245, 255, 250), (240, 255, 255), (240, 248, 255), (230, 230, 250), (255, 240, 245), (255, 228, 225), (255, 255, 240), (105, 105, 105), (112, 128, 144), (190, 190, 190), (245, 245, 245), (100, 149, 237), (65, 105, 225), (0, 191, 255), (135, 206, 250), (70, 130, 180), (255, 228, 181), (250, 235, 215), (95, 158, 160), (0, 250, 154), (255, 255, 0), (255, 239, 213), (255, 235, 205)] # label_values_RGB=[(0,0,0),(255,255,255)] gt = [] pred = [] t = time.time() GT_precode = util.reverse_one_hot(util.one_hot_it(GT_img, label_values_RGB)) PRED_precode = util.reverse_one_hot(util.one_hot_it(PRED_img, label_values_RGB)) gt = GT_precode.flatten() pred = PRED_precode.flatten() Acc = accuracy_score(gt, pred) F1 = f1_score(gt, pred, average=None) mean_F1 = np.nanmean(F1) IoU = jaccard_score(gt, pred, average=None) IoU_format = np.around(100 * IoU, 4) mean_IoU = np.nanmean(IoU) tt = time.time() - t print("class F1") print(F1) print("class IoU")
import cv2 as cv from pathlib import Path import natsort import util from tqdm import tqdm def load_image(path): image = cv.cvtColor(cv.imread(path, 1), cv.COLOR_BGR2RGB) return image GT_Path = Path("C://Users/think\Desktop/train_samesize\clean_data/test_gt") GT_File = natsort.natsorted(list(GT_Path.glob("*.png")), alg=natsort.PATH) GT_Str = [] for i in GT_File: GT_Str.append(str(i)) out_prefix="C:\\Users\\think\Desktop\\train_samesize\clean_data\\test_gt_precode\\" # label_values_GID_9 = [[0,0,0], [0,0,200],[250,150,150],[250,200,0],[0,200,0],[200,0,200],[200,0,0],[250,0,150],[200,150,150]] # label_values_GID_16=[[0,0,0],[0,200,0],[150,250,0],[150,200,150],[200,0,200],[150,0,250],[150,150,250],[250,200,0],[200,200,0],[200,0,0],[250,0,150],[200,150,150],[250,150,150],[0,0,200],[0,150,200],[0,200,250]] # label_values_GID_8 = [[0,0,0], [150,250,0],[0,200,0],[200,0,200],[250,200,0],[200,0,0],[250,150,150],[0,0,200]] # label_values_RGB_SCPA_WC = [[0,0,0], [128,0,0],[0,128,0],[128,128,0],[0,0,128],[128,0,128],[0,128,128]] label_values_binary=[[0,0,0],[255,255,255]] for k in tqdm(range(len(GT_Str))): gt=load_image(GT_Str[k]) out=util.reverse_one_hot(util.one_hot_it(gt,label_values_binary)) out_str=out_prefix+Path(GT_Str[k]).name cv.imwrite(out_str,out) # print("kk")
for j in range(id_start, id_stop): pic_small.append( cv.cvtColor(cv.imread(IMG_Str[j], cv.IMREAD_COLOR), cv.COLOR_BGR2RGB)) id_start += one_large_contain_small out_path_prefix = "SCPA_WC/large/color/frrnb/" out_name = str(k) + '_pred.png' combine_one(pic_small, out_name, width, height) #以下是18_preprocee里precode.py直接复制的 def load_image(path): image = cv.cvtColor(cv.imread(path, 1), cv.COLOR_BGR2RGB) return image GT_Path = Path("../16_find_compact/SCPA_WC/large/color/frrnb") GT_File = natsort.natsorted(list(GT_Path.glob("*.png")), alg=natsort.PATH) GT_Str = [] for i in GT_File: GT_Str.append(str(i)) out_prefix = "../16_find_compact/SCPA_WC/large/precode/frrnb/" label_values_RGB_SCPA_WC = [[0, 0, 0], [128, 0, 0], [0, 128, 0], [128, 128, 0], [0, 0, 128], [128, 0, 128], [0, 128, 128]] for k in tqdm(range(len(GT_Str))): gt = load_image(GT_Str[k]) out = util.reverse_one_hot(util.one_hot_it(gt, label_values_RGB_SCPA_WC)) out_str = out_prefix + Path(GT_Str[k]).name cv.imwrite(out_str, out) # print("kk")