Ejemplo n.º 1
0
    def make_segmentor(self):
        segmentor = build_segnet(self.img_shape,
                                 self.n_classes,
                                 l2_reg=0.,
                                 init='glorot_uniform',
                                 path_weights=None,
                                 freeze_layers_from=None,
                                 use_unpool=False,
                                 basic=False)
        lr = 1e-04
        optimizer = RMSprop(lr=lr, rho=0.9, epsilon=1e-8, clipnorm=10)
        print(
            '   Optimizer segmentor: rmsprop. Lr: {}. Rho: 0.9, epsilon=1e-8, '
            'clipnorm=10'.format(lr))
        the_loss = cce_flatt(self.cf.dataset.void_class,
                             self.cf.dataset.cb_weights)
        metrics = [IoU(self.cf.dataset.n_classes, self.cf.dataset.void_class)]

        segmentor.compile(loss=the_loss, metrics=metrics, optimizer=optimizer)
        return segmentor
Ejemplo n.º 2
0
    def make_one_net_model(self, cf, in_shape, loss, metrics, optimizer):
        # Create the *Keras* model
        if cf.model_name == 'fcn8':
            model = build_fcn8(in_shape, cf.dataset.n_classes, cf.weight_decay,
                               freeze_layers_from=cf.freeze_layers_from,
                               path_weights=cf.load_imageNet)
        elif cf.model_name == 'unet':
            model = build_unet(in_shape, cf.dataset.n_classes, cf.weight_decay,
                               freeze_layers_from=cf.freeze_layers_from,
                               path_weights=None)
        elif cf.model_name == 'segnet_basic':
            model = build_segnet(in_shape, cf.dataset.n_classes, cf.weight_decay,
                                 freeze_layers_from=cf.freeze_layers_from,
                                 path_weights=None, basic=True)
        elif cf.model_name == 'segnet_vgg':
            model = build_segnet(in_shape, cf.dataset.n_classes, cf.weight_decay,
                                 freeze_layers_from=cf.freeze_layers_from,
                                 path_weights=None, basic=False)
        elif cf.model_name == 'resnetFCN':
            model = build_resnetFCN(in_shape, cf.dataset.n_classes, cf.weight_decay,
                                    freeze_layers_from=cf.freeze_layers_from,
                                    path_weights=cf.load_imageNet)
        elif cf.model_name == 'densenetFCN':
            model = build_densenetFCN(in_shape, cf.dataset.n_classes, cf.weight_decay,
                                      freeze_layers_from=cf.freeze_layers_from,
                                      path_weights=None)
        elif cf.model_name == 'lenet':
            model = build_lenet(in_shape, cf.dataset.n_classes, cf.weight_decay)
        elif cf.model_name == 'alexNet':
            model = build_alexNet(in_shape, cf.dataset.n_classes, cf.weight_decay)
        elif cf.model_name == 'vgg16':
            model = build_vgg(in_shape, cf.dataset.n_classes, 16, cf.weight_decay,
                              load_pretrained=cf.load_imageNet,
                              freeze_layers_from=cf.freeze_layers_from)
        elif cf.model_name == 'vgg19':
            model = build_vgg(in_shape, cf.dataset.n_classes, 19, cf.weight_decay,
                              load_pretrained=cf.load_imageNet,
                              freeze_layers_from=cf.freeze_layers_from)
        elif cf.model_name == 'resnet50':
            model = build_resnet50(in_shape, cf.dataset.n_classes, cf.weight_decay,
                                   load_pretrained=cf.load_imageNet,
                                   freeze_layers_from=cf.freeze_layers_from)
        elif cf.model_name == 'InceptionV3':
            model = build_inceptionV3(in_shape, cf.dataset.n_classes,
                                      cf.weight_decay,
                                      load_pretrained=cf.load_imageNet,
                                      freeze_layers_from=cf.freeze_layers_from)
        elif cf.model_name == 'yolo':
            model = build_yolo(in_shape, cf.dataset.n_classes,
                               cf.dataset.n_priors,
                               load_pretrained=cf.load_imageNet,
                               freeze_layers_from=cf.freeze_layers_from, tiny=False)
        elif cf.model_name == 'tiny-yolo':
            model = build_yolo(in_shape, cf.dataset.n_classes,
                               cf.dataset.n_priors,
                               load_pretrained=cf.load_imageNet,
                               freeze_layers_from=cf.freeze_layers_from, tiny=True)
        else:
            raise ValueError('Unknown model')

        # Load pretrained weights
        if cf.load_pretrained:
            print('   loading model weights from: ' + cf.weights_file + '...')
            model.load_weights(cf.weights_file, by_name=True)

        # Compile model
        model.compile(loss=loss, metrics=metrics, optimizer=optimizer)

        # Show model structure
        if cf.show_model:
            model.summary()
            plot(model, to_file=os.path.join(cf.savepath, 'model.png'))

        # Output the model
        print ('   Model: ' + cf.model_name)
        # model is a keras model, Model is a class wrapper so that we can have
        # other models (like GANs) made of a pair of keras models, with their
        # own ways to train, test and predict
        return One_Net_Model(model, cf, optimizer)
Ejemplo n.º 3
0
    def make_one_net_model(self, cf, in_shape, loss, metrics, optimizer):
        # Assertions
        if 'tiramisu' in cf.model_name:
            input_rows, input_cols = cf.target_size_train[0], cf.target_size_train[1]
            multiple = 2 ** 5  # 5 transition blocks
            if input_rows is not None:
                if input_rows % multiple != 0:
                    raise ValueError('The number of rows of the input data must be a multiple of {}'.format(multiple))
            if input_cols is not None:
                if input_cols % multiple != 0:
                    raise ValueError(
                        'The number of columns of the input data must be a multiple of {}'.format(multiple))

        # Create the *Keras* model
        if cf.model_name == 'fcn8':
            model = build_fcn8(in_shape, cf.dataset.n_classes, cf.weight_decay,
                               freeze_layers_from=cf.freeze_layers_from,
                               # path_weights='weights/pascal-fcn8s-dag.mat')
                               path_weights=None)
        elif cf.model_name == 'dilation':
            model = build_dilation(in_shape, cf.dataset.n_classes, cf.weight_decay,
                                   freeze_layers_from=cf.freeze_layers_from,
                                   # path_weights='weights/pascal-fcn8s-dag.mat')
                                   path_weights=None)
        elif cf.model_name == 'segnet_basic':
            model = build_segnet(in_shape, cf.dataset.n_classes, cf.weight_decay,
                                 freeze_layers_from=cf.freeze_layers_from,
                                 path_weights=None, basic=True)
        elif cf.model_name == 'segnet_vgg':
            model = build_segnet(in_shape, cf.dataset.n_classes, cf.weight_decay,
                                 freeze_layers_from=cf.freeze_layers_from,
                                 path_weights=None, basic=False)
        elif cf.model_name == 'densenetFCN':
            model = build_densenetFCN(in_shape, cf.dataset.n_classes, cf.weight_decay,
                                      freeze_layers_from=cf.freeze_layers_from)
        elif cf.model_name == 'vgg16':
            model = build_vgg(in_shape, cf.dataset.n_classes, 16, cf.weight_decay,
                              load_pretrained=cf.load_imageNet,
                              freeze_layers_from=cf.freeze_layers_from)
        elif cf.model_name == 'vgg19':
            model = build_vgg(in_shape, cf.dataset.n_classes, 19, cf.weight_decay,
                              load_pretrained=cf.load_imageNet,
                              freeze_layers_from=cf.freeze_layers_from)
        elif cf.model_name == 'resnet50':
            model = build_resnet50(in_shape, cf.dataset.n_classes, cf.weight_decay,
                                   load_pretrained=cf.load_imageNet,
                                   freeze_layers_from=cf.freeze_layers_from)
        elif cf.model_name == 'yolo':
            model = build_yolo(in_shape, cf.dataset.n_classes,
                               cf.dataset.n_priors,
                               load_pretrained=cf.load_imageNet,
                               freeze_layers_from=cf.freeze_layers_from, tiny=False)
        elif cf.model_name == 'tiny-yolo':
            model = build_yolo(in_shape, cf.dataset.n_classes,
                               cf.dataset.n_priors,
                               load_pretrained=cf.load_imageNet,
                               freeze_layers_from=cf.freeze_layers_from, tiny=True)
        elif cf.model_name == 'ssd300':
            model = build_ssd300(in_shape, cf.dataset.n_classes + 1, cf.weight_decay,
                                 load_pretrained=cf.load_imageNet, freeze_layers_from=cf.freeze_layers_from)
        elif cf.model_name == 'deeplabV2':
            model = build_deeplabv2(in_shape, nclasses=cf.dataset.n_classes, load_pretrained=cf.load_imageNet,
                                    freeze_layers_from=cf.freeze_layers_from, weight_decay=cf.weight_decay)
        elif cf.model_name == 'ssd300':
            model = build_ssd300(in_shape, cf.dataset.n_classes + 1, cf.weight_decay,
                                 load_pretrained=cf.load_imageNet,
                                 freeze_layers_from=cf.freeze_layers_from)
        elif cf.model_name == 'tiramisu_fc56':
            model = build_tiramisu_fc56(in_shape, cf.dataset.n_classes, cf.weight_decay,
                                        compression=0, dropout=0.2, nb_filter=48,
                                        freeze_layers_from=cf.freeze_layers_from)
        elif cf.model_name == 'tiramisu_fc67':
            model = build_tiramisu_fc67(in_shape, cf.dataset.n_classes, cf.weight_decay,
                                        compression=0, dropout=0.2, nb_filter=48,
                                        freeze_layers_from=cf.freeze_layers_from)

        elif cf.model_name == 'tiramisu_fc103':
            model = build_tiramisu_fc103(in_shape, cf.dataset.n_classes, cf.weight_decay,
                                         compression=0, dropout=0.2, nb_filter=48,
                                         freeze_layers_from=cf.freeze_layers_from)

        else:
            raise ValueError('Unknown model')

        # Load pretrained weights
        if cf.load_pretrained:
            print('   loading model weights from: ' + cf.weights_file + '...')
            model.load_weights(cf.weights_file, by_name=True)

        # Compile model
        model.compile(loss=loss, metrics=metrics, optimizer=optimizer)

        # Show model structure
        if cf.show_model:
            model.summary()
            plot(model, to_file=os.path.join(cf.savepath, 'model.png'))

        # Output the model
        print ('   Model: ' + cf.model_name)
        # model is a keras model, Model is a class wrapper so that we can have
        # other models (like GANs) made of a pair of keras models, with their
        # own ways to train, test and predict
        return One_Net_Model(model, cf, optimizer)
