【问题标题】:Python Machine Learning Digit RecognitionPython机器学习数字识别
【发布时间】:2018-04-10 06:05:49
【问题描述】:

我正在关注此站点上的代码:

https://blog.luisfred.com.br/reconhecimento-de-escrita-manual-com-redes-neurais-convolucionais/

以下是该网站的代码:

from keras. datasets import mnist
from keras. models import Sequential
from keras. layers import Dense
from keras. layers import Dropout
from keras. layers import Flatten
import numpy as np
from matplotlib import pyplot as plt
from keras. layers . convolutional import Conv2D
from keras. layers . convolutional import MaxPooling2D
from keras. utils import np_utils
from keras import backend as K
K . set_image_dim_ordering ( 'th' )
import cv2
import matplotlib. pyplot as plt
#% inline matplotlib # If you are using Jupyter, it will be useful for plotting graphics or figures inside cells

#Divided the data into subsets of training and testing.
( X_train , y_train ) , ( X_test , y_test ) = mnist. load_data ( )
# Since we are working in gray scale we can
# set the depth to the value 1.
X_train = X_train . reshape ( X_train . shape [ 0 ] , 1 , 28 , 28 ) . astype ( 'float32' )
X_test = X_test . reshape ( X_test . shape [ 0 ] , 1 , 28 , 28 ) . astype ( 'float32' )
# We normalize our data according to the
# gray scale. The floating point values ​​are in the range [0,1], instead of [.255]
X_train = X_train / 255
X_test = X_test / 255
# Converts y_train and y_test, which are class vectors, to a binary class array (one-hot vectors)
y_train = np_utils. to_categorical ( y_train )
y_test = np_utils. to_categorical ( y_test )
# Number of digit types found in MNIST. In this case, the value is 10, corresponding to (0,1,2,3,4,5,6,7,8,9).
num_classes = y_test. shape [ 1 ]


def deeper_cnn_model ( ) :
    model = Sequential ( )
    # Convolution2D will be our input layer. We can observe that it has
    # 30 feature maps with size of 5 × 5 and an activation function of type ReLU.
    model.add ( Conv2D ( 30 , ( 5 , 5 ) , input_shape = ( 1 , 28 , 28 ) , activation = 'relu' ) )
    # The MaxPooling2D layer will be our second layer where we will have a sample window of size 2 x 2
    model.add ( MaxPooling2D ( pool_size = ( 2 , 2 ) ) )
    # A new convolutional layer, with 15 feature maps of size 3 × 3, and activation function ReLU
    model.add ( Conv2D ( 15 , ( 3 , 3 ) , activation = 'relu' ) )
    # A new subsampling with a 2x2 dimension pooling.
    model.add ( MaxPooling2D ( pool_size = ( 2 , 2 ) ) )

    # We include a dropout with a 20% probability (you can try other values)
    model.add ( Dropout ( 0.2 ) )
    # We need to convert the output of the convolutional layer, so that it can be used as input to the densely connected layer that is next.
    # What this does is "flatten / flatten" the structure of the output of the convolutional layers, creating a single long vector of features
    # that will be used by the Fully Connected layer.
    model.add ( Flatten ( ) )
    # Fully connected layer with 128 neurons.
    model.add ( Dense ( 128 , activation = 'relu' ) )
    # Followed by a new fully connected layer with 64 neurons
    model.add ( Dense ( 64 , activation = 'relu' ) )

    # Followed by a new fully connected layer with 32 neurons
    model.add ( Dense ( 32 , activation = 'relu' ) )
    # The output layer has the number of neurons compatible with the
    # number of classes to be obtained. Notice that we are using a softmax activation function,
    model.add ( Dense ( num_classes, activation = 'softmax' , name = 'preds' ) )
    # Configure the entire training process of the neural network
    model.compile ( loss = 'categorical_crossentropy' , optimizer = 'adam' , metrics = [ 'accuracy' ] )

    return model


model = deeper_cnn_model ( )
model.summary ( )
model.fit ( X_train , y_train, validation_data = ( X_test , y_test ) , epochs = 10 , batch_size = 200 )
scores = model. evaluate ( X_test , y_test, verbose = 0 )
print ( "\ nacc:% .2f %%" % (scores [1] * 100))


###enhance to check multiple numbers after the training is done

img_pred = cv2. imread ( 'five.JPG' ,   0 )

plt.imshow(img_pred, cmap='gray')
# forces the image to have the input dimensions equal to those used in the training data (28x28)
if img_pred. shape != [ 28 , 28 ] :
    img2 = cv2. resize ( img_pred, ( 28 , 28 ) )
    img_pred = img2. reshape ( 28 , 28 , - 1 ) ;
else :
    img_pred = img_pred. reshape ( 28 , 28 , - 1 ) ;

# here also we inform the value for the depth = 1, number of rows and columns, which correspond 28x28 of the image.
img_pred = img_pred. reshape ( 1 , 1 , 28 , 28 )
pred = model. predict_classes ( img_pred )
pred_proba = model. predict_proba ( img_pred )
pred_proba = "% .2f %%" % (pred_proba [0] [pred] * 100)
print ( pred [ 0 ] , "with probability of" , pred_proba )

最后,我尝试对我绘制和导入的数字 5 进行预测(我也尝试过其他手绘数字,结果同样糟糕):

img_pred = cv2. imread ( 'five.JPG' ,   0 )

plt.imshow(img_pred, cmap='gray')
# forces the image to have the input dimensions equal to those used in the training data (28x28)
if img_pred. shape != [ 28 , 28 ] :
    img2 = cv2. resize ( img_pred, ( 28 , 28 ) )
    img_pred = img2. reshape ( 28 , 28 , - 1 ) ;
else :
    img_pred = img_pred. reshape ( 28 , 28 , - 1 ) ;

# here also we inform the value for the depth = 1, number of rows and columns, which correspond 28x28 of the image.
img_pred = img_pred. reshape ( 1 , 1 , 28 , 28 )
pred = model. predict_classes ( img_pred )
pred_proba = model. predict_proba ( img_pred )
pred_proba = "% .2f %%" % (pred_proba [0] [pred] * 100)
print ( pred [ 0 ] , "with probability of" , pred_proba )

这里看一下五个.jpg:

hand drawn five image

但是当我输入自己的数字时,模型预测错误。关于为什么会这样的任何想法?我承认我是 ML 新手,刚刚开始涉足它。我的想法可能是图像的居中或图像的标准化已关闭?非常感谢任何帮助!

编辑1:

MNIST 测试编号如下所示:

white numbers black backgrounds

【问题讨论】:

  • 你也能显示一个工作测试图像吗?也许您自己的数字图像格式错误(需要灰度和一定的宽度/高度?)
  • 我同意@Noface,但另外,对于 28x28x1 的手写数字,它可能是 MNIST。 MNIST 通常是倒置的:黑色背景上的白色数字。这也是单通道强度。您的 jpeg 看起来像是彩色(3 通道),在浅色背景上绝对是深色数字。
  • 我添加了一张 MNIST 测试图像的图像,它肯定是黑色背景上的白色数字。我想也许代码获取了 5.jpg 图像并将其转换为灰度,但也许它也需要反转?感谢您的反馈!

标签: python machine-learning neural-network


【解决方案1】:

您似乎有两个问题,正如您所怀疑的,这与您的数据预处理有关。

首先是你的图像相对于训练数据是倒置的:

  • 使用img_pred = cv2. imread ( 'five.JPG' , 0 ) 读取您的.jpg 的一个通道后,背景像素接近白色,值在215-238 附近。
  • 如果您查看X_train 中的训练数据,背景像素全部为零,数字为白色或接近白色(210-255 上方)。

尝试将您的图像绘制在来自X_train 的一些选择旁边,您会看到它们是倒置的。

另一个问题是cv2.resize() 中的默认插值不会保留数据的缩放比例。调整数据大小后,最小值会上升到 60,而不是 0。比较调整步骤前后 img.pred.min()img.pred.max() 的值。

您可以使用如下函数反转和缩放数据,使其看起来更像 MNIST 输入数据:

 def mnist_bytescale(image):
    # Use float for rescaling
    img_temp = image.astype(np.float32)
    #Re-zero the data
    img_temp -= img_temp.min()
    #Re-scale and invert
    img_temp /= (img_temp.max()-img_temp.min())
    img_temp *= 255
    return 255 - img_temp.astype('uint')

这将翻转您的数据,并将其从 0 线性扩展到 255,就像网络正在训练的数据一样。但是,如果您绘制mnist_bytescale(img_pred),您会注意到大多数像素的背景级别仍然不是 0,因为原始图像的背景级别不是恒定的(可能是由于 JPEG 压缩。)如果您的网络仍然有问题有了这个翻转和缩放的数据,您可以尝试使用np.clip 将背景级别归零,看看是否有帮助。

【讨论】:

    猜你喜欢
    • 2019-01-19
    • 1970-01-01
    • 2018-01-30
    • 2021-09-01
    • 2017-01-22
    • 2019-06-16
    • 2017-03-03
    • 2019-05-14
    • 1970-01-01
    相关资源
    最近更新 更多