将每个帧编码为单独的功能会导致难以动态选择帧,因为
tf.parse_example()(和
tf.parse_single_example())的签名要求在图构造时固定一组已解析的功能名称。但是,您可以尝试将帧编码为
单个 功能,其中包含JPEG编码的字符串列表:
def _bytes_list_feature(values): """Wrapper for inserting bytes features into Example proto.""" return tf.train.Feature(bytes_list=tf.train.BytesList(value=values))with tf.python_io.TFRecordWriter(output_file) as writer: # Read and resize all video frames, np.uint8 of size [N,H,W,3] frames = ... features = {} features['num_frames'] = _int64_feature(frames.shape[0]) features['height'] = _int64_feature(frames.shape[1]) features['width'] = _int64_feature(frames.shape[2]) features['channels'] = _int64_feature(frames.shape[3]) features['class_label'] = _int64_feature(example['class_id']) features['class_text'] = _bytes_feature(tf.compat.as_bytes(example['class_label'])) features['filename'] = _bytes_feature(tf.compat.as_bytes(example['video_id'])) # Compress the frames using JPG and store in as a list of strings in 'frames' enpred_frames = [tf.compat.as_bytes(cv2.imenpre(".jpg", frame)[1].tobytes()) for frame in frames] features['frames'] = _bytes_list_feature(enpred_frames) tfrecord_example = tf.train.Example(features=tf.train.Features(feature=features)) writer.write(tfrecord_example.SerializeToString())完成此操作后,就可以
frames使用修改后的解析代码版本来动态切片功能:
def depre(serialized_example, sess): # Prepare feature list; read enpred JPG images as bytes features = dict() features["class_label"] = tf.FixedLenFeature((), tf.int64) features["frames"] = tf.VarLenFeature(tf.string) features["num_frames"] = tf.FixedLenFeature((), tf.int64) # Parse into tensors parsed_features = tf.parse_single_example(serialized_example, features) # Randomly sample offset from the valid range. random_offset = tf.random_uniform( shape=(), minval=0, maxval=parsed_features["num_frames"] - SEQ_NUM_frameS, dtype=tf.int64) offsets = tf.range(random_offset, random_offset + SEQ_NUM_frameS) # Depre the enpred JPG images images = tf.map_fn(lambda i: tf.image.depre_jpeg(parsed_features["frames"].values[i]), offsets) label = tf.cast(parsed_features["class_label"], tf.int64) return images, label
(请注意,我无法运行您的代码,因此可能会有一些小错误,但希望它足以使您入门。)



