-
Notifications
You must be signed in to change notification settings - Fork 0
/
process_regions.py
191 lines (157 loc) · 7 KB
/
process_regions.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
import torch
import os
import numpy as np
import time
from tqdm import tqdm
from pycocotools import mask as mask_utils
import torch
from PIL import Image
import torchvision.transforms as T
import math
import os
import argparse
import region_utils as utils
import torch.nn.functional as F
import cv2
import logging
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)
"""
Given extracted regions from SAM, create feature vectors for each region using some method (eg. avg)
"""
def region_features(args,image_id_to_sam):
if args.feature_dir!= None:
features_exist = True
# Get the intersection of the feature files and the sam regions
all_feature_files = [f for f in os.listdir(args.feature_dir) if os.path.isfile(os.path.join(args.feature_dir, f))]
feature_files_in_sam = [f for f in all_feature_files if os.path.splitext(f)[0] in image_id_to_sam]
features_minus_sam = set(all_feature_files) - set(feature_files_in_sam)
if len(features_minus_sam) > 0:
logger.warning(f'Found {len(features_minus_sam)} feature files that are not in the set of SAM region files: {features_minus_sam}')
prog_bar = tqdm(feature_files_in_sam)
def extract_features(f, args,device='cuda',features_exist=True):
prog_bar.set_description(f'Region features: {f}')
features = utils.open_file(os.path.join(args.feature_dir,f))
if len(features.shape)>4:
features = np.squeeze(features,axis=0)
file_name = f
ext = os.path.splitext(f)[1]
all_region_features_in_image = []
sam_regions = image_id_to_sam[file_name.replace(ext,'')]
if args.interpolate == 'downsample':
f1, h1, w1 = features[0].shape
for region in sam_regions:
sam_region_feature = {}
sam_region_feature['region_id'] = region['instance_id']
sam_region_feature['area'] = region['area']
sam_mask = mask_utils.decode(region['segmentation'])
h2, w2 = sam_mask.shape
downsampled_mask = torch.from_numpy(sam_mask).cuda()
downsampled_mask = downsampled_mask.unsqueeze(0).unsqueeze(0)
downsampled_mask = torch.nn.functional.interpolate(downsampled_mask, size=(h1, w1), mode='nearest').squeeze(0).squeeze(0)
if torch.sum(downsampled_mask).item() == 0:
continue
features_in_sam = torch.from_numpy(features).cuda().squeeze(dim = 0)[:, downsampled_mask==1].view(f1, -1).mean(1).cpu().numpy()
sam_region_feature['region_feature'] = features_in_sam
all_region_features_in_image.append(sam_region_feature)
else:
if len(sam_regions) > 0:
# sam regions within an image all have the same total size
new_h, new_w = mask_utils.decode(sam_regions[0]['segmentation']).shape
patch_length = args.dino_patch_length
padded_h, padded_w = math.ceil(new_h / patch_length) * patch_length, math.ceil(new_w / patch_length) * patch_length # Get the padded height and width
upsample_feature = torch.nn.functional.interpolate(torch.from_numpy(features).cuda(), size=[padded_h,padded_w],mode='bilinear') # First interpolate to the padded size
upsample_feature = T.CenterCrop((new_h, new_w)) (upsample_feature).squeeze(dim = 0) # Apply center cropping to the original size
f,h,w = upsample_feature.size()
for region in sam_regions:
start_region_time = time.time()
sam_region_feature = {}
if 'region_id' in region:
sam_region_feature['region_id'] = region['region_id']
sam_mask = mask_utils.decode(region['segmentation'])
if 'area' in region:
sam_region_feature['area'] = region['area']
r_1, r_2 = np.where(sam_mask == 1)
if args.pooling_method == 'average':
features_in_sam = upsample_feature[:,r_1,r_2].view(f,-1).mean(1).cpu().numpy()
elif args.pooling_method == 'max':
input_max, max_indices = torch.max(upsample_feature[:,r_1,r_2].view(f,-1), 1)
features_in_sam = input_max.cpu().numpy()
sam_region_feature['region_feature'] = features_in_sam
all_region_features_in_image.append(sam_region_feature)
utils.save_file(os.path.join(args.region_feature_dir, file_name.replace(ext,'.pkl')), all_region_features_in_image)
for i,f in enumerate(prog_bar):
try:
extract_features(f,args,features_exist=features_exist)
except torch.cuda.OutOfMemoryError as e:
logger.warning(f'Caught CUDA out of memory error for {f}; falling back to CPU')
torch.cuda.empty_cache()
continue
def load_all_regions(args):
if len(os.listdir(args.mask_dir)) == 0:
raise Exception(f"No regions found at {args.mask_dir}")
logger.info(f"Loading region masks from {args.mask_dir}")
image_id_to_mask = {}
for f in tqdm(os.listdir(args.mask_dir)):
filename_extension = os.path.splitext(f)[1]
regions = utils.open_file(os.path.join(args.mask_dir,f))
if not args.use_sam:
regions = [r for r in regions if 'mask' in list(r.keys())]
image_id_to_mask[f.replace(filename_extension,'')] = regions
return image_id_to_mask
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument(
"--feature_dir",
type=str,
default=None,
help="Location of extracted features",
)
parser.add_argument(
"--mask_dir",
type=str,
default=None,
help="Location of masks (sam or ground truth if given)",
)
parser.add_argument(
"--region_feature_dir",
type=str,
default=None,
help="Location of features per region/pooled features",
)
parser.add_argument(
"--dino_patch_length",
type=int,
default=14,
help="the length of dino patch",
)
parser.add_argument(
"--use_sam",
action="store_false",
help="If not using json sam regions"
)
parser.add_argument(
"--pooling_method",
type=str,
default='average',
choices=['average', 'max'],
help='pooling methods'
)
parser.add_argument(
"--interpolate",
type=str,
default='upsample',
choices=['upsample','downsample'],
help='interpolation'
)
parser.add_argument(
"--dtype",
type=str,
default='bf16',
choices=['fp16', 'fp32','bf16'],
help="Which mixed precision to use. Use fp32 for clip and dense_clip"
)
args = parser.parse_args()
image_id_to_mask = load_all_regions(args)
region_features(args,image_id_to_mask)
logger.info('Done')