0
$\begingroup$

Documentation have some examples about MNIST.But those image have same image.I make some trained data

data = RandomSample[
   Catenate[
    Thread /@ 
     Tuples[Function[font, 
         Image[Rasterize[Style[ToString[#], FontFamily -> font], 
             ImageResolution -> 150]] & /@ Range[21]] /@ 
        RandomSample[$FontFamilies, 10] -> {Range[21]}]]];

These images have difference size.And I don't know this trained net will face which size image.But it should recognize it.And I don't know it will face which font,so I make some random font to train it. There is a try about how to build layers to train it

net = NetChain[{ImageAugmentationLayer[{12, 12}, 
    "Input" -> NetEncoder[{"Image", {15, 15}}], 
    "Output" -> NetDecoder["Image"]], FlattenLayer[], 21}, 
  "Output" -> NetDecoder[{"Class", Range[21]}]]

But I don't know the error information mean what..

NetTrain[net, data]

$\endgroup$
  • $\begingroup$ Try this net: NetModel["LeNet Trained on MNIST Data"]. And if you want to retrain it on your own data: NetModel["LeNet Trained on MNIST Data", "UninitializedEvaluationNet"] (Actually turns out you'll need to adapt it. MNIST is only 1-9) $\endgroup$ – b3m2a1 Sep 16 '17 at 20:32
  • $\begingroup$ @b3m2a1 Yes,in my case,it will over 9.And it is not handwriting. $\endgroup$ – yode Sep 16 '17 at 20:44
  • $\begingroup$ You can pretty easily adapt LeNet to that, though (that's what I did). I'm sure with a bigger set of images you can get better results than my answer. $\endgroup$ – b3m2a1 Sep 17 '17 at 1:06
  • $\begingroup$ @b3m2a1 If the model is overfit,then even you train it with a bigger set,it will not be better I think(I just guess so).. $\endgroup$ – yode Sep 25 '17 at 2:33
2
$\begingroup$

So I pulled a net pretty much straight from here:

NetModel["LeNet Trained on MNIST Data", "ConstructionNotebook"]

What it ended looking like was:

nm =
  NetChain[{
    ConvolutionLayer[20, {5, 5}],
    ElementwiseLayer[Ramp],
    PoolingLayer[{2, 2}, {2, 2}],
    ConvolutionLayer[50, {5, 5}],
    ElementwiseLayer[Ramp],
    PoolingLayer[{2, 2}, {2, 2}],
    FlattenLayer[],
    DotPlusLayer[500],
    ElementwiseLayer[Ramp],
    LinearLayer[22],
    SoftmaxLayer[]
    },
   "Output" -> NetDecoder[{"Class", Range[0, 21]}],
   "Input" -> NetEncoder[{"Image", {28, 28}, "Grayscale"}]
   ];

All I changed was the LinearLayer[22] and the Range[0, 21].

Then:

data = RandomSample[
   Catenate[
    Thread /@ 
     Tuples[Function[font, 
         Image[Rasterize[Style[ToString[#], FontFamily -> font], 
             ImageResolution -> 150]] & /@ Range[21]] /@ 
        RandomSample[$FontFamilies, 10] -> {Range[21]}]]
   ];

trained = NetTrain[nm, Take[data, 150]];

Association[Reverse /@ Take[data, {151, -1}]] // Map[trained]

<|16 -> 16, 4 -> 4, 13 -> 13, 11 -> 21, 17 -> 17, 12 -> 12, 1 -> 1, 
 6 -> 5, 15 -> 14, 21 -> 21, 19 -> 19, 18 -> 19, 7 -> 7, 10 -> 16, 
 2 -> 2, 20 -> 20, 14 -> 14, 3 -> 3, 5 -> 5, 9 -> 9|>

(Note the attempted separation of test set and training set)

All in all that's pretty good.

$\endgroup$
  • $\begingroup$ When I set a ValidationSet,it don't converge like this. I mean it it overfit.It is not a good model.. $\endgroup$ – yode Sep 25 '17 at 2:30
  • $\begingroup$ @yode Ah too bad. Well maybe the model can be adapted, but I don't really know enough to say how. I was mostly interested showing how LeNet could be switched up for your case. $\endgroup$ – b3m2a1 Sep 25 '17 at 2:36
1
$\begingroup$

Only add a SoftmaxLayer[] layer,then it's OK

net = NetInitialize@NetChain[{ImageAugmentationLayer[{12, 12}, "Input" -> NetEncoder[{"Image", {15, 15}}], "Output" -> NetDecoder["Image"]], FlattenLayer[], 
                              21, 
                              SoftmaxLayer[]}, 
                             "Output" -> NetDecoder[{"Class", Range[21]}]]

Because of SoftmaxLayer[][lis] will make the output represent probability.

And the net is too simple,lenet based on CNN will help improve the result

$\endgroup$
  • $\begingroup$ That mean you can train your net now? $\endgroup$ – yode Sep 25 '17 at 2:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.