-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathclassification.py
111 lines (83 loc) · 3.79 KB
/
classification.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
# -*- coding: utf-8 -*-
"""Classification.ipynb
**
* This file is part of Hybrid CNN-LSTM for COVID-19 Severity Score Prediction paper.
*
* Written by Ankan Ghosh Dastider and Farhan Sadik.
*
* Copyright (c) by the authors under Apache-2.0 License. Some rights reserved, see LICENSE.
*/
"""
from keras.applications import ResNet152V2,DenseNet201,NASNetMobile,Xception
#from keras.applications import DenseNet121
from keras.layers.merge import concatenate
import tensorflow as tf
IMG_WIDTH = 128
IMG_HEIGHT = 128
IMG_CHANNELS = 3
#Build the model
#Branch 1
inputs = Input((IMG_HEIGHT, IMG_WIDTH, IMG_CHANNELS))
#s = Lambda(lambda x: x / 255)(inputs)
s=inputs
#Make 3 positional Arguments
c1 = Conv2D(256, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(s)
p1= MaxPool2D(pool_size=(2,2))(c1)
p1= Dropout(0.2)(p1)
mid1 = p1
c1_1= Conv2D(128, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(p1) #64,128
c2= Conv2D(128, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(s)
p2= MaxPool2D(pool_size=(2,2))(c2)
#p2= Dropout(0.5)(p2)
mid2 = p2
mid2 = Conv2D(64, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(mid2)
mid2 = Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(mid2)
mid2 = Dropout(0.2)(mid2)
P1_R = MaxPool2D(pool_size=(2,2))(mid2)
R1=concatenate([c1_1,p2])
R1.shape
#R1=Dropout(0.5)(R1) #Extra
C1_R=Conv2D(64, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(R1)
mid1 = Conv2D(128, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(mid1)
mid1 = Conv2D(64, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(mid1)
mid1 = Dropout(0.2)(mid1)
mid1_1 = concatenate([C1_R,mid1])
C11_R=Conv2D(64, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(mid1_1)
C11_R=Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(C11_R)
C11_R = MaxPool2D(pool_size=(2,2))(C11_R)
C11_R = Dropout(0.2)(C11_R)
mid2_1 = concatenate([C11_R,P1_R])
mid2_1 = Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(mid2_1)
x = Conv2D(16, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(mid2_1)
densenet = DenseNet201(weights='imagenet', include_top=False)
# input = Input(shape=(SIZE, SIZE, N_ch))
#x = Conv2D(3, (3, 3), padding='same',activation='relu')(s)
#x = Conv2D(3, (3, 3), padding='same',activation='relu')(x)
#x = Conv2D(3, (3, 3), padding='same',activation='relu')(x)
x = (Flatten())(x)
#branch 2
c_b_1=Conv2D(3, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(s)
branch_2 = densenet(c_b_1)
branch_2 = GlobalAveragePooling2D()(branch_2)
branch_2= BatchNormalization()(branch_2)
branch_2 = Dropout(0.5)(branch_2)
branch_2= Dense(256, activation='relu')(branch_2)
#concatenate model
final=concatenate([x,branch_2])
final = BatchNormalization()(final)
final = Dropout(0.2)(final)
final = Dense(1024, activation='relu')(final)
final= Dropout(0.2)(final)
final= Dense(512, activation='relu')(final)
final= Dropout(0.2)(final) #Extra
final= Dense(128, activation='relu')(final)
final= Dropout(0.5)(final) #Extra
final= Dense(64, activation='relu')(final)
final= Dropout(0.5)(final) #Extra
#multi output
output = Dense(3,activation = 'softmax', name='root')(final)
# model
model = Model(inputs,output)
optimizer = Adam(lr=0.002, beta_1=0.9, beta_2=0.999, epsilon=0.1, decay=0.0)#lr=0.002
model.compile(loss='categorical_crossentropy', optimizer=optimizer, metrics=['accuracy'])#kullback_leibler_divergence#categorical_crossentropy
model.summary()