Commit 4af51682 authored by Cresson Remi's avatar Cresson Remi

ENH: use Keras API for convolutions and dense layers

parent c1bee0ab
......@@ -29,35 +29,33 @@ parser.add_argument("--outdir", help="Output directory for SavedModel", required
params = parser.parse_args()
def conv2d_valid(x, kernel_size, filters, activation="relu"):
conv_op = tf.keras.layers.Conv2D(filters=filters, kernel_size=kernel_size, activation=activation)
return conv_op(x)
def my_model(x1, x2):
max_pool_2x = tf.keras.layers.MaxPool2D(pool_size=2, strides=2)
# The XS branch (input patches: 8x8x4)
conv1_x1 = tf.compat.v1.layers.conv2d(inputs=x1, filters=16, kernel_size=[5, 5], padding="valid",
activation=tf.nn.relu) # out size: 4x4x16
conv2_x1 = tf.compat.v1.layers.conv2d(inputs=conv1_x1, filters=32, kernel_size=[3, 3], padding="valid",
activation=tf.nn.relu) # out size: 2x2x32
conv3_x1 = tf.compat.v1.layers.conv2d(inputs=conv2_x1, filters=64, kernel_size=[2, 2], padding="valid",
activation=tf.nn.relu) # out size: 1x1x64
conv1_x1 = conv2d_valid(x1, filters=16, kernel_size=5) # out size: 4x4x16
conv2_x1 = conv2d_valid(conv1_x1, filters=32, kernel_size=3) # out size: 2x2x32
conv3_x1 = conv2d_valid(conv2_x1, filters=64, kernel_size=2) # out size: 1x1x64
# The PAN branch (input patches: 32x32x1)
conv1_x2 = tf.compat.v1.layers.conv2d(inputs=x2, filters=16, kernel_size=[5, 5], padding="valid",
activation=tf.nn.relu) # out size: 28x28x16
pool1_x2 = tf.compat.v1.layers.max_pooling2d(inputs=conv1_x2, pool_size=[2, 2],
strides=2) # out size: 14x14x16
conv2_x2 = tf.compat.v1.layers.conv2d(inputs=pool1_x2, filters=32, kernel_size=[5, 5], padding="valid",
activation=tf.nn.relu) # out size: 10x10x32
pool2_x2 = tf.compat.v1.layers.max_pooling2d(inputs=conv2_x2, pool_size=[2, 2],
strides=2) # out size: 5x5x32
conv3_x2 = tf.compat.v1.layers.conv2d(inputs=pool2_x2, filters=64, kernel_size=[3, 3], padding="valid",
activation=tf.nn.relu) # out size: 3x3x64
conv4_x2 = tf.compat.v1.layers.conv2d(inputs=conv3_x2, filters=64, kernel_size=[3, 3], padding="valid",
activation=tf.nn.relu) # out size: 1x1x64
conv1_x2 = conv2d_valid(x2, filters=16, kernel_size=5) # out size: 28x28x16
pool1_x2 = max_pool_2x(conv1_x2) # out size: 14x14x16
conv2_x2 = conv2d_valid(pool1_x2, filters=32, kernel_size=5) # out size: 10x10x32
pool2_x2 = max_pool_2x(conv2_x2) # out size: 5x5x32
conv3_x2 = conv2d_valid(pool2_x2, filters=64, kernel_size=3) # out size: 3x3x64
conv4_x2 = conv2d_valid(conv3_x2, filters=64, kernel_size=3) # out size: 1x1x64
# Stack features
features = tf.reshape(tf.stack([conv3_x1, conv4_x2], axis=3), shape=[-1, 128], name="features")
# 8 neurons for 8 classes
estimated = tf.compat.v1.layers.dense(inputs=features, units=params.nclasses, activation=None)
estimated_label = tf.argmax(estimated, 1, name="prediction")
# Neurons for classes
estimated = tf.keras.layers.Dense(params.nclasses)(features)
estimated_label = tf.argmax(estimated, name="prediction")
return estimated, estimated_label
......
......@@ -29,23 +29,27 @@ parser.add_argument("--outdir", help="Output directory for SavedModel", required
params = parser.parse_args()
def conv2d_valid(x, kernel_size, filters, activation="relu"):
conv_op = tf.keras.layers.Conv2D(filters=filters, kernel_size=kernel_size, activation=activation)
return conv_op(x)
def my_model(x):
max_pool_2x = tf.keras.layers.MaxPool2D(pool_size=2, strides=2)
# input patches: 16x16x4
conv1 = tf.compat.v1.layers.conv2d(inputs=x, filters=16, kernel_size=[5, 5], padding="valid",
activation=tf.nn.relu) # out size: 12x12x16
pool1 = tf.compat.v1.layers.max_pooling2d(inputs=conv1, pool_size=[2, 2], strides=2) # out: 6x6x16
conv2 = tf.compat.v1.layers.conv2d(inputs=pool1, filters=16, kernel_size=[3, 3], padding="valid",
activation=tf.nn.relu) # out size: 4x4x16
pool2 = tf.compat.v1.layers.max_pooling2d(inputs=conv2, pool_size=[2, 2], strides=2) # out: 2x2x16
conv3 = tf.compat.v1.layers.conv2d(inputs=pool2, filters=32, kernel_size=[2, 2], padding="valid",
activation=tf.nn.relu) # out size: 1x1x32
conv1 = conv2d_valid(x, filters=16, kernel_size=5) # out size: 12x12x16
pool1 = max_pool_2x(conv1) # out: 6x6x16
conv2 = conv2d_valid(pool1, filters=16, kernel_size=3) # out size: 4x4x16
pool2 = max_pool_2x(conv2) # out: 2x2x16
conv3 = conv2d_valid(pool2, filters=32, kernel_size=2) # out size: 1x1x32
# Features
features = tf.reshape(conv3, shape=[-1, 32], name="features")
# Neurons for classes
estimated = tf.compat.v1.layers.dense(inputs=features, units=params.nclasses, activation=None)
estimated_label = tf.argmax(estimated, 1, name="prediction")
estimated = tf.keras.layers.Dense(params.nclasses)(features)
estimated_label = tf.argmax(estimated, name="prediction")
return estimated, estimated_label
......
......@@ -29,23 +29,24 @@ parser.add_argument("--outdir", help="Output directory for SavedModel", required
params = parser.parse_args()
def conv2d_valid(x, kernel_size, filters, activation="relu"):
conv_op = tf.keras.layers.Conv2D(filters=filters, kernel_size=kernel_size, activation=activation)
return conv_op(x)
def my_model(x):
# input patches: 16x16x4
conv1 = tf.compat.v1.layers.conv2d(inputs=x, filters=16, kernel_size=[5, 5], padding="valid",
activation=tf.nn.relu) # out size: 12x12x16
conv2 = tf.compat.v1.layers.conv2d(inputs=conv1, filters=16, kernel_size=[5, 5], padding="valid",
activation=tf.nn.relu) # out size: 8x8x16
conv3 = tf.compat.v1.layers.conv2d(inputs=conv2, filters=32, kernel_size=[5, 5], padding="valid",
activation=tf.nn.relu) # out size: 4x4x32
conv4 = tf.compat.v1.layers.conv2d(inputs=conv3, filters=32, kernel_size=[4, 4], padding="valid",
activation=tf.nn.relu) # out size: 1x1x32
conv1 = conv2d_valid(x, filters=16, kernel_size=5) # out size: 12x12x16
conv2 = conv2d_valid(conv1, filters=16, kernel_size=5) # out size: 8x8x16
conv3 = conv2d_valid(conv2, filters=32, kernel_size=5) # out size: 4x4x32
conv4 = conv2d_valid(conv3, filters=32, kernel_size=4) # out size: 1x1x32
# Features
features = tf.reshape(conv4, shape=[-1, 32], name="features")
# 8 neurons for 8 classes
estimated = tf.compat.v1.layers.dense(inputs=features, units=params.nclasses, activation=None)
estimated_label = tf.argmax(estimated, 1, name="prediction")
# Neurons for classes
estimated = tf.keras.layers.Dense(params.nclasses)(features)
estimated_label = tf.argmax(estimated, name="prediction")
return estimated, estimated_label
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment