TFRecord for video data

xiaoxiao2021-02-28  5

https://github.com/tensorflow/magenta/blob/master/magenta/common/sequence_example_lib.py https://www.tensorflow.org/api_docs/python/tf/train/SequenceExample https://github.com/tensorflow/tensorflow/blob/r1.5/tensorflow/core/example/example.proto http://leix.me/2017/01/09/tensorflow-practical-guides/#引言

tf.train.SequenceExample

tf.train.SequenceExample用于处理序列化数据。比如视频。

tf.train.SequenceExample( context=context, featureList = featurelist )

SequenceExample的参数有两个,分别是:context和feature_lists, 创建SequenceExample的原型如下,其中的feat类型是python列表。

官方的说明文档是这样写的:原文点这里

// A SequenceExample is an Example representing one or more sequences, and // some context. The context contains features which apply to the entire // example. The feature_lists contain a key, value map where each key is // associated with a repeated set of Features (a FeatureList). // A FeatureList thus represents the values of a feature identified by its key // over time / frames.

也就是说,context这个参数表示可以用于整个example的,比如说我告诉你这一个视频的名字什么的 feature_lists是一个词典,包含key和value的映射。

并且给出了一个例子,是针对对于一个用户来说对电影的评分记录。

// Below is a SequenceExample for a movie recommendation application recording a // sequence of ratings by a user. The time-independent features ("locale", // "age", "favorites") describing the user are part of the context. The sequence // of movies the user rated are part of the feature_lists. For each movie in the // sequence we have information on its name and actors and the user's rating. // This information is recorded in three separate feature_list(s). // In the example below there are only two movies. All three feature_list(s), // namely "movie_ratings", "movie_names", and "actors" have a feature value for // both movies. Note, that "actors" is itself a bytes_list with multiple // strings per movie. // context: { // feature: { // key : "locale" #地点 // value: { // bytes_list: { // value: [ "pt_BR" ] #葡萄牙语? // } // } // } // feature: { // key : "age" #年龄 // value: { // float_list: { // value: [ 19.0 ] // } // } // } // feature: { // key : "favorites" // value: { // bytes_list: { // value: [ "Majesty Rose", "Savannah Outen", "One Direction" ] // } // } // } // } // 这个feature_lists里面包含3个feature_lists,这三个feature_lists分别表示影片评分、影片名、演员。 // feature_lists: { // feature_list: { #影片评分,有两个分数,对应两个电影 // key : "movie_ratings" // value: { // feature: { // float_list: { // value: [ 4.5 ] // } // } // feature: { // float_list: { // value: [ 5.0 ] // } // } // } // } // feature_list: { // key : "movie_names" #上面对应的电影名字,一部肖申克救赎(真的好看),还有搏击俱乐部(也好看) // value: { // feature: { // bytes_list: { // value: [ "The Shawshank Redemption" ] // } // } // feature: { // bytes_list: { // value: [ "Fight Club" ] // } // } // } // } // feature_list: { #第三个feature_list,对应上面电影的演员 // key : "actors" // value: { // feature: { // bytes_list: { // value: [ "Tim Robbins", "Morgan Freeman" ]#摩根弗里曼演的好好哦 // } // } // feature: { // bytes_list: { // value: [ "Brad Pitt", "Edward Norton", "Helena Bonham Carter" ] // } // } // } // } // } //

所以如果要将视频当中的每一帧作为feature来存:

context: { feature: { key : "width" #宽 value: { int64_list: { value: [ 320 ] } } } feature: { key : "height" #高 value: { int64_list: { value: [ 240 ] } } } feature: { key : "length" #帧数 value: { int64_list: { value: [ 210 ] } } feature: { key : "label" #这段视频的标签 value: { bytes_list: { value: [ "brush_hair" ] } } } } feature_lists里面包含所有的frames feature_lists: { feature_list: { 对应视频帧 key : "frames" value: { feature: { bytes_list: { value: [ frame1 ] } } feature: { bytes_list: { value: [ frame2 ] } } . .. ... .... ... .. . feature: { float_list: { value: [ last_frame ] } } } } }

参考官方示例代码:

def make_sequence_example(inputs, labels): """Returns a SequenceExample for the given inputs and labels. Args: inputs: A list of input videos. Each input is a list of images(which is a numpy array). labels: A list of ints. Returns: A tf.train.SequenceExample containing inputs and labels. """ input_features = [ tf.train.Feature(float_list=tf.train.FloatList(value=input_)) for input_ in inputs] label_features = [ tf.train.Feature(int64_list=tf.train.Int64List(value=[label])) for label in labels] feature_list = { 'inputs': tf.train.FeatureList(feature=input_features), 'labels': tf.train.FeatureList(feature=label_features) } feature_lists = tf.train.FeatureLists(feature_list=feature_list) return tf.train.SequenceExample(feature_lists=feature_lists)
转载请注明原文地址: https://www.6miu.com/read-2250232.html

最新回复(0)