我正在使用Java
进行手印多位数识别,使用OpenCV
库进行预处理和分割,并使用在MNIST上训练的Keras
模型(精度为0.98)进行识别。
除了一件事之外,这种认识似乎还不错。网络经常无法识别那些(数字“一”)。我无法弄清楚是否是由于分割的预处理/不正确的实现而发生的,或者是在标准MNIST上训练的网络只是没有看到看起来像我的测试用例的第一名。
这是经过预处理和分割后有问题的数字的样子:
可以通过改进细分过程来解决此问题吗?还是通过增强培训设置?
编辑:增强训练集(数据扩充)肯定会有所帮助,我已经在测试它,正确的预处理问题仍然存在。
我的预处理包括调整大小,转换为灰度,二值化,反演和放大。这是代码:
Mat resized = new Mat();
Imgproc.resize(image, resized, new Size(), 8, 8, Imgproc.INTER_CUBIC);
Mat grayscale = new Mat();
Imgproc.cvtColor(resized, grayscale, Imgproc.COLOR_BGR2GRAY);
Mat binImg = new Mat(grayscale.size(), CvType.CV_8U);
Imgproc.threshold(grayscale, binImg, 0, 255, Imgproc.THRESH_OTSU);
Mat inverted = new Mat();
Core.bitwise_not(binImg, inverted);
Mat dilated = new Mat(inverted.size(), CvType.CV_8U);
int dilation_size = 5;
Mat kernel = Imgproc.getStructuringElement(Imgproc.CV_SHAPE_CROSS, new Size(dilation_size, dilation_size));
Imgproc.dilate(inverted, dilated, kernel, new Point(-1,-1), 1);
然后将经过预处理的图像分割成各个数字,如下所示:
List<Mat> digits = new ArrayList<>();
List<MatOfPoint> contours = new ArrayList<>();
Imgproc.findContours(preprocessed.clone(), contours, new Mat(), Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
// code to sort contours
// code to check that contour is a valid char
List rects = new ArrayList<>();
for (MatOfPoint contour : contours) {
Rect boundingBox = Imgproc.boundingRect(contour);
Rect rectCrop = new Rect(boundingBox.x, boundingBox.y, boundingBox.width, boundingBox.height);
rects.add(rectCrop);
}
for (int i = 0; i < rects.size(); i++) {
Rect x = (Rect) rects.get(i);
Mat digit = new Mat(preprocessed, x);
int border = 50;
Mat result = digit.clone();
Core.copyMakeBorder(result, result, border, border, border, border, Core.BORDER_CONSTANT, new Scalar(0, 0, 0));
Imgproc.resize(result, result, new Size(28, 28));
digits.add(result);
}
我相信您的问题是扩张过程。我了解您希望规范化图像尺寸,但是您不应该破坏比例,您应该将一个轴调整为最大尺寸(该尺寸允许最大的缩放比例,而另一轴尺寸不超过最大尺寸)并填充具有背景色的图像的其余部分。并不是说“标准MNIST没看到过看起来像测试用例的数字”,而是让图像看起来像不同的受过训练的数字(可以识别的数字)]