Skip to content

Commit fe92f52

Browse files
committed
order by saliency
1 parent 71671cf commit fe92f52

File tree

5 files changed

+162
-24
lines changed

5 files changed

+162
-24
lines changed

.vscode/launch.json

+15-1
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,25 @@
44
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
55
"version": "0.2.0",
66
"configurations": [
7+
{
8+
"name": "Sort by Saliency",
9+
"type": "debugpy",
10+
"request": "launch",
11+
"program": "feature_order/saliency.py",
12+
"args": [
13+
"--karpathy_json_file", "/homes/hps01/dataset_coco_10.json",
14+
"--image_dir", "/import/gameai-01/eey362/datasets/coco/images/",
15+
"--feature_dir", "/import/gameai-01/eey362/VSUA-Captioning/data/cocobu_att/",
16+
"--bbox_dir", "/import/gameai-01/eey362/VSUA-Captioning/data/cocobu_box/",
17+
"--output_dir", "/import/gameai-01/eey362/datasets/coco/butd_ordered_saliency",
18+
],
19+
"console": "integratedTerminal"
20+
},
721
{
822
"name": "Sort BUTD by bbox",
923
"type": "debugpy",
1024
"request": "launch",
11-
"program": "feature_order/feature_order.py",
25+
"program": "feature_order/bbox_size.py",
1226
"args": [
1327
"--bbox_dir", "/import/gameai-01/eey362/VSUA-Captioning/data/cocobu_box/",
1428
"--feature_dir", "/import/gameai-01/eey362/VSUA-Captioning/data/cocobu_att/",
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
11
import os
22
import argparse
33
import numpy as np
4-
from tqdm import tqdm
5-
from multiprocessing import Pool
64

7-
def load_feature(feature_dir):
8-
return np.load(feature_dir)["feat"]
9-
10-
def load_bbox(bbox_dir):
11-
return np.load(bbox_dir) # [B, 4]
5+
from helpers import load_feature, load_bbox, save_feature, process_parallel
126

137
def caculate_area(bbox):
148
x1 = bbox[:, 0]
@@ -24,11 +18,6 @@ def sort_feature_by_area(features, bbox):
2418
features = features[sort_index]
2519
return features
2620

27-
def save_feature(output_dir, name, features):
28-
path = os.path.join(output_dir, f"{name}.npz")
29-
np.savez_compressed(path, feat=features)
30-
31-
3221
def process_individual(feature_dir, bbox_dir, output_dir, file):
3322
feature_path = os.path.join(feature_dir, file)
3423
features = load_feature(feature_path)
@@ -41,16 +30,6 @@ def process_individual(feature_dir, bbox_dir, output_dir, file):
4130
save_feature(output_dir, file.split(".")[0], sorted_features)
4231

4332

44-
# Using multiprocess, rewrite the process function so that it takes advantage of parallelism
45-
# and processes the features in parallel without tqdm progress bar. Instead, use Counter to keep track of the progress.
46-
# and print the progress every 1000 images.
47-
def process_parallel(feature_dir, bbox_dir, output_dir):
48-
files = os.listdir(feature_dir)
49-
with Pool() as p:
50-
p.starmap(process_individual, [(feature_dir, bbox_dir, output_dir, file) for file in files])
51-
52-
53-
5433
if __name__ == "__main__":
5534
args = argparse.ArgumentParser()
5635
args.add_argument("--feature_dir", type=str, required=True)
@@ -61,4 +40,4 @@ def process_parallel(feature_dir, bbox_dir, output_dir):
6140

6241
os.makedirs(args.output_dir, exist_ok=True)
6342

64-
process_parallel(args.feature_dir, args.bbox_dir, args.output_dir)
43+
process_parallel(args.feature_dir, args.bbox_dir, args.output_dir, process_individual)

feature_order/coco.py

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import json
2+
import os
3+
4+
import numpy as np
5+
import torch
6+
from torch.utils.data import Dataset
7+
8+
from PIL import Image
9+
10+
class CocoButdFeatures(Dataset):
11+
def __init__(
12+
self,
13+
captions_file: str,
14+
image_dir: str,
15+
feature_dir: str,
16+
bbox_dir: str,
17+
):
18+
self.captions_file = captions_file
19+
self.image_dir = image_dir
20+
self.feature_dir = feature_dir
21+
self.bbox_dir = bbox_dir
22+
23+
with open(self.captions_file, "r") as f:
24+
self.captions_file_data = json.load(f)
25+
26+
self.image_locations = []
27+
self.feature_locations = []
28+
self.box_locations = []
29+
30+
for image_data in self.captions_file_data["images"]:
31+
self.image_locations.append(
32+
os.path.join(
33+
self.image_dir,
34+
image_data["filepath"],
35+
image_data["filename"],
36+
)
37+
)
38+
39+
feat_path = os.path.join(
40+
self.feature_dir,
41+
(f"{image_data['cocoid']}.npz"),
42+
)
43+
self.feature_locations.append(feat_path)
44+
45+
bbox_path = os.path.join(
46+
self.bbox_dir,
47+
(f"{image_data['cocoid']}.npy"),
48+
)
49+
self.box_locations.append(bbox_path)
50+
51+
def __getitem__(self, index):
52+
image = Image.open(self.image_locations[index])
53+
image = np.array(image)
54+
55+
features = np.load(self.feature_locations[index])["feat"]
56+
57+
box_loc = self.box_locations[index]
58+
boxes = np.load(box_loc)
59+
boxes = torch.from_numpy(boxes).type(torch.float32)
60+
61+
name = box_loc.split("/")[-1].split(".")[0]
62+
63+
return name, image, features, boxes
64+
65+
def __len__(self):
66+
return len(self.box_locations)

feature_order/helpers.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import numpy as np
2+
import os
3+
from multiprocessing import Pool
4+
5+
def load_feature(feature_dir):
6+
return np.load(feature_dir)["feat"]
7+
8+
9+
def load_bbox(bbox_dir):
10+
return np.load(bbox_dir) # [B, 4]
11+
12+
13+
def save_feature(output_dir, name, features):
14+
path = os.path.join(output_dir, f"{name}.npz")
15+
np.savez_compressed(path, feat=features)
16+
17+
18+
def process_parallel(feature_dir, bbox_dir, output_dir, function):
19+
files = os.listdir(feature_dir)
20+
with Pool() as p:
21+
p.starmap(
22+
function,
23+
[(feature_dir, bbox_dir, output_dir, file) for file in files],
24+
)

feature_order/saliency.py

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import argparse
2+
from multiprocessing import Pool
3+
import os
4+
5+
import cv2
6+
import numpy as np
7+
8+
from coco import CocoButdFeatures
9+
from helpers import save_feature
10+
11+
12+
def get_order_index_from_saliency(image, bboxes):
13+
saliency = cv2.saliency.StaticSaliencySpectralResidual_create()
14+
(success, saliencyMap) = saliency.computeSaliency(image)
15+
saliency_scores = []
16+
for bbox in bboxes:
17+
x1, y1, x2, y2 = bbox
18+
saliency_score = np.mean(saliencyMap[int(y1):int(y2), int(x1):int(x2)])
19+
saliency_scores.append(saliency_score)
20+
21+
return np.argsort(saliency_scores)[::-1]
22+
23+
24+
def process_individual(dataset, index, output_dir):
25+
name, image, features, boxes = dataset.__getitem__(index)
26+
order = get_order_index_from_saliency(image, boxes)
27+
ordered_features = features[order]
28+
save_feature(output_dir, name, ordered_features)
29+
30+
31+
def process_parallel(output_dir, dataset, function):
32+
with Pool() as p:
33+
p.starmap(function, [(dataset, i, output_dir) for i in range(len(dataset))])
34+
35+
36+
if __name__ == "__main__":
37+
args = argparse.ArgumentParser()
38+
args.add_argument("--karpathy_json_file", type=str, required=True)
39+
args.add_argument("--image_dir", type=str, required=True)
40+
args.add_argument("--feature_dir", type=str, required=True)
41+
args.add_argument("--output_dir", type=str, required=True)
42+
args.add_argument("--bbox_dir", type=str, required=True)
43+
44+
args = args.parse_args()
45+
46+
os.makedirs(args.output_dir, exist_ok=True)
47+
48+
dataset = CocoButdFeatures(
49+
captions_file=args.karpathy_json_file,
50+
image_dir=args.image_dir,
51+
feature_dir=args.feature_dir,
52+
bbox_dir=args.bbox_dir,
53+
)
54+
55+
process_parallel(args.output_dir, dataset, process_individual)

0 commit comments

Comments
 (0)