我有一个用于单词分类的视频数据集,该数据集包含137,638个培训视频,42000个验证视频和18000个测试视频。每个视频的帧数不同,而所有帧的大小均相同(40x62x1)。类的数目为51。下面的代码使用CNN进行特征提取,使用LSTM进行分类。但是,当我尝试训练模型时,尽管 input_shape =(n_frames,frame_width,frame_height,n_channels),但出现了以下错误ValueError: input tensor must have rank 4
如代码中所示。
有人可以帮助我吗?
我的代码:
import tensorflow as tf
import numpy as np
import time
import os
import matplotlib.pyplot as plt
from keras.models import Sequential
from keras.layers import TimeDistributed
from keras.layers import Conv2D
from keras.layers import MaxPool2D
from keras.layers import Flatten
from keras.layers import Dense,Bidirectional
from keras.layers.recurrent import LSTM
start = time.time()
nb_classes = 51
# loading data
TRAIN_PATH = "E:/PhD/Video/Train"
VAL_PATH = "E:/PhD/Video/Validation"
TEST_PATH = "E:/PhD/Video/Test"
X_train = np.load(os.path.join(TRAIN_PATH,'Train.npy'))
X_val = np.load(os.path.join(VAL_PATH,'Validation.npy'))
X_test = np.load(os.path.join(TEST_PATH,'Test.npy'))
# loading labels
Y_train = np.load(os.path.join(TRAIN_PATH,'Train_labels_binary.npy'))
Y_val = np.load(os.path.join(VAL_PATH,'Validation_labels_binary.npy'))
Y_test = np.load(os.path.join(TEST_PATH,'Test_labels_binary.npy'))
print(len(X_train), 'train sequences')
print(len(X_val), 'validation sequences')
print(len(X_test), 'test sequences')
print('X_train shape:', X_train.shape)
print('X_val shape:', X_val.shape)
print('X_test shape:', X_test.shape)
print('y_train shape:', Y_train.shape)
print('y_val shape:', Y_val.shape)
print('y_test shape:', Y_test.shape)
print('Build model...')
#Model Design
model = tf.keras.models.Sequential([
tf.keras.layers.TimeDistributed(tf.keras.layers.Conv2D(32, (3,3), strides=(1,1), activation='relu',
kernel_initializer='uniform',
bias_initializer='one',
input_shape = (X_train.shape[0], 40, 62, 1),
data_format='channels_last')),
tf.keras.layers.TimeDistributed(tf.keras.layers.MaxPool2D(pool_size=(2, 2))),
tf.keras.layers.TimeDistributed(tf.keras.layers.Conv2D(32,(3,3), activation = 'relu')),
tf.keras.layers.TimeDistributed(tf.keras.layers.MaxPool2D(pool_size=(2, 2))),
tf.keras.layers.TimeDistributed(tf.keras.layers.Flatten()),
tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(2000, kernel_initializer ='uniform',
recurrent_initializer='uniform',
bias_initializer='one', activation='tanh',
recurrent_activation='sigmoid', return_sequences=True)),
tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(2000, kernel_initializer ='uniform',
recurrent_initializer='uniform',bias_initializer='one',
activation='tanh', recurrent_activation='sigmoid')),
tf.keras.layers.Dense(nb_classes, activation = 'softmax')
])
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
hist = model.fit(X_train, Y_train, validation_data=(X_val, Y_val), epochs=50,
batch_size=64, verbose=1)
# Final evaluation of the model
scores = model.evaluate(X_test, Y_test, verbose=1)
Y_pred = model.predict(X_test)
错误:
Traceback (most recent call last):
File "C:\Users\hms08\AppData\Local\Continuum\anaconda3\envs\keras-gpu\lib\site-packages\tensorflow_core\python\framework\tensor_shape.py", line 928, in merge_with
self.assert_same_rank(other)
File "C:\Users\hms08\AppData\Local\Continuum\anaconda3\envs\keras-gpu\lib\site-packages\tensorflow_core\python\framework\tensor_shape.py", line 983, in assert_same_rank
(self, other))
ValueError: Shapes (None, 62) and (None, None, None, None) must have the same rank
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\hms08\AppData\Local\Continuum\anaconda3\envs\keras-gpu\lib\site-packages\tensorflow_core\python\framework\tensor_shape.py", line 1013, in with_rank
return self.merge_with(unknown_shape(rank=rank))
File "C:\Users\hms08\AppData\Local\Continuum\anaconda3\envs\keras-gpu\lib\site-packages\tensorflow_core\python\framework\tensor_shape.py", line 934, in merge_with
raise ValueError("Shapes %s and %s are not compatible" % (self, other))
ValueError: Shapes (None, 62) and (None, None, None, None) are not compatible
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\hms08\AppData\Local\Continuum\anaconda3\envs\keras-gpu\lib\site-packages\tensorflow_core\python\ops\nn_ops.py", line 1064, in __init__
input_shape.with_rank(num_spatial_dims + 2)
File "C:\Users\hms08\AppData\Local\Continuum\anaconda3\envs\keras-gpu\lib\site-packages\tensorflow_core\python\framework\tensor_shape.py", line 1015, in with_rank
raise ValueError("Shape %s must have rank %d" % (self, rank))
ValueError: Shape (None, 62) must have rank 4
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<ipython-input-4-2ac41977fbc6>", line 1, in <module>
runfile('E:/PhD/Video/CNN+LSTM/CNN+LSTM.py', wdir='E:/PhD/Video/CNN+LSTM')
File "C:\Users\hms08\AppData\Local\Continuum\anaconda3\envs\keras-gpu\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 827, in runfile
execfile(filename, namespace)
File "C:\Users\hms08\AppData\Local\Continuum\anaconda3\envs\keras-gpu\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "E:/PhD/Video/CNN+LSTM/CNN+LSTM.py", line 77, in <module>
batch_size=64, verbose=1)
File "C:\Users\hms08\AppData\Local\Continuum\anaconda3\envs\keras-gpu\lib\site-packages\tensorflow_core\python\keras\engine\training.py", line 728, in fit
use_multiprocessing=use_multiprocessing)
File "C:\Users\hms08\AppData\Local\Continuum\anaconda3\envs\keras-gpu\lib\site-packages\tensorflow_core\python\keras\engine\training_v2.py", line 224, in fit
distribution_strategy=strategy)
File "C:\Users\hms08\AppData\Local\Continuum\anaconda3\envs\keras-gpu\lib\site-packages\tensorflow_core\python\keras\engine\training_v2.py", line 547, in _process_training_inputs
use_multiprocessing=use_multiprocessing)
File "C:\Users\hms08\AppData\Local\Continuum\anaconda3\envs\keras-gpu\lib\site-packages\tensorflow_core\python\keras\engine\training_v2.py", line 594, in _process_inputs
steps=steps)
File "C:\Users\hms08\AppData\Local\Continuum\anaconda3\envs\keras-gpu\lib\site-packages\tensorflow_core\python\keras\engine\training.py", line 2419, in _standardize_user_data
all_inputs, y_input, dict_inputs = self._build_model_with_inputs(x, y)
File "C:\Users\hms08\AppData\Local\Continuum\anaconda3\envs\keras-gpu\lib\site-packages\tensorflow_core\python\keras\engine\training.py", line 2622, in _build_model_with_inputs
self._set_inputs(cast_inputs)
File "C:\Users\hms08\AppData\Local\Continuum\anaconda3\envs\keras-gpu\lib\site-packages\tensorflow_core\python\keras\engine\training.py", line 2709, in _set_inputs
outputs = self(inputs, **kwargs)
File "C:\Users\hms08\AppData\Local\Continuum\anaconda3\envs\keras-gpu\lib\site-packages\tensorflow_core\python\keras\engine\base_layer.py", line 842, in __call__
outputs = call_fn(cast_inputs, *args, **kwargs)
File "C:\Users\hms08\AppData\Local\Continuum\anaconda3\envs\keras-gpu\lib\site-packages\tensorflow_core\python\keras\engine\sequential.py", line 270, in call
outputs = layer(inputs, **kwargs)
File "C:\Users\hms08\AppData\Local\Continuum\anaconda3\envs\keras-gpu\lib\site-packages\tensorflow_core\python\keras\engine\base_layer.py", line 817, in __call__
self._maybe_build(inputs)
File "C:\Users\hms08\AppData\Local\Continuum\anaconda3\envs\keras-gpu\lib\site-packages\tensorflow_core\python\keras\engine\base_layer.py", line 2141, in _maybe_build
self.build(input_shapes)
File "C:\Users\hms08\AppData\Local\Continuum\anaconda3\envs\keras-gpu\lib\site-packages\tensorflow_core\python\keras\layers\wrappers.py", line 203, in build
super(TimeDistributed, self).build(tuple(child_input_shape))
File "C:\Users\hms08\AppData\Local\Continuum\anaconda3\envs\keras-gpu\lib\site-packages\tensorflow_core\python\keras\layers\wrappers.py", line 61, in build
self.layer.build(input_shape)
File "C:\Users\hms08\AppData\Local\Continuum\anaconda3\envs\keras-gpu\lib\site-packages\tensorflow_core\python\keras\layers\convolutional.py", line 193, in build
self.rank + 2))
File "C:\Users\hms08\AppData\Local\Continuum\anaconda3\envs\keras-gpu\lib\site-packages\tensorflow_core\python\ops\nn_ops.py", line 1067, in __init__
"input tensor must have rank %d" % (num_spatial_dims + 2))
ValueError: input tensor must have rank 4