Read data from TFRecord file used in Object Detection API
Asked Answered
S

1

8

I want to read the data stored in a TFRecord file that I've used as a train record in TF Object Detection API.

However, I get an InvalidArgumentError: Input to reshape is a tensor with 91090 values, but the requested shape has 921600. I don't understand what the root of the error is, even though the discrepancy seems to be a factor of 10.

Question: How can I read the file without this error?

  • I can't rule out that the error is from creating the record, or if the error is in how I read it. Therefore, I've included my code for both.
  • I'm able to run object_detection/train.py with the data, AND generate a frozen graph from the trained model.
  • From this answer (and its mentioned GitHub issue), I figured out that I had to convert my PNG images into JPG, hence the as_jpg-part (see my code below).
  • I used the code from this answer as a starting point to read the file.
  • I use Tensorflow 1.7.0, Python 3.5

There is only one class: "Human". The record has 1000 images; each image can have a single bounding box, or multiple. (One for each human in the respective image.)

How I read the TFRecord: As mentioned above: I used the code from this answer as a starting point to read the file:

train_record = 'train.record'

def read_and_decode(filename_queue):
    reader = tf.TFRecordReader()
    _, serialized_example = reader.read(filename_queue)
    features = tf.parse_single_example(
        serialized_example,
        # Defaults are not specified since both keys are required.
        features={
            'image/height': tf.FixedLenFeature([], tf.int64),
            'image/width': tf.FixedLenFeature([], tf.int64),
            'image/source_id': tf.FixedLenFeature([], tf.string),
            'image/encoded': tf.FixedLenFeature([], tf.string),
            'image/format': tf.FixedLenFeature([], tf.string),
            'image/object/bbox/xmin': tf.VarLenFeature(tf.float32),
            'image/object/bbox/xmax': tf.VarLenFeature(tf.float32),
            'image/object/bbox/ymin': tf.VarLenFeature(tf.float32),
            'image/object/bbox/ymax': tf.VarLenFeature(tf.float32),
            'image/object/class/text': tf.VarLenFeature(tf.string),
            'image/object/class/label': tf.VarLenFeature(tf.int64)
        })
    image = tf.decode_raw(features['image/encoded'], tf.uint8)
    # label = tf.cast(features['image/object/class/label'], tf.int32)
    height = tf.cast(features['image/height'], tf.int32)
    width = tf.cast(features['image/width'], tf.int32)
    return image, height, width

def get_all_records(FILE):
    with tf.Session() as sess:
        filename_queue = tf.train.string_input_producer([ FILE ])
        image, height, width = read_and_decode(filename_queue)
        image = tf.reshape(image, tf.stack([height, width, 3]))
        image.set_shape([640,480,3])
        init_op = tf.initialize_all_variables()
        sess.run(init_op)
        coord = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(coord=coord)
        for i in range(1):
            example, l = sess.run([image])
            img = Image.fromarray(example, 'RGB')
            img.save( "output/" + str(i) + '-train.png')

            print (example,l)
        coord.request_stop()
        coord.join(threads)


get_all_records(train_record)

Creation:

I've made a class Image to logically model the image, and a class Rect to represent the bounding boxes and labels. This is not very relevant, but the code below is makes use of them when variable img or rect is seen.

A relevant part might be the get_bytes()-method, which is more a wrapper for using PIL's Image.open(file_path):

class Image:

    # ... rest of class 


    def open_img(self):
        if self.file_path is not None:
            return Image.open(self.file_path)

    def get_bytes(self, as_jpg=False):
        if self.file_path is None:
             return None
        if as_jpg:
            # Convert to jpg:
            with BytesIO() as f:
                self.open_img().convert('RGB').save(f, format='JPEG', quality=95)
                return f.getvalue()
        else:  # Assume png
            return np.array(self.open_img().convert('RGB')).tobytes()

How I created the Examples:

use_jpg = True

def create_tf_example(img):
    image_format= b'jpg' if use_jpg else b'png'
    encoded_image_data = img.get_bytes(as_jpg=use_jpg) # Encoded image bytes

    relative_path = img.get_file_path()
    if relative_path is None or not img.has_person():
        return None  # Ignore images without humans or image data
    else:
        filename = str(Path(relative_path).resolve()) # Absolute filename of the image. Empty if image is not from file

    xmins = []  # List of normalized left x coordinates in bounding box (1 per box)
    xmaxs = []  # List of normalized right x coordinates in bounding box (1 per box)
    ymins = []  # List of normalized top y coordinates in bounding box (1 per box)
    ymaxs = []  # List of normalized bottom y coordinates in bounding box (1 per box)
    classes_text = []  # List of string class name of bounding box (1 per box)
    classes = []  # List of integer class id of bounding box (1 per box)

    for rect in img.rects:
        if not rect.is_person:
            continue  # For now, ignore negative samples as TF does this by default
        else:
            xmin, xmax, ymin, ymax = rect.get_normalized_xy_min_max()
            xmins.append(xmin)
            xmaxs.append(xmax)
            ymins.append(ymin)
            ymaxs.append(ymax)
            # Human class:
            classes.append(1)
            classes_text.append('Human'.encode())

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

How I created the TFRecord:

def convert_to_tfrecord(imgs, output_file_path):
    with tf.python_io.TFRecordWriter(output_file_path) as writer:
        for img in imgs:
            tf_example = create_tf_example(img)
            if tf_example is not None:
                writer.write(tf_example.SerializeToString())


convert_to_tfrecord(train_imgs, 'train.record')
convert_to_tfrecord(validation_imgs, 'validate.record')
convert_to_tfrecord(test_imgs, 'test.record')

From the dataset_util module:

def int64_feature(value):
    return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))


def int64_list_feature(value):
    return tf.train.Feature(int64_list=tf.train.Int64List(value=value))


def bytes_feature(value):
    return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))


def bytes_list_feature(value):
    return tf.train.Feature(bytes_list=tf.train.BytesList(value=value))


def float_list_feature(value):
    return tf.train.Feature(float_list=tf.train.FloatList(value=value))
Smokeproof answered 23/4, 2018 at 17:12 Comment(0)
S
3

I resolved the issue by decoding the data as jpeg with tf.image.decode_jpeg.

Instead of:

def read_and_decode(filename_queue):
    # ...

    image = tf.decode_raw(features['image/encoded'], tf.uint8)

    # ...

I did:

def read_and_decode(filename_queue):
    # ...

    image = tf.image.decode_jpeg(features['image/encoded'])

    # ...

This explains the reason for why the difference between the expected size and the given size were so big: the given (read) bytes were "only" compressed JPEG data, and not a "complete" bitmap image of full size.

Smokeproof answered 23/4, 2018 at 20:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.