问题
I have trained a model (on the famous MNIST data) to learn to identify images of digits from 0 to 9. The intensity values were provided as feature sets.
Now I want to test the model myself, for that I want to say write a number on MS Paint and run through the model.
I know how to use PNG package to convert the image to grayscale values but I need help in creating the image with similar range of grayscle. Currently when I try to draw in Paint it ranges from 0:255, unlike in the training set which has negative values too.
Note: I do not know what format of image I need to use to get similar intensity values, my easiest way was to go to paint and draw the digit
The Problem:
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)
I want to create (draw) certain digits myself and test, but I want to make sure they belong to the same range of values, how do I achieve that?
回答1:
You want to scale the new data (from MSPaint) so it matches that of the data you used to train your model. Get the mean and variance of the training dataset and use scale
to apply it to the new image.
means <- colMeans(training_data)
std <- apply(training_data, 2, FUN = sd, na.rm = T)
new_im <- scale(im, center = means, scale = std)
I would also suggest going back and creating a new model where the training data is properly scaled beforehand. Like @MarkSetchell I'm confused as to what negative values mean in PNGs.
来源:https://stackoverflow.com/questions/38644813/generating-grayscale-values-of-an-image