|
a |
|
b/Projects/Caffe/allCNN/Info.py |
|
|
1 |
############################################################################################ |
|
|
2 |
# |
|
|
3 |
# The MIT License (MIT) |
|
|
4 |
# |
|
|
5 |
# Peter Moss Acute Myeloid/Lymphoblastic Leukemia AI Research Project |
|
|
6 |
# Copyright (C) 2018 Adam Milton-Barker (AdamMiltonBarker.com) |
|
|
7 |
# |
|
|
8 |
# Permission is hereby granted, free of charge, to any person obtaining a copy |
|
|
9 |
# of this software and associated documentation files (the "Software"), to deal |
|
|
10 |
# in the Software without restriction, including without limitation the rights |
|
|
11 |
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
|
12 |
# copies of the Software, and to permit persons to whom the Software is |
|
|
13 |
# furnished to do so, subject to the following conditions: |
|
|
14 |
# |
|
|
15 |
# The above copyright notice and this permission notice shall be included in |
|
|
16 |
# all copies or substantial portions of the Software. |
|
|
17 |
# |
|
|
18 |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
|
19 |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
|
20 |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
|
21 |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
|
22 |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
|
23 |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
|
24 |
# THE SOFTWARE. |
|
|
25 |
# |
|
|
26 |
# Title: Caffe Acute Lymphoblastic Leukemia CNN Info |
|
|
27 |
# Description: Used to view info Caffe Acute Lymphoblastic Leukemia CNN |
|
|
28 |
# Configuration: Required/Confs.json |
|
|
29 |
# Last Modified: 2019-03-10 |
|
|
30 |
# |
|
|
31 |
############################################################################################ |
|
|
32 |
|
|
|
33 |
import os, sys, cv2 |
|
|
34 |
sys.path.append('/home/upsquared/caffe/python') |
|
|
35 |
import caffe |
|
|
36 |
|
|
|
37 |
import numpy as np |
|
|
38 |
|
|
|
39 |
from Classes.Helpers import Helpers |
|
|
40 |
|
|
|
41 |
class allCNN(): |
|
|
42 |
|
|
|
43 |
def __init__(self): |
|
|
44 |
|
|
|
45 |
""" |
|
|
46 |
Sets up all default requirements and placeholders |
|
|
47 |
needed for the Caffe Acute Lymphoblastic Leukemia CNN. |
|
|
48 |
""" |
|
|
49 |
|
|
|
50 |
self.Helpers = Helpers() |
|
|
51 |
self.confs = self.Helpers.loadConfs() |
|
|
52 |
self.logFile = self.Helpers.setLogFile(self.confs["Settings"]["Logs"]["allCNN"]) |
|
|
53 |
|
|
|
54 |
self.Helpers.logMessage(self.logFile, "allCNN", "Status", "Init complete") |
|
|
55 |
|
|
|
56 |
def loadCaffeNet(self): |
|
|
57 |
|
|
|
58 |
""" |
|
|
59 |
Loads the Caffe network using prototxt layer definition. |
|
|
60 |
""" |
|
|
61 |
|
|
|
62 |
self.net = caffe.Net(self.confs["Settings"]["Classifier"]["Caffe"]["layerFile"], caffe.TEST) |
|
|
63 |
|
|
|
64 |
print("") |
|
|
65 |
self.Helpers.logMessage(self.logFile, "allCNN", "Status", "Caffe net initialized") |
|
|
66 |
|
|
|
67 |
def printDetails(self): |
|
|
68 |
|
|
|
69 |
""" |
|
|
70 |
Prints and logs input, blob and parameter info. |
|
|
71 |
""" |
|
|
72 |
|
|
|
73 |
# Prints the Net Inputs |
|
|
74 |
self.Helpers.logMessage(self.logFile, "allCNN", "Net Inputs", str(self.net.inputs)) |
|
|
75 |
|
|
|
76 |
# Prints the Net Blobs |
|
|
77 |
self.Helpers.logMessage(self.logFile, "allCNN", "Net Blobs", str(self.net.blobs)) |
|
|
78 |
|
|
|
79 |
# Prints the Net Blob shapes |
|
|
80 |
self.Helpers.logMessage(self.logFile, "allCNN", "Net Blob shapes", str([(k, v.data.shape) for k, v in self.net.blobs.items()])) |
|
|
81 |
|
|
|
82 |
# Prints the Net Params |
|
|
83 |
self.Helpers.logMessage(self.logFile, "allCNN", "Net Params", str(self.net.params)) |
|
|
84 |
|
|
|
85 |
# Prints the Net Params shapes |
|
|
86 |
self.Helpers.logMessage(self.logFile, "allCNN", "Net Params", str([(k, v[0].data.shape, v[1].data.shape) for k, v in self.net.params.items()])) |
|
|
87 |
|
|
|
88 |
print("") |
|
|
89 |
|
|
|
90 |
def writeOutputImages(self, image): |
|
|
91 |
|
|
|
92 |
""" |
|
|
93 |
Writes the output images for each neuron in the first convolution layer. |
|
|
94 |
""" |
|
|
95 |
|
|
|
96 |
# Transposes the input (50,50,3) -> (3,50,50) |
|
|
97 |
inp = np.transpose(cv2.imread(image)) |
|
|
98 |
|
|
|
99 |
# Reshape the data blob |
|
|
100 |
self.net.blobs['data'].reshape(1, *inp.shape) |
|
|
101 |
self.net.blobs['data'].data[...] = inp |
|
|
102 |
|
|
|
103 |
# Passes the input data through the network to compute the output |
|
|
104 |
self.net.forward() |
|
|
105 |
|
|
|
106 |
# Loops through each neuron in the first convolution layer and saves the images in that neuron |
|
|
107 |
for i in range(30): |
|
|
108 |
cv2.imwrite(self.confs["Settings"]["Classifier"]["Data"]["dir"] + self.confs["Settings"]["Classifier"]["Info"]["outDir"] + 'conv1/out_' + str(i) + '.jpg', |
|
|
109 |
255 * self.net.blobs['conv1'].data[0,i]) |
|
|
110 |
|
|
|
111 |
# Loops through each neuron in the second convolution layer and saves the images in that neuron |
|
|
112 |
for i in range(30): |
|
|
113 |
cv2.imwrite(self.confs["Settings"]["Classifier"]["Data"]["dir"] + self.confs["Settings"]["Classifier"]["Info"]["outDir"] + 'conv2/out_' + str(i) + '.jpg', |
|
|
114 |
255 * self.net.blobs['conv2'].data[0,i]) |
|
|
115 |
|
|
|
116 |
self.Helpers.logMessage(self.logFile, |
|
|
117 |
"allCNN", |
|
|
118 |
"Output Images", |
|
|
119 |
"Output images written to " + self.confs["Settings"]["Classifier"]["Data"]["dir"] + self.confs["Settings"]["Classifier"]["Info"]["outDir"]) |
|
|
120 |
|
|
|
121 |
def saveCaffeNet(self): |
|
|
122 |
|
|
|
123 |
""" |
|
|
124 |
Saves our Caffe network. |
|
|
125 |
""" |
|
|
126 |
|
|
|
127 |
self.net.save(self.confs["Settings"]["Classifier"]["Model"]["file"]) |
|
|
128 |
|
|
|
129 |
self.Helpers.logMessage(self.logFile, |
|
|
130 |
"allCNN", |
|
|
131 |
"Status", |
|
|
132 |
"Caffe net saved") |
|
|
133 |
|
|
|
134 |
allCNN = allCNN() |
|
|
135 |
|
|
|
136 |
def main(argv): |
|
|
137 |
|
|
|
138 |
if(len(argv) < 1): |
|
|
139 |
|
|
|
140 |
""" |
|
|
141 |
Incorrect arguments size. |
|
|
142 |
""" |
|
|
143 |
|
|
|
144 |
allCNN.Helpers.logMessage(allCNN.logFile, |
|
|
145 |
"allCNN", |
|
|
146 |
"Arguments", |
|
|
147 |
"Please provide NetworkInfo or Outputs argument") |
|
|
148 |
|
|
|
149 |
elif argv[0] == "NetworkInfo": |
|
|
150 |
|
|
|
151 |
""" |
|
|
152 |
Provides information about our Caffe network. |
|
|
153 |
""" |
|
|
154 |
|
|
|
155 |
allCNN.loadCaffeNet() |
|
|
156 |
allCNN.printDetails() |
|
|
157 |
|
|
|
158 |
elif argv[0] == "Outputs": |
|
|
159 |
|
|
|
160 |
""" |
|
|
161 |
Plots the outputs of each neuron as images. |
|
|
162 |
""" |
|
|
163 |
|
|
|
164 |
allCNN.loadCaffeNet() |
|
|
165 |
allCNN.writeOutputImages(allCNN.confs["Settings"]["Classifier"]["Data"]["dir"] + allCNN.confs["Settings"]["Classifier"]["Data"]["dirTest"] + allCNN.confs["Settings"]["Classifier"]["Info"]["testImage"]) |
|
|
166 |
|
|
|
167 |
elif argv[0] == "Save": |
|
|
168 |
|
|
|
169 |
""" |
|
|
170 |
Saves our Caffe network. |
|
|
171 |
""" |
|
|
172 |
|
|
|
173 |
allCNN.loadCaffeNet() |
|
|
174 |
allCNN.saveCaffeNet() |
|
|
175 |
|
|
|
176 |
if __name__ == "__main__": |
|
|
177 |
main(sys.argv[1:]) |