|
a |
|
b/src/LiviaNet/LiviaSoftmax.py |
|
|
1 |
""" |
|
|
2 |
Copyright (c) 2016, Jose Dolz .All rights reserved. |
|
|
3 |
|
|
|
4 |
Redistribution and use in source and binary forms, with or without modification, |
|
|
5 |
are permitted provided that the following conditions are met: |
|
|
6 |
|
|
|
7 |
1. Redistributions of source code must retain the above copyright notice, |
|
|
8 |
this list of conditions and the following disclaimer. |
|
|
9 |
2. Redistributions in binary form must reproduce the above copyright notice, |
|
|
10 |
this list of conditions and the following disclaimer in the documentation |
|
|
11 |
and/or other materials provided with the distribution. |
|
|
12 |
|
|
|
13 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
|
|
14 |
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
|
|
15 |
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
|
|
16 |
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
|
|
17 |
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
|
|
18 |
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
|
|
19 |
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
|
|
20 |
OTHER DEALINGS IN THE SOFTWARE. |
|
|
21 |
|
|
|
22 |
Jose Dolz. Dec, 2016. |
|
|
23 |
email: jose.dolz.upv@gmail.com |
|
|
24 |
LIVIA Department, ETS, Montreal. |
|
|
25 |
""" |
|
|
26 |
|
|
|
27 |
import numpy as np |
|
|
28 |
import theano |
|
|
29 |
import theano.tensor as T |
|
|
30 |
from theano.tensor.nnet import conv2d |
|
|
31 |
import theano.tensor.nnet.conv3d2d |
|
|
32 |
|
|
|
33 |
from LiviaNet3DConvLayer import LiviaNet3DConvLayer |
|
|
34 |
from Modules.General.Utils import initializeWeights |
|
|
35 |
from Modules.NeuralNetwork.ActivationFunctions import * |
|
|
36 |
from Modules.NeuralNetwork.layerOperations import * |
|
|
37 |
|
|
|
38 |
class LiviaSoftmax(LiviaNet3DConvLayer): |
|
|
39 |
""" Final Classification layer with Softmax """ |
|
|
40 |
|
|
|
41 |
def __init__(self, |
|
|
42 |
rng, |
|
|
43 |
layerID, |
|
|
44 |
inputSample_Train, |
|
|
45 |
inputSample_Test, |
|
|
46 |
inputToLayerShapeTrain, |
|
|
47 |
inputToLayerShapeTest, |
|
|
48 |
filterShape, |
|
|
49 |
applyBatchNorm, |
|
|
50 |
applyBatchNormNumberEpochs, |
|
|
51 |
maxPoolingParameters, |
|
|
52 |
weights_initialization, |
|
|
53 |
weights, |
|
|
54 |
activationType=0, |
|
|
55 |
dropoutRate=0.0, |
|
|
56 |
softmaxTemperature = 1.0) : |
|
|
57 |
|
|
|
58 |
LiviaNet3DConvLayer.__init__(self, |
|
|
59 |
rng, |
|
|
60 |
layerID, |
|
|
61 |
inputSample_Train, |
|
|
62 |
inputSample_Test, |
|
|
63 |
inputToLayerShapeTrain, |
|
|
64 |
inputToLayerShapeTest, |
|
|
65 |
filterShape, |
|
|
66 |
applyBatchNorm, |
|
|
67 |
applyBatchNormNumberEpochs, |
|
|
68 |
maxPoolingParameters, |
|
|
69 |
weights_initialization, |
|
|
70 |
weights, |
|
|
71 |
activationType, |
|
|
72 |
dropoutRate) |
|
|
73 |
|
|
|
74 |
self._numberOfOutputClasses = None |
|
|
75 |
self._bClassLayer = None |
|
|
76 |
self._softmaxTemperature = None |
|
|
77 |
|
|
|
78 |
self._numberOfOutputClasses = filterShape[0] |
|
|
79 |
self._softmaxTemperature = softmaxTemperature |
|
|
80 |
|
|
|
81 |
# Define outputs |
|
|
82 |
outputOfConvTrain = self.outputTrain |
|
|
83 |
outputOfConvTest = self.outputTest |
|
|
84 |
|
|
|
85 |
# define outputs shapes |
|
|
86 |
outputOfConvShapeTrain = self.outputShapeTrain |
|
|
87 |
outputOfConvShapeTest = self.outputShapeTest |
|
|
88 |
|
|
|
89 |
|
|
|
90 |
# Add bias before applying the softmax |
|
|
91 |
b_values = np.zeros( (self._numberOfFeatureMaps), dtype = 'float32') |
|
|
92 |
self._bClassLayer = theano.shared(value=b_values, borrow=True) |
|
|
93 |
|
|
|
94 |
inputToSoftmaxTrain = applyBiasToFeatureMaps( self._bClassLayer, outputOfConvTrain ) |
|
|
95 |
inputToSoftmaxTest = applyBiasToFeatureMaps( self._bClassLayer, outputOfConvTest ) |
|
|
96 |
|
|
|
97 |
self.params = self.params + [self._bClassLayer] |
|
|
98 |
|
|
|
99 |
# ============ Apply Softmax ============== |
|
|
100 |
# Training samples |
|
|
101 |
( self.p_y_given_x_train, self.y_pred_train ) = applySoftMax(inputToSoftmaxTrain, |
|
|
102 |
outputOfConvShapeTrain, |
|
|
103 |
self._numberOfOutputClasses, |
|
|
104 |
softmaxTemperature) |
|
|
105 |
|
|
|
106 |
# Testing samples |
|
|
107 |
( self.p_y_given_x_test, self.y_pred_test ) = applySoftMax(inputToSoftmaxTest, |
|
|
108 |
outputOfConvShapeTest, |
|
|
109 |
self._numberOfOutputClasses, |
|
|
110 |
softmaxTemperature) |
|
|
111 |
|
|
|
112 |
|
|
|
113 |
def negativeLogLikelihoodWeighted(self, y, weightPerClass): |
|
|
114 |
#Weighting the cost of the different classes in the cost-function, in order to counter class imbalance. |
|
|
115 |
e1 = np.finfo(np.float32).tiny |
|
|
116 |
addTinyProbMatrix = T.lt(self.p_y_given_x_train, 4*e1) * e1 |
|
|
117 |
|
|
|
118 |
weights = weightPerClass.dimshuffle('x', 0, 'x', 'x', 'x') |
|
|
119 |
log_p_y_given_x_train = T.log(self.p_y_given_x_train + addTinyProbMatrix) |
|
|
120 |
weighted_log_probs = log_p_y_given_x_train * weights |
|
|
121 |
|
|
|
122 |
wShape = weighted_log_probs.shape |
|
|
123 |
|
|
|
124 |
# Re-arrange |
|
|
125 |
idx0 = T.arange( wShape[0] ).dimshuffle( 0, 'x','x','x') |
|
|
126 |
idx2 = T.arange( wShape[2] ).dimshuffle('x', 0, 'x','x') |
|
|
127 |
idx3 = T.arange( wShape[3] ).dimshuffle('x','x', 0, 'x') |
|
|
128 |
idx4 = T.arange( wShape[4] ).dimshuffle('x','x','x', 0) |
|
|
129 |
|
|
|
130 |
return -T.mean( weighted_log_probs[ idx0, y, idx2, idx3, idx4] ) |
|
|
131 |
|
|
|
132 |
|
|
|
133 |
def predictionProbabilities(self) : |
|
|
134 |
return self.p_y_given_x_test |