forked from OlafenwaMoses/ImageAI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresnet50.py
More file actions
122 lines (81 loc) · 3.84 KB
/
resnet50.py
File metadata and controls
122 lines (81 loc) · 3.84 KB
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
from tensorflow.python import keras
from tensorflow.python.keras import layers
from tensorflow.python.keras.layers import Dense, Activation, Flatten, Conv2D, MaxPool2D, AvgPool2D, GlobalMaxPool2D, GlobalAvgPool2D, BatchNormalization, add, Input
from tensorflow.python.keras.models import Model
def resnet_module(input, channel_depth, strided_pool=False ):
residual_input = input
stride = 1
if(strided_pool):
stride = 2
residual_input = Conv2D(channel_depth, kernel_size=1, strides=stride, padding="same")(residual_input)
residual_input = BatchNormalization()(residual_input)
input = Conv2D(int(channel_depth/4), kernel_size=1, strides=stride, padding="same")(input)
input = BatchNormalization()(input)
input = Activation("relu")(input)
input = Conv2D(int(channel_depth / 4), kernel_size=3, strides=1, padding="same")(input)
input = BatchNormalization()(input)
input = Activation("relu")(input)
input = Conv2D(channel_depth, kernel_size=1, strides=1, padding="same")(input)
input = BatchNormalization()(input)
input = add([input, residual_input])
input = Activation("relu")(input)
return input
def resnet_first_block_first_module(input, channel_depth):
residual_input = input
stride = 1
residual_input = Conv2D(channel_depth, kernel_size=1, strides=1, padding="same")(residual_input)
residual_input = BatchNormalization()(residual_input)
input = Conv2D(int(channel_depth/4), kernel_size=1, strides=stride, padding="same")(input)
input = BatchNormalization()(input)
input = Activation("relu")(input)
input = Conv2D(int(channel_depth / 4), kernel_size=3, strides=stride, padding="same")(input)
input = BatchNormalization()(input)
input = Activation("relu")(input)
input = Conv2D(channel_depth, kernel_size=1, strides=stride, padding="same")(input)
input = BatchNormalization()(input)
input = add([input, residual_input])
input = Activation("relu")(input)
return input
def resnet_block(input, channel_depth, num_layers, strided_pool_first = False ):
for i in range(num_layers):
pool = False
if(i == 0 and strided_pool_first):
pool = True
input = resnet_module(input, channel_depth, strided_pool=pool)
return input
def ResNet50(include_top=True, non_top_pooling=None, model_input=None, num_classes=1000, weights='imagenet', model_path=""):
layers = [3,4,6,3]
channel_depths = [256, 512, 1024, 2048]
input_object = model_input
output = Conv2D(64, kernel_size=7, strides=2, padding="same")(input_object)
output = BatchNormalization()(output)
output = Activation("relu")(output)
output = MaxPool2D(pool_size=(3,3), strides=(2,2))(output)
output = resnet_first_block_first_module(output, channel_depths[0])
for i in range(4):
channel_depth = channel_depths[i]
num_layers = layers[i]
strided_pool_first = True
if(i == 0):
strided_pool_first = False
num_layers = num_layers - 1
output = resnet_block(output, channel_depth=channel_depth, num_layers=num_layers, strided_pool_first=strided_pool_first)
if(include_top):
output = GlobalAvgPool2D(name="global_avg_pooling")(output)
output = Dense(num_classes)(output)
output = Activation("softmax")(output)
else:
if (non_top_pooling == "Average"):
output = GlobalAvgPool2D()(output)
elif (non_top_pooling == "Maximum"):
output = GlobalMaxPool2D()(output)
elif (non_top_pooling == None):
pass
model = Model(inputs=input_object, outputs=output)
if(weights == "imagenet"):
weights_path = model_path
model.load_weights(weights_path)
elif (weights == "trained"):
weights_path = model_path
model.load_weights(weights_path)
return model