我正在 google colab(python) 中将 10.000 张图像的数据集分类为裸体/非裸体
我使用的是nudenet的NudeClassifier,它的主要工作原理是这样的。
from nudenet import NudeClassifier
# initialize classifier (downloads the checkpoint file automatically the first time)
classifier = NudeClassifier()
# A. Classify single image
print(classifier.classify('./image1.jpg'))
# This would print something like:
# {
# './image1.jpg': {
# 'safe': 0.00015856953,
# 'unsafe': 0.99984145
# }
# }
# B. Classify multiple images
# Returns {'path_to_image_1': {'safe': PROBABILITY, 'unsafe': PROBABILITY}}
# Classify multiple images (batch prediction)
# batch_size is optional; defaults to 4
print(
classifier.classify(
['./image1.jpg', './image2.jpg', './image3.jpg', './image4.jpg'],
batch_size=4
)
)
问题是,使用循环并对每个图像单独进行分类需要花费大量时间(图像大约 1 秒)
使用最后一个选项,更大的 bacth_size 会使分类问题运行得更快吗?
在这种情况下,这个问题的理想batch_size是多少?
非常感谢你
出于推理的目的,批量大小仅定义一次将有多少图像加载到内存中进行处理。因此批量大小越大,推理速度就越快。
当然限制是你的内存大小,所以你可以估计一下你能提高多少。
如果还是太慢,请考虑在 GPU 上进行推理。
增加批量大小肯定会缩短多个图像的推理时间。批量大小将根据可用的 GPU 内存而变化。尝试设置完全利用可用 GPU 的批量大小。
虽然晚了,但您可能想尝试一下 OpenNSFW 2,它对图像批次的推理有更好的支持。您甚至可以为每个图像生成Grad-CAM(请参阅用法),以便更好地了解模型决策。它的本质是 TensorFlow/Keras 模型。
您现在有这个数据集吗,请与我分享链接。