-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_single.py
736 lines (601 loc) · 31.8 KB
/
train_single.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
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
# Copyright by DrSAM team.
# All rights reserved.
# Reference from SAM and HQ-SAM, thanks to them.
import os
import argparse
import numpy as np
import torch
import torch.optim as optim
import torch.nn as nn
import torch.nn.functional as F
import matplotlib.pyplot as plt
import random
from typing import List, Tuple
from segment_anything import sam_model_registry
from segment_anything.modeling import TwoWayTransformer, MaskDecoder
from utils.dataloader import get_im_gt_name_dict, create_dataloaders, RandomHFlip, Resize, LargeScaleJitter
from utils.loss_mask import loss_masks
import utils.misc as misc
class LayerNorm2d(nn.Module):
def __init__(self, num_channels: int, eps: float = 1e-6) -> None:
super().__init__()
self.weight = nn.Parameter(torch.ones(num_channels))
self.bias = nn.Parameter(torch.zeros(num_channels))
self.eps = eps
def forward(self, x: torch.Tensor) -> torch.Tensor:
u = x.mean(1, keepdim=True)
s = (x - u).pow(2).mean(1, keepdim=True)
x = (x - u) / torch.sqrt(s + self.eps)
x = self.weight[:, None, None] * x + self.bias[:, None, None]
return x
class MLP(nn.Module):
def __init__(
self,
input_dim: int,
hidden_dim: int,
output_dim: int,
num_layers: int,
sigmoid_output: bool = False,
) -> None:
super().__init__()
self.num_layers = num_layers
h = [hidden_dim] * (num_layers - 1)
self.layers = nn.ModuleList(
nn.Linear(n, k) for n, k in zip([input_dim] + h, h + [output_dim])
)
self.sigmoid_output = sigmoid_output
def forward(self, x):
for i, layer in enumerate(self.layers):
x = F.relu(layer(x)) if i < self.num_layers - 1 else layer(x)
if self.sigmoid_output:
x = F.sigmoid(x)
return x
class DoubleConv(nn.Sequential):
def __init__(self, in_channels, out_channels, mid_channels=None):
if mid_channels is None:
mid_channels = out_channels
super(DoubleConv, self).__init__(
nn.Conv2d(in_channels, mid_channels, kernel_size=3, padding=1, bias=False),
nn.BatchNorm2d(mid_channels),
nn.ReLU(inplace=True),
nn.Conv2d(mid_channels, out_channels, kernel_size=3, padding=1, bias=False),
nn.BatchNorm2d(out_channels),
nn.ReLU(inplace=True)
)
class Up(nn.Module):
def __init__(self, in_channels, out_channels, bilinear=True):
super(Up, self).__init__()
if bilinear:
self.up = nn.Upsample(scale_factor=2, mode='bilinear', align_corners=True)
self.conv = DoubleConv(in_channels, out_channels, in_channels // 2)
else:
self.up = nn.ConvTranspose2d(in_channels, in_channels // 2, kernel_size=2, stride=2)
self.conv = DoubleConv(in_channels, out_channels)
self.conv1 = DoubleConv(in_channels + in_channels//4, out_channels)
def forward(self, x1: torch.Tensor, x2: torch.Tensor, x3=None) -> torch.Tensor:
x1 = self.up(x1)
# [N, C, H, W]
diff_y = x2.size()[2] - x1.size()[2]
diff_x = x2.size()[3] - x1.size()[3]
# padding_left, padding_right, padding_top, padding_bottom
x1 = F.pad(x1, [diff_x // 2, diff_x - diff_x // 2,
diff_y // 2, diff_y - diff_y // 2])
if x3 is not None:
x = torch.cat([x3, x2, x1], dim=1)
x = self.conv1(x)
else:
x = torch.cat([x2, x1], dim=1)
x = self.conv(x)
return x
class DrMaskDecoder(MaskDecoder):
def __init__(self, model_type, bilinear:bool = False):
super().__init__(transformer_dim=256,
transformer=TwoWayTransformer(
depth=2,
embedding_dim=256,
mlp_dim=2048,
num_heads=8,
),
num_multimask_outputs=3,
activation=nn.GELU,
iou_head_depth= 3,
iou_head_hidden_dim= 256,)
assert model_type in ["vit_b","vit_l","vit_h"]
checkpoint_dict = {"vit_b":"pretrained_checkpoint/sam_vit_b_maskdecoder.pth",
"vit_l":"pretrained_checkpoint/sam_vit_l_maskdecoder.pth",
'vit_h':"pretrained_checkpoint/sam_vit_h_maskdecoder.pth"}
checkpoint_path = checkpoint_dict[model_type]
self.load_state_dict(torch.load(checkpoint_path))
print("Dr-SAM init from SAM MaskDecoder")
for n, p in self.named_parameters():
p.requires_grad = False
transformer_dim = 256
vit_dim_dict = {"vit_b": 768, "vit_l": 1024, "vit_h": 1280}
vit_dim = vit_dim_dict[model_type]
self.hf_token = nn.Embedding(1, transformer_dim)
self.hf_mlp = MLP(transformer_dim, transformer_dim, transformer_dim // 8, 3)
self.num_mask_tokens = self.num_mask_tokens + 1
self.compress_vit_feat = nn.Sequential(
nn.ConvTranspose2d(vit_dim, transformer_dim, kernel_size=2, stride=2),
LayerNorm2d(transformer_dim),
nn.GELU(),
nn.ConvTranspose2d(transformer_dim, transformer_dim // 8, kernel_size=2, stride=2))
self.embedding_encoder = nn.Sequential(
nn.ConvTranspose2d(transformer_dim, transformer_dim // 4, kernel_size=2, stride=2),
LayerNorm2d(transformer_dim // 4),
nn.GELU(),
nn.ConvTranspose2d(transformer_dim // 4, transformer_dim // 8, kernel_size=2, stride=2),
)
self.embedding_maskfeature = nn.Sequential(
nn.Conv2d(transformer_dim // 8, transformer_dim // 4, 3, 1, 1),
LayerNorm2d(transformer_dim // 4),
nn.GELU(),
nn.Conv2d(transformer_dim // 4, transformer_dim // 8, 3, 1, 1))
self.up_bilinear2 = nn.ConvTranspose2d(vit_dim, transformer_dim // 2, kernel_size=2, stride=2)
self.up_bilinear4 = nn.ConvTranspose2d(vit_dim, transformer_dim // 4, kernel_size=4, stride=4)
self.up_bilinear8 = nn.ConvTranspose2d(vit_dim, transformer_dim // 8, kernel_size=8, stride=8)
factor = 2 if bilinear else 1
self.up1 = Up(transformer_dim, transformer_dim // 2 // factor, bilinear)
self.up2 = Up(transformer_dim // 2, transformer_dim // 4 // factor, bilinear)
self.up3 = Up(transformer_dim // 4, transformer_dim // 8, bilinear)
def forward(
self,
image_embeddings: torch.Tensor,
image_pe: torch.Tensor,
sparse_prompt_embeddings: torch.Tensor,
dense_prompt_embeddings: torch.Tensor,
multimask_output: bool,
med_token_only: bool = False,
hierarchical_embeddings: torch.Tensor = None,
) -> Tuple[torch.Tensor, torch.Tensor]:
"""
Predict masks given image and prompt embeddings.
Arguments:
image_embeddings (torch.Tensor): the embeddings from the ViT image encoder
image_pe (torch.Tensor): positional encoding with the shape of image_embeddings
sparse_prompt_embeddings (torch.Tensor): the embeddings of the points and boxes
dense_prompt_embeddings (torch.Tensor): the embeddings of the mask inputs
multimask_output (bool): Whether to return multiple masks or a single
mask.
Returns:
torch.Tensor: batched predicted DrSAM masks
"""
x1 = hierarchical_embeddings[0].permute(0, 3, 1, 2)
x1 = self.up_bilinear8(x1) # 512 dim64
x2 = hierarchical_embeddings[1].permute(0, 3, 1, 2)
x2 = self.up_bilinear4(x2) # 256 dim128
x3 = hierarchical_embeddings[2].permute(0, 3, 1, 2)
x3 = self.up_bilinear2(x3) # 128 dim256
batch_len = len(image_embeddings)
masks = []
iou_preds = []
for i_batch in range(batch_len):
mask, iou_pred = self.predict_masks(x3=x3[i_batch].unsqueeze(0), x2=x2[i_batch].unsqueeze(0), x1=x1[i_batch].unsqueeze(0),
image_embeddings=image_embeddings[i_batch].unsqueeze(0),
image_pe=image_pe[i_batch],
sparse_prompt_embeddings=sparse_prompt_embeddings[i_batch],
dense_prompt_embeddings=dense_prompt_embeddings[i_batch],
)
masks.append(mask)
iou_preds.append(iou_pred)
masks = torch.cat(masks,0)
iou_preds = torch.cat(iou_preds,0)
# Select the correct mask or masks for output
if multimask_output:
# mask with highest score
mask_slice = slice(1,self.num_mask_tokens-1)
iou_preds = iou_preds[:, mask_slice]
iou_preds, max_iou_idx = torch.max(iou_preds,dim=1)
iou_preds = iou_preds.unsqueeze(1)
masks_multi = masks[:, mask_slice, :, :]
masks_sam = masks_multi[torch.arange(masks_multi.size(0)),max_iou_idx].unsqueeze(1)
else:
# singale mask output, default
mask_slice = slice(0, 1)
masks_sam = masks[:,mask_slice]
masks_dr = masks[:,slice(self.num_mask_tokens-1, self.num_mask_tokens), :, :]
if med_token_only:
return masks_dr
else:
return masks_sam, masks_dr
def predict_masks(
self,
image_embeddings: torch.Tensor,
image_pe: torch.Tensor,
sparse_prompt_embeddings: torch.Tensor,
dense_prompt_embeddings: torch.Tensor,
x3: torch.Tensor = None, x2: torch.Tensor = None, x1: torch.Tensor = None,
) -> Tuple[torch.Tensor, torch.Tensor]:
"""Predicts masks. See 'forward' for more details."""
output_tokens = torch.cat([self.iou_token.weight, self.mask_tokens.weight, self.hf_token.weight], dim=0)
output_tokens = output_tokens.unsqueeze(0).expand(sparse_prompt_embeddings.size(0), -1, -1)
tokens = torch.cat((output_tokens, sparse_prompt_embeddings), dim=1)
# Expand per-image data in batch direction to be per-mask
src = torch.repeat_interleave(image_embeddings, tokens.shape[0], dim=0)
src = src + dense_prompt_embeddings
pos_src = torch.repeat_interleave(image_pe, tokens.shape[0], dim=0)
b, c, h, w = src.shape
# Run the transformer
hs, src = self.transformer(src, pos_src, tokens)
iou_token_out = hs[:, 0, :]
mask_tokens_out = hs[:, 1 : (1 + self.num_mask_tokens), :]
# Upscale mask embeddings and predict masks using the mask tokens
src = src.transpose(1, 2).view(b, c, h, w)
x4 = src # 64 dim256
upscaled_embedding_sam = self.output_upscaling(src) # dim256
x = self.up1(x4, x3) # 128 x4dim256 x3dim128
x = self.up2(x, x2, upscaled_embedding_sam) # 256 or (upscaled_embedding_sam, x2)
x = self.up3(x, x1) # 512
upscaled_embedding_ours = x
hyper_in_list: List[torch.Tensor] = []
for i in range(self.num_mask_tokens):
if i < 4:
hyper_in_list.append(self.output_hypernetworks_mlps[i](mask_tokens_out[:, i, :]))
else:
hyper_in_list.append(self.hf_mlp(mask_tokens_out[:, i, :]))
hyper_in = torch.stack(hyper_in_list, dim=1)
b, c, h, w = upscaled_embedding_sam.shape
b1, c1, h1, w1 = upscaled_embedding_ours.shape
masks_sam = (hyper_in[:,:4] @ upscaled_embedding_sam.view(b, c, h * w)).view(b, -1, h, w)
masks_sam = F.interpolate(masks_sam, size=(512, 512), mode='bilinear')
masks_ours = (hyper_in[:,4:] @ upscaled_embedding_ours.view(b1, c1, h1 * w1)).view(b1, -1, h1, w1)
masks = torch.cat([masks_sam,masks_ours],dim=1)
iou_pred = self.iou_prediction_head(iou_token_out)
return masks, iou_pred
def show_anns(masks, input_point, input_box, input_label, filename, image, ious, boundary_ious):
if len(masks) == 0:
return
for i, (mask, iou, biou) in enumerate(zip(masks, ious, boundary_ious)):
plt.figure(figsize=(10,10))
plt.imshow(image)
show_mask(mask, plt.gca())
if input_box is not None:
show_box(input_box, plt.gca())
if (input_point is not None) and (input_label is not None):
show_points(input_point, input_label, plt.gca())
plt.axis('off')
plt.savefig(filename+'_'+str(i)+'.png',bbox_inches='tight',pad_inches=-0.1)
plt.close()
def show_mask(mask, ax, random_color=False):
if random_color:
color = np.concatenate([np.random.random(3), np.array([0.6])], axis=0)
else:
color = np.array([30/255, 144/255, 255/255, 0.6])
h, w = mask.shape[-2:]
mask_image = mask.reshape(h, w, 1) * color.reshape(1, 1, -1)
ax.imshow(mask_image)
def show_points(coords, labels, ax, marker_size=375):
pos_points = coords[labels==1]
neg_points = coords[labels==0]
ax.scatter(pos_points[:, 0], pos_points[:, 1], color='green', marker='*', s=marker_size, edgecolor='white', linewidth=1.25)
ax.scatter(neg_points[:, 0], neg_points[:, 1], color='red', marker='*', s=marker_size, edgecolor='white', linewidth=1.25)
def show_box(box, ax):
x0, y0 = box[0], box[1]
w, h = box[2] - box[0], box[3] - box[1]
ax.add_patch(plt.Rectangle((x0, y0), w, h, edgecolor='green', facecolor=(0,0,0,0), lw=2))
def get_args_parser():
parser = argparse.ArgumentParser('DrSAM', add_help=False)
parser.add_argument("--output", type=str, default='work_dirs/DrSAM_b',
help="Path to the directory where masks and checkpoints will be output")
parser.add_argument("--model-type", type=str, default="vit_b",
help="The type of model to load, in ['vit_h', 'vit_l', 'vit_b']")
parser.add_argument("--checkpoint", type=str, default='./pretrained_checkpoint/sam_vit_b_01ec64.pth',
help="The path to the SAM checkpoint to use for mask generation.")
parser.add_argument("--device", type=str, default="cuda",
help="The device to run generation on.")
parser.add_argument('--seed', default=0, type=int)
parser.add_argument('--learning_rate', default=1e-3, type=float)
parser.add_argument('--start_epoch', default=0, type=int)
parser.add_argument('--lr_drop_epoch', default=3, type=int)
parser.add_argument('--max_epoch_num', default=12, type=int)
parser.add_argument('--input_size', default=[1024, 1024], type=list)
parser.add_argument('--batch_size_train', default=6, type=int)
parser.add_argument('--batch_size_valid', default=1, type=int)
parser.add_argument('--model_save_fre', default=1, type=int)
parser.add_argument('--world_size', default=1, type=int,
help='number of distributed processes')
parser.add_argument('--dist_url', default='env://', help='url used to set up distributed training')
parser.add_argument('--rank', default=0, type=int,
help='number of distributed processes')
parser.add_argument('--local_rank', type=int, help='local rank for dist')
parser.add_argument('--find_unused_params', action='store_true')
# if you want to eval or visulize
parser.add_argument('--eval', default=False)
parser.add_argument('--visualize', default=False)
parser.add_argument("--restore-model", type=str, default='work_dirs/DrSAM_b/epoch_1.pth',
help="The path to the hq_decoder training checkpoint for evaluation")
return parser.parse_args()
def main(net, train_datasets, valid_datasets, args):
torch.manual_seed(args.seed)
np.random.seed(args.seed)
random.seed(args.seed)
if not args.eval:
print("--- create training dataloader ---")
train_im_gt_list = get_im_gt_name_dict(train_datasets, flag="train")
train_dataloaders, train_datasets = create_dataloaders(train_im_gt_list,
my_transforms = [
RandomHFlip(),
LargeScaleJitter()
],
batch_size = args.batch_size_train,
training = True)
print(len(train_dataloaders), " train dataloaders created")
print("--- create valid dataloader ---")
valid_im_gt_list = get_im_gt_name_dict(valid_datasets, flag="valid")
valid_dataloaders, valid_datasets = create_dataloaders(valid_im_gt_list,
my_transforms = [
Resize(args.input_size)
],
batch_size=args.batch_size_valid,
training=False)
print(len(valid_dataloaders), " valid dataloaders created")
if torch.cuda.is_available():
net.cuda()
if not args.eval:
print("--- define optimizer ---")
optimizer = optim.AdamW(net.parameters(), lr=args.learning_rate, betas=(0.9, 0.999), eps=1e-08, weight_decay=0.01)
lr_scheduler = torch.optim.lr_scheduler.StepLR(optimizer, args.lr_drop_epoch)
lr_scheduler.last_epoch = args.start_epoch
train(args, net, optimizer, train_dataloaders, valid_dataloaders, lr_scheduler)
else:
sam = sam_model_registry[args.model_type](checkpoint=args.checkpoint)
_ = sam.to(device=args.device)
if args.restore_model:
print("restore model from:", args.restore_model)
if torch.cuda.is_available():
net.load_state_dict(torch.load(args.restore_model))
else:
net.load_state_dict(torch.load(args.restore_model,map_location="cpu"))
evaluate(args, net, sam, valid_dataloaders, args.visualize)
def train(args, net, optimizer, train_dataloaders, valid_dataloaders, lr_scheduler):
os.makedirs(args.output, exist_ok=True)
epoch_start = args.start_epoch
epoch_num = args.max_epoch_num
net.train()
_ = net.to(device=args.device)
sam = sam_model_registry[args.model_type](checkpoint=args.checkpoint)
_ = sam.to(device=args.device)
for epoch in range(epoch_start,epoch_num):
print("epoch: ",epoch, " learning rate: ", optimizer.param_groups[0]["lr"])
metric_logger = misc.MetricLogger(delimiter=" ")
for data in metric_logger.log_every(train_dataloaders,100):
inputs, labels = data['image'], data['label']
inputs = inputs.to(device=args.device)
labels = labels.to(device=args.device)
imgs = inputs.permute(0, 2, 3, 1).cpu().numpy()
# input prompt
input_keys = ['box','point','noise_mask']
labels_box = misc.masks_to_boxes(labels[:,0,:,:])
try:
labels_points = misc.masks_sample_points(labels[:,0,:,:])
except:
# less than 10 points
input_keys = ['box','noise_mask']
labels_256 = F.interpolate(labels, size=(256, 256), mode='bilinear')
labels_noisemask = misc.masks_noise(labels_256)
batched_input = []
for b_i in range(len(imgs)):
dict_input = dict()
input_image = torch.as_tensor(imgs[b_i].astype(dtype=np.uint8), device=sam.device).permute(2, 0, 1).contiguous()
dict_input['image'] = input_image
input_type = random.choice(input_keys)
if input_type == 'box':
dict_input['boxes'] = labels_box[b_i:b_i+1]
elif input_type == 'point':
point_coords = labels_points[b_i:b_i+1]
dict_input['point_coords'] = point_coords
dict_input['point_labels'] = torch.ones(point_coords.shape[1], device=point_coords.device)[None,:]
elif input_type == 'noise_mask':
dict_input['mask_inputs'] = labels_noisemask[b_i:b_i+1]
else:
raise NotImplementedError
dict_input['original_size'] = imgs[b_i].shape[:2]
batched_input.append(dict_input)
with torch.no_grad():
batched_output, hierarchical_embeddings = sam(batched_input, multimask_output=False)
batch_len = len(batched_output)
encoder_embedding = torch.cat([batched_output[i_l]['encoder_embedding'] for i_l in range(batch_len)], dim=0)
image_pe = [batched_output[i_l]['image_pe'] for i_l in range(batch_len)]
sparse_embeddings = [batched_output[i_l]['sparse_embeddings'] for i_l in range(batch_len)]
dense_embeddings = [batched_output[i_l]['dense_embeddings'] for i_l in range(batch_len)]
masks_dr = net(
image_embeddings=encoder_embedding,
image_pe=image_pe,
sparse_prompt_embeddings=sparse_embeddings,
dense_prompt_embeddings=dense_embeddings,
multimask_output=False,
med_token_only=True,
hierarchical_embeddings=hierarchical_embeddings,
)
loss_mask, loss_dice = loss_masks(masks_dr, labels/255.0, len(masks_dr))
loss = loss_mask + loss_dice
optimizer.zero_grad()
loss.backward()
optimizer.step()
metric_logger.update(training_loss=loss.item(), loss_mask=loss_mask.item(), loss_dice=loss_dice.item())
print("Finished epoch: ", epoch)
# metric_logger.synchronize_between_processes()
print("Averaged stats:", metric_logger)
train_stats = {k: meter.global_avg for k, meter in metric_logger.meters.items() if meter.count > 0}
lr_scheduler.step()
test_stats = evaluate(args, net, sam, valid_dataloaders)
train_stats.update(test_stats)
net.train()
if epoch % args.model_save_fre == 0:
model_name = "/epoch_"+str(epoch)+".pth"
print('come here save at', args.output + model_name)
misc.save_on_master(net.state_dict(), args.output + model_name)
# Finish training
print("Training Reaches The Maximum Epoch Number")
# merge sam and DrSAM
sam_ckpt = torch.load(args.checkpoint)
hq_decoder = torch.load(args.output + model_name)
for key in hq_decoder.keys():
sam_key = 'mask_decoder.'+key
if sam_key not in sam_ckpt.keys():
sam_ckpt[sam_key] = hq_decoder[key]
model_name = "/drsam_epoch_"+str(epoch)+".pth"
torch.save(sam_ckpt, args.output + model_name)
def compute_iou(preds, target):
assert target.shape[1] == 1, 'only support one mask per image now'
if(preds.shape[2]!=target.shape[2] or preds.shape[3]!=target.shape[3]):
postprocess_preds = F.interpolate(preds, size=target.size()[2:], mode='bilinear', align_corners=False)
else:
postprocess_preds = preds
iou = 0
for i in range(0,len(preds)):
iou = iou + misc.mask_iou(postprocess_preds[i],target[i])
return iou / len(preds)
def compute_boundary_iou(preds, target):
assert target.shape[1] == 1, 'only support one mask per image now'
if(preds.shape[2]!=target.shape[2] or preds.shape[3]!=target.shape[3]):
postprocess_preds = F.interpolate(preds, size=target.size()[2:], mode='bilinear', align_corners=False)
else:
postprocess_preds = preds
iou = 0
for i in range(0,len(preds)):
iou = iou + misc.boundary_iou(target[i],postprocess_preds[i])
return iou / len(preds)
def dice_coefficient(pred_label, label):
pred_label = (pred_label > 0).int()
label = (label > 128).int()
intersection = torch.sum(pred_label * label)
union = torch.sum(pred_label) + torch.sum(label)
dice = (2.0 * intersection) / (union + 1e-5) # 添加一个小的常数以避免除零错误
return dice
def compute_dice(preds, target):
assert target.shape[1] == 1, '只支持每张图像一个掩码'
if preds.shape[2] != target.shape[2] or preds.shape[3] != target.shape[3]:
postprocess_preds = torch.nn.functional.interpolate(preds, size=target.size()[2:], mode='bilinear',
align_corners=False)
else:
postprocess_preds = preds
dice = 0.0
for i in range(len(preds)):
dice += dice_coefficient(postprocess_preds[i], target[i])
return dice / len(preds)
def evaluate(args, net, sam, valid_dataloaders, visualize=False):
net.eval()
print("Validating...")
test_stats = {}
for k in range(len(valid_dataloaders)):
metric_logger = misc.MetricLogger(delimiter=" ")
valid_dataloader = valid_dataloaders[k]
print('valid_dataloader len:', len(valid_dataloader))
for data_val in metric_logger.log_every(valid_dataloader,100):
imidx_val, inputs_val, labels_val, shapes_val, labels_ori = data_val['imidx'], data_val['image'], data_val['label'], data_val['shape'], data_val['ori_label']
if torch.cuda.is_available():
inputs_val = inputs_val.cuda()
labels_val = labels_val.cuda()
labels_ori = labels_ori.cuda()
imgs = inputs_val.permute(0, 2, 3, 1).cpu().numpy()
labels_box = misc.masks_to_boxes(labels_val[:,0,:,:])
input_keys = ['box']
batched_input = []
for b_i in range(len(imgs)):
dict_input = dict()
input_image = torch.as_tensor(imgs[b_i].astype(dtype=np.uint8), device=sam.device).permute(2, 0, 1).contiguous()
dict_input['image'] = input_image
input_type = random.choice(input_keys)
if input_type == 'box':
dict_input['boxes'] = labels_box[b_i:b_i+1]
elif input_type == 'point':
point_coords = labels_points[b_i:b_i+1]
dict_input['point_coords'] = point_coords
dict_input['point_labels'] = torch.ones(point_coords.shape[1], device=point_coords.device)[None,:]
elif input_type == 'noise_mask':
dict_input['mask_inputs'] = labels_noisemask[b_i:b_i+1]
else:
raise NotImplementedError
dict_input['original_size'] = imgs[b_i].shape[:2]
batched_input.append(dict_input)
with torch.no_grad():
batched_output, hierarchical_embeddings = sam(batched_input, multimask_output=False)
batch_len = len(batched_output)
encoder_embedding = torch.cat([batched_output[i_l]['encoder_embedding'] for i_l in range(batch_len)], dim=0)
image_pe = [batched_output[i_l]['image_pe'] for i_l in range(batch_len)]
sparse_embeddings = [batched_output[i_l]['sparse_embeddings'] for i_l in range(batch_len)]
dense_embeddings = [batched_output[i_l]['dense_embeddings'] for i_l in range(batch_len)]
masks_sam, masks_dr = net(
image_embeddings=encoder_embedding,
image_pe=image_pe,
sparse_prompt_embeddings=sparse_embeddings,
dense_prompt_embeddings=dense_embeddings,
multimask_output=False,
med_token_only=False,
hierarchical_embeddings=hierarchical_embeddings,
)
iou = compute_iou(masks_dr, labels_ori)
boundary_iou = compute_boundary_iou(masks_dr, labels_ori)
dice = compute_dice(masks_dr, labels_ori)
if visualize:
print("visualize")
os.makedirs(args.output, exist_ok=True)
masks_hq_vis = (F.interpolate(masks_dr.detach(), (1024, 1024), mode="bilinear", align_corners=False) > 0).cpu()
for ii in range(len(imgs)):
base = data_val['imidx'][ii].item()
print('base:', base)
save_base = os.path.join(args.output, str(k)+'_'+ str(base))
imgs_ii = imgs[ii].astype(dtype=np.uint8)
show_iou = torch.tensor([iou.item()])
show_boundary_iou = torch.tensor([boundary_iou.item()])
show_anns(masks_hq_vis[ii], None, labels_box[ii].cpu(), None, save_base , imgs_ii, show_iou, show_boundary_iou)
loss_dict = {"val_iou_"+str(k): iou, "val_boundary_iou_"+str(k): boundary_iou, "val_dice_"+str(k): dice}
loss_dict_reduced = misc.reduce_dict(loss_dict)
metric_logger.update(**loss_dict_reduced)
print('============================')
# gather the stats from all processes
metric_logger.synchronize_between_processes()
print("Averaged stats:", metric_logger)
resstat = {k: meter.global_avg for k, meter in metric_logger.meters.items() if meter.count > 0}
test_stats.update(resstat)
return test_stats
if __name__ == "__main__":
### --------------- Configuring the Train and Valid datasets ---------------
dataset_Kvasir = {"name": "Kvasir_train",
"im_dir": "./data/hyper-kvasir-segmented-images/segmented-images/images",
"gt_dir": "./data/hyper-kvasir-segmented-images/segmented-images/masks",
"im_ext": ".jpg",
"gt_ext": ".jpg"}
dataset_CHASE = {"name": "CHASE_train",
"im_dir": "./data/CHASEDB1",
"gt_dir": "./data/CHASEDB1anno1",
"im_ext": ".jpg",
"gt_ext": "_1stHO.png"}
dataset_cell = {"name": "cell_train",
"im_dir": "./data/cell/train_image",
"gt_dir": "./data/cell/train_mask",
"im_ext": ".png",
"gt_ext": ".png"}
# valid set
dataset_Kvasir_val = {"name": "Kvasir_val",
"im_dir": "./data/DrSAMevaluate/kvasirimage",
"gt_dir": "./data/DrSAMevaluate/kvasirmask",
"im_ext": ".jpg",
"gt_ext": ".jpg"}
dataset_CHASE_val = {"name": "CHASE_val",
"im_dir": "./data/DrSAMevaluate/chaseimage",
"gt_dir": "./data/DrSAMevaluate/chasemask",
"im_ext": ".jpg",
"gt_ext": "_1stHO.png"}
dataset_cell_val = {"name": "cell_val",
"im_dir": "./data/DrSAMevaluate/cellimage",
"gt_dir": "./data/DrSAMevaluate/cellmask",
"im_ext": ".png",
"gt_ext": ".png"}
# zero-shot test
dataset_isic_val = {"name": "isic_val",
"im_dir": "./data/DrSAMevaluate/ISIC2018image",
"gt_dir": "./data/DrSAMevaluate/ISIC2018mask",
"im_ext": ".jpg",
"gt_ext": "_segmentation.png"}
dataset_Warwick_val = {"name": "Warwick_val",
"im_dir": "./data/DrSAMevaluate/Warwick",
"gt_dir": "./data/DrSAMevaluate/Warwickanno",
"im_ext": ".bmp",
"gt_ext": "_anno.bmp"}
train_datasets = [dataset_cell, dataset_Kvasir, dataset_CHASE]
valid_datasets = [dataset_cell_val, dataset_Kvasir_val, dataset_CHASE_val, dataset_Warwick_val, dataset_isic_val]
args = get_args_parser()
net = DrMaskDecoder(args.model_type)
main(net, train_datasets, valid_datasets, args)