# Image Generator with a Neural Network (CIFAR-10)

As some of you may know, CIFAR-10 is a data base which has 60,000 images (which are 32x32 in size) of 10 different categories (airplane, dog, cat, frog, deer, bird, horse, ship, automobile and truck). My idea is to revert the process, i.e I would like to be able to give the program a name (dog, for example) and recieve an image which resembles a that object (the dog, in this case). I am having problems though with the neural network itself and I am not sure of how I should give the input to it.

So, first of all I load CIFAR-10:

cifar10 = ResourceObject["CIFAR-10"]


I also loaded the training data and transformed all the names (airplane, dog, etc) into numbers from 0 to 9 because I think that it is going to be easier if I have those inputs as digits.

rawtraining =
ResourceData[cifar10, "TrainingData"] /. {"airplane" -> 0,
"dog" -> 1, "cat" -> 2, "frog" -> 3, "deer" -> 4, "bird" -> 5,
"horse" -> 6, "ship" -> 7, "automobile" -> 8, "truck" -> 9};

rawtest =
ResourceData[cifar10, "TestData"] /. {"airplane" -> 0, "dog" -> 1,
"cat" -> 2, "frog" -> 3, "deer" -> 4, "bird" -> 5, "horse" -> 6,
"ship" -> 7, "automobile" -> 8, "truck" -> 9};

RandomSample[rawtest, 5]


Afterwards, I just invert the input and the output with this function (I am also converting the image output into data with ImageData):

rawtoGAN[list_] := list[[2]] -> ImageData[list[[1]]]
training = rawtoGAN[#] & /@ rawtraining;
test = rawtoGAN[#] & /@ rawtest;


But the ImageData on my outputs gives a fairly big matrix which encodes the image in RGB parameters. When I try to train the following neural net it always gives me a message saying that the input is incorrect (sometimes it also says the output is incorrect, so I guess I am messing with both).

 generator =
NetInitialize[
NetChain[{256, Ramp, BatchNormalizationLayer[], 512, Ramp,
BatchNormalizationLayer[], 1024, Ramp, BatchNormalizationLayer[],
32^2, ReshapeLayer[{32, 32}]}, "Input" -> {1, 3}]]

trainedGen =
NetTrain[generator, training, ValidationSet -> test,
MaxTrainingRounds -> 4]


After "trainedGen", I see a message saying that either my input or my output are bad. I would like to know if anyone has an idea of how I could give proper inputs to my neural network or if I should change it all in order to obtain an image out of a number (the reverse process of CIFAR-10). I hope the explanation is not very confusing, I tried to do my best.

Thank you very much!

• The output of your generator is a grayscale image 32x32, while your training set contains RGB images. The input seems wrong too, because in the training set the input dimension is {1} while the generator expects a {1,3} . – Fortsaint Dec 19 '19 at 10:45

Based on the original description, it doesn't seem that you need a NN to do what you want so I'm most probably missing something. I think the following code will do what you want without a neural network.

ClearAll[objData, reverseObjData, byNameObjData, GetObject]
objData = ResourceData["CIFAR-10"];