如何在 CNTK 序列中添加两层
How to add two layers within a CNTK Sequential
我目前正在使用 CNTK 重新实现 Jonathan Longs FCN8-s 的 TensorFlow 实现。虽然 TensorFlow 同时对我来说非常熟悉,但我在使用 Microsoft 的 CNTK 方面还很缺乏经验。我阅读了一些 CNTK Github 教程,但现在我正处于要添加 pool4_score 和 upscore 层的地步。在 TensorFlow 中我会简单地使用 tf.add(pool4_score, upscore1)
但在 CNTK 中我必须使用 Sequentials(正确吗?)所以我的代码看起来像:
with default_options(activation=None, pad=True, bias=True):
z = Sequential([
For(range(2), lambda i: [
Convolution2D((3,3), 64, pad=True, name='conv1_{}'.format(i)),
Activation(activation=relu, name='relu1_{}'.format(i)),
]),
MaxPooling((2,2), (2,2), name='pool1'),
For(range(2), lambda i: [
Convolution2D((3,3), 128, pad=True, name='conv2_{}'.format(i)),
Activation(activation=relu, name='relu2_{}'.format(i)),
]),
MaxPooling((2,2), (2,2), name='pool2'),
For(range(3), lambda i: [
Convolution2D((3,3), 256, pad=True, name='conv3_{}'.format(i)),
Activation(activation=relu, name='relu3_{}'.format(i)),
]),
MaxPooling((2,2), (2,2), name='pool3'),
For(range(3), lambda i: [
Convolution2D((3,3), 512, pad=True, name='conv4_{}'.format(i)),
Activation(activation=relu, name='relu4_{}'.format(i)),
]),
MaxPooling((2,2), (2,2), name='pool4'),
For(range(3), lambda i: [
Convolution2D((3,3), 512, pad=True, name='conv5_{}'.format(i)),
Activation(activation=relu, name='relu5_{}'.format(i)),
]),
MaxPooling((2,2), (2,2), name='pool5'),
Convolution2D((7,7), 4096, pad=True, name='fc6'),
Activation(activation=relu, name='relu6'),
Dropout(0.5, name='drop6'),
Convolution2D((1,1), 4096, pad=True, name='fc7'),
Activation(activation=relu, name='relu7'),
Dropout(0.5, name='drop7'),
Convolution2D((1,1), num_classes, pad=True, name='fc8')
ConvolutionTranspose2D((4,4), num_classes, strides=(1,2), name='upscore1')
# TODO:
# conv for pool4_score with (1x512) and 21 classes
# combine upscore 1 and pool4_score
])(input)
我读到有一个 combine
方法。但是我没有找到如何在顺序中使用它的示例。那么如何使用 CNTK 实现 tf.add
方法呢?
非常感谢!
您可以使用 C.plus 或 +
,在这种情况下,您需要拆分序列才能到达要添加的图层。
例如以下:
z = Sequential([Convolution2D((3,3), 64, pad=True),
MaxPooling((2,2), (2,2))])(input)
相当于:
z1 = Convolution2D((3,3), 64, pad=True)(input)
z2 = MaxPooling((2,2), (2,2))(z1)
您现在可以执行 z1 + z2。
我目前正在使用 CNTK 重新实现 Jonathan Longs FCN8-s 的 TensorFlow 实现。虽然 TensorFlow 同时对我来说非常熟悉,但我在使用 Microsoft 的 CNTK 方面还很缺乏经验。我阅读了一些 CNTK Github 教程,但现在我正处于要添加 pool4_score 和 upscore 层的地步。在 TensorFlow 中我会简单地使用 tf.add(pool4_score, upscore1)
但在 CNTK 中我必须使用 Sequentials(正确吗?)所以我的代码看起来像:
with default_options(activation=None, pad=True, bias=True):
z = Sequential([
For(range(2), lambda i: [
Convolution2D((3,3), 64, pad=True, name='conv1_{}'.format(i)),
Activation(activation=relu, name='relu1_{}'.format(i)),
]),
MaxPooling((2,2), (2,2), name='pool1'),
For(range(2), lambda i: [
Convolution2D((3,3), 128, pad=True, name='conv2_{}'.format(i)),
Activation(activation=relu, name='relu2_{}'.format(i)),
]),
MaxPooling((2,2), (2,2), name='pool2'),
For(range(3), lambda i: [
Convolution2D((3,3), 256, pad=True, name='conv3_{}'.format(i)),
Activation(activation=relu, name='relu3_{}'.format(i)),
]),
MaxPooling((2,2), (2,2), name='pool3'),
For(range(3), lambda i: [
Convolution2D((3,3), 512, pad=True, name='conv4_{}'.format(i)),
Activation(activation=relu, name='relu4_{}'.format(i)),
]),
MaxPooling((2,2), (2,2), name='pool4'),
For(range(3), lambda i: [
Convolution2D((3,3), 512, pad=True, name='conv5_{}'.format(i)),
Activation(activation=relu, name='relu5_{}'.format(i)),
]),
MaxPooling((2,2), (2,2), name='pool5'),
Convolution2D((7,7), 4096, pad=True, name='fc6'),
Activation(activation=relu, name='relu6'),
Dropout(0.5, name='drop6'),
Convolution2D((1,1), 4096, pad=True, name='fc7'),
Activation(activation=relu, name='relu7'),
Dropout(0.5, name='drop7'),
Convolution2D((1,1), num_classes, pad=True, name='fc8')
ConvolutionTranspose2D((4,4), num_classes, strides=(1,2), name='upscore1')
# TODO:
# conv for pool4_score with (1x512) and 21 classes
# combine upscore 1 and pool4_score
])(input)
我读到有一个 combine
方法。但是我没有找到如何在顺序中使用它的示例。那么如何使用 CNTK 实现 tf.add
方法呢?
非常感谢!
您可以使用 C.plus 或 +
,在这种情况下,您需要拆分序列才能到达要添加的图层。
例如以下:
z = Sequential([Convolution2D((3,3), 64, pad=True),
MaxPooling((2,2), (2,2))])(input)
相当于:
z1 = Convolution2D((3,3), 64, pad=True)(input)
z2 = MaxPooling((2,2), (2,2))(z1)
您现在可以执行 z1 + z2。