2
$\begingroup$

Got this Mathematica ResNet- 50 Code online and has been trying to make run it using the same data set but it seems my directories to the dataset were not correct, keep getting this error:

In[35]:= feats = RandomSample[feats];
Length@feats
RandomSample::lrwl: The set of items to sample from, 
Join[normalFeats,virusFeats,bacFeats], should be a non-empty list or a rule 
weights -> choices.
Out[36]= 1

and at the TrainNet I encountered this:

In[20]:= trainedNet = 
NetTrain[learnNet, trainFeat, ValidationSet -> testFeat, 
MaxTrainingRounds -> 35, TargetDevice -> "GPU"]

NetTrain::jitgen: Generator function cannot be used when the net to be 
trained has not been fully specified.

NetTrain::nfspec: Cannot train net: input of first layer is not fully 
specified.

Out[20]= $Failed

Here is the full Code NotebookDirectory[]

"C:\\Users\\El_Elyon`s\\Desktop\\prj\\"

SetDirectory["/Users/El_Elyon`s/Desktop/prj/"]

"C:\\Users\\El_Elyon`s\\Desktop\\prj"

Importing Raw Pictures

normalFiles = FileNames[Except["."] ~~ "*.jpeg", NotebookDirectory[] <> 
"train/NORMAL"];

virusFiles =FileNames[Except["."] ~~ "*_virus_*.jpeg", 
NotebookDirectory[] <> "train/PNEUMONIA"];
bacFiles =FileNames[Except["."] ~~ "*_bacteria_*.jpeg", 
  NotebookDirectory[] <> "train/PNEUMONIA"];

normalImg = Import /@ normalFiles;

virusImg = Import /@ virusFiles;

bacImg = Import /@ bacFiles;

ClearAll[cropFinder];
cropFinder[x_, img_] := Module[{mid, flatValues, cropLeft, cropRight},
mid = Ceiling[Last[ImageDimensions[img]]/(x)];
flatValues = 
Flatten@ImageData[ImageTake[ColorConvert[img, "Grayscale"], {mid, mid}]];
cropLeft = LengthWhile[flatValues, # < 0.16 &];
cropRight = LengthWhile[Reverse[flatValues], # < 0.16 &];
List[cropLeft, cropRight]
]

ClearAll[widthResize];
widthResize[img_] := 
Block[{ flatValues, cropLeft, cropRight, meanLeft, meanRight, list1, list2, 
list3, list4, list5, resizedImage},
list1 = cropFinder[10, img];
list2 = cropFinder[2, img];
list3 = cropFinder[4, img];
list4 = cropFinder[1.5, img];
list5 = cropFinder[1, img];
meanLeft = meanLeft /. x_ /; x > 300 :> 0;
meanLeft = -(Max[{list1[[1]], list2[[1]], list3[[1]], list4[[1]], 
   list5[[1]]}]);
meanRight = meanRight /. x_ /; x > 300 :> 0;
meanRight = -((Max[{list1[[2]], list2[[2]], list3[[2]], list4[[2]], 
    list5[[2]]}]));
resizedImage = ImagePad[img, {{meanLeft, meanRight}, {0, 0}}];
ImageResize[resizedImage, {512, 512}]
]

normNorm = 
ConformImages[
Table[If[RandomReal[] > .8, 
 If[RandomReal[] > .5, rotateImage[i], shearImage[i]], i], {i, 
 normalImg}], {512, 512}];
virusNorm = 
ConformImages[
Table[If[RandomReal[] > .8, 
 If[RandomReal[] > .5, rotateImage[i], shearImage[i]], i], {i, 
 virusImg}], {512, 512}];

bacNorm = ConformImages[
Table[If[RandomReal[] > .8, 
 If[RandomReal[] > .5, rotateImage[i], shearImage[i]], i], {i, 
 bacImg}], {512, 512}];

Initializing and Configuring Neural Net

RayNet = NetModel["ResNet-50 Trained on ImageNet Competition Data"]

NetChain[ <> ]

In[3]:= FixedNet = NetTake[RayNet, 23]

Out[3]= NetChain[ <> ]

In[4]:= AdjustedNet = 
NetReplacePart[FixedNet, "Input" -> NetEncoder[{"Image", {512, 512}}]]

Out[4]= NetChain[ <> ]

In[5]:= learnNet = NetChain[{
DropoutLayer[0.3],
LinearLayer[3],
SoftmaxLayer[]
},
"Output" -> NetDecoder[{"Class", {0, 1, 2}}]
]

Out[5]= NetChain[ <> ]

Preparing Images/Training the Net

normalFeats = AdjustedNet[normNorm, TargetDevice -> "GPU"];

virusFeats = AdjustedNet[virusNorm, TargetDevice -> "GPU"];

bacFeats = AdjustedNet[bacNorm, TargetDevice -> "GPU"];

In[33]:= feats = Join[Map[# -> 0 &, normalFeats], Map[# -> 1 &, virusFeats], 
Map[# -> 2 &, bacFeats]];

In[35]:= feats = RandomSample[feats];
Length@feats

In[35]:= RandomSample::lrwl: The set of items to sample from, 
Join[normalFeats,virusFeats,bacFeats], should be a non-empty list or a rule 
weights -> choices.

Out[36]= 1

trainFeat = feats[[1 ;; 5000]];

testFeat = feats[[5000 ;;]];

In[20]:= trainedNet = 
NetTrain[learnNet, trainFeat, ValidationSet -> testFeat, 
MaxTrainingRounds -> 35, TargetDevice -> "GPU"]

In[20]:= NetTrain::jitgen: Generator function cannot be used when the net to 
be trained has not been fully specified.

In[20]:= NetTrain::nfspec: Cannot train net: input of first layer is not 
fully specified.

Out[20]= $Failed``

ResNet-50 was successfully loaded from the internet, as I said earlier I feel the network is not being able to find the datasets in the directory to train it can someone help me out on how to get this code run properly?

$\endgroup$
  • $\begingroup$ The error message "Generator function cannot be used when the net to be trained has not been fully specified." sounds like the net isn't fully specified. You can try the NetInitialize the net, then feed it some input samples. If it returns a well-formed result, the net should be ok. $\endgroup$ – Niki Estner May 31 at 5:38
  • $\begingroup$ OK will try that out immediately. Thanks @Niki Estner $\endgroup$ – Raymond Confidence Jun 1 at 17:12

Your Answer

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

Browse other questions tagged or ask your own question.