如何仅选择Tensorflow数据集的一部分并更改尺寸

DSP魔术师

我希望在UCF101的10个框架段上训练我的模型,而没有任何标签。目前我有这个:

import tensorflow as tf
import tensorflow_datasets as tfds
x_train = tfds.load('ucf101', split='train', shuffle_files=True, batch_size = 64)
>>> print(x_train)
<_OptionsDataset shapes: {label: (None,), video: (None, None, 256, 256, 3)}, types: {label: tf.int64, video: tf.uint8}>

我希望数据集的尺寸为(无,10、256、256、3),并且不包括标签。

编辑:我尝试在中使用lambda表达式.map(),但这产生了一个错误。

new_x_train = x_train.map(lambda x: tf.py_function(func=lambda y: tf.convert_to_tensor(sample(y.numpy().tolist(), 10), dtype=uint8), inp=[x['video']], Tout=tf.uint8))
NameError: name 'sample' is not defined
DSP魔术师

解决方案就是将数据集文件下载到其他位置,因此我的目录中有一个.avi文件列表,然后在tensorflow之外对这些文件进行预处理。我使用了cv2库和以下代码,从其他地方借来了两个函数:

# Utilities to open video files using CV2
def crop_center_square(frame):
  y, x = frame.shape[0:2]
  min_dim = min(y, x)
  start_x = (x // 2) - (min_dim // 2)
  start_y = (y // 2) - (min_dim // 2)
  return frame[start_y:start_y+min_dim,start_x:start_x+min_dim]

def load_video(path, max_frames=0, resize=(256, 256)):
  cap = cv2.VideoCapture(path)
  frames = []
  try:
    while True:
      ret, frame = cap.read()
      if not ret:
        break
      frame = crop_center_square(frame)
      frame = cv2.resize(frame, resize)
      frame = frame[:, :, [2, 1, 0]]
      frames.append(frame)

      if len(frames) == max_frames:
        break
  finally:
    cap.release()
  return np.array(frames) / 255.0


files = [f for f in glob.glob("**/*.avi", recursive=True)]

for video_path in files:
  video = load_video(video_path)
  video_name = video_path[video_path.find('/')+1:]
  num_frames = video.shape[0]
  print("Video in " + video_path + " has " + str(num_frames) + " frames.")
  for seg_num in range(math.floor(num_frames/10)):
    result = video[seg_num*10:(seg_num+1)*10, ...]
    new_filepath = video_name[:-4] + "_" + str(seg_num).zfill(2) + ".avi"
    print(new_filepath)
    out = cv2.VideoWriter(new_filepath,0, 25.0, (256,256))
    for frame_n in range(0,10):
      out.write(np.uint8(255*result[frame_n, ...]))
    out.release()
    del result
  del video

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章