Skip to content
This repository was archived by the owner on Apr 1, 2023. It is now read-only.

Commit 8a386b8

Browse files
committed
First commit of flickr crawler and post processing scripts
1 parent ee7054a commit 8a386b8

15 files changed

+25964
-0
lines changed

data_processing/coco_data_provider.py

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
import os
2+
import sys
3+
4+
sys.path.append('.')
5+
sys.path.append('./pycocotools')
6+
import pycocotools.coco as coco
7+
8+
pascal_classes = ['person', 'bird', 'cat', 'cow', 'dog', 'horse', 'sheep', 'aeroplane',
9+
'bicycle', 'boat', 'bus', 'car', 'motorbike', 'train', 'bottle', 'chair',
10+
'dining table', 'potted plant', 'sofa', 'tv/monitor']
11+
pascal_classes_mapped = ['person', 'bird', 'cat', 'cow', 'dog', 'horse', 'sheep', 'airplane',
12+
'bicycle', 'boat', 'bus', 'car (sedan)', 'motorcycle', 'train', 'bottle', 'chair',
13+
'dining table', 'potted plant', 'sofa', 'tv']
14+
15+
images_dir = '../Datasets/COCO/coco-master/images'
16+
anno_dir = '../Datasets/COCO/coco-master/annotations'
17+
itrain_2014 = 'instances_train2014.json'
18+
ival_2014 = 'instances_val2014.json'
19+
20+
21+
def img_info_list_to_dict(input_list):
22+
dic = {}
23+
for i in input_list:
24+
image_id = i['id']
25+
assert image_id not in dic.keys()
26+
dic[image_id] = i
27+
return dic
28+
29+
30+
def get_all_images_data_categories(split, catIds=[]):
31+
if split == 'train':
32+
COCO = coco.COCO(annotation_file=os.path.join(anno_dir, itrain_2014))
33+
elif split == 'test':
34+
COCO = coco.COCO(annotation_file=os.path.join(anno_dir, ival_2014))
35+
if len(catIds) == 0:
36+
return COCO.loadImgs(COCO.getImgIds()), COCO.loadAnns(COCO.getAnnIds()), COCO.loadCats(COCO.getCatIds())
37+
else:
38+
return COCO.loadImgs(ids=COCO.getImgIds(catIds=catIds)), COCO.loadAnns(
39+
ids=COCO.getAnnIds(catIds=catIds)), COCO.loadCats(COCO.getCatIds(catIds=catIds))
40+
41+
42+
def expand_bbox(bbox, max_height, max_width, frac):
43+
assert len(bbox) == 4
44+
half_width = round(bbox[2] / 2)
45+
half_height = round(bbox[3] / 2)
46+
mid_x = bbox[0] + half_width
47+
mid_y = bbox[1] + half_height
48+
49+
x_min = max(0, mid_x - half_width * frac)
50+
y_min = max(0, mid_y - half_height * frac)
51+
x_max = min(max_width, mid_x + half_width * frac)
52+
y_max = min(max_height, mid_y + half_height * frac)
53+
return [round(x_min), round(y_min), round(x_max), round(y_max)]
54+
55+
56+
def get_shared_classes(input=None, print_out=False, output_file=True):
57+
if input is None:
58+
ret = get_all_images_data_categories('train')[2]
59+
else:
60+
ret = input
61+
coco_classes = [cls['name'] for cls in ret]
62+
class_dict = {item['name']: item for item in ret}
63+
# Convert 'car' to 'car (sedan)' for comparison with Sketchy
64+
coco_classes[coco_classes.index('car')] = 'car (sedan)'
65+
with open('../../shared_classes', 'r') as f:
66+
shared_classes = [cls[:-1].replace('_', ' ') for cls in f.readlines()]
67+
68+
if print_out:
69+
print(len(shared_classes))
70+
print(shared_classes)
71+
if output_file:
72+
shared_classes2 = [(cls + '\n') for cls in shared_classes if cls in coco_classes]
73+
with open('../../shared_classes2', 'w') as f:
74+
f.writelines(shared_classes2)
75+
76+
shared_classes = [cls for cls in shared_classes if cls in coco_classes]
77+
print([cls for cls in shared_classes if cls in coco_classes and cls in pascal_classes_mapped])
78+
shared_classes[shared_classes.index('car (sedan)')] = 'car' # Convert 'car' back
79+
output_dict = {class_dict[cls]['id']: class_dict[cls] for cls in shared_classes}
80+
return shared_classes, output_dict
81+
82+
83+
def get_bbox():
84+
img_info, seg_info, cat_info = get_all_images_data_categories('train')
85+
img_info = img_info_list_to_dict(img_info)
86+
shared_classes, cls_info = get_shared_classes(input=cat_info, print_out=False, output_file=False)
87+
bbox_list = {cls: [] for cls in shared_classes}
88+
for object in seg_info:
89+
category_id = object['category_id']
90+
if category_id not in cls_info:
91+
continue
92+
93+
category_name = cls_info[category_id]['name']
94+
bbox = object['bbox']
95+
image_id = object['image_id']
96+
iscrowd = object['iscrowd']
97+
98+
this_img_info = img_info[image_id]
99+
file_name = this_img_info['file_name']
100+
height = this_img_info['height']
101+
width = this_img_info['width']
102+
bbox = expand_bbox(bbox, height, width, 1.5)
103+
104+
bbox_list[category_name].append({
105+
'image_id': image_id,
106+
'category_name': category_name,
107+
'category_id': category_id,
108+
'iscrowd': iscrowd,
109+
'file_name': file_name,
110+
'height': height,
111+
'width': width,
112+
'bbox': bbox,
113+
})
114+
115+
return bbox_list
116+
117+
118+
if __name__ == '__main__':
119+
get_bbox()
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
airplane
2+
apple
3+
bear
4+
bench
5+
bicycle
6+
cat
7+
car
8+
chair
9+
couch
10+
cow
11+
dog
12+
elephant
13+
giraffe
14+
horse
15+
knife
16+
motorcycle
17+
scissors
18+
sheep
19+
spoon
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
% Modified from pix2pix scripts: https://github.com/phillipi/pix2pix/blob/master/scripts/edges
2+
3+
%%% Prerequisites
4+
% You need to get the cpp file edgesNmsMex.cpp from https://raw.githubusercontent.com/pdollar/edges/master/private/edgesNmsMex.cpp
5+
% and compile it in Matlab: mex edgesNmsMex.cpp
6+
% You also need to download and install Piotr's Computer Vision Matlab Toolbox: https://pdollar.github.io/toolbox/
7+
8+
%%% parameters
9+
% hed_mat_dir: the hed mat file directory (the output of 'batch_hed.py')
10+
% edge_dir: the output HED edges directory
11+
% image_width: resize the edge map to [image_width, image_width]
12+
% threshold: threshold for image binarization (default 25.0/255.0)
13+
% small_edge: remove small edges (default 5)
14+
15+
function [] = PostprocessHED(hed_mat_dir, edge_dir, image_width, threshold, small_edge)
16+
17+
if ~exist(edge_dir, 'dir')
18+
mkdir(edge_dir);
19+
end
20+
fileList = dir(fullfile(hed_mat_dir, '*.mat'));
21+
nFiles = numel(fileList);
22+
fprintf('find %d mat files\n', nFiles);
23+
24+
parfor n = 1 : nFiles
25+
if mod(n, 1000) == 0
26+
fprintf('process %d/%d images\n', n, nFiles);
27+
end
28+
fileName = fileList(n).name;
29+
filePath = fullfile(hed_mat_dir, fileName);
30+
jpgName = strrep(fileName, '.mat', '.png');
31+
edge_path = fullfile(edge_dir, jpgName);
32+
33+
if ~exist(edge_path, 'file')
34+
E = GetEdge(filePath);
35+
E = imresize(E,[image_width,image_width]);
36+
E_simple = SimpleEdge(E, threshold, small_edge);
37+
E_simple = uint8(E_simple*255);
38+
imwrite(E_simple, edge_path);
39+
end
40+
end
41+
end
42+
43+
44+
45+
46+
function [E] = GetEdge(filePath)
47+
matdata = load(filePath);
48+
E = 1-matdata.predict;
49+
end
50+
51+
function [E4] = SimpleEdge(E, threshold, small_edge)
52+
if nargin <= 1
53+
threshold = 25.0/255.0;
54+
end
55+
56+
if nargin <= 2
57+
small_edge = 5;
58+
end
59+
60+
if ndims(E) == 3
61+
E = E(:,:,1);
62+
end
63+
64+
E1 = 1 - E;
65+
E2 = EdgeNMS(E1);
66+
E3 = double(E2>=max(eps,threshold));
67+
E3 = bwmorph(bwmorph(E3,'thin',inf), 'clean');
68+
E4 = bwareaopen(E3, small_edge);
69+
E4 = bwmorph(bwmorph(E4,'shrink'), 'spur');
70+
E4=1-E4;
71+
end
72+
73+
function [E_nms] = EdgeNMS( E )
74+
E=single(E);
75+
[Ox,Oy] = gradient2(convTri(E,4));
76+
[Oxx,~] = gradient2(Ox);
77+
[Oxy,Oyy] = gradient2(Oy);
78+
O = mod(atan(Oyy.*sign(-Oxy)./(Oxx+1e-5)),pi);
79+
E_nms = edgesNmsMex(E,O,1,5,1.01,1);
80+
end
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
# HED batch processing script
2+
# Modified from pix2pix scripts: https://github.com/phillipi/pix2pix/tree/master/scripts/edges
3+
# Notice that this script processes images in batches instead of one-by-one, which is slightly faster.
4+
# However, it will discard the last n images where 0 < n < batch_size.
5+
# Originally modified from https://github.com/s9xie/hed/blob/master/examples/hed/HED-tutorial.ipynb
6+
# Step 1: download the hed repo: https://github.com/s9xie/hed
7+
# Step 2: download the models and protoxt, and put them under {caffe_root}/examples/hed/
8+
# Step 3: put this script under {caffe_root}/examples/hed/
9+
# Step 4: run the following script:
10+
# python batch_hed.py --images_dir=/data/to/path/photos/ --hed_mat_dir=/data/to/path/hed_mat_files/
11+
# Step 5: run the MATLAB post-processing script "PostprocessHED.m"
12+
# The code sometimes crashes after computation is done. Error looks like "Check failed: ... driver shutting down". You can just kill the job.
13+
# For large images, it will produce gpu memory issue. Therefore, you better resize the images before running this script.
14+
15+
import numpy as np
16+
import scipy.misc
17+
from PIL import Image
18+
import matplotlib.pyplot as plt
19+
import matplotlib.pylab as pylab
20+
import matplotlib.cm as cm
21+
import scipy.io
22+
import os
23+
import cv2
24+
import argparse
25+
from time import time
26+
27+
28+
def parse_args():
29+
parser = argparse.ArgumentParser(description='batch proccesing: photos->edges')
30+
parser.add_argument('--caffe_root', dest='caffe_root', help='caffe root',
31+
default='../hed-master', type=str)
32+
parser.add_argument('--caffemodel', dest='caffemodel', help='caffemodel',
33+
default='../hed-master/examples/hed/hed_pretrained_bsds.caffemodel',
34+
type=str)
35+
parser.add_argument('--prototxt', dest='prototxt', help='caffe prototxt file',
36+
default='../hed-master/examples/hed/deploy.prototxt', type=str)
37+
parser.add_argument('--images_dir', dest='images_dir',
38+
default='../flickr_coco/airplane',
39+
help='directory to store input photos', type=str)
40+
parser.add_argument('--hed_mat_dir', dest='hed_mat_dir', default='../flickr_hed_coco/mat/airplane',
41+
help='directory to store output hed edges in mat file',
42+
type=str)
43+
parser.add_argument('--border', dest='border', help='padding border', type=int, default=128)
44+
parser.add_argument('--gpu_id', dest='gpu_id', help='gpu id', type=int, default=0)
45+
args = parser.parse_args()
46+
return args
47+
48+
49+
args = parse_args()
50+
for arg in vars(args):
51+
print('[%s] =' % arg, getattr(args, arg))
52+
# Make sure that caffe is on the python path:
53+
caffe_root = args.caffe_root # this file is expected to be in {caffe_root}/examples/hed/
54+
import sys
55+
sys.path.insert(0, os.path.join(caffe_root, 'python'))
56+
57+
# Suppress output
58+
os.environ['GLOG_minloglevel'] = '2'
59+
60+
import caffe
61+
import scipy.io as sio
62+
63+
if not os.path.exists(args.hed_mat_dir):
64+
print('create output directory %s' % args.hed_mat_dir)
65+
os.makedirs(args.hed_mat_dir)
66+
67+
imgList = os.listdir(args.images_dir)
68+
nImgs = len(imgList)
69+
print('#images = %d' % nImgs)
70+
71+
caffe.set_mode_gpu()
72+
caffe.set_device(args.gpu_id)
73+
# load net
74+
net = caffe.Net(args.prototxt, args.caffemodel, caffe.TEST)
75+
# pad border
76+
border = args.border
77+
78+
# Time counter
79+
prev_time = float("-inf")
80+
curr_time = float("-inf")
81+
image_list = []
82+
batch_size = 8
83+
84+
for i in range(nImgs):
85+
if i % 500 == 0:
86+
print('processing image %d/%d' % (i, nImgs))
87+
curr_time = time()
88+
elapsed = curr_time - prev_time
89+
print(
90+
"Now at iteration %d. Elapsed time: %.5fs. Average time: %.5fs/iter" % (i, elapsed, elapsed / 500.))
91+
prev_time = curr_time
92+
93+
if i % batch_size == 0 and i > 0:
94+
in_ = np.concatenate(image_list, axis=0)
95+
96+
# shape for input (data blob is N x C x H x W), set data
97+
net.blobs['data'].reshape(*in_.shape)
98+
net.blobs['data'].data[...] = in_
99+
# run net and take argmax for prediction
100+
net.forward()
101+
fuse = np.squeeze(net.blobs['sigmoid-fuse'].data)
102+
# get rid of the border
103+
fuse = fuse[:, border:-border, border:-border]
104+
105+
for j in range(batch_size):
106+
# save hed file to the disk
107+
name, ext = os.path.splitext(imgList[i - batch_size + j])
108+
sio.savemat(os.path.join(args.hed_mat_dir, name + '.mat'), {'predict': fuse[j]})
109+
110+
image_list = []
111+
112+
im = cv2.imread(os.path.join(args.images_dir, imgList[i]), cv2.IMREAD_COLOR)
113+
im = cv2.resize(im, (256, 256), interpolation=cv2.INTER_AREA)
114+
115+
in_ = im.astype(np.float32)
116+
in_ = np.pad(in_, ((border, border), (border, border), (0, 0)), 'reflect')
117+
118+
in_ = in_[:, :, ::-1]
119+
in_ -= np.array((104.00698793, 116.66876762, 122.67891434))
120+
in_ = np.expand_dims(in_.transpose((2, 0, 1)), axis=0)
121+
122+
image_list.append(in_)

0 commit comments

Comments
 (0)