mirror of
https://github.com/lordmathis/CUDANet.git
synced 2025-11-06 01:34:22 +00:00
Add Alexnet README
This commit is contained in:
28
examples/alexnet/README.md
Normal file
28
examples/alexnet/README.md
Normal file
@@ -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
|
||||||
|
```
|
||||||
@@ -1,14 +1,14 @@
|
|||||||
|
#include <conv2d.cuh>
|
||||||
|
#include <dense.cuh>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <max_pooling.cuh>
|
||||||
|
#include <model.hpp>
|
||||||
|
#include <opencv2/opencv.hpp>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <opencv2/opencv.hpp>
|
|
||||||
|
|
||||||
#include <model.hpp>
|
std::vector<float>
|
||||||
#include <conv2d.cuh>
|
readAndNormalizeImage(const std::string &imagePath, int width, int height) {
|
||||||
#include <max_pooling.cuh>
|
|
||||||
#include <dense.cuh>
|
|
||||||
|
|
||||||
std::vector<float> readAndNormalizeImage(const std::string& imagePath, int width, int height) {
|
|
||||||
// Read the image using OpenCV
|
// Read the image using OpenCV
|
||||||
cv::Mat image = cv::imread(imagePath, cv::IMREAD_COLOR);
|
cv::Mat image = cv::imread(imagePath, cv::IMREAD_COLOR);
|
||||||
|
|
||||||
@@ -35,13 +35,18 @@ std::vector<float> readAndNormalizeImage(const std::string& imagePath, int width
|
|||||||
return imageData;
|
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 =
|
CUDANet::Model *model =
|
||||||
new CUDANet::Model(inputSize, inputChannels, outputSize);
|
new CUDANet::Model(inputSize, inputChannels, outputSize);
|
||||||
|
|
||||||
// Block 1
|
// Block 1
|
||||||
CUDANet::Layers::Conv2d *conv1 = new CUDANet::Layers::Conv2d(
|
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(
|
CUDANet::Layers::MaxPooling2D *pool1 = new CUDANet::Layers::MaxPooling2D(
|
||||||
@@ -101,9 +106,9 @@ CUDANet::Model* createModel(const int inputSize, const int inputChannels, const
|
|||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, const char *const argv[]) {
|
int main(int argc, const char *const argv[]) {
|
||||||
|
|
||||||
if (argc != 3) {
|
if (argc != 3) {
|
||||||
std::cerr << "Usage: " << argv[0] << "<model_weights_path> <image_path>" << std::endl;
|
std::cerr << "Usage: " << argv[0] << "<model_weights_path> <image_path>"
|
||||||
|
<< std::endl;
|
||||||
return 1; // Return error code indicating incorrect usage
|
return 1; // Return error code indicating incorrect usage
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -122,7 +127,8 @@ int main(int argc, const char* const argv[]) {
|
|||||||
model->loadWeights(modelWeightsPath);
|
model->loadWeights(modelWeightsPath);
|
||||||
|
|
||||||
// Read and normalize the image
|
// Read and normalize the image
|
||||||
std::vector<float> imageData = readAndNormalizeImage(imagePath, inputSize, inputSize);
|
std::vector<float> imageData =
|
||||||
|
readAndNormalizeImage(imagePath, inputSize, inputSize);
|
||||||
|
|
||||||
// Print the size of the image data
|
// Print the size of the image data
|
||||||
float *output = model->predict(imageData.data());
|
float *output = model->predict(imageData.data());
|
||||||
|
|||||||
Reference in New Issue
Block a user