Remove preprocessing test

This commit is contained in:
2024-09-04 21:31:53 +02:00
parent 4fef663360
commit c8bc6f7a39
2 changed files with 0 additions and 326 deletions

View File

@@ -1,60 +0,0 @@
import sys
import torch
import torchvision.transforms as transforms
from PIL import Image
import numpy as np
sys.path.append("../../../tools")
from utils import print_cpp_vector
torch.manual_seed(0)
def generate_random_image(size=(24, 24)):
# Generate a random RGB image
random_image = np.random.randint(0, 256, size=(*size, 3), dtype=np.uint8)
return Image.fromarray(random_image)
def preprocess_image(image, resize=16, crop=16):
preprocess = transforms.Compose([
transforms.Resize(resize),
transforms.CenterCrop(crop),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
])
return preprocess(image)
def normalize_tensor(tensor):
normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
return normalize(tensor)
def gen_preprocess_test_result():
# Generate a random image
random_image = generate_random_image()
random_image.save("resources/test_image.jpg")
# Preprocess the image
preprocessed = preprocess_image(random_image)
# Print the preprocessed data
print("Preprocessed image data:")
print_cpp_vector(preprocessed.flatten(), "output")
def gen_normalize_test_result():
input_tensor = torch.rand(3, 8, 8)
print("Input tensor: ")
print_cpp_vector(input_tensor.flatten(), "input")
normalized = normalize_tensor(input_tensor)
print_cpp_vector(normalized.flatten(), "expected_output")
if __name__ == "__main__":
# print("Preprocess Test\n")
# gen_preprocess_test_result()
print("\nNormalize Test\n")
gen_normalize_test_result()

View File

