File size: 10,971 Bytes
e290a7d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
import os
import time
from tqdm import tqdm
import cv2
import numpy as np
import torch
import pdb
import torch.cuda.amp as amp
import torch.distributed as dist
import torch.nn.functional as F
import wandb
from loguru import logger
from utils.dataset_verbonly import tokenize
from utils.misc import (AverageMeter, ProgressMeter, concat_all_gather,
                        trainMetricGPU)

## todo : add oIoU metric
def train(train_loader, model, optimizer, scheduler, scaler, epoch,  args):
    # torch.autograd.set_detect_anomaly(True)
    batch_time = AverageMeter('Batch', ':2.2f')
    data_time = AverageMeter('Data', ':2.2f')
    lr = AverageMeter('Lr', ':1.6f')
    loss_meter = AverageMeter('Loss', ':2.4f')
    iou_meter = AverageMeter('IoU', ':2.2f')
    pr_meter = AverageMeter('Prec@50', ':2.2f')
    progress = ProgressMeter(
        len(train_loader),
        [batch_time, data_time, lr, loss_meter, iou_meter, pr_meter],
        prefix="Training: Epoch=[{}/{}] ".format(epoch, args.epochs))


    model.train()
    time.sleep(2)
    end = time.time()

    # size_list = [320, 352, 384, 416, 448, 480, 512]
    # idx = np.random.choice(len(size_list))
    # new_size = size_list[idx]

    for i, (image, text, target, hardpos, params) in enumerate(train_loader):
        data_time.update(time.time() - end)

        # data
        image = image.cuda(non_blocking=True)
        text = text.cuda(non_blocking=True)
        target = target.cuda(non_blocking=True).unsqueeze(1)
        hardpos = hardpos.cuda(non_blocking=True)
        hp_emb = params['hardpos_emb'].cuda(non_blocking=True)

        with amp.autocast():
            pred, target, loss = model(image, text, target, hardpos, hp_emb) # , fq, vis, word, state

   
        # backward
        optimizer.zero_grad()
        # scaler.scale(loss).backward()
        scaler.scale(loss).backward()
        # loss.backward()

        # for name, param in model.named_parameters():
        #     if param.grad is not None:
        #         if torch.isinf(param.grad).any() or torch.isnan(param.grad).any():
        #             print(f"Inf/NaN in gradients: {name}")
        # for name, param in model.named_parameters():
        #     if param.grad is not None:
        #         grad_norm = param.grad.norm()
        #         if torch.isnan(grad_norm):
        #             print(f"NaN gradient detected in {name}")

        if args.max_norm:
            torch.nn.utils.clip_grad_norm_(model.parameters(), args.max_norm)

        # optimizer.step()
        # scheduler.step()
        scaler.step(optimizer)
        scaler.update()

        # metric
        iou, pr5 = trainMetricGPU(pred, target, 0.35, 0.5)
        dist.all_reduce(loss.detach())
        dist.all_reduce(iou)
        dist.all_reduce(pr5)
        loss = loss / dist.get_world_size()
        iou = iou / dist.get_world_size()
        pr5 = pr5 / dist.get_world_size()

        loss_meter.update(loss.item(), image.size(0))
        iou_meter.update(iou.item(), image.size(0))
        pr_meter.update(pr5.item(), image.size(0))
        lr.update(scheduler.get_last_lr()[-1])
        batch_time.update(time.time() - end)
        end = time.time()

        # if (i + 1) % args.print_freq == 0:
        #     progress.display(i + 1)
        #     if dist.get_rank() in [-1, 0]:
        #         wandb.log(
        #             {
        #                 "time/batch": batch_time.val,
        #                 "time/data": data_time.val,
        #                 "training/lr": lr.val,
        #                 "training/loss": loss_meter.val,
        #                 "training/iou": iou_meter.val,
        #                 "training/prec@50": pr_meter.val,
        #             },
        #             step=epoch * len(train_loader) + (i + 1))


