Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.85 KB | None | 0 0
  1. In[35]:= feats = RandomSample[feats];
  2. Length@feats
  3. RandomSample::lrwl: The set of items to sample from,
  4. Join[normalFeats,virusFeats,bacFeats], should be a non-empty list or a rule
  5. weights -> choices.
  6. Out[36]= 1
  7.  
  8. In[20]:= trainedNet =
  9. NetTrain[learnNet, trainFeat, ValidationSet -> testFeat,
  10. MaxTrainingRounds -> 35, TargetDevice -> "GPU"]
  11.  
  12. NetTrain::jitgen: Generator function cannot be used when the net to be
  13. trained has not been fully specified.
  14.  
  15. NetTrain::nfspec: Cannot train net: input of first layer is not fully
  16. specified.
  17.  
  18. Out[20]= $Failed
  19.  
  20. "C:\Users\El_Elyon`s\Desktop\prj\"
  21.  
  22. SetDirectory["/Users/El_Elyon`s/Desktop/prj/"]
  23.  
  24. "C:\Users\El_Elyon`s\Desktop\prj"
  25.  
  26. normalFiles = FileNames[Except["."] ~~ "*.jpeg", NotebookDirectory[] <>
  27. "train/NORMAL"];
  28.  
  29. virusFiles =FileNames[Except["."] ~~ "*_virus_*.jpeg",
  30. NotebookDirectory[] <> "train/PNEUMONIA"];
  31. bacFiles =FileNames[Except["."] ~~ "*_bacteria_*.jpeg",
  32. NotebookDirectory[] <> "train/PNEUMONIA"];
  33.  
  34. normalImg = Import /@ normalFiles;
  35.  
  36. virusImg = Import /@ virusFiles;
  37.  
  38. bacImg = Import /@ bacFiles;
  39.  
  40. ClearAll[cropFinder];
  41. cropFinder[x_, img_] := Module[{mid, flatValues, cropLeft, cropRight},
  42. mid = Ceiling[Last[ImageDimensions[img]]/(x)];
  43. flatValues =
  44. Flatten@ImageData[ImageTake[ColorConvert[img, "Grayscale"], {mid, mid}]];
  45. cropLeft = LengthWhile[flatValues, # < 0.16 &];
  46. cropRight = LengthWhile[Reverse[flatValues], # < 0.16 &];
  47. List[cropLeft, cropRight]
  48. ]
  49.  
  50. ClearAll[widthResize];
  51. widthResize[img_] :=
  52. Block[{ flatValues, cropLeft, cropRight, meanLeft, meanRight, list1, list2,
  53. list3, list4, list5, resizedImage},
  54. list1 = cropFinder[10, img];
  55. list2 = cropFinder[2, img];
  56. list3 = cropFinder[4, img];
  57. list4 = cropFinder[1.5, img];
  58. list5 = cropFinder[1, img];
  59. meanLeft = meanLeft /. x_ /; x > 300 :> 0;
  60. meanLeft = -(Max[{list1[[1]], list2[[1]], list3[[1]], list4[[1]],
  61. list5[[1]]}]);
  62. meanRight = meanRight /. x_ /; x > 300 :> 0;
  63. meanRight = -((Max[{list1[[2]], list2[[2]], list3[[2]], list4[[2]],
  64. list5[[2]]}]));
  65. resizedImage = ImagePad[img, {{meanLeft, meanRight}, {0, 0}}];
  66. ImageResize[resizedImage, {512, 512}]
  67. ]
  68.  
  69. normNorm =
  70. ConformImages[
  71. Table[If[RandomReal[] > .8,
  72. If[RandomReal[] > .5, rotateImage[i], shearImage[i]], i], {i,
  73. normalImg}], {512, 512}];
  74. virusNorm =
  75. ConformImages[
  76. Table[If[RandomReal[] > .8,
  77. If[RandomReal[] > .5, rotateImage[i], shearImage[i]], i], {i,
  78. virusImg}], {512, 512}];
  79.  
  80. bacNorm = ConformImages[
  81. Table[If[RandomReal[] > .8,
  82. If[RandomReal[] > .5, rotateImage[i], shearImage[i]], i], {i,
  83. bacImg}], {512, 512}];
  84.  
  85. RayNet = NetModel["ResNet-50 Trained on ImageNet Competition Data"]
  86.  
  87. NetChain[ <> ]
  88.  
  89. In[3]:= FixedNet = NetTake[RayNet, 23]
  90.  
  91. Out[3]= NetChain[ <> ]
  92.  
  93. In[4]:= AdjustedNet =
  94. NetReplacePart[FixedNet, "Input" -> NetEncoder[{"Image", {512, 512}}]]
  95.  
  96. Out[4]= NetChain[ <> ]
  97.  
  98. In[5]:= learnNet = NetChain[{
  99. DropoutLayer[0.3],
  100. LinearLayer[3],
  101. SoftmaxLayer[]
  102. },
  103. "Output" -> NetDecoder[{"Class", {0, 1, 2}}]
  104. ]
  105.  
  106. Out[5]= NetChain[ <> ]
  107.  
  108. normalFeats = AdjustedNet[normNorm, TargetDevice -> "GPU"];
  109.  
  110. virusFeats = AdjustedNet[virusNorm, TargetDevice -> "GPU"];
  111.  
  112. bacFeats = AdjustedNet[bacNorm, TargetDevice -> "GPU"];
  113.  
  114. In[33]:= feats = Join[Map[# -> 0 &, normalFeats], Map[# -> 1 &, virusFeats],
  115. Map[# -> 2 &, bacFeats]];
  116.  
  117. In[35]:= feats = RandomSample[feats];
  118. Length@feats
  119.  
  120. In[35]:= RandomSample::lrwl: The set of items to sample from,
  121. Join[normalFeats,virusFeats,bacFeats], should be a non-empty list or a rule
  122. weights -> choices.
  123.  
  124. Out[36]= 1
  125.  
  126. trainFeat = feats[[1 ;; 5000]];
  127.  
  128. testFeat = feats[[5000 ;;]];
  129.  
  130. In[20]:= trainedNet =
  131. NetTrain[learnNet, trainFeat, ValidationSet -> testFeat,
  132. MaxTrainingRounds -> 35, TargetDevice -> "GPU"]
  133.  
  134. In[20]:= NetTrain::jitgen: Generator function cannot be used when the net to
  135. be trained has not been fully specified.
  136.  
  137. In[20]:= NetTrain::nfspec: Cannot train net: input of first layer is not
  138. fully specified.
  139.  
  140. Out[20]= $Failed``
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement