Skip to content

Commit

Permalink
Accuracy Verification (#151)
Browse files Browse the repository at this point in the history
Close #78

---------

Co-authored-by: AndreySorokin7 <andrey_sorokin_nn@mail,ru>
  • Loading branch information
AndreySorokin7 and AndreySorokin7 authored Aug 30, 2024
1 parent dfa1fb9 commit b087381
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 0 deletions.
42 changes: 42 additions & 0 deletions app/Accuracy/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
file(MAKE_DIRECTORY "${CMAKE_SOURCE_DIR}/3rdparty/opencv/build")

execute_process(
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
WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}/3rdparty/opencv/build"
)
execute_process(
COMMAND ${CMAKE_COMMAND} --build "${CMAKE_SOURCE_DIR}/3rdparty/opencv/build" --config "${CMAKE_BUILD_TYPE}"
WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}/3rdparty/opencv/build"
)

set(INCLUDE_HEADERS "${CMAKE_CURRENT_SOURCE_DIR}/acc.hpp")
set(SRC_FILES "${CMAKE_CURRENT_SOURCE_DIR}/acc.cpp")
add_library(ACCLib STATIC ${INCLUDE_HEADERS} ${SRC_FILES})

set_target_properties(ReadLib PROPERTIES LINKER_LANGUAGE CXX)

find_package( OpenCV REQUIRED PATHS "${CMAKE_SOURCE_DIR}/3rdparty/opencv/build" )
include_directories( ${OpenCV_INCLUDE_DIRS} )
target_link_libraries( ACCLib ${OpenCV_LIBS} )
target_link_libraries( ACCLib TBB::tbb)
target_link_libraries( ACCLib layers_lib)
target_link_libraries( ACCLib gtest_main)

add_executable(Accuracy_Check accuracy_check.cpp)
target_link_libraries(Accuracy_Check ACCLib)

if (WIN32)
add_custom_command(TARGET Accuracy_Check POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_directory
"${CMAKE_SOURCE_DIR}/3rdparty/opencv/build/bin/${CMAKE_BUILD_TYPE}"
"${CMAKE_BINARY_DIR}/app/ReaderImage/${CMAKE_BUILD_TYPE}/")
endif()

file(DOWNLOAD
"https://raw.githubusercontent.com/opencv/opencv/4.x/samples/data/lena.jpg"
"${CMAKE_CURRENT_BINARY_DIR}/image.jpg"
SHOW_PROGRESS
STATUS status_code
LOG log_file
)
add_definitions(-DIMAGE1_PATH="${CMAKE_CURRENT_BINARY_DIR}/image.jpg")
3 changes: 3 additions & 0 deletions app/Accuracy/acc.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#include "acc.hpp"

#include <stdexcept>
3 changes: 3 additions & 0 deletions app/Accuracy/acc.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#pragma once
#include <iostream>
#include <opencv2/opencv.hpp>
69 changes: 69 additions & 0 deletions app/Accuracy/accuracy_check.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#include "acc.hpp"
#include "graph/graph.hpp"
#include "layers/ConvLayer.hpp"
#include "layers/EWLayer.hpp"
#include "layers/FCLayer.hpp"
#include "layers/InputLayer.hpp"
#include "layers/OutputLayer.hpp"
#include "layers/PoolingLayer.hpp"

using namespace itlab_2023;

int main() {
std::string image_path = IMAGE1_PATH;
cv::Mat image = cv::imread(image_path);
if (image.empty()) {
throw std::runtime_error("Failed to load image");
}
cv::Mat resized_image;
cv::resize(image, resized_image, cv::Size(227, 227));
std::vector<cv::Mat> channels;
cv::split(resized_image, channels);
int count_pic = 1;
std::vector<float> res(count_pic * 227 * 227 * 3);
int c = 0;
for (int i = 0; i < 227; ++i) {
for (int j = 0; j < 227; ++j) {
res[c] = channels[2].at<uchar>(i, j);
c++;
res[c] = channels[1].at<uchar>(i, j);
c++;
res[c] = channels[0].at<uchar>(i, j);
c++;
}
}
Shape sh({static_cast<size_t>(count_pic), 227, 227, 3});
Tensor t = make_tensor<float>(res, sh);
Graph graph(6);
Shape sh1({1, 5, 5, 3});
std::vector<float> vec;
vec.reserve(75);
for (int i = 0; i < 75; ++i) {
vec.push_back(3);
}
Tensor input = t;
Tensor output = make_tensor(vec, sh1);
InputLayer a1(kNhwc, kNchw, 1, 2);
std::vector<float> kernelvec = {1, 1, 1, 1, 1, 1, 1, 1, 1};
Shape sh2({3, 3});
Tensor kernel = make_tensor(kernelvec, sh2);
ConvolutionalLayer a2(1, 0, 0, kernel);
Shape poolshape = {2, 2};
EWLayer a3("linear", 2.0F, 3.0F);
PoolingLayer a4(poolshape, "average");
FCLayer a6;
OutputLayer a5;
graph.setInput(a1, input);
graph.makeConnection(a1, a2);
graph.makeConnection(a2, a3);
graph.makeConnection(a3, a4);
graph.makeConnection(a4, a5);
graph.makeConnection(a5, a6);
graph.setOutput(a5, output);
graph.inference();
std::vector<float> tmp = *output.as<float>();
std::vector<float> tmp_output = softmax<float>(*output.as<float>());
for (float i : tmp) {
std::cout << i << " ";
}
}
2 changes: 2 additions & 0 deletions app/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
add_subdirectory(ReaderImage)

add_subdirectory(Accuracy)
add_subdirectory(AlexNet)

0 comments on commit b087381

Please sign in to comment.