Ejemplo n.º 4
0
    def make_one_net_model(self, cf, in_shape, loss, metrics, optimizer):
        # Create the *Keras* model
        if cf.model_name == 'fcn8':
            model = build_fcn8(in_shape, cf.dataset.n_classes, cf.weight_decay,
                               freeze_layers_from=cf.freeze_layers_from,
                               path_weights=cf.load_imageNet)
        elif cf.model_name == 'unet':
            model = build_unet(in_shape, cf.dataset.n_classes, cf.weight_decay,
                               freeze_layers_from=cf.freeze_layers_from,
                               path_weights=None)
        elif cf.model_name == 'segnet_basic':
            model = build_segnet(in_shape, cf.dataset.n_classes, cf.weight_decay,
                                 freeze_layers_from=cf.freeze_layers_from,
                                 basic=True)
        elif cf.model_name == 'segnet_vgg':
            model = build_segnet(in_shape, cf.dataset.n_classes, cf.weight_decay,
                                 freeze_layers_from=cf.freeze_layers_from,
                                 basic=False)
        elif cf.model_name == 'resnetFCN':
            model = build_resnetFCN(in_shape, cf.dataset.n_classes, cf.weight_decay,
                                    freeze_layers_from=cf.freeze_layers_from,
                                    path_weights=None)
        elif cf.model_name == 'inceptionFCN':
            model = build_inceptionFCN(in_shape, cf.dataset.n_classes, cf.weight_decay,
                                    freeze_layers_from=cf.freeze_layers_from,
                                    path_weights=None)									
        elif cf.model_name == 'densenet':
            model = build_densenet(in_shape, cf.dataset.n_classes, cf.weight_decay,
                                      freeze_layers_from=cf.freeze_layers_from,
                                      path_weights=None)
        elif cf.model_name == 'lenet':
            model = build_lenet(in_shape, cf.dataset.n_classes, cf.weight_decay)
        elif cf.model_name == 'alexNet':
            model = build_alexNet(in_shape, cf.dataset.n_classes, cf.weight_decay)
        elif cf.model_name == 'vgg16':
            model = build_vgg(in_shape, cf.dataset.n_classes, 16, cf.weight_decay,
                              load_imageNet=cf.load_imageNet,
                              freeze_layers_from=cf.freeze_layers_from)
        elif cf.model_name == 'vgg19':
            model = build_vgg(in_shape, cf.dataset.n_classes, 19, cf.weight_decay,
                              load_imageNet=cf.load_imageNet,
                              freeze_layers_from=cf.freeze_layers_from)
        elif cf.model_name == 'resnet50':
            model = build_resnet50(in_shape, cf.dataset.n_classes, cf.weight_decay,
                                   load_imageNet=cf.load_imageNet,
                                   freeze_layers_from=cf.freeze_layers_from)
        elif cf.model_name == 'InceptionV3':
            model = build_inceptionV3(in_shape, cf.dataset.n_classes,
                                      cf.weight_decay,
                                      load_imageNet=cf.load_imageNet,
                                      freeze_layers_from=cf.freeze_layers_from)
        elif cf.model_name == 'yolo':
            model = build_yolo(in_shape, cf.dataset.n_classes,
                               cf.dataset.n_priors,
                               load_imageNet=cf.load_imageNet,
                               freeze_layers_from=cf.freeze_layers_from, tiny=False)
        elif cf.model_name == 'tiny-yolo':
            model = build_yolo(in_shape, cf.dataset.n_classes,
                               cf.dataset.n_priors,
                               load_imageNet=cf.load_imageNet,
                               freeze_layers_from=cf.freeze_layers_from, tiny=True)
        elif cf.model_name == 'ssd':
            model = build_ssd(in_shape, cf.dataset.n_classes+1,
                              cf.dataset.n_priors,
                              freeze_layers_from=cf.freeze_layers_from)
        elif cf.model_name == 'densenet_segmentation':
            model = build_densenet_segmentation(in_shape, cf.dataset.n_classes, weight_decay = cf.weight_decay,
                   freeze_layers_from = cf.freeze_layers_from, path_weights = cf.load_imageNet)
        else:
            raise ValueError('Unknown model')

        # Load pretrained weights
        if cf.load_pretrained:
            print('   loading model weights from: ' + cf.weights_file + '...')
            # If the weights are from different datasets
            if cf.different_datasets:
                if cf.freeze_layers_from == 'base_model':
                    raise TypeError('Please, enter the layer id instead of "base_model"'
                          ' for the freeze_layers_from config parameter')
                croppedmodel = model_from_json(model.to_json())
                # Remove not frozen layers
                for i in range(len(model.layers[cf.freeze_layers_from:])):
                    croppedmodel.layers.pop()
                # Load weights only for the frozen layers
                croppedmodel.load_weights(cf.weights_file, by_name=True)
                model.set_weights(croppedmodel.get_weights())
            else:
                model.load_weights(cf.weights_file, by_name=True)

        # Compile model
        model.compile(loss=loss, metrics=metrics, optimizer=optimizer)

        # Show model structure
        if cf.show_model:
            model.summary()
            plot(model, to_file=os.path.join(cf.savepath, 'model.png'))

        # Output the model
        print ('   Model: ' + cf.model_name)
        # model is a keras model, Model is a class wrapper so that we can have
        # other models (like GANs) made of a pair of keras models, with their
        # own ways to train, test and predict
        return One_Net_Model(model, cf, optimizer)
