Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
MLAir
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
esde
machine-learning
MLAir
Commits
72bc0b23
Commit
72bc0b23
authored
5 years ago
by
Felix Kleinert
Browse files
Options
Downloads
Patches
Plain Diff
include call of padding layers within an inception block
#62
parent
60c5244a
No related branches found
No related tags found
2 merge requests
!59
Develop
,
!53
Felix issue062 apply advanced pooling in inception block feat
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/model_modules/inception_model.py
+62
-47
62 additions, 47 deletions
src/model_modules/inception_model.py
with
62 additions
and
47 deletions
src/model_modules/inception_model.py
+
62
−
47
View file @
72bc0b23
...
@@ -54,6 +54,7 @@ class InceptionModelBase:
...
@@ -54,6 +54,7 @@ class InceptionModelBase:
regularizer
=
kwargs
.
get
(
'
regularizer
'
,
keras
.
regularizers
.
l2
(
0.01
))
regularizer
=
kwargs
.
get
(
'
regularizer
'
,
keras
.
regularizers
.
l2
(
0.01
))
bn_settings
=
kwargs
.
get
(
'
bn_settings
'
,
{})
bn_settings
=
kwargs
.
get
(
'
bn_settings
'
,
{})
act_settings
=
kwargs
.
get
(
'
act_settings
'
,
{})
act_settings
=
kwargs
.
get
(
'
act_settings
'
,
{})
padding
=
kwargs
.
get
(
'
padding
'
,
'
ZeroPad2D
'
)
logging
.
debug
(
f
'
Inception Block with activation:
{
activation
}
'
)
logging
.
debug
(
f
'
Inception Block with activation:
{
activation
}
'
)
block_name
=
f
'
Block_
{
self
.
number_of_blocks
}{
self
.
block_part_name
()
}
_
{
tower_kernel
[
0
]
}
x
{
tower_kernel
[
1
]
}
'
block_name
=
f
'
Block_
{
self
.
number_of_blocks
}{
self
.
block_part_name
()
}
_
{
tower_kernel
[
0
]
}
x
{
tower_kernel
[
1
]
}
'
...
@@ -74,7 +75,7 @@ class InceptionModelBase:
...
@@ -74,7 +75,7 @@ class InceptionModelBase:
name
=
f
'
Block_
{
self
.
number_of_blocks
}{
self
.
block_part_name
()
}
_1x1
'
)(
input_x
)
name
=
f
'
Block_
{
self
.
number_of_blocks
}{
self
.
block_part_name
()
}
_1x1
'
)(
input_x
)
tower
=
self
.
act
(
tower
,
activation
,
**
act_settings
)
tower
=
self
.
act
(
tower
,
activation
,
**
act_settings
)
tower
=
self
.
padding_layer
(
'
SymPad2D
'
)(
padding
=
padding_size
,
tower
=
self
.
padding_layer
(
padding
)(
padding
=
padding_size
,
name
=
f
'
Block_
{
self
.
number_of_blocks
}{
self
.
block_part_name
()
}
_Pad
'
name
=
f
'
Block_
{
self
.
number_of_blocks
}{
self
.
block_part_name
()
}
_Pad
'
)(
tower
)
)(
tower
)
...
@@ -116,19 +117,19 @@ class InceptionModelBase:
...
@@ -116,19 +117,19 @@ class InceptionModelBase:
}
}
if
isinstance
(
padding
,
str
):
if
isinstance
(
padding
,
str
):
try
:
try
:
pad2
D
=
allowed_paddings
[
padding
]
pad2
d
=
allowed_paddings
[
padding
]
except
KeyError
as
einfo
:
except
KeyError
as
einfo
:
raise
NotImplementedError
(
raise
NotImplementedError
(
f
"
`
{
einfo
}
'
is not implemented as padding.
"
f
"
`
{
einfo
}
'
is not implemented as padding.
"
"
Use one of those: i) `RefPad2D
'
, ii) `SymPad2D
'
, iii) `ZeroPad2D
'"
)
"
Use one of those: i) `RefPad2D
'
, ii) `SymPad2D
'
, iii) `ZeroPad2D
'"
)
else
:
else
:
if
padding
in
allowed_paddings
.
values
():
if
padding
in
allowed_paddings
.
values
():
pad2
D
=
padding
pad2
d
=
padding
else
:
else
:
raise
Valu
eError
(
f
"
`
{
padding
.
__name__
}
'
is not a valid padding
padding
.
"
raise
Typ
eError
(
f
"
`
{
padding
.
__name__
}
'
is not a valid padding
layer type
.
"
"
Use one of those:
"
"
Use one of those:
"
"
i) ReflectionPadding2D, ii)
`
SymmetricPadding2D
'
, iii)
`
ZeroPadding2D
'
"
)
"
i) ReflectionPadding2D, ii) SymmetricPadding2D, iii) ZeroPadding2D
"
)
return
pad2
D
return
pad2
d
def
create_pool_tower
(
self
,
input_x
,
pool_kernel
,
tower_filter
,
activation
=
'
relu
'
,
max_pooling
=
True
,
**
kwargs
):
def
create_pool_tower
(
self
,
input_x
,
pool_kernel
,
tower_filter
,
activation
=
'
relu
'
,
max_pooling
=
True
,
**
kwargs
):
"""
"""
...
@@ -143,6 +144,7 @@ class InceptionModelBase:
...
@@ -143,6 +144,7 @@ class InceptionModelBase:
self
.
part_of_block
+=
1
self
.
part_of_block
+=
1
self
.
act_number
=
1
self
.
act_number
=
1
act_settings
=
kwargs
.
get
(
'
act_settings
'
,
{})
act_settings
=
kwargs
.
get
(
'
act_settings
'
,
{})
padding
=
kwargs
.
get
(
'
padding
'
,
'
ZeroPad2D
'
)
padding_size
=
PadUtils
.
get_padding_for_same
(
kernel_size
=
pool_kernel
)
padding_size
=
PadUtils
.
get_padding_for_same
(
kernel_size
=
pool_kernel
)
# pooling block
# pooling block
...
@@ -154,11 +156,11 @@ class InceptionModelBase:
...
@@ -154,11 +156,11 @@ class InceptionModelBase:
block_type
=
"
AvgPool
"
block_type
=
"
AvgPool
"
pooling
=
layers
.
AveragePooling2D
pooling
=
layers
.
AveragePooling2D
tower
=
self
.
padding_layer
(
'
SymPad2D
'
)(
padding
=
padding_size
,
name
=
block_name
+
'
Pad
'
)(
input_x
)
tower
=
self
.
padding_layer
(
padding
)(
padding
=
padding_size
,
name
=
block_name
+
'
Pad
'
)(
input_x
)
tower
=
pooling
(
pool_kernel
,
strides
=
(
1
,
1
),
padding
=
'
valid
'
,
name
=
block_name
+
block_type
)(
tower
)
tower
=
pooling
(
pool_kernel
,
strides
=
(
1
,
1
),
padding
=
'
valid
'
,
name
=
block_name
+
block_type
)(
tower
)
# convolution block
# convolution block
tower
=
layers
.
Conv2D
(
tower_filter
,
(
1
,
1
),
padding
=
'
same
'
,
name
=
block_name
+
"
1x1
"
)(
tower
)
tower
=
layers
.
Conv2D
(
tower_filter
,
(
1
,
1
),
padding
=
'
valid
'
,
name
=
block_name
+
"
1x1
"
)(
tower
)
tower
=
self
.
act
(
tower
,
activation
,
**
act_settings
)
tower
=
self
.
act
(
tower
,
activation
,
**
act_settings
)
return
tower
return
tower
...
@@ -170,16 +172,22 @@ class InceptionModelBase:
...
@@ -170,16 +172,22 @@ class InceptionModelBase:
:param tower_conv_parts: dict containing settings for parts of inception block; Example:
:param tower_conv_parts: dict containing settings for parts of inception block; Example:
tower_conv_parts = {
'
tower_1
'
: {
'
reduction_filter
'
: 32,
tower_conv_parts = {
'
tower_1
'
: {
'
reduction_filter
'
: 32,
'
tower_filter
'
: 64,
'
tower_filter
'
: 64,
'
tower_kernel
'
: (3, 1)},
'
tower_kernel
'
: (3, 1),
'
activation
'
:
'
relu
'
,
'
padding
'
:
'
SymPad2D
'
}
'
tower_2
'
: {
'
reduction_filter
'
: 32,
'
tower_2
'
: {
'
reduction_filter
'
: 32,
'
tower_filter
'
: 64,
'
tower_filter
'
: 64,
'
tower_kernel
'
: (5, 1)},
'
tower_kernel
'
: (5, 1),
'
activation
'
: LeakyReLU,
'
padding
'
: keras.layers.ZeroPadding2D}
'
tower_3
'
: {
'
reduction_filter
'
: 32,
'
tower_3
'
: {
'
reduction_filter
'
: 32,
'
tower_filter
'
: 64,
'
tower_filter
'
: 64,
'
tower_kernel
'
: (1, 1)},
'
tower_kernel
'
: (1, 1),
'
activation
'
: ELU,
'
padding
'
: src.model_modules.advanced_paddings.ReflectionPadding2D}
}
}
:param tower_pool_parts: dict containing settings for pool part of inception block; Example:
:param tower_pool_parts: dict containing settings for pool part of inception block; Example:
tower_pool_parts = {
'
pool_kernel
'
: (3, 1),
'
tower_filter
'
: 64}
tower_pool_parts = {
'
pool_kernel
'
: (3, 1),
'
tower_filter
'
: 64
,
'
padding
'
:
'
RefPad2D
'
}
:return:
:return:
"""
"""
self
.
number_of_blocks
+=
1
self
.
number_of_blocks
+=
1
...
@@ -201,41 +209,41 @@ class InceptionModelBase:
...
@@ -201,41 +209,41 @@ class InceptionModelBase:
return
block
return
block
if
__name__
==
'
__main__
'
:
#
if __name__ == '__main__':
from
keras.models
import
Model
#
from keras.models import Model
from
keras.layers
import
Conv2D
,
Flatten
,
Dense
,
Input
#
from keras.layers import Conv2D, Flatten, Dense, Input
import
numpy
as
np
#
import numpy as np
#
#
kernel_1
=
(
3
,
3
)
#
kernel_1 = (3, 3)
kernel_2
=
(
5
,
5
)
#
kernel_2 = (5, 5)
x
=
np
.
array
(
range
(
2000
)).
reshape
(
-
1
,
10
,
10
,
1
)
#
x = np.array(range(2000)).reshape(-1, 10, 10, 1)
y
=
x
.
mean
(
axis
=
(
1
,
2
))
#
y = x.mean(axis=(1, 2))
#
x_input
=
Input
(
shape
=
x
.
shape
[
1
:])
#
x_input = Input(shape=x.shape[1:])
pad1
=
PadUtils
.
get_padding_for_same
(
kernel_size
=
kernel_1
)
#
pad1 = PadUtils.get_padding_for_same(kernel_size=kernel_1)
x_out
=
InceptionModelBase
.
padding_layer
(
'
RefPad2D
'
)(
padding
=
pad1
,
name
=
"
RefPAD1
"
)(
x_input
)
#
x_out = InceptionModelBase.padding_layer('RefPad2D')(padding=pad1, name="RefPAD1")(x_input)
# x_out = ReflectionPadding2D(padding=pad1, name="RefPAD")(x_input)
#
# x_out = ReflectionPadding2D(padding=pad1, name="RefPAD")(x_input)
x_out
=
Conv2D
(
5
,
kernel_size
=
kernel_1
,
activation
=
'
relu
'
)(
x_out
)
#
x_out = Conv2D(5, kernel_size=kernel_1, activation='relu')(x_out)
#
pad2
=
PadUtils
.
get_padding_for_same
(
kernel_size
=
kernel_2
)
#
pad2 = PadUtils.get_padding_for_same(kernel_size=kernel_2)
x_out
=
InceptionModelBase
.
padding_layer
(
SymmetricPadding2D
)(
padding
=
pad2
,
name
=
"
SymPAD1
"
)(
x_out
)
#
x_out = InceptionModelBase.padding_layer(SymmetricPadding2D)(padding=pad2, name="SymPAD1")(x_out)
# x_out = SymmetricPadding2D(padding=pad2, name="SymPAD")(x_out)
#
# x_out = SymmetricPadding2D(padding=pad2, name="SymPAD")(x_out)
x_out
=
Conv2D
(
2
,
kernel_size
=
kernel_2
,
activation
=
'
relu
'
)(
x_out
)
#
x_out = Conv2D(2, kernel_size=kernel_2, activation='relu')(x_out)
x_out
=
Flatten
()(
x_out
)
#
x_out = Flatten()(x_out)
x_out
=
Dense
(
1
,
activation
=
'
linear
'
)(
x_out
)
#
x_out = Dense(1, activation='linear')(x_out)
#
model
=
Model
(
inputs
=
x_input
,
outputs
=
x_out
)
#
model = Model(inputs=x_input, outputs=x_out)
model
.
compile
(
'
adam
'
,
loss
=
'
mse
'
)
#
model.compile('adam', loss='mse')
model
.
summary
()
#
model.summary()
# model.fit(x, y, epochs=10)
#
# model.fit(x, y, epochs=10)
if
__name__
==
'
__main__
'
:
if
__name__
==
'
__main__
'
:
print
(
__name__
)
print
(
__name__
)
from
keras.datasets
import
cifar10
from
keras.datasets
import
cifar10
from
keras.utils
import
np_utils
from
keras.utils
import
np_utils
from
keras.layers
import
Input
from
keras.layers
import
Input
from
keras.layers.advanced_activations
import
LeakyReLU
from
keras.layers.advanced_activations
import
LeakyReLU
,
ELU
from
keras.optimizers
import
SGD
from
keras.optimizers
import
SGD
from
keras.layers
import
Dense
,
Flatten
,
Conv2D
,
MaxPooling2D
from
keras.layers
import
Dense
,
Flatten
,
Conv2D
,
MaxPooling2D
from
keras.models
import
Model
from
keras.models
import
Model
...
@@ -244,11 +252,17 @@ if __name__ == '__main__':
...
@@ -244,11 +252,17 @@ if __name__ == '__main__':
conv_settings_dict
=
{
'
tower_1
'
:
{
'
reduction_filter
'
:
64
,
conv_settings_dict
=
{
'
tower_1
'
:
{
'
reduction_filter
'
:
64
,
'
tower_filter
'
:
64
,
'
tower_filter
'
:
64
,
'
tower_kernel
'
:
(
3
,
3
),
'
tower_kernel
'
:
(
3
,
3
),
'
activation
'
:
LeakyReLU
},
'
activation
'
:
LeakyReLU
,
},
'
tower_2
'
:
{
'
reduction_filter
'
:
64
,
'
tower_2
'
:
{
'
reduction_filter
'
:
64
,
'
tower_filter
'
:
64
,
'
tower_filter
'
:
64
,
'
tower_kernel
'
:
(
5
,
5
),
'
tower_kernel
'
:
(
5
,
5
),
'
activation
'
:
'
relu
'
}
'
activation
'
:
'
relu
'
,
'
padding
'
:
'
SymPad2D
'
},
'
tower_3
'
:
{
'
reduction_filter
'
:
64
,
'
tower_filter
'
:
64
,
'
tower_kernel
'
:
(
1
,
1
),
'
activation
'
:
ELU
,
'
padding
'
:
ReflectionPadding2D
}
}
}
pool_settings_dict
=
{
'
pool_kernel
'
:
(
3
,
3
),
pool_settings_dict
=
{
'
pool_kernel
'
:
(
3
,
3
),
'
tower_filter
'
:
64
,
'
tower_filter
'
:
64
,
...
@@ -266,7 +280,7 @@ if __name__ == '__main__':
...
@@ -266,7 +280,7 @@ if __name__ == '__main__':
# create inception net
# create inception net
inception_net
=
InceptionModelBase
()
inception_net
=
InceptionModelBase
()
output
=
inception_net
.
inception_block
(
input_img
,
conv_settings_dict
,
pool_settings_dict
)
output
=
inception_net
.
inception_block
(
input_img
,
conv_settings_dict
,
pool_settings_dict
,
batch_normalisation
=
True
)
output
=
Flatten
()(
output
)
output
=
Flatten
()(
output
)
output
=
Dense
(
10
,
activation
=
'
softmax
'
)(
output
)
output
=
Dense
(
10
,
activation
=
'
softmax
'
)(
output
)
model
=
Model
(
inputs
=
input_img
,
outputs
=
output
)
model
=
Model
(
inputs
=
input_img
,
outputs
=
output
)
...
@@ -281,5 +295,6 @@ if __name__ == '__main__':
...
@@ -281,5 +295,6 @@ if __name__ == '__main__':
print
(
X_train
.
shape
)
print
(
X_train
.
shape
)
keras
.
utils
.
plot_model
(
model
,
to_file
=
'
model.pdf
'
,
show_shapes
=
True
,
show_layer_names
=
True
)
keras
.
utils
.
plot_model
(
model
,
to_file
=
'
model.pdf
'
,
show_shapes
=
True
,
show_layer_names
=
True
)
# model.fit(X_train, y_train, epochs=epochs, validation_data=(X_test, y_test))
# model.fit(X_train, y_train, epochs=epochs, validation_data=(X_test, y_test))
print
(
'
test
'
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment