mirror of
https://github.com/lordmathis/CUDANet.git
synced 2025-11-05 17:34:21 +00:00
Start implementing AlexNet
This commit is contained in:
26
examples/alexnet/CMakeLists.txt
Normal file
26
examples/alexnet/CMakeLists.txt
Normal file
@@ -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)
|
||||
59
examples/alexnet/main.cpp
Normal file
59
examples/alexnet/main.cpp
Normal file
@@ -0,0 +1,59 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <opencv2/opencv.hpp>
|
||||
|
||||
#include <model.hpp>
|
||||
|
||||
std::vector<float> 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<float> imageData;
|
||||
for (int i = 0; i < image.rows; ++i) {
|
||||
for (int j = 0; j < image.cols; ++j) {
|
||||
imageData.push_back(image.at<float>(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] << "<model_weights_path> <image_path>" << 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<float> imageData = readAndNormalizeImage(imagePath, inputSize, inputSize);
|
||||
|
||||
// Print the size of the image data
|
||||
std::cout << "Size of image data: " << imageData.size() << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user