예제 #1
0
    def dict_to_coco_example(img_data):
    """Convert python dictionary formath data of one image to tf.Example proto.
    Args:
        img_data: infomation of one image, inclue bounding box, labels of bounding box,\
            height, width, encoded pixel data.
    Returns:
        example: The converted tf.Example
    """
    bboxes = img_data['bboxes']
    xmin, xmax, ymin, ymax = [], [], [], []
    for bbox in bboxes:
        xmin.append(bbox[0])
        xmax.append(bbox[0] + bbox[2])
        ymin.append(bbox[1])
        ymax.append(bbox[1] + bbox[3])

    example = tf.train.Example(features=tf.train.Features(feature={
        'image/height': utils.int64_feature(img_data['height']),
        'image/width': utils.int64_feature(img_data['width']),
        'image/object/bbox/xmin': utils.float_list_feature(xmin),
        'image/object/bbox/xmax': utils.float_list_feature(xmax),
        'image/object/bbox/ymin': utils.float_list_feature(ymin),
        'image/object/bbox/ymax': utils.float_list_feature(ymax),
        'image/object/class/label': utils.int64_list_feature(img_data['labels']),
        'image/encoded': utils.bytes_feature(img_data['pixel_data']),
        'image/format': utils.bytes_feature('jpeg'.encode('utf-8')),
    }))
    return example
def create_tf_example(filename, encoded_jpeg, annotations, resize=True):
    """
    This function create a tf.train.Example from the Waymo frame.

    args:
        - filename [str]: name of the image
        - encoded_jpeg [bytes]: jpeg encoded image
        - annotations [protobuf object]: bboxes and classes

    returns:
        - tf_example [tf.Train.Example]: tf example in the objection detection api format.
    """
    if not resize:
        encoded_jpg_io = io.BytesIO(encoded_jpeg)
        image = Image.open(encoded_jpg_io)
        width, height = image.size
        width_factor, height_factor = image.size
    else:
        image_tensor = tf.io.decode_jpeg(encoded_jpeg)
        height_factor, width_factor, _ = image_tensor.shape
        image_res = tf.cast(tf.image.resize(image_tensor, (640, 640)), tf.uint8)
        encoded_jpeg = tf.io.encode_jpeg(image_res).numpy()
        width, height = 640, 640

    mapping = {1: 'vehicle', 2: 'pedestrian', 4: 'cyclist'}
    image_format = b'jpg'
    xmins = []
    xmaxs = []
    ymins = []
    ymaxs = []
    classes_text = []
    classes = []
    filename = filename.encode('utf8')

    for ann in annotations:
        xmin, ymin = ann.box.center_x - 0.5 * ann.box.length, ann.box.center_y - 0.5 * ann.box.width
        xmax, ymax = ann.box.center_x + 0.5 * ann.box.length, ann.box.center_y + 0.5 * ann.box.width
        xmins.append(xmin / width_factor)
        xmaxs.append(xmax / width_factor)
        ymins.append(ymin / height_factor)
        ymaxs.append(ymax / height_factor)
        classes.append(ann.type)
        classes_text.append(mapping[ann.type].encode('utf8'))

    tf_example = tf.train.Example(features=tf.train.Features(feature={
        'image/height': int64_feature(height),
        'image/width': int64_feature(width),
        'image/filename': bytes_feature(filename),
        'image/source_id': bytes_feature(filename),
        'image/encoded': bytes_feature(encoded_jpeg),
        'image/format': bytes_feature(image_format),
        'image/object/bbox/xmin': float_list_feature(xmins),
        'image/object/bbox/xmax': float_list_feature(xmaxs),
        'image/object/bbox/ymin': float_list_feature(ymins),
        'image/object/bbox/ymax': float_list_feature(ymaxs),
        'image/object/class/text': bytes_list_feature(classes_text),
        'image/object/class/label': int64_list_feature(classes),
    }))
    return tf_example
