full_pascalvoc_test_preprocess.py 11.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
# Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
14 15

import xml.etree.ElementTree
16 17 18 19 20 21
from PIL import Image
import numpy as np
import os
import sys
from paddle.dataset.common import download
import tarfile
22
from six.moves import StringIO
23 24
import hashlib
import tarfile
25
import argparse
26 27 28 29 30

DATA_URL = "http://host.robots.ox.ac.uk/pascal/VOC/voc2007/VOCtest_06-Nov-2007.tar"
DATA_DIR = os.path.expanduser("~/.cache/paddle/dataset/pascalvoc/")
TAR_FILE = "VOCtest_06-Nov-2007.tar"
TAR_PATH = os.path.join(DATA_DIR, TAR_FILE)
31 32
SIZE_FLOAT32 = 4
SIZE_INT64 = 8
33 34
RESIZE_H = 300
RESIZE_W = 300
35 36
MEAN_VALUE = [127.5, 127.5, 127.5]
AP_VERSION = '11point'
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
DATA_OUT = 'pascalvoc_full.bin'
DATA_OUT_PATH = os.path.join(DATA_DIR, DATA_OUT)
BIN_TARGETHASH = "f6546cadc42f5ff13178b84ed29b740b"
TAR_TARGETHASH = "b6e924de25625d8de591ea690078ad9f"
TEST_LIST_KEY = "VOCdevkit/VOC2007/ImageSets/Main/test.txt"
BIN_FULLSIZE = 5348678856


def preprocess(img):
    img_width, img_height = img.size
    img = img.resize((RESIZE_W, RESIZE_H), Image.ANTIALIAS)
    img = np.array(img)
    # HWC to CHW
    if len(img.shape) == 3:
        img = np.swapaxes(img, 1, 2)
        img = np.swapaxes(img, 1, 0)
    # RBG to BGR
    img = img[[2, 1, 0], :, :]
    img = img.astype('float32')
56
    img_mean = np.array(MEAN_VALUE)[:, np.newaxis, np.newaxis].astype('float32')
57 58 59 60 61
    img -= img_mean
    img = img * 0.007843
    return img


62 63 64
def convert_pascalvoc_local2bin(args):
    data_dir = os.path.expanduser(args.data_dir)
    label_fpath = os.path.join(data_dir, args.label_file)
65
    assert data_dir, 'Once set --local, user need to provide the --data_dir'
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
    flabel = open(label_fpath)
    label_list = [line.strip() for line in flabel]

    img_annotation_list_path = os.path.join(data_dir, args.img_annotation_list)
    flist = open(img_annotation_list_path)
    lines = [line.strip() for line in flist]

    output_file_path = os.path.join(data_dir, args.output_file)
    f1 = open(output_file_path, "w+b")
    f1.seek(0)
    image_nums = len(lines)
    f1.write(np.array(image_nums).astype('int64').tobytes())

    boxes = []
    lbls = []
    difficults = []
    object_nums = []

    for line in lines:
        image_path, label_path = line.split()
        image_path = os.path.join(data_dir, image_path)
        label_path = os.path.join(data_dir, label_path)

        im = Image.open(image_path)
        if im.mode == 'L':
            im = im.convert('RGB')
        im_width, im_height = im.size

        im = preprocess(im)
        np_im = np.array(im)
        f1.write(np_im.astype('float32').tobytes())

        # layout: label | xmin | ymin | xmax | ymax | difficult
        bbox_labels = []
        root = xml.etree.ElementTree.parse(label_path).getroot()

        objects = root.findall('object')
        objects_size = len(objects)
        object_nums.append(objects_size)

        for object in objects:
            bbox_sample = []
            # start from 1
            bbox_sample.append(
                float(label_list.index(object.find('name').text)))
            bbox = object.find('bndbox')
            difficult = float(object.find('difficult').text)
            bbox_sample.append(float(bbox.find('xmin').text) / im_width)
            bbox_sample.append(float(bbox.find('ymin').text) / im_height)
            bbox_sample.append(float(bbox.find('xmax').text) / im_width)
            bbox_sample.append(float(bbox.find('ymax').text) / im_height)
            bbox_sample.append(difficult)
            bbox_labels.append(bbox_sample)

        bbox_labels = np.array(bbox_labels)
        if len(bbox_labels) == 0: continue

        lbls.extend(bbox_labels[:, 0])
        boxes.extend(bbox_labels[:, 1:5])
        difficults.extend(bbox_labels[:, -1])

    f1.write(np.array(object_nums).astype('uint64').tobytes())
    f1.write(np.array(lbls).astype('int64').tobytes())
    f1.write(np.array(boxes).astype('float32').tobytes())
    f1.write(np.array(difficults).astype('int64').tobytes())
    f1.close()

    object_nums_sum = sum(object_nums)
