【问题标题】:How to preprocess videos for a Conv3D Model如何为 Conv3D 模型预处理视频
【发布时间】:2018-10-02 05:58:54
【问题描述】:

我在 Keras 中有这个 Conv3D 模型:

model = Sequential(

Conv3D(32, (3,3,3), activation='relu', input_shape=self.input_shape),
MaxPooling3D(pool_size=(1, 2, 2), strides=(1, 2, 2)),
Conv3D(64, (3,3,3), activation='relu'),
MaxPooling3D(pool_size=(1, 2, 2), strides=(1, 2, 2)),
Conv3D(128, (3,3,3), activation='relu'),
Conv3D(128, (3,3,3), activation='relu'),
MaxPooling3D(pool_size=(1, 2, 2), strides=(1, 2, 2)),
Conv3D(256, (2,2,2), activation='relu'),
Conv3D(256, (2,2,2), activation='relu'),
MaxPooling3D(pool_size=(1, 2, 2), strides=(1, 2, 2)),

Flatten(),
Dense(1024)),
Dropout(0.5),
Dense(1024),
Dropout(0.5)),
Dense(self.nb_classes, activation='softmax')
)

此模型基于本文https://arxiv.org/pdf/1412.0767.pdf

使用此 Conv3D 预处理要预测的视频数据的最佳方法是什么?

我编写了这个函数来从 UCF-101 的每个视频中提取帧:

def frame_writer(pathIn, pathOut, class_name):
"""
This function will read videos and write frames in a new dataset
args:
    pathIn -> base dataset of videos
    pathOut -> destination folder for the frames ('data/path')
"""
#creating output path if it not exists
try:
  if not os.path.exists(pathOut + '/' + class_name):
    os.makedirs(pathOut + '/' + class_name)

  else:
    pass
except:
  print('Invalid path!')

#getting the list containing all files from the directory
pathIn_files = glob.glob(pathIn + '\\' + class_name + '\\' + '*.avi')
video_limit = len(pathIn_files)

#iterating over all files
for i, j in zip(pathIn_files, range(len(pathIn_files))):
  #getting the names from file paths
    base_name = os.path.basename(pathIn_files[j])
    file_name = base_name[0:-4] #taking only the file name (without extension)

    #getting the frames
    vidcap = cv2.VideoCapture(i)
    success,image = vidcap.read()
    count = 0
    success = True
    while success:
      success,image = vidcap.read()
      print ('Read a new frame: ', success)
      cv2.imwrite(pathOut + '\\' + class_name + "\\%s_frame%d.jpg" % (file_name, count), image)
      count += 1
print('Done!')

现在我有这样的帧数据集:

文件夹:数据

-子文件夹:火车

--子文件夹:class1

---frame1_video1_class1.jpg

---frame2_video1_class1.jpg

---frame3_video1_class1.jpg

...

---frameN_videoN_class1.jpg

--子文件夹:class2

---frame1_video1_class2.jpg

---frame2_video1_class2.jpg

---frame3_video1_class2.jpg

...

---frameN_videoN_class2.jpg

-子文件夹:测试

--子文件夹:class1

---frame1_video1_class1.jpg

---frame2_video1_class1.jpg

---frame3_video1_class1.jpg

...

---frameN_videoN_class1.jpg

--子文件夹:class2

---frame1_video1_class2.jpg

---frame2_video1_class2.jpg

---frame3_video1_class2.jpg

...

---frameN_videoN_class2.jpg

所以我将所有视频的所有帧都放在与其类对应的文件夹中。

我必须使用 keras 函数中的 ImageDataGenerator 将它传递给我的 Conv3D 模型吗?

那么,在这种情况下,每次传递每个类的每个视频的每一帧?

或者我必须以其他方式这样做?

我只需要使用这个模型来预测视频!

感谢支持!

【问题讨论】:

    标签: python video machine-learning keras artificial-intelligence


    【解决方案1】:

    一种方法是将所有帧放入一个大张量中,相应地标记它们,并将其用作 Keras 模型的输入。张量中的帧数将是您的批量大小。

    【讨论】:

    • 好!我会试试这个!你有这个教程链接吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-31
    • 1970-01-01
    • 2018-12-07
    • 2020-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多