예제 #3
0
def create_tf_example(filename, encoded_jpeg, annotations):
    """
    convert to tensorflow object detection API format
    args:
    - filename [str]: name of the image
    - encoded_jpeg [bytes-likes]: encoded image
    - annotations [list]: bboxes and classes
    returns:
    - tf_example [tf.Example]
    """
    # TO BE IMPLEMENTED
    encoded_jpg_io = io.BytesIO(encoded_jpeg)
    image = Image.open(encoded_jpg_io)
    width, height = image.size

    mapping = {1: 'vehicle', 2: 'pedestrian', 4: 'cyclist'}

    image_format = b'jpg'

    xmins = []
    xmaxs = []
    ymins = []
    ymaxs = []

    classes_text = []
    classes = []

    filename = filename.encode('utf8')

    for ann in annotations:
        xmin, ymin = ann.box.center_x - 0.5 * ann.box.length, ann.box.center_y - 0.5 * ann.box.width
        xmax, ymax = ann.box.center_x + 0.5 * ann.box.length, ann.box.center_y + 0.5 * ann.box.width

        xmins.append(xmin / width)
        xmaxs.append(xmax / width)
        ymins.append(ymin / height)
        ymaxs.append(ymax / height)

        classes.append(ann.type)
        classes_text.append(mapping[ann.type].encode('utf8'))

        tf_example = tf.train.Example(features=tf.train.Features(
            feature={
                'image/height': int64_feature(height),
                'image/width': int64_feature(width),
                'image/filename': bytes_feature(filename),
                'image/source_id': bytes_feature(filename),
                'image/encoded': bytes_feature(encoded_jpeg),
                'image/format': bytes_feature(image_format),
                'image/object/bbox/xmin': float_list_feature(xmins),
                'image/object/bbox/xmax': float_list_feature(xmaxs),
                'image/object/bbox/ymin': float_list_feature(ymins),
                'image/object/bbox/ymax': float_list_feature(ymaxs),
                'image/object/class/text': bytes_list_feature(classes_text),
                'image/object/class/label': int64_list_feature(classes)
            }))

    return tf_example
예제 #4
0
def create_tf_example(image_buffer, label, width, height):
    image_format = b'png'
    return tf.train.Example(features=tf.train.Features(
        feature={
            'image/height': utils.int64_feature(height),
            'image/width': utils.int64_feature(width),
            'image/encoded': utils.bytes_feature(image_buffer),
            'image/format': utils.bytes_feature(image_format),
            'image/object/class/label': utils.int64_feature(label)
        }))
def img_to_tfrecord(train_imgs, train_labels, output_dir, name):
    """

    :param image_dir: image_dir just like "data/Challenge2_Training_Task12_Images/*.jpg"
    :param text_dir: label file dir
    :param text_name: label file name
    :param output_dir: output dir
    :param name: output file name
    :return: NULL
    """

    tf_filename = _get_output_filename(output_dir, name)

    imgLists = train_imgs  # return a list
    labels = train_labels

    labels_encord, lengths = encode_labels(labels, alphabet)

    image_format = b'JPEG'
    with tf.python_io.TFRecordWriter(tf_filename) as tfrecord_writer:
        for i, filename in enumerate(imgLists):
            sys.stdout.write('\r>> Converting image %d/%d' %
                             (i + 1, len(imgLists)))
            sys.stdout.flush()
            example = tf.train.Example(features=tf.train.Features(
                feature={
                    "label/value": int64_feature(labels_encord[i]),
                    "label/length": int64_feature(lengths[i]),
                    "image": bytes_feature(filename)
                }))
            tfrecord_writer.write(example.SerializeToString())
    print('\nFinished converting the dataset!')
