Example #1
0
def residual_path(input_var,
                  out_channels,
                  scope=None,
                  stride=[1, 1],
                  kernel_sizes=[1, 3],
                  activation=tf.keras.activations.relu,
                  padding='SAME',
                  spectral_norm_flag=False,
                  update_collection=False,
                  is_training=True,
                  use_batch_norm=False,
                  use_cross_replica_batch_norm=False,
                  num_cores=8,
                  initializer=tf.keras.initializers.glorot_normal):

    with tf.variable_scope(scope):
        # first conv
        output_var = conv_batchnorm_relu(
            input_var,
            'Conv2d_Unit1',
            out_channels,
            kernel_size=kernel_sizes[0],
            stride=stride[0],
            is_training=is_training,
            use_batch_norm=use_batch_norm,
            use_cross_replica_batch_norm=use_cross_replica_batch_norm,
            num_cores=num_cores,
            padding=padding)
        # second conv
        output_var = conv_batchnorm_relu(
            output_var,
            'Conv2d_Unit2',
            out_channels,
            kernel_size=kernel_sizes[1],
            stride=stride[1],
            is_training=is_training,
            use_batch_norm=use_batch_norm,
            use_cross_replica_batch_norm=use_cross_replica_batch_norm,
            num_cores=num_cores,
            padding=padding)
        # after this technically we need to merge other paths with a 1x1 convolution
        return output_var
Example #2
0
def network_head(input_var,
                 out_channels,
                 stride=[1, 1],
                 kernel_sizes=[3, 3],
                 activation=tf.keras.activations.relu,
                 padding='SAME',
                 spectral_norm_flag=False,
                 update_collection=False,
                 is_training=True,
                 use_batch_norm=False,
                 use_cross_replica_batch_norm=False,
                 num_cores=8,
                 initializer=tf.keras.initializers.glorot_normal):

    intermediate_layer = conv_batchnorm_relu(
        input_var,
        'Conv2d_3x3_a',
        out_channels,
        kernel_size=kernel_sizes[0],
        stride=stride[0],
        padding=padding,
        is_training=is_training,
        num_cores=num_cores,
        use_batch_norm=use_batch_norm,
        use_cross_replica_batch_norm=use_cross_replica_batch_norm)
    head = conv_batchnorm_relu(
        intermediate_layer,
        'Conv2d_3x3_b',
        out_channels,
        kernel_size=kernel_sizes[0],
        stride=stride[0],
        padding=padding,
        is_training=is_training,
        num_cores=num_cores,
        use_batch_norm=use_batch_norm,
        use_cross_replica_batch_norm=use_cross_replica_batch_norm)
    return head
Example #3
0
    def build_network():
        network = i3d.InceptionI3d(
            final_endpoint=FLAGS.final_endpoint,
            use_batch_norm=FLAGS.use_batch_norm,
            use_cross_replica_batch_norm=FLAGS.use_cross_replica_batch_norm,
            num_cores=FLAGS.train_num_cores,
            num_classes=FLAGS.num_classes,
            spatial_squeeze=True,
            dropout_keep_prob=0.7)

        if FLAGS.final_endpoint == 'Logits':
            logits, end_points = network(
                inputs=features['video'],
                is_training=(mode == tf.estimator.ModeKeys.TRAIN))

            return logits
        else:
            descriptors, end_points = network(
                inputs=features['video'],
                is_training=(mode == tf.estimator.ModeKeys.TRAIN))
            # From the descriptors, we need to downsample now. That should be an input argument.
            t_subsample_factor = FLAGS.time_divisor
            hw_subsample_factor = FLAGS.hw_divisor
            spatial_squeeze = True
            end_point = 'Logits'
            with tf.variable_scope(end_point):
                fc_inputs = avgpool(descriptors,
                                    ksize=[
                                        1, t_subsample_factor,
                                        hw_subsample_factor,
                                        hw_subsample_factor, 1
                                    ],
                                    strides=[1, 1, 1, 1, 1],
                                    padding='VALID')
                get_shape = fc_inputs.get_shape().as_list()
                print('{} / Average-pool3D: {}'.format(end_point, get_shape))
                end_points[end_point + '_average_pool3d'] = fc_inputs

                # Dropout
                fc_inputs = tf.nn.dropout(fc_inputs, 0.7)

                # Use two FC layers
                if FLAGS.double_logits:
                    fc_inputs = conv_batchnorm_relu(
                        fc_inputs,
                        'Conv3d_xx_1x1',
                        256,
                        kernel_size=1,
                        stride=1,
                        use_batch_norm=FLAGS.use_batch_norm,
                        use_cross_replica_batch_norm=FLAGS.
                        use_cross_replica_batch_norm,
                        is_training=(mode == tf.estimator.ModeKeys.TRAIN),
                        num_cores=FLAGS.train_num_cores)
                    get_shape = fc_inputs.get_shape().as_list()
                    print('{} / Conv3d_xx_1x1 : {}'.format(
                        end_point, get_shape))

                # 1x1x1 Conv, stride 1
                logits = conv_batchnorm_relu(
                    fc_inputs,
                    'Conv3d_0c_1x1',
                    FLAGS.num_classes,
                    kernel_size=1,
                    stride=1,
                    activation=None,
                    use_batch_norm=False,
                    use_cross_replica_batch_norm=False,
                    is_training=(mode == tf.estimator.ModeKeys.TRAIN),
                    num_cores=FLAGS.train_num_cores)
                get_shape = logits.get_shape().as_list()
                print('{} / Conv3d_0c_1x1 : {}'.format(end_point, get_shape))

                if spatial_squeeze:
                    # Removes dimensions of size 1 from the shape of a tensor
                    # Specify which dimensions have to be removed: 2 and 3
                    logits = tf.squeeze(logits, [2, 3], name='SpatialSqueeze')
                    get_shape = logits.get_shape().as_list()
                    print('{} / Spatial Squeeze : {}'.format(
                        end_point, get_shape))

            averaged_logits = tf.reduce_mean(logits,
                                             axis=1)  # [N, num_classes]
            get_shape = averaged_logits.get_shape().as_list()
            print('{} / Averaged Logits : {}'.format(end_point, get_shape))

            end_points[end_point] = averaged_logits
            return averaged_logits
Example #4
0
    def model(inputs, is_training):

        net = inputs
        layer_outputs = {}

        print('Input: {}'.format(net.get_shape().as_list()))

        with tf.variable_scope(model_scope_name):
            # Encode_1
            end_point = 'Encode_1'
            with tf.variable_scope(end_point):
                # 3x3 Conv, padding='same'
                conv2d_1a = conv_batchnorm_relu(
                    net,
                    'Conv2d_1a',
                    num_classes,
                    kernel_size=3,
                    stride=1,
                    padding='SAME',
                    is_training=is_training,
                    num_cores=num_cores,
                    use_batch_norm=use_batch_norm,
                    use_cross_replica_batch_norm=use_cross_replica_batch_norm)
                get_shape = conv2d_1a.get_shape().as_list()
                full_layer_name = model_scope_name + '/' + end_point + '/' + 'Conv2d_1a'
                print('{}: {}'.format(full_layer_name, get_shape))
                layer_outputs[full_layer_name] = conv2d_1a

                # 3x3 Conv, padding='same'
                conv2d_1b = conv_batchnorm_relu(
                    conv2d_1a,
                    'Conv2d_1b',
                    num_classes,
                    kernel_size=3,
                    stride=1,
                    padding='SAME',
                    is_training=is_training,
                    num_cores=num_cores,
                    use_batch_norm=use_batch_norm,
                    use_cross_replica_batch_norm=use_cross_replica_batch_norm)
                get_shape = conv2d_1b.get_shape().as_list()
                full_layer_name = model_scope_name + '/' + end_point + '/' + 'Conv2d_1b'
                print('{}: {}'.format(full_layer_name, get_shape))
                layer_outputs[full_layer_name] = conv2d_1b

                # 2x2 MaxPool
                maxpool_1a = maxpool(conv2d_1b,
                                     'MaxPool_1a',
                                     ksize=[1, 2, 2, 1],
                                     strides=[1, 2, 2, 1],
                                     padding='SAME')
                get_shape = maxpool_1a.get_shape().as_list()
                full_layer_name = model_scope_name + '/' + end_point + '/' + 'MaxPool_1a'
                print('{}: {}'.format(full_layer_name, get_shape))
                layer_outputs[full_layer_name] = maxpool_1a

            if final_endpoint == end_point: return maxpool_1a, layer_outputs

            # Encode_2
            end_point = 'Encode_2'
            with tf.variable_scope(end_point):
                # 3x3 Conv, padding='same'
                conv2d_2a = conv_batchnorm_relu(
                    maxpool_1a,
                    'Conv2d_2a',
                    num_classes * 2,
                    kernel_size=3,
                    stride=1,
                    padding='SAME',
                    is_training=is_training,
                    num_cores=num_cores,
                    use_batch_norm=use_batch_norm,
                    use_cross_replica_batch_norm=use_cross_replica_batch_norm)
                get_shape = conv2d_2a.get_shape().as_list()
                full_layer_name = model_scope_name + '/' + end_point + '/' + 'Conv2d_2a'
                print('{}: {}'.format(full_layer_name, get_shape))
                layer_outputs[full_layer_name] = conv2d_2a

                # 3x3 Conv, padding='same'
                conv2d_2b = conv_batchnorm_relu(
                    conv2d_2a,
                    'Conv2d_2b',
                    num_classes * 2,
                    kernel_size=3,
                    stride=1,
                    padding='SAME',
                    is_training=is_training,
                    num_cores=num_cores,
                    use_batch_norm=use_batch_norm,
                    use_cross_replica_batch_norm=use_cross_replica_batch_norm)
                get_shape = conv2d_2b.get_shape().as_list()
                full_layer_name = model_scope_name + '/' + end_point + '/' + 'Conv2d_2b'
                print('{}: {}'.format(full_layer_name, get_shape))
                layer_outputs[full_layer_name] = conv2d_2b

                # 2x2 MaxPool
                maxpool_2a = maxpool(conv2d_2b,
                                     'MaxPool_2a',
                                     ksize=[1, 2, 2, 1],
                                     strides=[1, 2, 2, 1],
                                     padding='SAME')
                get_shape = maxpool_2a.get_shape().as_list()
                full_layer_name = model_scope_name + '/' + end_point + '/' + 'MaxPool_2a'
                print('{}: {}'.format(full_layer_name, get_shape))
                layer_outputs[full_layer_name] = maxpool_2a

            layer_outputs[end_point] = maxpool_2a
            if final_endpoint == end_point: return maxpool_2a, layer_outputs

            # Encode_3
            end_point = 'Encode_3'
            with tf.variable_scope(end_point):
                # 3x3 Conv, padding='same'
                conv2d_3a = conv_batchnorm_relu(
                    maxpool_2a,
                    'Conv2d_3a',
                    num_classes * 4,
                    kernel_size=3,
                    stride=1,
                    padding='SAME',
                    is_training=is_training,
                    num_cores=num_cores,
                    use_batch_norm=use_batch_norm,
                    use_cross_replica_batch_norm=use_cross_replica_batch_norm)
                get_shape = conv2d_3a.get_shape().as_list()
                full_layer_name = model_scope_name + '/' + end_point + '/' + 'Conv2d_3a'
                print('{}: {}'.format(full_layer_name, get_shape))
                layer_outputs[full_layer_name] = conv2d_3a

                # 3x3 Conv, padding='same'
                conv2d_3b = conv_batchnorm_relu(
                    conv2d_3a,
                    'Conv2d_3b',
                    num_classes * 4,
                    kernel_size=3,
                    stride=1,
                    padding='SAME',
                    is_training=is_training,
                    num_cores=num_cores,
                    use_batch_norm=use_batch_norm,
                    use_cross_replica_batch_norm=use_cross_replica_batch_norm)
                get_shape = conv2d_3b.get_shape().as_list()
                full_layer_name = model_scope_name + '/' + end_point + '/' + 'Conv2d_3b'
                print('{}: {}'.format(full_layer_name, get_shape))
                layer_outputs[full_layer_name] = conv2d_3b

                # 2x2 MaxPool
                maxpool_3a = maxpool(conv2d_3b,
                                     'MaxPool_3a',
                                     ksize=[1, 2, 2, 1],
                                     strides=[1, 2, 2, 1],
                                     padding='SAME')
                get_shape = maxpool_3a.get_shape().as_list()
                full_layer_name = model_scope_name + '/' + end_point + '/' + 'MaxPool_3a'
                print('{}: {}'.format(full_layer_name, get_shape))
                layer_outputs[full_layer_name] = maxpool_3a

            layer_outputs[end_point] = maxpool_3a
            if final_endpoint == end_point: return maxpool_3a, layer_outputs

            # Encode_4
            end_point = 'Encode_4'
            with tf.variable_scope(end_point):
                # 3x3 Conv, padding='same'
                conv2d_4a = conv_batchnorm_relu(
                    maxpool_3a,
                    'Conv2d_4a',
                    num_classes * 8,
                    kernel_size=3,
                    stride=1,
                    padding='SAME',
                    is_training=is_training,
                    num_cores=num_cores,
                    use_batch_norm=use_batch_norm,
                    use_cross_replica_batch_norm=use_cross_replica_batch_norm)
                get_shape = conv2d_4a.get_shape().as_list()
                full_layer_name = model_scope_name + '/' + end_point + '/' + 'Conv2d_4a'
                print('{}: {}'.format(full_layer_name, get_shape))
                layer_outputs[full_layer_name] = conv2d_4a

                # 3x3 Conv, padding='same'
                conv2d_4b = conv_batchnorm_relu(
                    conv2d_4a,
                    'Conv2d_4b',
                    num_classes * 8,
                    kernel_size=3,
                    stride=1,
                    padding='SAME',
                    is_training=is_training,
                    num_cores=num_cores,
                    use_batch_norm=use_batch_norm,
                    use_cross_replica_batch_norm=use_cross_replica_batch_norm)
                get_shape = conv2d_4b.get_shape().as_list()
                full_layer_name = model_scope_name + '/' + end_point + '/' + 'Conv2d_4b'
                print('{}: {}'.format(full_layer_name, get_shape))
                layer_outputs[full_layer_name] = conv2d_4b

                # 2x2 MaxPool
                maxpool_4a = maxpool(conv2d_4b,
                                     'MaxPool_4a',
                                     ksize=[1, 2, 2, 1],
                                     strides=[1, 2, 2, 1],
                                     padding='SAME')
                get_shape = maxpool_4a.get_shape().as_list()
                full_layer_name = model_scope_name + '/' + end_point + '/' + 'MaxPool_4a'
                print('{}: {}'.format(full_layer_name, get_shape))
                layer_outputs[full_layer_name] = maxpool_4a

            layer_outputs[end_point] = maxpool_4a
            if final_endpoint == end_point: return maxpool_4a, layer_outputs

            # Encode_5
            end_point = 'Encode_5'
            with tf.variable_scope(end_point):
                # 3x3 Conv, padding='same'
                conv2d_5a = conv_batchnorm_relu(
                    maxpool_4a,
                    'Conv2d_5a',
                    num_classes * 16,
                    kernel_size=3,
                    stride=1,
                    padding='SAME',
                    is_training=is_training,
                    num_cores=num_cores,
                    use_batch_norm=use_batch_norm,
                    use_cross_replica_batch_norm=use_cross_replica_batch_norm)
                get_shape = conv2d_5a.get_shape().as_list()
                full_layer_name = model_scope_name + '/' + end_point + '/' + 'Conv2d_5a'
                print('{}: {}'.format(full_layer_name, get_shape))
                layer_outputs[full_layer_name] = conv2d_5a

                # 3x3 Conv, padding='same'
                conv2d_5b = conv_batchnorm_relu(
                    conv2d_5a,
                    'Conv2d_5b',
                    num_classes * 16,
                    kernel_size=3,
                    stride=1,
                    padding='SAME',
                    is_training=is_training,
                    num_cores=num_cores,
                    use_batch_norm=use_batch_norm,
                    use_cross_replica_batch_norm=use_cross_replica_batch_norm)
                get_shape = conv2d_5b.get_shape().as_list()
                full_layer_name = model_scope_name + '/' + end_point + '/' + 'Conv2d_5b'
                print('{}: {}'.format(full_layer_name, get_shape))
                layer_outputs[full_layer_name] = conv2d_5b

            layer_outputs[end_point] = conv2d_5b
            if final_endpoint == end_point: return conv2d_5b, layer_outputs

            # Decode_1
            end_point = 'Decode_1'
            with tf.variable_scope(end_point):
                # Up-convolution
                upconv2d_1a = upconv_2D(conv2d_5b,
                                        'UpConv2d_1a',
                                        num_classes * 8,
                                        kernel_size=(2, 2),
                                        strides=(2, 2),
                                        use_bias=True,
                                        padding='valid')
                get_shape = upconv2d_1a.get_shape().as_list()
                full_layer_name = model_scope_name + '/' + end_point + '/' + 'UpConv2d_1a'
                print('{}: {}'.format(full_layer_name, get_shape))
                layer_outputs[full_layer_name] = upconv2d_1a

                # Merge
                merge_1a = tf.concat([conv2d_4b, upconv2d_1a],
                                     axis=-1,
                                     name='merge_1a')
                get_shape = merge_1a.get_shape().as_list()
                full_layer_name = model_scope_name + '/' + end_point + '/' + 'merge_1a'
                print('{}: {}'.format(full_layer_name, get_shape))
                layer_outputs[full_layer_name] = merge_1a

                # 3x3 Conv, padding='same'
                conv2d_d_1a = conv_batchnorm_relu(
                    merge_1a,
                    'Conv2d_d_1a',
                    num_classes * 8,
                    kernel_size=3,
                    stride=1,
                    padding='SAME',
                    is_training=is_training,
                    num_cores=num_cores,
                    use_batch_norm=use_batch_norm,
                    use_cross_replica_batch_norm=use_cross_replica_batch_norm)
                get_shape = conv2d_d_1a.get_shape().as_list()
                full_layer_name = model_scope_name + '/' + end_point + '/' + 'Conv2d_d_1a'
                print('{}: {}'.format(full_layer_name, get_shape))
                layer_outputs[full_layer_name] = conv2d_d_1a

                # 3x3 Conv, padding='same'
                conv2d_d_1b = conv_batchnorm_relu(
                    conv2d_d_1a,
                    'Conv2d_d_1b',
                    num_classes * 8,
                    kernel_size=3,
                    stride=1,
                    padding='SAME',
                    is_training=is_training,
                    num_cores=num_cores,
                    use_batch_norm=use_batch_norm,
                    use_cross_replica_batch_norm=use_cross_replica_batch_norm)
                get_shape = conv2d_d_1b.get_shape().as_list()
                full_layer_name = model_scope_name + '/' + end_point + '/' + 'Conv2d_d_1b'
                print('{}: {}'.format(full_layer_name, get_shape))
                layer_outputs[full_layer_name] = conv2d_d_1b

            layer_outputs[end_point] = conv2d_d_1b
            if final_endpoint == end_point: return conv2d_d_1b, layer_outputs

            # Decode_2
            end_point = 'Decode_2'
            with tf.variable_scope(end_point):
                # Up-convolution
                upconv2d_2a = upconv_2D(conv2d_d_1b,
                                        'UpConv2d_2a',
                                        num_classes * 4,
                                        kernel_size=(2, 2),
                                        strides=(2, 2),
                                        use_bias=True,
                                        padding='valid')
                get_shape = upconv2d_2a.get_shape().as_list()
                full_layer_name = model_scope_name + '/' + end_point + '/' + 'UpConv2d_2a'
                print('{}: {}'.format(full_layer_name, get_shape))
                layer_outputs[full_layer_name] = upconv2d_2a

                # Merge
                merge_2a = tf.concat([conv2d_3b, upconv2d_2a],
                                     axis=-1,
                                     name='merge_2a')
                get_shape = merge_2a.get_shape().as_list()
                full_layer_name = model_scope_name + '/' + end_point + '/' + 'merge_2a'
                print('{}: {}'.format(full_layer_name, get_shape))
                layer_outputs[full_layer_name] = merge_2a

                # 3x3 Conv, padding='same'
                conv2d_d_2a = conv_batchnorm_relu(
                    merge_2a,
                    'Conv2d_d_2a',
                    num_classes * 4,
                    kernel_size=3,
                    stride=1,
                    padding='SAME',
                    is_training=is_training,
                    num_cores=num_cores,
                    use_batch_norm=use_batch_norm,
                    use_cross_replica_batch_norm=use_cross_replica_batch_norm)
                get_shape = conv2d_d_2a.get_shape().as_list()
                full_layer_name = model_scope_name + '/' + end_point + '/' + 'Conv2d_d_2a'
                print('{}: {}'.format(full_layer_name, get_shape))
                layer_outputs[full_layer_name] = conv2d_d_2a

                # 3x3 Conv, padding='same'
                conv2d_d_2b = conv_batchnorm_relu(
                    conv2d_d_2a,
                    'Conv2d_d_2b',
                    num_classes * 4,
                    kernel_size=3,
                    stride=1,
                    padding='SAME',
                    is_training=is_training,
                    num_cores=num_cores,
                    use_batch_norm=use_batch_norm,
                    use_cross_replica_batch_norm=use_cross_replica_batch_norm)
                get_shape = conv2d_d_2b.get_shape().as_list()
                full_layer_name = model_scope_name + '/' + end_point + '/' + 'Conv2d_d_2b'
                print('{}: {}'.format(full_layer_name, get_shape))
                layer_outputs[full_layer_name] = conv2d_d_2b

            layer_outputs[end_point] = conv2d_d_2b
            if final_endpoint == end_point: return conv2d_d_2b, layer_outputs

            # Decode_3
            end_point = 'Decode_3'
            with tf.variable_scope(end_point):
                # Up-convolution
                upconv2d_3a = upconv_2D(conv2d_d_2b,
                                        'UpConv2d_3a',
                                        num_classes * 2,
                                        kernel_size=(2, 2),
                                        strides=(2, 2),
                                        use_bias=True,
                                        padding='valid')
                get_shape = upconv2d_3a.get_shape().as_list()
                full_layer_name = model_scope_name + '/' + end_point + '/' + 'UpConv2d_3a'
                print('{}: {}'.format(full_layer_name, get_shape))
                layer_outputs[full_layer_name] = upconv2d_3a

                # Merge
                merge_3a = tf.concat([conv2d_2b, upconv2d_3a],
                                     axis=-1,
                                     name='merge_3a')
                get_shape = merge_3a.get_shape().as_list()
                full_layer_name = model_scope_name + '/' + end_point + '/' + 'merge_3a'
                print('{}: {}'.format(full_layer_name, get_shape))
                layer_outputs[full_layer_name] = merge_3a

                # 3x3 Conv, padding='same'
                conv2d_d_3a = conv_batchnorm_relu(
                    merge_3a,
                    'Conv2d_d_3a',
                    num_classes * 2,
                    kernel_size=3,
                    stride=1,
                    padding='SAME',
                    is_training=is_training,
                    num_cores=num_cores,
                    use_batch_norm=use_batch_norm,
                    use_cross_replica_batch_norm=use_cross_replica_batch_norm)
                get_shape = conv2d_d_3a.get_shape().as_list()
                full_layer_name = model_scope_name + '/' + end_point + '/' + 'Conv2d_d_3a'
                print('{}: {}'.format(full_layer_name, get_shape))
                layer_outputs[full_layer_name] = conv2d_d_3a

                # 3x3 Conv, padding='same'
                conv2d_d_3b = conv_batchnorm_relu(
                    conv2d_d_3a,
                    'Conv2d_d_3b',
                    num_classes * 2,
                    kernel_size=3,
                    stride=1,
                    padding='SAME',
                    is_training=is_training,
                    num_cores=num_cores,
                    use_batch_norm=use_batch_norm,
                    use_cross_replica_batch_norm=use_cross_replica_batch_norm)
                get_shape = conv2d_d_3b.get_shape().as_list()
                full_layer_name = model_scope_name + '/' + end_point + '/' + 'Conv2d_d_3b'
                print('{}: {}'.format(full_layer_name, get_shape))
                layer_outputs[full_layer_name] = conv2d_d_3b

            layer_outputs[end_point] = conv2d_d_3b
            if final_endpoint == end_point: return conv2d_d_3b, layer_outputs

            # Decode_4
            end_point = 'Decode_4'
            with tf.variable_scope(end_point):
                # Up-convolution
                upconv2d_4a = upconv_2D(conv2d_d_3b,
                                        'UpConv2d_4a',
                                        num_classes,
                                        kernel_size=(2, 2),
                                        strides=(2, 2),
                                        use_bias=True,
                                        padding='valid')
                get_shape = upconv2d_4a.get_shape().as_list()
                full_layer_name = model_scope_name + '/' + end_point + '/' + 'UpConv2d_4a'
                print('{}: {}'.format(full_layer_name, get_shape))
                layer_outputs[full_layer_name] = upconv2d_4a

                # Merge
                merge_4a = tf.concat([conv2d_1b, upconv2d_4a],
                                     axis=-1,
                                     name='merge_4a')
                get_shape = merge_4a.get_shape().as_list()
                full_layer_name = model_scope_name + '/' + end_point + '/' + 'merge_4a'
                print('{}: {}'.format(full_layer_name, get_shape))
                layer_outputs[full_layer_name] = merge_4a

                # 3x3 Conv, padding='same'
                conv2d_d_4a = conv_batchnorm_relu(
                    merge_4a,
                    'Conv2d_d_4a',
                    num_classes,
                    kernel_size=3,
                    stride=1,
                    padding='SAME',
                    is_training=is_training,
                    num_cores=num_cores,
                    use_batch_norm=use_batch_norm,
                    use_cross_replica_batch_norm=use_cross_replica_batch_norm)
                get_shape = conv2d_d_4a.get_shape().as_list()
                full_layer_name = model_scope_name + '/' + end_point + '/' + 'Conv2d_d_4a'
                print('{}: {}'.format(full_layer_name, get_shape))
                layer_outputs[full_layer_name] = conv2d_d_4a

                # 3x3 Conv, padding='same'
                conv2d_d_4b = conv_batchnorm_relu(
                    conv2d_d_4a,
                    'Conv2d_d_4b',
                    num_classes,
                    kernel_size=3,
                    stride=1,
                    padding='SAME',
                    is_training=is_training,
                    num_cores=num_cores,
                    use_batch_norm=use_batch_norm,
                    use_cross_replica_batch_norm=use_cross_replica_batch_norm)
                get_shape = conv2d_d_4b.get_shape().as_list()
                full_layer_name = model_scope_name + '/' + end_point + '/' + 'Conv2d_d_4b'
                print('{}: {}'.format(full_layer_name, get_shape))
                layer_outputs[full_layer_name] = conv2d_d_4b

                # 1x1 Conv, padding='same'
                if single_channel:
                    num_output_channels = 1
                else:
                    num_output_channels = num_classes

####
                conv2d_d_4c = conv_batchnorm_relu(
                    conv2d_d_4b,
                    'Conv2d_d_4c',
                    num_output_channels,
                    kernel_size=1,
                    stride=1,
                    padding='SAME',
                    is_training=is_training,
                    num_cores=num_cores,
                    use_batch_norm=use_batch_norm,
                    use_cross_replica_batch_norm=use_cross_replica_batch_norm
                )  #,
                #activation=None)
                get_shape = conv2d_d_4c.get_shape().as_list()
                full_layer_name = model_scope_name + '/' + end_point + '/' + 'Conv2d_d_4c'
                print('{}: {}'.format(full_layer_name, get_shape))
                layer_outputs[full_layer_name] = conv2d_d_4c

            layer_outputs[end_point] = conv2d_d_4c
            if final_endpoint == end_point: return conv2d_d_4c, layer_outputs
Example #5
0
    def model(inputs, is_training):

        net = inputs
        end_points = {}

        print('Inputs: {}'.format(net.get_shape().as_list()))

        # 7x7x7 Conv, stride 2
        end_point = 'Conv3d_1a_7x7'
        net = conv_batchnorm_relu(
            net,
            end_point,
            64,
            kernel_size=7,
            stride=2,
            is_training=is_training,
            num_cores=num_cores,
            use_batch_norm=use_batch_norm,
            use_cross_replica_batch_norm=use_cross_replica_batch_norm)
        get_shape = net.get_shape().as_list()
        print('{} : {}'.format(end_point, get_shape))

        end_points[end_point] = net
        if final_endpoint == end_point: return net, end_points

        # 1x3x3 Max-pool, stride 1, 2, 2
        end_point = 'MaxPool3d_2a_3x3'
        net = maxpool(net,
                      end_point,
                      ksize=[1, 1, 3, 3, 1],
                      strides=[1, 1, 2, 2, 1],
                      padding='SAME')
        get_shape = net.get_shape().as_list()
        print('{} : {}'.format(end_point, get_shape))

        end_points[end_point] = net
        if final_endpoint == end_point: return net, end_points

        # 1x1x1 Conv, stride 1
        end_point = 'Conv3d_2b_1x1'
        net = conv_batchnorm_relu(
            net,
            end_point,
            64,
            kernel_size=1,
            stride=1,
            is_training=is_training,
            num_cores=num_cores,
            use_batch_norm=use_batch_norm,
            use_cross_replica_batch_norm=use_cross_replica_batch_norm)
        get_shape = net.get_shape().as_list()
        print('{} : {}'.format(end_point, get_shape))

        end_points[end_point] = net
        if final_endpoint == end_point: return net, end_points

        # 3x3x3 Conv, stride 1
        end_point = 'Conv3d_2c_3x3'
        net = conv_batchnorm_relu(
            net,
            end_point,
            192,
            kernel_size=3,
            stride=1,
            is_training=is_training,
            num_cores=num_cores,
            use_batch_norm=use_batch_norm,
            use_cross_replica_batch_norm=use_cross_replica_batch_norm)
        get_shape = net.get_shape().as_list()
        print('{} : {}'.format(end_point, get_shape))

        end_points[end_point] = net
        if final_endpoint == end_point: return net, end_points

        # 1x3x3 Max-pool, stride 1, 2, 2
        end_point = 'MaxPool3d_3a_3x3'
        net = maxpool(net,
                      end_point,
                      ksize=[1, 1, 3, 3, 1],
                      strides=[1, 1, 2, 2, 1],
                      padding='SAME')
        get_shape = net.get_shape().as_list()
        print('{} : {}'.format(end_point, get_shape))

        end_points[end_point] = net
        if final_endpoint == end_point: return net, end_points

        # Mixed 3b : Inception block
        end_point = 'Mixed_3b'
        with tf.variable_scope(end_point):
            with tf.variable_scope('Branch_0'):
                # 1x1x1 Conv, stride 1
                branch_0 = conv_batchnorm_relu(
                    net,
                    'Conv3d_0a_1x1',
                    64,
                    kernel_size=1,
                    stride=1,
                    is_training=is_training,
                    num_cores=num_cores,
                    use_batch_norm=use_batch_norm,
                    use_cross_replica_batch_norm=use_cross_replica_batch_norm)

            with tf.variable_scope('Branch_1'):
                # 1x1x1 Conv, stride 1
                branch_1 = conv_batchnorm_relu(
                    net,
                    'Conv3d_0a_1x1',
                    96,
                    kernel_size=1,
                    stride=1,
                    is_training=is_training,
                    num_cores=num_cores,
                    use_batch_norm=use_batch_norm,
                    use_cross_replica_batch_norm=use_cross_replica_batch_norm)
                # 3x3x3 Conv, stride 1
                branch_1 = conv_batchnorm_relu(
                    branch_1,
                    'Conv3d_0b_3x3',
                    128,
                    kernel_size=3,
                    stride=1,
                    is_training=is_training,
                    num_cores=num_cores,
                    use_batch_norm=use_batch_norm,
                    use_cross_replica_batch_norm=use_cross_replica_batch_norm)

            with tf.variable_scope('Branch_2'):
                # 1x1x1 Conv, stride 1
                branch_2 = conv_batchnorm_relu(
                    net,
                    'Conv3d_0a_1x1',
                    16,
                    kernel_size=1,
                    stride=1,
                    is_training=is_training,
                    num_cores=num_cores,
                    use_batch_norm=use_batch_norm,
                    use_cross_replica_batch_norm=use_cross_replica_batch_norm)
                # 3x3x3 Conv, stride 1
                branch_2 = conv_batchnorm_relu(
                    branch_2,
                    'Conv3d_0b_3x3',
                    32,
                    kernel_size=3,
                    stride=1,
                    is_training=is_training,
                    num_cores=num_cores,
                    use_batch_norm=use_batch_norm,
                    use_cross_replica_batch_norm=use_cross_replica_batch_norm)

            with tf.variable_scope('Branch_3'):
                # 3x3x3 Max-pool, stride 1, 1, 1
                branch_3 = maxpool(net,
                                   'MaxPool3d_0a_3x3',
                                   ksize=[1, 3, 3, 3, 1],
                                   strides=[1, 1, 1, 1, 1],
                                   padding='SAME')
                # 1x1x1 Conv, stride 1
                branch_3 = conv_batchnorm_relu(
                    branch_3,
                    'Conv3d_0b_1x1',
                    32,
                    kernel_size=1,
                    stride=1,
                    is_training=is_training,
                    num_cores=num_cores,
                    use_batch_norm=use_batch_norm,
                    use_cross_replica_batch_norm=use_cross_replica_batch_norm)

            # Concat branch_[0-3]
            net = tf.concat([branch_0, branch_1, branch_2, branch_3], 4)
        get_shape = net.get_shape().as_list()
        print('{} : {}'.format(end_point, get_shape))

        end_points[end_point] = net
        if final_endpoint == end_point: return net, end_points

        # Mixed 3c: Inception block
        end_point = 'Mixed_3c'
        with tf.variable_scope(end_point):
            with tf.variable_scope('Branch_0'):
                # 1x1x1 Conv, stride 1
                branch_0 = conv_batchnorm_relu(
                    net,
                    'Conv3d_0a_1x1',
                    128,
                    kernel_size=1,
                    stride=1,
                    is_training=is_training,
                    num_cores=num_cores,
                    use_batch_norm=use_batch_norm,
                    use_cross_replica_batch_norm=use_cross_replica_batch_norm)

            with tf.variable_scope('Branch_1'):
                # 1x1x1 Conv, stride 1
                branch_1 = conv_batchnorm_relu(
                    net,
                    'Conv3d_0b_1x1',
                    128,
                    kernel_size=1,
                    stride=1,
                    is_training=is_training,
                    num_cores=num_cores,
                    use_batch_norm=use_batch_norm,
                    use_cross_replica_batch_norm=use_cross_replica_batch_norm)
                # 3x3x3 Conv, stride 1
                branch_1 = conv_batchnorm_relu(
                    branch_1,
                    'Conv3d_0b_3x3',
                    192,
                    kernel_size=3,
                    stride=1,
                    is_training=is_training,
                    num_cores=num_cores,
                    use_batch_norm=use_batch_norm,
                    use_cross_replica_batch_norm=use_cross_replica_batch_norm)

            with tf.variable_scope('Branch_2'):
                # 1x1x1 Conv, stride 1
                branch_2 = conv_batchnorm_relu(
                    net,
                    'Conv3d_0a_1x1',
                    32,
                    kernel_size=1,
                    stride=1,
                    is_training=is_training,
                    num_cores=num_cores,
                    use_batch_norm=use_batch_norm,
                    use_cross_replica_batch_norm=use_cross_replica_batch_norm)
                # 3x3x3 Conv, stride 1
                branch_2 = conv_batchnorm_relu(
                    branch_2,
                    'Conv3d_0b_3x3',
                    96,
                    kernel_size=3,
                    stride=1,
                    is_training=is_training,
                    num_cores=num_cores,
                    use_batch_norm=use_batch_norm,
                    use_cross_replica_batch_norm=use_cross_replica_batch_norm)

            with tf.variable_scope('Branch_3'):
                # 3x3x3 Max-Pool, stride 1, 1, 1
                branch_3 = maxpool(net,
                                   'MaxPool3d_0a_3x3',
                                   ksize=[1, 3, 3, 3, 1],
                                   strides=[1, 1, 1, 1, 1],
                                   padding='SAME')
                # 1x1x1 Conv, stide 1
                branch_3 = conv_batchnorm_relu(
                    branch_3,
                    'Conv3d_0b_1x1',
                    64,
                    kernel_size=1,
                    stride=1,
                    is_training=is_training,
                    num_cores=num_cores,
                    use_batch_norm=use_batch_norm,
                    use_cross_replica_batch_norm=use_cross_replica_batch_norm)

            # Concat branch_[0-3]
            net = tf.concat([branch_0, branch_1, branch_2, branch_3], 4)
        get_shape = net.get_shape().as_list()
        print('{} : {}'.format(end_point, get_shape))

        end_points[end_point] = net
        if final_endpoint == end_point: return net, end_points

        # 3x3x3 Max-pool, stride 2, 2, 2
        end_point = 'MaxPool3d_4a_3x3'
        net = maxpool(net,
                      end_point,
                      ksize=[1, 3, 3, 3, 1],
                      strides=[1, 2, 2, 2, 1],
                      padding='SAME')
        get_shape = net.get_shape().as_list()
        print('{} : {}'.format(end_point, get_shape))

        end_points[end_point] = net
        if final_endpoint == end_point: return net, end_points

        # Mixed 4b: Inception block
        end_point = 'Mixed_4b'
        with tf.variable_scope(end_point):
            with tf.variable_scope('Branch_0'):
                # 1x1x1 Conv, stride 1
                branch_0 = conv_batchnorm_relu(
                    net,
                    'Conv3d_0a_1x1',
                    192,
                    kernel_size=1,
                    stride=1,
                    is_training=is_training,
                    num_cores=num_cores,
                    use_batch_norm=use_batch_norm,
                    use_cross_replica_batch_norm=use_cross_replica_batch_norm)

            with tf.variable_scope('Branch_1'):
                # 1x1x1 Conv, stride 1
                branch_1 = conv_batchnorm_relu(
                    net,
                    'Conv3d_0a_1x1',
                    96,
                    kernel_size=1,
                    stride=1,
                    is_training=is_training,
                    num_cores=num_cores,
                    use_batch_norm=use_batch_norm,
                    use_cross_replica_batch_norm=use_cross_replica_batch_norm)
                # 3x3x3 Conv, stride 1
                branch_1 = conv_batchnorm_relu(
                    branch_1,
                    'Conv3d_0b_3x3',
                    208,
                    kernel_size=3,
                    stride=1,
                    is_training=is_training,
                    num_cores=num_cores,
                    use_batch_norm=use_batch_norm,
                    use_cross_replica_batch_norm=use_cross_replica_batch_norm)

            with tf.variable_scope('Branch_2'):
                # 1x1x1 Conv, stride 1
                branch_2 = conv_batchnorm_relu(
                    net,
                    'Conv3d_0a_1x1',
                    16,
                    kernel_size=1,
                    stride=1,
                    is_training=is_training,
                    num_cores=num_cores,
                    use_batch_norm=use_batch_norm,
                    use_cross_replica_batch_norm=use_cross_replica_batch_norm)
                # 3x3x3 Conv, stride 1
                branch_2 = conv_batchnorm_relu(
                    branch_2,
                    'Conv3d_0b_3x3',
                    48,
                    kernel_size=3,
                    stride=1,
                    is_training=is_training,
                    num_cores=num_cores,
                    use_batch_norm=use_batch_norm,
                    use_cross_replica_batch_norm=use_cross_replica_batch_norm)

            with tf.variable_scope('Branch_3'):
                # 3x3x3 Max-pool, stride 1, 1, 1
                branch_3 = maxpool(net,
                                   'MaxPool3d_0a_3x3',
                                   ksize=[1, 3, 3, 3, 1],
                                   strides=[1, 1, 1, 1, 1],
                                   padding='SAME')
                # 1x1x1 Conv, stride 1
                branch_3 = conv_batchnorm_relu(
                    branch_3,
                    'Conv3d_0b_1x1',
                    64,
                    kernel_size=1,
                    stride=1,
                    is_training=is_training,
                    num_cores=num_cores,
                    use_batch_norm=use_batch_norm,
                    use_cross_replica_batch_norm=use_cross_replica_batch_norm)

            # Concat branch_[0-3]
            net = tf.concat([branch_0, branch_1, branch_2, branch_3], 4)
        get_shape = net.get_shape().as_list()
        print('{} : {}'.format(end_point, get_shape))

        end_points[end_point] = net
        if final_endpoint == end_point: return net, end_points

        # Mixed 4c: Inception block
        end_point = 'Mixed_4c'
        with tf.variable_scope(end_point):
            with tf.variable_scope('Branch_0'):
                # 1x1x1 Conv, stride 1
                branch_0 = conv_batchnorm_relu(
                    net,
                    'Conv3d_0a_1x1',
                    160,
                    kernel_size=1,
                    stride=1,
                    is_training=is_training,
                    num_cores=num_cores,
                    use_batch_norm=use_batch_norm,
                    use_cross_replica_batch_norm=use_cross_replica_batch_norm)

            with tf.variable_scope('Branch_1'):
                # 1x1x1 Conv, stride 1
                branch_1 = conv_batchnorm_relu(
                    net,
                    'Conv3d_0a_1x1',
                    112,
                    kernel_size=1,
                    stride=1,
                    is_training=is_training,
                    num_cores=num_cores,
                    use_batch_norm=use_batch_norm,
                    use_cross_replica_batch_norm=use_cross_replica_batch_norm)
                # 3x3x3 Conv, stride 1
                branch_1 = conv_batchnorm_relu(
                    branch_1,
                    'Conv3d_0b_3x3',
                    224,
                    kernel_size=3,
                    stride=1,
                    is_training=is_training,
                    num_cores=num_cores,
                    use_batch_norm=use_batch_norm,
                    use_cross_replica_batch_norm=use_cross_replica_batch_norm)

            with tf.variable_scope('Branch_2'):
                # 1x1x1 Conv, stride 1
                branch_2 = conv_batchnorm_relu(
                    net,
                    'Conv3d_0a_1x1',
                    24,
                    kernel_size=1,
                    stride=1,
                    is_training=is_training,
                    num_cores=num_cores,
                    use_batch_norm=use_batch_norm,
                    use_cross_replica_batch_norm=use_cross_replica_batch_norm)
                # 3x3x3 Conv, stride 1
                branch_2 = conv_batchnorm_relu(
                    branch_2,
                    'Conv3d_0b_3x3',
                    64,
                    kernel_size=3,
                    stride=1,
                    is_training=is_training,
                    num_cores=num_cores,
                    use_batch_norm=use_batch_norm,
                    use_cross_replica_batch_norm=use_cross_replica_batch_norm)

            with tf.variable_scope('Branch_3'):
                # 3x3x3 Max-pool, stride 1, 1, 1
                branch_3 = maxpool(net,
                                   'MaxPool3d_0a_3x3',
                                   ksize=[1, 3, 3, 3, 1],
                                   strides=[1, 1, 1, 1, 1],
                                   padding='SAME')
                # 1x1x1 Conv, stride 1
                branch_3 = conv_batchnorm_relu(
                    branch_3,
                    'Conv3d_0b_1x1',
                    64,
                    kernel_size=1,
                    stride=1,
                    is_training=is_training,
                    num_cores=num_cores,
                    use_batch_norm=use_batch_norm,
                    use_cross_replica_batch_norm=use_cross_replica_batch_norm)

            # Concat branch_[0-3]
            net = tf.concat([branch_0, branch_1, branch_2, branch_3], 4)
        get_shape = net.get_shape().as_list()
        print('{} : {}'.format(end_point, get_shape))

        end_points[end_point] = net
        if final_endpoint == end_point: return net, end_points

        # Mixed 4d: Inception block
        end_point = 'Mixed_4d'
        with tf.variable_scope(end_point):
            with tf.variable_scope('Branch_0'):
                # 1x1x1 Conv, stride 1
                branch_0 = conv_batchnorm_relu(
                    net,
                    'Conv3d_0a_1x1',
                    128,
                    kernel_size=1,
                    stride=1,
                    is_training=is_training,
                    num_cores=num_cores,
                    use_batch_norm=use_batch_norm,
                    use_cross_replica_batch_norm=use_cross_replica_batch_norm)

            with tf.variable_scope('Branch_1'):
                # 1x1x1 Conv, stride 1
                branch_1 = conv_batchnorm_relu(
                    net,
                    'Conv3d_0a_1x1',
                    128,
                    kernel_size=1,
                    stride=1,
                    is_training=is_training,
                    num_cores=num_cores,
                    use_batch_norm=use_batch_norm,
                    use_cross_replica_batch_norm=use_cross_replica_batch_norm)
                # 3x3x3 Conv, stride 1
                branch_1 = conv_batchnorm_relu(
                    branch_1,
                    'Conv3d_0b_3x3',
                    256,
                    kernel_size=3,
                    stride=1,
                    is_training=is_training,
                    num_cores=num_cores,
                    use_batch_norm=use_batch_norm,
                    use_cross_replica_batch_norm=use_cross_replica_batch_norm)

            with tf.variable_scope('Branch_2'):
                # 1x1x1 Conv, stride 1
                branch_2 = conv_batchnorm_relu(
                    net,
                    'Conv3d_0a_1x1',
                    24,
                    kernel_size=1,
                    stride=1,
                    is_training=is_training,
                    num_cores=num_cores,
                    use_batch_norm=use_batch_norm,
                    use_cross_replica_batch_norm=use_cross_replica_batch_norm)
                # 3x3x3 Conv, stride 1
                branch_2 = conv_batchnorm_relu(
                    branch_2,
                    'Conv3d_0b_3x3',
                    64,
                    kernel_size=3,
                    stride=1,
                    is_training=is_training,
                    num_cores=num_cores,
                    use_batch_norm=use_batch_norm,
                    use_cross_replica_batch_norm=use_cross_replica_batch_norm)

            with tf.variable_scope('Branch_3'):
                # 3x3x3 Max-pool, stride 1, 1, 1
                branch_3 = maxpool(net,
                                   'MaxPool3d_0a_3x3',
                                   ksize=[1, 3, 3, 3, 1],
                                   strides=[1, 1, 1, 1, 1],
                                   padding='SAME')
                # 1x1x1 Conv, stride 1
                branch_3 = conv_batchnorm_relu(
                    branch_3,
                    'Conv3d_0b_1x1',
                    64,
                    kernel_size=1,
                    stride=1,
                    is_training=is_training,
                    num_cores=num_cores,
                    use_batch_norm=use_batch_norm,
                    use_cross_replica_batch_norm=use_cross_replica_batch_norm)

            # Concat branch_[0-3]
            net = tf.concat([branch_0, branch_1, branch_2, branch_3], 4)
        get_shape = net.get_shape().as_list()
        print('{} : {}'.format(end_point, get_shape))

        end_points[end_point] = net
        if final_endpoint == end_point: return net, end_points

        # Mixed 4e: Inception block
        end_point = 'Mixed_4e'
        with tf.variable_scope(end_point):
            with tf.variable_scope('Branch_0'):
                # 1x1x1 Conv, stride 1
                branch_0 = conv_batchnorm_relu(
                    net,
                    'Conv3d_0a_1x1',
                    112,
                    kernel_size=1,
                    stride=1,
                    is_training=is_training,
                    num_cores=num_cores,
                    use_batch_norm=use_batch_norm,
                    use_cross_replica_batch_norm=use_cross_replica_batch_norm)

            with tf.variable_scope('Branch_1'):
                # 1x1x1 Conv, stride 1
                branch_1 = conv_batchnorm_relu(
                    net,
                    'Conv3d_0a_1x1',
                    144,
                    kernel_size=1,
                    stride=1,
                    is_training=is_training,
                    num_cores=num_cores,
                    use_batch_norm=use_batch_norm,
                    use_cross_replica_batch_norm=use_cross_replica_batch_norm)
                # 3x3x3 Conv, stride 1
                branch_1 = conv_batchnorm_relu(
                    branch_1,
                    'Conv3d_0b_3x3',
                    288,
                    kernel_size=3,
                    stride=1,
                    is_training=is_training,
                    num_cores=num_cores,
                    use_batch_norm=use_batch_norm,
                    use_cross_replica_batch_norm=use_cross_replica_batch_norm)

            with tf.variable_scope('Branch_2'):
                # 1x1x1 Conv, stride 1
                branch_2 = conv_batchnorm_relu(
                    net,
                    'Conv3d_0a_1x1',
                    32,
                    kernel_size=1,
                    stride=1,
                    is_training=is_training,
                    num_cores=num_cores,
                    use_batch_norm=use_batch_norm,
                    use_cross_replica_batch_norm=use_cross_replica_batch_norm)
                # 3x3x3 Conv, stride 1
                branch_2 = conv_batchnorm_relu(
                    branch_2,
                    'Conv3d_0b_3x3',
                    64,
                    kernel_size=3,
                    stride=1,
                    is_training=is_training,
                    num_cores=num_cores,
                    use_batch_norm=use_batch_norm,
                    use_cross_replica_batch_norm=use_cross_replica_batch_norm)

            with tf.variable_scope('Branch_3'):
                # 3x3x3 Max-pool, stride 1, 1, 1
                branch_3 = maxpool(net,
                                   'MaxPool3d_0a_3x3',
                                   ksize=[1, 3, 3, 3, 1],
                                   strides=[1, 1, 1, 1, 1],
                                   padding='SAME')
                # 1x1x1 Conv, stride 1
                branch_3 = conv_batchnorm_relu(
                    branch_3,
                    'Conv3d_0b_1x1',
                    64,
                    kernel_size=1,
                    stride=1,
                    is_training=is_training,
                    num_cores=num_cores,
                    use_batch_norm=use_batch_norm,
                    use_cross_replica_batch_norm=use_cross_replica_batch_norm)

            # Concat branch_[0-3]
            net = tf.concat([branch_0, branch_1, branch_2, branch_3], 4)
        get_shape = net.get_shape().as_list()
        print('{} : {}'.format(end_point, get_shape))

        end_points[end_point] = net
        if final_endpoint == end_point: return net, end_points

        # Mixed 4f: Inception block
        end_point = 'Mixed_4f'
        with tf.variable_scope(end_point):
            with tf.variable_scope('Branch_0'):
                # 1x1x1 Conv, stride 1
                branch_0 = conv_batchnorm_relu(
                    net,
                    'Conv3d_0a_1x1',
                    256,
                    kernel_size=1,
                    stride=1,
                    is_training=is_training,
                    num_cores=num_cores,
                    use_batch_norm=use_batch_norm,
                    use_cross_replica_batch_norm=use_cross_replica_batch_norm)

            with tf.variable_scope('Branch_1'):
                # 1x1x1 Conv, stride 1
                branch_1 = conv_batchnorm_relu(
                    net,
                    'Conv3d_0a_1x1',
                    160,
                    kernel_size=1,
                    stride=1,
                    is_training=is_training,
                    num_cores=num_cores,
                    use_batch_norm=use_batch_norm,
                    use_cross_replica_batch_norm=use_cross_replica_batch_norm)
                # 3x3x3 Conv, stride 1
                branch_1 = conv_batchnorm_relu(
                    branch_1,
                    'Conv3d_0b_3x3',
                    320,
                    kernel_size=3,
                    stride=1,
                    is_training=is_training,
                    num_cores=num_cores,
                    use_batch_norm=use_batch_norm,
                    use_cross_replica_batch_norm=use_cross_replica_batch_norm)

            with tf.variable_scope('Branch_2'):
                # 1x1x1 Conv, stride 1
                branch_2 = conv_batchnorm_relu(
                    net,
                    'Conv3d_0a_1x1',
                    32,
                    kernel_size=1,
                    stride=1,
                    is_training=is_training,
                    num_cores=num_cores,
                    use_batch_norm=use_batch_norm,
                    use_cross_replica_batch_norm=use_cross_replica_batch_norm)
                # 3x3x3 Conv, stride 1
                branch_2 = conv_batchnorm_relu(
                    branch_2,
                    'Conv3d_0b_3x3',
                    128,
                    kernel_size=3,
                    stride=1,
                    is_training=is_training,
                    num_cores=num_cores,
                    use_batch_norm=use_batch_norm,
                    use_cross_replica_batch_norm=use_cross_replica_batch_norm)

            with tf.variable_scope('Branch_3'):
                # 3x3x3 Max-pool, stride 1, 1, 1
                branch_3 = maxpool(net,
                                   'MaxPool3d_0a_3x3',
                                   ksize=[1, 3, 3, 3, 1],
                                   strides=[1, 1, 1, 1, 1],
                                   padding='SAME')
                # 1x1x1 Conv, stride 1
                branch_3 = conv_batchnorm_relu(
                    branch_3,
                    'Conv3d_0b_1x1',
                    128,
                    kernel_size=1,
                    stride=1,
                    is_training=is_training,
                    num_cores=num_cores,
                    use_batch_norm=use_batch_norm,
                    use_cross_replica_batch_norm=use_cross_replica_batch_norm)

            # Concat branch_[0-3]
            net = tf.concat([branch_0, branch_1, branch_2, branch_3], 4)
        get_shape = net.get_shape().as_list()
        print('{} : {}'.format(end_point, get_shape))

        end_points[end_point] = net
        if final_endpoint == end_point: return net, end_points

        # 2x2x2 Max-pool, stride 2x2x2
        end_point = 'MaxPool3d_5a_2x2'
        net = maxpool(net,
                      end_point,
                      ksize=[1, 2, 2, 2, 1],
                      strides=[1, 2, 2, 2, 1],
                      padding='SAME')
        get_shape = net.get_shape().as_list()
        print('{} : {}'.format(end_point, get_shape))

        end_points[end_point] = net
        if final_endpoint == end_point: return net, end_points

        # Mixed 5b: Inception block
        end_point = 'Mixed_5b'
        with tf.variable_scope(end_point):
            with tf.variable_scope('Branch_0'):
                # 1x1x1 Conv, stride 1
                branch_0 = conv_batchnorm_relu(
                    net,
                    'Conv3d_0a_1x1',
                    256,
                    kernel_size=1,
                    stride=1,
                    is_training=is_training,
                    num_cores=num_cores,
                    use_batch_norm=use_batch_norm,
                    use_cross_replica_batch_norm=use_cross_replica_batch_norm)

            with tf.variable_scope('Branch_1'):
                # 1x1x1 Conv, stride 1
                branch_1 = conv_batchnorm_relu(
                    net,
                    'Conv3d_0a_1x1',
                    160,
                    kernel_size=1,
                    stride=1,
                    is_training=is_training,
                    num_cores=num_cores,
                    use_batch_norm=use_batch_norm,
                    use_cross_replica_batch_norm=use_cross_replica_batch_norm)
                # 3x3x3 Conv, stride 1
                branch_1 = conv_batchnorm_relu(
                    branch_1,
                    'Conv3d_0b_3x3',
                    320,
                    kernel_size=3,
                    stride=1,
                    is_training=is_training,
                    num_cores=num_cores,
                    use_batch_norm=use_batch_norm,
                    use_cross_replica_batch_norm=use_cross_replica_batch_norm)

            with tf.variable_scope('Branch_2'):
                # 1x1x1 Conv, stride 1
                branch_2 = conv_batchnorm_relu(
                    net,
                    'Conv3d_0a_1x1',
                    32,
                    kernel_size=1,
                    stride=1,
                    is_training=is_training,
                    num_cores=num_cores,
                    use_batch_norm=use_batch_norm,
                    use_cross_replica_batch_norm=use_cross_replica_batch_norm)
                # 3x3x3 Conv, stride 1
                branch_2 = conv_batchnorm_relu(
                    branch_2,
                    'Conv3d_0b_3x3',
                    128,
                    kernel_size=3,
                    stride=1,
                    is_training=is_training,
                    num_cores=num_cores,
                    use_batch_norm=use_batch_norm,
                    use_cross_replica_batch_norm=use_cross_replica_batch_norm)

            with tf.variable_scope('Branch_3'):
                # 3x3x3 Max-pool, stride 1, 1, 1
                branch_3 = maxpool(net,
                                   'MaxPool3d_0a_3x3',
                                   ksize=[1, 3, 3, 3, 1],
                                   strides=[1, 1, 1, 1, 1],
                                   padding='SAME')
                # 1x1x1 Conv, stride 1
                branch_3 = conv_batchnorm_relu(
                    branch_3,
                    'Conv3d_0b_1x1',
                    128,
                    kernel_size=1,
                    stride=1,
                    is_training=is_training,
                    num_cores=num_cores,
                    use_batch_norm=use_batch_norm,
                    use_cross_replica_batch_norm=use_cross_replica_batch_norm)

            # Concat branch_[0-3]
            net = tf.concat([branch_0, branch_1, branch_2, branch_3], 4)
        get_shape = net.get_shape().as_list()
        print('{} : {}'.format(end_point, get_shape))

        end_points[end_point] = net
        if final_endpoint == end_point: return net, end_points

        # Mixed 5c: Inception block
        end_point = 'Mixed_5c'
        with tf.variable_scope(end_point):
            with tf.variable_scope('Branch_0'):
                # 1x1x1 Conv, stride 1
                branch_0 = conv_batchnorm_relu(
                    net,
                    'Conv3d_0a_1x1',
                    384,
                    kernel_size=1,
                    stride=1,
                    is_training=is_training,
                    num_cores=num_cores,
                    use_batch_norm=use_batch_norm,
                    use_cross_replica_batch_norm=use_cross_replica_batch_norm)

            with tf.variable_scope('Branch_1'):
                # 1x1x1 Conv, stride 1
                branch_1 = conv_batchnorm_relu(
                    net,
                    'Conv3d_0a_1x1',
                    192,
                    kernel_size=1,
                    stride=1,
                    is_training=is_training,
                    num_cores=num_cores,
                    use_batch_norm=use_batch_norm,
                    use_cross_replica_batch_norm=use_cross_replica_batch_norm)
                # 3x3x3 Conv, stride 1
                branch_1 = conv_batchnorm_relu(
                    branch_1,
                    'Conv3d_0b_3x3',
                    384,
                    kernel_size=3,
                    stride=1,
                    is_training=is_training,
                    num_cores=num_cores,
                    use_batch_norm=use_batch_norm,
                    use_cross_replica_batch_norm=use_cross_replica_batch_norm)

            with tf.variable_scope('Branch_2'):
                # 1x1x1 Conv, stride 1
                branch_2 = conv_batchnorm_relu(
                    net,
                    'Conv3d_0a_1x1',
                    48,
                    kernel_size=1,
                    stride=1,
                    is_training=is_training,
                    num_cores=num_cores,
                    use_batch_norm=use_batch_norm,
                    use_cross_replica_batch_norm=use_cross_replica_batch_norm)
                # 3x3x3 Conv, stride 1
                branch_2 = conv_batchnorm_relu(
                    branch_2,
                    'Conv3d_0b_3x3',
                    128,
                    kernel_size=3,
                    stride=1,
                    is_training=is_training,
                    num_cores=num_cores,
                    use_batch_norm=use_batch_norm,
                    use_cross_replica_batch_norm=use_cross_replica_batch_norm)

            with tf.variable_scope('Branch_3'):
                # 3x3x3 Max-pool, stride 1, 1, 1
                branch_3 = maxpool(net,
                                   'MaxPool3d_0a_3x3',
                                   ksize=[1, 3, 3, 3, 1],
                                   strides=[1, 1, 1, 1, 1],
                                   padding='SAME')
                # 1x1x1 Conv, stride 1
                branch_3 = conv_batchnorm_relu(
                    branch_3,
                    'Conv3d_0b_1x1',
                    128,
                    kernel_size=1,
                    stride=1,
                    is_training=is_training,
                    num_cores=num_cores,
                    use_batch_norm=use_batch_norm,
                    use_cross_replica_batch_norm=use_cross_replica_batch_norm)

            # Concat branch_[0-3]
            net = tf.concat([branch_0, branch_1, branch_2, branch_3], 4)
        get_shape = net.get_shape().as_list()
        print('{} : {}'.format(end_point, get_shape))

        end_points[end_point] = net
        if final_endpoint == end_point: return net, end_points

        # Logits
        end_point = 'Logits'
        with tf.variable_scope(end_point):
            # 2x7x7 Average-pool, stride 1, 1, 1
            net = avgpool(net,
                          ksize=[1, 2, 7, 7, 1],
                          strides=[1, 1, 1, 1, 1],
                          padding='VALID')
            get_shape = net.get_shape().as_list()
            print('{} / Average-pool3D: {}'.format(end_point, get_shape))

            # Dropout
            net = tf.nn.dropout(net, dropout_keep_prob)

            # 1x1x1 Conv, stride 1
            logits = conv_batchnorm_relu(
                net,
                'Conv3d_0c_1x1',
                num_classes,
                kernel_size=1,
                stride=1,
                activation=None,
                use_batch_norm=use_batch_norm,
                use_cross_replica_batch_norm=use_cross_replica_batch_norm,
                is_training=is_training,
                num_cores=num_cores)
            get_shape = logits.get_shape().as_list()
            print('{} / Conv3d_0c_1x1 : {}'.format(end_point, get_shape))

            if spatial_squeeze:
                # Removes dimensions of size 1 from the shape of a tensor
                # Specify which dimensions have to be removed: 2 and 3
                logits = tf.squeeze(logits, [2, 3], name='SpatialSqueeze')
                get_shape = logits.get_shape().as_list()
                print('{} / Spatial Squeeze : {}'.format(end_point, get_shape))

        averaged_logits = tf.reduce_mean(logits, axis=1)
        get_shape = averaged_logits.get_shape().as_list()
        print('{} / Averaged Logits : {}'.format(end_point, get_shape))

        end_points[end_point] = averaged_logits
        if final_endpoint == end_point: return averaged_logits, end_points

        # Predictions
        end_point = 'Predictions'
        predictions = tf.nn.softmax(averaged_logits)
        end_points[end_point] = predictions
        return predictions, end_points
Example #6
0
def resnet_block(model_scope_name='resunet_1',
                 end_point='',
                 scope_id=0,
                 inputs=None,
                 out_channels=64,
                 is_training=True,
                 num_cores=8,
                 use_batch_norm=False,
                 use_cross_replica_batch_norm=False):

    # Shortcut: 3x3 Conv, padding='same', no activation
    shortcut_conv2d = conv_batchnorm_relu(
        inputs,
        'shortcut_Conv2d_{}a'.format(scope_id),
        out_channels,
        kernel_size=3,
        stride=1,
        padding='SAME',
        activation=None,
        is_training=is_training,
        num_cores=num_cores,
        use_batch_norm=use_batch_norm,
        use_cross_replica_batch_norm=use_cross_replica_batch_norm)
    get_shape = shortcut_conv2d.get_shape().as_list()
    full_layer_name = model_scope_name + '/' + end_point + '/' +\
            'shortcut_Conv2d_{}a'.format(scope_id)
    print('{}: {}'.format(full_layer_name, get_shape))
    layer_outputs[full_layer_name] = shortcut_conv2d

    # 3x3 Conv, padding='same'
    conv2d_1 = conv_batchnorm_relu(
        inputs,
        'Conv2d_{}a'.format(scope_id),
        out_channels,
        kernel_size=3,
        stride=1,
        padding='SAME',
        is_training=is_training,
        num_cores=num_cores,
        use_batch_norm=use_batch_norm,
        use_cross_replica_batch_norm=use_cross_replica_batch_norm)
    get_shape = conv2d_1.get_shape().as_list()
    full_layer_name = model_scope_name + '/' + end_point + '/'\
            + 'Conv2d_{}a'.format(scope_id)
    print('{}: {}'.format(full_layer_name, get_shape))
    layer_outputs[full_layer_name] = conv2d_1

    # 3x3 Conv, padding='same', no activation
    conv2d_2 = conv_batchnorm_relu(
        conv2d_1,
        'Conv2d_{}b'.format(scope_id),
        out_channels,
        kernel_size=3,
        stride=1,
        padding='SAME',
        is_training=is_training,
        num_cores=num_cores,
        activation=None,
        use_batch_norm=use_batch_norm,
        use_cross_replica_batch_norm=use_cross_replica_batch_norm)
    get_shape = conv2d_2.get_shape().as_list()
    full_layer_name = model_scope_name + '/' + end_point + '/' +\
            'Conv2d_{}b'.format(scope_id)
    print('{}: {}'.format(full_layer_name, get_shape))
    layer_outputs[full_layer_name] = conv2d_2

    # Sum: x + f(x) and relu
    with tf.variable_scope('sum_relu'):
        sum_1 = tf.nn.relu(shortcut_conv2d + conv2d_2)
    get_shape = sum_1.get_shape().as_list()
    full_layer_name = model_scope_name + '/' + end_point + '/' + 'sum_relu'
    print('{}: {}'.format(full_layer_name, get_shape))
    layer_outputs[full_layer_name] = sum_1

    return sum_1
Example #7
0
def gala(net,
         kernel_size=[],
         reduction=8,
         at_act=tf.nn.relu,
         comb_act=tf.keras.activations.softmax,
         comb_type='interpolate',
         include_fa=True,
         num_cores=8):
    # include_fa = feature/global attention

    # 1. FC layer with c / r channels + a nonlinearity
    channels = int(net.get_shape()[-1])
    intermediate_channels = int(channels / reduction)
    intermediate_activities = conv_batchnorm_relu(
        net,
        'ATTENTION_intermediate',
        intermediate_channels,
        activation=tf.keras.activations.relu,
        kernel_size=kernel_size,
        stride=1,
        num_cores=num_cores)
    ''' 
    intermediate_activities = tf.layers.conv2d(
        inputs=bottom,
        filters=intermediate_channels,
        kernel_size=intermediate_kernel,
        activation=intermediate_nl,
        padding='SAME',
        use_bias=True,
        kernel_initializer=tf.variance_scaling_initializer(),
        trainable=training,
        name='%s_ATTENTION_intermediate' % name)
    '''
    # 2. Spatial attention map
    output_activities = conv_batchnorm_relu(intermediate_activities,
                                            'ATTENTION_output',
                                            1,
                                            activation=None,
                                            kernel_size=kernel_size,
                                            stride=1,
                                            num_cores=num_cores)

    # Also calculate se attention
    if include_fa:
        fa_map = se(net,
                    intermediate_nl=tf.keras.activations.relu,
                    reduction=reduction,
                    return_map=True,
                    squash=tf.keras.activations.sigmoid,
                    pool_fun=tf.reduce_max)

    if comb_type == 'interpolate':
        alpha = tf.get_variable(name='alpha',
                                shape=[1, 1, 1, channels],
                                initializer=tf.constant_initializer(0.0),
                                dtype=tf.float32)
        alpha = tf.keras.activations.sigmoid(alpha)
        additive = output_activities + fa_map
        multiplicative = output_activities * fa_map
        output_activities = alpha * additive + (1 - alpha) * multiplicative
        '''
        if interaction == 'both':
          k = fa_map.get_shape().as_list()[-1]
          alpha = tf.get_variable(
              name='alpha_%s' % name,
              shape=[1, 1, 1, k],
              initializer=tf.variance_scaling_initializer(),
              dtype=tf.bfloat16 if self.use_tpu else tf.float32)
          beta = tf.get_variable(
              name='beta_%s' % name,
              shape=[1, 1, 1, k],
              initializer=tf.variance_scaling_initializer(),
              dtype=tf.bfloat16 if self.use_tpu else tf.float32)
          additive = output_activities + fa_map
          multiplicative = output_activities * fa_map
          output_activities = alpha * additive + beta * multiplicative
        '''
    elif comb_type == 'multiplicative':
        output_activities = output_activities * fa_map
    elif comb_type == 'additive':
        output_activities = output_activities + fa_map
    else:
        raise NotImplementedError(interaction)

    if comb_act == tf.keras.activations.softmax:
        act_shape = output_activities.get_shape().as_list()
        reshape_output_act = tf.reshape(
            tf.transpose(output_activities, perm=[0, 1, 4, 2, 3]),
            shape=[act_shape[0], act_shape[1], act_shape[-1], -1])
        output_activities = comb_act(reshape_output_act)
        output_activities = tf.transpose(tf.reshape(output_activities,
                                                    shape=[
                                                        act_shape[0],
                                                        act_shape[1],
                                                        act_shape[-1],
                                                        act_shape[2],
                                                        act_shape[3]
                                                    ]),
                                         perm=[0, 1, 3, 4, 2])
    else:
        output_activites = comb_act(output_activities)

    net = net * output_activities

    return net, output_activities
Example #8
0
def resnext_layer(input_var,
                  out_channels,
                  cardinality,
                  scope=None,
                  stride=[1, 2, 1],
                  kernel_sizes=[1, 3, 1],
                  activation=tf.keras.activations.relu,
                  padding='SAME',
                  spectral_norm_flag=False,
                  update_collection=False,
                  is_training=True,
                  use_batch_norm=False,
                  use_cross_replica_batch_norm=False,
                  num_cores=8,
                  initializer=tf.keras.initializers.glorot_normal):
    with tf.variable_scope(scope):
        input_shape = input_var.get_shape().as_list()
        # get number of channels in input
        ch = input_shape[-1]
        # group conv out_channels
        group_conv_channels = out_channels // 2
        # create a list of split layers
        split_layers = list()
        for path in range(cardinality):
            current_split = residual_path(
                input_var,
                group_conv_channels // cardinality,
                scope='Path' + str(path),
                stride=stride[:2],
                kernel_sizes=kernel_sizes[:2],
                activation=activation,
                padding=padding,
                spectral_norm_flag=spectral_norm_flag,
                update_collection=update_collection,
                is_training=is_training,
                use_batch_norm=use_batch_norm,
                use_cross_replica_batch_norm=use_cross_replica_batch_norm,
                num_cores=num_cores,
                initializer=initializer)
            # append to the list
            split_layers.append(current_split)

        # concatenate all the splits
        grouped_input = tf.concat(split_layers, axis=3)
        # 1x1 convolution. IMP: No relu here!
        merged_input = conv_batchnorm_relu(
            grouped_input,
            'Merge',
            out_channels,
            kernel_size=kernel_sizes[-1],
            stride=stride[-1],
            is_training=is_training,
            use_batch_norm=use_batch_norm,
            use_cross_replica_batch_norm=use_cross_replica_batch_norm,
            num_cores=num_cores,
            padding=padding,
            activation=None)
        sh = merged_input.get_shape().as_list()
        if not (sh[1] == input_shape[1]):
            mod_input = conv_batchnorm_relu(
                input_var,
                'Downsample',
                out_channels,
                kernel_size=1,
                stride=2,
                is_training=is_training,
                use_batch_norm=use_batch_norm,
                use_cross_replica_batch_norm=use_cross_replica_batch_norm,
                num_cores=num_cores,
                padding=padding,
                activation=None)
        else:
            mod_input = input_var

        # add the skip connection and the rectify!
        return tf.nn.relu(mod_input + merged_input, name='rectify')
Example #9
0
    def model(inputs, is_training):

        net = inputs
        end_points = {}

        print('Input: {}'.format(net.get_shape().as_list()))
        '''
        Build the feedforward part of the network
        Here is where we select the model type.
        Currently supported options: resnet, resnet150, resnext, resnext150
        '''
        stack_architecture = [3, 4, 6, 3]
        # define the stacking hyperparameters for the larger model type
        if '150' in model_type:
            stack_architecture = [3, 8, 36, 3]

        _, layers = residual_network(
            inputs,
            is_training,
            num_cores=num_cores,
            use_batch_norm=use_batch_norm,
            use_cross_replica_batch_norm=use_cross_replica_batch_norm,
            final_endpoint='Encode_5',
            model_type=model_type,
            stack_architecture=stack_architecture)

        #########
        # Feature Pyramid
        #########
        heads = {}
        end_point = 'Pyr_5'
        with tf.variable_scope(end_point):
            fpn_5 = conv_batchnorm_relu(
                layers['Encode_5'],
                'Conv2d_1x1',
                256,
                kernel_size=1,
                stride=1,
                padding='SAME',
                is_training=is_training,
                num_cores=num_cores,
                use_batch_norm=use_batch_norm,
                use_cross_replica_batch_norm=use_cross_replica_batch_norm)
            fpn_5_head = network_head(fpn_5, 128, is_training=is_training)
            heads[end_point] = fpn_5_head
        get_shape = fpn_5.get_shape().as_list()
        print('{} : {}'.format(end_point, get_shape))

        end_point = 'Pyr_4'
        with tf.variable_scope(end_point):
            fpn_4 = conv_batchnorm_relu(
                layers['Encode_4'],
                'Conv2d_1x1',
                256,
                kernel_size=1,
                stride=1,
                padding='SAME',
                is_training=is_training,
                num_cores=num_cores,
                use_batch_norm=use_batch_norm,
                use_cross_replica_batch_norm=use_cross_replica_batch_norm,
                activation=None)
            # upsample fpn_5 and add
            sh = fpn_5.get_shape().as_list()
            #fpn_4 = tf.nn.relu(fpn_4 + tf.image.resize_images(fpn_5, [sh[1]*2, sh[2]*2]))
            fpn_4 = tf.nn.relu(fpn_4 + nearest_upsampling(fpn_5, 2))
            fpn_4_head = network_head(tf.nn.relu(fpn_4),
                                      128,
                                      is_training=is_training)
            heads[end_point] = fpn_4_head
        get_shape = fpn_4.get_shape().as_list()
        print('{} : {}'.format(end_point, get_shape))

        end_point = 'Pyr_3'
        with tf.variable_scope(end_point):
            fpn_3 = conv_batchnorm_relu(
                layers['Encode_3'],
                'Conv2d_1x1',
                256,
                kernel_size=1,
                stride=1,
                padding='SAME',
                is_training=is_training,
                num_cores=num_cores,
                use_batch_norm=use_batch_norm,
                use_cross_replica_batch_norm=use_cross_replica_batch_norm,
                activation=None)
            # upsample fpn_4 and add
            sh = fpn_4.get_shape().as_list()
            #fpn_3 = tf.nn.relu(fpn_3 + tf.image.resize_images(fpn_4, [sh[1]*2, sh[2]*2]))
            fpn_3 = tf.nn.relu(fpn_3 + nearest_upsampling(fpn_4, 2))
            fpn_3_head = network_head(tf.nn.relu(fpn_3),
                                      128,
                                      is_training=is_training)
            heads[end_point] = fpn_3_head
        get_shape = fpn_3.get_shape().as_list()
        print('{} : {}'.format(end_point, get_shape))

        end_point = 'Pyr_2'
        with tf.variable_scope(end_point):
            fpn_2 = conv_batchnorm_relu(
                layers['Encode_2'],
                'Conv2d_1x1',
                256,
                kernel_size=1,
                stride=1,
                padding='SAME',
                is_training=is_training,
                num_cores=num_cores,
                use_batch_norm=use_batch_norm,
                use_cross_replica_batch_norm=use_cross_replica_batch_norm,
                activation=None)
            # upsample fpn_3 and add
            sh = fpn_3.get_shape().as_list()
            #fpn_2 = tf.nn.relu(fpn_2 + tf.image.resize_images(fpn_3, [sh[1]*2, sh[2]*2]))
            fpn_2 = tf.nn.relu(fpn_2 + nearest_upsampling(fpn_3, 2))
            fpn_2_head = network_head(tf.nn.relu(fpn_2),
                                      128,
                                      is_training=is_training)
            heads[end_point] = fpn_2_head
        get_shape = fpn_2.get_shape().as_list()
        print('{} : {}'.format(end_point, get_shape))

        end_point = 'Head_Master'
        with tf.variable_scope(end_point):
            net_heads = list()
            inp_shape = inputs.get_shape().as_list()
            for head_name in heads.keys():
                sh = heads[head_name].get_shape().as_list()
                # calculate scale at which upsampling must be done
                scale_factor = inp_shape[1] // sh[1]
                #up_sampled_layer = tf.image.resize_images(heads[head_name],[inp_shape[1], inp_shape[2]])
                up_sampled_layer = nearest_upsampling(heads[head_name],
                                                      scale_factor)
                net_heads.append(up_sampled_layer)

            # concatenate
            concat_head = tf.concat(net_heads, axis=3)
            model_output = conv_batchnorm_relu(
                concat_head,
                'Conv2d_3x3_Final',
                num_classes,
                kernel_size=3,
                stride=1,
                padding='SAME',
                is_training=is_training,
                num_cores=num_cores,
                use_batch_norm=use_batch_norm,
                use_cross_replica_batch_norm=use_cross_replica_batch_norm,
                activation=None)
        get_shape = model_output.get_shape().as_list()
        print('{} : {}'.format(end_point, get_shape))

        return model_output
Example #10
0
def residual_network(inputs,
                     is_training,
                     num_cores=8,
                     use_batch_norm=False,
                     use_cross_replica_batch_norm=False,
                     final_endpoint='Head_Master',
                     model_type='resnet',
                     stack_architecture=[3, 4, 6, 3]):

    if final_endpoint not in VALID_ENDPOINTS:
        raise ValueError('Unknown final endpoint %s' % final_endpoint)

    net = inputs
    end_points = {}

    print('Input: {}'.format(net.get_shape().as_list()))

    # Encode_1
    end_point = 'Encode_1'
    out_channels_1a = 64
    with tf.variable_scope(end_point):
        # 7x7 Conv, padding='same'
        encode_1 = conv_batchnorm_relu(
            net,
            'Conv2d_1a',
            out_channels_1a,
            kernel_size=7,
            stride=2,
            padding='SAME',
            is_training=is_training,
            num_cores=num_cores,
            use_batch_norm=use_batch_norm,
            use_cross_replica_batch_norm=use_cross_replica_batch_norm)

        get_shape = encode_1.get_shape().as_list()
        print('{} / Encode_1: {}'.format(end_point, get_shape))

    end_points[end_point] = encode_1
    if final_endpoint == end_point: return encode_1, end_points

    if len(stack_architecture) < 4:
        raise ValueError(
            'Not enough hyperparameter specifications. This module needs at least 4 blocks of stacks to be defined'
        )

    # Encode_2
    end_point = 'Encode_2'
    cardinality = 32
    # 3
    encode_2 = residual_stack(
        encode_1,
        256,
        cardinality,
        num_stacks=stack_architecture[0],
        scope=end_point,
        should_stride=True,
        is_training=is_training,
        use_batch_norm=use_batch_norm,
        use_cross_replica_batch_norm=use_cross_replica_batch_norm,
        num_cores=num_cores,
        model_type=model_type)
    get_shape = encode_2.get_shape().as_list()
    print('{} / Encode_2: {}'.format(end_point, get_shape))
    end_points[end_point] = encode_2
    if final_endpoint == end_point: return encode_2, end_points

    # Encode_3
    end_point = 'Encode_3'
    # 4
    encode_3 = residual_stack(
        encode_2,
        512,
        cardinality,
        num_stacks=stack_architecture[1],
        scope=end_point,
        should_stride=True,
        is_training=is_training,
        use_batch_norm=use_batch_norm,
        use_cross_replica_batch_norm=use_cross_replica_batch_norm,
        num_cores=num_cores,
        model_type=model_type)

    get_shape = encode_3.get_shape().as_list()
    print('{} / Encode_3: {}'.format(end_point, get_shape))
    end_points[end_point] = encode_3
    if final_endpoint == end_point: return encode_3, end_points

    # Encode_4
    end_point = 'Encode_4'
    # 6
    encode_4 = residual_stack(
        encode_3,
        1024,
        cardinality,
        num_stacks=stack_architecture[2],
        scope=end_point,
        should_stride=True,
        is_training=is_training,
        use_batch_norm=use_batch_norm,
        use_cross_replica_batch_norm=use_cross_replica_batch_norm,
        num_cores=num_cores,
        model_type=model_type)

    get_shape = encode_4.get_shape().as_list()
    print('{} / Encode_4: {}'.format(end_point, get_shape))
    end_points[end_point] = encode_4
    if final_endpoint == end_point: return encode_4, end_points

    # Encode_5
    end_point = 'Encode_5'
    # 3
    encode_5 = residual_stack(
        encode_4,
        2048,
        cardinality,
        num_stacks=stack_architecture[3],
        scope=end_point,
        should_stride=True,
        is_training=is_training,
        use_batch_norm=use_batch_norm,
        use_cross_replica_batch_norm=use_cross_replica_batch_norm,
        num_cores=num_cores,
        model_type=model_type)

    get_shape = encode_5.get_shape().as_list()
    print('{} / Encode_5: {}'.format(end_point, get_shape))
    end_points[end_point] = encode_5
    if final_endpoint == end_point: return encode_5, end_points

    return end_points
Example #11
0
def residual_stack(input_var,
                   out_channels,
                   cardinality,
                   num_stacks=1,
                   scope=None,
                   should_stride=True,
                   activation=tf.keras.activations.relu,
                   padding='SAME',
                   spectral_norm_flag=False,
                   update_collection=False,
                   is_training=True,
                   use_batch_norm=False,
                   use_cross_replica_batch_norm=False,
                   num_cores=8,
                   initializer=tf.keras.initializers.glorot_normal,
                   model_type='resnet'):

    with tf.variable_scope(scope):
        layer = input_var
        for stack in range(num_stacks):
            if (stack == 0) and should_stride:
                flag = True
                stride = [1, 2, 1]
            else:
                flag = False
                stride = [1, 1, 1]
            ####
            # account for the different models here
            ####

            if 'resnet' in model_type:
                cur_output = resnet_layer(
                    layer,
                    out_channels,
                    cardinality,
                    scope='Layer_' + str(stack),
                    stride=stride,
                    activation=activation,
                    padding=padding,
                    spectral_norm_flag=spectral_norm_flag,
                    update_collection=update_collection,
                    is_training=is_training,
                    use_batch_norm=use_batch_norm,
                    use_cross_replica_batch_norm=use_cross_replica_batch_norm,
                    num_cores=num_cores,
                    initializer=initializer)
            elif 'resnext' in model_type:
                cur_output = resnext_layer(
                    layer,
                    out_channels,
                    cardinality,
                    scope='Layer_' + str(stack),
                    stride=stride,
                    activation=activation,
                    padding=padding,
                    spectral_norm_flag=spectral_norm_flag,
                    update_collection=update_collection,
                    is_training=is_training,
                    use_batch_norm=use_batch_norm,
                    use_cross_replica_batch_norm=use_cross_replica_batch_norm,
                    num_cores=num_cores,
                    initializer=initializer)
            else:
                raise ValueError(
                    'This model type is currently not supported. Use [resnet|resnet150|resnext|resnext150]'
                )

            if flag:
                # need to do some dimension handling
                pool_inp = tf.layers.average_pooling2d(inputs=layer,
                                                       pool_size=[2, 2],
                                                       strides=2,
                                                       padding='SAME')
                pool_inp = conv_batchnorm_relu(
                    pool_inp,
                    'skip1x1_layer' + str(stack),
                    cur_output.get_shape().as_list()[-1],
                    stride=1,
                    kernel_size=1,
                    is_training=is_training,
                    use_batch_norm=use_batch_norm,
                    use_cross_replica_batch_norm=use_cross_replica_batch_norm,
                    num_cores=num_cores,
                    activation=None)
            else:
                # dimensions match!
                pool_inp = layer
            # add the skip connection
            layer = tf.nn.relu(pool_inp + cur_output)

        return layer
Example #12
0
def resnet_layer(input_var,
                 out_channels,
                 cardinality,
                 scope=None,
                 stride=[1, 2, 1],
                 kernel_sizes=[1, 3, 1],
                 activation=tf.keras.activations.relu,
                 padding='SAME',
                 spectral_norm_flag=False,
                 update_collection=False,
                 is_training=True,
                 use_batch_norm=False,
                 use_cross_replica_batch_norm=False,
                 num_cores=8,
                 initializer=tf.keras.initializers.glorot_normal):
    with tf.variable_scope(scope):
        input_shape = input_var.get_shape().as_list()
        # get number of channels in input
        ch = input_shape[-1]
        # group conv out_channels
        group_conv_channels = out_channels // 2
        output_var = conv_batchnorm_relu(
            input_var,
            'Conv2d_Unit1',
            group_conv_channels,
            kernel_size=kernel_sizes[0],
            stride=stride[0],
            is_training=is_training,
            use_batch_norm=use_batch_norm,
            use_cross_replica_batch_norm=use_cross_replica_batch_norm,
            num_cores=num_cores,
            padding=padding)
        # second conv
        output_var = conv_batchnorm_relu(
            output_var,
            'Conv2d_Unit2',
            group_conv_channels,
            kernel_size=kernel_sizes[1],
            stride=stride[1],
            is_training=is_training,
            use_batch_norm=use_batch_norm,
            use_cross_replica_batch_norm=use_cross_replica_batch_norm,
            num_cores=num_cores,
            padding=padding)

        # third conv
        output_var = conv_batchnorm_relu(
            output_var,
            'Conv2d_Unit3',
            out_channels,
            kernel_size=kernel_sizes[2],
            stride=stride[2],
            is_training=is_training,
            use_batch_norm=use_batch_norm,
            use_cross_replica_batch_norm=use_cross_replica_batch_norm,
            num_cores=num_cores,
            padding=padding,
            activation=None)

        sh = output_var.get_shape().as_list()
        if not (sh[1] == input_shape[1]):
            mod_input = conv_batchnorm_relu(
                input_var,
                'Downsample',
                out_channels,
                kernel_size=1,
                stride=2,
                is_training=is_training,
                use_batch_norm=use_batch_norm,
                use_cross_replica_batch_norm=use_cross_replica_batch_norm,
                num_cores=num_cores,
                padding=padding,
                activation=None)
        else:
            mod_input = input_var

        # add the skip connection and the rectify!
        return tf.nn.relu(mod_input + output_var, name='rectify')