Skip to content

Commit b087381

Browse files
AndreySorokin7AndreySorokin7
and
AndreySorokin7
authored
Accuracy Verification (#151)
Close #78 --------- Co-authored-by: AndreySorokin7 <andrey_sorokin_nn@mail,ru>
1 parent dfa1fb9 commit b087381

File tree

5 files changed

+119
-0
lines changed

5 files changed

+119
-0
lines changed

app/Accuracy/CMakeLists.txt

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
file(MAKE_DIRECTORY "${CMAKE_SOURCE_DIR}/3rdparty/opencv/build")
2+
3+
execute_process(
4+
COMMAND ${CMAKE_COMMAND} -S "${CMAKE_SOURCE_DIR}/3rdparty/opencv" -B "${CMAKE_SOURCE_DIR}/3rdparty/opencv/build" -DCMAKE_C_COMPILER_LAUNCHER=${CMAKE_C_COMPILER_LAUNCHER} -DCMAKE_CXX_COMPILER_LAUNCHER=${CMAKE_CXX_COMPILER_LAUNCHER} -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE} -DBUILD_PERF_TESTS=OFF -DBUILD_TESTS=OFF -DBUILD_opencv_apps=OFF
5+
WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}/3rdparty/opencv/build"
6+
)
7+
execute_process(
8+
COMMAND ${CMAKE_COMMAND} --build "${CMAKE_SOURCE_DIR}/3rdparty/opencv/build" --config "${CMAKE_BUILD_TYPE}"
9+
WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}/3rdparty/opencv/build"
10+
)
11+
12+
set(INCLUDE_HEADERS "${CMAKE_CURRENT_SOURCE_DIR}/acc.hpp")
13+
set(SRC_FILES "${CMAKE_CURRENT_SOURCE_DIR}/acc.cpp")
14+
add_library(ACCLib STATIC ${INCLUDE_HEADERS} ${SRC_FILES})
15+
16+
set_target_properties(ReadLib PROPERTIES LINKER_LANGUAGE CXX)
17+
18+
find_package( OpenCV REQUIRED PATHS "${CMAKE_SOURCE_DIR}/3rdparty/opencv/build" )
19+
include_directories( ${OpenCV_INCLUDE_DIRS} )
20+
target_link_libraries( ACCLib ${OpenCV_LIBS} )
21+
target_link_libraries( ACCLib TBB::tbb)
22+
target_link_libraries( ACCLib layers_lib)
23+
target_link_libraries( ACCLib gtest_main)
24+
25+
add_executable(Accuracy_Check accuracy_check.cpp)
26+
target_link_libraries(Accuracy_Check ACCLib)
27+
28+
if (WIN32)
29+
add_custom_command(TARGET Accuracy_Check POST_BUILD
30+
COMMAND ${CMAKE_COMMAND} -E copy_directory
31+
"${CMAKE_SOURCE_DIR}/3rdparty/opencv/build/bin/${CMAKE_BUILD_TYPE}"
32+
"${CMAKE_BINARY_DIR}/app/ReaderImage/${CMAKE_BUILD_TYPE}/")
33+
endif()
34+
35+
file(DOWNLOAD
36+
"https://raw.githubusercontent.com/opencv/opencv/4.x/samples/data/lena.jpg"
37+
"${CMAKE_CURRENT_BINARY_DIR}/image.jpg"
38+
SHOW_PROGRESS
39+
STATUS status_code
40+
LOG log_file
41+
)
42+
add_definitions(-DIMAGE1_PATH="${CMAKE_CURRENT_BINARY_DIR}/image.jpg")

app/Accuracy/acc.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#include "acc.hpp"
2+
3+
#include <stdexcept>

app/Accuracy/acc.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#pragma once
2+
#include <iostream>
3+
#include <opencv2/opencv.hpp>

app/Accuracy/accuracy_check.cpp

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#include "acc.hpp"
2+
#include "graph/graph.hpp"
3+
#include "layers/ConvLayer.hpp"
4+
#include "layers/EWLayer.hpp"
5+
#include "layers/FCLayer.hpp"
6+
#include "layers/InputLayer.hpp"
7+
#include "layers/OutputLayer.hpp"
8+
#include "layers/PoolingLayer.hpp"
9+
10+
using namespace itlab_2023;
11+
12+
int main() {
13+
std::string image_path = IMAGE1_PATH;
14+
cv::Mat image = cv::imread(image_path);
15+
if (image.empty()) {
16+
throw std::runtime_error("Failed to load image");
17+
}
18+
cv::Mat resized_image;
19+
cv::resize(image, resized_image, cv::Size(227, 227));
20+
std::vector<cv::Mat> channels;
21+
cv::split(resized_image, channels);
22+
int count_pic = 1;
23+
std::vector<float> res(count_pic * 227 * 227 * 3);
24+
int c = 0;
25+
for (int i = 0; i < 227; ++i) {
26+
for (int j = 0; j < 227; ++j) {
27+
res[c] = channels[2].at<uchar>(i, j);
28+
c++;
29+
res[c] = channels[1].at<uchar>(i, j);
30+
c++;
31+
res[c] = channels[0].at<uchar>(i, j);
32+
c++;
33+
}
34+
}
35+
Shape sh({static_cast<size_t>(count_pic), 227, 227, 3});
36+
Tensor t = make_tensor<float>(res, sh);
37+
Graph graph(6);
38+
Shape sh1({1, 5, 5, 3});
39+
std::vector<float> vec;
40+
vec.reserve(75);
41+
for (int i = 0; i < 75; ++i) {
42+
vec.push_back(3);
43+
}
44+
Tensor input = t;
45+
Tensor output = make_tensor(vec, sh1);
46+
InputLayer a1(kNhwc, kNchw, 1, 2);
47+
std::vector<float> kernelvec = {1, 1, 1, 1, 1, 1, 1, 1, 1};
48+
Shape sh2({3, 3});
49+
Tensor kernel = make_tensor(kernelvec, sh2);
50+
ConvolutionalLayer a2(1, 0, 0, kernel);
51+
Shape poolshape = {2, 2};
52+
EWLayer a3("linear", 2.0F, 3.0F);
53+
PoolingLayer a4(poolshape, "average");
54+
FCLayer a6;
55+
OutputLayer a5;
56+
graph.setInput(a1, input);
57+
graph.makeConnection(a1, a2);
58+
graph.makeConnection(a2, a3);
59+
graph.makeConnection(a3, a4);
60+
graph.makeConnection(a4, a5);
61+
graph.makeConnection(a5, a6);
62+
graph.setOutput(a5, output);
63+
graph.inference();
64+
std::vector<float> tmp = *output.as<float>();
65+
std::vector<float> tmp_output = softmax<float>(*output.as<float>());
66+
for (float i : tmp) {
67+
std::cout << i << " ";
68+
}
69+
}

app/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
add_subdirectory(ReaderImage)
2+
3+
add_subdirectory(Accuracy)
24
add_subdirectory(AlexNet)

0 commit comments

Comments
 (0)