Exemplo n.º 1
0
def EfficientNet(input_shape,
                 block_args_list,
                 global_params,
                 include_top=True,
                 pooling=None):
    batch_norm_momentum = global_params.batch_norm_momentum
    batch_norm_epsilon = global_params.batch_norm_epsilon
    if global_params.data_format == 'channels_first':
        channel_axis = 1
    else:
        channel_axis = -1

    # Stem part
    inputs = KL.Input(shape=input_shape)
    x = inputs
    x = KL.Conv2D(filters=round_filters(32, global_params),
                  kernel_size=[3, 3],
                  strides=[2, 2],
                  kernel_initializer=ConvKernalInitializer(),
                  padding='same',
                  use_bias=False)(x)
    x = KL.BatchNormalization(axis=channel_axis,
                              momentum=batch_norm_momentum,
                              epsilon=batch_norm_epsilon)(x)
    x = Swish()(x)

    # Blocks part
    block_idx = 1
    n_blocks = sum([block_args.num_repeat for block_args in block_args_list])
    drop_rate = global_params.drop_connect_rate or 0
    drop_rate_dx = drop_rate / n_blocks

    for block_args in block_args_list:
        assert block_args.num_repeat > 0
        # Update block input and output filters based on depth multiplier.
        block_args = block_args._replace(
            input_filters=round_filters(block_args.input_filters,
                                        global_params),
            output_filters=round_filters(block_args.output_filters,
                                         global_params),
            num_repeat=round_repeats(block_args.num_repeat, global_params))

        # The first block needs to take care of stride and filter size increase.
        x = MBConvBlock(block_args,
                        global_params,
                        drop_connect_rate=drop_rate_dx * block_idx)(x)
        block_idx += 1

        if block_args.num_repeat > 1:
            block_args = block_args._replace(
                input_filters=block_args.output_filters, strides=[1, 1])

        for _ in xrange(block_args.num_repeat - 1):
            x = MBConvBlock(block_args,
                            global_params,
                            drop_connect_rate=drop_rate_dx * block_idx)(x)
            block_idx += 1

    # Head part
    x = KL.Conv2D(filters=round_filters(1280, global_params),
                  kernel_size=[1, 1],
                  strides=[1, 1],
                  kernel_initializer=ConvKernalInitializer(),
                  padding='same',
                  use_bias=False)(x)
    x = KL.BatchNormalization(axis=channel_axis,
                              momentum=batch_norm_momentum,
                              epsilon=batch_norm_epsilon)(x)
    x = Swish()(x)

    if include_top:
        x = KL.GlobalAveragePooling2D(data_format=global_params.data_format)(x)
        if global_params.dropout_rate > 0:
            x = KL.Dropout(global_params.dropout_rate)(x)
        x = KL.Dense(global_params.num_classes,
                     kernel_initializer=DenseKernalInitializer())(x)
        x = KL.Activation('softmax')(x)
    else:
        if pooling == 'avg':
            x = KL.GlobalAveragePooling2D(
                data_format=global_params.data_format)(x)
        elif pooling == 'max':
            x = KL.GlobalMaxPooling2D(data_format=global_params.data_format)(x)

    outputs = x
    model = KM.Model(inputs, outputs)

    return model
Exemplo n.º 2
0
                      validation_freq=2)

print('history....', history.history)

network.sample_weights('weight.ckpt')
print('saved weights')
del network
network = Sequential([
    layers.Dense(256, activation='relu'),
    layers.Dense(128, activation='relu'),
    layers.Dense(64, activation='relu'),
    layers.Dense(32, activation='relu'),
    layers.Dense(10)
])

network.compile(optimizer=optimizers.Adam(lr=0.01),
                loss=tf.losses.CategoricalCrossentropy(from_logits=True),
                metrics=['accuracy'])
network.load_weights('weight.ckpt')
print('loaded weight!')

global_average_layer = layers.GlobalAveragePooling2D()
x = tf.random.normal([4, 7, 7, 2048])
out = global_average_layer(x)
print('############out.shape##############', out.shape)

fc = layers.Dense(100)
x = tf.random.normal([4, 2048])
out = fc(x)
print('........out.shape.........', out.shape)
Exemplo n.º 3
0
    def __init__(
        self,
        num_blocks: List[int],
        output_channels: List[int],
        stage_downsample: List[bool],
        stage_conv: List[bool],
        stage_pooling: List[Optional[Tuple[int, int]]],
        origin_stem: bool = True,
        stem_channels: int = 64,
        attn_module: Optional[Callable[[int], layers.Layer]] = None,
        include_top: bool = True,
        num_classes: int = 1000,
        cfg: Optional[Dict[str, Any]] = None,
        input_shape: Optional[Tuple[int, int, int]] = None,
    ) -> None:

        inplanes = stem_channels
        if origin_stem:
            _layers = [
                *conv_sequence(inplanes,
                               "relu",
                               True,
                               kernel_size=7,
                               strides=2,
                               input_shape=input_shape),
                layers.MaxPool2D(pool_size=(3, 3), strides=2, padding="same"),
            ]
        else:
            _layers = [
                *conv_sequence(inplanes // 2,
                               "relu",
                               True,
                               kernel_size=3,
                               input_shape=input_shape),
                *conv_sequence(inplanes, "relu", True, kernel_size=3),
                layers.MaxPool2D(pool_size=2, strides=2, padding="valid"),
            ]

        for n_blocks, out_chan, down, conv, pool in zip(
                num_blocks, output_channels, stage_downsample, stage_conv,
                stage_pooling):
            _layers.extend(
                resnet_stage(n_blocks, out_chan, out_chan != inplanes, down))
            if attn_module is not None:
                _layers.append(attn_module(out_chan))
            if conv:
                _layers.extend(
                    conv_sequence(out_chan,
                                  activation="relu",
                                  bn=True,
                                  kernel_size=3))
            if pool:
                _layers.append(
                    layers.MaxPool2D(pool_size=pool,
                                     strides=pool,
                                     padding="valid"))
            inplanes = out_chan

        if include_top:
            _layers.extend([
                layers.GlobalAveragePooling2D(),
                layers.Dense(num_classes),
            ])

        super().__init__(_layers)
        self.cfg = cfg
Exemplo n.º 4
0
def block(inputs,
          activation: str = "swish",
          drop_rate: float = 0.,
          name: str = "",
          input_channel: int = 32,
          output_channel: int = 16,
          kernel_size: int = 3,
          strides: int = 1,
          expand_ratio: int = 1,
          use_se: bool = True,
          se_ratio: float = 0.25):
    """An inverted residual block.

      Arguments:
          inputs: input tensor.
          activation: activation function.
          drop_rate: float between 0 and 1, fraction of the input units to drop.
          name: string, block label.
          input_channel: integer, the number of input filters.
          output_channel: integer, the number of output filters.
          kernel_size: integer, the dimension of the convolution window.
          strides: integer, the stride of the convolution.
          expand_ratio: integer, scaling coefficient for the input filters.
          use_se: whether to use se
          se_ratio: float between 0 and 1, fraction to squeeze the input filters.

      Returns:
          output tensor for the block.
      """
    # Expansion phase
    filters = input_channel * expand_ratio
    if expand_ratio != 1:
        x = layers.Conv2D(filters=filters,
                          kernel_size=1,
                          padding="same",
                          use_bias=False,
                          kernel_initializer=CONV_KERNEL_INITIALIZER,
                          name=name + "expand_conv")(inputs)
        x = layers.BatchNormalization(name=name + "expand_bn")(x)
        x = layers.Activation(activation, name=name + "expand_activation")(x)
    else:
        x = inputs

    # Depthwise Convolution
    if strides == 2:
        x = layers.ZeroPadding2D(padding=correct_pad(filters, kernel_size),
                                 name=name + "dwconv_pad")(x)

    x = layers.DepthwiseConv2D(kernel_size=kernel_size,
                               strides=strides,
                               padding="same" if strides == 1 else "valid",
                               use_bias=False,
                               depthwise_initializer=CONV_KERNEL_INITIALIZER,
                               name=name + "dwconv")(x)
    x = layers.BatchNormalization(name=name + "bn")(x)
    x = layers.Activation(activation, name=name + "activation")(x)

    if use_se:
        filters_se = int(input_channel * se_ratio)
        se = layers.GlobalAveragePooling2D(name=name + "se_squeeze")(x)
        se = layers.Reshape((1, 1, filters), name=name + "se_reshape")(se)
        se = layers.Conv2D(filters=filters_se,
                           kernel_size=1,
                           padding="same",
                           activation=activation,
                           kernel_initializer=CONV_KERNEL_INITIALIZER,
                           name=name + "se_reduce")(se)
        se = layers.Conv2D(filters=filters,
                           kernel_size=1,
                           padding="same",
                           activation="sigmoid",
                           kernel_initializer=CONV_KERNEL_INITIALIZER,
                           name=name + "se_expand")(se)
        x = layers.multiply([x, se], name=name + "se_excite")

    # Output phase
    x = layers.Conv2D(filters=output_channel,
                      kernel_size=1,
                      padding="same",
                      use_bias=False,
                      kernel_initializer=CONV_KERNEL_INITIALIZER,
                      name=name + "project_conv")(x)
    x = layers.BatchNormalization(name=name + "project_bn")(x)
    if strides == 1 and input_channel == output_channel:
        if drop_rate > 0:
            x = layers.Dropout(rate=drop_rate,
                               noise_shape=(None, 1, 1, 1),
                               name=name + "drop")(x)
        x = layers.add([x, inputs], name=name + "add")

    return x
Exemplo n.º 5
0
def mb_conv_block(inputs, block_args, activation, drop_rate=None, prefix=''):
    Dropout = get_dropout()

    #-------------------------------------------------#
    #   利用Inverted residuals
    #   part1 利用1x1卷积进行通道数上升
    #-------------------------------------------------#
    filters = block_args.input_filters * block_args.expand_ratio
    if block_args.expand_ratio != 1:
        x = layers.Conv2D(filters,
                          1,
                          padding='same',
                          use_bias=False,
                          kernel_initializer=CONV_KERNEL_INITIALIZER,
                          name=prefix + 'expand_conv')(inputs)
        x = layers.BatchNormalization(momentum=MOMENTUM,
                                      epsilon=EPSILON,
                                      name=prefix + 'expand_bn')(x)
        x = layers.Activation(activation, name=prefix + 'expand_activation')(x)
    else:
        x = inputs

    #------------------------------------------------------#
    #   如果步长为2x2的话,利用深度可分离卷积进行高宽压缩
    #   part2 利用3x3卷积对每一个channel进行卷积
    #------------------------------------------------------#
    x = layers.DepthwiseConv2D(block_args.kernel_size,
                               strides=block_args.strides,
                               padding='same',
                               use_bias=False,
                               depthwise_initializer=CONV_KERNEL_INITIALIZER,
                               name=prefix + 'dwconv')(x)
    x = layers.BatchNormalization(momentum=MOMENTUM,
                                  epsilon=EPSILON,
                                  name=prefix + 'bn')(x)
    x = layers.Activation(activation, name=prefix + 'activation')(x)

    #------------------------------------------------------#
    #   完成深度可分离卷积后
    #   对深度可分离卷积的结果施加注意力机制
    #------------------------------------------------------#
    if 0 < block_args.se_ratio <= 1:
        num_reduced_filters = max(
            1, int(block_args.input_filters * block_args.se_ratio))
        se_tensor = layers.GlobalAveragePooling2D(name=prefix +
                                                  'se_squeeze')(x)
        se_tensor = layers.Reshape((1, 1, filters),
                                   name=prefix + 'se_reshape')(se_tensor)
        #------------------------------------------------------#
        #   通道先压缩后上升,最后利用sigmoid将值固定到0-1之间
        #------------------------------------------------------#
        se_tensor = layers.Conv2D(num_reduced_filters,
                                  1,
                                  activation=activation,
                                  padding='same',
                                  use_bias=True,
                                  kernel_initializer=CONV_KERNEL_INITIALIZER,
                                  name=prefix + 'se_reduce')(se_tensor)
        se_tensor = layers.Conv2D(filters,
                                  1,
                                  activation='sigmoid',
                                  padding='same',
                                  use_bias=True,
                                  kernel_initializer=CONV_KERNEL_INITIALIZER,
                                  name=prefix + 'se_expand')(se_tensor)
        x = layers.multiply([x, se_tensor], name=prefix + 'se_excite')

    #------------------------------------------------------#
    #   part3 利用1x1卷积进行通道下降
    #------------------------------------------------------#
    x = layers.Conv2D(block_args.output_filters,
                      1,
                      padding='same',
                      use_bias=False,
                      kernel_initializer=CONV_KERNEL_INITIALIZER,
                      name=prefix + 'project_conv')(x)
    x = layers.BatchNormalization(momentum=MOMENTUM,
                                  epsilon=EPSILON,
                                  name=prefix + 'project_bn')(x)

    #------------------------------------------------------#
    #   part4 如果满足残差条件,那么就增加残差边
    #------------------------------------------------------#
    if block_args.id_skip and all(
            s == 1 for s in block_args.strides
    ) and block_args.input_filters == block_args.output_filters:
        if drop_rate and (drop_rate > 0):
            x = Dropout(drop_rate,
                        noise_shape=(None, 1, 1, 1),
                        name=prefix + 'drop')(x)
        x = layers.add([x, inputs], name=prefix + 'add')

    return x
Exemplo n.º 6
0
def ResNet(stack_fn,
           preact,
           use_bias,
           model_name='resnet',
           include_top=True,
           weights='imagenet',
           input_tensor=None,
           input_shape=None,
           pooling=None,
           classes=1000,
           **kwargs):
    """Instantiates the ResNet, ResNetV2, and ResNeXt architecture.

    Optionally loads weights pre-trained on ImageNet.
    Note that the data format convention used by the model is
    the one specified in your Keras config at `~/.keras/keras.json`.

    # Arguments
        stack_fn: a function that returns output tensor for the
            stacked residual blocks.
        preact: whether to use pre-activation or not
            (True for ResNetV2, False for ResNet and ResNeXt).
        use_bias: whether to use biases for convolutional layers or not
            (True for ResNet and ResNetV2, False for ResNeXt).
        model_name: string, model name.
        include_top: whether to include the fully-connected
            layer at the top of the network.
        weights: one of `None` (random initialization),
              'imagenet' (pre-training on ImageNet),
              or the path to the weights file to be loaded.
        input_tensor: optional Keras tensor
            (i.e. output of `layers.Input()`)
            to use as image input for the model.
        input_shape: optional shape tuple, only to be specified
            if `include_top` is False (otherwise the input shape
            has to be `(224, 224, 3)` (with `channels_last` data format)
            or `(3, 224, 224)` (with `channels_first` data format).
            It should have exactly 3 inputs channels.
        pooling: optional pooling mode for feature extraction
            when `include_top` is `False`.
            - `None` means that the output of the model will be
                the 4D tensor output of the
                last convolutional layer.
            - `avg` means that global average pooling
                will be applied to the output of the
                last convolutional layer, and thus
                the output of the model will be a 2D tensor.
            - `max` means that global max pooling will
                be applied.
        classes: optional number of classes to classify images
            into, only to be specified if `include_top` is True, and
            if no `weights` argument is specified.

    # Returns
        A Keras model instance.

    # Raises
        ValueError: in case of invalid argument for `weights`,
            or invalid input shape.
    """
    if not (weights in {'imagenet', None} or os.path.exists(weights)):
        raise ValueError('The `weights` argument should be either '
                         '`None` (random initialization), `imagenet` '
                         '(pre-training on ImageNet), '
                         'or the path to the weights file to be loaded.')

    if weights == 'imagenet' and include_top and classes != 1000:
        raise ValueError('If using `weights` as `"imagenet"` with `include_top`'
                         ' as true, `classes` should be 1000')

    if input_tensor is None:
        img_input = layers.Input(shape=input_shape)
    else:
        if not backend.is_keras_tensor(input_tensor):
            img_input = layers.Input(tensor=input_tensor, shape=input_shape)
        else:
            img_input = input_tensor

    bn_axis = 3 if backend.image_data_format() == 'channels_last' else 1

    x = layers.ZeroPadding2D(padding=((3, 3), (3, 3)), name='conv1_pad')(img_input)
    x = layers.Conv2D(64, 7, strides=2, use_bias=use_bias, name='conv1_conv')(x)

    if preact is False:
        x = layers.BatchNormalization(axis=bn_axis, epsilon=1.001e-5,
                                      name='conv1_bn')(x)
        x = layers.Activation('relu', name='conv1_relu')(x)

    x = layers.ZeroPadding2D(padding=((1, 1), (1, 1)), name='pool1_pad')(x)
    x = layers.MaxPooling2D(3, strides=2, name='pool1_pool')(x)

    x = stack_fn(x)

    if preact is True:
        x = layers.BatchNormalization(axis=bn_axis, epsilon=1.001e-5,
                                      name='post_bn')(x)
        x = layers.Activation('relu', name='post_relu')(x)

    if include_top:
        x = layers.GlobalAveragePooling2D(name='avg_pool')(x)
        x = layers.Dense(classes, activation='softmax', name='probs')(x)
    else:
        if pooling == 'avg':
            x = layers.GlobalAveragePooling2D(name='avg_pool')(x)
        elif pooling == 'max':
            x = layers.GlobalMaxPooling2D(name='max_pool')(x)

    # Ensure that the model takes into account
    # any potential predecessors of `input_tensor`.
    if input_tensor is not None:
        inputs = get_source_inputs(input_tensor)
    else:
        inputs = img_input

    # Create model.
    model = models.Model(inputs, x, name=model_name)

    # Load weights.
    if (weights == 'imagenet') and (model_name in WEIGHTS_HASHES):
        if include_top:
            file_name = model_name + '_weights_tf_dim_ordering_tf_kernels.h5'
            file_hash = WEIGHTS_HASHES[model_name][0]
        else:
            file_name = model_name + '_weights_tf_dim_ordering_tf_kernels_notop.h5'
            file_hash = WEIGHTS_HASHES[model_name][1]
        weights_path = get_file(file_name,
                                            BASE_WEIGHTS_PATH + file_name,
                                            cache_subdir='models',
                                            file_hash=file_hash)
        model.load_weights(weights_path)
    elif weights is not None:
        model.load_weights(weights)

    return model
def InceptionMobileNet(include_top=False,
                       weights=None,
                       input_tensor=None,
                       input_shape=None,
                       pooling=None,
                       classes=1000,
                       **kwargs):
    """Instantiates the InceptionMobileNet architecture.

    # Arguments
        include_top: whether to include the fully-connected
            layer at the top of the network.
        weights: must be None.
        input_tensor: Keras tensor (i.e. output of `layers.Input()`)
            to use as image input for the model.
        input_shape: input tensor shape, which is used to create an
            input tensor if `input_tensor` is not specified.
        pooling: Optional pooling mode for feature extraction
            when `include_top` is `False`.
            - `None` means that the output of the model will be
                the 4D tensor output of the last convolutional block.
            - `avg` means that global average pooling will be applied
                to the output of the last convolutional block, and thus
                the output of the model will be a 2D tensor.
            - `max` means that global max pooling will be applied.
        classes: optional number of classes to classify images
            into, only to be specified if `include_top` is True.

    # Returns
        A Keras model instance.

    # Raises
        ValueError: in case of invalid argument for `weights`,
            or invalid input shape.
    """
    if weights is not None:
        raise ValueError('weights is not currently supported')
    if input_tensor is None:
        if input_shape is None:
            raise ValueError('neither input_tensor nor input_shape is given')
        img_input = layers.Input(shape=input_shape)
    else:
        if not backend.is_keras_tensor(input_tensor):
            img_input = layers.Input(tensor=input_tensor, shape=input_shape)
        else:
            img_input = input_tensor

    x = _conv2d_bn(img_input, 32, (3, 3), strides=(2, 2),
                   name='conv1a_s2')  # 1a: 112x112x32
    x = _conv2d_bn(x, 32, (3, 3), name='conv1b')  # 1b: 112x112x32
    x = _conv2d_bn(x, 64, (3, 3), name='conv1c')  # 1c: 112x112x64

    x = _conv2d_bn(x, 128, (3, 3), strides=(2, 2),
                   name='conv2a_s2')  # 2a: 56x56x128
    x = _mixed(x, (32, (32, 32), 48, 16), name='mixed2b')  # 2b: 56x56x128
    x = _mixed(x, (32, (32, 32), 48, 16), name='mixed2c')  # 2c: 56x56x128

    x = _mixed_s2(x, ((64, 64), 64), name='mixed3a_s2')  # 3a: 28x28x256
    x = _mixed(x, (64, (64, 64), 96, 32), name='mixed3b')  # 3b: 28x28x256
    x = _mixed(x, (64, (64, 64), 96, 32), name='mixed3c')  # 3c: 28x28x256
    x = _mixed(x, (64, (64, 96), 96, 32), name='mixed3d')  # 3d: 28x28x256

    x = _mixed_s2(x, ((96, 128), 128), name='mixed4a_s2')  # 4a: 14x14x512
    x = _mixed(x, (128, (96, 128), 192, 64), name='mixed4b')  # 4b: 14x14x512
    x = _mixed(x, (128, (96, 128), 192, 64), name='mixed4c')  # 4c: 14x14x512
    x = _mixed(x, (128, (96, 128), 192, 64), name='mixed4d')  # 4d: 14x14x512

    x = _mixed_s2(x, ((192, 256), 256), name='mixed5a_s2')  # 5a: 7x7x1024
    x = _mixed(x, (256, (192, 256), 384, 128), name='mixed5b')  # 5b: 7x7x1024
    x = _mixed(x, (256, (192, 256), 384, 128), name='mixed5c')  # 5c: 7x7x1024

    if include_top:
        # Classification block
        if pooling == 'avg':
            x = layers.GlobalAveragePooling2D(name='global_pool')(x)
        elif pooling == 'max':
            x = layers.GlobalMaxPooling2D(name='global_pool')(x)
        else:
            raise ValueError('bad spec of global pooling')
        x = layers.Dropout(0.4)(x)
        x = layers.Dense(classes, activation='softmax', name='predictions')(x)

    # Ensure that the model takes into account
    # any potential predecessors of `input_tensor`.
    if input_tensor is not None:
        inputs = keras_utils.get_source_inputs(input_tensor)
    else:
        inputs = img_input

    # Create model.
    model = models.Model(inputs, x, name='inception_mobilenet')

    return model
Exemplo n.º 8
0
def TransferModel(model, input_shape, label_num, aug=None, output_bias=None):
  inputs = {
        'image': tf.keras.Input(input_shape, name='image'),
        # 'degree': tf.keras.Input([degree_num], name='degree'),
  }
  x = inputs['image']
  # d = inputs['degree']

  # d = ly.Embedding(degree_num+1, input_shape[0]*input_shape[1]*input_shape[2],
  #                    input_length=1)(d)
  # d = ly.Reshape(input_shape)(d)
  # x = ly.Add()([x, d])

  if model == "InceptionV3":
    model_body = tf.keras.applications.InceptionV3(input_shape=input_shape, 
      include_top=False, weights=None)
  elif model == "MobileNetV2":
    model_body = tf.keras.applications.MobileNetV2(input_shape=input_shape, 
      include_top=False, weights=None)
  elif model == "MobileNet":
    model_body = tf.keras.applications.MobileNet(input_shape=input_shape, 
      include_top=False, weights=None)
  elif model == "ResNet50V2":
    model_body = tf.keras.applications.ResNet50V2(input_shape=input_shape, 
      include_top=False, weights=None)
  elif model == "ResNet50":
    model_body = tf.keras.applications.ResNet50(input_shape=input_shape, 
      include_top=False, weights=None)
  elif model == "DenseNet121":
    model_body = tf.keras.applications.DenseNet121(input_shape=input_shape, 
      include_top=False, weights=None)
  elif model == "InceptionResNetV2":
    model_body = tf.keras.applications.InceptionResNetV2(input_shape=input_shape, 
      include_top=False, weights=None)
  elif model == "VGG16":
    model_body = tf.keras.applications.VGG16(input_shape=input_shape, 
      include_top=False, weights=None)
  elif model == "Xception":
    model_body = tf.keras.applications.Xception(input_shape=input_shape, 
      include_top=False, weights=None)

  for layer in model_body.layers:
    layer.trainable = True

  # embedded = embedding(d)
  # x = ly.Concatenate()([x, embedded])
  # x = embedded * x
  if aug != None:
    x = aug(x)
  x = model_body(x)
  x = ly.GlobalAveragePooling2D()(x)
  x = ly.Dense(128, name='dense_128')(x)
  x = ly.Activation('relu', name='act_128')(x)
  # x = ly.Add()([x, d])
  # x = ly.Concatenate()([x, d])
  if output_bias != None:
    x = ly.Dense(label_num, name='dense_logits', bias_initializer=output_bias)(x)
  else:
    x = ly.Dense(label_num, name='dense_logits')(x)
  x = ly.Activation('softmax', dtype='float32', name='predictions')(x)

  return tf.keras.Model(inputs=inputs, outputs=x)
Exemplo n.º 9
0
def build_efficient_net_model(block_specs, nn_spec):
    import tensorflow.keras.layers as L
    _kernel_initializer = tf.keras.initializers.VarianceScaling(
        scale=2.0, mode='fan_out', distribution='normal')
    _dense_kernel_initializer = tf.keras.initializers.VarianceScaling(
        scale=1. / 3, mode='fan_out', distribution='uniform')

    inputs = tf.keras.Input(shape=(224, 224, 3))

    x = preprocessing.Rescaling(1. / 255)(inputs)
    x = preprocessing.Normalization()(x)

    x = L.Conv2D(round_filters(32, nn_spec.w, nn_spec.depth_divisor),
                 kernel_size=3,
                 strides=2,
                 kernel_initializer=_kernel_initializer,
                 padding='same',
                 use_bias=False,
                 name='stem_conv')(x)
    x = L.BatchNormalization(name='stem_bn')(x)
    x = L.Activation(nn_spec.activation, name='stem_nonlinear')(x)

    # Several stages of MobileNetV2 blocks
    block_num_sum = sum(
        round_repeats(block_spec.repeat_num, nn_spec.d)
        for block_spec in block_specs) * 1.
    block_index = 0

    for block_stage, block_spec in enumerate(block_specs):
        block_spec = block_spec._replace(
            input_nf=round_filters(block_spec.input_nf, nn_spec.w,
                                   nn_spec.depth_divisor),
            output_nf=round_filters(block_spec.output_nf, nn_spec.w,
                                    nn_spec.depth_divisor),
            repeat_num=round_repeats(block_spec.repeat_num, nn_spec.d))

        for sub_block_index in range(block_spec.repeat_num):
            block_spec = block_spec._replace(
                drop_conn_rate=nn_spec.drop_conn_rate * block_index /
                block_num_sum,
                prefix='block{}{}_'.format(block_stage + 1,
                                           chr(sub_block_index + 97)))
            if sub_block_index > 0:
                block_spec = block_spec._replace(dw_strides=1,
                                                 input_nf=block_spec.output_nf)
            x = mobilenet_v2_block(x, block_spec)
            # self._building_blocks.append(block_spec)
            block_index += 1

    # cnn head
    x = tf.keras.layers.Conv2D(round_filters(1280, nn_spec.w,
                                             nn_spec.depth_divisor),
                               kernel_size=1,
                               strides=1,
                               kernel_initializer=_kernel_initializer,
                               padding='same',
                               use_bias=False,
                               name='head_conv')(x)
    x = L.BatchNormalization(name='head_bn')(x)
    x = L.Activation(nn_spec.activation, name='head_nonlinear')(x)

    if nn_spec.include_top:
        x = L.GlobalAveragePooling2D(name='top_pooling')(x)
        if nn_spec.dropout_rate > 0:
            x = L.Dropout(nn_spec.dropout_rate, name='top_dropout')(x)

        x = tf.keras.layers.Dense(nn_spec.classes,
                                  activation='softmax',
                                  kernel_initializer=_dense_kernel_initializer,
                                  name='probs')(x)
    else:
        if nn_spec.pooling == 'avg':
            x = tf.keras.layers.GlobalAveragePooling2D(name='top_pooling')(x)
        elif nn_spec.pooling == 'max':
            x = tf.keras.layers.GlobalMaxPool2D(name='top_pooling')(x)
        else:
            pass

    model = tf.keras.Model(inputs=[inputs], outputs=[x])

    return model
