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