gpt4 book ai didi

performance - Tensorflow - 添加 Dropout 层显着增加推理时间

转载 作者:行者123 更新时间:2023-12-01 15:02:14 31 4
gpt4 key购买 nike

我的 CNN 相对较小

model = tf.keras.models.Sequential([
tf.keras.layers.Conv2D(input_shape=(400,400,3), filters=6, kernel_size=5, padding='same', activation='relu'),
tf.keras.layers.Conv2D(filters=12, kernel_size=3, padding='same', activation='relu'),
tf.keras.layers.Conv2D(filters=24, kernel_size=3, strides=2, padding='valid', activation='relu'),
tf.keras.layers.Conv2D(filters=32, kernel_size=3, strides=2, padding='valid', activation='relu'),
tf.keras.layers.Conv2D(filters=48, kernel_size=3, strides=2, padding='valid', activation='relu'),
tf.keras.layers.Conv2D(filters=64, kernel_size=3, strides=2, padding='valid', activation='relu'),
tf.keras.layers.Conv2D(filters=96, kernel_size=3, strides=2, padding='valid', activation='relu'),
tf.keras.layers.Conv2D(filters=128, kernel_size=3, strides=2, padding='valid', activation='relu'),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(256, activation='relu'),
tf.keras.layers.Dense(256, activation='relu'),
tf.keras.layers.Dense(256, activation='relu'),
tf.keras.layers.Dense(240, activation='softmax')
])
model.compile(optimizer='adam', loss='categorical_crossentropy')

我使用以下代码来衡量模型性能:

for img_per_batch in [1, 5, 10, 50]:
# warm up the model
image = np.random.random(size=(img_per_batch, 400, 400, 3)).astype('float32')
model(image, training=False)

n_iter = 100
start_time = time.time()
for _ in range(n_iter):
image = np.random.random(size=(img_per_batch, 400, 400, 3)).astype('float32')
model(image, training=False)
dt = (time.time() - start_time) * 1000
print(f'img_per_batch = {img_per_batch}, {dt/n_iter:.2f} ms per iteration, {dt/n_iter/img_per_batch:.2f} ms per image')

我的输出(Nvidia Jetson Xavier,tensorflow==2.0.0):

img_per_batch = 1, 21.74 ms per iteration, 21.74 ms per image
img_per_batch = 5, 42.35 ms per iteration, 8.47 ms per image
img_per_batch = 10, 68.37 ms per iteration, 6.84 ms per image
img_per_batch = 50, 312.83 ms per iteration, 6.26 ms per image

然后我在每个完全连接的层之后添加 dropout 层:

model = tf.keras.models.Sequential([
# ... convolution layers are same
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(256, activation='relu'),
tf.keras.layers.Dropout(.3),
tf.keras.layers.Dense(256, activation='relu'),
tf.keras.layers.Dropout(.3),
tf.keras.layers.Dense(256, activation='relu'),
tf.keras.layers.Dropout(.3),
tf.keras.layers.Dense(240, activation='softmax')
])

添加层后,输出如下:

img_per_batch = 1, 31.18 ms per iteration, 31.18 ms per image
img_per_batch = 5, 76.15 ms per iteration, 15.23 ms per image
img_per_batch = 10, 127.91 ms per iteration, 12.79 ms per image
img_per_batch = 50, 513.85 ms per iteration, 10.28 ms per image

理论上,dropout 层不应该影响推理性能。但在上面的代码中,添加 dropout 层会使单图像预测时间增加 1.5 倍,而 10 图像批量预测几乎比没有 dropout 慢两倍。我做错了什么吗?

最佳答案

显然这是 TensorFlow 2.0.0 中的一个已知问题:see this GitHub comment .

尝试使用model.predict(x)而不是model(x)

也可以通过更新到 TensorFlow 的最新版本(例如 2.1.0)来修复此问题。

希望这有帮助

关于performance - Tensorflow - 添加 Dropout 层显着增加推理时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60799113/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com