mirror of
https://github.com/lordmathis/CUDANet.git
synced 2025-11-05 17:34:21 +00:00
Implement model test
This commit is contained in:
@@ -8,7 +8,7 @@
|
||||
TEST(Model, TestModelPredict) {
|
||||
int inputSize = 6;
|
||||
int inputChannels = 2;
|
||||
int outputSize = 6;
|
||||
int outputSize = 3;
|
||||
|
||||
int kernelSize = 3;
|
||||
int stride = 1;
|
||||
@@ -26,27 +26,12 @@ TEST(Model, TestModelPredict) {
|
||||
);
|
||||
// weights 6*6*2*2
|
||||
std::vector<float> conv2dWeights = {
|
||||
0.18313f, 0.53363f, 0.39527f, 0.27575f, 0.3433f, 0.41746f, 0.16831f,
|
||||
0.61693f, 0.54599f, 0.99692f, 0.77127f, 0.25146f, 0.4206f, 0.16291f,
|
||||
0.93484f, 0.79765f, 0.74982f, 0.78336f, 0.6386f, 0.87744f, 0.33587f,
|
||||
0.9691f, 0.68437f, 0.65098f, 0.48153f, 0.97546f, 0.8026f, 0.36689f,
|
||||
0.98152f, 0.37351f, 0.68407f, 0.2684f, 0.2855f, 0.76195f, 0.67828f,
|
||||
0.57567f, 0.6752f, 0.83416f, 0.62217f, 0.92441f, 0.96507f, 0.11171f,
|
||||
0.52438f, 0.90435f, 0.46854f, 0.59987f, 0.21747f, 0.82223f, 0.2709f,
|
||||
0.69207f, 0.16794f, 0.26679f, 0.49572f, 0.42392f, 0.49808f, 0.33058f,
|
||||
0.54071f, 0.83304f, 0.03446f, 0.65041f, 0.58601f, 0.7208f, 0.49659f,
|
||||
0.60447f, 0.70867f, 0.33336f, 0.0199f, 0.53188f, 0.15774f, 0.31791f,
|
||||
0.2611f, 0.66174f, 0.22588f, 0.95612f, 0.01209f, 0.2239f, 0.51731f,
|
||||
0.80405f, 0.09126f, 0.85215f, 0.01911f, 0.7448f, 0.61376f, 0.22161f,
|
||||
0.71483f, 0.36953f, 0.67528f, 0.77609f, 0.5933f, 0.66035f, 0.79205f,
|
||||
0.04973f, 0.78845f, 0.4023f, 0.55086f, 0.03391f, 0.06616f, 0.45673f,
|
||||
0.24497f, 0.87024f, 0.43097f, 0.24168f, 0.66931f, 0.03734f, 0.31513f,
|
||||
0.46383f, 0.74909f, 0.57525f, 0.5295f, 0.48876f, 0.89053f, 0.31964f,
|
||||
0.87242f, 0.08605f, 0.30465f, 0.8018f, 0.53419f, 0.73341f, 0.12307f,
|
||||
0.70645f, 0.40608f, 0.89397f, 0.97853f, 0.67084f, 0.47644f, 0.39974f,
|
||||
0.97879f, 0.86642f, 0.20244f, 0.66219f, 0.11623f, 0.18979f, 0.52886f,
|
||||
0.44583f, 0.41313f, 0.19766f, 0.47676f, 0.48318f, 0.02079f, 0.83777f,
|
||||
0.41167f, 0.57684f, 0.79578f, 0.17775f
|
||||
0.18313f, 0.53363f, 0.39527f, 0.27575f, 0.3433f, 0.41746f,
|
||||
0.16831f, 0.61693f, 0.54599f, 0.99692f, 0.77127f, 0.25146f,
|
||||
0.4206f, 0.16291f, 0.93484f, 0.79765f, 0.74982f, 0.78336f,
|
||||
0.6386f, 0.87744f, 0.33587f, 0.9691f, 0.68437f, 0.65098f,
|
||||
0.48153f, 0.97546f, 0.8026f, 0.36689f, 0.98152f, 0.37351f,
|
||||
0.68407f, 0.2684f, 0.2855f, 0.76195f, 0.67828f, 0.603f
|
||||
};
|
||||
conv2d.setWeights(conv2dWeights.data());
|
||||
model.addLayer("conv2d", &conv2d);
|
||||
@@ -59,26 +44,18 @@ TEST(Model, TestModelPredict) {
|
||||
|
||||
// dense
|
||||
CUDANet::Layers::Dense dense(
|
||||
18, 6, CUDANet::Layers::ActivationType::SOFTMAX
|
||||
8, 3, CUDANet::Layers::ActivationType::SOFTMAX
|
||||
);
|
||||
// dense weights 18*6
|
||||
std::vector<float> denseWeights = {
|
||||
0.36032f, 0.33115f, 0.02948f, 0.56265f, 0.23524f, 0.96589f, 0.09802f,
|
||||
0.45072f, 0.56266f, 0.5246f, 0.86663f, 0.30252f, 0.43514f, 0.80946f,
|
||||
0.43439f, 0.95206f, 0.5658f, 0.16344f, 0.90916f, 0.08605f, 0.07473f,
|
||||
0.95572f, 0.9127f, 0.96697f, 0.94788f, 0.66168f, 0.34927f, 0.86927f,
|
||||
0.10111f, 0.67001f, 0.09464f, 0.61963f, 0.73775f, 0.15255f, 0.37537f,
|
||||
0.72831f, 0.51559f, 0.81916f, 0.64915f, 0.23607f, 0.88699f, 0.39844f,
|
||||
0.03934f, 0.87608f, 0.68364f, 0.03633f, 0.11632f, 0.99925f, 0.86102f,
|
||||
0.6659f, 0.11022f, 0.47878f, 0.92411f, 0.38027f, 0.06771f, 0.99645f,
|
||||
0.47783f, 0.54653f, 0.41552f, 0.61055f, 0.50326f, 0.79817f, 0.20008f,
|
||||
0.32929f, 0.23562f, 0.0033f, 0.46628f, 0.04958f, 0.05235f, 0.28102f,
|
||||
0.45705f, 0.78327f, 0.91427f, 0.41122f, 0.08883f, 0.43558f, 0.14724f,
|
||||
0.74515f, 0.98215f, 0.50503f, 0.02887f, 0.25426f, 0.3463f, 0.81567f,
|
||||
0.84608f, 0.15469f, 0.6069f, 0.54311f, 0.77967f, 0.50657f, 0.18208f,
|
||||
0.7969f, 0.48401f, 0.36097f, 0.7563f, 0.50316f, 0.1134f, 0.98089f,
|
||||
0.97041f, 0.4832f, 0.79216f, 0.06572f, 0.09688f, 0.51555f, 0.1652f,
|
||||
0.73933f, 0.44365f, 0.66949f
|
||||
0.36032f, 0.33115f, 0.02948f,
|
||||
0.09802f, 0.45072f, 0.56266f,
|
||||
0.43514f, 0.80946f, 0.43439f,
|
||||
0.90916f, 0.08605f, 0.07473f,
|
||||
0.94788f, 0.66168f, 0.34927f,
|
||||
0.09464f, 0.61963f, 0.73775f,
|
||||
0.51559f, 0.81916f, 0.64915f,
|
||||
0.03934f, 0.87608f, 0.68364f,
|
||||
};
|
||||
dense.setWeights(denseWeights.data());
|
||||
model.addLayer("dense", &dense);
|
||||
@@ -98,12 +75,14 @@ TEST(Model, TestModelPredict) {
|
||||
0.30514f, 0.23521f
|
||||
};
|
||||
|
||||
std::vector<float> expected = {2e-05f, 0.00021f, 0.99977f};
|
||||
|
||||
// predict
|
||||
const float* output = model.predict(input.data());
|
||||
float sum = 0.0f;
|
||||
|
||||
// float sum = 0.0f;
|
||||
float sum = 0.0f;
|
||||
for (int i = 0; i < outputSize; ++i) {
|
||||
EXPECT_NEAR(expected[i], output[i], 1e-5f);
|
||||
sum += output[i];
|
||||
}
|
||||
|
||||
|
||||
88
tools/model_test.py
Normal file
88
tools/model_test.py
Normal file
@@ -0,0 +1,88 @@
|
||||
import torch
|
||||
import utils
|
||||
|
||||
class TestModel(torch.nn.Module):
|
||||
|
||||
def __init__(self, *args, **kwargs) -> None:
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
self.conv1 = torch.nn.Conv2d(
|
||||
in_channels=2,
|
||||
out_channels=2,
|
||||
kernel_size=3,
|
||||
stride=1,
|
||||
padding=0,
|
||||
bias=False
|
||||
)
|
||||
|
||||
self.maxpool1 = torch.nn.MaxPool2d(
|
||||
kernel_size=2,
|
||||
stride=2
|
||||
)
|
||||
self.activation = torch.nn.ReLU()
|
||||
|
||||
self.linear = torch.nn.Linear(
|
||||
in_features=8,
|
||||
out_features=3,
|
||||
bias=False
|
||||
)
|
||||
self.softmax = torch.nn.Softmax(dim=0)
|
||||
|
||||
def set_weights(self):
|
||||
|
||||
conv2d_weights = torch.tensor([
|
||||
0.18313, 0.53363, 0.39527, 0.27575, 0.3433, 0.41746,
|
||||
0.16831, 0.61693, 0.54599, 0.99692, 0.77127, 0.25146,
|
||||
0.4206, 0.16291, 0.93484, 0.79765, 0.74982, 0.78336,
|
||||
0.6386, 0.87744, 0.33587, 0.9691, 0.68437, 0.65098,
|
||||
0.48153, 0.97546, 0.8026, 0.36689, 0.98152, 0.37351,
|
||||
0.68407, 0.2684, 0.2855, 0.76195, 0.67828, 0.603
|
||||
|
||||
]).reshape(2, 2, 3, 3)
|
||||
self.conv1.weight = torch.nn.Parameter(conv2d_weights)
|
||||
|
||||
linear_weights = torch.tensor([
|
||||
0.36032, 0.33115, 0.02948,
|
||||
0.09802, 0.45072, 0.56266,
|
||||
0.43514, 0.80946, 0.43439,
|
||||
0.90916, 0.08605, 0.07473,
|
||||
0.94788, 0.66168, 0.34927,
|
||||
0.09464, 0.61963, 0.73775,
|
||||
0.51559, 0.81916, 0.64915,
|
||||
0.03934, 0.87608, 0.68364,
|
||||
]).reshape(3, 8)
|
||||
self.linear.weight = torch.nn.Parameter(linear_weights)
|
||||
|
||||
def forward(self, x):
|
||||
x = self.conv1(x)
|
||||
x = self.maxpool1(x)
|
||||
x = self.activation(x)
|
||||
|
||||
x = torch.flatten(x)
|
||||
x = self.linear(x)
|
||||
x = self.softmax(x)
|
||||
|
||||
return x
|
||||
|
||||
if __name__ == "__main__":
|
||||
model = TestModel()
|
||||
model.set_weights()
|
||||
|
||||
input = torch.tensor([
|
||||
0.12762, 0.99056, 0.77565, 0.29058, 0.29787, 0.58415, 0.20484,
|
||||
0.05415, 0.60593, 0.3162, 0.08198, 0.92749, 0.72392, 0.91786,
|
||||
0.65266, 0.80908, 0.53389, 0.36069, 0.18614, 0.52381, 0.08525,
|
||||
0.43054, 0.3355, 0.96587, 0.98194, 0.71336, 0.78392, 0.50648,
|
||||
0.40355, 0.31863, 0.54686, 0.1836, 0.77171, 0.01262, 0.41108,
|
||||
0.53467, 0.3553, 0.42808, 0.45798, 0.29958, 0.3923, 0.98277,
|
||||
0.02033, 0.99868, 0.90584, 0.57554, 0.15957, 0.91273, 0.38901,
|
||||
0.27097, 0.64788, 0.84272, 0.42984, 0.07466, 0.53658, 0.83388,
|
||||
0.28232, 0.48046, 0.85626, 0.04721, 0.36139, 0.6123, 0.56991,
|
||||
0.84854, 0.61415, 0.2466, 0.20017, 0.78952, 0.93797, 0.27884,
|
||||
0.30514, 0.23521
|
||||
]).reshape(2, 6, 6)
|
||||
|
||||
# input = torch.rand(2, 6, 6)
|
||||
|
||||
out = model(input)
|
||||
utils.print_cpp_vector(out)
|
||||
Reference in New Issue
Block a user