Skip to content

Commit

Permalink
Merge pull request #230 from Manojkumarmuru/efficientdet
Browse files Browse the repository at this point in the history
Efficientdet
  • Loading branch information
oarriaga authored Oct 27, 2022
2 parents feaded9 + 8607225 commit 149d763
Showing 1 changed file with 89 additions and 95 deletions.
184 changes: 89 additions & 95 deletions examples/efficientdet/efficientdet_test.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,24 @@
import numpy as np
# from examples.efficientdet.efficientdet_blocks import FeatureNode
import pytest
import tensorflow as tf
from tensorflow.keras import backend as K
from keras.utils.layer_utils import count_params
from tensorflow.keras.layers import Input
from tensorflow.keras.models import model_from_json

from anchors import build_prior_boxes
from efficientdet import (EFFICIENTDETD0, EFFICIENTDETD1, EFFICIENTDETD2,
EFFICIENTDETD3, EFFICIENTDETD4, EFFICIENTDETD5,
EFFICIENTDETD6, EFFICIENTDETD7)
from efficientdet_blocks import FuseFeature
from efficientnet_model import EfficientNet, conv_normal_initializer
from efficientnet_model import EfficientNet


@pytest.fixture
def models_base_path():
return ("/home/manummk95/Desktop/efficientdet_working/"
"required/test_files/efficientdet_architectures/")
def model_input_name():
return 'image'


@pytest.fixture
def model_input_output_base_path():
return ("/home/manummk95/Desktop/efficientdet_working/"
"required/test_files/test_model_outputs/")


@pytest.fixture
def model_anchor_boxes_path():
return ("/home/manummk95/Desktop/efficientdet_working/"
"required/test_files/efficientdet_anchors/")
def model_output_name():
return 'boxes'


