diff --git a/examples/alexnet/CMakeLists.txt b/examples/alexnet/CMakeLists.txt new file mode 100644 index 0000000..e919122 --- /dev/null +++ b/examples/alexnet/CMakeLists.txt @@ -0,0 +1,26 @@ +cmake_minimum_required(VERSION 3.17) + +project(AlexNet + LANGUAGES CXX CUDA +) + +add_executable(alexnet main.cpp) + +find_library(CUDANet_LIBRARY NAMES CUDANet HINTS ${CMAKE_CURRENT_SOURCE_DIR}/../../build) +find_package(OpenCV REQUIRED COMPONENTS core imgcodecs imgproc) +find_package(CUDAToolkit REQUIRED) + +set (CUDANet_INCLUDE_DIRS + ${CMAKE_CURRENT_SOURCE_DIR}/../../include + ${CMAKE_CURRENT_SOURCE_DIR}/../../include/utils + ${CMAKE_CURRENT_SOURCE_DIR}/../../include/kernels + ${CMAKE_CURRENT_SOURCE_DIR}/../../include/layers + ${CMAKE_CURRENT_SOURCE_DIR}/../../include/model +) + +target_include_directories( + alexnet PRIVATE ${OpenCV_INCLUDE_DIRS} ${CUDANet_INCLUDE_DIRS} ${CUDAToolkit_INCLUDE_DIRS}) + +target_link_libraries(alexnet PRIVATE ${CUDANet_LIBRARY} ${OpenCV_LIBS} CUDA::cudart) + +set_property(TARGET alexnet PROPERTY CXX_STANDARD 20) \ No newline at end of file diff --git a/examples/alexnet/main.cpp b/examples/alexnet/main.cpp new file mode 100644 index 0000000..a15cb46 --- /dev/null +++ b/examples/alexnet/main.cpp @@ -0,0 +1,59 @@ +#include +#include +#include +#include + +#include + +std::vector readAndNormalizeImage(const std::string& imagePath, int width, int height) { + // Read the image using OpenCV + cv::Mat image = cv::imread(imagePath, cv::IMREAD_GRAYSCALE); + + // Resize and normalize the image + cv::resize(image, image, cv::Size(width, height)); + image.convertTo(image, CV_32F); + cv::normalize(image, image, 0.0, 1.0, cv::NORM_MINMAX); + + // Convert the 2D image matrix to a 1D array of floats + std::vector imageData; + for (int i = 0; i < image.rows; ++i) { + for (int j = 0; j < image.cols; ++j) { + imageData.push_back(image.at(i, j)); + } + } + + return imageData; +} + +CUDANet::Model* createModel(const int inputSize, const int inputChannels, const int outputSize) { + CUDANet::Model *model = + new CUDANet::Model(inputSize, inputChannels, outputSize); + return model; +} + +int main(int argc, const char* const argv[]) { + + if (argc != 3) { + std::cerr << "Usage: " << argv[0] << " " << std::endl; + return 1; // Return error code indicating incorrect usage + } + + // Path to the image file + std::string modelWeightsPath = argv[1]; + std::string imagePath = argv[2]; + + const int inputSize = 227; + const int inputChannels = 3; + const int outputSize = 1000; + + CUDANet::Model *model = createModel(inputSize, inputChannels, outputSize); + + + // Read and normalize the image + std::vector imageData = readAndNormalizeImage(imagePath, inputSize, inputSize); + + // Print the size of the image data + std::cout << "Size of image data: " << imageData.size() << std::endl; + + return 0; +} \ No newline at end of file