def eff_b0_01():
    # 224
    base = EfficientNetB0(
        include_top=False,
        input_shape=(config.img_size, config.img_size, config.channel),
        weights=None
    )

    # Using BN to normalize images
    # base.layers.pop(0)
    # inp = Input(shape=(config.img_size, config.img_size, config.channel))
    # x = BatchNormalization()(inp)
    # base_out = base(inp)

    x = GlobalAveragePooling2D()(base.output)
    x = Dense(1280)(x)
    x = BatchNormalization()(x)
    x = Activation('relu')(x)
    x = Dense(128)(x)
    x = BatchNormalization()(x)
    x = Activation('relu')(x)
    x = Dense(config.n_labels, activation='softmax')(x)
    model = Model(inputs=base.input, outputs=x)

    return model
    def getTrainModel(self):
        input_shape = (self.cut_size["height"], self.cut_size["width"],
                       self.channel)
        print("input_shape : {}".format(input_shape))

        # EfficientNetのオプションについては随時調査(やりたいのは1から学習)
        effnet_instance = EfficientNetB0(input_shape=input_shape,
                                         weights='imagenet',
                                         include_top=False)
        x = effnet_instance.output
        x = self.bottleneck(x)

        model = Model(inputs=effnet_instance.input, outputs=x)
        return model
示例#3
0
def get_fe(fe, input_image):
    if fe == 'effnetb0':
        return EfficientNetB0(input_tensor=input_image, include_top=False), [
            'swish_last', 'block5_i_MB_swish_1', 'block3_i_MB_swish_1'
        ]
    elif fe == 'effnetb1':
        return EfficientNetB1(input_tensor=input_image, include_top=False), [
            'swish_last', 'block5_i_MB_swish_1', 'block3_i_MB_swish_1'
        ]
    elif fe == 'effnetb2':
        return EfficientNetB2(input_tensor=input_image, include_top=False), [
            'swish_last', 'block5_i_MB_swish_1', 'block3_i_MB_swish_1'
        ]
    elif fe == 'effnetb3':
        return EfficientNetB3(input_tensor=input_image, include_top=False), [
            'swish_last', 'block5_i_MB_swish_1', 'block3_i_MB_swish_1'
        ]
    elif fe == 'effnetb4':
        return EfficientNetB4(input_tensor=input_image, include_top=False), [
            'swish_last', 'block5_i_MB_swish_1', 'block3_i_MB_swish_1'
        ]
    elif fe == 'effnetb5':
        return EfficientNetB5(input_tensor=input_image, include_top=False), [
            'swish_last', 'block5_i_MB_swish_1', 'block3_i_MB_swish_1'
        ]
    elif fe == 'd53':
        return d53(input_image)
    elif fe == 'mnetv2':
        mnet = MobileNetV2(input_tensor=input_image, weights='imagenet')
        return mnet, [
            'out_relu', 'block_13_expand_relu', 'block_6_expand_relu'
        ]
    elif fe == 'mnet':
        mnet = MobileNet(input_tensor=input_image, weights='imagenet')
        return mnet, ['conv_pw_13_relu', 'conv_pw_11_relu', 'conv_pw_5_relu']
    elif fe == 'r50':
        r50 = ResNet50(input_tensor=input_image, weights='imagenet')
        return r50, ['activation_49', 'activation_40', 'activation_22']
    raise ValueError('Pls put the correct fe')
def get_model(model_name="vgg16"):
    tf.keras.backend.clear_session()
    if (model_name == "vgg16"):
        from tensorflow.keras.applications.vgg16 import preprocess_input
        model = VGG16(weights='imagenet', include_top=False)
        return (model, preprocess_input)
    elif (model_name == "vgg19"):
        return (tf.keras.applications.vgg19.VGG19(weights='imagenet',
                                                  include_top=False),
                tf.keras.applications.vgg16.preprocess_input)
    elif (model_name == "resnet50"):
        return (tf.keras.applications.resnet50.ResNet50(weights="imagenet",
                                                        include_top=False),
                tf.keras.applications.resnet50.preprocess_input)
    elif (model_name == "mobilenet"):
        return (tf.keras.applications.mobilenet.MobileNet(weights="imagenet",
                                                          include_top=False),
                tf.keras.applications.mobilenet.preprocess_input)
    # elif (model_name == "mobilenetv2"):
    #     return (tf.keras.applications.mobilenet_v2.MobileNetV2(weights="imagenet", include_top=False), tf.keras.applications.mobilenet_v2.preprocess_input)
    elif (model_name == "xception"):
        return (tf.keras.applications.xception.Xception(weights="imagenet",
                                                        include_top=False),
                tf.keras.applications.xception.preprocess_input)
    elif (model_name == "densenet121"):
        return (tf.keras.applications.densenet.DenseNet121(weights="imagenet",
                                                           include_top=False),
                tf.keras.applications.densenet.preprocess_input)
    elif (model_name == "inceptionv3"):
        return (tf.keras.applications.inception_v3.InceptionV3(
            weights="imagenet", include_top=False),
                tf.keras.applications.inception_v3.preprocess_input)
    elif (model_name == "efficientnetb0"):
        return (EfficientNetB0(weights='imagenet',
                               include_top=False), effpreprocess)
    elif (model_name == "efficientnetb5"):
        return (EfficientNetB5(weights='imagenet',
                               include_top=False), effpreprocess)
