Ejemplo n.º 1
0
def _obtain_model(model_backend_name, violation_class,
                  nb_of_conv_layers_to_fine_tune):

    if model_backend_name == 'VGG16':
        model = HRA_VGG16(
            weights='HRA',
            violation_class=violation_class,
            nb_of_conv_layers_to_fine_tune=nb_of_conv_layers_to_fine_tune)

    elif model_backend_name == 'VGG19':
        model = HRA_VGG19(
            weights='HRA',
            violation_class=violation_class,
            nb_of_conv_layers_to_fine_tune=nb_of_conv_layers_to_fine_tune)

    elif model_backend_name == 'ResNet50':
        model = HRA_ResNet50(
            weights='HRA',
            violation_class=violation_class,
            nb_of_conv_layers_to_fine_tune=nb_of_conv_layers_to_fine_tune)

    elif model_backend_name == 'VGG16_Places365':
        model = HRA_VGG16_Places365(
            weights='HRA',
            violation_class=violation_class,
            nb_of_conv_layers_to_fine_tune=nb_of_conv_layers_to_fine_tune)

    return model
def single_img_HRA_inference_return_only(img_path, violation_class,
                                         model_backend_name,
                                         nb_of_conv_layers_to_fine_tune):
    """Performs single image inference.

    # Arguments
        img_path: Path to image file
        violation_class: one of `cl` (HRA dataset with 2 classes - [i]'child_labour' and [ii]'no violation')
            or `dp` (HRA dataset with 2 classes - [i]'displaced_populations' and [ii]'no violation')
        model_backend_name: One of `VGG16`, `VGG19`, `ResNet50` or `VGG16_Places365`.
        nb_of_conv_layers_to_fine_tune: integer to indicate the number of convolutional layers to fine-tune.
    # Returns
        Three integer values corresponding to `valence`, `arousal` and `dominance`.

    """

    if model_backend_name == 'VGG16':
        model = HRA_VGG16(
            weights='HRA',
            violation_class=violation_class,
            nb_of_conv_layers_to_fine_tune=nb_of_conv_layers_to_fine_tune)

    elif model_backend_name == 'VGG19':
        model = HRA_VGG19(
            weights='HRA',
            violation_class=violation_class,
            nb_of_conv_layers_to_fine_tune=nb_of_conv_layers_to_fine_tune)

    elif model_backend_name == 'ResNet50':
        model = HRA_ResNet50(
            weights='HRA',
            violation_class=violation_class,
            nb_of_conv_layers_to_fine_tune=nb_of_conv_layers_to_fine_tune)

    elif model_backend_name == 'VGG16_Places365':
        model = HRA_VGG16_Places365(
            weights='HRA',
            violation_class=violation_class,
            nb_of_conv_layers_to_fine_tune=nb_of_conv_layers_to_fine_tune)

    ## Uncomment for extra verbosity
    # print('[INFO] HRA-2class model has been loaded')

    img = image.load_img(img_path, target_size=(224, 224))

    from applications.hra_utils import predict as pd
    raw_preds, decoded_preds = pd(violation_class=violation_class,
                                  model=model,
                                  img=img,
                                  target_size=(224, 224))

    return raw_preds
Ejemplo n.º 3
0
    # ---------------------------------------------------- #



    if violation_class == 'cl':
        main_test_dir = '/home/sandbox/Desktop/HRA-2clas-full-test/ChildLabour'
    elif violation_class =='dp':
        main_test_dir = '/home/sandbox/Desktop/HRA-2clas-full-test/DisplacedPopulations'

    if model_backend_name == 'ResNet50':
        model = HRA_ResNet50(include_top=True, weights='HRA',
                             nb_of_conv_layers_to_fine_tune=nb_of_conv_layers_to_fine_tune,
                             violation_class=violation_class)
    elif model_backend_name == 'VGG16':
        model = HRA_VGG16(include_top=True, weights='HRA',
                          nb_of_conv_layers_to_fine_tune=nb_of_conv_layers_to_fine_tune,
                          violation_class=violation_class)
    elif model_backend_name == 'VGG19':
        model = HRA_VGG19(include_top=True, weights='HRA',
                          nb_of_conv_layers_to_fine_tune=nb_of_conv_layers_to_fine_tune,
                          violation_class=violation_class)
    elif model_backend_name == 'VGG16_Places365':
        model = HRA_VGG16_Places365(include_top=True, weights='HRA',
                                    nb_of_conv_layers_to_fine_tune=nb_of_conv_layers_to_fine_tune,
                                    violation_class=violation_class)




    base_evaluator = BaseEvaluator(model=model,
                                   model_backend_name=model_backend_name,
                        help='Number of predictions returned by the model')
    parser.add_argument("--to_file",
                        type=str,
                        help='name to save the super imposed image to disk')

    args = parser.parse_args()
    return args


# model = baseline_model(classes=9, epochs=40, weights='HRA')

args = get_args()

if args.pre_trained_model == 'VGG16':
    model = HRA_VGG16(weights='HRA',
                      mode='fine_tuning',
                      pooling_mode=args.pooling_mode)
    layer2visualise = 'block5_conv3'

elif args.pre_trained_model == 'VGG19':
    model = HRA_VGG19(weights='HRA',
                      mode='fine_tuning',
                      pooling_mode=args.pooling_mode)
    layer2visualise = 'block5_conv3'

elif args.pre_trained_model == 'ResNet50':
    model = HRA_ResNet50(weights='HRA',
                         mode='fine_tuning',
                         pooling_mode=args.pooling_mode)

elif args.pre_trained_model == 'VGG16_Places365':
def single_img_HRA_inference(img_path, violation_class, model_backend_name,
                             nb_of_conv_layers_to_fine_tune):
    """Performs single image inference.

    # Arguments
        img_path: Path to image file
        violation_class: one of `cl` (HRA dataset with 2 classes - [i]'child_labour' and [ii]'no violation')
            or `dp` (HRA dataset with 2 classes - [i]'displaced_populations' and [ii]'no violation')
        model_backend_name: One of `VGG16`, `VGG19`, `ResNet50` or `VGG16_Places365`.
        nb_of_conv_layers_to_fine_tune: integer to indicate the number of convolutional layers to fine-tune.
    # Returns
        Three integer values corresponding to `valence`, `arousal` and `dominance`.

    """
    (head, tail) = os.path.split(img_path)
    filename_only = os.path.splitext(tail)[0]

    if model_backend_name == 'VGG16':
        model = HRA_VGG16(
            weights='HRA',
            violation_class=violation_class,
            nb_of_conv_layers_to_fine_tune=nb_of_conv_layers_to_fine_tune)

        print("[INFO] Loading and preprocessing image...")

        x = prepare_input_data(img_path=img_path,
                               objects_or_places_flag='objects')

    elif model_backend_name == 'VGG19':
        model = HRA_VGG19(
            weights='HRA',
            violation_class=violation_class,
            nb_of_conv_layers_to_fine_tune=nb_of_conv_layers_to_fine_tune)

        print("[INFO] Loading and preprocessing image...")

        x = prepare_input_data(img_path=img_path,
                               objects_or_places_flag='objects')

    elif model_backend_name == 'ResNet50':
        model = HRA_ResNet50(
            weights='HRA',
            violation_class=violation_class,
            nb_of_conv_layers_to_fine_tune=nb_of_conv_layers_to_fine_tune)

        print("[INFO] Loading and preprocessing image...")

        x = prepare_input_data(img_path=img_path,
                               objects_or_places_flag='objects')

    elif model_backend_name == 'VGG16_Places365':
        model = HRA_VGG16_Places365(
            weights='HRA',
            violation_class=violation_class,
            nb_of_conv_layers_to_fine_tune=nb_of_conv_layers_to_fine_tune)

        print("[INFO] Loading and preprocessing image...")

        x = prepare_input_data(img_path=img_path,
                               objects_or_places_flag='places')

    # There seems to be relatively high confidence scores with the following method
    # preds = model.predict(x)[0]
    #
    # print ('Raw predictions: ', preds)
    #
    # top_preds = np.argsort(preds)[::-1][0:2]
    #
    # print('Sorted predictions: ', top_preds)
    #
    # if violation_class == 'cl':
    #     file_name = 'categories_HRA_2classCL.txt'
    # elif violation_class == 'dp':
    #     file_name = 'categories_HRA_2classDP.txt'
    #
    # classes = list()
    # with open(file_name) as class_file:
    #     for line in class_file:
    #         classes.append(line.strip().split(' ')[0][3:])
    # classes = tuple(classes)
    #
    #
    # print ('\n')
    # print('--PREDICTED HRA 2 CLASSES:')
    # # output the prediction
    # for i in range(0, 2):
    #     print(classes[top_preds[i]], '->', preds[top_preds[i]])

    img = image.load_img(img_path, target_size=(224, 224))

    from applications.hra_utils import predict as pd
    raw_preds, decoded_preds = pd(violation_class=violation_class,
                                  model=model,
                                  img=img,
                                  target_size=(224, 224))

    # print('Raw preds: ', raw_preds)
    # print ('Decoded preds: ', decoded_preds)

    # print (type(raw_preds))
    # print('Raw preds: ', raw_preds[0])
    # print(type(raw_preds[0]))

    top_1_predicted_probability = decoded_preds[0][2]

    # top_1_predicted = np.argmax(preds)
    top_1_predicted_label = decoded_preds[0][1]
    # print(top_1_predicted_label, '->' , top_1_predicted_probability)

    overlayed_text = str(top_1_predicted_label) + ' (' + str(
        round(top_1_predicted_probability, 2)) + ')'

    return raw_preds, overlayed_text, top_1_predicted_label
# ==== CompoundNet model / Early-fusion==========================================================================================================
# model= CompoundNet_VGG16(weights='HRA', mode= 'fine_tuning', fusion_strategy='average',  pooling_mode='avg', data_augm_enabled=False)
# model.summary()
# ===============================================================================================================================================


# ==== Object-centric CompoundNet model =========================================================================================================
# model= CompoundNet_VGG16_VGG19(weights='HRA', mode= 'fine_tuning', fusion_strategy='maximum',  pooling_mode='max')
# model.summary()
# ===============================================================================================================================================


# ==== Late-fusion =========================================================================================================
# pooling_mode = 'max'

model_a = HRA_VGG16(weights='HRA', mode='fine_tuning', pooling_mode='max', include_top=True, data_augm_enabled=False)
model_a.summary()

model_b = HRA_VGG16_Places365(weights='HRA', mode='fine_tuning', pooling_mode='flatten', include_top=True, data_augm_enabled=False)
model_b.summary()

# ===============================================================================================================================================



model_name='BEST_COVERAGE_late_fusion'



metrics = HRA_metrics(main_test_dir ='/home/sandbox/Desktop/Human_Rights_Archive_DB/test_uniform')
Ejemplo n.º 7
0
    def train(self):
        """Loads the selected model & starts the training process.
        """

        if self.pre_trained_model == 'vgg16':

            print('[INFO] Instantiating HRA-2CLASS-VGG16...')

            if self.train_mode == 'feature_extraction':

                model = HRA_VGG16(include_top=True,
                                  weights=None,
                                  input_tensor=None,
                                  input_shape=None,
                                  nb_of_conv_layers_to_fine_tune=self.
                                  nb_of_conv_layers_to_fine_tune,
                                  first_phase_trained_weights=None,
                                  verbose=1)
            else:

                if os.path.isfile(self.first_phase_trained_weights) is False:
                    raise IOError("No such weights file: `" +
                                  self.first_phase_trained_weights + "`. ")

                model = HRA_VGG16(include_top=True,
                                  weights=None,
                                  input_tensor=None,
                                  input_shape=None,
                                  nb_of_conv_layers_to_fine_tune=self.
                                  nb_of_conv_layers_to_fine_tune,
                                  first_phase_trained_weights=self.
                                  first_phase_trained_weights,
                                  verbose=1)

            print('[INFO] HRA-2CLASS-VGG16 model loaded')

        elif self.pre_trained_model == 'vgg19':

            print('[INFO] Instantiating HRA-2CLASS-VGG19...')

            if self.train_mode == 'feature_extraction':

                model = HRA_VGG19(include_top=True,
                                  weights=None,
                                  input_tensor=None,
                                  input_shape=None,
                                  nb_of_conv_layers_to_fine_tune=self.
                                  nb_of_conv_layers_to_fine_tune,
                                  first_phase_trained_weights=None,
                                  verbose=1)
            else:

                if os.path.isfile(self.first_phase_trained_weights) is False:
                    raise IOError("No such weights file: `" +
                                  self.first_phase_trained_weights + "`. ")

                model = HRA_VGG19(include_top=True,
                                  weights=None,
                                  input_tensor=None,
                                  input_shape=None,
                                  nb_of_conv_layers_to_fine_tune=self.
                                  nb_of_conv_layers_to_fine_tune,
                                  first_phase_trained_weights=self.
                                  first_phase_trained_weights,
                                  verbose=1)

            print('[INFO] HRA-2CLASS-VGG19 model loaded')

        elif self.pre_trained_model == 'resnet50':

            print('[INFO] Instantiating HRA-2CLASS-ResNet50...')

            if self.train_mode == 'feature_extraction':

                model = HRA_ResNet50(include_top=True,
                                     weights=None,
                                     input_tensor=None,
                                     input_shape=None,
                                     nb_of_conv_layers_to_fine_tune=self.
                                     nb_of_conv_layers_to_fine_tune,
                                     first_phase_trained_weights=None,
                                     verbose=1)
            else:

                if os.path.isfile(self.first_phase_trained_weights) is False:
                    raise IOError("No such weights file: `" +
                                  self.first_phase_trained_weights + "`. ")

                model = HRA_ResNet50(include_top=True,
                                     weights=None,
                                     input_tensor=None,
                                     input_shape=None,
                                     nb_of_conv_layers_to_fine_tune=self.
                                     nb_of_conv_layers_to_fine_tune,
                                     first_phase_trained_weights=self.
                                     first_phase_trained_weights,
                                     verbose=1)

            print('[INFO] HRA-2CLASS-ResNet50 model loaded')

        elif self.pre_trained_model == 'vgg16_places365':

            print('[INFO] Instantiating HRA-2CLASS-VGG16_Places365...')

            if self.train_mode == 'feature_extraction':

                model = HRA_VGG16_Places365(
                    include_top=True,
                    weights=None,
                    input_tensor=None,
                    input_shape=None,
                    nb_of_conv_layers_to_fine_tune=self.
                    nb_of_conv_layers_to_fine_tune,
                    first_phase_trained_weights=None,
                    verbose=1)
            else:

                if os.path.isfile(self.first_phase_trained_weights) is False:
                    raise IOError("No such weights file: `" +
                                  self.first_phase_trained_weights + "`. ")

                model = HRA_VGG16_Places365(
                    include_top=True,
                    weights=None,
                    input_tensor=None,
                    input_shape=None,
                    nb_of_conv_layers_to_fine_tune=self.
                    nb_of_conv_layers_to_fine_tune,
                    first_phase_trained_weights=self.
                    first_phase_trained_weights,
                    verbose=1)

            print('[INFO] HRA-2CLASS-VGG16-Places365 model loaded')

        # Finally start fitting the dataset

        if self.train_mode == 'feature_extraction':
            print(
                '[INFO] Start training the randomly initialised classifier on top of the pre-trained conv. base...'
            )

            start_time = time.time()

            history = model.fit_generator(self.train_generator,
                                          epochs=self.nb_of_epochs,
                                          steps_per_epoch=self.steps_per_epoch,
                                          validation_data=self.val_generator,
                                          validation_steps=self.val_steps,
                                          callbacks=self.callbacks_list)

            end_time = time.time()
            print(
                "[INFO] It took {} to train the randomly initialised classifier on top of the pre-trained conv. base"
                .format(hms_string(end_time - start_time)))

            print('[INFO] Saved trained model as: %s ' % self.weights_to_file)

        else:
            print('[INFO] Start fine-tuning the model...')

            start_time = time.time()

            history = model.fit_generator(self.train_generator,
                                          epochs=self.nb_of_epochs,
                                          steps_per_epoch=self.steps_per_epoch,
                                          validation_data=self.val_generator,
                                          validation_steps=self.val_steps,
                                          callbacks=self.callbacks_list)

            end_time = time.time()
            print(
                "[INFO] It took {} to fine-tune the top layers of the frozen conv. base"
                .format(hms_string(end_time - start_time)))

            print('[INFO] Saved trained model as: %s ' % self.weights_to_file)