我正在使用 CNN 解决图像分类问题。我有一个包含重复图像的图像数据集。当我用这些数据训练 CNN 时,它出现了过拟合。因此,我需要删除那些重复项。
我们粗略地称为重复的东西对于算法来说可能很难辨别。
您的重复项可以是:
- 精确重复
- 近乎精确的重复。 (对图像等进行少量编辑)
- 感知重复(相同的内容,但不同的视图、相机等)
No1和2更容易解决。第三,非常主观,仍然是一个研究课题。
我可以为No1和2提供解决方案。
两种解决方案都使用了优秀的图像哈希库:https://github.com/JohannesBuchner/imagehash 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) )
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)