def __init__(self):

        self.convert_RGB_to_HSV = ConvertColor(current='RGB', to='HSV')
        self.convert_HSV_to_RGB = ConvertColor(current='HSV', to='RGB')
        self.convert_to_float32 = ConvertDataType(to='float32')
        self.convert_to_uint8 = ConvertDataType(to='uint8')
        self.convert_to_3_channels = ConvertTo3Channels()
        self.random_brightness = RandomBrightness(lower=-32,
                                                  upper=32,
                                                  prob=0.5)
        self.random_contrast = RandomContrast(lower=0.5, upper=1.5, prob=0.5)
        self.random_saturation = RandomSaturation(lower=0.5,
                                                  upper=1.5,
                                                  prob=0.5)
        self.random_hue = RandomHue(max_delta=18, prob=0.5)
        self.random_channel_swap = RandomChannelSwap(prob=0.0)

        self.sequence1 = [
            self.convert_to_3_channels, self.convert_to_float32,
            self.random_brightness, self.random_contrast,
            self.convert_to_uint8, self.convert_RGB_to_HSV,
            self.convert_to_float32, self.random_saturation, self.random_hue,
            self.convert_to_uint8, self.convert_HSV_to_RGB,
            self.random_channel_swap
        ]

        self.sequence2 = [
            self.convert_to_3_channels, self.convert_to_float32,
            self.random_brightness, self.convert_to_uint8,
            self.convert_RGB_to_HSV, self.convert_to_float32,
            self.random_saturation, self.random_hue, self.convert_to_uint8,
            self.convert_HSV_to_RGB, self.convert_to_float32,
            self.random_contrast, self.convert_to_uint8,
            self.random_channel_swap
        ]
예제 #2
0
def predict_all_to_json(out_file,
                        model,
                        img_height,
                        img_width,
                        classes_to_cats,
                        data_generator,
                        batch_size,
                        data_generator_mode='resize',
                        model_mode='training',
                        confidence_thresh=0.01,
                        iou_threshold=0.45,
                        top_k=200,
                        pred_coords='centroids',
                        normalize_coords=True):
    '''
    Runs detection predictions over the whole dataset given a model and saves them in a JSON file
    in the MS COCO detection results format.

    Arguments:
        out_file (str): The file name (full path) under which to save the results JSON file.
        model (Keras model): A Keras SSD model object.
        img_height (int): The input image height for the model.
        img_width (int): The input image width for the model.
        classes_to_cats (dict): A dictionary that maps the consecutive class IDs predicted by the model
            to the non-consecutive original MS COCO category IDs.
        data_generator (DataGenerator): A `DataGenerator` object with the evaluation dataset.
        batch_size (int): The batch size for the evaluation.
        data_generator_mode (str, optional): Either of 'resize' or 'pad'. If 'resize', the input images will
            be resized (i.e. warped) to `(img_height, img_width)`. This mode does not preserve the aspect ratios of the images.
            If 'pad', the input images will be first padded so that they have the aspect ratio defined by `img_height`
            and `img_width` and then resized to `(img_height, img_width)`. This mode preserves the aspect ratios of the images.
        model_mode (str, optional): The mode in which the model was created, i.e. 'training', 'inference' or 'inference_fast'.
            This is needed in order to know whether the model output is already decoded or still needs to be decoded. Refer to
            the model documentation for the meaning of the individual modes.
        confidence_thresh (float, optional): A float in [0,1), the minimum classification confidence in a specific
            positive class in order to be considered for the non-maximum suppression stage for the respective class.
            A lower value will result in a larger part of the selection process being done by the non-maximum suppression
            stage, while a larger value will result in a larger part of the selection process happening in the confidence
            thresholding stage.
        iou_threshold (float, optional): A float in [0,1]. All boxes with a Jaccard similarity of greater than `iou_threshold`
            with a locally maximal box will be removed from the set of predictions for a given class, where 'maximal' refers
            to the box score.
        top_k (int, optional): The number of highest scoring predictions to be kept for each batch item after the
            non-maximum suppression stage. Defaults to 200, following the paper.
        input_coords (str, optional): The box coordinate format that the model outputs. Can be either 'centroids'
            for the format `(cx, cy, w, h)` (box center coordinates, width, and height), 'minmax' for the format
            `(xmin, xmax, ymin, ymax)`, or 'corners' for the format `(xmin, ymin, xmax, ymax)`.
        normalize_coords (bool, optional): Set to `True` if the model outputs relative coordinates (i.e. coordinates in [0,1])
            and you wish to transform these relative coordinates back to absolute coordinates. If the model outputs
            relative coordinates, but you do not want to convert them back to absolute coordinates, set this to `False`.
            Do not set this to `True` if the model already outputs absolute coordinates, as that would result in incorrect
            coordinates. Requires `img_height` and `img_width` if set to `True`.

    Returns:
        None.
    '''

    convert_to_3_channels = ConvertTo3Channels()
    resize = Resize(height=img_height, width=img_width)
    if data_generator_mode == 'resize':
        transformations = [convert_to_3_channels, resize]
    elif data_generator_mode == 'pad':
        random_pad = RandomPadFixedAR(patch_aspect_ratio=img_width /
                                      img_height,
                                      clip_boxes=False)
        transformations = [convert_to_3_channels, random_pad, resize]
    else:
        raise ValueError(
            "Unexpected argument value: `data_generator_mode` can be either of 'resize' or 'pad', but received '{}'."
            .format(data_generator_mode))

    # Set the generator parameters.
    generator = data_generator.generate(
        batch_size=batch_size,
        shuffle=False,
        transformations=transformations,
        label_encoder=None,
        returns={'processed_images', 'image_ids', 'inverse_transform'},
        keep_images_without_gt=True)
    # Put the results in this list.
    results = []
    # Compute the number of batches to iterate over the entire dataset.
    n_images = data_generator.get_dataset_size()
    print("Number of images in the evaluation dataset: {}".format(n_images))
    n_batches = int(ceil(n_images / batch_size))
    # Loop over all batches.
    tr = trange(n_batches, file=sys.stdout)
    tr.set_description('Producing results file')
    for i in tr:
        # Generate batch.
        batch_X, batch_image_ids, batch_inverse_transforms = next(generator)
        # Predict.
        y_pred = model.predict(batch_X)
        # If the model was created in 'training' mode, the raw predictions need to
        # be decoded and filtered, otherwise that's already taken care of.
        if model_mode == 'training':
            # Decode.
            y_pred = decode_detections(y_pred,
                                       confidence_thresh=confidence_thresh,
                                       iou_threshold=iou_threshold,
                                       top_k=top_k,
                                       input_coords=pred_coords,
                                       normalize_coords=normalize_coords,
                                       img_height=img_height,
                                       img_width=img_width)
        else:
            # Filter out the all-zeros dummy elements of `y_pred`.
            y_pred_filtered = []
            for i in range(len(y_pred)):
                y_pred_filtered.append(y_pred[i][y_pred[i, :, 0] != 0])
            y_pred = y_pred_filtered
        # Convert the predicted box coordinates for the original images.
        y_pred = apply_inverse_transforms(y_pred, batch_inverse_transforms)

        # Convert each predicted box into the results format.
        for k, batch_item in enumerate(y_pred):
            for box in batch_item:
                class_id = box[0]
                # Transform the consecutive class IDs back to the original COCO category IDs.
                cat_id = classes_to_cats[class_id]
                # Round the box coordinates to reduce the JSON file size.
                xmin = float(round(box[2], 1))
                ymin = float(round(box[3], 1))
                xmax = float(round(box[4], 1))
                ymax = float(round(box[5], 1))
                width = xmax - xmin
                height = ymax - ymin
                bbox = [xmin, ymin, width, height]
                result = {}
                result['image_id'] = batch_image_ids[k]
                result['category_id'] = cat_id
                result['score'] = float(round(box[1], 3))
                result['bbox'] = bbox
                results.append(result)

    with open(out_file, 'w') as f:
        json.dump(results, f)

    print("Prediction results saved in '{}'".format(out_file))
예제 #3
0
def create_generators(args):
    model = args
    path = "./datasets"
    
    train_dataset = DataGenerator(load_images_into_memory=False, hdf5_dataset_path=None)
    val_dataset = DataGenerator(load_images_into_memory=False, hdf5_dataset_path=None)

    VOC_2007_images_dir      = path + '/VOCdevkit/VOC2007/JPEGImages/'
    VOC_2012_images_dir      = path + '/VOCdevkit/VOC2012/JPEGImages/'

    VOC_2007_annotations_dir      = path + '/VOCdevkit/VOC2007/Annotations/'
    VOC_2012_annotations_dir      = path + '/VOCdevkit/VOC2012/Annotations/'

    VOC_2007_trainval_image_set_filename = path + '/VOCdevkit/VOC2007/ImageSets/Main/trainval.txt'
    VOC_2012_trainval_image_set_filename = path + '/VOCdevkit/VOC2012/ImageSets/Main/trainval.txt'
    VOC_2007_test_image_set_filename     = path + '/VOCdevkit/VOC2007/ImageSets/Main/test.txt'

    classes = ['background',
            'aeroplane', 'bicycle', 'bird', 'boat',
            'bottle', 'bus', 'car', 'cat',
            'chair', 'cow', 'diningtable', 'dog',
            'horse', 'motorbike', 'person', 'pottedplant',
            'sheep', 'sofa', 'train', 'tvmonitor']

    train_dataset.parse_xml(images_dirs=[VOC_2007_images_dir,
                                        VOC_2012_images_dir],
                            image_set_filenames=[VOC_2007_trainval_image_set_filename,
                                                VOC_2012_trainval_image_set_filename],
                            annotations_dirs=[VOC_2007_annotations_dir,
                                            VOC_2012_annotations_dir],
                            classes=classes,
                            include_classes='all',
                            exclude_truncated=False,
                            exclude_difficult=False,
                            ret=False)

    val_dataset.parse_xml(images_dirs=[VOC_2007_images_dir],
                        image_set_filenames=[VOC_2007_test_image_set_filename],
                        annotations_dirs=[VOC_2007_annotations_dir],
                        classes=classes,
                        include_classes='all',
                        exclude_truncated=False,
                        exclude_difficult=True,
                        ret=False)

    batch_size = 32

    ssd_data_augmentation = SSDDataAugmentation(img_height=img_height,
                                                img_width=img_width,
                                                background=mean_color)

    convert_to_3_channels = ConvertTo3Channels()
    resize = Resize(height=img_height, width=img_width)

    predictor_sizes = [model.get_layer('conv4_3_norm_mbox_conf').output_shape[1:3],
                    model.get_layer('fc7_mbox_conf').output_shape[1:3],
                    model.get_layer('conv6_2_mbox_conf').output_shape[1:3],
                    model.get_layer('conv7_2_mbox_conf').output_shape[1:3],
                    model.get_layer('conv8_2_mbox_conf').output_shape[1:3],
                    model.get_layer('conv9_2_mbox_conf').output_shape[1:3]]

    ssd_input_encoder = SSDInputEncoder(img_height=img_height,
                                        img_width=img_width,
                                        n_classes=n_classes,
                                        predictor_sizes=predictor_sizes,
                                        scales=scales,
                                        aspect_ratios_per_layer=aspect_ratios,
                                        two_boxes_for_ar1=two_boxes_for_ar1,
                                        steps=steps,
                                        offsets=offsets,
                                        clip_boxes=clip_boxes,
                                        variances=variances,
                                        matching_type='multi',
                                        pos_iou_threshold=0.5,
                                        neg_iou_limit=0.5,
                                        normalize_coords=normalize_coords)

    train_generator = train_dataset.generate(batch_size=batch_size,
                                            shuffle=True,
                                            transformations=[ssd_data_augmentation],
                                            label_encoder=ssd_input_encoder,
                                            returns={'processed_images',
                                                    'encoded_labels'},
                                            keep_images_without_gt=False)

    val_generator = val_dataset.generate(batch_size=batch_size,
                                        shuffle=False,
                                        transformations=[convert_to_3_channels,
                                                        resize],
                                        label_encoder=ssd_input_encoder,
                                        returns={'processed_images',
                                                'encoded_labels'},
                                        keep_images_without_gt=False)

    val_dataset_size   = val_dataset.get_dataset_size()

    
    model_checkpoint = ModelCheckpoint(filepath='ssd300_pascal_07+12_epoch-{epoch:02d}_loss-{loss:.4f}_val_loss-{val_loss:.4f}.h5',
                                    monitor='val_loss',
                                    verbose=1,
                                    save_best_only=True,
                                    save_weights_only=False,
                                    mode='auto',
                                    period=1)

    csv_logger = CSVLogger(filename='ssd300_pascal_07+12_training_log.csv',
                        separator=',',
                        append=True)

    learning_rate_scheduler = LearningRateScheduler(schedule=lr_schedule,
                                                    verbose=1)

    terminate_on_nan = TerminateOnNaN()

    callbacks = [model_checkpoint,
                csv_logger,
                learning_rate_scheduler,
                terminate_on_nan]

    val_steps = ceil(val_dataset_size/batch_size)

    return train_generator, callbacks, val_generator, val_steps
    def __init__(self,
                 resize_height,
                 resize_width,
                 random_brightness=(-48, 48, 0.5),
                 random_contrast=(0.5, 1.8, 0.5),
                 random_saturation=(0.5, 1.8, 0.5),
                 random_hue=(18, 0.5),
                 random_flip=0.5,
                 random_rotate=([90, 180, 270], 0.5),
                 min_scale=0.3,
                 max_scale=2.0,
                 min_aspect_ratio=0.8,
                 max_aspect_ratio=1.25,
                 n_trials_max=3,
                 clip_boxes=True,
                 overlap_criterion='area',
                 bounds_box_filter=(0.3, 1.0),
                 bounds_validator=(0.5, 1.0),
                 n_boxes_min=1,
                 background=(0, 0, 0),
                 labels_format={
                     'class_id': 0,
                     'xmin': 1,
                     'ymin': 2,
                     'xmax': 3,
                     'ymax': 4
                 }):

        self.n_trials_max = n_trials_max
        self.clip_boxes = clip_boxes
        self.overlap_criterion = overlap_criterion
        self.bounds_box_filter = bounds_box_filter
        self.bounds_validator = bounds_validator
        self.n_boxes_min = n_boxes_min
        self.background = background
        self.labels_format = labels_format

        # Determines which boxes are kept in an image after the transformations have been applied.
        self.box_filter_patch = BoxFilter(
            check_overlap=True,
            check_min_area=False,
            check_degenerate=False,
            overlap_criterion=self.overlap_criterion,
            overlap_bounds=self.bounds_box_filter,
            labels_format=self.labels_format)

        self.box_filter_resize = BoxFilter(check_overlap=False,
                                           check_min_area=True,
                                           check_degenerate=True,
                                           min_area=16,
                                           labels_format=self.labels_format)

        # Determines whether the result of the transformations is a valid training image.
        self.image_validator = ImageValidator(
            overlap_criterion=self.overlap_criterion,
            bounds=self.bounds_validator,
            n_boxes_min=self.n_boxes_min,
            labels_format=self.labels_format)

        # Utility transformations
        self.convert_to_3_channels = ConvertTo3Channels(
        )  # Make sure all images end up having 3 channels.
        self.convert_RGB_to_HSV = ConvertColor(current='RGB', to='HSV')
        self.convert_HSV_to_RGB = ConvertColor(current='HSV', to='RGB')
        self.convert_to_float32 = ConvertDataType(to='float32')
        self.convert_to_uint8 = ConvertDataType(to='uint8')
        self.resize = Resize(height=resize_height,
                             width=resize_width,
                             box_filter=self.box_filter_resize,
                             labels_format=self.labels_format)

        # Photometric transformations
        self.random_brightness = RandomBrightness(lower=random_brightness[0],
                                                  upper=random_brightness[1],
                                                  prob=random_brightness[2])
        self.random_contrast = RandomContrast(lower=random_contrast[0],
                                              upper=random_contrast[1],
                                              prob=random_contrast[2])
        self.random_saturation = RandomSaturation(lower=random_saturation[0],
                                                  upper=random_saturation[1],
                                                  prob=random_saturation[2])
        self.random_hue = RandomHue(max_delta=random_hue[0],
                                    prob=random_hue[1])

        # Geometric transformations
        self.random_horizontal_flip = RandomFlip(
            dim='horizontal',
            prob=random_flip,
            labels_format=self.labels_format)
        self.random_vertical_flip = RandomFlip(
            dim='vertical', prob=random_flip, labels_format=self.labels_format)
        self.random_rotate = RandomRotate(angles=random_rotate[0],
                                          prob=random_rotate[1],
                                          labels_format=self.labels_format)
        self.patch_coord_generator = PatchCoordinateGenerator(
            must_match='w_ar',
            min_scale=min_scale,
            max_scale=max_scale,
            scale_uniformly=False,
            min_aspect_ratio=min_aspect_ratio,
            max_aspect_ratio=max_aspect_ratio)
        self.random_patch = RandomPatch(
            patch_coord_generator=self.patch_coord_generator,
            box_filter=self.box_filter_patch,
            image_validator=self.image_validator,
            n_trials_max=self.n_trials_max,
            clip_boxes=self.clip_boxes,
            prob=1.0,
            can_fail=False,
            labels_format=self.labels_format)

        # Define the processing chain.
        self.transformations = [
            self.convert_to_3_channels, self.convert_to_float32,
            self.random_brightness, self.random_contrast,
            self.convert_to_uint8, self.convert_RGB_to_HSV,
            self.convert_to_float32, self.random_saturation, self.random_hue,
            self.convert_to_uint8, self.convert_HSV_to_RGB,
            self.random_horizontal_flip, self.random_vertical_flip,
            self.random_rotate, self.random_patch, self.resize
        ]
    def __init__(self,
                 random_brightness=(-48, 48, 0.5),
                 random_contrast=(0.5, 1.8, 0.5),
                 random_saturation=(0.5, 1.8, 0.5),
                 random_hue=(18, 0.5),
                 random_flip=0.5,
                 random_translate=((0.03, 0.5), (0.03, 0.5), 0.5),
                 random_scale=(0.5, 2.0, 0.5),
                 n_trials_max=3,
                 clip_boxes=True,
                 overlap_criterion='area',
                 bounds_box_filter=(0.3, 1.0),
                 bounds_validator=(0.5, 1.0),
                 n_boxes_min=1,
                 background=(0, 0, 0),
                 labels_format={
                     'class_id': 0,
                     'xmin': 1,
                     'ymin': 2,
                     'xmax': 3,
                     'ymax': 4
                 }):

        if (random_scale[0] >= 1) or (random_scale[1] <= 1):
            raise ValueError(
                "This sequence of transformations only makes sense if the minimum scaling factor is <1 and the maximum scaling factor is >1."
            )

        self.n_trials_max = n_trials_max
        self.clip_boxes = clip_boxes
        self.overlap_criterion = overlap_criterion
        self.bounds_box_filter = bounds_box_filter
        self.bounds_validator = bounds_validator
        self.n_boxes_min = n_boxes_min
        self.background = background
        self.labels_format = labels_format

        # Determines which boxes are kept in an image after the transformations have been applied.
        self.box_filter = BoxFilter(check_overlap=True,
                                    check_min_area=True,
                                    check_degenerate=True,
                                    overlap_criterion=self.overlap_criterion,
                                    overlap_bounds=self.bounds_box_filter,
                                    min_area=16,
                                    labels_format=self.labels_format)

        # Determines whether the result of the transformations is a valid training image.
        self.image_validator = ImageValidator(
            overlap_criterion=self.overlap_criterion,
            bounds=self.bounds_validator,
            n_boxes_min=self.n_boxes_min,
            labels_format=self.labels_format)

        # Utility distortions
        self.convert_RGB_to_HSV = ConvertColor(current='RGB', to='HSV')
        self.convert_HSV_to_RGB = ConvertColor(current='HSV', to='RGB')
        self.convert_to_float32 = ConvertDataType(to='float32')
        self.convert_to_uint8 = ConvertDataType(to='uint8')
        self.convert_to_3_channels = ConvertTo3Channels(
        )  # Make sure all images end up having 3 channels.

        # Photometric transformations
        self.random_brightness = RandomBrightness(lower=random_brightness[0],
                                                  upper=random_brightness[1],
                                                  prob=random_brightness[2])
        self.random_contrast = RandomContrast(lower=random_contrast[0],
                                              upper=random_contrast[1],
                                              prob=random_contrast[2])
        self.random_saturation = RandomSaturation(lower=random_saturation[0],
                                                  upper=random_saturation[1],
                                                  prob=random_saturation[2])
        self.random_hue = RandomHue(max_delta=random_hue[0],
                                    prob=random_hue[1])

        # Geometric transformations
        self.random_flip = RandomFlip(dim='horizontal',
                                      prob=random_flip,
                                      labels_format=self.labels_format)
        self.random_translate = RandomTranslate(
            dy_minmax=random_translate[0],
            dx_minmax=random_translate[1],
            prob=random_translate[2],
            clip_boxes=self.clip_boxes,
            box_filter=self.box_filter,
            image_validator=self.image_validator,
            n_trials_max=self.n_trials_max,
            background=self.background,
            labels_format=self.labels_format)
        self.random_zoom_in = RandomScale(min_factor=1.0,
                                          max_factor=random_scale[1],
                                          prob=random_scale[2],
                                          clip_boxes=self.clip_boxes,
                                          box_filter=self.box_filter,
                                          image_validator=self.image_validator,
                                          n_trials_max=self.n_trials_max,
                                          background=self.background,
                                          labels_format=self.labels_format)
        self.random_zoom_out = RandomScale(
            min_factor=random_scale[0],
            max_factor=1.0,
            prob=random_scale[2],
            clip_boxes=self.clip_boxes,
            box_filter=self.box_filter,
            image_validator=self.image_validator,
            n_trials_max=self.n_trials_max,
            background=self.background,
            labels_format=self.labels_format)

        # If we zoom in, do translation before scaling.
        self.sequence1 = [
            self.convert_to_3_channels, self.convert_to_float32,
            self.random_brightness, self.random_contrast,
            self.convert_to_uint8, self.convert_RGB_to_HSV,
            self.convert_to_float32, self.random_saturation, self.random_hue,
            self.convert_to_uint8, self.convert_HSV_to_RGB,
            self.random_translate, self.random_zoom_in, self.random_flip
        ]

        # If we zoom out, do scaling before translation.
        self.sequence2 = [
            self.convert_to_3_channels, self.convert_to_float32,
            self.random_brightness, self.convert_to_uint8,
            self.convert_RGB_to_HSV, self.convert_to_float32,
            self.random_saturation, self.random_hue, self.convert_to_uint8,
            self.convert_HSV_to_RGB, self.convert_to_float32,
            self.random_contrast, self.convert_to_uint8, self.random_zoom_out,
            self.random_translate, self.random_flip
        ]