-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy patheval.py
187 lines (165 loc) · 6.72 KB
/
eval.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import argparse
import os
import sys
import cv2
import numpy as np
import torch
import torch.nn.functional as F
from transformers import AutoTokenizer, BitsAndBytesConfig
from model.evf_sam import EvfSamModel
import os
from model.segment_anything.utils.transforms import ResizeLongestSide
from utils.utils import (AverageMeter, ProgressMeter, Summary, dict_to_cuda,
intersectionAndUnionGPU)
from utils.dataset import collate_fn, ValDataset, Resize
from functools import partial
import tqdm
from torch.utils.data import DataLoader, DistributedSampler
from torch.nn.parallel import DistributedDataParallel
import torch.distributed as dist
def parse_args(args):
parser = argparse.ArgumentParser(description="EVF eval")
parser.add_argument("--version")
parser.add_argument(
"--precision",
default="fp16",
type=str,
choices=["fp32", "bf16", "fp16"],
help="precision for inference",
)
parser.add_argument("--image_size", default=224, type=int, help="image size")
parser.add_argument("--val_dataset", default="refcoco|unc|testA", type=str,
choices=["refcoco|unc|val", "refcoco|unc|testA", "refcoco|unc|testB",
"refcoco+|unc|val", "refcoco+|unc|testA", "refcoco+|unc|testB",
"refcocog|umd|val", "refcocog|umd|test"])
parser.add_argument("--dataset_dir", default="./dataset", type=str)
parser.add_argument("--local_rank", default=0, type=int, help="node rank")
parser.add_argument("--load_in_8bit", action="store_true", default=False)
parser.add_argument("--load_in_4bit", action="store_true", default=False)
parser.add_argument("--model_type", default="ori", choices=["ori", "effi", "sam2"])
return parser.parse_args(args)
def main(args):
args = parse_args(args)
dist.init_process_group('nccl', init_method="env://")
rank = int(os.environ["LOCAL_RANK"])
torch.cuda.set_device(rank)
# Create model
tokenizer = AutoTokenizer.from_pretrained(
args.version,
padding_side="right",
use_fast=False,
)
torch_dtype = torch.float32
if args.precision == "bf16":
torch_dtype = torch.bfloat16
elif args.precision == "fp16":
torch_dtype = torch.half
kwargs = {"torch_dtype": torch_dtype}
if args.load_in_4bit:
kwargs.update(
{
"torch_dtype": torch.half,
"quantization_config": BitsAndBytesConfig(
load_in_4bit=True,
bnb_4bit_compute_dtype=torch.float16,
bnb_4bit_use_double_quant=True,
bnb_4bit_quant_type="nf4",
llm_int8_skip_modules=["visual_model"],
),
}
)
elif args.load_in_8bit:
kwargs.update(
{
"torch_dtype": torch.half,
"quantization_config": BitsAndBytesConfig(
llm_int8_skip_modules=["visual_model"],
load_in_8bit=True,
),
}
)
if args.model_type=="ori":
from model.evf_sam import EvfSamModel
model = EvfSamModel.from_pretrained(args.version, low_cpu_mem_usage=True, **kwargs)
elif args.model_type=="sam2":
from model.evf_sam2 import EvfSam2Model
model = EvfSam2Model.from_pretrained(args.version, low_cpu_mem_usage=True, **kwargs)
elif args.model_type=="effi":
from model.evf_effisam import EvfEffiSamModel
model = EvfEffiSamModel.from_pretrained(args.version, low_cpu_mem_usage=True, **kwargs)
if (not args.load_in_4bit) and (not args.load_in_8bit):
model = model.cuda()
model = DistributedDataParallel(model, device_ids=[rank])
model.eval()
val_dataset = ValDataset(
args.dataset_dir,
args.val_dataset,
args.image_size,
model_type=args.model_type,
transform=ResizeLongestSide(1024) if args.model_type=="ori" else Resize(1024)
)
sampler = DistributedSampler(val_dataset, shuffle=False, drop_last=False, rank=rank)
val_loader = DataLoader(
val_dataset,
batch_size=1,
shuffle=False,
pin_memory=False,
sampler=sampler,
collate_fn=partial(
collate_fn,
tokenizer=tokenizer,
local_rank=rank,
),
)
args.log_dir = "runs"
os.makedirs(args.log_dir, exist_ok=True)
giou, ciou = validate(val_loader, model, args)
if rank==0:
print(args.val_dataset)
print("giou{:.3f}_ciou{:.3f}".format(giou, ciou))
dist.destroy_process_group()
def validate(val_loader, model, args):
intersection_meter = AverageMeter("Intersec", ":6.3f", Summary.SUM)
union_meter = AverageMeter("Union", ":6.3f", Summary.SUM)
acc_iou_meter = AverageMeter("gIoU", ":6.3f", Summary.SUM)
for input_dict in tqdm.tqdm(val_loader):
torch.cuda.empty_cache()
input_dict = dict_to_cuda(input_dict)
if args.precision == "fp16":
input_dict["images"] = input_dict["images"].half()
input_dict["images_evf"] = input_dict["images_evf"].half()
elif args.precision == "bf16":
input_dict["images"] = input_dict["images"].bfloat16()
input_dict["images_evf"] = input_dict["images_evf"].bfloat16()
else:
input_dict["images"] = input_dict["images"].float()
input_dict["images_evf"] = input_dict["images_evf"].float()
with torch.no_grad():
output_dict = model(**input_dict)
pred_masks = output_dict["pred_masks"]
masks_list = output_dict["gt_masks"][0].int()
output_list = (pred_masks[0] > 0).int()
assert len(pred_masks) == 1
intersection, union, acc_iou = 0.0, 0.0, 0.0
for mask_i, output_i in zip(masks_list, output_list):
intersection_i, union_i, _ = intersectionAndUnionGPU(
output_i.contiguous().clone(), mask_i.contiguous(), 2, ignore_index=255
)
intersection += intersection_i
union += union_i
acc_iou += intersection_i / (union_i + 1e-10)
acc_iou[union_i == 0] += 1.0 # no-object target
intersection, union = intersection.cpu().numpy(), union.cpu().numpy()
acc_iou = acc_iou.cpu().numpy() / masks_list.shape[0]
intersection_meter.update(intersection)
union_meter.update(union)
acc_iou_meter.update(acc_iou, n=masks_list.shape[0])
intersection_meter.all_reduce()
union_meter.all_reduce()
acc_iou_meter.all_reduce()
iou_class = intersection_meter.sum / (union_meter.sum + 1e-10)
ciou = iou_class[1]
giou = acc_iou_meter.avg[1]
return giou, ciou
if __name__ == "__main__":
main(sys.argv[1:])