예제 #1
0
    def minibatch(self):
        with tf.name_scope('batch_processing'):
            images = []
            bboxes = []

            record_input = data_flow_ops.RecordInput(
                file_pattern=dp.tf_records(
                    self.data_set, '{0}_keypoint'.format(self.keypoint.name)),
                seed=301,
                parallelism=64,
                buffer_size=2000,
                shift_ratio=0.2,
                batch_size=self.batch_size,
                name='record_input')
            records = record_input.get_yield_op()
            records = tf.split(records, self.batch_size, 0)
            records = [tf.reshape(record, []) for record in records]
            for i in xrange(self.batch_size):
                value = records[i]
                image, bbox = self._parse_example_proto(value, i)
                bbox = bbox[:4]
                image, bbox = self.distort_image(image, bbox)
                image -= tf.reduce_mean(image, axis=[0, 1])
                images.append(image)
                bboxes.append(bbox)
            images = tf.parallel_stack(images)

            images = tf.reshape(images,
                                shape=[
                                    self.batch_size, self.image_shape[0],
                                    self.image_shape[1], -1
                                ])
            bboxes = tf.reshape(bboxes, (self.batch_size, 1, 4))

            return images, bboxes
예제 #2
0
    def minibatch(self):
        with tf.name_scope('batch_processing'):
            images = []

            record_input = data_flow_ops.RecordInput(
                file_pattern=dp.tf_records(
                    self.data_set,
                    '{0}_crop_patch_full_{1}'.format(self.file_format,
                                                     self.region)),
                seed=301,
                parallelism=64,
                buffer_size=5000,
                shift_ratio=0.2,
                batch_size=self.batch_size,
                name='record_input')
            records = record_input.get_yield_op()
            records = tf.split(records, self.batch_size, 0)
            records = [tf.reshape(record, []) for record in records]
            for i in xrange(self.batch_size):
                value = records[i]
                image, dim = self._parse_example_proto(value, i)
                image = tf.image.resize_images(image, self.image_shape)
                image = self.distort_image(image)
                image = image[:, :, 0]
                images.append(image)
            images = tf.parallel_stack(images)

            images = tf.reshape(images,
                                shape=[
                                    self.batch_size, self.image_shape[0],
                                    self.image_shape[1], -1
                                ])
            return images
예제 #3
0
def create_keypoint_tf_record(data_set, region, random_seed):
    np.random.seed(random_seed)

    if region == 'face':
        keypoint = FACE
    elif region == 'butt':
        keypoint = BUTT

    ids = Table().read_table(dp.ids_by_data_set(data_set)).column('Id')

    tf_record_name = '{}_keypoint'.format(keypoint.name)
    print('Creating: {0}_{1}'.format(data_set, tf_record_name))
    writer = tf.python_io.TFRecordWriter(
        dp.tf_records(data_set, tf_record_name))

    base_xml_path = dp.REPO_HOME_PATH + '/data/bbox/' + keypoint.name
    base_png_path = dp.LARGE_DATA_BIN + '/data/raw/'
    if keypoint.file_format == 'a3daps':
        base_png_path += 'a3daps_png/'
    else:
        base_png_path += 'aps_png/'
    for _id in ids:
        for i in keypoint.slices:
            image = np.array(
                Image.open('{0}/{1}/{1}_{2}.png'.format(
                    base_png_path, i, _id))).astype(np.float32)
            xml_path = '{0}/{1}_{2}.xml'.format(base_xml_path, i, _id)
            if os.path.exists(xml_path):
                bboxes = np.array(parse_bbox_xml(xml_path)).ravel()
                bboxes = np.append(bboxes, (BOX_COUNT * 4 - len(bboxes)) *
                                   [0]).astype(np.int64)
                if np.sum(bboxes) == 0:
                    continue
                example = tf.train.Example(features=tf.train.Features(
                    feature={
                        'image': _bytes_feature(image.tostring()),
                        'bbox': _bbox_feature(bboxes),
                    }))
                writer.write(example.SerializeToString())
    writer.close()
예제 #4
0
    def minibatch(self):
        with tf.name_scope('batch_processing'):
            images = []
            bboxes = []
            labels = []
            slcs = []

            record_input = data_flow_ops.RecordInput(
                file_pattern=dp.tf_records(
                    self.data_set, '{}_localization'.format(self.region)),
                seed=301,
                parallelism=64,
                buffer_size=5000,
                shift_ratio=0.2,
                batch_size=self.batch_size,
                name='record_input')
            records = record_input.get_yield_op()
            records = tf.split(records, self.batch_size, 0)
            records = [tf.reshape(record, []) for record in records]
            for i in xrange(self.batch_size):
                value = records[i]
                image, bbox, dim, label, slc = self._parse_example_proto(
                    value, i)
                image = tf.image.resize_images(image, self.image_shape)
                bbox = tf.cast(
                    tf.cast(bbox, tf.float32) * float(IMAGE_LENGTH) /
                    tf.cast(dim, tf.float32), tf.int64)
                image, bbox = self.distort_image(image, bbox)
                unmaked_image = image
                bbox = tf.cast(bbox, tf.int32)
                bbox_mask = tf.ones((bbox[3] - bbox[1], bbox[2] - bbox[0]))
                bbox_mask_left = tf.zeros((bbox[3] - bbox[1], bbox[0]))
                bbox_mask_right = tf.zeros(
                    (bbox[3] - bbox[1], IMAGE_LENGTH - bbox[2]))
                bbox_mask_top = tf.zeros((bbox[1], IMAGE_LENGTH))
                bbox_mask_bottom = tf.zeros(
                    (IMAGE_LENGTH - bbox[3], IMAGE_LENGTH))
                bbox_mask = tf.concat(
                    (bbox_mask_left, bbox_mask, bbox_mask_right), axis=1)
                bbox_mask = tf.concat(
                    (bbox_mask_top, bbox_mask, bbox_mask_bottom), axis=0)
                bbox_mask *= 150
                image = image[:, :, 0]
                image -= self.mean_subtract
                image += bbox_mask
                image = tf.minimum(image, 255)
                bbox_mask.set_shape((IMAGE_LENGTH, IMAGE_LENGTH))
                images.append(image)
                bbox = tf.cast(bbox, tf.int64)
                bboxes.append(bbox)
                labels.append(label)
                slcs.append(slc)
            images = tf.parallel_stack(images)

            images = tf.reshape(images,
                                shape=[
                                    self.batch_size, self.image_shape[0],
                                    self.image_shape[1], -1
                                ])
            bboxes = tf.reshape(bboxes, (self.batch_size, BOX_COUNT, 4))
            x_min, y_min, x_max, y_max = tf.split(value=tf.reshape(
                bboxes, (-1, 4)),
                                                  num_or_size_splits=4,
                                                  axis=1)
            normalized_boxes = tf.cast(
                tf.reshape(tf.stack((y_min, x_min, y_max, x_max), axis=1),
                           (self.batch_size, BOX_COUNT, 4)),
                tf.float32) / float(IMAGE_LENGTH)
            # images = tf.image.draw_bounding_boxes(images, normalized_boxes)
            labels = tf.reshape(labels, (self.batch_size, 1))
            slcs = tf.reshape(slcs, (self.batch_size, ))
            return images, labels, slcs