def get_effnet_model(save_path,
                     model_res=1024,
                     image_size=256,
                     depth=1,
                     size=3,
                     activation='elu',
                     loss='logcosh',
                     optimizer='adam'):

    if os.path.exists(save_path):
        print('Loading model')
        return load_model(save_path)

    # Build model
    print('Building model')
    model_scale = int(2 *
                      (math.log(model_res, 2) - 1))  # For example, 1024 -> 18
    if (size <= 0):
        effnet = EfficientNetB0(include_top=False,
                                weights='imagenet',
                                input_shape=(image_size, image_size, 3))
    if (size == 1):
        effnet = EfficientNetB1(include_top=False,
                                weights='imagenet',
                                input_shape=(image_size, image_size, 3))
    if (size == 2):
        effnet = EfficientNetB2(include_top=False,
                                weights='imagenet',
                                input_shape=(image_size, image_size, 3))
    if (size >= 3):
        effnet = EfficientNetB3(include_top=False,
                                weights='imagenet',
                                input_shape=(image_size, image_size, 3))

    layer_size = model_scale * 8 * 8 * 8
    if is_square(layer_size):  # work out layer dimensions
        layer_l = int(math.sqrt(layer_size) + 0.5)
        layer_r = layer_l
    else:
        layer_m = math.log(math.sqrt(layer_size), 2)
        layer_l = 2**math.ceil(layer_m)
        layer_r = layer_size // layer_l
    layer_l = int(layer_l)
    layer_r = int(layer_r)

    x_init = None
    inp = Input(shape=(image_size, image_size, 3))
    x = effnet(inp)
    if (size < 1):
        x = Conv2D(model_scale * 8, 1, activation=activation)(x)  # scale down
        if (depth > 0):
            x = Reshape((layer_r, layer_l))(
                x
            )  # See https://github.com/OliverRichter/TreeConnect/blob/master/cifar.py - TreeConnect inspired layers instead of dense layers.
    else:
        if (depth < 1):
            depth = 1
        if (size <= 2):
            x = Conv2D(model_scale * 8 * 4, 1,
                       activation=activation)(x)  # scale down a bit
            x = Reshape((layer_r * 2, layer_l * 2))(
                x
            )  # See https://github.com/OliverRichter/TreeConnect/blob/master/cifar.py - TreeConnect inspired layers instead of dense layers.
        else:
            x = Reshape((384, 256))(x)  # full size for B3
    while (depth > 0):
        x = LocallyConnected1D(layer_r, 1, activation=activation)(x)
        x = Permute((2, 1))(x)
        x = LocallyConnected1D(layer_l, 1, activation=activation)(x)
        x = Permute((2, 1))(x)
        if x_init is not None:
            x = Add()([x, x_init])  # add skip connection
        x_init = x
        depth -= 1
    if (
            size >= 2
    ):  # add unshared layers at end for different sections of the latent space
        x_init = x
        if layer_r % 3 == 0 and layer_l % 3 == 0:
            a = LocallyConnected1D(layer_r, 1, activation=activation)(x)
            b = LocallyConnected1D(layer_r, 1, activation=activation)(x)
            c = LocallyConnected1D(layer_r, 1, activation=activation)(x)
            a = Permute((2, 1))(a)
            b = Permute((2, 1))(b)
            c = Permute((2, 1))(c)
            a = LocallyConnected1D(layer_l // 3, 1, activation=activation)(a)
            b = LocallyConnected1D(layer_l // 3, 1, activation=activation)(b)
            c = LocallyConnected1D(layer_l // 3, 1, activation=activation)(c)
            x = Concatenate()([a, b, c])
        else:
            a = LocallyConnected1D(layer_r // 2, 1, activation=activation)(x)
            b = LocallyConnected1D(layer_r // 2, 1, activation=activation)(x)
            c = LocallyConnected1D(layer_r // 2, 1, activation=activation)(x)
            d = LocallyConnected1D(layer_r // 2, 1, activation=activation)(x)
            a = Permute((2, 1))(a)
            b = Permute((2, 1))(b)
            c = Permute((2, 1))(c)
            d = Permute((2, 1))(d)
            a = LocallyConnected1D(layer_l // 2, 1, activation=activation)(a)
            b = LocallyConnected1D(layer_l // 2, 1, activation=activation)(b)
            c = LocallyConnected1D(layer_l // 2, 1, activation=activation)(c)
            d = LocallyConnected1D(layer_l // 2, 1, activation=activation)(d)
            x = Concatenate()([a, b, c, d])
        x = Add()([x, x_init])  # add skip connection
    x = Reshape((model_scale, 512))(x)  # train against all dlatent values
    model = Model(inputs=inp, outputs=x)
    model.compile(loss=loss, metrics=[], optimizer=optimizer
                  )  # By default: adam optimizer, logcosh used for loss.
    return model
示例#6
0
        def _create_model():
            print('Creating model')
            # load the pretrained model, without the classification (top) layers
            if self.transfer_model == 'Xception':
                base_model = tf.keras.applications.Xception(
                    weights='imagenet',
                    include_top=False,
                    input_shape=(*self.target_size, 3))
                based_model_last_block = 116  # last block 126, two blocks 116
            elif self.transfer_model == 'Inception_Resnet':
                base_model = tf.keras.applications.InceptionResNetV2(
                    weights='imagenet',
                    include_top=False,
                    input_shape=(*self.target_size, 3))
                based_model_last_block = 287  # last block 630, two blocks 287
            elif self.transfer_model == 'Resnet':
                base_model = tf.keras.applications.ResNet50(
                    weights='imagenet',
                    include_top=False,
                    input_shape=(*self.target_size, 3))
                based_model_last_block = 155  # last block 165, two blocks 155
            elif self.transfer_model == 'B0':
                base_model = EfficientNetB0(weights='imagenet',
                                            include_top=False,
                                            input_shape=(*self.target_size, 3))
                based_model_last_block = 213  # last block 229, two blocks 213
            elif self.transfer_model == 'B3':
                base_model = EfficientNetB3(weights='imagenet',
                                            include_top=False,
                                            input_shape=(*self.target_size, 3))
                based_model_last_block = 354  # last block 370, two blocks 354
            elif self.transfer_model == 'B5':
                base_model = EfficientNetB5(weights='imagenet',
                                            include_top=False,
                                            input_shape=(*self.target_size, 3))
                based_model_last_block = 417  # last block 559, two blocks 417
            else:
                base_model = tf.keras.applications.InceptionV3(
                    weights='imagenet',
                    include_top=False,
                    input_shape=(*self.target_size, 3))
                based_model_last_block = 249  # last block 280, two blocks 249

            # Set only the top layers as trainable (if we want to do fine-tuning,
            # we can train the base layers as a second step)
            base_model.trainable = False

            # Target size infered from the base model
            self.target_size = base_model.input_shape[1:3]

            # Add the classification layers using Keras functional API
            x = base_model.output
            x = tf.keras.layers.GlobalAveragePooling2D()(x)
            # Hidden layer for classification
            if hidden_size == 0:
                x = tf.keras.layers.Dropout(rate=dropout)(x)
            elif bn_after_ac:
                x = tf.keras.layers.Dense(
                    hidden_size,
                    activation=activation,
                    kernel_regularizer=tf.keras.regularizers.l2(
                        l=l2_lambda))(x)
                x = tf.keras.layers.BatchNormalization()(x)
                x = tf.keras.layers.Dropout(rate=dropout)(x)
            else:
                x = tf.keras.layers.Dense(
                    hidden_size,
                    use_bias=False,
                    kernel_regularizer=tf.keras.regularizers.l2(
                        l=l2_lambda))(x)
                # scale: When the next layer is linear (also e.g. nn.relu), this can be disabled since the scaling can be done by the next layer.
                x = tf.keras.layers.BatchNormalization(
                    scale=activation != 'relu')(x)
                x = tf.keras.layers.Activation(activation=activation)(x)
                x = tf.keras.layers.Dropout(rate=dropout)(x)

            predictions = tf.keras.layers.Dense(len(self.categories),
                                                activation='softmax')(
                                                    x)  # Output layer
            # Define the optimizer and the loss and the optimizer
            loss = 'sparse_categorical_crossentropy'
            metrics = ['sparse_categorical_accuracy']
            optimizer = tf.keras.optimizers.Adam(lr=learning_rate)

            return tf.keras.Model(
                inputs=base_model.input, outputs=predictions
            ), base_model, based_model_last_block, loss, metrics, optimizer
示例#7
0
    def build_model(self):

        # tf.summary.image('input_image', image_input, 10)
        # tf.summary.image('mask', mask, 10)

        if (self.is_pb == False):
            is_training_seg = tf.placeholder(tf.bool, name='is_training_seg')
            is_training_dec = tf.placeholder(tf.bool, name='is_training_dec')
            if IMAGE_MODE == 0:
                image_input = tf.placeholder(tf.float32,
                                             shape=(self.__batch_size,
                                                    IMAGE_SIZE[0],
                                                    IMAGE_SIZE[1], 1),
                                             name='Image')
                num_ch = 1
            else:
                image_input = tf.placeholder(tf.float32,
                                             shape=(self.__batch_size,
                                                    IMAGE_SIZE[0],
                                                    IMAGE_SIZE[1], 3),
                                             name='Image')
                num_ch = 3
            label = tf.placeholder(tf.float32,
                                   shape=(self.__batch_size, CLASS_NUM),
                                   name='Label')
            mask = tf.placeholder(tf.float32,
                                  shape=(self.__batch_size, IMAGE_SIZE[0],
                                         IMAGE_SIZE[1], CLASS_NUM),
                                  name='mask')

        else:
            is_training_seg = False
            is_training_dec = False
            if IMAGE_MODE == 0:
                image_input = tf.placeholder(
                    tf.float32,
                    shape=(self.__batch_size_inference, IMAGE_SIZE[0],
                           IMAGE_SIZE[1], 1),
                    name='Image')
                num_ch = 1
            else:
                image_input = tf.placeholder(
                    tf.float32,
                    shape=(self.__batch_size_inference, IMAGE_SIZE[0],
                           IMAGE_SIZE[1], 3),
                    name='Image')
                num_ch = 3
            label = tf.placeholder(tf.float32,
                                   shape=(self.__batch_size_inference,
                                          CLASS_NUM),
                                   name='Label')
            mask = tf.placeholder(tf.float32,
                                  shape=(self.__batch_size_inference,
                                         IMAGE_SIZE[0], IMAGE_SIZE[1],
                                         CLASS_NUM),
                                  name='mask')

        # create backbone
        if self.backbone == 'mixnet':
            from MixNet import MixNetSmall
            self.__checkPoint_dir = 'checkpoint/mixnet'
            backbone_output = MixNetSmall(
                image_input,
                scope='segmentation',
                include_top=False,
                keep_dropout_backbone=self.keep_dropout_backbone,
                training=is_training_seg)

        elif self.backbone == 'mixnet_official':
            from MixNet_official import build_model_base
            self.__checkPoint_dir = 'checkpoint/mixnet_official'
            backbone_output = build_model_base(image_input,
                                               'mixnet-s',
                                               training=is_training_seg,
                                               override_params=None,
                                               scope='segmentation')

        elif self.backbone == 'efficientnet':
            from efficientnet import EfficientNetB0
            self.__checkPoint_dir = 'checkpoint/efficientnet'
            backbone_output = EfficientNetB0(
                image_input,
                model_name='segmentation',
                input_shape=(None, IMAGE_SIZE[0], IMAGE_SIZE[1], num_ch),
                keep_dropout_backbone=self.keep_dropout_backbone,
                training=is_training_seg,
                include_top=True,
                classes=CLASS_NUM)

        elif self.backbone == 'fast_scnn':
            from fast_scnn import build_fast_scnn
            self.__checkPoint_dir = 'checkpoint/fast_scnn'
            backbone_output = build_fast_scnn(
                image_input,
                'segmentation',
                is_training=is_training_seg,
                keep_dropout_backbone=self.keep_dropout_backbone)
        elif self.backbone == 'ghostnet':
            from GhostNet import ghostnet_base
            self.__checkPoint_dir = 'checkpoint/ghostnet'
            # Set depth_multiplier to change the depth of GhostNet
            backbone_output = ghostnet_base(image_input,
                                            mode=self.__mode,
                                            data_format=DATA_FORMAT,
                                            scope='segmentation',
                                            dw_code=None,
                                            ratio_code=None,
                                            se=1,
                                            min_depth=8,
                                            depth=1,
                                            depth_multiplier=0.5,
                                            conv_defs=None,
                                            is_training=is_training_seg,
                                            momentum=self.__bn_momentum)

        elif self.backbone == 'sinet':
            from SiNet import SINet
            self.__checkPoint_dir = 'checkpoint/sinet'
            backbone_output = SINet(image_input,
                                    classes=CLASS_NUM,
                                    p=2,
                                    q=8,
                                    chnn=1,
                                    training=True,
                                    bn_momentum=0.99,
                                    scope='segmentation',
                                    reuse=False)

        elif self.backbone == 'lednet':
            from LEDNet import lednet
            self.__checkPoint_dir = 'checkpoint/lednet'
            backbone_output = lednet(image_input,
                                     training=is_training_seg,
                                     scope='segmentation',
                                     keep_dropout=False)

        elif self.backbone == 'cspnet':
            from CSPDenseNet import CSPPeleeNet
            self.__checkPoint_dir = 'checkpoint/cspnet'
            backbone_output = CSPPeleeNet(image_input,
                                          data_format=DATA_FORMAT,
                                          drop_rate=0.0,
                                          training=self.is_training,
                                          momentum=self.__bn_momentum,
                                          name="segmentation",
                                          mode=self.__mode,
                                          activation=ACTIVATION)
        elif self.backbone == 'FCNN':
            self.__checkPoint_dir = 'checkpoint/fcnn'
            from FCNN import conv_block, LearningToDownsample, GlobalFeatureExtractor, FeatureFusion, Classifier

            def SegmentNet(input, scope, is_training, reuse=None):
                with tf.variable_scope(scope, reuse=None):
                    lds2, lds1 = LearningToDownsample(input,
                                                      is_training,
                                                      scope='lds',
                                                      reuse=reuse)
                    gfe = GlobalFeatureExtractor(lds2,
                                                 is_training,
                                                 scope='gfe',
                                                 reuse=reuse)
                    ff = FeatureFusion(lds1,
                                       lds2,
                                       gfe,
                                       is_training,
                                       scope='ff',
                                       reuse=reuse)
                    features, logits = Classifier(ff,
                                                  is_training,
                                                  scope='classify',
                                                  reuse=reuse)
                    mask = tf.nn.sigmoid(logits, name='softmax1')
                    return features, logits, mask

            features, logits_pixel, mask = SegmentNet(image_input,
                                                      'segmentation',
                                                      self.is_training)

            if self.is_pb == True:
                self.mask_out_l = tf.nn.sigmoid(logits_pixel[0],
                                                name='mask_out1')
                self.mask_out_r = tf.nn.sigmoid(logits_pixel[1],
                                                name='mask_out2')

            if self.is_pb == False:
                # Variable list
                segmentation_loss = tf.reduce_mean(
                    tf.nn.sigmoid_cross_entropy_with_logits(
                        logits=logits_pixel, labels=mask))
                train_segment_var_list = [
                    v for v in tf.trainable_variables()
                    if 'segmentation' in v.name
                ]

                update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
                update_ops_segment = [
                    v for v in update_ops if 'segmentation' in v.name
                ]

                optimizer_segmentation = tf.train.AdamOptimizer(
                    self.__learn_rate)

                with tf.control_dependencies(update_ops_segment):
                    optimize_segment = optimizer_segmentation.minimize(
                        segmentation_loss, var_list=train_segment_var_list)

                self.segmentation_loss = segmentation_loss
                self.optimize_segment = optimize_segment
            mask_out = tf.nn.sigmoid(logits_pixel, name='mask_out')
            self.mask_out = mask_out
            init_op = tf.global_variables_initializer()
            self.Image = image_input
            self.is_training_seg = is_training_seg
            self.is_training_dec = is_training_dec
            self.mask = mask
            self.label = label

            self.init_op = init_op

            return

        else:
            raise ValueError("Unknown Backbone")

        # create segmentation head and segmentation loss

        if len(backbone_output) == 5:
            if self.neck == 'bifpn':
                from FPN import bifpn_neck
                P3_out, P4_out, P5_out, P6_out, P7_out = bifpn_neck(
                    backbone_output,
                    64,
                    scope='segmentation',
                    is_training=is_training_seg,
                    momentum=self.__bn_momentum,
                    mode=self.__mode,
                    data_format=DATA_FORMAT)
                with tf.variable_scope('segmentation'):

                    P3 = tf.layers.conv2d(P3_out,
                                          CLASS_NUM, (1, 1), (1, 1),
                                          use_bias=False,
                                          name='P3',
                                          data_format=DATA_FORMAT)
                    P4 = tf.layers.conv2d(P4_out,
                                          CLASS_NUM, (1, 1), (1, 1),
                                          use_bias=False,
                                          name='P4',
                                          data_format=DATA_FORMAT)
                    P5 = tf.layers.conv2d(P5_out,
                                          CLASS_NUM, (1, 1), (1, 1),
                                          use_bias=False,
                                          name='P5',
                                          data_format=DATA_FORMAT)
                    P6 = tf.layers.conv2d(P6_out,
                                          CLASS_NUM, (1, 1), (1, 1),
                                          use_bias=False,
                                          name='P6',
                                          data_format=DATA_FORMAT)
                    P7 = tf.layers.conv2d(P7_out,
                                          CLASS_NUM, (1, 1), (1, 1),
                                          use_bias=False,
                                          name='P7',
                                          data_format=DATA_FORMAT)
                    neck_output = [P3, P4, P5, P6, P7]
                    decision_in = [P3_out, P3]
            elif self.neck == 'fpn':
                from FPN import fpn_neck

                neck_output = fpn_neck(
                    backbone_output,
                    CLASS_NUM,
                    drop_rate=0.2,
                    keep_dropout_head=self.keep_dropout_head,
                    scope='segmentation',
                    training=is_training_seg)
            elif self.neck == 'bfp':
                from BFP import bfp_segmentation_head

                P3_out, P4_out, P5_out, P6_out, P7_out = bfp_segmentation_head(
                    backbone_output,
                    64,
                    scope='segmentation',
                    is_training=is_training_seg)
                with tf.variable_scope('segmentation'):
                    P3 = tf.layers.conv2d(P3_out,
                                          CLASS_NUM, (1, 1), (1, 1),
                                          use_bias=False,
                                          name='P3')
                    P4 = tf.layers.conv2d(P4_out,
                                          CLASS_NUM, (1, 1), (1, 1),
                                          use_bias=False,
                                          name='P4')
                    P5 = tf.layers.conv2d(P5_out,
                                          CLASS_NUM, (1, 1), (1, 1),
                                          use_bias=False,
                                          name='P5')
                    P6 = tf.layers.conv2d(P6_out,
                                          CLASS_NUM, (1, 1), (1, 1),
                                          use_bias=False,
                                          name='P6')
                    P7 = tf.layers.conv2d(P7_out,
                                          CLASS_NUM, (1, 1), (1, 1),
                                          use_bias=False,
                                          name='P7')
                neck_output = [P3, P4, P5, P6, P7]
                decision_in = [P3_out, P3]
            elif self.neck == 'pan':
                from FPN import PAN
                with tf.variable_scope('segmentation'):
                    backbone_output = PAN(backbone_output,
                                          128,
                                          training=self.is_training_seg)

                    seg_fea = backbone_output[-1]
                    seg_fea = tf.keras.layers.UpSampling2D((4, 4))(seg_fea)
                    seg_fea = tf.keras.layers.DepthwiseConv2D(
                        (3, 3), (1, 1), padding='same')(seg_fea)
                    seg_fea = tf.layers.batch_normalization(
                        seg_fea, training=is_training_seg)
                    seg_fea = tf.nn.relu(seg_fea)
                    seg_fea = tf.layers.conv2d(seg_fea, 128, (1, 1))

                    seg_fea = tf.keras.layers.SeparableConv2D(
                        128, (3, 3), padding='same')(seg_fea)
                    seg_fea = tf.layers.batch_normalization(
                        seg_fea, training=is_training_seg)
                    seg_fea = tf.nn.relu(seg_fea)

                    seg_fea = tf.keras.layers.SeparableConv2D(
                        128, (3, 3), padding='same')(seg_fea)
                    seg_fea = tf.layers.batch_normalization(
                        seg_fea, training=is_training_seg)
                    seg_fea = tf.nn.relu(seg_fea)

                    seg_fea1 = tf.layers.conv2d(seg_fea, 1, (1, 1))

                    neck_output = [seg_fea, seg_fea1]
            else:
                raise ValueError(" Unknown neck ")

            if len(neck_output) == 5:
                if DATA_FORMAT == 'channels_first':
                    for nec_index in range(len(neck_output)):
                        neck_output[nec_index] = tf.transpose(
                            neck_output[nec_index], [0, 2, 3, 1])
                logits_pixel = tf.image.resize_images(
                    neck_output[0], (IMAGE_SIZE[0], IMAGE_SIZE[1]),
                    align_corners=True,
                    method=tf.image.ResizeMethod.NEAREST_NEIGHBOR)
                segmentation_loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=logits_pixel, labels=mask)) + \
                                    tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=neck_output[1], labels=tf.image.resize_images(mask, (IMAGE_SIZE[0] // 4, IMAGE_SIZE[1] // 4)))) + \
                                    tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=neck_output[2], labels=tf.image.resize_images(mask, (IMAGE_SIZE[0] // 8, IMAGE_SIZE[1] // 8)))) + \
                                    tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=neck_output[3], labels=tf.image.resize_images(mask, (IMAGE_SIZE[0] // 16, IMAGE_SIZE[1] // 16)))) + \
                                    tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=neck_output[4], labels=tf.image.resize_images(mask, (IMAGE_SIZE[0] // 32, IMAGE_SIZE[1] // 32))))

            elif len(neck_output) == 2:
                decision_in = neck_output
                if DATA_FORMAT == 'channels_first':
                    for nec_index in range(len(neck_output)):
                        neck_output[nec_index] = tf.transpose(
                            neck_output[nec_index], [0, 2, 3, 1])
                logits_pixel = tf.image.resize_images(
                    neck_output[1], (IMAGE_SIZE[0], IMAGE_SIZE[1]),
                    align_corners=True)
                segmentation_loss = tf.reduce_mean(
                    tf.nn.sigmoid_cross_entropy_with_logits(
                        logits=logits_pixel, labels=mask))
        elif len(backbone_output) == 2:
            decision_in = [backbone_output[0], backbone_output[1]]
            if DATA_FORMAT == 'channels_first':
                for nec_index in range(len(backbone_output)):
                    backbone_output[nec_index] = tf.transpose(
                        backbone_output[nec_index], [0, 2, 3, 1])
            logits_pixel = tf.image.resize_images(
                backbone_output[1], (IMAGE_SIZE[0], IMAGE_SIZE[1]),
                align_corners=True)

            segmentation_loss = tf.reduce_mean(
                tf.nn.sigmoid_cross_entropy_with_logits(logits=logits_pixel,
                                                        labels=mask))
        else:
            raise ValueError(
                " Incorrect backbone output number, must be 3 or 5")

        mask_out = tf.nn.sigmoid(logits_pixel, name='mask_out')
        if self.is_pb == True:
            self.mask_out_l = tf.nn.sigmoid(logits_pixel[0], name='mask_out1')
            self.mask_out_r = tf.nn.sigmoid(logits_pixel[1], name='mask_out2')

        # Create decision head
        dec_out = decision_head(decision_in[0],
                                decision_in[1],
                                class_num=CLASS_NUM,
                                scope='decision',
                                keep_dropout_head=self.keep_dropout_head,
                                training=is_training_dec,
                                data_format=DATA_FORMAT,
                                momentum=self.__bn_momentum,
                                mode=self.__mode,
                                activation=ACTIVATION)
        decision_out = tf.nn.sigmoid(dec_out, name='decision_out')
        decision_loss = tf.nn.sigmoid_cross_entropy_with_logits(logits=dec_out,
                                                                labels=label)
        decision_loss = tf.reduce_mean(decision_loss)
        total_loss = segmentation_loss + decision_loss

        if self.is_pb == False:
            # Variable list
            train_segment_var_list = [
                v for v in tf.trainable_variables() if 'segmentation' in v.name
            ]

            train_decision_var_list = [
                v for v in tf.trainable_variables() if 'decision' in v.name
            ]
            train_var_list = train_segment_var_list + train_decision_var_list

            update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
            update_ops_segment = [
                v for v in update_ops if 'segmentation' in v.name
            ]

            update_ops_decision = [
                v for v in update_ops if 'decision' in v.name
            ]
            # optimizer_segmentation = tf.train.GradientDescentOptimizer(self.__learn_rate)
            optimizer_segmentation = tf.train.AdamOptimizer(self.__learn_rate)
            optimizer_decision = tf.train.AdamOptimizer(self.__learn_rate)
            # optimizer_decision = tf.train.GradientDescentOptimizer(self.__learn_rate)
            optimizer_total = tf.train.AdamOptimizer(self.__learn_rate)

            with tf.control_dependencies(update_ops_segment):
                optimize_segment = optimizer_segmentation.minimize(
                    segmentation_loss, var_list=train_segment_var_list)
            with tf.control_dependencies(update_ops_decision):
                optimize_decision = optimizer_decision.minimize(
                    decision_loss, var_list=train_decision_var_list)

            with tf.control_dependencies(update_ops):
                optimize_total = optimizer_total.minimize(
                    total_loss, var_list=train_var_list)

            self.segmentation_loss = segmentation_loss
            self.decision_loss = decision_loss
            self.optimize_segment = optimize_segment
            self.optimize_decision = optimize_decision
            self.optimize_total = optimize_total

        if not os.path.exists(self.tensorboard_logdir):
            os.makedirs(self.tensorboard_logdir)
        # merged = tf.summary.merge_all()
        # train_writer = tf.summary.FileWriter(self.tensorboard_logdir, self.session.graph)
        init_op = tf.global_variables_initializer()

        # self.update_ops_segment = update_ops_segment
        # self.train_segment_var_list = [v for v in tf.trainable_variables() if 'segmentation' in v.name]
        # self.backbone_output = backbone_output
        self.Image = image_input
        self.is_training_seg = is_training_seg
        self.is_training_dec = is_training_dec
        self.mask = mask
        self.label = label
        self.decison_out = decision_out
        self.mask_out = mask_out
        self.init_op = init_op
示例#8
0
from efficientnet import EfficientNetB0, preprocess_input
from sklearn.metrics import f1_score
from keras.preprocessing.image import ImageDataGenerator
import numpy as np

model = EfficientNetB0(weights=None, classes=80)
model.load_weights('/home/palm/PycharmProjects/ptt/weights/b0.h5')
d = ImageDataGenerator(preprocessing_function=preprocess_input)
val_generator = d.flow_from_directory(
    '/media/palm/data/scene_validation_20170908/ai_challenger_scene_validation_20170908/images',
    batch_size=32,
    target_size=(224, 224),
    shuffle=False)

y = model.predict_generator(val_generator, steps=len(val_generator), verbose=1)
y_pred = np.argmax(y, axis=1)
print(f1_score(val_generator.classes, y_pred, average='macro'))
train_generator = train_datagen.flow_from_directory(
    # This is the target directory
    train_dir,
    # All images will be resized to target height and width.
    target_size=(height, width),
    batch_size=batch_size,
    # Since we use categorical_crossentropy loss, we need categorical labels
    class_mode='categorical')

validation_generator = validation_datagen.flow_from_directory(
    validation_dir,
    target_size=(height, width),
    batch_size=batch_size,
    class_mode='categorical')

conv_base = Net(weights='imagenet', include_top=False, input_shape=input_shape)
# model = add_new_last_layer(conv_base, nb_classes)
model = models.Sequential()
# model.add(layers.Flatten(name="flatten"))
model.add(conv_base)
model.add(layers.GlobalMaxPooling2D(name="gap"))
if dropout_rate > 0:
    model.add(layers.Dropout(dropout_rate, name="dropout_out"))
# model.add(layers.Dense(256, activation='relu', name="fc1"))
model.add(layers.Dense(2, activation='softmax', name="fc_out"))  # 这里的2代表要分类的数目

# model.summary()

# 冻结卷积层不参与训练
conv_base.trainable = False

# In[14]:

HEIGHT = 299
WIDTH = 299

input_shape = (HEIGHT, WIDTH, 3)

FC_LAYERS = [1024]
dropout = 0.7
epochs = 150
swa = SWA('./keras_swa.model', epochs - 3)

base_model = EfficientNetB0(weights='imagenet',
                            include_top=False,
                            input_shape=(HEIGHT, WIDTH, 3))

finetune_model = build_finetune_model(base_model,
                                      dropout=dropout,
                                      num_classes=196)

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

snapshot = SnapshotCallbackBuilder(nb_epochs=epochs,
                                   nb_snapshots=1,
                                   init_lr=1e-3)

history = finetune_model.fit_generator(generator=train_generator,
#     添加最后的层
#     输入
#     base_model和分类数量
#     输出
#     新的keras的model
#     """
#     x = base_model.output
#     x = GlobalAveragePooling2D()(x)
#     predictions = Dense(nb_classes, activation='softmax')(x)  # new softmax layer
#     model = Model(input=base_model.input, output=predictions)
#     return model

# loading pretrained conv base model
# 使用在ImageNet ILSVRC比赛中已经训练好的权重,权重训练自ImageNet,ImageNet预训练权重
# 对输入类型等实例化   参数weights为指定模型的初始化权重检查点
conv_base = Net(weights='imagenet', include_top=False, input_shape=input_shape)
# model = add_new_last_layer(conv_base, nb_classes)
model = models.Sequential()
# model.add(layers.Flatten(name="flatten"))
model.add(conv_base)
model.add(layers.GlobalMaxPooling2D(name="gap"))
if dropout_rate > 0:
    model.add(layers.Dropout(dropout_rate, name="dropout_out"))
# model.add(layers.Dense(256, activation='relu', name="fc1"))
model.add(layers.Dense(2, activation='softmax', name="fc_out"))  # 这里的2代表要分类的数目

# 输出网络模型参数  查看一下实例化后卷积基模型
# model.summary()

# 冻结卷积层不参与训练
conv_base.trainable = False
示例#12
0
batch_size = 50 #จำนวน data ที่ส่งไป Train ในแต่ละครั้ง 
width = 150 
height = 150 
input_shape = (height, width, 3) #ขนาด image enter
epochs = 2000  #จำนวนรอบในการ Train
NUM_TRAIN = 2914# จำนวนภาพ Train
NUM_TEST = 125 #จำนวนภาพ Test
dropout_rate = 0.2 

import sys
sys.path.append('/media/tohn/SSD/Sub_Efficient_USAI/content/efficientnet_keras_transfer_learning')
from efficientnet import EfficientNetB0 as Net
from efficientnet import center_crop_and_resize, preprocess_input

# loading pretrained conv base model
conv_base = Net(weights='imagenet', include_top=False, input_shape=input_shape) 

# data augmentation เพื่อลดโอกาสการเกิด overfitting

from tensorflow.keras.preprocessing.image import ImageDataGenerator
train_datagen = ImageDataGenerator(
      rescale=1./255, # image input 0-255 --> 0-1 เปลี่ยนค่าสี
      rotation_range=40, # หมุนภาพในองศา
      width_shift_range=0.2, #เปลี่ยนความกว้าง
      height_shift_range=0.2, #ปลี่ยนความสูง
      shear_range=0.2, #ทำให้ภาพเบี้ยว
      zoom_range=0.2, #ซุม image มากสุด 20%
      horizontal_flip=True, #พลิกภาพแบบสุ่มตามแนวนอน
      fill_mode='nearest') 

# Note that the validation data should not be augmented!
示例#13
0
        shear_range=0.2,
        zoom_range=0.2,
        horizontal_flip=True,
        vertical_flip=True,
        # validation_split=0.2,
        preprocessing_function=randaug)
    d = ImageDataGenerator(preprocessing_function=randaug)
    train_generator = g.flow_from_directory(impath,
                                            batch_size=32,
                                            target_size=(224, 224))
    val_generator = d.flow_from_directory(
        '/media/palm/data/scene_validation_20170908/ai_challenger_scene_validation_20170908/images',
        batch_size=32,
        target_size=(224, 224))

    model = EfficientNetB0(include_top=False, pooling='avg')
    x = model.output
    x = layers.Dense(80, activation='softmax')(x)
    model = models.Model(model.input, x)
    model.compile(optimizer=optimizers.SGD(0.01, momentum=0.9),
                  loss='categorical_crossentropy',
                  metrics=['acc', f1_m])

    tb = CustomTensorBoard('B0, preprocess, 224*224, batch_size 32',
                           log_dir='logs/B0-1',
                           write_graph=False)
    cp = ModelCheckpoint('weights/b0.h5',
                         save_weights_only=True,
                         save_best_only=True,
                         monitor='val_acc',
                         mode='max')