CNN-> 视频的 LSTM 网络
CNN-> LSTM network for videos
我有 X 个视频,每个视频都有不同的帧数,假设为 Y(x)。所有视频 224X224X3 的帧大小都相同。我将每一帧传递给 CNN,它输出一个 1024 的特征向量。现在,我想将它传递给 LSTM。对于 LSTM batch_size, time_steps , number_of_feature 是必需的。我应该如何决定这些价值?我有两种配置,但不知道该如何进行。
我是否应该将 1024 分成 32 X 32 来定义 time_steps 和 number_of_features 而 batch_size 是帧数
应该time_step对应帧数,number_of_feature应该是1024,batch_size(?)
所以这取决于您要解决的问题。
Action classification using videos?
如果您尝试从视频中预测 action/event,则必须使用 num_of_frames
作为 time_steps
,而 batch_size
将是您想要预测的视频数量一起处理。
Per frame object classification ?
在这种情况下,您可以将特征拆分为 32x32
为 time_steps
、
考虑使用 Keras Layers 构建模型,您可以像这样堆叠所有层:
model = Sequential()
model.add(TimeDistributed(Conv2D...))
model.add(TimeDistributed(MaxPooling2D...))
model.add(TimeDistributed(Flatten()))
model.add(TimeDistributed(LSTM, return_sequences=False...)) #or True, in case of Stacked
model.add(TimeDistributed(Dense...))
并尝试直接使用 OpenCV 预处理视频,例如从每个视频中读取多个帧并将它们存储到一个大张量中,您可以使用 sklearn train_test_split 进行拆分,如下所示:
video_folder = '/path.../'
X_data = []
y_data = []
list_of_videos = os.listdir(vide_folder)
for i in list_of_videos:
#Video Path
vid = str(video_folder + i) #path to each video from list1 = os.listdir(path)
#Reading the Video
cap = cv2.VideoCapture(vid)
#Reading Frames
#fps = vcap.get(5)
#To Store Frames
frames = []
for j in range(40): #here we get 40 frames, for example
ret, frame = cap.read()
if ret == True:
print('Class 1 - Success!')
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) #converting to gray
frame = cv2.resize(frame,(30,30),interpolation=cv2.INTER_AREA)
frames.append(frame)
else:
print('Error!')
X_data.append(frames) #appending each tensor of 40 frames resized for 30x30
y_data.append(1) #appending a class label to the set of 40 frames
X_data = np.array(X_data)
y_data = np.array(y_data) #ready to split! :)
希望对您有所帮助! :)
我有 X 个视频,每个视频都有不同的帧数,假设为 Y(x)。所有视频 224X224X3 的帧大小都相同。我将每一帧传递给 CNN,它输出一个 1024 的特征向量。现在,我想将它传递给 LSTM。对于 LSTM batch_size, time_steps , number_of_feature 是必需的。我应该如何决定这些价值?我有两种配置,但不知道该如何进行。
我是否应该将 1024 分成 32 X 32 来定义 time_steps 和 number_of_features 而 batch_size 是帧数
应该time_step对应帧数,number_of_feature应该是1024,batch_size(?)
所以这取决于您要解决的问题。
Action classification using videos?
如果您尝试从视频中预测 action/event,则必须使用 num_of_frames
作为 time_steps
,而 batch_size
将是您想要预测的视频数量一起处理。
Per frame object classification ?
在这种情况下,您可以将特征拆分为 32x32
为 time_steps
、
考虑使用 Keras Layers 构建模型,您可以像这样堆叠所有层:
model = Sequential()
model.add(TimeDistributed(Conv2D...))
model.add(TimeDistributed(MaxPooling2D...))
model.add(TimeDistributed(Flatten()))
model.add(TimeDistributed(LSTM, return_sequences=False...)) #or True, in case of Stacked
model.add(TimeDistributed(Dense...))
并尝试直接使用 OpenCV 预处理视频,例如从每个视频中读取多个帧并将它们存储到一个大张量中,您可以使用 sklearn train_test_split 进行拆分,如下所示:
video_folder = '/path.../'
X_data = []
y_data = []
list_of_videos = os.listdir(vide_folder)
for i in list_of_videos:
#Video Path
vid = str(video_folder + i) #path to each video from list1 = os.listdir(path)
#Reading the Video
cap = cv2.VideoCapture(vid)
#Reading Frames
#fps = vcap.get(5)
#To Store Frames
frames = []
for j in range(40): #here we get 40 frames, for example
ret, frame = cap.read()
if ret == True:
print('Class 1 - Success!')
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) #converting to gray
frame = cv2.resize(frame,(30,30),interpolation=cv2.INTER_AREA)
frames.append(frame)
else:
print('Error!')
X_data.append(frames) #appending each tensor of 40 frames resized for 30x30
y_data.append(1) #appending a class label to the set of 40 frames
X_data = np.array(X_data)
y_data = np.array(y_data) #ready to split! :)
希望对您有所帮助! :)