生成图像的灰度值

时间:2016-07-28 19:07:10

标签: r image image-recognition mnist

我已经训练了一个模型(在着名的MNIST数据上)来学习识别从0到9的数字图像。强度值是作为特征集提供的。

现在我想自己测试模型,因为我想说在MS Paint上写一个数字并运行模型。

我知道如何使用PNG包将图像转换为灰度值,但我需要帮助创建具有相似灰度范围的图像。目前,当我尝试绘制Paint时,它的范围为0:255,与具有负值的训练集不同。

注意:我不知道我需要使用什么格式的图像来获得类似的强度值,我最简单的方法是去画画并画出数字

问题: An Example of the Grayscale Values of the Digit 1

im <- matrix(data=rev(X[567,]), nrow=20, ncol=20)

     [,1] [,2] [,3]          [,4]          [,5]          [,6]          [,7]          [,8]          [,9]         [,10]         [,11]         [,12]         [,13]
 [1,]    0    0    0  0.000000e+00  0.0000000000  0.0000000000  0.000000e+00  0.0000000000  0.000000e+00  0.000000e+00  0.0000000000  0.000000e+00  0.000000e+00
 [2,]    0    0    0  0.000000e+00  0.0000000000  0.0000000000  0.000000e+00  0.0000000000  0.000000e+00  0.000000e+00  0.0000000000  4.306236e-04 -4.538135e-03
 [3,]    0    0    0  0.000000e+00  0.0000000000  0.0000000000  0.000000e+00  0.0000000000  0.000000e+00  0.000000e+00  0.0002343973 -1.140496e-02  2.497616e-02
 [4,]    0    0    0  0.000000e+00  0.0000000000  0.0000000000  0.000000e+00  0.0000000000  0.000000e+00  1.084559e-04 -0.0017490639 -1.345621e-02  4.384232e-01
 [5,]    0    0    0  0.000000e+00  0.0000000000  0.0000000000  0.000000e+00  0.0000000000  2.941176e-05 -4.375000e-04 -0.0261209150  2.488099e-01  9.544290e-01
 [6,]    0    0    0  0.000000e+00  0.0000000000  0.0000000000  0.000000e+00  0.0000000000  2.393280e-04 -2.528663e-02  0.1323503711  8.318632e-01  1.015593e+00
 [7,]    0    0    0  0.000000e+00  0.0000000000  0.0000000000  0.000000e+00  0.0001834414 -6.974929e-03  3.770381e-02  0.6445272331  1.033006e+00  8.613194e-01
 [8,]    0    0    0  0.000000e+00  0.0000000000  0.0000000000  2.445885e-05 -0.0001720764 -2.008902e-02  2.677583e-01  1.0012065346  9.811198e-01  3.359074e-01
 [9,]    0    0    0  0.000000e+00  0.0000000000  0.0000000000  3.487541e-04 -0.0256610004  1.337907e-01  8.190443e-01  1.0119159688  5.299348e-01 -2.273144e-03
[10,]    0    0    0  0.000000e+00  0.0000000000  0.0007352941 -1.754459e-02  0.0444801985  6.604204e-01  1.036383e+00  0.7427878881  6.253465e-02 -1.474359e-02
[11,]    0    0    0  0.000000e+00  0.0000000000 -0.0053142872  3.982375e-02  0.6389624523  1.033114e+00  8.733544e-01  0.1483327546 -1.978789e-02  5.014064e-04
[12,]    0    0    0  0.000000e+00  0.0003370098 -0.0245936309  2.214513e-01  0.9496550623  1.001519e+00  4.032970e-01 -0.0262422045 -1.246885e-03  3.668827e-05
[13,]    0    0    0  2.201296e-05 -0.0071477926  0.0124218676  5.890595e-01  1.0428453590  7.664877e-01  6.238350e-02 -0.0170552566  1.654030e-04  0.000000e+00
[14,]    0    0    0 -1.326593e-04 -0.0214352533  0.1659780263  8.681923e-01  1.0228496087  4.833438e-01 -2.121145e-02 -0.0023410267  1.021242e-06  0.000000e+00
[15,]    0    0    0 -1.412275e-03 -0.0192939474  0.4265679126  1.037142e+00  0.9012997670  9.698972e-02 -1.290765e-02  0.0002604167  0.000000e+00  0.000000e+00
[16,]    0    0    0 -1.593035e-03 -0.0186662922  0.4575771889  1.093103e+00  0.7281629027 -3.015387e-02  7.327410e-04  0.0000000000  0.000000e+00  0.000000e+00
[17,]    0    0    0 -3.634600e-04 -0.0099532952  0.1448730596  4.328676e-01  0.1434386592 -9.253983e-03  3.063725e-05  0.0000000000  0.000000e+00  0.000000e+00
[18,]    0    0    0  4.647181e-05  0.0011291835 -0.0175039746 -5.072072e-02 -0.0191029196  1.039501e-03  1.043178e-17  0.0000000000  0.000000e+00  0.000000e+00
[19,]    0    0    0  0.000000e+00  0.0000000000  0.0000000000  0.000000e+00  0.0000000000  0.000000e+00  0.000000e+00  0.0000000000  0.000000e+00  0.000000e+00
[20,]    0    0    0  0.000000e+00  0.0000000000  0.0000000000  0.000000e+00  0.0000000000  0.000000e+00  0.000000e+00  0.0000000000  0.000000e+00  0.000000e+00
              [,14]         [,15]         [,16]         [,17] [,18] [,19] [,20]
 [1,]  0.0000000000  0.000000e+00  0.000000e+00  0.000000e+00     0     0     0
 [2,] -0.0216402846 -1.239362e-02  2.570125e-05  6.076389e-05     0     0     0
 [3,]  0.6271519370  6.741898e-01  1.490166e-02 -3.797542e-03     0     0     0
 [4,]  1.0680277608  5.460697e-01 -8.837061e-03 -2.389553e-03     0     0     0
 [5,]  0.8673059811  1.769967e-01 -1.832898e-02 -3.108660e-04     0     0     0
 [6,]  0.4489610566 -2.979454e-02 -2.363971e-03  5.238971e-05     0     0     0
 [7,]  0.0371314849 -7.122634e-03  2.040441e-04  0.000000e+00     0     0     0
 [8,] -0.0279941706  7.847214e-04  0.000000e+00  0.000000e+00     0     0     0
 [9,] -0.0058922249  1.244466e-17  0.000000e+00  0.000000e+00     0     0     0
[10,]  0.0003111383  9.320045e-32  0.000000e+00  0.000000e+00     0     0     0
[11,]  0.0000245098  0.000000e+00  0.000000e+00  0.000000e+00     0     0     0
[12,]  0.0000000000  0.000000e+00  0.000000e+00  0.000000e+00     0     0     0
[13,]  0.0000000000  0.000000e+00  0.000000e+00  0.000000e+00     0     0     0
[14,]  0.0000000000  0.000000e+00  0.000000e+00  0.000000e+00     0     0     0
[15,]  0.0000000000  0.000000e+00  0.000000e+00  0.000000e+00     0     0     0
[16,]  0.0000000000  0.000000e+00  0.000000e+00  0.000000e+00     0     0     0
[17,]  0.0000000000  0.000000e+00  0.000000e+00  0.000000e+00     0     0     0
[18,]  0.0000000000  0.000000e+00  0.000000e+00  0.000000e+00     0     0     0
[19,]  0.0000000000  0.000000e+00  0.000000e+00  0.000000e+00     0     0     0
[20,]  0.0000000000  0.000000e+00  0.000000e+00  0.000000e+00     0     0     0

image(1:20, 1:20, im, col=gray((0:255)/255)) #But My Lower Values Extend Past Help Required Here too to set the range (NOT 0:255)

Using the Image Function to Plot

我想自己创建(绘制)某些数字并进行测试,但我想确保它们属于相同的值范围,我该如何实现?

1 个答案:

答案 0 :(得分:1)

您希望缩放新数据(来自MSPaint),以便它与您用于训练模型的数据相匹配。获取训练数据集的均值和方差,并使用scale将其应用于新图像。

means <- colMeans(training_data)
std <- apply(training_data, 2, FUN = sd, na.rm = T)
new_im <- scale(im, center = means, scale = std)

我还建议返回并创建一个新模型,其中训练数据预先正确缩放。就像@MarkSetchell一样,我对PNG中负值的含义感到困惑。