无法解决keras顺序模型中的错误
Cannot resolve error in keras sequential model
我正在处理手势识别问题。为此,我有一套火车。训练集由多个文件夹组成,每个文件夹由一系列 30 张图像组成。从这些图像中训练模型。我还有一个 csv 文件,其中包含每个文件夹的 class 标签。 class 标签是:“向左滑动”、“向右滑动”、“停止”、“不赞成”和“赞成”。这些标签存在于一个 np.array 变量 train_class 中。现在,我创建了一个 CNN 模型,然后将其输入到一个顺序模型中。
代码可在以下 GIT 位置找到
https://github.com/subhrajyoti-ghosh/ML-and-Deep-Learning/blob/main/Gesture_Recognition.ipynb
但是当我尝试拟合模型时,我收到错误消息。你能帮我理解错误以及如何解决吗?
您正在尝试在 2D 输入 (batch_size, 256)
上使用 TimeDistributed
层,这将不起作用,因为该层至少需要一个 3D 张量。您应该尝试使用 tf.keras.layers.RepeatVector
:
import tensorflow as tf
resnet = tf.keras.applications.ResNet50(include_top=False,weights='imagenet',input_shape=(224,224,3))
cnn = tf.keras.Sequential([resnet])
cnn.add(tf.keras.layers.Conv2D(64,(2,2),strides=(1,1)))
cnn.add(tf.keras.layers.Conv2D(16,(3,3),strides=(1,1)))
cnn.add(tf.keras.layers.Flatten())
inputs = tf.keras.layers.Input(shape=(224,224,3))
x = cnn(inputs)
x = tf.keras.layers.RepeatVector(n=30)(x)
x = tf.keras.layers.GRU(16,return_sequences=True)(x)
x = tf.keras.layers.GRU(8)(x)
outputs = tf.keras.layers.Dense(5,activation='softmax')(x)
model = tf.keras.Model(inputs, outputs)
dummy_x = tf.random.normal((1, 224,224,3))
print(model.summary())
print(model(dummy_x))
Model: "model_2"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_14 (InputLayer) [(None, 224, 224, 3)] 0
sequential_6 (Sequential) (None, 256) 24121296
repeat_vector_2 (RepeatVect (None, 30, 256) 0
or)
gru_5 (GRU) (None, 30, 16) 13152
gru_6 (GRU) (None, 8) 624
dense_7 (Dense) (None, 5) 45
=================================================================
Total params: 24,135,117
Trainable params: 24,081,997
Non-trainable params: 53,120
_________________________________________________________________
None
我正在处理手势识别问题。为此,我有一套火车。训练集由多个文件夹组成,每个文件夹由一系列 30 张图像组成。从这些图像中训练模型。我还有一个 csv 文件,其中包含每个文件夹的 class 标签。 class 标签是:“向左滑动”、“向右滑动”、“停止”、“不赞成”和“赞成”。这些标签存在于一个 np.array 变量 train_class 中。现在,我创建了一个 CNN 模型,然后将其输入到一个顺序模型中。
代码可在以下 GIT 位置找到 https://github.com/subhrajyoti-ghosh/ML-and-Deep-Learning/blob/main/Gesture_Recognition.ipynb
但是当我尝试拟合模型时,我收到错误消息。你能帮我理解错误以及如何解决吗?
您正在尝试在 2D 输入 (batch_size, 256)
上使用 TimeDistributed
层,这将不起作用,因为该层至少需要一个 3D 张量。您应该尝试使用 tf.keras.layers.RepeatVector
:
import tensorflow as tf
resnet = tf.keras.applications.ResNet50(include_top=False,weights='imagenet',input_shape=(224,224,3))
cnn = tf.keras.Sequential([resnet])
cnn.add(tf.keras.layers.Conv2D(64,(2,2),strides=(1,1)))
cnn.add(tf.keras.layers.Conv2D(16,(3,3),strides=(1,1)))
cnn.add(tf.keras.layers.Flatten())
inputs = tf.keras.layers.Input(shape=(224,224,3))
x = cnn(inputs)
x = tf.keras.layers.RepeatVector(n=30)(x)
x = tf.keras.layers.GRU(16,return_sequences=True)(x)
x = tf.keras.layers.GRU(8)(x)
outputs = tf.keras.layers.Dense(5,activation='softmax')(x)
model = tf.keras.Model(inputs, outputs)
dummy_x = tf.random.normal((1, 224,224,3))
print(model.summary())
print(model(dummy_x))
Model: "model_2"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_14 (InputLayer) [(None, 224, 224, 3)] 0
sequential_6 (Sequential) (None, 256) 24121296
repeat_vector_2 (RepeatVect (None, 30, 256) 0
or)
gru_5 (GRU) (None, 30, 16) 13152
gru_6 (GRU) (None, 8) 624
dense_7 (Dense) (None, 5) 45
=================================================================
Total params: 24,135,117
Trainable params: 24,081,997
Non-trainable params: 53,120
_________________________________________________________________
None