def test_prior_boxes(self, original_prior_boxes_file='test_resources/prior_boxes_ssd300.pkl'): prior_box_creator = PriorBoxCreator(self.model) self.prior_boxes = prior_box_creator.create_boxes() original_prior_boxes = pickle.load( open(original_prior_boxes_file, 'rb')) prior_box_difference = self.prior_boxes - original_prior_boxes[:, :4] print('shape {}, max value {}, min value {}'.format( prior_box_difference.shape, prior_box_difference.max(), prior_box_difference.min()))
def __init__(self, model, test_keys, prior_boxes=None, dataset_name='VOC2007'): self.model = model box_creator = PriorBoxCreator(self.model) self.prior_boxes = box_creator.create_boxes() self.class_names = get_class_names(dataset_name) self.num_classes = len(self.class_names) self.colors = plt.cm.hsv(np.linspace(0, 1, self.num_classes)).tolist() self.colors = np.asarray(self.colors) * 255 self.arg_to_class = dict( zip(list(range(self.num_classes)), self.class_names)) self.overlap_threshold = .5 self.background_id = 0 self.test_keys = test_keys self.root_prefix = '../datasets/VOCdevkit/VOC2007/' self.ground_data_prefix = self.root_prefix + 'Annotations/' self.image_prefix = self.root_prefix + 'JPEGImages/' self.image_size = (300, 300) self.ground_truth_manager = DataLoader(dataset_name) self.ground_truth_data = self.ground_truth_manager.get_data()
from image_generator import ImageGenerator from models import SSD300 from utils.prior_box_creator import PriorBoxCreator from utils.prior_box_assigner import PriorBoxAssigner from utils.box_transformer import BoxTransformer from utils.XML_parser import XMLParser from utils.utils import split_data from utils.utils import read_image, resize_image import numpy as np import matplotlib.pyplot as plt image_shape = (300, 300, 3) model = SSD300(image_shape) box_creator = PriorBoxCreator(model) prior_boxes = box_creator.create_boxes() layer_scale, box_arg = 0, 780 box_coordinates = prior_boxes[layer_scale][box_arg, :, :] image_path = '../images/' image_key = '007040.jpg' box_creator.draw_boxes(image_path + image_key, box_coordinates) data_path = '../datasets/VOCdevkit/VOC2007/' ground_truths = XMLParser(data_path + 'Annotations/').get_data() prior_box_manager = PriorBoxAssigner(prior_boxes, ground_truths) assigned_boxes = prior_box_manager.assign_boxes() prior_box_manager.draw_assigned_boxes(image_path, image_shape[0:2], image_key) batch_size = 7 train_keys, validation_keys = split_data(assigned_boxes, training_ratio=.8) assigned_image_generator = ImageGenerator(assigned_boxes, batch_size,
image_shape = (300, 300, 3) model = mini_SSD300(image_shape, num_classes=num_classes) plot(model, to_file='mini_SSD300.png') def class_accuracy(y_true, y_pred): y_pred_classification = y_pred[:, :, 4:(4 + num_classes)] y_true_classification = y_true[:, :, 4:(4 + num_classes)] return categorical_accuracy(y_true_classification, y_pred_classification) 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,