예제 #5
0
def _create_localization_tf_record(data_set, region, random_seed):
    np.random.seed(random_seed)

    ids = Table().read_table(dp.ids_by_data_set(data_set)).column('Id')

    tf_record_name = '{0}_localization'.format(region)
    print('Creating: {0}_{1}'.format(data_set, tf_record_name))
    writer = tf.python_io.TFRecordWriter(
        dp.tf_records(data_set, tf_record_name))

    base_xml_path = '{0}/data/bbox/aps_threats/zone_'.format(dp.REPO_HOME_PATH)

    if region == 'arm':
        zones = {1: 0, 2: 1, 3: 2, 4: 3}
    if region == 'thigh':
        zones = {8: 0, 9: 1, 10: 2, 11: 3, 12: 4}

    masks = [
        make_vertical_stripe_mask((4, 4), (4, 4)),
        make_vertical_stripe_mask((4, 4), (4, 15)),
        make_horizontal_stripe_mask((4, 4), (4, 4)),
        make_horizontal_stripe_mask((4, 4), (15, 4)),
        make_squares_mask((10, 10), (10, 25)),
        make_squares_mask((5, 5), (10, 25)),
        make_squares_mask((5, 5), (5, 15)),
        make_vertical_stripe_mask((2, 2), (2, 2))
    ]

    examples = []
    for _id in ids:
        for slc in range(16):
            for zone in zones.keys():
                xml_path = base_xml_path + '{0}/{1}_{2}.xml'.format(
                    zone, slc, _id)
                if os.path.exists(xml_path):
                    boxes = parse_bbox_xml(xml_path)
                    if len(boxes) > 0:
                        examples += [(_id, slc, zone)]

    np.random.shuffle(examples)

    for _id, slc, zone in examples:
        image, cropped_bbox = crop_and_pad(_id,
                                           region,
                                           'aps',
                                           slc,
                                           keypoint_pad=None)
        pad = pad_dim((cropped_bbox[3] - cropped_bbox[1],
                       cropped_bbox[2] - cropped_bbox[0]))
        xml_path = base_xml_path + '{0}/{1}_{2}.xml'.format(zone, slc, _id)

        box = parse_bbox_xml(xml_path)[0]
        rescaled_box = [
            box[0] - cropped_bbox[0] + pad[1],
            box[1] - cropped_bbox[1] + pad[0],
            box[2] - cropped_bbox[0] + pad[1],
            box[3] - cropped_bbox[1] + pad[0]
        ]
        rescaled_box[0] = max(rescaled_box[0], pad[1])
        rescaled_box[1] = max(rescaled_box[1], pad[0])
        rescaled_box[2] = min(rescaled_box[2],
                              cropped_bbox[2] - cropped_bbox[0] + pad[1])
        rescaled_box[3] = min(rescaled_box[3],
                              cropped_bbox[3] - cropped_bbox[1] + pad[0])
        bboxes = np.array(rescaled_box).astype(np.int64)
        image = image.astype(np.float32)
        label = zones[zone]
        example = tf.train.Example(features=tf.train.Features(
            feature={
                'image':
                _bytes_feature(image.tostring()),
                'bbox':
                _bbox_feature(bboxes),
                'dim':
                tf.train.Feature(int64_list=tf.train.Int64List(
                    value=[image.shape[0]])),
                'label':
                tf.train.Feature(int64_list=tf.train.Int64List(value=[label])),
                'slc':
                tf.train.Feature(int64_list=tf.train.Int64List(value=[slc]))
            }))
        writer.write(example.SerializeToString())
        if 'train' in data_set:
            for _ in range(len(masks) - 1):
                writer.write(example.SerializeToString())
            for mask in masks:
                masked_image = (image * mask(image.shape)).astype(np.float32)
                example = tf.train.Example(features=tf.train.Features(
                    feature={
                        'image':
                        _bytes_feature(masked_image.tostring()),
                        'bbox':
                        _bbox_feature(bboxes),
                        'dim':
                        tf.train.Feature(int64_list=tf.train.Int64List(
                            value=[image.shape[0]])),
                        'label':
                        tf.train.Feature(int64_list=tf.train.Int64List(
                            value=[label])),
                        'slc':
                        tf.train.Feature(int64_list=tf.train.Int64List(
                            value=[slc]))
                    }))
                writer.write(example.SerializeToString())
    writer.close()
