我为每个手势(5 个手势)训练数据 700 张图像, 验证测试数据 200 张图像
和测试数据 150 张图像。
我的模型是:
def get_model():
"""
Returns a compiled convolutional neural network model. Assume that the
`input_shape` of the first layer is `(IMG_WIDTH, IMG_HEIGHT, 3)`.
The output layer should have `NUM_CATEGORIES` units, one for each category.
"""
# Create a convolutional neural network
model = tf.keras.models.Sequential(
[
# Convolutional layer. Learn 32 filters using a 3x3 kernel
tf.keras.layers.Conv2D(
32, (5, 5), activation='relu', input_shape=(IMG_WIDTH, IMG_HEIGHT, 3)
),
# Max-pooling layer, using 2x2 pool size
tf.keras.layers.MaxPool2D(pool_size=(2, 2)),
tf.keras.layers.Conv2D(
64, (3, 3), activation='relu', input_shape=(IMG_WIDTH, IMG_HEIGHT, 3)
),
# Max-pooling layer, using 2x2 pool size
tf.keras.layers.MaxPool2D(pool_size=(2, 2)),
tf.keras.layers.Conv2D(
128, (3, 3), activation='relu', input_shape=((IMG_WIDTH), (IMG_HEIGHT), 3)
),
tf.keras.layers.MaxPool2D(pool_size=(2, 2)),
tf.keras.layers.Conv2D(
256, (3, 3), activation='relu', input_shape=((IMG_WIDTH), (IMG_HEIGHT), 3)
),
tf.keras.layers.MaxPool2D(pool_size=(2, 2)),
tf.keras.layers.Flatten(),
# Add a hidden layer with dropout
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dropout(0.3),
# Add an output layer with output units for all 6 gestures
tf.keras.layers.Dense(NUM_CATEGORIES, activation='softmax')
])
optimizer = tf.keras.optimizers.Adam(learning_rate=0.001)
model.compile(
optimizer=optimizer ,
loss="categorical_crossentropy",
metrics=["accuracy"]
)
return model
模型拟合部分:
y_test = tf.keras.utils.to_categorical(labels_test)
y_train = tf.keras.utils.to_categorical(labels_train)
y_valid = tf.keras.utils.to_categorical(valid_label)
x_train = np.array(images_train)/255
x_test = np.array(images_test)/255
x_valid = np.array(valid_image)/255
# Get a compiled neural network
fitting_time = datetime.now()
model = get_model()
# Fit model on training data
model.fit(x_train, y_train, epochs=EPOCHS, validation_data = (x_valid, y_valid))
# Evaluate neural network performance
model.evaluate(x_test, y_test, verbose=2)
如何避免过拟合以及为什么测试准确率如此低?
答案 0 :(得分:-1)
这种情况可能会发生,因为您在所有卷积层中都设置了 input_shape 参数。 您应该只在第一个中定义它。
您的代码应如下所示:
def get_model():
"""
Returns a compiled convolutional neural network model. Assume that the
`input_shape` of the first layer is `(IMG_WIDTH, IMG_HEIGHT, 3)`.
The output layer should have `NUM_CATEGORIES` units, one for each category.
"""
# Create a convolutional neural network
model = tf.keras.models.Sequential(
[
# Convolutional layer. Learn 32 filters using a 3x3 kernel
tf.keras.layers.Conv2D(
32, (5, 5), activation='relu', input_shape=(IMG_WIDTH, IMG_HEIGHT, 3)
),
# Max-pooling layer, using 2x2 pool size
tf.keras.layers.MaxPool2D(pool_size=(2, 2)),
tf.keras.layers.Conv2D(
64, (3, 3), activation='relu'
),
# Max-pooling layer, using 2x2 pool size
tf.keras.layers.MaxPool2D(pool_size=(2, 2)),
tf.keras.layers.Conv2D(
128, (3, 3), activation='relu'
),
tf.keras.layers.MaxPool2D(pool_size=(2, 2)),
tf.keras.layers.Conv2D(
256, (3, 3), activation='relu'
),
tf.keras.layers.MaxPool2D(pool_size=(2, 2)),
tf.keras.layers.Flatten(),
# Add a hidden layer with dropout
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dropout(0.3),
# Add an output layer with output units for all 6 gestures
tf.keras.layers.Dense(NUM_CATEGORIES, activation='softmax')
])
optimizer = tf.keras.optimizers.Adam(learning_rate=0.001)
model.compile(
optimizer=optimizer ,
loss="categorical_crossentropy",
metrics=["accuracy"]
)
return model
此外,您还应该检查训练和验证集中的分布是否相同。