我正在使用 CNN 解决图像分类问题。我有一个包含重复图像的图像数据集。当我用这些数据训练 CNN 时,它出现了过拟合。因此,我需要删除这些重复项。
最佳答案
我们粗略地称为重复的内容可能很难让算法识别。 您的重复项可以是:
- 完全重复
- 几乎完全重复。 (对图像等进行少量编辑)
- 感知重复(内容相同,但 View 、相机等不同)
No1 和 2 更容易解决。第三,非常主观,仍然是一个研究课题。 我可以为No1和2提供解决方案。 两种解决方案都使用优秀的图像哈希散列库:https://github.com/JohannesBuchner/imagehash
- 完全重复 可以使用感知散列测量来找到精确的重复项。 phash 库在这方面非常擅长。我经常用它来清洁 训练数据。 使用(来自 github 站点)非常简单:
from PIL import Image
import imagehash
# image_fns : List of training image files
img_hashes = {}
for img_fn in sorted(image_fns):
hash = imagehash.average_hash(Image.open(image_fn))
if hash in img_hashes:
print( '{} duplicate of {}'.format(image_fn, img_hashes[hash]) )
else:
img_hashes[hash] = image_fn
- 几乎完全重复 在这种情况下,您必须设置一个阈值并比较哈希值与每个哈希值的距离。 其他。这必须通过对图像内容进行反复试验来完成。
from PIL import Image
import imagehash
# image_fns : List of training image files
img_hashes = {}
epsilon = 50
for img_fn1, img_fn2 in zip(image_fns, image_fns[::-1]):
if image_fn1 == image_fn2:
continue
hash1 = imagehash.average_hash(Image.open(image_fn1))
hash2 = imagehash.average_hash(Image.open(image_fn2))
if hash1 - hash2 < epsilon:
print( '{} is near duplicate of {}'.format(image_fn1, image_fn2) )
关于python - 训练CNN时如何去除重复项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56564128/