コード例 #1
0
    def create_seg_model(self,
                         net,
                         n=21,
                         backbone='mobilenetv2',
                         load_weights=False,
                         multi_gpu=False):
        '''
        Net is:
        1. original deeplab v3+
        2. original deeplab v3+ and subpixel upsampling layer
        '''

        model = Deeplabv3(weights=None,
                          input_tensor=None,
                          infer=False,
                          input_shape=self.sz + (3, ),
                          classes=21,
                          backbone=backbone,
                          OS=8,
                          alpha=1)

        base_model = Model(model.input, model.layers[-5].output)
        for layer in base_model.layers:
            layer.trainable = False

        self.net = net
        self.modelpath = 'weights/{}_{}.h5'.format(backbone, net)
        if backbone == 'xception':
            scale = 4
        else:
            scale = 8
        if net == 'original':
            x = Conv2D(n, (1, 1), padding='same',
                       name='conv_upsample')(base_model.output)
            x = Lambda(lambda x: K.tf.image.resize_bilinear(
                x, size=(self.sz[0], self.sz[1])))(x)
            x = Reshape((self.sz[0] * self.sz[1], -1))(x)
            x = Activation('softmax', name='pred_mask')(x)
            model = Model(base_model.input, x, name='deeplabv3p')
        elif net == 'subpixel':
            x = Subpixel(n, 1, scale, padding='same')(base_model.output)
            x = Reshape((self.sz[0] * self.sz[1], -1))(x)
            x = Activation('softmax', name='pred_mask')(x)
            model = Model(base_model.input, x, name='deeplabv3p_subpixel')
        # Do ICNR
        for layer in model.layers:
            if type(layer) == Subpixel:
                c, b = layer.get_weights()
                w = icnr_weights(scale=scale, shape=c.shape)
                layer.set_weights([w, b])

        if load_weights:
            model.load_weights('weights/{}_{}.h5'.format(backbone, net))

        if multi_gpu:
            from keras.utils import multi_gpu_model
            model = multi_gpu_model(model, gpus=len(get_available_gpus()))

        self.model = model
        return model
コード例 #2
0
def get_uncompiled_model(input_shape, num_classes, backbone, infer=False):

    model = Deeplabv3(weights=None, input_tensor=None, infer=infer,
                      input_shape=input_shape, classes=num_classes,
                      backbone=backbone, OS=16, alpha=1)


    base_model = Model(model.input, model.layers[-5].output)
    #self.net = net
    #modelpath = 'weights/{}_{}.h5'.format(backbone, net)

    if backbone=='xception':
        scale = 4
    else:
        scale = 8

    #elif net == 'subpixel':
    x = Subpixel(num_classes, 1, scale, padding='same')(base_model.output)
    #x = Reshape((input_shape[0]*input_shape[1], -1)) (x)
    #x = Reshape((input_shape[0]*input_shape[1], num_classes)) (x)
    x = Activation('softmax', name = 'pred_mask')(x)
    model = Model(base_model.input, x, name='deeplabv3p_subpixel')

    # Do ICNR
    for layer in model.layers:
        if type(layer) == Subpixel:
            c, b = layer.get_weights()
            w = ICNR(scale=scale)(shape=c.shape)
            #W = tf.convert_to_tensor(w, dtype=tf.float32)
            layer.set_weights([w, b])

    return model
コード例 #3
0
ファイル: utils.py プロジェクト: xiangweifeng/deeplabv3_keras
    def create_seg_model(self,
                         opt,
                         net,
                         load_weights=False,
                         multi_gpu=True,
                         to_compile=True):

        self.net = net
        n_classes = len(get_VOC2012_classes()) - 1

        if net == 'enet':
            self.modelpath = 'weights/enet.h5'
            model, _ = build(nc=n_classes,
                             w=self.sz[1],
                             h=self.sz[0],
                             plot=False)
        elif net == 'mobileunet':
            self.modelpath = 'weights/mobileunet.h5'
            model = MobileUNet(input_shape=(self.sz[1], self.sz[0], 3),
                               alpha=1,
                               alpha_up=1,
                               depth_multiplier=1,
                               n_classes=n_classes)
        elif net == 'unet':
            self.modelpath = 'weights/unet.h5'
            model = create_unet(self.sz, n_classes)
        elif net == 'segnet':
            self.modelpath = 'weights/segnet.h5'
            model = create_segnet(self.sz, n_classes)
        elif net == 'tiramisu':
            self.modelpath = 'weights/tiramisu.h5'
            model = Tiramisu(self.sz + (3, ), n_classes)
        else:
            self.modelpath = 'weights/deeplabv3.h5'
            model = Deeplabv3(weights=None,
                              input_tensor=None,
                              input_shape=self.sz + (3, ),
                              classes=21,
                              backbone='mobilenetv2',
                              OS=16,
                              alpha=1,
                              use_coordconv=self.coords)
        if self.coords:
            self.modelpath = 'weights/deeplabv3_coordconv.h5'

        if multi_gpu:
            from keras.utils import multi_gpu_model
            model = multi_gpu_model(model, gpus=len(get_available_gpus()))

        if to_compile:
            model.compile(
                optimizer=opt,
                sample_weight_mode="temporal",
                loss=softmax_sparse_crossentropy_ignoring_last_label,
                #loss = wisense_loss(30),
                metrics=[
                    Mean_IOU, background_sparse_accuracy,
                    sparse_accuracy_ignoring_last_label,
                    foreground_sparse_accuracy, 'acc'
                ])
        if load_weights:
            self.load_weights(model)
        return model