-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodule_complex.py
109 lines (99 loc) · 5.23 KB
/
module_complex.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
import keras
import keras.backend as K
import keras.layers as KL
import keras.engine as KE
import keras.models as KM
# VGG
def c3t2mp(in_layer, name, idx, filters=3, act='relu'):
x=cvac(in_layer,name+'_2',idx,filters,act)
x=cvac(x,name+'_1',idx,filters,act)
return mp(x,name,idx=idx,fs=None,act=None,size=2,stride=2)
def c3t3mp(in_layer, name, idx, filters=3, act='relu'):
x=cvac(in_layer,name+'_3',idx,filters,act)
x=cvac(x,name+'_2',idx,filters,act)
x=cvac(x,name+'_1',idx,filters,act)
return mp(x,name,idx=idx,fs=None,act=None,size=2,stride=2)
# Resnet # https://github.com/fchollet/deep-learning-models/blob/master/resnet50.py
# rn: ResNet https://arxiv.org/pdf/1512.03385.pdf 224x224
# first layer f64, k7x7, s2x2 -> maxpool k3x3, s2x2
# (64,3x3->64,3x3) repeat 2~6 or (64,1x1->64,3x3->256,1x1) repeat 3~8 times, double filters
from b2_net_multi import BaseNetM
from module import cv, cvac, cvbn, cvbnac, adac, mp
def c7m3d4(in_layer, name, idx, filters, act, batch_norm=False): #pre-process conv+maxpool size down 4x
ca_f=cvbnac if batch_norm else cvac
x=ca_f(in_layer,name+'_c7d2',idx=idx,fs=filters,act=act,size=7,stride=2)
x=mp(x,name+'_m3d2',idx=idx,fs=None,act=None,size=3,stride=2)
return x
def rn131c(in_layer, name, idx, filters, act, batch_norm=False, stride=2, dilate=1):
ca_f,c_f=(cvbnac,cvbn) if batch_norm else (cvac,cv)
x=ca_f(in_layer, name+'_3', idx, filters[0], act, size=1, stride=stride)
x=ca_f(x, name+'_2', idx, filters[1], act, size=3, dilation=dilate)
x=c_f(x, name+'_1', idx, filters[2], act, size=1)
y=c_f(in_layer, name+'_s', idx, filters[2], act, size=1, stride=stride)
return adac(x,y,name,idx,filters,act)
def rn131i(in_layer, name, idx, filters, act, batch_norm=False, stride=1, dilate=1):
ca_f,c_f=(cvbnac,cvbn) if batch_norm else (cvac,cv)
x=ca_f(in_layer, name+'_3', idx, filters[0], act, size=1, stride=stride)
x=ca_f(x, name+'_2', idx, filters[1], act, size=3, dilation=dilate)
x=c_f(x, name+'_1', idx, filters[2], act, size=1, stride=stride)
return adac(x,in_layer,name,idx,filters,act)
def rn131r(in_layer, name, repeat, filters, act, batch_norm=False,initial_stride=2):
filters=filters if isinstance(filters, list) else [filters, filters, filters*4] if isinstance(filters, int) else [64,64,256]
x=in_layer
for r in range(repeat):
if r==0:
x=rn131c(x,name+'_c',r,filters,act,batch_norm,initial_stride)
else:
x=rn131i(x,'%s_i%d'%(name,r),r,filters,act,batch_norm)
return x
def resnet(input_image, repeats, filters, act='relu', batch_norm=False):
# c1=x=c7m3d4(input_image,'res1',0,filters[0],act,batch_norm) # downsample twice
# c2=x=rn131r(x,'res2',repeats[1],filters[1],act,batch_norm,initial_stride=1) # override stride 2->1, same dim as prev maxpool
c1=x=rn131r(input_image,'res1',repeats[0],filters[0],act,batch_norm) # downsample twice
c2=x=rn131r(x,'res2',repeats[1],filters[1],act,batch_norm) # override stride 2->1, same dim as prev maxpool
c3=x=rn131r(x,'res3',repeats[2],filters[2],act,batch_norm) # downconv once initially
c4=x=rn131r(x,'res4',repeats[3],filters[3],act,batch_norm) # downconv once initially
c5=rn131r(x,'res5',repeats[4],filters[4],act,batch_norm) # downconv once initially
return [c1, c2, c3, c4, c5]
def resnet_50(input_image):
return resnet(input_image, repeats=[1,3,4,6,3], filters=[64,64,128,256,512])
def resnet_101(input_image):
return resnet(input_image, repeats=[1,3,4,23,3], filters=[64,64,128,256,512])
def resnet_152(input_image):
return resnet(input_image, repeats=[1,3,8,36,3], filters=[64,64,128,256,512])
class MRCNN_Res_50(BaseNetM):
def __init__(self,**kwargs):
super(MRCNN_Res_50,self).__init__(backbone=resnet_50,**kwargs)
# dn: Dense Net https://arxiv.org/pdf/1608.06993.pdf 224x224
# first layer f64, k7x7, s2x2 -> maxpool k3x3, s2x2
# (12,1x1->12,3x3) repeat 6, 12, 24,... increase repetition/density per block. nb-ac-conv suggested.
def dn13(in_layer, name, idx, filters, act):
x=cvac(in_layer, name+'_2', idx, filters, act, size=1)
x=cv(x, name+'_1', idx, filters, act, size=3)
y=cv(in_layer, name+'_s', idx, filters, act, size=1)
return adac(x,y,name,idx,filters,act)
def dn13n(in_layer, name, idx, filters, act):
x=cvbnac(in_layer, name+'_2', idx, filters, act, size=1)
x=cvbnac(x, name+'_1', idx, filters, act, size=3)
y=cvbn(in_layer, name+'_s', idx, filters, act, size=1)
return adac(x,y,name,idx,filters,act)
def dn13r(in_layer, name, idx, filters, act):
x, rep=in_layer, 6**(idx+1)
for i in range(rep-1, -1, -1):
x=dn13(x, name+str(i)[:i], idx, filters, act)
return x
def dn13nr(in_layer, name, idx, filters, act):
x, rep=in_layer, 6**(idx+1)
for i in range(rep-1, -1, -1):
x=dn13n(x, name+str(i)[:i], idx, filters, act)
return x
def dn13r1(in_layer, name, idx, filters, act):
x, rep=in_layer, 6**(idx+1)
for i in range(rep-1, -1, -1):
x=dn13(x, name+str(i)[:i+1], idx, filters, act)
return cvac(x, name, filters, act, size=1)
def dn13nr1(in_layer, name, idx, filters, act):
x, rep=in_layer, 6**(idx+1)
for i in range(rep-1, -1, -1):
x=dn13n(x, name+str(i)[:i+1], idx, filters, act)
return cvbnac(x, name, filters, act, size=1)