Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
- 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
- "C:\Users\El_Elyon`s\Desktop\prj\"
- SetDirectory["/Users/El_Elyon`s/Desktop/prj/"]
- "C:\Users\El_Elyon`s\Desktop\prj"
- 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}];
- 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[ <> ]
- 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``
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement