tensorflow - Using height, width information stored in a TFRecords file to set shape of a Tensor -


i have converted directory of images , labels tfrecords file, feature maps include image_raw, label, height, width , depth. function follows:

def convert_to_tfrecords(data_samples, filename):     def _int64_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]))     writer = tf.python_io.tfrecordwriter(filename)     fname, lb in data_samples:         im = cv2.imread(fname, cv2.imread_unchanged)         image_raw = im.tostring()         feats = tf.train.features(             feature =             {                 'image_raw': _bytes_feature(image_raw),                 'label': _int64_feature(int(lb)),                 'height': _int64_feature(im.shape[0]),                 'width': _int64_feature(im.shape[1]),                 'depth': _int64_feature(im.shape[2])             }         )         example = tf.train.example(features=feats)         writer.write(example.serializetostring())     writer.close() 

now, read tfrecords file feed input pipeline. however, since image_raw has been flattened, need reshape original [height, width, depth] size. how can values of height, width , depth tfrecords file? seems following code cannot work because height tensor without values.

def read_and_decode(filename_queue):     reader = tf.tfrecordreader()     _, serialized_example = reader.read(filename_queue)     feats = {         'image_raw': tf.fixedlenfeature([], tf.string),         'label': tf.fixedlenfeature([], tf.int64),         'height': tf.fixedlenfeature([], tf.int64),         'width': tf.fixedlenfeature([], tf.int64),         'depth': tf.fixedlenfeature([], tf.int64)     }     features = tf.parse_single_example(serialized_example, features=feats)     image = tf.decode_raw(features['image_raw'], tf.uint8)     label = tf.cast(features['label'], tf.int32)     height = tf.cast(features['height'], tf.int32)     width = tf.cast(features['width'], tf.int32)     depth = tf.cast(features['depth'], tf.int32)     image = tf.reshape(image, [height, width, depth]) # <== not work     image = tf.cast(image, tf.float32) * (1. / 255) - 0.5     return image, label 

when read tensorflow's official documents, found pass known size, saying [224,224,3]. however, don't it, because information has been stored tfrecords file, , manually passing fixed size cannot ensure size consistent data stored in file.

so ideas?

the height returned tf.parse_single_example tensor, , way value call session.run() on it, or similar. however, think that's overkill.

since tensorflow example protocol buffer (see documentation), don't have use tf.parse_single_example read it. instead parse , read shapes want out directly.

you might consider filing feature request on tensorflow's github issues tracker --- agree api seems bit awkward use case.


Comments

Popular posts from this blog

python - How to insert QWidgets in the middle of a Layout? -

python - serve multiple gunicorn django instances under nginx ubuntu -

module - Prestashop displayPaymentReturn hook url -