Ejemplo n.º 1
0
limit_boxes = False  # Whether or not you want to limit the anchor boxes to lie entirely within the image boundaries
variances = [
    1.0, 1.0, 1.0, 1.0
]  # The list of variances by which the encoded target coordinates are scaled
coords = 'centroids'  # Whether the box coordinates to be used should be in the 'centroids' or 'minmax' format, see documentation
normalize_coords = False  # Whether or not the model is supposed to use relative coordinates that are within [0,1]

# II. Build or load the model
# II.1 Create a new model
K.clear_session()  # Clear previous models from memory.
model = build_model(image_size=(img_height, img_width, img_channels),
                    n_classes=n_classes,
                    min_scale=min_scale,
                    max_scale=max_scale,
                    scales=scales,
                    aspect_ratios_global=aspect_ratios,
                    aspect_ratios_per_layer=None,
                    two_boxes_for_ar1=two_boxes_for_ar1,
                    limit_boxes=limit_boxes,
                    variances=variances,
                    coords=coords,
                    normalize_coords=normalize_coords)
adam = Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=5e-04)
ssd_loss = SSDLoss(neg_pos_ratio=3, n_neg_min=0, alpha=1.0)
model.compile(optimizer=adam, loss=ssd_loss.compute_loss)

# III. Set up the data generators for the training
train_dataset = BatchGenerator(
    box_output_format=['class_id', 'xmin', 'xmax', 'ymin', 'ymax'])
val_dataset = BatchGenerator(
    box_output_format=['class_id', 'xmin', 'xmax', 'ymin', 'ymax'])
normalize_coords = True # Whether or not the model is supposed to use relative coordinates that are within [0,1]

#=========================
# 1: Build the Keras model 
#=========================

K.clear_session() # Clear previous models from memory.

model = build_model(image_size=(img_height, img_width, img_channels),
                    n_classes=n_classes,
                    l2_regularization=0.0005,
                    scales=scales,
                    aspect_ratios_global=aspect_ratios,
                    aspect_ratios_per_layer=None,
                    two_boxes_for_ar1=two_boxes_for_ar1,
                    steps=steps,
                    offsets=offsets,
                    limit_boxes=limit_boxes,
                    variances=variances,
                    coords=coords,
                    normalize_coords=normalize_coords,
                    subtract_mean=subtract_mean,
                    divide_by_stddev=divide_by_stddev,
                    swap_channels=False)

#===============================
# 2: Optional: Load some weights
#===============================
#model.load_weights('h5 files/ssd7_udacity_weights.h5')

#==================================================================================
# 3: Instantiate an Adam optimizer and the SSD loss function and compile the model