Skip to content
Snippets Groups Projects
Commit 56bccf0a authored by Cyril Moineau's avatar Cyril Moineau
Browse files

Add test for every models.

parent 3008cf52
No related branches found
No related tags found
2 merge requests!50.1.0,!4Fix module
......@@ -4,10 +4,14 @@ import aidge_core
import aidge_learning
import pytest
from utils import Test_Networks
from utils import ModelTester
from models import (
anomaly_model,
kws_model,
lenet_model,
perceptron_model
mobilenet_model,
perceptron_model,
resnet_model,
)
torch.backends.cudnn.enabled = True
torch.backends.cudnn.benchmark = True
......@@ -19,36 +23,106 @@ RELATIVE_PRECISION = 0.001
ABSOLUTE_PRECISION = 0.0001
NB_RUN = 10
# Note: Unreadable logs without this
aidge_core.Log.set_console_level(aidge_core.Level.Fatal)
# === Test case list ===
test_cases = [
{
"name": "Anomaly_eval",
"model": anomaly_model.get_model(32* 32, BIAS=True),
"input_size": (BATCH_SIZE, 32* 32),
"output_size": (BATCH_SIZE, 128),
"test_backward": False,
}
,
{
"name": "Anomaly_train",
"model": anomaly_model.get_model(32 * 32, BIAS=True),
"input_size": (BATCH_SIZE, 32 * 32),
"output_size": (BATCH_SIZE, 128),
"test_backward": True,
"expected_failure": "Epoch 0 failed: Anomaly_train: Outputs differ"
},
{
"name": "KWS_eval",
"model": kws_model.KWS_Net(10),
"input_size": (BATCH_SIZE, 1, 49, 10),
"output_size": (BATCH_SIZE, 10),
"test_backward": False,
}
,
{
"name": "KWS_train",
"model": kws_model.KWS_Net(10),
"input_size": (BATCH_SIZE, 1, 49, 10),
"output_size": (BATCH_SIZE, 10),
"test_backward": True,
"expected_failure": "Epoch 0 failed: KWS_train: Outputs differ"
},
{
"name": "LeNet_eval",
"model": lenet_model.TorchLeNet,
"model": lenet_model.TorchLeNet(),
"input_size": (BATCH_SIZE, 1, 32, 32),
"output_size": (BATCH_SIZE, 10),
"eval_mode": True,
"test_backward": False,
"expect_failure": False
},
{
"name": "LeNet_train",
"model": lenet_model.TorchLeNet,
"model": lenet_model.TorchLeNet(),
"input_size": (BATCH_SIZE, 1, 32, 32),
"output_size": (BATCH_SIZE, 10),
"eval_mode": True,
"test_backward": True,
"expected_failure": "Batckward of BatchNorm is not implemented"
"expected_failure": "Backward of BatchNorm is not implemented"
},
{
"name": "EasyGraph_train",
"model": perceptron_model.Perceptron,
"name": "MobileNet_eval",
"model": mobilenet_model.mobilenet_v1(),
"input_size": (BATCH_SIZE, 3, 96, 96),
"output_size": (BATCH_SIZE, 10),
"test_backward": False,
"expected_failure": "forward() not implemented yet for operator of type AveragePool"
},
{
"name": "MobileNet_train",
"model": mobilenet_model.mobilenet_v1(),
"input_size": (BATCH_SIZE, 1, 96, 96),
"output_size": (BATCH_SIZE, 10),
"test_backward": True,
"expected_failure": "forward() not implemented yet for operator of type AveragePool"
},
{
"name": "Perceptron_eval",
"model": perceptron_model.Perceptron(),
"input_size": (BATCH_SIZE, 3, 1, 1),
"output_size": (BATCH_SIZE, 4),
"test_backward": False,
},
{
"name": "Perceptron_train",
"model": perceptron_model.Perceptron(),
"input_size": (BATCH_SIZE, 3, 1, 1),
"output_size": (BATCH_SIZE, 4),
"eval_mode": False,
"test_backward": True,
"expect_failure": False
}
},
{
"name": "ResNet_eval",
"model": resnet_model.ResNetV1(input_shape=(3, 32, 32), num_classes=10),
"input_size": (BATCH_SIZE, 3, 32, 32),
"output_size": (BATCH_SIZE, 10),
"test_backward": False,
"expected_failure": "forward() not implemented yet for operator of type AveragePool"
},
{
"name": "ResNet_train",
"model": resnet_model.ResNetV1(input_shape=(3, 32, 32), num_classes=10),
"input_size": (BATCH_SIZE, 3, 32, 32),
"output_size": (BATCH_SIZE, 10),
"test_backward": True,
"expected_failure": "forward() not implemented yet for operator of type AveragePool"
},
]
......@@ -62,7 +136,7 @@ test_cases = [
], ids=[case["name"] for case in test_cases])
def model_test_case(request):
cfg = request.param
torch_model = cfg["model"]()
torch_model = cfg["model"]
input_size = cfg["input_size"]
# Wrap with Aidge
......@@ -82,19 +156,17 @@ def model_test_case(request):
"aidge_model": aidge_model,
"input_size": input_size,
"output_size": cfg["output_size"],
"eval_mode": cfg["eval_mode"],
"test_backward": cfg["test_backward"]
}
# === Main test ===
def test_model_interop(model_test_case):
tester = Test_Networks(
tester = ModelTester(
model1 = model_test_case["torch_model"],
model2 = model_test_case["aidge_model"],
name = model_test_case["name"],
test_backward = model_test_case["test_backward"],
eval_mode = model_test_case["eval_mode"],
epochs = NB_RUN,
relative_precision = RELATIVE_PRECISION,
absolute_precision = ABSOLUTE_PRECISION,
......
import torch
import numpy as np
class Test_Networks:
def __init__(self, model1, model2, name="", test_backward=True, eval_mode=False,
epochs=10, relative_precision=0.001, absolute_precision=0.0001,
learning_rate=0.01, cuda=False):
class ModelTester:
def __init__(self,
model1,
model2,
name="",
test_backward=True,
epochs=10,
relative_precision=0.001,
absolute_precision=0.0001,
learning_rate=0.01,
cuda=False):
self.relative_precision = relative_precision
self.absolute_precision = absolute_precision
self.epochs = epochs
......@@ -15,12 +23,12 @@ class Test_Networks:
if self.cuda:
self.model1 = self.model1.cuda()
self.model2 = self.model2.cuda()
if eval_mode:
self.model1.eval()
self.model2.eval()
else:
if test_backward:
self.model1.train()
self.model2.train()
else:
self.model1.eval()
self.model2.eval()
self.name = name
if self.test_backward:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment