如何预处理Conv3D模型的视频

时间:2018-04-21 20:21:33

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

我在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!')

现在我的框架数据集如下:

文件夹:数据

-SUBFOLDER:火车

- SUBFOLDER:class1

--- frame1_video1_class1.jpg

--- frame2_video1_class1.jpg

--- frame3_video1_class1.jpg

...

--- frameN_videoN_class1.jpg

- SUBFOLDER:class2

--- frame1_video1_class2.jpg

--- frame2_vide1_class2.jpg

--- frame3_video1_class2.jpg

...

--- frameN_videoN_class2.jpg

-SUBFOLDER:测试

- SUBFOLDER: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模型吗?

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

或者我必须以另一种方式做到这一点?

我只需要使用此模型预测视频!

感谢您的支持!

1 个答案:

答案 0 :(得分:1)

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