diff --git a/examples/alexnet/README.md b/examples/alexnet/README.md new file mode 100644 index 0000000..2f50737 --- /dev/null +++ b/examples/alexnet/README.md @@ -0,0 +1,28 @@ +# AlexNet + +AlexNet Inference on CUDANet + +## Usage + +1. Export pytorch AlexNet weight pretrained on ImageNet (requires pytorch and torchvision): + +```sh +python alexnet.py +``` + +2. Follow the instructions from repository root to build CUDANet library. + +3. Build AlexNet + +```sh +mkdir build +cd build +cmake -S .. +make +``` + +4. Run AlexNet inference + +```sh +alexnet ../alexnet_weights.bin ../image.jpg +``` \ No newline at end of file diff --git a/examples/alexnet/main.cpp b/examples/alexnet/main.cpp index baf8ada..6dd3915 100644 --- a/examples/alexnet/main.cpp +++ b/examples/alexnet/main.cpp @@ -1,14 +1,14 @@ +#include +#include #include +#include +#include +#include #include #include -#include -#include -#include -#include -#include - -std::vector readAndNormalizeImage(const std::string& imagePath, int width, int height) { +std::vector +readAndNormalizeImage(const std::string &imagePath, int width, int height) { // Read the image using OpenCV cv::Mat image = cv::imread(imagePath, cv::IMREAD_COLOR); @@ -35,15 +35,20 @@ std::vector readAndNormalizeImage(const std::string& imagePath, int width return imageData; } -CUDANet::Model* createModel(const int inputSize, const int inputChannels, const int outputSize) { +CUDANet::Model *createModel( + const int inputSize, + const int inputChannels, + const int outputSize +) { CUDANet::Model *model = new CUDANet::Model(inputSize, inputChannels, outputSize); // Block 1 CUDANet::Layers::Conv2d *conv1 = new CUDANet::Layers::Conv2d( - inputSize, inputChannels, 11, 4, 64, 2, CUDANet::Layers::ActivationType::RELU + inputSize, inputChannels, 11, 4, 64, 2, + CUDANet::Layers::ActivationType::RELU ); - model->addLayer("features.0", conv1); // Match pytorch naming + model->addLayer("features.0", conv1); // Match pytorch naming CUDANet::Layers::MaxPooling2D *pool1 = new CUDANet::Layers::MaxPooling2D( 56, 64, 3, 2, CUDANet::Layers::ActivationType::NONE ); @@ -100,20 +105,20 @@ CUDANet::Model* createModel(const int inputSize, const int inputChannels, const return model; } -int main(int argc, const char* const argv[]) { - +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 + 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]; + std::string imagePath = argv[2]; - const int inputSize = 227; + const int inputSize = 227; const int inputChannels = 3; - const int outputSize = 1000; + const int outputSize = 1000; CUDANet::Model *model = createModel(inputSize, inputChannels, outputSize); @@ -122,10 +127,11 @@ int main(int argc, const char* const argv[]) { model->loadWeights(modelWeightsPath); // Read and normalize the image - std::vector imageData = readAndNormalizeImage(imagePath, inputSize, inputSize); + std::vector imageData = + readAndNormalizeImage(imagePath, inputSize, inputSize); // Print the size of the image data - float* output = model->predict(imageData.data()); + float *output = model->predict(imageData.data()); // Get max index int maxIndex = 0;