예제 #6
0
def _create_full_tf_record(data_set,
                           file_format,
                           region,
                           keypoint_pad,
                           random_seed,
                           example_multiple=3):
    np.random.seed(random_seed)

    ids = Table().read_table(dp.ids_by_data_set(data_set)).column('Id')

    tf_record_name = '{0}_crop_patch_full_{1}'.format(file_format, region)
    print('Creating: {0}_{1}'.format(data_set, tf_record_name))
    writer = tf.python_io.TFRecordWriter(
        dp.tf_records(data_set, tf_record_name))

    base_xml_path = '{0}/data/bbox/{1}_merged_threats_{2}'.format(
        dp.REPO_HOME_PATH, file_format, region)

    masks = [
        make_vertical_stripe_mask((4, 4), (4, 4)),
        make_vertical_stripe_mask((4, 4), (4, 15)),
        make_horizontal_stripe_mask((4, 4), (4, 4)),
        make_horizontal_stripe_mask((4, 4), (15, 4)),
        make_squares_mask((10, 10), (10, 25)),
        make_squares_mask((5, 5), (10, 25)),
        make_squares_mask((5, 5), (5, 15)),
        make_vertical_stripe_mask((2, 2), (2, 2))
    ]

    if file_format == 'aps':
        slcs = range(16)
    if file_format == 'a3daps':
        slcs = [t for t in range(64) if t % 2 == 0]

    existing_paths, non_existing_paths = 0, 0
    for _id in ids:
        for i in slcs:
            xml_path = '{0}/{1}_{2}.xml'.format(base_xml_path, i, _id)
            if os.path.exists(xml_path):
                existing_paths += 1
            else:
                non_existing_paths += 1

    ids_and_slcs = []
    for _id in ids:
        for i in slcs:
            xml_path = '{0}/{1}_{2}.xml'.format(base_xml_path, i, _id)
            if os.path.exists(xml_path):
                ids_and_slcs += example_multiple * [(_id, i)]
            elif np.random.binomial(
                    1,
                    min(1, example_multiple * existing_paths /
                        non_existing_paths)):
                ids_and_slcs += [((_id, i))]
    np.random.shuffle(ids_and_slcs)
    for _id, i in ids_and_slcs:
        image, cropped_bbox = crop_and_pad(_id,
                                           region,
                                           file_format,
                                           i,
                                           keypoint_pad=keypoint_pad)
        pad = pad_dim((cropped_bbox[3] - cropped_bbox[1],
                       cropped_bbox[2] - cropped_bbox[0]))
        xml_path = '{0}/{1}_{2}.xml'.format(base_xml_path, i, _id)

        if os.path.exists(xml_path):
            bboxes = parse_bbox_xml(xml_path)
            if np.sum(np.array(bboxes).ravel()) == 0:
                continue
            cropped_bboxes = []
            for box in bboxes:
                rescaled_box = [
                    box[0] - cropped_bbox[0] + pad[1],
                    box[1] - cropped_bbox[1] + pad[0],
                    box[2] - cropped_bbox[0] + pad[1],
                    box[3] - cropped_bbox[1] + pad[0]
                ]
                rescaled_box[0] = max(rescaled_box[0], pad[1])
                rescaled_box[1] = max(rescaled_box[1], pad[0])
                rescaled_box[2] = min(
                    rescaled_box[2],
                    cropped_bbox[2] - cropped_bbox[0] + pad[1])
                rescaled_box[3] = min(
                    rescaled_box[3],
                    cropped_bbox[3] - cropped_bbox[1] + pad[0])
                cropped_bboxes.append(rescaled_box)
            bboxes = np.array(cropped_bboxes).ravel()
            bboxes = np.append(bboxes, (BOX_COUNT * 4 - len(bboxes)) *
                               [0]).astype(np.int64)
        else:
            bboxes = np.array(BOX_COUNT * 4 * [0]).astype(np.int64)

        image = image.astype(np.float32)
        example = tf.train.Example(features=tf.train.Features(
            feature={
                'image':
                _bytes_feature(image.tostring()),
                'bbox':
                _bbox_feature(bboxes),
                'dim':
                tf.train.Feature(int64_list=tf.train.Int64List(
                    value=[image.shape[0]]))
            }))
        writer.write(example.SerializeToString())
        if 'train' in data_set:
            for _ in range(len(masks) - 1):
                writer.write(example.SerializeToString())
            for mask in masks:
                masked_image = (image * mask(image.shape)).astype(np.float32)
                example = tf.train.Example(features=tf.train.Features(
                    feature={
                        'image':
                        _bytes_feature(masked_image.tostring()),
                        'bbox':
                        _bbox_feature(bboxes),
                        'dim':
                        tf.train.Feature(int64_list=tf.train.Int64List(
                            value=[image.shape[0]]))
                    }))
                writer.write(example.SerializeToString())
    writer.close()