Exemplo n.º 10
0
    def get_custom_model(self):

        # -------------------------------------------------- 224, 224 --------------------------------------------------
        input = layers.Input(shape=(args.img_height, args.img_width,
                                    args.img_channels))

        # -------------------------------------------------- 224, 224 --------------------------------------------------
        block_1 = self.get_conv_block(input_layer=input, level=1)

        # -------------------------------------------------- 112, 112 --------------------------------------------------
        block_2 = self.get_conv_block(input_layer=block_1, level=2)
        down_2_1 = self.get_downsampling_block(input_layer=block_1,
                                               end_level=2,
                                               start_level=1)
        concat_2 = layers.concatenate([block_2, down_2_1])

        # --------------------------------------------------- 56, 56 ---------------------------------------------------
        block_3 = self.get_conv_block(input_layer=concat_2, level=3)
        down_3_1 = self.get_downsampling_block(input_layer=block_1,
                                               end_level=3,
                                               start_level=1)
        down_3_2 = self.get_downsampling_block(input_layer=block_2,
                                               end_level=3,
                                               start_level=2)
        concat_3 = layers.concatenate([block_3, down_3_1, down_3_2])

        # --------------------------------------------------- 28, 28 ---------------------------------------------------
        block_4 = self.get_conv_block(input_layer=concat_3, level=4)
        down_4_1 = self.get_downsampling_block(input_layer=block_1,
                                               end_level=4,
                                               start_level=1)
        down_4_2 = self.get_downsampling_block(input_layer=block_2,
                                               end_level=4,
                                               start_level=2)
        down_4_3 = self.get_downsampling_block(input_layer=block_3,
                                               end_level=4,
                                               start_level=3)
        concat_4 = layers.concatenate([block_4, down_4_1, down_4_2, down_4_3])

        # --------------------------------------------------- 14, 14 ---------------------------------------------------
        block_5 = self.get_conv_block(input_layer=concat_4, level=5)
        down_5_1 = self.get_downsampling_block(input_layer=block_1,
                                               end_level=5,
                                               start_level=1)
        down_5_2 = self.get_downsampling_block(input_layer=block_2,
                                               end_level=5,
                                               start_level=2)
        down_5_3 = self.get_downsampling_block(input_layer=block_3,
                                               end_level=5,
                                               start_level=3)
        down_5_4 = self.get_downsampling_block(input_layer=block_4,
                                               end_level=5,
                                               start_level=4)
        concat_5 = layers.concatenate(
            [block_5, down_5_1, down_5_2, down_5_3, down_5_4])

        output = layers.GlobalAveragePooling2D()(concat_5)
        model_layer = models.Model(inputs=input,
                                   outputs=output,
                                   name='backbone')
        return model_layer
