# CNN Nettrain problem

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? • Probably you need to NetInitialize your graph first. – swish Jan 18 '17 at 17:49 • Hi. I tried initNN = NetInitialize[lenet] but I get the same error-message with trainedNN. – user3483676 Jan 18 '17 at 18:02 • RandomSample doesn't work as you expect there, fix it first. – swish Jan 18 '17 at 18:08 • 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. – user3483676 Jan 18 '17 at 18:13 • Reverse the arrows in your Association, outputs should be on the right. – swish Jan 18 '17 at 18:23 ## 1 Answer 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]

• I tried your code but still get the same error. – user3483676 Jan 18 '17 at 19:06
• 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. – user3483676 Jan 19 '17 at 4:32
• Ok. The training is now running. Thx for the support. – user3483676 Jan 19 '17 at 16:14