예제 #7
0
def _create_pure_tf_record(data_set,
                           file_format,
                           region,
                           keypoint_pad,
                           random_seed,
                           example_multiple=3):
    np.random.seed(random_seed)

    ids = Table().read_table(dp.ids_by_data_set(data_set)).column('Id')

    tf_record_name = '{0}_crop_patch_pure_{1}'.format(file_format, region)
    print('Creating: {0}_{1}'.format(data_set, tf_record_name))
    writer = tf.python_io.TFRecordWriter(
        dp.tf_records(data_set, tf_record_name))

    base_xml_path = '{0}/data/bbox/{1}_merged_threats_{2}'.format(
        dp.REPO_HOME_PATH, file_format, region)

    if file_format == 'aps':
        slcs = range(16)
    if file_format == 'a3daps':
        slcs = [t for t in range(64) if t % 2 == 0]

    ids_and_slcs = []
    for _id in ids:
        for i in slcs:
            xml_path = '{0}/{1}_{2}.xml'.format(base_xml_path, i, _id)
            if os.path.exists(xml_path):
                ids_and_slcs += [(_id, i)]

    np.random.shuffle(ids_and_slcs)

    for _id, i in ids_and_slcs:
        image, cropped_bbox = crop_and_pad(_id,
                                           region,
                                           file_format,
                                           i,
                                           keypoint_pad=keypoint_pad)
        pad = pad_dim((cropped_bbox[3] - cropped_bbox[1],
                       cropped_bbox[2] - cropped_bbox[0]))
        xml_path = '{0}/{1}_{2}.xml'.format(base_xml_path, i, _id)

        if os.path.exists(xml_path):
            bboxes = parse_bbox_xml(xml_path)
            if np.sum(np.array(bboxes).ravel()) == 0:
                continue
            cropped_bboxes = []
            for box in bboxes:
                rescaled_box = [
                    box[0] - cropped_bbox[0] + pad[1],
                    box[1] - cropped_bbox[1] + pad[0],
                    box[2] - cropped_bbox[0] + pad[1],
                    box[3] - cropped_bbox[1] + pad[0]
                ]
                rescaled_box[0] = max(rescaled_box[0], pad[1])
                rescaled_box[1] = max(rescaled_box[1], pad[0])
                rescaled_box[2] = min(
                    rescaled_box[2],
                    cropped_bbox[2] - cropped_bbox[0] + pad[1])
                rescaled_box[3] = min(
                    rescaled_box[3],
                    cropped_bbox[3] - cropped_bbox[1] + pad[0])
                cropped_bboxes.append(rescaled_box)
            bboxes = np.array(cropped_bboxes).ravel()
            bboxes = np.append(bboxes, (BOX_COUNT * 4 - len(bboxes)) *
                               [0]).astype(np.int64)
        else:
            continue

        image = image.astype(np.float32)
        example = tf.train.Example(features=tf.train.Features(
            feature={
                'image':
                _bytes_feature(image.tostring()),
                'bbox':
                _bbox_feature(bboxes),
                'dim':
                tf.train.Feature(int64_list=tf.train.Int64List(
                    value=[image.shape[0]]))
            }))
        writer.write(example.SerializeToString())
    writer.close()