예제 #6
0
def img2tfrecord(image_dir, label_path, output_path, debug=False):
    """

    :param image_dir: image_dir just like "****/****/*.jpg"
    :param label_path: image pathlabeltext
    :param output_path: output path
    :return: NULL
    """

    tf_filename = _get_output_filename(output_path)
    labels = []
    indexs = []
    imgLists = os.listdir(image_dir)
    with open(label_path) as f:
        for i, line in enumerate(f):
            if i == 0:
                continue
            sline = line.split('')
            if len(sline) <> 3:
                if debug == True:
                    print tc.UseStyle(
                        "[WARN] length of label file is invalid.length=%d" %
                        (len(sline)),
                        fore='red')
                continue
            indexs.append(sline[0])
            labels.append(sline[1])

    image_format = b'JPEG'
    with tf.python_io.TFRecordWriter(tf_filename) as tfrecord_writer:
        for i, filename in enumerate(indexs):
            sys.stdout.write('\r>> Converting image %d/%d' %
                             (i + 1, len(imgLists)))
            sys.stdout.flush()
            image_data = tf.gfile.FastGFile(image_dir + "/" + filename,
                                            'rb').read()

            example = tf.train.Example(features=tf.train.Features(
                feature={
                    "label": bytes_feature(labels[i]),
                    "image/encoded": bytes_feature(image_data),
                    'image/format': bytes_feature(image_format)
                }))
            tfrecord_writer.write(example.SerializeToString())
    print('\nFinished converting the dataset!')
    def convert_batch(self, image_paths, labels, out_path):
        """
        Convert Images at image_paths to tfRecords at output
    """
        writer = tf.python_io.TFRecordWriter(out_path)

        for count, label in enumerate(labels):
            img = np.array(
                (load_img(image_paths[count],
                          target_size=(self.img_size, self.img_size))))
            g_labels = label.astype(np.uint8)
            example = tf.train.Example(features=tf.train.Features(
                feature={
                    'image': utils.bytes_feature(img.tostring()),
                    'labels': utils.bytes_feature(g_labels.tostring())
                }))

            writer.write(example.SerializeToString())

        writer.close()
예제 #8
0
def _convert_to_example(image, label):
    example = tf.train.Example(features=tf.train.Features(
        feature={
            'image/height': int64_feature(image.shape[1]),
            'image/width': int64_feature(image.shape[0]),
            'image/ids': int64_feature(label),
            # 'image/colorspace': _bytes_feature(tf.compat.as_bytes(colorspace)),
            # 'image/channels': _int64_feature(channels),
            # 'image/class/label': _int64_feature(label),
            # 'image/class/text': _bytes_feature(tf.compat.as_bytes(text)),
            # 'image/format': _bytes_feature(tf.compat.as_bytes(image_format)),
            # 'image/filename': _bytes_feature(tf.compat.as_bytes(os.path.basename(filename))),
            'image/array': bytes_feature(image.tobytes(order='C'))
        }))
    return example
예제 #9
0
def save_tfrecord(data):
    coder = utils.ImageCoder()
    tf.gfile.MakeDirs(FLAGS.output_dir)
    for subset in data:
        if subset == 'train' or subset == 'extra':
            num_shards = 128
        else:
            num_shards = 64
        shard_spacing = np.linspace(0, data[subset]['images'].shape[0],
                                    num_shards + 1).astype(np.int)
        cnt = 0
        if subset == 'extra':
            output_dir = os.path.join(FLAGS.output_dir, 'unlabeled')
            tf.gfile.MakeDirs(output_dir)
        else:
            output_dir = FLAGS.output_dir
        for i in range(len(shard_spacing) - 1):
            if FLAGS.full_train_data:
                output_filename = '%s_full-%.5d-of-%.5d' % (subset, i,
                                                            num_shards)
            else:
                output_filename = '%s-%.5d-of-%.5d' % (subset, i, num_shards)
            if i % 10 == 0:
                tf.logging.info('saving {}'.format(output_filename))
            writer = tf.python_io.TFRecordWriter(
                os.path.join(output_dir, output_filename))
            for j in range(shard_spacing[i], shard_spacing[i + 1]):
                cnt += 1
                image = data[subset]['images'][j]
                label = data[subset]['labels'][j] + 1
                image_data = coder.encode_jpeg(image)
                features = {
                    'image/encoded': utils.bytes_feature(image_data),
                    'image/class/label': utils.int64_feature(label),
                }
                if subset == 'extra':
                    features['probabilities'] = utils.float_feature([0] * 10)
                    features['label'] = utils.int64_feature(0)
                    features['prob'] = utils.float_feature(0)
                example = tf.train.Example(features=tf.train.Features(
                    feature=features))
                writer.write(example.SerializeToString())
            writer.close()
        assert cnt == data[subset]['images'].shape[0]
