2
$\begingroup$

I 500 pictures of tables and chairs each (849x849) and want to train a convolutional neural net to classify new pictures accordingly. This is my code so far:

cfiles = Import["D:\\Mathematica11\\bo_sample\\chairs\\*"];
tfiles = Import["D:\\Mathematica11\\bo_sample\\tables\\*"];

$train = 300;
trainingData = Join[
 Thread[cfiles[[;; $train]] -> "chairs"],
 Thread[tfiles[[;; $train]] -> "tables"]
 ];
testingData = Join[
  Thread[cfiles[[$train + 1 ;;]] -> "chairs"] ,
  Thread[tfiles[[$train + 1 ;;]] -> "tables"]
  ];

lenet = NetChain[{ConvolutionLayer[20, 5], 
              Ramp, 
              PoolingLayer[2, 2], 
              ConvolutionLayer[50, 5], 
              Ramp, 
              PoolingLayer[2, 2], 
              FlattenLayer[],
              500, 
              Ramp, 
              2, 
              SoftmaxLayer[]}, 
              "Output" -> NetDecoder[{"Class", Range[0, 1]}], 
              "Input" -> NetEncoder[{"Image", {849, 849}, "RGB"}]];


trainedNN =  NetTrain[lenet, trainingData, ValidationSet -> testingData,  MaxTrainingRounds -> 3];

...Failure GPU Memory exhausted

It seems the usage of NetTrain is not correct in both cases but the error-messages are not clear to me. Can someone take a look please?

$\endgroup$
  • $\begingroup$ Probably you need to NetInitialize your graph first. $\endgroup$ – swish Jan 18 '17 at 17:49
  • $\begingroup$ Hi. I tried initNN = NetInitialize[lenet] but I get the same error-message with trainedNN. $\endgroup$ – user3483676 Jan 18 '17 at 18:02
  • $\begingroup$ RandomSample doesn't work as you expect there, fix it first. $\endgroup$ – swish Jan 18 '17 at 18:08
  • $\begingroup$ I deleted the first NetTrain segment (lenet = NetTrain[lenet, RandomSample.... which produced the first error message. This was just a different approach which did not work. Actually I would prefer the second approach with trainedNN = NetTrain... to work. $\endgroup$ – user3483676 Jan 18 '17 at 18:13
  • $\begingroup$ Reverse the arrows in your Association, outputs should be on the right. $\endgroup$ – swish Jan 18 '17 at 18:23
5
$\begingroup$
  1. Make proper data format as list of rules $(Image \rightarrow Class)$

    trainingData = Join[
        Thread[cfiles[[;; $train]] -> "chairs"],
        Thread[tfiles[[;; $train]] -> "tables"]
    ];
    testingData = Join[
        Thread[cfiles[[$train + 1 ;;]] -> "chairs"] ,
        Thread[tfiles[[$train + 1 ;;]] -> "tables"]
    ];
    
  2. NetDecoder should look like this

    NetDecoder[{"Class", {"chairs", "tables"}}]
    

This works for me:

cfiles = Array[RandomImage[1, {100, 100}] &, {100}, 
   ColorSpace -> "RGB"];
tfiles = Array[RandomImage[1, {100, 100}] &, {100}, 
   ColorSpace -> "RGB"];

$train = 80;
trainingData = Join[
   Thread[cfiles[[;; $train]] -> "chairs"],
   Thread[tfiles[[;; $train]] -> "tables"]
   ];
testingData = Join[
   Thread[cfiles[[$train + 1 ;;]] -> "chairs"] ,
   Thread[tfiles[[$train + 1 ;;]] -> "tables"]
   ];
lenet = NetChain[{ConvolutionLayer[20, 5], Ramp, PoolingLayer[2, 2], 
    ConvolutionLayer[50, 5], Ramp, PoolingLayer[2, 2], FlattenLayer[],
     500, Ramp, 2, SoftmaxLayer[]}, 
   "Output" -> NetDecoder[{"Class", {"chairs", "tables"}}], 
   "Input" -> NetEncoder[{"Image", {100, 100}, "RGB"}]];
trainedNN = 
 NetTrain[lenet, trainingData, ValidationSet -> testingData, 
  MaxTrainingRounds -> 3]
$\endgroup$
  • $\begingroup$ I tried your code but still get the same error. $\endgroup$ – user3483676 Jan 18 '17 at 19:06
  • $\begingroup$ I tried your code. It works but if I use it on my imported pictures I get error-message GPU memory exhausted. I probably have to resize them because your trainingset is smaller and also the size of the random images. $\endgroup$ – user3483676 Jan 19 '17 at 4:32
  • $\begingroup$ Ok. The training is now running. Thx for the support. $\endgroup$ – user3483676 Jan 19 '17 at 16:14

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.