134 135 136 137 138 139
    # The data should be contains 
    # number of images + all images data + an array that represent object numbers of each image
    # + labels of all objects in images + bboxes of all objects + difficulties of all objects
    # so the target size should be as follows:
    target_size = SIZE_INT64 + image_nums * 3 * args.resize_h * args.resize_h * SIZE_FLOAT32 + image_nums * SIZE_INT64 + object_nums_sum * (
        SIZE_INT64 + 4 * SIZE_FLOAT32 + SIZE_INT64)
140
    if (os.path.getsize(output_file_path) == target_size):
141
        print("Success! \nThe local data output binary file can be found at: ",
142 143 144 145 146
              output_file_path)
    else:
        print("Conversion failed!")


147 148 149 150 151 152 153 154
def print_processbar(done_percentage):
    done_filled = done_percentage * '='
    empty_filled = (100 - done_percentage) * ' '
    sys.stdout.write("\r[%s%s]%d%%" %
                     (done_filled, empty_filled, done_percentage))
    sys.stdout.flush()


155
def convert_pascalvoc_tar2bin(tar_path, data_out_path):
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
    print("Start converting ...\n")
    images = {}
    gt_labels = {}
    boxes = []
    lbls = []
    difficults = []
    object_nums = []

    # map label to number (index)
    label_list = [
        "background", "aeroplane", "bicycle", "bird", "boat", "bottle", "bus",
        "car", "cat", "chair", "cow", "diningtable", "dog", "horse",
        "motorbike", "person", "pottedplant", "sheep", "sofa", "train",
        "tvmonitor"
    ]
    print_processbar(0)
    #read from tar file and write to bin
    tar = tarfile.open(tar_path, "r")
    f_test = tar.extractfile(TEST_LIST_KEY).read()
    lines = f_test.split('\n')
    del lines[-1]
177 178
    image_nums = len(lines)
    per_percentage = image_nums / 100
179 180 181

    f1 = open(data_out_path, "w+b")
    f1.seek(0)
182
    f1.write(np.array(image_nums).astype('int64').tobytes())
183 184 185 186 187 188 189 190 191 192 193
    for tarInfo in tar:
        if tarInfo.isfile():
            tmp_filename = tarInfo.name
            name_arr = tmp_filename.split('/')
            name_prefix = name_arr[-1].split('.')[0]
            if name_arr[-2] == 'JPEGImages' and name_prefix in lines:
                images[name_prefix] = tar.extractfile(tarInfo).read()
            if name_arr[-2] == 'Annotations' and name_prefix in lines:
                gt_labels[name_prefix] = tar.extractfile(tarInfo).read()

    for line_idx, name_prefix in enumerate(lines):
194
        im = Image.open(StringIO(images[name_prefix]))
195 196 197 198 199 200 201 202 203 204
        if im.mode == 'L':
            im = im.convert('RGB')
        im_width, im_height = im.size

        im = preprocess(im)
        np_im = np.array(im)
        f1.write(np_im.astype('float32').tobytes())

        # layout: label | xmin | ymin | xmax | ymax | difficult
        bbox_labels = []
205
        root = xml.etree.ElementTree.fromstring(gt_labels[name_prefix])
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

        objects = root.findall('object')
        objects_size = len(objects)
        object_nums.append(objects_size)

        for object in objects:
            bbox_sample = []
            bbox_sample.append(
                float(label_list.index(object.find('name').text)))
            bbox = object.find('bndbox')
            difficult = float(object.find('difficult').text)
            bbox_sample.append(float(bbox.find('xmin').text) / im_width)
            bbox_sample.append(float(bbox.find('ymin').text) / im_height)
            bbox_sample.append(float(bbox.find('xmax').text) / im_width)
            bbox_sample.append(float(bbox.find('ymax').text) / im_height)
            bbox_sample.append(difficult)
            bbox_labels.append(bbox_sample)

        bbox_labels = np.array(bbox_labels)
        if len(bbox_labels) == 0: continue
        lbls.extend(bbox_labels[:, 0])
        boxes.extend(bbox_labels[:, 1:5])
        difficults.extend(bbox_labels[:, -1])

        if line_idx % per_percentage:
            print_processbar(line_idx / per_percentage)

