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]

This is a little sample of how it looks

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 = 
  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!

  • $\begingroup$ 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} . $\endgroup$
    – Fortsaint
    Dec 19, 2019 at 10:45
  • 1
    $\begingroup$ Have a look at NetGANOperator reference.wolfram.com/language/ref/… $\endgroup$
    – flinty
    May 17, 2020 at 12:13

1 Answer 1


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"];
reverseObjData = MapThread[Rule, {Values[objData], Keys[objData]}];
byNameObjData = GatherBy[reverseObjData, Keys[#] &];
byNameObjData = Keys[First[#]] -> Values[#] & /@ byNameObjData;
GetObject[aName_String] := RandomChoice[Lookup[byNameObjData, aName]];
        {chosen, {"airplane", "automobile", "bird", "cat", "deer", 
           "dog", "frog", "horse", "ship", "truck"}}]

Related to your question, do you have a description of the CIFAR-10 NN in Mathematica? I see how to get the data from the Wolfram repository but I am not seeing the CNN description.

  • $\begingroup$ Nice code! But they want to generate an image, from my understanding they already showed the ability to randomly choose an example, albeit not named. I believe that portion is to give random examples upon which to train their GAN that should output a generated example of the named input. $\endgroup$ Dec 18, 2019 at 23:18
  • $\begingroup$ Thanks for your comment. Now I understand - they want to somehow auto-generate, not pick from among the choices. $\endgroup$
    – Mark R
    Dec 19, 2019 at 20:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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