Skip to content

Commit fed1103

Browse files
authored
Merge pull request #1 from Shakshi3104/add
Implement MarNASNet-D and MarNASNet-Ca
2 parents af98a75 + 1c0acb6 commit fed1103

File tree

5 files changed

+162
-5
lines changed

5 files changed

+162
-5
lines changed

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
setuptools.setup(
44
name="tfmars",
5-
version="1.0.0",
5+
version="1.0.1",
66
author="Shakshi3104",
77
description="MarNASNet and famous CNN models for Sensor-based Human Activity Recognition",
88
url="https://github.com/Shakshi3104/tfmars",

src/tfmars/applications/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,5 @@
3030
"EfficientNetB0", "EfficientNetB0WithAttention",
3131
"EfficientNetLite0", "EfficientNetLite0WithAttention",
3232
"DenseNet121", "DenseNet121WithAttention",
33-
"MarNASNetC", "MarNASNetA", "MarNASNetB", "MarNASNetE"
33+
"MarNASNetC", "MarNASNetA", "MarNASNetB", "MarNASNetE", "MarNASNetD", "MarNASNetCa"
3434
]
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
from .marnasnet_c import MarNASNetC
1+
from .marnasnet_c import MarNASNetC, MarNASNetCa
22
from .marnasnet_a import MarNASNetA
33
from .marnasnet_b import MarNASNetB
44
from .marnasnet_e import MarNASNetE
5+
from .marnasnet_d import MarNASNetD
56

67
__all__ = [
78
"MarNASNetC",
89
"MarNASNetA",
910
"MarNASNetB",
10-
"MarNASNetE"
11+
"MarNASNetE",
12+
"MarNASNetD",
13+
"MarNASNetCa"
1114
]

src/tfmars/applications/marnasnet/marnasnet_c.py

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import tensorflow as tf
22
import math
33

4-
54
from .blocks import ConvBlock, SkipOperation, RegularConvBlock, MBConvBlock
65
from ..mobile_inverted_bottleneck import Top, Stem
76

@@ -26,6 +25,7 @@ def MarNASNetC(width_coefficient=1.0, depth_coefficient=1.0, depth_divisor=8,
2625
Returns
2726
-------
2827
"""
28+
2929
def round_filters(filters_, divisor=depth_divisor):
3030
"""Round number of filters based on depth multiplier."""
3131
filters_ *= width_coefficient
@@ -131,3 +131,39 @@ def round_repeats(repeats_):
131131
# Create model
132132
model = tf.keras.models.Model(inputs, outputs)
133133
return model
134+
135+
136+
# MarNASNet-Ca
137+
def MarNASNetCa(include_top=True, input_shape=(256, 3), pooling=None, classes=6, classifier_activation='softmax'):
138+
params = [
139+
{
140+
'conv_op': "MBConv",
141+
'kernel_size': 5,
142+
'skip_op': "identity",
143+
'layers': 2,
144+
'filters': 32
145+
},
146+
{
147+
'conv_op': "Conv",
148+
'kernel_size': 2,
149+
'skip_op': "identity",
150+
'layers': 4,
151+
'filters': 82
152+
},
153+
{
154+
'conv_op': "MBConv",
155+
'kernel_size': 2,
156+
'skip_op': "none",
157+
'layers': 2,
158+
'filters': 132
159+
},
160+
{
161+
'conv_op': "MBConv",
162+
'kernel_size': 5,
163+
'skip_op': "identity",
164+
'layers': 5,
165+
'filters': 192
166+
}
167+
]
168+
169+
return MarNASNetC(1.0, 1.0, 8, include_top, input_shape, pooling, classes, classifier_activation, params)
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
import tensorflow as tf
2+
import math
3+
4+
5+
from .blocks import ConvBlock, SkipOperation, RegularConvBlock, MBConvBlock
6+
from ..mobile_inverted_bottleneck import Top, Stem
7+
8+
9+
# MarNASNet-D
10+
def MarNASNetD(include_top=True, input_shape=(256, 3), pooling=None, classes=6, classifier_activation='softmax',
11+
params=None):
12+
"""
13+
14+
Parameters
15+
----------
16+
include_top
17+
input_shape
18+
pooling
19+
classes
20+
classifier_activation
21+
params
22+
23+
Returns
24+
-------
25+
26+
"""
27+
28+
# se ratio
29+
se_ratio = 0.25
30+
31+
# inputs
32+
inputs = tf.keras.layers.Input(shape=input_shape)
33+
34+
# Build stem
35+
filters_in = 32
36+
outputs = Stem(filters_in)(inputs)
37+
38+
if params is None:
39+
params = [
40+
{
41+
'conv_op': "MBConv",
42+
'kernel_size': 3,
43+
'skip_op': "none",
44+
'layers': 5,
45+
'filters': int(0.75 * 76)
46+
},
47+
{
48+
'conv_op': "MBConv",
49+
'kernel_size': 5,
50+
'skip_op': "identity",
51+
'layers': 5,
52+
'filters': int(1.25 * 88)
53+
},
54+
{
55+
'conv_op': "MBConv",
56+
'kernel_size': 2,
57+
'skip_op': "identity",
58+
'layers': 2,
59+
'filters': int(0.75 * 100)
60+
},
61+
{
62+
'conv_op': "MBConv",
63+
'kernel_size': 2,
64+
'skip_op': "none",
65+
'layers': 2,
66+
'filters': int(0.75 * 112)
67+
}
68+
]
69+
70+
for i, param in enumerate(params):
71+
block_id = i + 1
72+
73+
conv_op = ConvBlock[param["conv_op"]]
74+
skip_op = SkipOperation[param["skip_op"]]
75+
76+
repeats = param["layers"]
77+
kernel_size = param["kernel_size"]
78+
filters = param["filters"]
79+
80+
if conv_op == ConvBlock.Conv:
81+
outputs = RegularConvBlock(
82+
repeats=repeats,
83+
kernel_size=kernel_size,
84+
filters=filters,
85+
skip_op=skip_op,
86+
strides=1,
87+
se_ratio=se_ratio,
88+
block_id=block_id
89+
)(outputs)
90+
91+
elif conv_op == ConvBlock.MBConv:
92+
outputs = MBConvBlock(
93+
repeats=repeats,
94+
kernel_size=kernel_size,
95+
filters_in=filters_in,
96+
filters_out=filters,
97+
expand_ratio=1,
98+
skip_op=skip_op,
99+
strides=1 if i == 0 else 2,
100+
se_ratio=se_ratio,
101+
block_id=block_id
102+
)(outputs)
103+
104+
filters_in = filters
105+
106+
# Build top
107+
outputs = Top(1280, classes, classifier_activation=classifier_activation,
108+
include_top=include_top)(outputs)
109+
110+
if not include_top:
111+
if pooling == "avg":
112+
outputs = tf.keras.layers.GlobalAveragePooling1D(name="avg_pool")(outputs)
113+
elif pooling == "max":
114+
outputs = tf.keras.layers.GlobalMaxPooling1D(name="max_pool")(outputs)
115+
116+
# Create model
117+
model = tf.keras.models.Model(inputs, outputs)
118+
return model

0 commit comments

Comments
 (0)