@torch.no_grad()
def validate(val_loader, model, epoch, args):
    iou_list = []
    I_list = []
    U_list = []
    model.eval()
    time.sleep(2)
    for imgs, texts, masks, param in val_loader:
        # data
        imgs = imgs.cuda(non_blocking=True)
        texts = texts.cuda(non_blocking=True)
        # inference
        preds = model(imgs, texts)
        preds = torch.sigmoid(preds)
        if preds.shape[-2:] != imgs.shape[-2:]:
            preds = F.interpolate(preds,
                                  size=imgs.shape[-2:],
                                  mode='bicubic',
                                  align_corners=True).squeeze(1)
        # process one batch
        # for pred, mask_dir, mat, ori_size in zip(preds, param['mask_dir'],
        #                                          param['inverse'],
        #                                          param['ori_size']):
        #     h, w = np.array(ori_size)
        #     mat = np.array(mat)
        #     pred = pred.cpu().numpy()
        #     pred = cv2.warpAffine(pred, mat, (w, h),
        #                           flags=cv2.INTER_CUBIC,
        #                           borderValue=0.)
        #     pred = np.array(pred > 0.35)
        #     mask = cv2.imread(mask_dir, flags=cv2.IMREAD_GRAYSCALE)
        #     mask = mask / 255.
        #     # iou
        #     inter = np.logical_and(pred, mask)
        #     union = np.logical_or(pred, mask)
        #     iou = np.sum(inter) / (np.sum(union) + 1e-6)
        #     iou_list.append(iou)
        #     I_list.append(inter)
        #     U_list.append(union)
        for pred, mask in zip(preds, masks):
            # h, w = np.array(ori_size)
            # mat = np.array(mat)
            pred = pred.cpu().numpy()
            # pred = cv2.warpAffine(pred, mat, (w, h),
            #                       flags=cv2.INTER_CUBIC,
            #                       borderValue=0.)
            pred = np.array(pred > 0.35)
            # mask = cv2.imread(mask_dir, flags=cv2.IMREAD_GRAYSCALE)
            # mask = mask / 255.
            mask = mask.numpy()
            # iou
            inter = np.logical_and(pred, mask)
            union = np.logical_or(pred, mask)
            iou = np.sum(inter) / (np.sum(union) + 1e-6)
            I_list.append(inter)
            U_list.append(union)
            iou_list.append(iou)

    iou_list = np.stack(iou_list)
    iou_list = torch.from_numpy(iou_list).to(imgs.device)
    iou_list = concat_all_gather(iou_list)
    
    I_list = np.stack(I_list)
    I_list = torch.from_numpy(I_list).to(imgs.device)
    I_list = concat_all_gather(I_list)
 
    U_list = np.stack(U_list)
    U_list = torch.from_numpy(U_list).to(imgs.device)
    U_list = concat_all_gather(U_list)

    overall_I = I_list.sum().item()
    overall_U = U_list.sum().item()
    overall_IoU = overall_I / (overall_U + 1e-6)  # to avoid division by zero

    
    prec_list = []
    for thres in torch.arange(0.5, 1.0, 0.1):
        tmp = (iou_list > thres).float().mean()
        prec_list.append(tmp)
    iou = iou_list.mean()
    prec = {}
    temp = '  '
    for i, thres in enumerate(range(5, 10)):
        key = 'Pr@{}'.format(thres * 10)
        value = prec_list[i].item()
        prec[key] = value
        temp += "{}: {:.2f}  ".format(key, 100. * value)
    head = 'Evaluation: Epoch=[{}/{}]  IoU={:.2f}  OIoU={:.4f}'.format(
        epoch, args.epochs, 100. * iou.item(), 100. * overall_IoU)
    logger.info(head + temp)
    # print(head)
    
    # return three results : mIoU, oIoU and prec results
    return iou.item(), overall_IoU, prec


@torch.no_grad()
def inference(test_loader, model, args):
    iou_list = []
    I_list = []
    U_list = []

    tbar = tqdm(test_loader, desc='Inference:', ncols=100)
    model.eval()
    time.sleep(2)
    for img, mask, param in tbar:
        # data
        # img = img.cuda(non_blocking=True)
        # mask = cv2.imread(param['mask_dir'][0], flags=cv2.IMREAD_GRAYSCALE)
        img = img.cuda(non_blocking=True)
        mask = mask[0].cpu().numpy()
        
        # dump image & mask
        if args.visualize:
            seg_id = param['seg_id'][0].cpu().numpy()
            img_name = '{}-img.jpg'.format(seg_id)
            mask_name = '{}-mask.png'.format(seg_id)
            cv2.imwrite(filename=os.path.join(args.vis_dir, img_name),
                        img=param['ori_img'][0].cpu().numpy())
            cv2.imwrite(filename=os.path.join(args.vis_dir, mask_name),
                        img=mask)
        # multiple sentences
        for sent in param['sents']:
            # mask = mask / 255.
            text = tokenize(sent, args.word_len, True)
            text = text.cuda(non_blocking=True)
            # inference
            pred = model(img, text)
            pred = torch.sigmoid(pred)
            if pred.shape[-2:] != img.shape[-2:]:
                pred = F.interpolate(pred,
                                     size=img.shape[-2:],
                                     mode='bicubic',
                                     align_corners=True).squeeze()
            # process one sentence
            # h, w = param['ori_size'].numpy()[0]
            # mat = param['inverse'].numpy()[0]
            pred = pred.cpu().numpy()
            # pred = cv2.warpAffine(pred, mat, (w, h),
            #                       flags=cv2.INTER_CUBIC,
            #                       borderValue=0.)
            pred = np.array(pred > 0.35)
            # iou
            inter = np.logical_and(pred, mask)
            union = np.logical_or(pred, mask)
            iou = np.sum(inter) / (np.sum(union) + 1e-6)
            iou_list.append(iou)
            I_list.append(inter)
            U_list.append(union)
            # dump prediction
            if args.visualize:
                pred = np.array(pred*255, dtype=np.uint8)
                sent = "_".join(sent[0].split(" "))
                pred_name = '{}-iou={:.2f}-{}.png'.format(seg_id, iou*100, sent)
                cv2.imwrite(filename=os.path.join(args.vis_dir, pred_name),
                            img=pred)
    logger.info('=> Metric Calculation <=')
    iou_list = np.stack(iou_list)
    iou_list = torch.from_numpy(iou_list).to(img.device)

    I_list = np.stack(I_list)
    I_list = torch.from_numpy(I_list).to(img.device)
    U_list = np.stack(U_list)
    U_list = torch.from_numpy(U_list).to(img.device)
    overall_I = I_list.sum().item()
    overall_U = U_list.sum().item()
    overall_IoU = overall_I / (overall_U + 1e-6)  # to avoid division by zero

    prec_list = []
    for thres in torch.arange(0.5, 1.0, 0.1):
        tmp = (iou_list > thres).float().mean()
        prec_list.append(tmp)
    iou = iou_list.mean()
    prec = {}
    for i, thres in enumerate(range(5, 10)):
        key = 'Pr@{}'.format(thres*10)
        value = prec_list[i].item()
        prec[key] = value
    logger.info('IoU={:.2f}  OIoU={:.4f}'.format(100.*iou.item(), 100. * overall_IoU))
    print('IoU={:.2f}  OIoU={:.4f}'.format(100.*iou.item(), 100. * overall_IoU))
    for k, v in prec.items():
        logger.info('{}: {:.2f}.'.format(k, 100.*v))

    return iou.item(), overall_IoU, prec