Ejemplo n.º 5
0
    def make_one_net_model(self, cf, in_shape, loss, metrics, optimizer):
        # Create the *Keras* model
        model_name = cf.model_name
        if cf.model_name == 'fcn8':
            model = build_fcn8(in_shape,
                               cf.dataset.n_classes,
                               cf.weight_decay,
                               freeze_layers_from=cf.freeze_layers_from,
                               path_weights=cf.load_imageNet)
        elif cf.model_name == 'unet':
            model = build_unet(in_shape,
                               cf.dataset.n_classes,
                               cf.weight_decay,
                               freeze_layers_from=cf.freeze_layers_from,
                               path_weights=None)
        elif cf.model_name == 'segnet_basic':
            model = build_segnet(in_shape,
                                 cf.dataset.n_classes,
                                 cf.weight_decay,
                                 freeze_layers_from=cf.freeze_layers_from,
                                 path_weights=None,
                                 basic=True)
        elif cf.model_name == 'segnet_vgg':
            model = build_segnet(in_shape,
                                 cf.dataset.n_classes,
                                 cf.weight_decay,
                                 freeze_layers_from=cf.freeze_layers_from,
                                 path_weights=None,
                                 basic=False)
        elif cf.model_name == 'resnetFCN':
            model = build_resnetFCN(in_shape,
                                    cf.dataset.n_classes,
                                    cf.weight_decay,
                                    freeze_layers_from=cf.freeze_layers_from,
                                    path_weights=None)
        elif cf.model_name == 'densenetFCN':
            model = build_densenetFCN(in_shape,
                                      cf.dataset.n_classes,
                                      cf.weight_decay,
                                      freeze_layers_from=cf.freeze_layers_from,
                                      path_weights=None)
        elif cf.model_name == 'densenet_fc':
            model = DenseNetFCN((224, 224, 3),
                                nb_dense_block=5,
                                growth_rate=16,
                                nb_layers_per_block=4,
                                upsampling_type='upsampling',
                                classes=cf.dataset.n_classes)
        elif cf.model_name == 'lenet':
            model = build_lenet(in_shape, cf.dataset.n_classes,
                                cf.weight_decay)
        elif cf.model_name == 'alexNet':
            model = build_alexNet(in_shape, cf.dataset.n_classes,
                                  cf.weight_decay)
        elif cf.model_name == 'vgg16':
            model = build_vgg(in_shape,
                              cf.dataset.n_classes,
                              16,
                              cf.weight_decay,
                              load_pretrained=cf.load_imageNet,
                              freeze_layers_from=cf.freeze_layers_from)
        elif cf.model_name == 'vgg19':
            model = build_vgg(in_shape,
                              cf.dataset.n_classes,
                              19,
                              cf.weight_decay,
                              load_pretrained=cf.load_imageNet,
                              freeze_layers_from=cf.freeze_layers_from)
        elif cf.model_name == 'resnet50Keras':
            model = build_resnet50(in_shape,
                                   cf.dataset.n_classes,
                                   cf.weight_decay,
                                   load_pretrained=cf.load_imageNet,
                                   freeze_layers_from=cf.freeze_layers_from)
        elif cf.model_name == 'resnet18':
            model = ResnetBuilder.build_resnet_18(in_shape,
                                                  cf.dataset.n_classes)
        elif cf.model_name == 'resnet34':
            model = ResnetBuilder.build_resnet_34(in_shape,
                                                  cf.dataset.n_classes)
        elif cf.model_name == 'resnet50':
            model = ResnetBuilder.build_resnet_50(in_shape,
                                                  cf.dataset.n_classes)
        elif cf.model_name == 'resnet101':
            model = ResnetBuilder.build_resnet_101(in_shape,
                                                   cf.dataset.n_classes)
        elif cf.model_name == 'resnet152':
            model = ResnetBuilder.build_resnet_152(in_shape,
                                                   cf.dataset.n_classes)
        elif cf.model_name == 'InceptionV3':
            model = build_inceptionV3(in_shape,
                                      cf.dataset.n_classes,
                                      cf.weight_decay,
                                      load_pretrained=cf.load_imageNet,
                                      freeze_layers_from=cf.freeze_layers_from)
        elif cf.model_name == 'densenet':
            model = build_densenet(in_shape, cf.dataset.n_classes,
                                   cf.weight_decay)

        elif cf.model_name == 'yolo':
            model = build_yolo(in_shape,
                               cf.dataset.n_classes,
                               cf.dataset.n_priors,
                               load_pretrained=cf.load_imageNet,
                               freeze_layers_from=cf.freeze_layers_from,
                               typeNet='Regular')
        elif cf.model_name == 'tiny-yolo':
            if hasattr(cf, 'lookTwice'):
                yolt = cf.lookTwice
                if yolt:
                    model_name = 'Tiny-YOLT'
            else:
                yolt = False
            model = build_yolo(in_shape,
                               cf.dataset.n_classes,
                               cf.dataset.n_priors,
                               load_pretrained=cf.load_imageNet,
                               freeze_layers_from=cf.freeze_layers_from,
                               typeNet='Tiny',
                               lookTwice=yolt)
        elif cf.model_name == 'yolt':
            model = build_yolo(in_shape,
                               cf.dataset.n_classes,
                               cf.dataset.n_priors,
                               load_pretrained=cf.load_imageNet,
                               freeze_layers_from=cf.freeze_layers_from,
                               typeNet='YOLT')
        elif cf.model_name == 'ssd':
            model = Build_SSD(in_shape,
                              cf.dataset.n_classes + 1,
                              load_pretrained=cf.load_imageNet,
                              freeze_layers_from=cf.freeze_layers_from)

        else:
            raise ValueError('Unknown model')

        # Load pretrained weights
        if cf.load_pretrained:
            print('   loading model weights from: ' + cf.weights_file)
            model.load_weights(cf.weights_file, by_name=True)
        else:
            try:
                if cf.load_transferlearning:
                    print('   loading model weights from: ' + cf.weights_file)
                    old_name = model.layers[-2].name
                    model.layers[-2].name = model.layers[-2].name + '_replaced'
                    model.load_weights(cf.weights_file, by_name=True)
                    model.layers[-2].name = old_name
            except:
                pass
        # Compile model
        model.compile(loss=loss, metrics=metrics, optimizer=optimizer)

        # Show model structure
        if cf.show_model:
            model.summary()
            plot(model, to_file=os.path.join(cf.savepath, 'model.png'))

        # Output the model
        print('   Model: ' + model_name)
        # model is a keras model, Model is a class wrapper so that we can have
        # other models (like GANs) made of a pair of keras models, with their
        # own ways to train, test and predict
        return One_Net_Model(model, cf, optimizer)