Exemplo n.º 11
0
def classif_tf(train_x, train_y, test_x, test_y, mlp=True):
    import tensorflow as tf
    from tensorflow.keras import layers

    batch_size = 128

    inputs = layers.Input(shape=(3, 32, 32))
    if not mlp:
        out = layers.Permute((2, 3, 1))(inputs)
        out = layers.Conv2D(32, 3, activation="relu")(out)
        for i in range(3):
            for j in range(3):

                conv = layers.Conv2D(32 * (i + 1),
                                     3,
                                     activation="linear",
                                     padding="SAME")(out)
                bn = layers.BatchNormalization(axis=-1)(conv)
                relu = layers.Activation("relu")(bn)
                conv = layers.Conv2D(32 * (i + 1),
                                     3,
                                     activation="linear",
                                     padding="SAME")(relu)
                bn = layers.BatchNormalization(axis=-1)(conv)

                out = layers.Add()([out, bn])
            out = layers.AveragePooling2D()(out)
            out = layers.Conv2D(32 * (i + 2), 1, activation="linear")(out)
            print(out.shape)
        out = layers.GlobalAveragePooling2D()(out)
    else:
        out = layers.Flatten()(inputs)
        for i in range(6):
            out = layers.Dense(4000, activation="linear")(out)
            bn = layers.BatchNormalization(axis=-1)(out)
            out = layers.Activation("relu")(bn)
    outputs = layers.Dense(10, activation="linear")(out)

    model = tf.keras.Model(inputs, outputs)
    optimizer = tf.keras.optimizers.Adam(learning_rate=0.001)

    for epoch in range(5):
        accu = 0
        for x, y in symjax.data.utils.batchify(train_x,
                                               train_y,
                                               batch_size=batch_size,
                                               option="random"):
            with tf.GradientTape() as tape:
                preds = model(x, training=True)
                loss = tf.reduce_mean(
                    tf.nn.sparse_softmax_cross_entropy_with_logits(y, preds))
            accu += tf.reduce_mean(tf.cast(y == tf.argmax(preds, 1),
                                           "float32"))
            grads = tape.gradient(loss, model.trainable_variables)
            optimizer.apply_gradients(zip(grads, model.trainable_variables))
        print("training", accu / (len(train_x) // batch_size))
        accu = 0
        for x, y in symjax.data.utils.batchify(test_x,
                                               test_y,
                                               batch_size=batch_size,
                                               option="continuous"):
            preds = model(x, training=False)
            accu += tf.reduce_mean(tf.cast(y == tf.argmax(preds, 1),
                                           "float32"))
        print(accu / (len(test_x) // batch_size))
Exemplo n.º 12
0
        subset="validation")

    # =====================================================================
    # CREATE MODEL

    inception_v3_model = keras.applications.inception_v3.InceptionV3(
        input_shape=(200, 200, 3), include_top=False, weights='imagenet')

    inception_v3_model.summary()

    inception_output_layer = inception_v3_model.get_layer('mixed7')
    print('Inception model output shape:', inception_output_layer.output_shape)

    inception_output = inception_v3_model.output

    layers = layers.GlobalAveragePooling2D()(inception_output)
    layers = layers.Dense(1024, activation='relu')(layers)
    layers = layers.Dense(51, activation='softmax')(layers)

    model = Model(inception_v3_model.input, x)

    model.compile(optimizer=SGD(lr=1e-4, momentum=0.9),
                  loss='categorical_crossentropy',
                  metrics=['acc'])
    for layer in model.layers[:249]:
        layer.trainable = False
    for layer in model.layers[249:]:
        layer.trainable = True

    # =====================================================================
    # CREATE CALLBACK
Exemplo n.º 13
0
def build_model(n_classes):  # ResNet50
    def conv_bn_relu(inputs,
                     filters,
                     kernel_size,
                     strides,
                     padding,
                     bn=True,
                     act='relu'):
        x = layers.Conv2D(filters,
                          kernel_size=kernel_size,
                          strides=strides,
                          padding=padding)(inputs)
        if bn:
            x = layers.BatchNormalization()(x)
        if act:
            x = layers.Activation(act)(x)

        return x

    def conv_block(inputs, filters, strides=2, padding='same'):
        shortcut = conv_bn_relu(inputs,
                                filters * 4,
                                1,
                                strides=strides,
                                padding=padding,
                                bn=False,
                                act=None)

        x = conv_bn_relu(inputs, filters, 1, strides=strides, padding=padding)
        x = conv_bn_relu(x, filters, 3, strides=1, padding=padding)
        x = conv_bn_relu(x,
                         filters * 4,
                         1,
                         strides=1,
                         padding=padding,
                         act=None)
        x = layers.Add()([x, shortcut])
        return x

    def identity_block(inputs, filters, strides=1, padding='same'):
        shortcut = inputs
        x = conv_bn_relu(inputs, filters, 1, strides=strides, padding=padding)
        x = conv_bn_relu(x, filters, 3, strides=strides, padding=padding)
        x = conv_bn_relu(x,
                         filters * 4,
                         1,
                         strides=strides,
                         padding=padding,
                         act=None)
        x = layers.Add()([x, shortcut])
        return x

    def res_block(inputs, repeats, filters, strides=2):
        x = conv_block(inputs, filters, strides=strides)
        for i in range(repeats - 1):
            x = identity_block(x, filters)
        return x

    inputs = keras.Input(shape=[224, 224, 3], dtype='float32')
    x = conv_bn_relu(inputs,
                     filters=64,
                     kernel_size=7,
                     strides=2,
                     padding='same')

    x = layers.MaxPool2D(pool_size=3, strides=2, padding='same')(x)
    # [3,4,6,3]
    x = res_block(x, 3, 64, strides=1)
    x = res_block(x, 4, 128)
    x = res_block(x, 6, 256)
    x = res_block(x, 3, 512)

    x = layers.Activation('relu')(x)  # [batch_size,7,7,2048]
    x = layers.GlobalAveragePooling2D()(x)  # keras mode :[batch_size,2048]
    x = layers.Dense(units=512, activation='relu')(x)
    x = layers.Dense(units=128, activation='relu')(x)
    x = layers.Dense(units=32, activation='relu')(x)
    # x = layers.Dense(units=classes,activation='softmax')(x)  # [n,classes]
    x = layers.Dense(units=n_classes)(x)  # logits,[batch_size,n]

    model = Model(inputs=[inputs], outputs=[x])
    # print(model.summary())
    return model
Exemplo n.º 14
0
def MobileNet(input_shape=None,
              alpha=1.0,
              depth_multiplier=1,
              dropout=1e-3,
              classes=4):

    rows = input_shape[0]
    cols = input_shape[1]

    if rows != cols or rows not in [128, 160, 192, 224]:
        rows = 224
        warnings.warn('`input_shape` is undefined or non-square, '
                      'or `rows` is not in [128, 160, 192, 224]. '
                      'Weights for input shape (224, 224) will be'
                      ' loaded as the default.')

    img_input = layers.Input(shape=input_shape)

    x = _conv_block(img_input, 4, alpha, strides=(2, 2))
    x = _depthwise_conv_block(x, 16, alpha, depth_multiplier, block_id=1)

    x = _depthwise_conv_block(x,
                              32,
                              alpha,
                              depth_multiplier,
                              strides=(2, 2),
                              block_id=2)
    # x = _depthwise_conv_block(x, 128, alpha, depth_multiplier, block_id=3)

    # x = _depthwise_conv_block(x, 256, alpha, depth_multiplier,
    #                           strides=(2, 2), block_id=4)
    # x = _depthwise_conv_block(x, 256, alpha, depth_multiplier, block_id=5)

    # x = _depthwise_conv_block(x, 512, alpha, depth_multiplier,
    #                           strides=(2, 2), block_id=6)
    x = _depthwise_conv_block(x, 64, alpha, depth_multiplier, block_id=7)
    # x = _depthwise_conv_block(x, 512, alpha, depth_multiplier, block_id=8)
    # x = _depthwise_conv_block(x, 512, alpha, depth_multiplier, block_id=9)
    # x = _depthwise_conv_block(x, 512, alpha, depth_multiplier, block_id=10)
    # x = _depthwise_conv_block(x, 512, alpha, depth_multiplier, block_id=11)

    x = _depthwise_conv_block(x,
                              128,
                              alpha,
                              depth_multiplier,
                              strides=(2, 2),
                              block_id=12)
    x = _depthwise_conv_block(x, 128, alpha, depth_multiplier, block_id=13)

    shape = (1, 1, int(128 * alpha))
    x = layers.GlobalAveragePooling2D()(x)
    x = layers.Reshape(shape, name='reshape_1')(x)
    x = layers.Dropout(dropout, name='dropout')(x)
    x = layers.Conv2D(classes, (1, 1), padding='same', name='conv_preds')(x)
    x = layers.Reshape((classes, ), name='reshape_2')(x)
    x = layers.Activation('softmax', name='act_softmax')(x)

    model = tf.keras.Model(img_input,
                           x,
                           name='mobilenet_%0.2f_%s' % (alpha, rows))
    return model
Exemplo n.º 15
0
def ResNeXt50_v2(include_top=True,
                 weights=None,
                 input_tensor=None,
                 input_shape=None,
                 pooling=None,
                 classes=1000,
                 **kwargs):
    """Code Adapted from https://github.com/keras-team/keras-applications/
    blob/master/keras_applications/resnet50.py using information found in
    https://arxiv.org/pdf/1611.05431.pdf in order to adapt the resnet50
    architecture into the respective ResNeXt50 architecture. Main difference
    is the implementation of pre-activated grouped convolutions, interpretated
    based on my understanding of the publication. Since this a modified version of
    ResNet50, there are no pre-trained weights available. Code is adapted
    to work with the Tensorflow-Keras backend and not pure Keras.
    # Arguments
        include_top: whether to include the fully-connected
            layer at the top of the network.
        weights: one of `None` (random initialization),
              or the path to the weights file to be loaded.
        input_tensor: optional Keras tensor (i.e. output of `layers.Input()`)
            to use as image input for the model.
        input_shape: optional shape tuple, only to be specified
            if `include_top` is False (otherwise the input shape
            has to be `(224, 224, 3)` (with `channels_last` data format)
            or `(3, 224, 224)` (with `channels_first` data format).
            It should have exactly 3 inputs channels,
            and width and height should be no smaller than 32.
            E.g. `(200, 200, 3)` would be one valid value.
        pooling: Optional pooling mode for feature extraction
            when `include_top` is `False`.
            - `None` means that the output of the model will be
                the 4D tensor output of the
                last convolutional block.
            - `avg` means that global average pooling
                will be applied to the output of the
                last convolutional block, and thus
                the output of the model will be a 2D tensor.
            - `max` means that global max pooling will
                be applied.
        classes: optional number of classes to classify images
            into, only to be specified if `include_top` is True, and
            if no `weights` argument is specified.
    # Returns
        A Keras model instance.
    # Raises
        ValueError: in case of invalid argument for `weights`,
            or invalid input shape.
    """
    global backend, layers, CARDINALITY

    if not (weights in {None} or os.path.exists(weights)):
        raise ValueError('The `weights` argument should be either` '
                         '`None` (random initialization),'
                         'or the path to the weights file to be loaded.')

    if weights == 'imagenet':
        raise ValueError('If using `weights` as `"imagenet"` is not allowed.')

    # Determine proper input shape
    if input_tensor is None:
        img_input = Input(shape=input_shape)
    else:
        if not backend.is_keras_tensor(input_tensor):
            img_input = layers.Input(tensor=input_tensor, shape=input_shape)
        else:
            img_input = input_tensor
    if backend.image_data_format() == 'channels_last':
        bn_axis = 3
    else:
        bn_axis = 1

    CARDINALITY = 32

    x = layers.ZeroPadding2D(padding=(3, 3), name='conv1_pad')(img_input)
    x = layers.Conv2D(64, (7, 7),
                      strides=(2, 2),
                      padding='valid',
                      kernel_initializer='he_normal',
                      name='conv1')(x)
    x = layers.BatchNormalization(axis=bn_axis, name='bn_conv1')(x)
    x = activations.relu(x)
    x = layers.ZeroPadding2D(padding=(1, 1), name='pool1_pad')(x)
    x = layers.MaxPooling2D((3, 3), strides=(2, 2))(x)

    x = conv_block(x, 3, [64, 64, 256], stage=2, block='a', strides=(1, 1))
    x = identity_block(x, 3, [64, 64, 256], stage=2, block='b')
    x = identity_block(x, 3, [64, 64, 256], stage=2, block='c')

    x = conv_block(x, 3, [128, 128, 512], stage=3, block='a')
    x = identity_block(x, 3, [128, 128, 512], stage=3, block='b')
    x = identity_block(x, 3, [128, 128, 512], stage=3, block='c')
    x = identity_block(x, 3, [128, 128, 512], stage=3, block='d')

    x = conv_block(x, 3, [256, 256, 1024], stage=4, block='a')
    x = identity_block(x, 3, [256, 256, 1024], stage=4, block='b')
    x = identity_block(x, 3, [256, 256, 1024], stage=4, block='c')
    x = identity_block(x, 3, [256, 256, 1024], stage=4, block='d')
    x = identity_block(x, 3, [256, 256, 1024], stage=4, block='e')
    x = identity_block(x, 3, [256, 256, 1024], stage=4, block='f')

    x = conv_block(x, 3, [512, 512, 2048], stage=5, block='a')
    x = identity_block(x, 3, [512, 512, 2048], stage=5, block='b')
    x = identity_block(x, 3, [512, 512, 2048], stage=5, block='c')

    x = layers.BatchNormalization(axis=bn_axis, name='bn_conv6')(x)
    x = activations.relu(x)

    if include_top:
        x = layers.GlobalAveragePooling2D(name='avg_pool')(x)
        x = layers.Dense(classes, activation='softmax', name='fc1000')(x)
    else:
        if pooling == 'avg':
            x = layers.GlobalAveragePooling2D()(x)
        elif pooling == 'max':
            x = layers.GlobalMaxPooling2D()(x)
        else:
            warnings.warn('The output shape of `ResNet50(include_top=False)` '
                          'has been changed since Keras 2.2.0.')

    # Create model.
    model = Model(img_input, x, name='resnext50')

    # Load weights.
    if weights == 'imagenet':
        raise ValueError('Imagenet pre-trained weights are not` ' 'available')
    elif weights is not None:
        model.load_weights(weights)

    return model
Exemplo n.º 16
0
                                                  y_col=None,
                                                  shuffle=False,
                                                  class_mode=None,
                                                  target_size=(IMG_SIZE,
                                                               IMG_SIZE),
                                                  batch_size=BATCH_SIZE,
                                                  seed=RANDOM_SEED)
#Model

base_model = efn.EfficientNetB6(weights='imagenet',
                                include_top=False,
                                input_shape=input_shape)
base_model.trainable = False
model = M.Sequential()
model.add(base_model)
model.add(L.GlobalAveragePooling2D(), )
model.add(L.Dense(CLASS_NUM, activation='softmax'))
model.compile(loss="categorical_crossentropy",
              optimizer=optimizers.Adam(lr=0.001),
              metrics=["accuracy"])
model.load_weights('best_model.hdf5')

#Prediction

predictions = model.predict_generator(test_generator,
                                      steps=len(test_generator),
                                      verbose=1)
predictions = np.argmax(predictions, axis=-1)
label_map = ({0: 'female', 1: 'male'})
predictions = [label_map[k] for k in predictions]
prediction = dict(zip(file_names, predictions))
Exemplo n.º 17
0
def squeezenet(num_classes,
               batch_size=None):

    """Instantiates the SqueezeNet architecture.

    Args:
        num_classes: `int` number of classes for image classification.
        batch_size: Size of the batches for each step.

    Returns:
        A Keras model instance.
    """

    input_shape = (227, 227, 3)
    img_input = layers.Input(shape=input_shape, batch_size=batch_size)
    x = img_input

    if backend.image_data_format() == 'channels_first':
        x = layers.Permute((3, 1, 2))(x)
        bn_axis = 1
    else:  # channels_last
        bn_axis = -1

    x = layers.Conv2D(
        filters=96,
        kernel_size=(7, 7),
        strides=(2, 2),
        activation='relu',
        kernel_initializer='he_normal',
        padding='valid',
        name='conv1')(x)
    x = layers.MaxPool2D(
        pool_size=(3, 3),
        strides=(2, 2),
        padding='valid')(x)

    x = fire(x, name='fire2', s1x1=16, e1x1=64, e3x3=64)
    x = fire(x, name='fire3', s1x1=16, e1x1=64, e3x3=64)
    x = fire(x, name='fire4', s1x1=32, e1x1=128, e3x3=128)

    x = layers.MaxPool2D(
        pool_size=(3, 3),
        strides=(2, 2),
        padding='valid')(x)

    x = fire(x, name='fire5', s1x1=32, e1x1=128, e3x3=128)
    x = fire(x, name='fire6', s1x1=48, e1x1=192, e3x3=192)
    x = fire(x, name='fire7', s1x1=48, e1x1=192, e3x3=192)
    x = fire(x, name='fire8', s1x1=64, e1x1=256, e3x3=256)

    x = layers.MaxPool2D(
        pool_size=(3, 3),
        strides=(2, 2),
        padding='valid')(x)

    x = fire(x, name='fire9', s1x1=64, e1x1=256, e3x3=256)

    x = layers.Dropout(rate=0.5)(x)
    x = layers.Conv2D(
        filters=num_classes,
        kernel_size=(1, 1),
        strides=(1, 1),
        activation='relu',
        kernel_initializer='he_normal',
        padding='valid',
        name='conv10')(x)

    x = layers.GlobalAveragePooling2D()(x)

    # A softmax that is followed by the model loss must be done cannot be done
    # in float16 due to numeric issues. So we pass dtype=float32.
    x = layers.Activation('softmax', dtype='float32')(x)

    # Create model.
    return models.Model(img_input, x, name='squeeze')
Exemplo n.º 18
0
def main():
    # 输入:[b, 32, 32, 3]
    model = ResNet18()
    # model = resnet1.ResNet([2, 2, 2], 10)
    model.build(input_shape=(None, 32, 32, 3))
    model.summary()

    mydense = layers.Dense(100, activation=None)
    fc_net = Sequential([mydense])
    fc_net.build(input_shape=(None, 512))
    fc_net.summary()

    lr = 0.1
    optimizer = optimizers.SGD(lr=lr, momentum=0.9, decay=5e-4)
    variables = model.trainable_variables + fc_net.trainable_variables
    for epoch in range(500):

        for step, (x, y) in enumerate(train_db):
            with tf.GradientTape() as tape:
                # [b, 32, 32, 3] => [b, 100]
                out = model(x, training=True)
                avgpool = layers.GlobalAveragePooling2D()(out)
                logits = fc_net(avgpool)
                y_onehot = tf.one_hot(y, depth=100)
                # 多类别交叉熵损失   结果维度[b]
                loss = tf.reduce_mean(
                    tf.losses.categorical_crossentropy(y_onehot,
                                                       logits,
                                                       from_logits=True))
                # 添加正则项,所有可以训练的权重添加l2正则项
                loss_regularization = []
                for p in variables:
                    loss_regularization.append(tf.nn.l2_loss(p))
                loss_regularization = tf.reduce_sum(
                    tf.stack(loss_regularization))
                loss = loss + 5e-4 * loss_regularization

            # 梯度求解
            grads = tape.gradient(loss, variables)
            # 梯度更新
            optimizer.apply_gradients(zip(grads, variables))
            # 学习率动态调整
            lr = lr_schedule_200ep(epoch)
            # 每100个step打印一次
            if step % 100 == 0:
                print('epoch:', epoch, 'step:', step, 'loss:', float(loss),
                      'lr:', lr)

        # 做测试
        total_num = 0
        total_correct = 0
        for x, y in test_db:
            out = model(x, training=False)
            avgpool = layers.GlobalAveragePooling2D()(out)
            output = fc_net(avgpool)
            # 预测可能性。
            prob = tf.nn.softmax(output, axis=1)
            pred = tf.argmax(prob, axis=1)  # 还记得吗pred类型为int64,需要转换一下。
            pred = tf.cast(pred, dtype=tf.int32)
            # 拿到预测值pred和真实值比较。
            correct = tf.cast(tf.equal(pred, y), dtype=tf.int32)
            correct = tf.reduce_sum(correct)
            total_num += x.shape[0]
            total_correct += int(correct)  # 转换为numpy数据

        acc = total_correct / total_num
        print('epoch:', epoch, 'test_acc:', acc)
Exemplo n.º 19
0
    def __init__(self, input_shape, partition_layer, final_layer, **kwargs):
        super(InceptionWJ, self).__init__(**kwargs)
        channel_axis = 3
        img_input = layers.Input(shape=input_shape)
        # partition layer
        flag = False
        self.layer_list = []
        print("##########", img_input)

        if partition_layer == 'input':
            flag = True
            x = conv2d_bn(img_input, 32, 3, 3, strides=(2, 2), padding='valid')

        if flag or partition_layer == 'conv2d':
            if partition_layer == 'conv2d':
                x = img_input
                flag = True
            x = conv2d_bn(x, 32, 3, 3, padding='valid')

        if flag or partition_layer == 'conv2d_1':
            if partition_layer == 'conv2d_1':
                x = img_input
                flag = True
            x = conv2d_bn(x, 64, 3, 3)

        if flag or partition_layer == 'conv2d_2':
            if partition_layer == 'conv2d_2':
                x = img_input
                flag = True
            x = layers.MaxPooling2D((3, 3), strides=(2, 2))(x)
        if flag or partition_layer == 'max_pooling2d':
            if partition_layer == 'max_pooling2d':
                x = img_input
                flag = True
            x = conv2d_bn(x, 80, 1, 1, padding='valid')
        if flag or partition_layer == 'conv2d_3':
            if partition_layer == 'conv2d_3':
                x = img_input
                flag = True
            x = conv2d_bn(x, 192, 3, 3, padding='valid')
            print("==========partition_layer", partition_layer)
        if flag or partition_layer == 'conv2d_4':
            if partition_layer == 'conv2d_4':
                x = img_input
                flag = True
            x = layers.MaxPooling2D((3, 3), strides=(2, 2))(x)
        if flag or partition_layer == 'max_pooling2d_1':
            # print("=====================partition_layer", partition_layer)
            if partition_layer == 'max_pooling2d_1':
                x = img_input
                flag = True

            # mixed 0: 35 x 35 x 256
            branch1x1 = conv2d_bn(x, 64, 1, 1)
            branch5x5 = conv2d_bn(x, 48, 1, 1)
            branch5x5 = conv2d_bn(branch5x5, 64, 5, 5)

            branch3x3dbl = conv2d_bn(x, 64, 1, 1)
            branch3x3dbl = conv2d_bn(branch3x3dbl, 96, 3, 3)
            branch3x3dbl = conv2d_bn(branch3x3dbl, 96, 3, 3)

            branch_pool = layers.AveragePooling2D((3, 3),
                                                  strides=(1, 1),
                                                  padding='same')(x)
            branch_pool = conv2d_bn(branch_pool, 32, 1, 1)
            x = layers.concatenate(
                [branch1x1, branch5x5, branch3x3dbl, branch_pool],
                axis=channel_axis,
                name='mixed0')
        if flag or partition_layer == 'mixed0':

            if partition_layer == 'mixed0':
                x = img_input
                flag = True
            # mixed 1: 35 x 35 x 288
            branch1x1 = conv2d_bn(x, 64, 1, 1)

            branch5x5 = conv2d_bn(x, 48, 1, 1)
            branch5x5 = conv2d_bn(branch5x5, 64, 5, 5)

            branch3x3dbl = conv2d_bn(x, 64, 1, 1)
            branch3x3dbl = conv2d_bn(branch3x3dbl, 96, 3, 3)
            branch3x3dbl = conv2d_bn(branch3x3dbl, 96, 3, 3)

            branch_pool = layers.AveragePooling2D((3, 3),
                                                  strides=(1, 1),
                                                  padding='same')(x)
            branch_pool = conv2d_bn(branch_pool, 64, 1, 1)
            x = layers.concatenate(
                [branch1x1, branch5x5, branch3x3dbl, branch_pool],
                axis=channel_axis,
                name='mixed1')
        if flag or partition_layer == 'mixed1':
            if partition_layer == 'mixed1':
                x = img_input
                flag = True
            # mixed 2: 35 x 35 x 288
            branch1x1 = conv2d_bn(x, 64, 1, 1)

            branch5x5 = conv2d_bn(x, 48, 1, 1)
            branch5x5 = conv2d_bn(branch5x5, 64, 5, 5)

            branch3x3dbl = conv2d_bn(x, 64, 1, 1)
            branch3x3dbl = conv2d_bn(branch3x3dbl, 96, 3, 3)
            branch3x3dbl = conv2d_bn(branch3x3dbl, 96, 3, 3)

            branch_pool = layers.AveragePooling2D((3, 3),
                                                  strides=(1, 1),
                                                  padding='same')(x)
            branch_pool = conv2d_bn(branch_pool, 64, 1, 1)
            x = layers.concatenate(
                [branch1x1, branch5x5, branch3x3dbl, branch_pool],
                axis=channel_axis,
                name='mixed2')
        if flag or partition_layer == 'mixed2':
            if partition_layer == 'mixed2':
                x = img_input
                flag = True
            # mixed 3: 17 x 17 x 768
            branch3x3 = conv2d_bn(x,
                                  384,
                                  3,
                                  3,
                                  strides=(2, 2),
                                  padding='valid')

            branch3x3dbl = conv2d_bn(x, 64, 1, 1)
            branch3x3dbl = conv2d_bn(branch3x3dbl, 96, 3, 3)
            branch3x3dbl = conv2d_bn(branch3x3dbl,
                                     96,
                                     3,
                                     3,
                                     strides=(2, 2),
                                     padding='valid')

            branch_pool = layers.MaxPooling2D((3, 3), strides=(2, 2))(x)
            x = layers.concatenate([branch3x3, branch3x3dbl, branch_pool],
                                   axis=channel_axis,
                                   name='mixed3')
        if flag or partition_layer == 'mixed3':
            if partition_layer == 'mixed3':
                x = img_input
                flag = True
            # mixed 4: 17 x 17 x 768
            branch1x1 = conv2d_bn(x, 192, 1, 1)

            branch7x7 = conv2d_bn(x, 128, 1, 1)
            branch7x7 = conv2d_bn(branch7x7, 128, 1, 7)
            branch7x7 = conv2d_bn(branch7x7, 192, 7, 1)

            branch7x7dbl = conv2d_bn(x, 128, 1, 1)
            branch7x7dbl = conv2d_bn(branch7x7dbl, 128, 7, 1)
            branch7x7dbl = conv2d_bn(branch7x7dbl, 128, 1, 7)
            branch7x7dbl = conv2d_bn(branch7x7dbl, 128, 7, 1)
            branch7x7dbl = conv2d_bn(branch7x7dbl, 192, 1, 7)

            branch_pool = layers.AveragePooling2D((3, 3),
                                                  strides=(1, 1),
                                                  padding='same')(x)
            branch_pool = conv2d_bn(branch_pool, 192, 1, 1)
            x = layers.concatenate(
                [branch1x1, branch7x7, branch7x7dbl, branch_pool],
                axis=channel_axis,
                name='mixed4')

        # mixed 5, 6: 17 x 17 x 768

        if flag or partition_layer == 'mixed4':
            if partition_layer == 'mixed4':
                x = img_input
                flag = True
            branch1x1 = conv2d_bn(x, 192, 1, 1)

            branch7x7 = conv2d_bn(x, 160, 1, 1)
            branch7x7 = conv2d_bn(branch7x7, 160, 1, 7)
            branch7x7 = conv2d_bn(branch7x7, 192, 7, 1)

            branch7x7dbl = conv2d_bn(x, 160, 1, 1)
            branch7x7dbl = conv2d_bn(branch7x7dbl, 160, 7, 1)
            branch7x7dbl = conv2d_bn(branch7x7dbl, 160, 1, 7)
            branch7x7dbl = conv2d_bn(branch7x7dbl, 160, 7, 1)
            branch7x7dbl = conv2d_bn(branch7x7dbl, 192, 1, 7)

            branch_pool = layers.AveragePooling2D((3, 3),
                                                  strides=(1, 1),
                                                  padding='same')(x)
            branch_pool = conv2d_bn(branch_pool, 192, 1, 1)
            x = layers.concatenate(
                [branch1x1, branch7x7, branch7x7dbl, branch_pool],
                axis=channel_axis,
                name='mixed' + str(5))

        if flag or partition_layer == 'mixed5':
            if partition_layer == 'mixed5':
                x = img_input
                flag = True
            branch1x1 = conv2d_bn(x, 192, 1, 1)

            branch7x7 = conv2d_bn(x, 160, 1, 1)
            branch7x7 = conv2d_bn(branch7x7, 160, 1, 7)
            branch7x7 = conv2d_bn(branch7x7, 192, 7, 1)

            branch7x7dbl = conv2d_bn(x, 160, 1, 1)
            branch7x7dbl = conv2d_bn(branch7x7dbl, 160, 7, 1)
            branch7x7dbl = conv2d_bn(branch7x7dbl, 160, 1, 7)
            branch7x7dbl = conv2d_bn(branch7x7dbl, 160, 7, 1)
            branch7x7dbl = conv2d_bn(branch7x7dbl, 192, 1, 7)

            branch_pool = layers.AveragePooling2D((3, 3),
                                                  strides=(1, 1),
                                                  padding='same')(x)
            branch_pool = conv2d_bn(branch_pool, 192, 1, 1)
            x = layers.concatenate(
                [branch1x1, branch7x7, branch7x7dbl, branch_pool],
                axis=channel_axis,
                name='mixed' + str(6))
        if flag or partition_layer == 'mixed6':
            if partition_layer == 'mixed6':
                flag = True
                x = img_input
            # mixed 7: 17 x 17 x 768
            self.layer_list.append()
            branch1x1 = conv2d_bn(x, 192, 1, 1)

            branch7x7 = conv2d_bn(x, 192, 1, 1)
            branch7x7 = conv2d_bn(branch7x7, 192, 1, 7)
            branch7x7 = conv2d_bn(branch7x7, 192, 7, 1)

            branch7x7dbl = conv2d_bn(x, 192, 1, 1)
            branch7x7dbl = conv2d_bn(branch7x7dbl, 192, 7, 1)
            branch7x7dbl = conv2d_bn(branch7x7dbl, 192, 1, 7)
            branch7x7dbl = conv2d_bn(branch7x7dbl, 192, 7, 1)
            branch7x7dbl = conv2d_bn(branch7x7dbl, 192, 1, 7)

            branch_pool = layers.AveragePooling2D((3, 3),
                                                  strides=(1, 1),
                                                  padding='same')(x)
            branch_pool = conv2d_bn(branch_pool, 192, 1, 1)
            x = layers.concatenate(
                [branch1x1, branch7x7, branch7x7dbl, branch_pool],
                axis=channel_axis,
                name='mixed7')
        if flag or partition_layer == 'mixed7':
            if partition_layer == 'mixed7':
                x = img_input
                flag = True
            # mixed 8: 8 x 8 x 1280
            branch3x3 = conv2d_bn(x, 192, 1, 1)
            branch3x3 = conv2d_bn(branch3x3,
                                  320,
                                  3,
                                  3,
                                  strides=(2, 2),
                                  padding='valid')

            branch7x7x3 = conv2d_bn(x, 192, 1, 1)
            branch7x7x3 = conv2d_bn(branch7x7x3, 192, 1, 7)
            branch7x7x3 = conv2d_bn(branch7x7x3, 192, 7, 1)
            branch7x7x3 = conv2d_bn(branch7x7x3,
                                    192,
                                    3,
                                    3,
                                    strides=(2, 2),
                                    padding='valid')

            branch_pool = layers.MaxPooling2D((3, 3), strides=(2, 2))(x)
            x = layers.concatenate([branch3x3, branch7x7x3, branch_pool],
                                   axis=channel_axis,
                                   name='mixed8')

        # mixed 9: 8 x 8 x 2048
        i = 0
        if flag or partition_layer == 'mixed8':
            if partition_layer == 'mixed8':
                x = img_input
                flag = True
            branch1x1 = conv2d_bn(x, 320, 1, 1)

            branch3x3 = conv2d_bn(x, 384, 1, 1)
            branch3x3_1 = conv2d_bn(branch3x3, 384, 1, 3)
            branch3x3_2 = conv2d_bn(branch3x3, 384, 3, 1)
            branch3x3 = layers.concatenate([branch3x3_1, branch3x3_2],
                                           axis=channel_axis,
                                           name='mixed9_' + str(i))

            branch3x3dbl = conv2d_bn(x, 448, 1, 1)
            branch3x3dbl = conv2d_bn(branch3x3dbl, 384, 3, 3)
            branch3x3dbl_1 = conv2d_bn(branch3x3dbl, 384, 1, 3)
            branch3x3dbl_2 = conv2d_bn(branch3x3dbl, 384, 3, 1)
            branch3x3dbl = layers.concatenate([branch3x3dbl_1, branch3x3dbl_2],
                                              axis=channel_axis)

            branch_pool = layers.AveragePooling2D((3, 3),
                                                  strides=(1, 1),
                                                  padding='same')(x)
            branch_pool = conv2d_bn(branch_pool, 192, 1, 1)
            x = layers.concatenate(
                [branch1x1, branch3x3, branch3x3dbl, branch_pool],
                axis=channel_axis,
                name='mixed' + str(9 + i))
        i = 1
        if flag or partition_layer == 'mixed9':
            if partition_layer == 'mixed9':
                x = img_input
                flag = True
            branch1x1 = conv2d_bn(x, 320, 1, 1)

            branch3x3 = conv2d_bn(x, 384, 1, 1)
            branch3x3_1 = conv2d_bn(branch3x3, 384, 1, 3)
            branch3x3_2 = conv2d_bn(branch3x3, 384, 3, 1)
            branch3x3 = layers.concatenate([branch3x3_1, branch3x3_2],
                                           axis=channel_axis,
                                           name='mixed9_' + str(i))

            branch3x3dbl = conv2d_bn(x, 448, 1, 1)
            branch3x3dbl = conv2d_bn(branch3x3dbl, 384, 3, 3)
            branch3x3dbl_1 = conv2d_bn(branch3x3dbl, 384, 1, 3)
            branch3x3dbl_2 = conv2d_bn(branch3x3dbl, 384, 3, 1)
            branch3x3dbl = layers.concatenate([branch3x3dbl_1, branch3x3dbl_2],
                                              axis=channel_axis)

            branch_pool = layers.AveragePooling2D((3, 3),
                                                  strides=(1, 1),
                                                  padding='same')(x)
            branch_pool = conv2d_bn(branch_pool, 192, 1, 1)
            x = layers.concatenate(
                [branch1x1, branch3x3, branch3x3dbl, branch_pool],
                axis=channel_axis,
                name='mixed' + str(9 + i))
        if partition_layer == 'mixed10' or flag:
            if partition_layer == 'mixed10':
                x = img_input
                flag = True

        x = layers.GlobalAveragePooling2D(name='avg_pool')(x)
        x = layers.Dense(1000, activation="softmax", name='predictions')(x)
        #model = tf.keras.Model(img_input, x, name='inception_v3')
        return x
Exemplo n.º 20
0
  else:r_num=random.randint(0,len(a)-1)   
  print('랜덤 index number:',r_num+1)                   
  output=a[r_num]   
  me=output #현 output에 붙은 최말단 레이어:me index:r_num
  #random
  short_cut_dec=random.randint(1,5)             #40%확률적으로 shortcut
  if (short_cut_dec==1 or short_cut_dec==2) and len(net_list)>1:
    add_num=add_num+1
    for _ in range( random.randint(0,len(net_list)-1) ): #random개만큼 add한다
      a_layer_num=random.randint(0,len(net_list)-1)
      if a_layer_num!=r_num:
        c=layers.Add()([net_list[a_layer_num],output])
        output=c
        net_list.append(output)

output = layers.GlobalAveragePooling2D()(output)
output = layers.Dense(1000, activation='relu')(output)
dropout = layers.Dropout(rate=0.25)(output)
output = layers.Dense(10, activation='softmax')(dropout)

model = keras.Model(inputs=inputs, outputs=output)

model.summary()

model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])

history = model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, verbose=1, validation_data=(x_test, y_test),callbacks=[checkpoint,early_stopping])

score = model.evaluate(x_test, y_test, verbose=0)
print('Test loss:',  score[0])
print('Test accuracy:', score[1])
Exemplo n.º 21
0
def efficient_net(width_coefficient,
                  depth_coefficient,
                  input_shape=(224, 224, 3),
                  dropout_rate=0.2,
                  drop_connect_rate=0.2,
                  activation="swish",
                  model_name="efficientnet",
                  include_top=True,
                  num_classes=1000):
    """Instantiates the EfficientNet architecture using given scaling coefficients.

      Reference:
      - [EfficientNet: Rethinking Model Scaling for Convolutional Neural Networks](
          https://arxiv.org/abs/1905.11946) (ICML 2019)

      Optionally loads weights pre-trained on ImageNet.
      Note that the data format convention used by the model is
      the one specified in your Keras config at `~/.keras/keras.json`.

      Arguments:
        width_coefficient: float, scaling coefficient for network width.
        depth_coefficient: float, scaling coefficient for network depth.
        input_shape: tuple, default input image shape(not including the batch size).
        dropout_rate: float, dropout rate before final classifier layer.
        drop_connect_rate: float, dropout rate at skip connections.
        activation: activation function.
        model_name: string, model name.
        include_top: whether to include the fully-connected
            layer at the top of the network.
        num_classes: optional number of classes to classify images
            into, only to be specified if `include_top` is True, and
            if no `weights` argument is specified.

      Returns:
        A `keras.Model` instance.
    """

    # kernel_size, repeats, in_channel, out_channel, exp_ratio, strides, SE
    block_args = [[3, 1, 32, 16, 1, 1, True],
                  [3, 2, 16, 24, 6, 2, True],
                  [5, 2, 24, 40, 6, 2, True],
                  [3, 3, 40, 80, 6, 2, True],
                  [5, 3, 80, 112, 6, 1, True],
                  [5, 4, 112, 192, 6, 2, True],
                  [3, 1, 192, 320, 6, 1, True]]

    def round_filters(filters, divisor=8):
        """Round number of filters based on depth multiplier."""
        filters *= width_coefficient
        new_filters = max(divisor, int(filters + divisor / 2) // divisor * divisor)
        # Make sure that round down does not go down by more than 10%.
        if new_filters < 0.9 * filters:
            new_filters += divisor
        return int(new_filters)

    def round_repeats(repeats):
        """Round number of repeats based on depth multiplier."""
        return int(math.ceil(depth_coefficient * repeats))

    img_input = layers.Input(shape=input_shape)

    # data preprocessing
    x = layers.experimental.preprocessing.Rescaling(1. / 255.)(img_input)
    x = layers.experimental.preprocessing.Normalization()(x)

    # first conv2d
    x = layers.ZeroPadding2D(padding=correct_pad(input_shape[:2], 3),
                             name="stem_conv_pad")(x)
    x = layers.Conv2D(filters=round_filters(32),
                      kernel_size=3,
                      strides=2,
                      padding="valid",
                      use_bias=False,
                      kernel_initializer=CONV_KERNEL_INITIALIZER,
                      name="stem_conv")(x)
    x = layers.BatchNormalization(name="stem_bn")(x)
    x = layers.Activation(activation, name="stem_activation")(x)

    # build blocks
    b = 0
    num_blocks = float(sum(round_repeats(i[1]) for i in block_args))
    for i, args in enumerate(block_args):
        assert args[1] > 0
        # Update block input and output filters based on depth multiplier.
        args[2] = round_filters(args[2])  # input_channel
        args[3] = round_filters(args[3])  # output_channel

        for j in range(round_repeats(args[1])):
            x = block(x,
                      activation=activation,
                      drop_rate=drop_connect_rate * b / num_blocks,
                      name="block{}{}_".format(i + 1, chr(j + 97)),
                      kernel_size=args[0],
                      input_channel=args[2] if j == 0 else args[3],
                      output_channel=args[3],
                      expand_ratio=args[4],
                      strides=args[5] if j == 0 else 1,
                      use_se=args[6])
            b += 1

    # build top
    x = layers.Conv2D(round_filters(1280),
                      kernel_size=1,
                      padding="same",
                      use_bias=False,
                      kernel_initializer=CONV_KERNEL_INITIALIZER,
                      name="top_conv")(x)
    x = layers.BatchNormalization(name="top_bn")(x)
    x = layers.Activation(activation, name="top_activation")(x)
    if include_top:
        x = layers.GlobalAveragePooling2D(name="avg_pool")(x)
        if dropout_rate > 0:
            x = layers.Dropout(dropout_rate, name="top_dropout")(x)
        x = layers.Dense(units=num_classes,
                         activation="softmax",
                         kernel_initializer=DENSE_KERNEL_INITIALIZER,
                         name="predictions")(x)

    model = Model(img_input, x, name=model_name)

    return model
Exemplo n.º 22
0
def ResNet50(inputs,
             preact=False,
             use_bias=True,
             model_name='resnet50',
             include_top=False,
             pooling='avg',
             classes=1000,
             return_feature_maps=True,
             return_last_map=False):
    """Instantiates the ResNet, ResNetV2, and ResNeXt architecture.

    Optionally loads weights pre-trained on ImageNet.
    Note that the data format convention used by the model is
    the one specified in your Keras config at `~/.keras/keras.json`.

    # Arguments
        stack_fn: a function that returns output tensor for the
            stacked residual blocks.
        preact: whether to use pre-activation or not
            (True for ResNetV2, False for ResNet and ResNeXt).
        use_bias: whether to use biases for convolutional layers or not
            (True for ResNet and ResNetV2, False for ResNeXt).
        model_name: string, model name.
        include_top: whether to include the fully-connected
            layer at the top of the network.
        weights: one of `None` (random initialization),
              'imagenet' (pre-training on ImageNet),
              or the path to the weights file to be loaded.
        input_tensor: optional Keras tensor
            (i.e. output of `layers.Input()`)
            to use as image input for the model.
        input_shape: optional shape tuple, only to be specified
            if `include_top` is False (otherwise the input shape
            has to be `(224, 224, 3)` (with `channels_last` data format)
            or `(3, 224, 224)` (with `channels_first` data format).
            It should have exactly 3 inputs channels.
        pooling: optional pooling mode for feature extraction
            when `include_top` is `False`.
            - `None` means that the output of the model will be
                the 4D tensor output of the
                last convolutional layer.
            - `avg` means that global average pooling
                will be applied to the output of the
                last convolutional layer, and thus
                the output of the model will be a 2D tensor.
            - `max` means that global max pooling will
                be applied.
        classes: optional number of classes to classify images
            into, only to be specified if `include_top` is True, and
            if no `weights` argument is specified.

    # Returns
        A Keras model instance.

    # Raises
        ValueError: in case of invalid argument for `weights`,
            or invalid input shape.
    """
    # global backend, layers, models, keras_utils
    # backend, layers, models, keras_utils = get_submodules_from_kwargs(kwargs)

    # Determine proper input shape
    bn_axis = 3

    x = layers.ZeroPadding2D(padding=((3, 3), (3, 3)), name='conv1_pad')(inputs)
    x = layers.Conv2D(64, 7, strides=2, use_bias=use_bias, name='conv1_conv')(x)

    if preact is False:
        x = layers.BatchNormalization(axis=bn_axis, epsilon=1.001e-5,
                                      name='conv1_bn')(x)
        x = layers.Activation('relu', name='conv1_relu')(x)

    x = layers.ZeroPadding2D(padding=((1, 1), (1, 1)), name='pool1_pad')(x)
    x = layers.MaxPooling2D(3, strides=2, name='pool1_pool')(x)

    outputs = []
    x = stack1(x, 64, 3, stride1=1, name='conv2')
    outputs.append(x)

    x = stack1(x, 128, 4, name='conv3')
    outputs.append(x)

    x = stack1(x, 256, 6, name='conv4')
    outputs.append(x)

    x = stack1(x, 512, 3, name='conv5')
    outputs.append(x)
    # x = stack_fn(x)

    if return_last_map:
        # x_shape = x.get_shape()
        # s_shape = x_shape[1]*x_shape[2]
        # x = tf.reshape(x, [tf.shape(x)[0], x_shape[1] * x_shape[2], x_shape[-1]])
        # x = tf.reshape(x, [tf.shape(x)[0], tf.shape(x)[1] * tf.shape(x)[2], tf.shape(x)[-1]])
        model = models.Model(inputs, x, name='last_map')
        return model

    if return_feature_maps:
        model = models.Model(inputs, outputs, name=model_name)
        return model

    if preact is True:
        x = layers.BatchNormalization(axis=bn_axis, epsilon=1.001e-5,
                                      name='post_bn')(x)
        x = layers.Activation('relu', name='post_relu')(x)

    if include_top:
        x = layers.GlobalAveragePooling2D(name='avg_pool')(x)
        x = layers.Dense(classes, activation='softmax', name='probs')(x)
    else:
        if pooling == 'avg':
            x = layers.GlobalAveragePooling2D(name='avg_pool')(x)
        elif pooling == 'max':
            x = layers.GlobalMaxPooling2D(name='max_pool')(x)
        x = layers.Dense(1, activation='linear', name='final_fc')(x)

    # Create model.
    model = models.Model(inputs, x, name=model_name)

    return model
Exemplo n.º 23
0
def EfficientNet(input_shape,
                 block_args_list,
                 global_params,
                 include_top=True,
                 input_tensor=None):
    batch_norm_momentum = global_params.batch_norm_momentum
    batch_norm_epsilon = global_params.batch_norm_epsilon
    l2_reg = global_params.l2_reg

    if global_params.data_format == 'channels_first':
        channel_axis = 1
    else:
        channel_axis = -1

    # Stem part
    inputs = KL.Input(shape=input_shape) if input_tensor is None else KL.Input(
        tensor=input_tensor)
    x = inputs
    x = KL.Conv2D(filters=round_filters(32, global_params),
                  kernel_size=[3, 3],
                  strides=[2, 2],
                  kernel_initializer=conv_kernel_initializer,
                  padding='same',
                  use_bias=False,
                  kernel_regularizer=l2(l2_reg))(x)
    x = KL.BatchNormalization(axis=channel_axis,
                              momentum=batch_norm_momentum,
                              epsilon=batch_norm_epsilon)(x)
    x = KL.ReLU()(x)
    # x = Swish()(x)

    # Blocks part
    for block_args in block_args_list:
        assert block_args.num_repeat > 0
        # Update block input and output filters based on depth multiplier.
        block_args = block_args._replace(
            input_filters=round_filters(block_args.input_filters,
                                        global_params),
            output_filters=round_filters(block_args.output_filters,
                                         global_params),
            num_repeat=round_repeats(block_args.num_repeat, global_params))

        # The first block needs to take care of stride and filter size increase.
        x = MBConvBlock(block_args, global_params)(x)

        if block_args.num_repeat > 1:
            block_args = block_args._replace(
                input_filters=block_args.output_filters, strides=[1, 1])

        for _ in xrange(block_args.num_repeat - 1):
            x = MBConvBlock(block_args, global_params)(x)

    # Head part
    x = KL.Conv2D(filters=round_filters(1280, global_params),
                  kernel_size=[1, 1],
                  strides=[1, 1],
                  kernel_initializer=conv_kernel_initializer,
                  padding='same',
                  use_bias=False,
                  kernel_regularizer=l2(l2_reg))(x)
    x = KL.BatchNormalization(axis=channel_axis,
                              momentum=batch_norm_momentum,
                              epsilon=batch_norm_epsilon)(x)
    x = KL.ReLU()(x)
    # x = Swish()(x)

    if include_top:
        x = KL.GlobalAveragePooling2D(data_format=global_params.data_format)(x)
        if global_params.dropout_rate > 0:
            x = KL.Dropout(global_params.dropout_rate)(x)
        x = KL.Dense(global_params.num_classes,
                     kernel_initializer=dense_kernel_initializer,
                     kernel_regularizer=l2(l2_reg))(x)
        x = KL.Activation('softmax')(x)

    outputs = x
    model = KM.Model(inputs, outputs)

    return model
    def __init__(self,   config, 
                         dataset,
                         num_run):

        
        #############################################################################################
        # LIBRARIES
        #############################################################################################        
        import os
        import numpy as np
        import tensorflow as tf
        from PIL import ImageFont
        from tensorflow.python import pywrap_tensorflow
        from tensorflow.keras import optimizers, losses, models, backend, layers, metrics
        
        self.run_path = os.path.dirname(os.path.realpath(__file__))
        os.chdir(self.run_path)
        
        utils         = local_module("utils")
        logger        = local_module("logger")
        lossnet       = local_module("lossnet")
        data_pipeline = local_module("data_pipeline")
        backbones     = local_module("backbones")
        
        self.font_size = 12
        self.font = ImageFont.truetype(os.path.join(self.run_path,"miscellaneous","InconsolataGo-Nerd-Font-Complete-Mono.ttf"), size=self.font_size)
        
        #############################################################################################
        # PARAMETERS RUN
        #############################################################################################
        self.number_images_vid  = 30
        
        self.config          = config
        self.dataset         = dataset
        self.num_run         = num_run
        self.group           = "Stage_"+str(num_run)
        self.name_run        = "Test"+self.group 
        
        self.run_dir         = os.path.join(config["PROJECT"]["group_dir"],self.group)
        self.run_dir_check   = os.path.join(self.run_dir ,'checkpoints')
        self.checkpoints_path= os.path.join(self.run_dir_check,'checkpoint.{epoch:03d}.hdf5')
        self.user            = get_user()
        
        self.transfer_weight_path = self.config['TRAIN']["transfer_weight_path"]
        self.input_shape       = [self.config["NETWORK"]["INPUT_SIZE"], self.config["NETWORK"]["INPUT_SIZE"], 3]
        
        self.pre ='\033[1;36m' + self.name_run + '_' + config["PROJECT"]["group"]  + '\033[0;0m' #"____" #
        self.problem ='\033[1;31m' + self.name_run+ '_' + config["PROJECT"]["group"] + '\033[0;0m'
        
        # Creating the test folder
        self.evaluation_folder = os.path.join(self.run_dir, 'evaluation')
        self.evaluation_file   = os.path.join(self.evaluation_folder, "accuracy_epoch.csv")
        
        try:
            os.mkdir(self.evaluation_folder)
        except:
            pass

        
        #############################################################################################
        # SETUP TENSORFLOW SESSION
        #############################################################################################

        self.graph = tf.Graph()
        with self.graph.as_default():
            config_tf = tf.ConfigProto(allow_soft_placement=True) 
            config_tf.gpu_options.allow_growth = True 
            self.sess = tf.Session(config=config_tf,graph=self.graph)
            with self.sess.as_default():

                #############################################################################################
                # SETUP WANDB
                #############################################################################################
                import wandb

                self.wandb = wandb
                self.run_wandb = self.wandb.init(project  = config["PROJECT"]["project"], 
                                                group    = config["PROJECT"]["group"], 
                                                name     = "Test_"+str(num_run),
                                                job_type = self.group ,
                                                sync_tensorboard = True,
                                                config = config)

                #############################################################################################
                # LOAD DATA
                #############################################################################################
                self.DataGen = data_pipeline.ClassificationDataset( config["TEST"]["batch_size"],
                                                                    self.dataset,
                                                                    subset = "test",
                                                                    original_size      = config["DATASET"]["original_size"],
                                                                    data_augmentation  = False)  
                
                
                self.num_class = len(self.DataGen.list_classes)
                
                #############################################################################################
                # GLOBAL PROGRESS
                #############################################################################################
                self.steps_per_epoch  = int(np.ceil(self.DataGen.nb_elements/config["TEST"]["batch_size"]))
                self.total_epochs  = self.config['TRAIN']["EPOCH_WHOLE"] + self.config['TRAIN']["EPOCH_SLIT"]
                self.total_tests = int(np.floor(self.total_epochs/self.config['TRAIN']["test_each"]))
                print(self.pre,'Number of elements in the test set', self.DataGen.nb_elements)
                
                
                #############################################################################################
                # DEFINE CLASSIFIER
                #############################################################################################
                # set input
                img_input = tf.keras.Input(tensor=self.DataGen.images_tensor ,name= 'input_image')
                #img_input = tf.keras.Input(self.input_shape,name= 'input_image')
                
                include_top = True

                # Get the selected backbone
                """
                ResNet18
                ResNet50
                ResNet101
                ResNet152
                ResNet50V2
                ResNet101V2
                ResNet152V2
                ResNeXt50
                ResNeXt101
                """
                print(self.pre, "The backbone is: ",self.config["NETWORK"]["Backbone"])
                self.backbone = getattr(backbones,self.config["NETWORK"]["Backbone"])
                #
                c_pred_features = self.backbone(input_tensor=img_input, classes= self.num_class, include_top=include_top)
                self.c_pred_features=  c_pred_features
                if include_top: # include top classifier
                    # class predictions
                    c_pred = c_pred_features[0]
                else:
                    x = layers.GlobalAveragePooling2D(name='pool1')(c_pred_features[0])
                    x = layers.Dense(self.num_class, name='fc1')(x)
                    c_pred = layers.Activation('softmax', name='c_pred')(x)
                    c_pred_features[0] = c_pred

                #self.classifier = models.Model(inputs=[img_input], outputs=c_pred_features,name='Classifier') 
                

                #############################################################################################
                # DEFINE FULL MODEL
                #############################################################################################
                #c_pred_features_1 = self.classifier(img_input)
                #c_pred_1 = c_pred_features_1[0]
                loss_pred_embeddings = lossnet.Lossnet(c_pred_features, self.config["NETWORK"]["embedding_size"])
                
                # add some inputs to prediction and testing
                labels_tensor = tf.keras.Input(tensor=self.DataGen.next_element[1], name= 'labels_tensor')
                files_tesor   = tf.keras.Input(tensor=self.DataGen.next_element[2], name= 'files_tesor')
                
                model_inputs  = [img_input, labels_tensor, files_tesor]
                model_outputs = [c_pred, loss_pred_embeddings[0], loss_pred_embeddings[2], labels_tensor, files_tesor]
                
                self.model = models.Model(inputs=model_inputs, outputs=model_outputs)

                #############################################################################################
                # INIT VARIABLES
                #############################################################################################
                #self.sess.graph.as_default()
                backend.set_session(self.sess)
                self.sess.run(tf.local_variables_initializer())

                ##################
                # SETUP WATCHER
                ################## 
                self.run_watcher = get_run_watcher()

                self.run_watcher.add_run.remote(name=self.name_run,
                                                user=self.user,
                                                progress=0,
                                                wandb_url=self.wandb.run.get_url(),
                                                status="Idle") 
                
                self.progress = 0
                
                self.run_watcher.update_run.remote(name=self.name_run, progress=self.progress)
                
                print(self.pre,'Init done')
Exemplo n.º 25
0
from tensorflow.keras import layers

height = 64
width = 64
channels = 3
num_classes = 10

model = Sequential()
model.add(
    layers.SeparableConv2D(32,
                           3,
                           activation='relu',
                           input_shape=(height, width, channels)))
model.add(layers.SeparableConv2D(64, 3, activation='relu'))
model.add(layers.MaxPooling2D(2))

model.add(layers.SeparableConv2D(64, 3, activation='relu'))
model.add(layers.SeparableConv2D(128, 3, activation='relu'))
model.add(layers.MaxPooling2D(2))

model.add(layers.SeparableConv2D(64, 3, activation='relu'))
model.add(layers.SeparableConv2D(128, 3, activation='relu'))
model.add(layers.GlobalAveragePooling2D())

model.add(layers.Dense(32, activation='relu'))
model.add(layers.Dense(num_classes, activation='softmax'))

model.compile(optimizer='rmsprop', loss='categorical_crossentropy')

model.summary()
Exemplo n.º 26
0
def avg_pool(x):
    return lyrs.GlobalAveragePooling2D()(x)
Exemplo n.º 27
0
    x = layers.Conv2D(32, 1, activation=actfun, padding='same')(block_output)
    x = layers.Conv2D(32, 3, activation=None, padding='same')(x)
    x = layers.add([x, block_output])
    x = layers.Conv2D(32, 1, activation=actfun)(x)
    block_output = layers.AveragePooling2D(pool_size=2, strides=2)(x)

    x = layers.Conv2D(32, 1, activation=actfun, padding='same')(block_output)
    x = layers.Conv2D(32, 3, activation=None, padding='same')(x)
    x = layers.add([x, block_output])
    x = layers.Conv2D(32, 1, activation=actfun)(x)
    x = layers.AveragePooling2D(2, strides=2)

    x = layers.Conv2D(32, 1, activation=actfun)(block_output)
    x = layers.Conv2D(32, 3, activation=None)(x)
    x = layers.GlobalAveragePooling2D()(x)

    x = layers.Flatten()(x)
    x = layers.Dense(32, activation=actfun)(x)
    x = layers.Dense(1)(x)
    counts = tf.keras.activations.softplus(x)
    model = tf.keras.Model(inputs, counts, name='toy_resnet')
    model.summary()

root = None
args.start_epoch = 0

print('Creating optimizer..')
with tf.device(args.device):
    optimizer = tf.optimizers.Adam()
root = tf.train.Checkpoint(optimizer=optimizer,
    def __init__(self,
                 config,
                 dataset,
                 num_run=0,
                 resume_model_path=False,
                 resume=False):

        #############################################################################################
        # LIBRARIES
        #############################################################################################
        import os
        import numpy as np
        import tensorflow as tf
        from tensorflow.python import pywrap_tensorflow
        from tensorflow.keras import optimizers, losses, models, backend, layers, metrics
        from tensorflow.keras.utils import multi_gpu_model

        self.run_path = os.path.dirname(os.path.realpath(__file__))
        os.chdir(self.run_path)

        utils = local_module("utils")
        logger = local_module("logger")
        lossnet = local_module("lossnet")
        data_pipeline = local_module("data_pipeline")
        backbones = local_module("backbones")

        #############################################################################################
        # PARAMETERS RUN
        #############################################################################################
        self.config = config
        self.dataset = dataset
        self.num_run = num_run
        self.group = "Stage_" + str(num_run)
        self.name_run = "Train_" + self.group

        self.run_dir = os.path.join(config["PROJECT"]["group_dir"], self.group)
        self.run_dir_check = os.path.join(self.run_dir, 'checkpoints')
        self.checkpoints_path = os.path.join(self.run_dir_check,
                                             'checkpoint.{epoch:03d}.hdf5')
        self.user = get_user()
        self.training_thread = None
        self.resume_training = resume

        #self.num_data_train  = len(labeled_set)
        self.resume_model_path = resume_model_path
        self.transfer_weight_path = self.config['TRAIN'][
            "transfer_weight_path"]
        self.input_shape = [
            self.config["NETWORK"]["INPUT_SIZE"],
            self.config["NETWORK"]["INPUT_SIZE"], 3
        ]

        self.pre = '\033[1;36m' + self.name_run + '\033[0;0m'  #"____" #
        self.problem = '\033[1;31m' + self.name_run + '\033[0;0m'

        # Creating the train folde
        import shutil

        # create base dir and gr
        if os.path.exists(config["PROJECT"]["project_dir"]) is False:
            os.mkdir(config["PROJECT"]["project_dir"])

        if os.path.exists(self.run_dir) and self.resume_model_path is False:
            shutil.rmtree(config["PROJECT"]["group_dir"])
            os.mkdir(config["PROJECT"]["group_dir"])

        if os.path.exists(config["PROJECT"]["group_dir"]) is False:
            os.mkdir(config["PROJECT"]["group_dir"])

        if os.path.exists(self.run_dir) is False:
            os.mkdir(self.run_dir)

        if os.path.exists(self.run_dir_check) is False:
            os.mkdir(self.run_dir_check)

        #############################################################################################
        # SETUP TENSORFLOW SESSION
        #############################################################################################
        # Create a MirroredStrategy.
        #self.strategy = tf.distribute.MirroredStrategy()
        #print(self.pre,'Number of devices: {}'.format(self.strategy.num_replicas_in_sync))

        #with self.strategy.scope():
        if True:
            self.graph = tf.Graph()
            with self.graph.as_default():
                config_tf = tf.ConfigProto(allow_soft_placement=True)
                config_tf.gpu_options.allow_growth = True
                self.sess = tf.Session(config=config_tf, graph=self.graph)
                backend.set_session(self.sess)
                with self.sess.as_default():

                    #############################################################################################
                    # SETUP WANDB
                    #############################################################################################
                    import wandb

                    self.wandb = wandb
                    self.wandb.init(project=config["PROJECT"]["project"],
                                    group=config["PROJECT"]["group"],
                                    name="Train_" + str(num_run),
                                    job_type=self.group,
                                    sync_tensorboard=True,
                                    config=config)

                    #############################################################################################
                    # LOAD DATA
                    #############################################################################################

                    self.DataGen = data_pipeline.ClassificationDataset(
                        config["TRAIN"]["batch_size"],
                        self.dataset,
                        subset="train",
                        original_size=config["DATASET"]["original_size"],
                        data_augmentation=config["DATASET"]
                        ["Data_augementation"],
                        random_flip=config["DATASET"]["random_flip"],
                        pad=config["DATASET"]["pad"],
                        random_crop_pad=config["DATASET"]["random_crop_pad"],
                        random_hue=config["DATASET"]["random_hue"],
                        random_brightness=config["DATASET"]
                        ["random_brightness"],
                        random_saturation=config["DATASET"]
                        ["random_saturation"])

                    self.num_class = len(self.DataGen.list_classes)

                    #############################################################################################
                    # GLOBAL PROGRESS
                    #############################################################################################
                    self.steps_per_epoch = int(
                        np.ceil(self.DataGen.nb_elements /
                                config["TRAIN"]["batch_size"]))
                    self.split_epoch = self.config['TRAIN']["EPOCH_WHOLE"]
                    self.total_epochs = self.config['TRAIN'][
                        "EPOCH_WHOLE"] + self.config['TRAIN']["EPOCH_SLIT"]
                    self.total_steps = self.steps_per_epoch * self.total_epochs

                    #############################################################################################
                    # DEFINE CLASSIFIER
                    #############################################################################################
                    # set input
                    img_input = tf.keras.Input(
                        tensor=self.DataGen.images_tensor, name='input_image')
                    #img_input = tf.keras.Input(self.input_shape,name= 'input_image')

                    include_top = True

                    # Get the selected backbone
                    """
                    ResNet18
                    ResNet50
                    ResNet101
                    ResNet152
                    ResNet50V2
                    ResNet101V2
                    ResNet152V2
                    ResNeXt50
                    ResNeXt101
                    """
                    print(self.pre, "The backbone is: ",
                          self.config["NETWORK"]["Backbone"])
                    self.backbone = getattr(backbones,
                                            self.config["NETWORK"]["Backbone"])
                    #
                    c_pred_features = self.backbone(input_tensor=img_input,
                                                    classes=self.num_class,
                                                    include_top=include_top)
                    self.c_pred_features = c_pred_features
                    if include_top:  # include top classifier
                        # class predictions
                        c_pred = c_pred_features[0]
                    else:
                        x = layers.GlobalAveragePooling2D(name='pool1')(
                            c_pred_features[0])
                        x = layers.Dense(self.num_class, name='fc1')(x)
                        c_pred = layers.Activation('softmax', name='c_pred')(x)
                        c_pred_features[0] = c_pred

                    #self.classifier = models.Model(inputs=[img_input], outputs=c_pred_features,name='Classifier')

                    #############################################################################################
                    # DEFINE FULL MODEL
                    #############################################################################################
                    #c_pred_features_1 = self.classifier(img_input)
                    #c_pred_1 = c_pred_features[0]
                    loss_pred_embeddings = lossnet.Lossnet(
                        c_pred_features,
                        self.config["NETWORK"]["embedding_size"])

                    model_inputs = [img_input]
                    model_outputs = [c_pred] + loss_pred_embeddings

                    self.model = models.Model(
                        inputs=model_inputs,
                        outputs=model_outputs)  #, embedding_s] )

                    ########################################
                    # INIT GLOBAL VARIABLES
                    #######################################
                    self.sess.run(tf.global_variables_initializer())

                    #############################################################################################
                    # LOAD PREVIUS WEIGTHS
                    #############################################################################################
                    if self.resume_model_path:
                        # check the epoch where is loaded
                        try:
                            loaded_epoch = int(
                                self.resume_model_path.split('.')[-2])
                            print(self.pre, "Loading weigths from: ",
                                  self.resume_model_path)
                            print(self.pre, "The detected epoch is: ",
                                  loaded_epoch)
                            # load weigths
                            self.model.load_weights(self.resume_model_path)
                        except:
                            print(self.problem,
                                  "=> Problem loading the weights from ",
                                  self.resume_model_path)
                            print(self.problem, '=> It will rain from scratch')
                    elif self.transfer_weight_path:
                        try:
                            print(
                                self.pre,
                                "(transfer learning) Loading weigths by name from: ",
                                self.transfer_weight_path)
                            # load weigths
                            self.model.load_weights(self.transfer_weight_path,
                                                    by_name=True)
                        except:
                            print(
                                self.problem,
                                "=>(transfer learning) Problem loading the weights from ",
                                self.transfer_weight_path)
                            print(self.problem, '=> It will rain from scratch')

                    if self.resume_training:
                        self.current_epoch = loaded_epoch
                        self.current_step = loaded_epoch * self.steps_per_epoch

                        if self.current_epoch > self.total_epochs:
                            raise ValueError(
                                "The starting epoch is higher that the total epochs"
                            )
                        else:
                            print(self.pre,
                                  "Resuming the training from stage: ",
                                  self.num_run, " at epoch ",
                                  self.current_epoch)
                    else:
                        self.current_epoch = 0
                        self.current_step = 0

                    #############################################################################################
                    # DEFINE WEIGHT DECAY
                    #############################################################################################
                    if self.config['TRAIN']['apply_weight_decay']:
                        utils.add_weight_decay(
                            self.model, self.config['TRAIN']['weight_decay'])

                    #############################################################################################
                    # DEFINE LOSSES
                    #############################################################################################

                    # losses
                    self.loss_dict = {}
                    self.loss_dict[
                        'c_pred'] = losses.sparse_categorical_crossentropy
                    self.loss_dict['l_pred_w'] = lossnet.Loss_Lossnet
                    self.loss_dict['l_pred_s'] = lossnet.Loss_Lossnet
                    # weights
                    self.weight_w = backend.variable(
                        self.config['TRAIN']['weight_lossnet_loss'])
                    self.weight_s = backend.variable(0)

                    self.loss_w_dict = {}
                    self.loss_w_dict['c_pred'] = 1.0
                    self.loss_w_dict['l_pred_w'] = self.weight_w
                    self.loss_w_dict['l_pred_s'] = self.weight_s
                    #self.loss_w_dict['Embedding']  = 0

                    #############################################################################################
                    # DEFINE METRICS
                    #############################################################################################
                    # metrics
                    self.metrics_dict = {}
                    self.metrics_dict[
                        'c_pred'] = tf.keras.metrics.SparseCategoricalAccuracy(
                        )
                    #self.metrics_dict['l_pred_w']   = lossnet.MAE_Lossnet
                    #self.metrics_dict['l_pred_s']   = lossnet.MAE_Lossnet

                    #############################################################################################
                    # DEFINE OPTIMIZER
                    #############################################################################################
                    self.opt = optimizers.Adam(lr=self.config['TRAIN']['lr'])

                    #############################################################################################
                    # DEFINE CALLBACKS
                    #############################################################################################
                    # Checkpoint saver
                    self.callbacks = []
                    model_checkpoint_callback = tf.keras.callbacks.ModelCheckpoint(
                        filepath=self.checkpoints_path,
                        save_weights_only=True,
                        period=self.config["TRAIN"]["test_each"])

                    self.callbacks.append(model_checkpoint_callback)

                    # Callback to wandb
                    # i remplaced this for a custom callback that saves the logs with better names
                    #self.callbacks.append(self.wandb.keras.WandbCallback())

                    # Callback Learning Rate
                    def scheduler(epoch):
                        lr = self.config['TRAIN']['lr']
                        for i in self.config['TRAIN']['MILESTONES']:
                            if epoch > i:
                                lr *= 0.1
                        return lr

                    #self.callbacks.append(tf.keras.callbacks.LearningRateScheduler(scheduler))

                    # callback to change the weigths for the split training:
                    self.callbacks.append(
                        lossnet.Change_loss_weights(
                            self.weight_w, self.weight_s, self.split_epoch,
                            self.config['TRAIN']['weight_lossnet_loss']))

                    ##################
                    # SETUP WATCHER
                    ##################
                    self.run_watcher = get_run_watcher()

                    self.run_watcher.add_run.remote(
                        name=self.name_run,
                        user=self.user,
                        progress=0,
                        wandb_url=self.wandb.run.get_url(),
                        status="Idle")

                    # Callback update progress
                    self.Update_progress = logger.Update_progress(
                        self.run_watcher, self.wandb, self.name_run,
                        self.steps_per_epoch, self.total_epochs,
                        self.total_steps, self.current_epoch,
                        self.current_step)

                    self.callbacks.append(self.Update_progress)

                    #############################################################################################
                    # COMPILE MODEL
                    #############################################################################################
                    self.model.compile(
                        loss=self.loss_dict,
                        loss_weights=self.loss_w_dict,
                        metrics=self.metrics_dict,
                        optimizer=self.opt,
                        target_tensors=self.DataGen.labels_tensor)

                    ########################################
                    # INIT LOCAL VARIABLES
                    #######################################
                    self.sess.run(tf.local_variables_initializer())

            print(self.pre, 'Init done')
def Xception(include_top=True,
             weights='imagenet',
             input_tensor=None,
             input_shape=None,
             pooling=None,
             classes=1000,
             **kwargs):
    """Instantiates the Xception architecture.

    Optionally loads weights pre-trained on ImageNet.
    Note that the data format convention used by the model is
    the one specified in your Keras config at `~/.keras/keras.json`.

    Note that the default input image size for this model is 299x299.

    # Arguments
        include_top: whether to include the fully-connected
            layer at the top of the network.
        weights: one of `None` (random initialization),
              'imagenet' (pre-training on ImageNet),
              or the path to the weights file to be loaded.
        input_tensor: optional Keras tensor
            (i.e. output of `layers.Input()`)
            to use as image input for the model.
        input_shape: optional shape tuple, only to be specified
            if `include_top` is False (otherwise the input shape
            has to be `(299, 299, 3)`.
            It should have exactly 3 inputs channels,
            and width and height should be no smaller than 71.
            E.g. `(150, 150, 3)` would be one valid value.
        pooling: Optional pooling mode for feature extraction
            when `include_top` is `False`.
            - `None` means that the output of the model will be
                the 4D tensor output of the
                last convolutional block.
            - `avg` means that global average pooling
                will be applied to the output of the
                last convolutional block, and thus
                the output of the model will be a 2D tensor.
            - `max` means that global max pooling will
                be applied.
        classes: optional number of classes to classify images
            into, only to be specified if `include_top` is True,
            and if no `weights` argument is specified.

    # Returns
        A Keras model instance.

    # Raises
        ValueError: in case of invalid argument for `weights`,
            or invalid input shape.
        RuntimeError: If attempting to run this model with a
            backend that does not support separable convolutions.
    """

    if not (weights in {'imagenet', None} or os.path.exists(weights)):
        raise ValueError('The `weights` argument should be either '
                         '`None` (random initialization), `imagenet` '
                         '(pre-training on ImageNet), '
                         'or the path to the weights file to be loaded.')

    if weights == 'imagenet' and include_top and classes != 1000:
        raise ValueError(
            'If using `weights` as `"imagenet"` with `include_top`'
            ' as true, `classes` should be 1000')

    # Determine proper input shape
    input_shape = _obtain_input_shape(input_shape,
                                      default_size=299,
                                      min_size=71,
                                      data_format=backend.image_data_format(),
                                      require_flatten=include_top,
                                      weights=weights)

    if input_tensor is None:
        img_input = layers.Input(shape=input_shape)
    else:
        if not backend.is_keras_tensor(input_tensor):
            img_input = layers.Input(tensor=input_tensor, shape=input_shape)
        else:
            img_input = input_tensor

    channel_axis = 1 if backend.image_data_format() == 'channels_first' else -1

    x = layers.Conv2D(32, (3, 3),
                      padding='same',
                      strides=(2, 2),
                      use_bias=False,
                      name='block1_conv1')(img_input)
    x = layers.BatchNormalization(axis=channel_axis, name='block1_conv1_bn')(x)
    x = layers.Activation('relu', name='block1_conv1_act')(x)
    x = layers.Conv2D(64, (3, 3),
                      padding='same',
                      use_bias=False,
                      name='block1_conv2')(x)
    x = layers.BatchNormalization(axis=channel_axis, name='block1_conv2_bn')(x)
    x = layers.Activation('relu', name='block1_conv2_act')(x)

    residual = layers.Conv2D(128, (1, 1),
                             strides=(2, 2),
                             padding='same',
                             use_bias=False)(x)
    residual = layers.BatchNormalization(axis=channel_axis)(residual)

    x = layers.SeparableConv2D(128, (3, 3),
                               padding='same',
                               use_bias=False,
                               name='block2_sepconv1')(x)
    x = layers.BatchNormalization(axis=channel_axis,
                                  name='block2_sepconv1_bn')(x)
    x = layers.Activation('relu', name='block2_sepconv2_act')(x)
    x = layers.SeparableConv2D(128, (3, 3),
                               padding='same',
                               use_bias=False,
                               name='block2_sepconv2')(x)
    x = layers.BatchNormalization(axis=channel_axis,
                                  name='block2_sepconv2_bn')(x)

    x = layers.MaxPooling2D((3, 3),
                            strides=(2, 2),
                            padding='same',
                            name='block2_pool')(x)
    x = layers.add([x, residual])

    residual = layers.Conv2D(256, (1, 1),
                             strides=(2, 2),
                             padding='same',
                             use_bias=False)(x)
    residual = layers.BatchNormalization(axis=channel_axis)(residual)

    x = layers.Activation('relu', name='block3_sepconv1_act')(x)
    x = layers.SeparableConv2D(256, (3, 3),
                               padding='same',
                               use_bias=False,
                               name='block3_sepconv1')(x)
    x = layers.BatchNormalization(axis=channel_axis,
                                  name='block3_sepconv1_bn')(x)
    x = layers.Activation('relu', name='block3_sepconv2_act')(x)
    x = layers.SeparableConv2D(256, (3, 3),
                               padding='same',
                               use_bias=False,
                               name='block3_sepconv2')(x)
    x = layers.BatchNormalization(axis=channel_axis,
                                  name='block3_sepconv2_bn')(x)

    x = layers.MaxPooling2D((3, 3),
                            strides=(2, 2),
                            padding='same',
                            name='block3_pool')(x)
    x = layers.add([x, residual])

    residual = layers.Conv2D(728, (1, 1),
                             strides=(2, 2),
                             padding='same',
                             use_bias=False)(x)
    residual = layers.BatchNormalization(axis=channel_axis)(residual)

    x = layers.Activation('relu', name='block4_sepconv1_act')(x)
    x = layers.SeparableConv2D(728, (3, 3),
                               padding='same',
                               use_bias=False,
                               name='block4_sepconv1')(x)
    x = layers.BatchNormalization(axis=channel_axis,
                                  name='block4_sepconv1_bn')(x)
    x = layers.Activation('relu', name='block4_sepconv2_act')(x)
    x = layers.SeparableConv2D(728, (3, 3),
                               padding='same',
                               use_bias=False,
                               name='block4_sepconv2')(x)
    x = layers.BatchNormalization(axis=channel_axis,
                                  name='block4_sepconv2_bn')(x)

    x = layers.MaxPooling2D((3, 3),
                            strides=(2, 2),
                            padding='same',
                            name='block4_pool')(x)
    x = layers.add([x, residual])

    for i in range(8):
        residual = x
        prefix = 'block' + str(i + 5)

        x = layers.Activation('relu', name=prefix + '_sepconv1_act')(x)
        x = layers.SeparableConv2D(728, (3, 3),
                                   padding='same',
                                   use_bias=False,
                                   name=prefix + '_sepconv1')(x)
        x = layers.BatchNormalization(axis=channel_axis,
                                      name=prefix + '_sepconv1_bn')(x)
        x = layers.Activation('relu', name=prefix + '_sepconv2_act')(x)
        x = layers.SeparableConv2D(728, (3, 3),
                                   padding='same',
                                   use_bias=False,
                                   name=prefix + '_sepconv2')(x)
        x = layers.BatchNormalization(axis=channel_axis,
                                      name=prefix + '_sepconv2_bn')(x)
        x = layers.Activation('relu', name=prefix + '_sepconv3_act')(x)
        x = layers.SeparableConv2D(728, (3, 3),
                                   padding='same',
                                   use_bias=False,
                                   name=prefix + '_sepconv3')(x)
        x = layers.BatchNormalization(axis=channel_axis,
                                      name=prefix + '_sepconv3_bn')(x)

        x = layers.add([x, residual])

    residual = layers.Conv2D(1024, (1, 1),
                             strides=(2, 2),
                             padding='same',
                             use_bias=False)(x)
    residual = layers.BatchNormalization(axis=channel_axis)(residual)

    x = layers.Activation('relu', name='block13_sepconv1_act')(x)
    x = layers.SeparableConv2D(728, (3, 3),
                               padding='same',
                               use_bias=False,
                               name='block13_sepconv1')(x)
    x = layers.BatchNormalization(axis=channel_axis,
                                  name='block13_sepconv1_bn')(x)
    x = layers.Activation('relu', name='block13_sepconv2_act')(x)
    x = layers.SeparableConv2D(1024, (3, 3),
                               padding='same',
                               use_bias=False,
                               name='block13_sepconv2')(x)
    x = layers.BatchNormalization(axis=channel_axis,
                                  name='block13_sepconv2_bn')(x)

    x = layers.MaxPooling2D((3, 3),
                            strides=(2, 2),
                            padding='same',
                            name='block13_pool')(x)
    x = layers.add([x, residual])

    x = layers.SeparableConv2D(1536, (3, 3),
                               padding='same',
                               use_bias=False,
                               name='block14_sepconv1')(x)
    x = layers.BatchNormalization(axis=channel_axis,
                                  name='block14_sepconv1_bn')(x)
    x = layers.Activation('relu', name='block14_sepconv1_act')(x)

    x = layers.SeparableConv2D(2048, (3, 3),
                               padding='same',
                               use_bias=False,
                               name='block14_sepconv2')(x)
    x = layers.BatchNormalization(axis=channel_axis,
                                  name='block14_sepconv2_bn')(x)
    x = layers.Activation('relu', name='block14_sepconv2_act')(x)

    if include_top:
        x = layers.GlobalAveragePooling2D(name='avg_pool')(x)
        x = layers.Dense(classes, activation='softmax', name='predictions')(x)
    else:
        if pooling == 'avg':
            x = layers.GlobalAveragePooling2D()(x)
        elif pooling == 'max':
            x = layers.GlobalMaxPooling2D()(x)

    # Ensure that the model takes into account
    # any potential predecessors of `input_tensor`.
    if input_tensor is not None:
        inputs = get_source_inputs(input_tensor)
    else:
        inputs = img_input
    # Create model.
    model = models.Model(inputs, x, name='xception')

    # Load weights.
    if weights == 'imagenet':
        if include_top:
            weights_path = get_file(
                'xception_weights_tf_dim_ordering_tf_kernels.h5',
                TF_WEIGHTS_PATH,
                cache_subdir='models',
                file_hash='0a58e3b7378bc2990ea3b43d5981f1f6')
        else:
            weights_path = get_file(
                'xception_weights_tf_dim_ordering_tf_kernels_notop.h5',
                TF_WEIGHTS_PATH_NO_TOP,
                cache_subdir='models',
                file_hash='b0042744bf5b25fce3cb969f33bebb97')
        model.load_weights(weights_path)
        # if backend.backend() == 'theano':
        #     convert_all_kernels_in_model(model)
    elif weights is not None:
        model.load_weights(weights)

    return model
Exemplo n.º 30
0
 def _se_module(input, levels, factor=16):
     x = layers.GlobalAveragePooling2D()(input)
     x = layers.Dense(levels//factor, activation="relu")(x)
     x = layers.Dense(levels, activation="sigmoid")(x)
     se_layer = layers.Multiply()([input, x])
     return se_layer