File size: 5,721 Bytes
d86998c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Written by Dr Daniel Buscombe, Marda Science LLC
# for the SandSnap Program
#
# MIT License
#
# Copyright (c) 2020-2021, Marda Science LLC
#
# 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.


##> Release v1.4 (Aug 2021)

###===================================================
# import libraries
from sedinet_utils import *

###===================================================
def conv_block2(inp, filters=32, bn=True, pool=True, drop=True):
   """
   This function generates a SediNet convolutional block
   """
   # _ = Conv2D(filters=filters, kernel_size=3, activation='relu',
   #            kernel_initializer='he_uniform')(inp)

   #relu creating dead neurons?
   _ = SeparableConv2D(filters=filters, kernel_size=3, activation='relu')(inp) #'relu' #kernel_initializer='he_uniform'
   if bn:
       _ = BatchNormalization()(_)
   if pool:
       _ = MaxPool2D()(_)
   if drop:
       _ = Dropout(0.2)(_)
   return _

###===================================================
def make_cat_sedinet(ID_MAP, dropout):
    """
    This function creates an implementation of SediNet for estimating
	sediment category
    """

    base = BASE_CAT ##30

    input_layer = Input(shape=(IM_HEIGHT, IM_WIDTH, 3))
    _ = conv_block2(input_layer, filters=base, bn=False, pool=False, drop=False) #x #
    _ = conv_block2(_, filters=base*2, bn=False, pool=True,drop=False)
    _ = conv_block2(_, filters=base*3, bn=False, pool=True,drop=False)
    _ = conv_block2(_, filters=base*4, bn=False, pool=True,drop=False)

    bottleneck = GlobalMaxPool2D()(_)
    bottleneck = Dropout(dropout)(bottleneck)

     # for class prediction
    _ = Dense(units=CAT_DENSE_UNITS, activation='relu')(bottleneck)  ##128
    output = Dense(units=len(ID_MAP), activation='softmax', name='output')(_)

    model = Model(inputs=input_layer, outputs=[output])

    OPT = tf.keras.optimizers.Adam(learning_rate=MAX_LR)

    if CAT_LOSS == 'focal':
       model.compile(optimizer=OPT,
                  loss={'output': tfa.losses.SigmoidFocalCrossEntropy() },
                  metrics={'output': 'accuracy'})
    else:
       model.compile(optimizer=OPT, #'adam',
                  loss={'output': CAT_LOSS}, #'categorical_crossentropy'
                  metrics={'output': 'accuracy'})


    print("==========================================")
    print('[INFORMATION] Model summary:')
    model.summary()
    return model


###===================================================
def make_sedinet_siso_simo(vars, greyscale, dropout):
    """
    This function creates an implementation of SediNet for estimating
	sediment metric on a continuous scale
    """

    base = BASE_CONT ##30 ## suggested range = 20 -- 40
    if greyscale==True:
       input_layer = Input(shape=(IM_HEIGHT, IM_WIDTH, 1))
    else:
       input_layer = Input(shape=(IM_HEIGHT, IM_WIDTH, 3))

    _ = conv_block2(input_layer, filters=base, bn=False, pool=False, drop=False) #x #
    _ = conv_block2(_, filters=base*2, bn=False, pool=True,drop=False)
    _ = conv_block2(_, filters=base*3, bn=False, pool=True,drop=False)
    _ = conv_block2(_, filters=base*4, bn=False, pool=True,drop=False)
    _ = conv_block2(_, filters=base*5, bn=False, pool=True,drop=False)

    if not SHALLOW:
       _ = conv_block2(_, filters=base*6, bn=False, pool=True,drop=False)
       _ = conv_block2(_, filters=base*7, bn=False, pool=True,drop=False)
       _ = conv_block2(_, filters=base*8, bn=False, pool=True,drop=False)
       _ = conv_block2(_, filters=base*9, bn=False, pool=True,drop=False)

    _ = BatchNormalization(axis=-1)(_)
    bottleneck = GlobalMaxPool2D()(_)
    bottleneck = Dropout(dropout)(bottleneck)

    units = CONT_DENSE_UNITS ## suggested range 512 -- 1024
    _ = Dense(units=units, activation='relu')(bottleneck) #'relu'

    ##would it be better to predict the full vector directly instread of one by one?
    outputs = []
    for var in vars:
       outputs.append(Dense(units=1, activation='linear', name=var+'_output')(_) ) #relu

    if CONT_LOSS == 'pinball':
       loss = dict(zip([k+"_output" for k in vars], [tfa.losses.PinballLoss(tau=.5) for k in vars]))
    else: ## 'mse'
       loss = dict(zip([k+"_output" for k in vars], ['mse' for k in vars])) #loss = tf.keras.losses.MeanSquaredError(reduction=tf.keras.losses.Reduction.NONE)  # Sum of squared error

    metrics = dict(zip([k+"_output" for k in vars], ['mae' for k in vars]))

    OPT = tf.keras.optimizers.Adam(learning_rate=MAX_LR)

    model = Model(inputs=input_layer, outputs=outputs)
    model.compile(optimizer=OPT,loss=loss, metrics=metrics)
    #print("==========================================")
    #print('[INFORMATION] Model summary:')
    #model.summary()
    return model