Ejemplo n.º 1
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.º 2
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)