예제 #10
0
def write_tfexamples(data, tfexample_path):

    colorspace = "RGB"
    channels = 1
    image_format = "JPEG"
    height = FLAGS.canvas_size
    width = FLAGS.canvas_size

    image_coder = utils.ImageCoder(FLAGS.canvas_size)

    builder = tf.python_io.TFRecordWriter(tfexample_path)
    tf.logging.info("Writing %s TF Example.", tfexample_path)

    for index, datum in enumerate(data):
        # Convert the contents of the datum into a tf example.

        image_encoded = image_coder.encode_image(datum.images)
        latents = [float(x) for x in datum.true_latents]
        labels = datum.attribute_labels

        example = tf.train.Example(features=tf.train.Features(
            feature={
                "image/height": utils.int64_feature(height),
                "image/width": utils.int64_feature(width),
                "image/colorspace": utils.string_feature(colorspace.encode()),
                "image/channels": utils.int64_feature(channels),
                "latents": utils.float_feature(latents),
                "labels": utils.int64_feature(labels),
                "image/format": utils.string_feature(image_format.encode()),
                "image/encoded": utils.bytes_feature(image_encoded)
            }))
        builder.write(example.SerializeToString())

        if index % 1000 == 0:
            tf.logging.info("Wrote %d datapoints to the tfexample.", index)

    builder.close()
예제 #11
0
 def get_image_list(features):
     dump_features = {}
     prob = keep_idx[worker_id][cnt][1]
     label = keep_idx[worker_id][cnt][0]
     include_copy = keep_idx[worker_id][cnt][2]
     image_bytes = features['image/encoded'][0]
     dump_features['image/encoded'] = utils.bytes_feature(image_bytes)
     dump_features['prob'] = utils.float_feature(prob)
     dump_features['probabilities'] = utils.float_feature(
         keep_idx[worker_id][cnt][3])
     dump_features['label'] = utils.int64_feature(label)
     example = tf.train.Example(features=tf.train.Features(
         feature=dump_features))
     cur_image_list = []
     for j in range(include_copy):
         image_info = {
             'example': example,
             'label': label,
             'prob': prob,
             'image_bytes': image_bytes,
             'cnt': cnt,
         }
         cur_image_list += [image_info]
     return cur_image_list
예제 #12
0
def _convert_to_example(embeddings):
    return [
        tf.train.Example(features=tf.train.Features(
            feature={'image/features': bytes_feature(feature.tobytes())}))
        for feature in embeddings
    ]
예제 #13
0
def run_prediction(estimator):
    global shard_id
    shard_id_list = FLAGS.shard_id.split(',')
    for cur_shard_id in shard_id_list:
        shard_id = int(cur_shard_id)

        worker_image_num = get_num_image()
        cnt, predict_result_list = predict_on_dataset(estimator,
                                                      worker_image_num)
        tf.logging.info('predicted on %d images', cnt)
        assert cnt == worker_image_num, (cnt, worker_image_num)

        tf.gfile.MakeDirs(FLAGS.output_dir)
        if FLAGS.reassign_label:
            sample_dir = os.path.join(FLAGS.output_dir, 'samples')
            uid_list = utils.get_uid_list()
            for uid in uid_list:
                tf.gfile.MakeDirs(os.path.join(sample_dir, uid))

            image_bytes_placeholder = tf.placeholder(dtype=tf.string)
            decoded_image = utils.decode_raw_image(image_bytes_placeholder)

            raw_dst = get_input_fn({'batch_size': 1}, raw_data=True)
            raw_iter = raw_dst.make_initializable_iterator()
            raw_elem = raw_iter.get_next()

            filename = utils.get_reassign_filename(FLAGS.label_data_dir,
                                                   FLAGS.file_prefix, shard_id,
                                                   FLAGS.num_shards,
                                                   FLAGS.worker_id)
            record_writer = tf.python_io.TFRecordWriter(
                os.path.join(FLAGS.output_dir, os.path.basename(filename)))
            sample_prob = 30000. / (worker_image_num * FLAGS.num_shards)
            with tf.Session() as sess:
                sess.run(raw_iter.initializer)
                for i in range(worker_image_num):
                    features = sess.run(raw_elem)
                    encoded_image = features['image/encoded']
                    features = {}
                    label = predict_result_list[i]['label']
                    prob = predict_result_list[i]['prob']
                    features['image/encoded'] = utils.bytes_feature(
                        encoded_image)
                    features['prob'] = utils.float_feature(prob)
                    features['label'] = utils.int64_feature(label)
                    features['probabilities'] = utils.float_feature(
                        predict_result_list[i]['probabilities'])
                    example = tf.train.Example(features=tf.train.Features(
                        feature=features))
                    record_writer.write(example.SerializeToString())
                    if np.random.random() < sample_prob:
                        uid = uid_list[label]
                        filename = os.path.join(
                            sample_dir, uid,
                            'image_{:d}_{:d}_{:.2f}.jpeg'.format(
                                shard_id, i, prob))
                        tf.logging.info('saving {:s}'.format(filename))
                        image = sess.run(
                            decoded_image,
                            feed_dict={image_bytes_placeholder: encoded_image})
                        utils.save_pic(image, filename)

            record_writer.close()
        else:
            filename = 'train-info-%.5d-of-%.5d-%.5d' % (
                shard_id, FLAGS.num_shards, FLAGS.worker_id)
            writer = tf.python_io.TFRecordWriter(
                os.path.join(FLAGS.output_dir, filename))
            for result in predict_result_list:
                features = {}
                features['probabilities'] = utils.float_feature(
                    result['probabilities'])
                features['classes'] = utils.int64_feature(result['label'])

                example = tf.train.Example(features=tf.train.Features(
                    feature=features))
                writer.write(example.SerializeToString())
            writer.close()
def img_to_tfrecord(train_imgs,train_labels,output_dir,name):
    """

    :param image_dir: image_dir just like "data/Challenge2_Training_Task12_Images/*.jpg"
    :param text_dir: label file dir
    :param text_name: label file name
    :param output_dir: output dir
    :param name: output file name
    :return: NULL
    """

    tf_filename = _get_output_filename(output_dir, name)

    imgLists = train_imgs  # return a list
    labels = train_labels

    labels_encord,lengths = encode_labels(labels)

    image_format = b'JPEG'
    with tf.python_io.TFRecordWriter(tf_filename) as tfrecord_writer:
        for i, filename in enumerate(imgLists):
            sys.stdout.write('\r>> Converting image %d/%d' % (i + 1, len(imgLists)))
            sys.stdout.flush()
            #image_data = tf.gfile.FastGFile(filename, 'rb').read()
            image_data = load_image(filename)
            #with tf.gfile.GFile(filename, 'rb') as fid:
            #    image_data = fid.read()
            # with tf.Session() as sess:
            #     image = tf.image.decode_jpeg(image_data)
            #     image = sess.run(image)
            #     print(image.shape)#(32, 100, 3)


            example = tf.train.Example(features=tf.train.Features(feature={"label/value": int64_feature(labels_encord[i]),
                                                                            "label/length": int64_feature(lengths[i]),
                                                                           "image/encoded": bytes_feature(image_data),
                                                                           'image/format': bytes_feature(image_format)}))
            tfrecord_writer.write(example.SerializeToString())
    print('\nFinished converting the dataset!')