@@ -1,266 +0,0 @@
#include <gtest/gtest.h>
#include <cmath>
#include <inception_v3.hpp>
#include <opencv2/opencv.hpp>
#include <vector>
TEST(ImageProcessingTest, TestNormalization) {
std::vector<float> input = {
0.49626f, 0.76822f, 0.08848f, 0.13203f, 0.30742f, 0.63408f, 0.49009f,
0.89644f, 0.45563f, 0.63231f, 0.34889f, 0.40172f, 0.02233f, 0.16886f,
0.29389f, 0.51852f, 0.69767f, 0.80001f, 0.16103f, 0.28227f, 0.68161f,
0.91519f, 0.3971f, 0.87416f, 0.41941f, 0.55291f, 0.95274f, 0.03616f,
0.18523f, 0.37342f, 0.3051f, 0.932f, 0.17591f, 0.26983f, 0.15068f,
0.03172f, 0.20813f, 0.9298f, 0.72311f, 0.74234f, 0.5263f, 0.24366f,
0.58459f, 0.03315f, 0.13872f, 0.24224f, 0.81547f, 0.79316f, 0.27825f,
0.48196f, 0.81978f, 0.99707f, 0.69844f, 0.56755f, 0.83524f, 0.2056f,
0.59317f, 0.11235f, 0.15346f, 0.24171f, 0.72624f, 0.70108f, 0.20382f,
0.65105f, 0.77449f, 0.43689f, 0.51909f, 0.61585f, 0.81019f, 0.9801f,
0.11469f, 0.31677f, 0.6965f, 0.91427f, 0.9351f, 0.94118f, 0.59951f,
0.06521f, 0.546f, 0.1872f, 0.03402f, 0.94425f, 0.88018f, 0.00124f,
0.59359f, 0.41577f, 0.41772f, 0.27112f, 0.69228f, 0.20385f, 0.6833f,
0.75285f, 0.85794f, 0.68696f, 0.00513f, 0.17565f, 0.74966f, 0.60465f,
0.10996f, 0.21209f, 0.97037f, 0.83691f, 0.28199f, 0.37416f, 0.0237f,
0.49101f, 0.12347f, 0.11432f, 0.47245f, 0.57507f, 0.29523f, 0.79669f,
0.19573f, 0.95369f, 0.84265f, 0.07836f, 0.37556f, 0.52256f, 0.57295f,
0.61859f, 0.69621f, 0.52995f, 0.25604f, 0.73659f, 0.02038f, 0.20365f,
0.37484f, 0.25644f, 0.32508f, 0.09019f, 0.39364f, 0.60688f, 0.17427f,
0.47434f, 0.85793f, 0.4486f, 0.5139f, 0.45687f, 0.60119f, 0.81792f,
0.97362f, 0.81753f, 0.97471f, 0.46384f, 0.05084f, 0.26296f, 0.84045f,
0.49676f, 0.25148f, 0.11684f, 0.03207f, 0.078f, 0.39858f, 0.7742f,
0.77032f, 0.01778f, 0.81189f, 0.10875f, 0.39429f, 0.29726f, 0.40369f,
0.40183f, 0.05133f, 0.06828f, 0.42176f, 0.50647f, 0.27286f, 0.68835f,
0.04997f, 0.46626f, 0.93971f, 0.29605f, 0.9515f, 0.68108f, 0.04877f,
0.81635f, 0.4423f, 0.2768f, 0.89983f, 0.09595f, 0.55365f, 0.39532f,
0.85706f, 0.63957f, 0.74025f, 0.67658f, 0.37976f, 0.39485f, 0.08796f,
0.77092f, 0.89699f, 0.84211f
};
std::vector<float> expected_output = {
0.04916f, 1.23678f, -1.73154f, -1.54135f, -0.77545f, 0.651f,
0.02224f, 1.7967f, -0.12826f, 0.64326f, -0.59435f, -0.36368f,
-2.02041f, -1.38053f, -0.83455f, 0.14638f, 0.92868f, 1.3756f,
-1.41472f, -0.88529f, 0.85855f, 1.87858f, -0.38384f, 1.69937f,
-0.28643f, 0.29654f, 2.04252f, -1.95998f, -1.30903f, -0.48726f,
-0.78559f, 1.95197f, -1.34974f, -0.93959f, -1.45991f, -1.97939f,
-1.20904f, 1.94235f, 1.03978f, 1.12374f, 0.18033f, -1.05389f,
0.4349f, -1.97313f, -1.51215f, -1.06011f, 1.4431f, 1.34568f,
-0.90283f, -0.01328f, 1.46192f, 2.2361f, 0.93206f, 0.36046f,
1.52945f, -1.22009f, 0.47237f, -1.6273f, -1.44779f, -1.06241f,
1.05343f, 0.94358f, -1.22784f, 0.72512f, 1.42181f, -0.08531f,
0.28166f, 0.71363f, 1.5812f, 2.33972f, -1.52371f, -0.62158f,
1.07368f, 2.04587f, 2.13886f, 2.16597f, 0.64066f, -1.7446f,
0.40177f, -1.20001f, -1.88383f, 2.17967f, 1.89366f, -2.0302f,
0.61422f, -0.1796f, -0.1709f, -0.82535f, 1.05481f, -1.12568f,
1.01471f, 1.32524f, 1.79436f, 1.03105f, -2.0128f, -1.25156f,
1.31097f, 0.66362f, -1.54483f, -1.08888f, 2.29632f, 1.70049f,
-0.77684f, -0.36537f, -1.92991f, 0.15631f, -1.48451f, -1.52535f,
0.07344f, 0.53157f, -0.7177f, 1.52093f, -1.16192f, 2.22181f,
1.72612f, -1.6859f, -0.35912f, 0.29715f, 0.5221f, 0.72584f,
1.07238f, 0.33013f, -0.8927f, 1.25265f, -1.94475f, -1.12658f,
-0.36234f, -0.89088f, -0.35963f, -1.4036f, -0.05492f, 0.89279f,
-1.02992f, 0.30373f, 2.00856f, 0.18933f, 0.47954f, 0.22607f,
0.86751f, 1.83075f, 2.52277f, 1.82901f, 2.52759f, 0.25706f,
-1.57849f, -0.63573f, 1.9309f, 0.40337f, -0.68677f, -1.28514f,
-1.66189f, -1.4578f, -0.03297f, 1.63646f, 1.6192f, -1.7254f,
1.80396f, -1.32113f, -0.05202f, -0.48327f, -0.01026f, -0.01854f,
-1.57633f, -1.50097f, 0.07005f, 0.44652f, -0.59172f, 1.25489f,
-1.58235f, 0.26781f, 2.37204f, -0.48865f, 2.42445f, 1.22256f,
-1.58769f, 1.82377f, 0.16135f, -0.57424f, 2.19479f, -1.37799f,
0.65623f, -0.04749f, 2.00469f, 1.0381f, 1.48557f, 1.20258f,
-0.11661f, -0.04957f, -1.41351f, 1.62188f, 2.18217f, 1.93828f
};
const int height = 8;
const int width = 8;
const int channels = 3;
ASSERT_EQ(input.size(), channels * height * width);
ASSERT_EQ(expected_output.size(), channels * height * width);
cv::Mat image(height, width, CV_32FC3);
for (int y = 0; y < height; ++y) {
for (int x = 0; x < width; ++x) {
for (int c = 0; c < channels; ++c) {
image.at<cv::Vec3f>(y, x)[c] = input[c * height * width + y * width + x];
}
}
}
cv::Mat mean(image.size(), CV_32FC3, cv::Scalar(0.485, 0.456, 0.406));
cv::Mat std(image.size(), CV_32FC3, cv::Scalar(0.229, 0.224, 0.225));
cv::subtract(image, mean, image);
cv::divide(image, std, image);
std::vector<float> output(channels * height * width);
for (int y = 0; y < height; ++y) {
for (int x = 0; x < width; ++x) {
for (int c = 0; c < channels; ++c) {
output[c * height * width + y * width + x] = image.at<cv::Vec3f>(y, x)[c];
}
}
}
ASSERT_EQ(output.size(), expected_output.size());
for (size_t i = 0; i < output.size(); ++i) {
EXPECT_NEAR(output[i], expected_output[i], 1e-3f)
<< "Mismatch at index " << i
<< " (channel=" << (i / (height * width))
<< ", y=" << ((i % (height * width)) / width)
<< ", x=" << ((i % (height * width)) % width) << ")";
}
}
TEST(ImageProcessingTest, TestPreprocessing) {
int input_size = 24;
int resize_size = 16;
int crop_size = 16;
std::string input_path = "../tests/resources/test_image.jpg";
std::vector<float> expected_output = {
-0.23418f, -0.57668f, -1.07329f, 0.77618f, 0.26244f, 0.02269f,
1.52967f, 0.34806f, 0.43368f, 0.22819f, -0.91917f, -0.83355f,
0.46793f, 0.36519f, -0.09718f, -0.11431f, -0.47393f, -1.19317f,
-0.06293f, 0.9988f, 0.74193f, 0.51931f, 0.84468f, 0.09119f,
0.27956f, 0.09119f, 0.14256f, -0.01156f, -0.23418f, 0.09119f,
-0.08006f, 0.81043f, -0.23418f, 0.89605f, 0.96455f, 0.50218f,
0.57068f, -0.04581f, 0.82755f, 0.51931f, 0.72481f, 0.84468f,
-0.35405f, -0.57668f, -0.42255f, -0.47393f, -0.69655f, 0.58781f,
0.9303f, 0.84468f, -0.83355f, -0.50818f, -0.35405f, 0.72481f,
0.9303f, -0.3883f, 0.19394f, -0.04581f, 0.31381f, -0.71367f,
-1.24454f, -0.47393f, 0.26244f, -0.78217f, -0.43968f, 0.36519f,
0.91318f, -0.28556f, -0.01156f, 1.08443f, 0.75905f, -0.67942f,
0.33094f, 0.02269f, 0.41656f, 0.58781f, 0.02269f, -0.71367f,
-0.3198f, -0.30268f, -0.16568f, -0.18281f, 1.15292f, 0.00557f,
-0.35405f, -0.16568f, 0.70768f, 0.48506f, 0.38231f, 1.03305f,
0.9988f, -0.02868f, -0.7308f, -0.91917f, -0.01156f, 0.65631f,
-0.71367f, 0.03981f, 0.87893f, 0.00557f, 0.03981f, -0.42255f,
0.46793f, 0.33094f, -0.11431f, -0.35405f, -0.61093f, -0.16568f,
-1.09042f, 0.05694f, -0.14856f, 0.74193f, -0.42255f, 0.24531f,
0.91318f, 0.70768f, 1.03305f, 1.03305f, 1.08443f, 0.67343f,
-0.71367f, -1.14179f, -0.54243f, 0.19394f, -0.42255f, -0.43968f,
-0.85067f, 0.34806f, -0.30268f, 0.17681f, 0.75905f, 0.81043f,
0.91318f, 1.15292f, 0.41656f, 0.58781f, -0.5253f, -0.43968f,
-0.28556f, -0.47393f, 0.27956f, -0.37118f, 0.57068f, 0.58781f,
0.12544f, -0.25131f, -0.25131f, -0.04581f, 0.9988f, 1.1358f,
1.10155f, 0.10831f, -0.09718f, 0.24531f, -0.43968f, -0.78217f,
-0.18281f, 0.82755f, 0.45081f, 0.26244f, 0.22819f, -0.81642f,
-0.11431f, -0.23418f, 0.07406f, 0.22819f, -0.23418f, -0.7993f,
-0.25131f, -0.16568f, -0.35405f, -0.9363f, -0.30268f, 0.98168f,
0.72481f, -0.21706f, -0.09718f, -0.42255f, -0.54243f, -0.02868f,
0.07406f, -1.07329f, 0.24531f, -0.01156f, 0.57068f, 0.15969f,
0.65631f, -0.28556f, -0.57668f, 0.91318f, 0.9303f, -0.19993f,
-0.74792f, -0.40543f, -0.37118f, -0.76505f, -0.11431f, 0.07406f,
0.33094f, 1.10155f, 0.34806f, 0.36519f, 0.84468f, 0.7933f,
0.45081f, 0.19394f, -0.23418f, -0.9363f, -0.04581f, -0.02868f,
0.14256f, -1.07329f, -0.8678f, -0.16568f, -0.25131f, 0.84468f,
1.32417f, -0.5253f, -0.37118f, 0.57068f, 0.15969f, -0.71367f,
-0.7993f, 0.50218f, -0.55955f, -0.40543f, -0.09718f, -0.25131f,
-0.91917f, -0.69655f, -0.49105f, 0.9303f, 0.15969f, 0.63918f,
0.63918f, 0.8618f, 1.10155f, -0.21706f, 0.48506f, 0.45081f,
-0.5253f, -0.33693f, 0.36519f, -0.04581f, 0.43368f, -0.37118f,
0.17681f, -0.08006f, 0.03981f, 1.0673f, 0.48506f, 0.36519f,
0.45081f, 0.07406f, 0.07406f, 0.12544f, 0.59034f, -0.23249f,
-0.51261f, 0.73039f, -0.09244f, -0.39006f, -0.7577f, -0.0049f,
-0.60014f, -0.89776f, -0.14496f, 0.41527f, -0.32003f, 0.7479f,
-0.44258f, -0.19748f, 0.60784f, -0.61765f, 0.17017f, 0.38025f,
0.34524f, 0.10014f, -0.42507f, -0.33753f, -0.39006f, 0.15266f,
0.60784f, -0.10994f, 0.53782f, 0.73039f, -0.30252f, -0.56513f,
0.993f, -0.0049f, -0.16246f, -0.14496f, 0.22269f, -0.37255f,
0.38025f, -0.05742f, -0.53011f, -0.7577f, -0.25f, -0.33753f,
-0.05742f, 0.18768f, -0.53011f, -0.26751f, 0.27521f, -1.0028f,
-0.23249f, -0.25f, 0.66036f, 0.03011f, 0.71289f, 0.83543f,
-0.23249f, 0.64286f, -0.70518f, -0.44258f, 0.13515f, 0.13515f,
0.69538f, 0.53782f, -0.67017f, -1.33543f, 0.31022f, -0.21499f,
0.48529f, -0.65266f, -0.09244f, 0.46779f, 0.71289f, 0.38025f,
-0.86274f, -0.21499f, 0.32773f, -0.70518f, 0.2402f, -0.88025f,
-0.53011f, -1.12535f, -0.12745f, -0.09244f, 0.46779f, 0.71289f,
0.94048f, 0.36275f, -0.05742f, 0.15266f, 0.31022f, 1.02801f,
1.16807f, -0.40756f, 0.5028f, -0.16246f, 0.5028f, 0.18768f,
-0.09244f, -0.91527f, 0.95798f, 0.39776f, 0.78291f, 0.78291f,
0.13515f, 0.06513f, 0.27521f, 0.81793f, -0.25f, -0.09244f,
0.15266f, 0.43277f, 0.80042f, -0.16246f, -0.35504f, -0.40756f,
0.31022f, -0.32003f, 0.29272f, 0.08263f, -0.35504f, -0.98529f,
-0.81022f, -0.42507f, -0.03992f, -0.02241f, -0.23249f, -0.12745f,
0.92297f, 0.43277f, -0.40756f, -0.17997f, -0.7577f, -0.25f,
0.81793f, 0.87045f, 0.94048f, 0.32773f, 0.17017f, -0.03992f,
0.83543f, -0.12745f, -0.12745f, 0.13515f, -0.09244f, -0.33753f,
0.22269f, 0.64286f, -0.51261f, 0.18768f, 0.08263f, 1.29062f,
1.11555f, 0.55532f, 0.73039f, -0.30252f, -0.86274f, -0.56513f,
-0.14496f, -0.37255f, -0.19748f, 0.06513f, -0.93277f, 0.06513f,
-0.60014f, -0.88025f, -0.67017f, -0.05742f, 0.31022f, 0.32773f,
-0.56513f, 0.2577f, -0.17997f, -0.0049f, -0.53011f, -0.98529f,
0.46779f, -0.39006f, 0.01261f, -0.10994f, 0.80042f, 0.15266f,
-0.44258f, 0.43277f, 0.88796f, -0.12745f, -0.86274f, -0.0049f,
0.69538f, 1.2381f, -0.23249f, 0.17017f, 0.36275f, -0.10994f,
1.43067f, 1.30812f, 1.16807f, 1.34314f, 0.88796f, 1.04552f,
0.60784f, 0.22269f, -0.12745f, 0.5028f, 0.13515f, 0.48529f,
-0.09244f, -0.17997f, -0.14496f, 1.44818f, 0.71289f, 0.08263f,
0.10014f, 0.03011f, 0.53782f, 1.02801f, 1.0105f, 0.90546f,
-0.23249f, 0.66036f, 1.11555f, 0.29272f, 0.52031f, 0.76541f,
-0.32003f, 0.69538f, -0.30252f, -0.93277f, 0.43277f, 0.22269f,
0.08263f, 1.20308f, 0.17017f, 0.5028f, 0.97549f, 0.71289f,
0.39776f, 0.36275f, 0.83543f, -0.23249f, -0.30252f, 0.59034f,
-0.16246f, 0.34524f, 0.43277f, 0.08263f, 0.2577f, 0.64286f,
0.71289f, 0.20518f, -0.16246f, 0.76541f, 1.51821f, -0.10994f,
-0.72269f, -1.5105f, -0.51468f, -0.37525f, 0.2522f, 1.21081f,
1.76854f, 0.51364f, 0.6705f, 1.22824f, 0.58336f, 0.53107f,
0.65307f, 0.75765f, 0.33935f, 0.65307f, -0.20096f, 0.74022f,
-0.51468f, -0.77612f, -0.00924f, 1.40253f, 0.23477f, -0.61926f,
0.07791f, -0.00924f, -0.39268f, 0.58336f, 0.04305f, 0.47878f,
0.72279f, 0.18248f, 0.26963f, 0.68793f, 0.49621f, 0.18248f,
-0.27068f, -0.06152f, 0.89708f, -0.35782f, -0.21839f, -0.09638f,
0.07791f, 1.10623f, 0.51364f, 0.02562f, 0.60078f, -0.02667f,
-0.09638f, -0.49725f, 0.47878f, 0.07791f, -0.60183f, -0.20096f,
1.10623f, 0.51364f, 0.58336f, -0.37525f, -0.06152f, 0.07791f,
0.86222f, 0.56593f, -0.21839f, -0.07895f, 0.46135f, 0.06048f,
0.16505f, 0.68793f, 0.46135f, 0.47878f, 0.42649f, 0.39163f,
0.79251f, -0.18353f, 0.2522f, 1.0888f, 0.77508f, 0.19991f,
0.33935f, 0.07791f, 0.72279f, 0.79251f, 1.31538f, 0.49621f,
0.61821f, 0.28706f, 0.56593f, 1.45482f, 1.24566f, 0.70536f,
1.87312f, 1.64654f, 1.15852f, 1.59425f, 0.42649f, -0.11381f,
0.00819f, 0.04305f, 0.40906f, 1.00166f, 0.18248f, 0.40906f,
0.84479f, 0.09534f, 0.5485f, 0.16505f, 0.35678f, 0.18248f,
0.94937f, 1.33281f, -0.11381f, -0.09638f, 0.32192f, 0.47878f,
-0.37525f, 0.80993f, 0.75765f, 0.80993f, 0.93194f, 0.60078f,
-0.06152f, -0.02667f, 0.72279f, 0.6705f, -0.47983f, 0.19991f,
-0.25325f, -0.34039f, 0.40906f, -0.06152f, 0.1302f, 1.3851f,
0.9668f, 0.58336f, 1.05394f, 1.12366f, 0.19991f, -0.11381f,
0.58336f, 0.18248f, 1.01909f, -0.63669f, 0.3742f, -0.32296f,
-0.44497f, -1.12471f, 0.40906f, 1.05394f, -0.14867f, 0.00819f,
0.93194f, 0.39163f, 1.07137f, -0.35782f, 0.40906f, 1.17595f,
0.93194f, -0.67155f, 0.11277f, 0.39163f, 1.21081f, -0.0441f,
1.14109f, 0.5485f, 0.06048f, -0.0441f, -0.14867f, 0.63564f,
0.02562f, 0.84479f, 0.58336f, 0.56593f, -0.07895f, -0.00924f,
0.32192f, 0.2522f, 0.61821f, -0.5844f, 0.9668f, 0.6705f,
1.22824f, 0.53107f, -0.32296f, -0.95041f, -0.67155f, -0.13124f,
0.21734f, 0.65307f, 1.12366f, 0.56593f, -0.77612f, 0.07791f,
0.39163f, 0.91451f, 0.33935f, 0.84479f, 0.6705f, -0.18353f,
-0.65412f, -0.98527f, -0.68898f, -0.11381f, 0.68793f, 1.21081f,
1.15852f, 0.80993f, -0.35782f, 0.91451f, 0.61821f, 1.00166f,
0.32192f, 0.68793f, 0.26963f, 0.42649f, -0.51468f, -0.77612f,
0.6705f, 0.77508f, 0.84479f, 0.5485f, 0.58336f, 0.87965f,
0.63564f, 0.33935f, 0.32192f, 0.18248f, 0.18248f, 1.14109f,
0.75765f, 0.11277f, -0.27068f, 0.51364f, 0.86222f, -0.35782f,
1.0888f, -0.02667f, -0.21839f, 0.18248f, 0.02562f, 0.28706f,
0.11277f, 0.19991f, 0.35678f, -0.02667f, 0.18248f, 0.18248f,
0.44392f, 1.54196f, 1.14109f, -0.68898f, -0.86327f, -0.2881f,
1.05394f, 0.70536f, -0.42754f, -1.0027f, 1.40253f, 1.43739f
};
ASSERT_EQ(expected_output.size(), crop_size * crop_size * 3);
// Process the image using your function
std::vector<float> output =
readAndNormalizeImage(input_path, resize_size, crop_size);
// Compare the output with the expected output
ASSERT_EQ(output.size(), crop_size * crop_size * 3);
for (size_t i = 0; i < output.size(); ++i) {
EXPECT_NEAR(output[i], expected_output[i], 1e-5)
<< "Mismatch at index " << i << " (y=" << (i / (crop_size * 3))
<< ", x=" << ((i / 3) % crop_size) << ", channel=" << (i % 3)
<< ")";
}
}