def test_model(self):
        inputs = []
        images = []
        file_paths = list_files_in_directory('test_resources/*.jpg')
        for file_path in file_paths:
            image_array = load_image(file_path, False, target_size=(300, 300))
            images.append(imread(file_path))
            inputs.append(image_array)
        inputs = np.asarray(inputs, dtype='float32')
        inputs = preprocess_input(inputs)
        predictions = self.model.predict(inputs, batch_size=1, verbose=1)
        prior_box_manager = PriorBoxManager(self.prior_boxes,
                            box_scale_factors=[.1, .1, .2, .2])
        results = prior_box_manager.detection_out(predictions)
        for i, img in enumerate(images):
            # Parse the outputs.
            det_label = results[i][:, 0]
            det_conf = results[i][:, 1]
            det_xmin = results[i][:, 2]
            det_ymin = results[i][:, 3]
            det_xmax = results[i][:, 4]
            det_ymax = results[i][:, 5]

            # Get detections with confidence higher than 0.6.
            top_indices = [i for i, conf in enumerate(det_conf) if conf >= 0.8]

            top_conf = det_conf[top_indices]
            top_label_indices = det_label[top_indices].tolist()
            top_xmin = det_xmin[top_indices]
            top_ymin = det_ymin[top_indices]
            top_xmax = det_xmax[top_indices]
            top_ymax = det_ymax[top_indices]

            colors = plt.cm.hsv(np.linspace(0, 1, 21)).tolist()

            plt.imshow(img / 255.)
            currentAxis = plt.gca()

            for i in range(top_conf.shape[0]):
                xmin = int(round(top_xmin[i] * img.shape[1]))
                ymin = int(round(top_ymin[i] * img.shape[0]))
                xmax = int(round(top_xmax[i] * img.shape[1]))
                ymax = int(round(top_ymax[i] * img.shape[0]))
                score = top_conf[i]
                label = int(top_label_indices[i])
                #label_name = voc_classes[label - 1]
                #label_name = self.class_names[label - 1]
                label_name = self.class_names[label]
                display_txt = '{:0.2f}, {}'.format(score, label_name)
                coords = (xmin, ymin), xmax-xmin+1, ymax-ymin+1
                color = colors[label]
                currentAxis.add_patch(plt.Rectangle(*coords, fill=False, edgecolor=color, linewidth=2))
                currentAxis.text(xmin, ymin, display_txt, bbox={'facecolor':color, 'alpha':0.5})
            plt.show()
multibox_loss = MultiboxLoss(num_classes, neg_pos_ratio=2.0).compute_loss
model.compile(optimizer=Adam(lr=3e-4),
              loss=multibox_loss,
              metrics=[class_accuracy])
box_creator = PriorBoxCreator(model)
prior_boxes = box_creator.create_boxes()
ground_truth_manager = XMLParser(ground_data_prefix,
                                 background_id=None,
                                 class_names=classes)
ground_truth_data = ground_truth_manager.get_data()
print('Number of ground truth samples:', len(ground_truth_data.keys()))
train_keys, validation_keys = split_data(ground_truth_data, training_ratio=.8)

prior_box_manager = PriorBoxManager(prior_boxes,
                                    box_scale_factors=[.1, .1, .2, .2],
                                    num_classes=num_classes)

image_generator = ImageGenerator(ground_truth_data,
                                 prior_box_manager,
                                 batch_size,
                                 image_shape[0:2],
                                 train_keys,
                                 validation_keys,
                                 image_prefix,
                                 vertical_flip_probability=0,
                                 horizontal_flip_probability=0.5)

model_names = ('../trained_models/model_checkpoints/' +
               'weights.{epoch:02d}-{val_loss:.2f}.hdf5')
model_checkpoint = ModelCheckpoint(model_names,
# parameters
root_prefix = '../datasets/VOCdevkit/VOC2007/'
image_prefix = root_prefix + 'JPEGImages/'
ground_data_prefix = root_prefix + 'Annotations/'
model = my_SSD(num_classes=21)
image_shape = model.input_shape[1:]
background_id = 0

ground_truth_manager = XMLParser(ground_data_prefix, background_id)
ground_truth_data = ground_truth_manager.get_data()
VOC2007_decoder = ground_truth_manager.arg_to_class

box_creator = PriorBoxCreator(model)
prior_boxes = box_creator.create_boxes()
vis = BoxVisualizer(image_prefix, image_shape[0:2], VOC2007_decoder)
prior_boxes = flatten_prior_boxes(prior_boxes)

selected_key =  random.choice(list(ground_truth_data.keys()))
selected_data = ground_truth_data[selected_key]
selected_box_coordinates = selected_data[:, 0:4]

prior_box_manager = PriorBoxManager(prior_boxes, background_id,box_scale_factors=[.1,.1,.2,.2])
encoded_boxes = prior_box_manager.assign_boxes(selected_data)
positive_mask = encoded_boxes[:, 4 + background_id] != 1

vis.draw_normalized_box(encoded_boxes[positive_mask], selected_key)
decoded_boxes = prior_box_manager.decode_boxes(encoded_boxes)
vis.draw_normalized_box(decoded_boxes[positive_mask], selected_key)
vis.draw_normalized_box(prior_boxes[positive_mask], selected_key)
Example #4
0
from utils.utils import flatten_prior_boxes
from utils.utils import split_data

# parameters
root_prefix = '../datasets/VOCdevkit/VOC2007/'
image_prefix = root_prefix + 'JPEGImages/'
ground_data_prefix = root_prefix + 'Annotations/'
model = my_SSD(num_classes=21)
image_shape = model.input_shape[1:]
background_id = 0

ground_truth_manager = XMLParser(ground_data_prefix, background_id)
ground_truth_data = ground_truth_manager.get_data()
box_creator = PriorBoxCreator(model)
prior_boxes = box_creator.create_boxes()
prior_box_manager = PriorBoxManager(prior_boxes, background_id)

prior_boxes = flatten_prior_boxes(prior_boxes)

train_keys, validation_keys = split_data(ground_truth_data, training_ratio=.8)

batch_size = 10
image_generator = ImageGenerator(ground_truth_data,
                                 prior_box_manager,
                                 batch_size,
                                 image_shape[0:2],
                                 train_keys,
                                 validation_keys,
                                 image_prefix,
                                 vertical_flip_probability=0,
                                 horizontal_flip_probability=0.5)
    branch_3 = Reshape((-1, 4 + num_classes))(branch_3)
    local_3 = Lambda(lambda x: x[:, :, :4])(branch_3)
    class_3 = Lambda(lambda x: K.softmax(x[:, :, 4:]))(branch_3)

    classification_tensor = merge([class_1, class_2, class_3],
                                  mode='concat',
                                  concat_axis=1)

    localization_tensor = merge([local_1, local_2, local_3],
                                mode='concat',
                                concat_axis=1)
    output_tensor = merge([localization_tensor, classification_tensor],
                          mode='concat',
                          concat_axis=-1)
    model = Model(input_tensor, output_tensor)
    return model


if __name__ == '__main__':
    input_shape = (100, 100, 3)
    model = mini_SSD(input_shape)
    model.compile(optimizer='adam', loss='categorical_crossentropy')
    print(model.summary())
    plot(model, 'mini_SSD.png')
    from utils.prior_box_creator import PriorBoxCreator
    from utils.prior_box_manager import PriorBoxManager
    prior_box_creator = PriorBoxCreator(model)
    prior_boxes = prior_box_creator.create_boxes()
    prior_box_manager = PriorBoxManager(prior_boxes)
    print(prior_box_manager.prior_boxes.shape)
Example #6
0
    if layer.name in freeze:
        layer.trainable = False

multibox_loss = MultiboxLoss(num_classes, neg_pos_ratio=2.0).compute_loss
model.compile(optimizer=Adam(lr=3e-4), loss=multibox_loss, metrics=['acc'])

box_creator = PriorBoxCreator(model)
prior_boxes = box_creator.create_boxes()
#prior_boxes = pickle.load(open('prior_boxes_ssd300.pkl', 'rb'))

ground_truth_manager = XMLParser(ground_data_prefix, background_id=None)
ground_truth_data = ground_truth_manager.get_data()

train_keys, validation_keys = split_data(ground_truth_data, training_ratio=.8)

prior_box_manager = PriorBoxManager(prior_boxes,
                                    box_scale_factors=[.1, .1, .2, .2])

image_generator = ImageGenerator(ground_truth_data,
                                 prior_box_manager,
                                 batch_size,
                                 image_shape[0:2],
                                 train_keys,
                                 validation_keys,
                                 image_prefix,
                                 vertical_flip_probability=0,
                                 horizontal_flip_probability=0.5)

model_names = ('../trained_models/model_checkpoints/' +
               'weights.{epoch:02d}-{val_loss:.2f}.hdf5')
model_checkpoint = ModelCheckpoint(model_names,
                                   monitor='val_loss',
    layer_scale, box_arg = 0, 780
    box_coordinates = prior_boxes[layer_scale][box_arg, :, :]
    #box_visualizer.draw_normalized_box(box_coordinates)

    ground_data_prefix = root_prefix + 'Annotations/'
    ground_truth_data = XMLParser(ground_data_prefix).get_data()
    random_key = random.choice(list(ground_truth_data.keys()))
    selected_data = ground_truth_data[random_key]
    selected_box_coordinates = selected_data[:, 0:4]

    #box_visualizer.draw_normalized_box(selected_box_coordinates, random_key)
    train_keys, validation_keys = split_data(ground_truth_data,
                                             training_ratio=.8)

    prior_box_manager = PriorBoxManager(prior_boxes)
    assigned_encoded_boxes = prior_box_manager.assign_boxes(selected_data)
    positive_mask = assigned_encoded_boxes[:, -8] > 0
    assigned_decoded_boxes = prior_box_manager.decode_boxes(
        assigned_encoded_boxes)
    decoded_positive_boxes = assigned_decoded_boxes[positive_mask, 0:4]
    #box_visualizer.draw_normalized_box(decoded_positive_boxes, random_key)

    batch_size = 10
    image_generator = ImageGenerator(ground_truth_data,
                                     prior_box_manager,
                                     batch_size,
                                     image_shape[0:2],
                                     train_keys,
                                     validation_keys,
                                     image_prefix,
Example #8
0
from models import SSD300
from utils.prior_box_manager import PriorBoxManager
from utils.prior_box_creator import PriorBoxCreator
from utils.XML_parser import XMLParser

image_shape = (300, 300, 3)
overlap_threshold = .5
model = SSD300(image_shape)
box_creator = PriorBoxCreator(model)
prior_boxes = box_creator.create_boxes()

data_path = '../datasets/VOCdevkit/VOC2007/'
ground_truths = XMLParser(data_path + 'Annotations/').get_data()

prior_box_manager = PriorBoxManager(prior_boxes,
                                    overlap_threshold,
                                    background_id=0)

a = prior_box_manager.assign_boxes(ground_truths['000009.jpg'])

encoded_boxes = prior_box_manager.encoded_boxes
best_iou_indices = prior_box_manager.best_iou_indices
num_assigned_boxes = prior_box_manager.num_assigned_boxes
best_iou_mask = prior_box_manager.best_iou_mask