def get_test_images(image_size, batch_size=1):
Expand Down Expand Up @@ -121,85 +111,89 @@ def test_efficientnet_features(input_shape, backbone, feature_shape,
del branch_tensors


@pytest.mark.parametrize('implemented_model, model_id',
[
(EFFICIENTDETD0, 0),
(EFFICIENTDETD1, 1),
(EFFICIENTDETD2, 2),
(EFFICIENTDETD3, 3),
(EFFICIENTDETD4, 4),
(EFFICIENTDETD5, 5),
(EFFICIENTDETD6, 6),
(EFFICIENTDETD7, 7),
])
def test_efficientdet_architecture(models_base_path,
implemented_model,
model_id):
custom_objects = {"conv_normal_initializer": conv_normal_initializer,
"FuseFeature": FuseFeature}
K.clear_session()
reference_model_path = (models_base_path + 'EFFICIENTDETD' +
str(model_id) + '.json')
reference_model_file = open(reference_model_path, 'r')
loaded_model_json = reference_model_file.read()
reference_model_file.close()
reference_model = model_from_json(loaded_model_json,
custom_objects=custom_objects)
K.clear_session()
assert (implemented_model().get_config() ==
reference_model.get_config()), ('EFFICIENTDETD' + str(model_id)
+ " architecture mismatch")
del implemented_model, reference_model


@pytest.mark.parametrize('model, model_id',
@pytest.mark.parametrize(('model, model_name, trainable_parameters,'
'non_trainable_parameters, input_shape,'
'output_shape'),
[
(EFFICIENTDETD0, 0),
(EFFICIENTDETD1, 1),
(EFFICIENTDETD2, 2),
(EFFICIENTDETD3, 3),
(EFFICIENTDETD4, 4),
(EFFICIENTDETD5, 5),
(EFFICIENTDETD6, 6),
(EFFICIENTDETD7, 7),
(EFFICIENTDETD0, 'efficientdet-d0', 3880067,
47136, (512, 512, 3), (49104, 94)),
(EFFICIENTDETD1, 'efficientdet-d1', 6625898,
71456, (640, 640, 3), (76725, 94)),
(EFFICIENTDETD2, 'efficientdet-d2', 8097039,
81776, (768, 768, 3), (110484, 94)),
(EFFICIENTDETD3, 'efficientdet-d3', 12032296,
114304, (896, 896, 3), (150381, 94)),
(EFFICIENTDETD4, 'efficientdet-d4', 20723675,
167312, (1024, 1024, 3), (196416, 94)),
(EFFICIENTDETD5, 'efficientdet-d5', 33653315,
227392, (1280, 1280, 3), (306900, 94)),
(EFFICIENTDETD6, 'efficientdet-d6', 51871934,
311984, (1280, 1280, 3), (306900, 94)),
(EFFICIENTDETD7, 'efficientdet-d7', 51871934,
311984, (1536, 1536, 3), (441936, 94)),
])
def test_efficientdet_anchor_boxes(model_anchor_boxes_path, model, model_id):
reference_anchor_file = (model_anchor_boxes_path + 'EFFICIENTDETD' +
str(model_id) + '_anchors.npy')
with open(reference_anchor_file, 'rb') as f:
reference_anchor = np.load(f)
model_anchor = model().prior_boxes
assert np.array_equal(model_anchor, reference_anchor)
del model


@pytest.mark.skip(reason="Training of model needs to carried out from D0-D7")
@pytest.mark.parametrize('model, model_idx, preprocessed_inputs',
def test_efficientdet_architecture(model, model_name, model_input_name,
model_output_name, trainable_parameters,
non_trainable_parameters, input_shape,
output_shape):
implemented_model = model()
trainable_count = count_params(
implemented_model.trainable_weights)
non_trainable_count = count_params(
implemented_model.non_trainable_weights)
assert implemented_model.name == model_name, "Model name incorrect"
assert implemented_model.input_names[0] == model_input_name, (
"Input name incorrect")
assert implemented_model.output_names[0] == model_output_name, (
"Output name incorrect")
assert trainable_count == trainable_parameters, (
"Incorrect trainable parameters count")
assert non_trainable_count == non_trainable_parameters, (
"Incorrect non-trainable parameters count")
assert implemented_model.input_shape[1:] == input_shape, (
"Incorrect input shape")
assert implemented_model.output_shape[1:] == output_shape, (
"Incorrect output shape")
del implemented_model


@pytest.mark.parametrize(('min_level, max_level, num_scales, aspect_ratios,'
'anchor_scale, image_size, anchor_count'),
[
(EFFICIENTDETD0, 0, (1, 2, 3, 4, 5)),
(EFFICIENTDETD1, 1, (1, 2, 3, 4, 5)),
(EFFICIENTDETD2, 2, (1, 2, 3, 4, 5)),
(EFFICIENTDETD3, 3, (1, 2, 3, 4, 5)),
(EFFICIENTDETD4, 4, (1, 2, 3, 4, 5)),
(EFFICIENTDETD5, 5, (1, 2, 3, 4, 5)),
(EFFICIENTDETD6, 6, (1, 2, 3, 4, 5)),
(EFFICIENTDETD7, 7, (1, 2, 3, 4, 5))
(3, 7, 3, [1.0, 2.0, 0.5], 4.0, (512, 512),
49104),
(3, 7, 3, [1.0, 2.0, 0.5], 4.0, (640, 640),
76725),
(3, 7, 3, [1.0, 2.0, 0.5], 4.0, (768, 768),
110484),
(3, 7, 3, [1.0, 2.0, 0.5], 4.0, (896, 896),
150381),
(3, 7, 3, [1.0, 2.0, 0.5], 4.0, (1024, 1024),
196416),
(3, 7, 3, [1.0, 2.0, 0.5], 4.0, (1280, 1280),
306900),
(3, 7, 3, [1.0, 2.0, 0.5], 4.0, (1536, 1536),
441936),
])
def test_efficientdet_result(model_input_output_base_path, model,
model_idx, preprocessed_inputs):
for preprocessed_input_idx in preprocessed_inputs:
preprocessed_input_file = model_input_output_base_path + \
'EFFICIENTDETD' + str(model_idx) + '/inputs/test_image_' + \
str(preprocessed_input_idx) + '.npy'
with open(preprocessed_input_file, 'rb') as f:
preprocessed_input = np.load(f)

target_model_output_file = model_input_output_base_path + \
'EFFICIENTDETD' + str(model_idx) + '/outputs/model_output_' + \
str(preprocessed_input_idx) + '.npy'
with open(target_model_output_file, 'rb') as f:
target_model_output = np.load(target_model_output_file)

assert np.all(model()(preprocessed_input).numpy() ==
target_model_output), 'Model result not as expected'
del model
def test_build_prior_boxes(min_level, max_level, num_scales, aspect_ratios,
anchor_scale, image_size, anchor_count):
prior_boxes = build_prior_boxes(
min_level, max_level, num_scales, aspect_ratios, anchor_scale,
image_size)
anchor_x, anchor_y = prior_boxes[:, 0], prior_boxes[:, 1]
anchor_W, anchor_H = prior_boxes[:, 2], prior_boxes[:, 3]
measured_aspect_ratios = set(np.unique(np.round((anchor_W/anchor_H), 2)))
assert np.logical_and(anchor_x >= 0, anchor_x <= 1).all(), (
"Invalid x-coordinates of anchor centre")
assert np.logical_and(anchor_y >= 0, anchor_y <= 1).all(), (
"Invalid y-coordinates of anchor centre")
assert (anchor_W > 0).all(), "Invalid/negative anchor width"
assert (anchor_H > 0).all(), "Invalid/negative anchor height"
assert np.round(np.mean(anchor_x), 2) == 0.5, (
"Anchor boxes asymmetrically distributed along X-direction")
assert np.round(np.mean(anchor_y), 2) == 0.5, (
"Anchor boxes asymmetrically distributed along Y-direction")
assert measured_aspect_ratios == set(aspect_ratios), (
"Anchor aspect ratios not as expected")
assert prior_boxes.shape[0] == anchor_count, (
"Incorrect number of anchor boxes")

0 comments on commit 149d763

Please sign in to comment.