我正在使用Java
进行手印多位数识别,使用OpenCV
库进行预处理和分段,并使用在MNIST上训练的Keras
模型(精度为0.98)进行识别。
除了一件事情外,这种认可似乎还算不错。网络经常无法识别那些(数字“一”)。我不知道是由于分割的预处理/错误实现而发生的,还是在标准MNIST上训练的网络只是没有看到看起来像我的测试用例的第一名而已。
这是经过预处理和分割后出现问题的数字的样子:
变为
,并分类为
4
。
变为
,并分类为
7
。
变为
,并分类为
4
。
等等...
通过改进分割过程,可以解决此问题吗?还是通过增强培训设置?
编辑:增强训练集(数据扩充)肯定会有所帮助,这已经在我测试中,正确预处理的问题仍然存在。
我的预处理包括调整大小,转换为灰度,二值化,反转和膨胀。这是代码:
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没看到过看起来像测试用例的数字”,而是让图像看起来像不同的受过训练的数字(被识别出的数字)
如果您维护了图像的正确长宽比(源图像和后处理图像),则可以看到您不仅调整了图像的大小,而且使它“失真”了。可能是非均匀膨胀或尺寸调整不正确的结果
关于java - 如何提高在MNIST上训练的模型的数字识别能力?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58398983/