|
|
import os |
|
|
import re |
|
|
import numpy as np |
|
|
from PIL import Image |
|
|
|
|
|
|
|
|
|
|
|
folder_A = '/mnt/lustre/zhouyue1.vendor/geoground/data/exp_1104/mix_hbb/vis_seg_b8_test_box2mask' |
|
|
folder_B = '/mnt/lustre/zhouyue1.vendor/geoground/data/masks/rrsisd' |
|
|
|
|
|
|
|
|
def calculate_iou(mask1, mask2): |
|
|
|
|
|
intersection = np.logical_and(mask1, mask2).sum() |
|
|
union = np.logical_or(mask1, mask2).sum() |
|
|
if union == 0: |
|
|
return 1.0 |
|
|
return intersection / union, intersection, union |
|
|
|
|
|
|
|
|
files_A = [f for f in os.listdir(folder_A) if f.endswith('.png')] |
|
|
files_B = [f for f in os.listdir(folder_B) if f.endswith('.png')] |
|
|
|
|
|
|
|
|
pattern = re.compile(r'^(\d+)_') |
|
|
|
|
|
|
|
|
b_files_dict = {} |
|
|
|
|
|
|
|
|
for file_B in files_B: |
|
|
match_B = pattern.match(file_B) |
|
|
if match_B: |
|
|
prefix = match_B.group(1) |
|
|
if prefix in b_files_dict: |
|
|
b_files_dict[prefix].append(file_B) |
|
|
else: |
|
|
b_files_dict[prefix] = [file_B] |
|
|
|
|
|
|
|
|
ious = [] |
|
|
correct_count = 0 |
|
|
total_count = 0 |
|
|
cum_I, cum_U = 0, 0 |
|
|
|
|
|
|
|
|
for file_A in files_A: |
|
|
match_A = pattern.match(file_A) |
|
|
if match_A: |
|
|
prefix = match_A.group(1) |
|
|
if prefix in b_files_dict: |
|
|
|
|
|
img_A_path = os.path.join(folder_A, file_A) |
|
|
img_B_path = os.path.join(folder_B, b_files_dict[prefix][0]) |
|
|
|
|
|
|
|
|
img_A = Image.open(img_A_path).convert('L') |
|
|
img_B = Image.open(img_B_path).convert('L') |
|
|
|
|
|
img_B_size = img_B.size |
|
|
img_A = img_A.resize(img_B_size) |
|
|
|
|
|
|
|
|
mask_A = np.array(img_A) > 128 |
|
|
mask_B = np.array(img_B) > 128 |
|
|
|
|
|
|
|
|
iou, I, U = calculate_iou(mask_A, mask_B) |
|
|
ious.append(iou) |
|
|
|
|
|
cum_I += I |
|
|
cum_U += U |
|
|
|
|
|
|
|
|
total_count += 1 |
|
|
|
|
|
|
|
|
if iou >= 0.5: |
|
|
correct_count += 1 |
|
|
|
|
|
print(f"IoU for {file_A} and {b_files_dict[prefix][0]}: {iou}") |
|
|
else: |
|
|
print(f"No match found for {file_A} in folder B") |
|
|
|
|
|
|
|
|
if ious: |
|
|
mean_iou = np.mean(ious) |
|
|
print(f"mIoU: {mean_iou}") |
|
|
print(f"oIoU: {cum_I / cum_U}") |
|
|
else: |
|
|
mean_iou = 0 |
|
|
print("No matches found to calculate IoU.") |
|
|
|
|
|
|
|
|
|
|
|
if total_count > 0: |
|
|
acc_0_5 = correct_count / total_count |
|
|
print(f"Acc@0.5: {acc_0_5}") |
|
|
else: |
|
|
acc_0_5 = 0 |
|
|
print("No matches found to calculate Acc@0.5.") |