233 234 235
    # The data should be stored in binary in following sequence: 
    # number of images->all images data->an array that represent object numbers in each image
    # ->labels of all objects in images->bboxes of all objects->difficulties of all objects
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
    f1.write(np.array(object_nums).astype('uint64').tobytes())
    f1.write(np.array(lbls).astype('int64').tobytes())
    f1.write(np.array(boxes).astype('float32').tobytes())
    f1.write(np.array(difficults).astype('int64').tobytes())
    f1.close()
    print_processbar(100)
    print("Conversion finished!\n")


def download_pascalvoc(data_url, data_dir, tar_targethash, tar_path):
    print("Downloading pascalvcoc test set...")
    download(data_url, data_dir, tar_targethash)
    if not os.path.exists(tar_path):
        print("Failed in downloading pascalvoc test set. URL %s\n" % data_url)
    else:
        tmp_hash = hashlib.md5(open(tar_path, 'rb').read()).hexdigest()
        if tmp_hash != tar_targethash:
            print("Downloaded test set is broken, removing ...\n")
        else:
            print("Downloaded successfully. Path: %s\n" % tar_path)


def run_convert():
    try_limit = 2
    retry = 0
    while not (os.path.exists(DATA_OUT_PATH) and
               os.path.getsize(DATA_OUT_PATH) == BIN_FULLSIZE and BIN_TARGETHASH
               == hashlib.md5(open(DATA_OUT_PATH, 'rb').read()).hexdigest()):
        if os.path.exists(DATA_OUT_PATH):
            sys.stderr.write(
                "The existing binary file is broken. It is being removed...\n")
            os.remove(DATA_OUT_PATH)
        if retry < try_limit:
            retry = retry + 1
        else:
            download_pascalvoc(DATA_URL, DATA_DIR, TAR_TARGETHASH, TAR_PATH)
272 273 274 275 276 277
            convert_pascalvoc_tar2bin(TAR_PATH, DATA_OUT_PATH)
    print("Success!\nThe binary file can be found at %s\n" % DATA_OUT_PATH)


def main_pascalvoc_preprocess(args):
    parser = argparse.ArgumentParser(
278 279 280
        description="Convert the full pascalvoc val set or local data to binary file.",
        usage=None,
        add_help=True)
281
    parser.add_argument(
282 283 284
        '--local',
        action="store_true",
        help="If used, user need to set --data_dir and then convert file")
285
    parser.add_argument(
286
        "--data_dir", default="", type=str, help="Dataset root directory")
287 288 289 290
    parser.add_argument(
        "--img_annotation_list",
        type=str,
        default="test_100.txt",
291
        help="A file containing the image file path and corresponding annotation file path"
292 293 294 295 296
    )
    parser.add_argument(
        "--label_file",
        type=str,
        default="label_list",
297
        help="List of object labels with same sequence as denoted in the annotation file"
298 299 300 301 302 303
    )
    parser.add_argument(
        "--output_file",
        type=str,
        default="pascalvoc_small.bin",
        help="File path of the output binary file")
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
    parser.add_argument(
        "--resize_h",
        type=int,
        default=RESIZE_H,
        help="Image preprocess with resize_h")
    parser.add_argument(
        "--resize_w",
        type=int,
        default=RESIZE_W,
        help="Image prerocess with resize_w")
    parser.add_argument(
        "--mean_value",
        type=str,
        default=MEAN_VALUE,
        help="Image preprocess with mean_value")
    parser.add_argument(
        "--ap_version",
        type=str,
        default=AP_VERSION,
        help="Image preprocess with ap_version")
324
    args = parser.parse_args()
325
    if args.local:
326
        convert_pascalvoc_local2bin(args)
327
    else:
328
        run_convert()
329 330 331


if __name__ == "__main__":
332
    main_pascalvoc_preprocess(sys.argv)