forked from weningerleon/TextileDefectDetection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Softmax2D.py
54 lines (42 loc) · 1.75 KB
/
Softmax2D.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
from keras import backend as K
from keras import activations
from keras.engine.topology import Layer
from keras.layers.embeddings import Embedding
import numpy as np
class Softmax2D(Layer):
'''Applies an activation function to an output.
# Arguments
activation: name of activation function to use
(see: [activations](../activations.md)),
or alternatively, a Theano or TensorFlow operation.
# Input shape
Arbitrary. Use the keyword argument `input_shape`
(tuple of integers, does not include the samples axis)
when using this layer as the first layer in a model.
# Output shape
Same shape as input.
'''
def __softmax2d(self, x):
if K.ndim(x) == 4: ### THIS IS ADDED TO NORMAL SOFTMAX
e = K.exp(x - K.max(x, axis=1, keepdims=True))
s = K.sum(e, axis=1, keepdims=True)
return e / s
else:
raise ValueError('Cannot apply softmax2d to a tensor '
'that is not 4D. '
'Here, ndim=' + str(K.ndim(x)))
def __init__(self, trainable=False, activation=None, **kwargs):
self.supports_masking = True
super(Softmax2D, self).__init__(**kwargs)
def call(self, x, mask=None):
return self.__softmax2d(x)
def get_config(self):
config = {'activation': 'Softmax2D'}
base_config = super(Softmax2D, self).get_config()
return dict(list(base_config.items()) + list(config.items()))
def categorical_accuracy_fcn(y_true, y_pred):
'''Calculates the mean accuracy rate across all predictions for
multiclass classification problems.
'''
return K.mean(K.equal(K.argmax(y_true, axis=1),
K.argmax(y_pred, axis=1)))