add onnx pack
This commit is contained in:
@@ -65,7 +65,7 @@ _________________________________________________________________
|
|||||||
```
|
```
|
||||||
推理测试
|
推理测试
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
File diff suppressed because one or more lines are too long
@@ -1,29 +1,20 @@
|
|||||||
//#include "tos_k.h"
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
//#include <string.h>
|
|
||||||
//#include <int.h>
|
|
||||||
//#include <math.h>
|
|
||||||
|
|
||||||
//#include "mnist.h"
|
|
||||||
#include "mnist_int.h"
|
#include "mnist_int.h"
|
||||||
#include "onnx.h"
|
#include "onnx.h"
|
||||||
|
|
||||||
static const char codeLib[] = "@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\\|()1{}[]?-_+~<>i!lI;:,\"^`'. ";
|
static const char codeLib[] = "@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\\|()1{}[]?-_+~<>i!lI;:,\"^`'. ";
|
||||||
int data[5]={1.5 , 2.5 , 3.5 , 4.5 , 5.5};
|
int data[5]={1.5 , 2.5 , 3.5 , 4.5 , 5.5};
|
||||||
//static const int img[2][784] = {IMG0, IMG1};
|
|
||||||
static const int img[2][784] = {IMG0, IMG1};
|
static const int img[2][784] = {IMG0, IMG1};
|
||||||
//static const int imgint[2][784] = {IMG0_INT, IMG1_INT};
|
|
||||||
|
|
||||||
static const int img1[784] = {1,2,3,4,5};
|
static const int img1[784] = {1,2,3,4,5};
|
||||||
|
|
||||||
int hello()
|
int hello()
|
||||||
{
|
{
|
||||||
printf("hello\r\n");
|
printf("hello pnnx\r\n");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
//void print_img(void)
|
|
||||||
void print_img(void * buf)
|
void print_img(void * buf)
|
||||||
{
|
{
|
||||||
int index = 0;
|
int index = 0;
|
||||||
@@ -39,27 +30,18 @@ void print_img(void * buf)
|
|||||||
{
|
{
|
||||||
|
|
||||||
index = 0;
|
index = 0;
|
||||||
//if(img[0][y*28+x] > 600)
|
|
||||||
if(((int*)buf)[y*28+x] > 600)
|
if(((int*)buf)[y*28+x] > 600)
|
||||||
//if(data[0] > 0.6)
|
|
||||||
{
|
{
|
||||||
index =69;
|
index =69;
|
||||||
//ch = ' ';
|
|
||||||
}
|
}
|
||||||
if(index < 0)
|
if(index < 0)
|
||||||
{
|
{
|
||||||
index = 0;
|
index = 0;
|
||||||
//ch = ' ';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
printf("%c",codeLib[index]);
|
printf("%c",codeLib[index]);
|
||||||
printf("%c",codeLib[index]);
|
printf("%c",codeLib[index]);
|
||||||
|
|
||||||
|
|
||||||
//printf("%c",ch);
|
|
||||||
//printf("%c",ch);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
printf("\r\n");
|
printf("\r\n");
|
||||||
}
|
}
|
||||||
@@ -69,14 +51,6 @@ int mnist()
|
|||||||
{
|
{
|
||||||
printf("test1\r\n");
|
printf("test1\r\n");
|
||||||
int img_index = 1;
|
int img_index = 1;
|
||||||
/*
|
|
||||||
if(argc == 2)
|
|
||||||
{
|
|
||||||
img_index = atoi(argv[1]);
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
//print_img();
|
|
||||||
print_img(img[img_index]);
|
print_img(img[img_index]);
|
||||||
|
|
||||||
printf("img ok\r\n");
|
printf("img ok\r\n");
|
||||||
@@ -186,14 +160,6 @@ int mnist()
|
|||||||
|
|
||||||
printf("Softmax ok\r\n");
|
printf("Softmax ok\r\n");
|
||||||
|
|
||||||
// 10. Result
|
|
||||||
/*
|
|
||||||
printf("\n\rdense2: \n\r");
|
|
||||||
for(int i = 0; i < 10; i++)
|
|
||||||
printf("%d ", dense2[i]);
|
|
||||||
*/
|
|
||||||
|
|
||||||
|
|
||||||
int max = 0;
|
int max = 0;
|
||||||
int min = output[0];
|
int min = output[0];
|
||||||
int max_index = 0;
|
int max_index = 0;
|
||||||
@@ -223,4 +189,3 @@ int mnist()
|
|||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
//MSH_CMD_EXPORT(mnist, mnist simple example)
|
|
||||||
|
@@ -1,125 +0,0 @@
|
|||||||
#include <rtthread.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <float.h>
|
|
||||||
#include <math.h>
|
|
||||||
|
|
||||||
#include "mnist.h"
|
|
||||||
#include "onnx.h"
|
|
||||||
|
|
||||||
int mnist(int argc, char const *argv[])
|
|
||||||
{
|
|
||||||
int img_index = 0;
|
|
||||||
if(argc == 2)
|
|
||||||
{
|
|
||||||
img_index = atoi(argv[1]);
|
|
||||||
}
|
|
||||||
print_img(img[img_index]);
|
|
||||||
|
|
||||||
// 1. Conv2D
|
|
||||||
int64_t shapeW3[] = {2, 1, 3, 3};
|
|
||||||
int64_t dimW3 = 4;
|
|
||||||
int64_t permW3_t[] = { 0, 2, 3, 1};
|
|
||||||
float* W3_t = transpose(W3, shapeW3, dimW3, permW3_t);
|
|
||||||
|
|
||||||
float* conv1 = (float*) malloc(sizeof(float)*28*28*2);
|
|
||||||
memset(conv1, 0, sizeof(sizeof(float)*28*28*2));
|
|
||||||
conv2D(img[img_index], 28, 28, 1, W3, 2, 3, 3, 1, 1, 1, 1, B3, conv1, 28, 28);
|
|
||||||
|
|
||||||
free(W3_t);
|
|
||||||
|
|
||||||
// 2. Relu
|
|
||||||
float* relu1 = (float*) malloc(sizeof(float)*28*28*2);
|
|
||||||
relu(conv1, 28*28*2, relu1);
|
|
||||||
|
|
||||||
free(conv1);
|
|
||||||
|
|
||||||
// 3. Maxpool
|
|
||||||
float* maxpool1 = (float*) malloc(sizeof(float)*14*14*2);
|
|
||||||
memset(maxpool1, 0, sizeof(sizeof(float)*14*14*2));
|
|
||||||
maxpool(relu1, 28, 28, 2, 2, 2, 0, 0, 2, 2, 14, 14, maxpool1);
|
|
||||||
|
|
||||||
free(relu1);
|
|
||||||
|
|
||||||
// 4. Conv2D
|
|
||||||
int64_t shapeW2[] = {2, 2, 3, 3};
|
|
||||||
int64_t dimW2 = 4;
|
|
||||||
int64_t perm_t[] = { 0, 2, 3, 1};
|
|
||||||
float* W2_t = transpose(W2, shapeW2, dimW2, perm_t);
|
|
||||||
|
|
||||||
float* conv2 = (float*) malloc(sizeof(float)*14*14*2);
|
|
||||||
memset(conv2, 0, sizeof(sizeof(float)*14*14*2));
|
|
||||||
conv2D(maxpool1, 14, 14, 2, W2_t, 2, 3, 3, 1, 1, 1, 1, B2, conv2, 14, 14);
|
|
||||||
|
|
||||||
free(W2_t);
|
|
||||||
free(maxpool1);
|
|
||||||
|
|
||||||
// 5. Relu
|
|
||||||
float* relu2 = (float*) malloc(sizeof(float)*14*14*2);
|
|
||||||
relu(conv2, 14*14*2, relu2);
|
|
||||||
|
|
||||||
free(conv2);
|
|
||||||
|
|
||||||
// 6. Maxpool
|
|
||||||
float* maxpool2 = (float*) malloc(sizeof(float)*7*7*2);
|
|
||||||
memset(maxpool2, 0, sizeof(sizeof(float)*7*7*2));
|
|
||||||
maxpool(relu2, 14, 14, 2, 2, 2, 0, 0, 2, 2, 7, 7, maxpool2);
|
|
||||||
|
|
||||||
free(relu2);
|
|
||||||
|
|
||||||
// Flatten NOT REQUIRED
|
|
||||||
|
|
||||||
// 7. Dense
|
|
||||||
int64_t shapeW1[] = {98, 4};
|
|
||||||
int64_t dimW1 = 2;
|
|
||||||
int64_t permW1_t[] = { 1, 0};
|
|
||||||
float* W1_t = transpose(W1, shapeW1, dimW1, permW1_t);
|
|
||||||
|
|
||||||
float* dense1 = (float*) malloc(sizeof(float)*4);
|
|
||||||
memset(dense1, 0, sizeof(sizeof(float)*4));
|
|
||||||
dense(maxpool2, W1_t, 98, 4, B1, dense1);
|
|
||||||
|
|
||||||
free(W1_t);
|
|
||||||
free(maxpool2);
|
|
||||||
|
|
||||||
// 8. Dense
|
|
||||||
int64_t shapeW[] = {4, 10};
|
|
||||||
int64_t dimW = 2;
|
|
||||||
int64_t permW_t[] = { 1, 0};
|
|
||||||
float* W_t = transpose(W, shapeW, dimW, permW_t);
|
|
||||||
|
|
||||||
float* dense2 = (float*) malloc(sizeof(float)*10);
|
|
||||||
memset(dense2, 0, sizeof(sizeof(float)*10));
|
|
||||||
dense(dense1, W_t, 4, 10, B, dense2);
|
|
||||||
|
|
||||||
free(W_t);
|
|
||||||
free(dense1);
|
|
||||||
|
|
||||||
// 9. Softmax
|
|
||||||
float* output = (float*) malloc(sizeof(float)*10);
|
|
||||||
memset(output, 0, sizeof(sizeof(float)*10));
|
|
||||||
softmax(dense2, 10, output);
|
|
||||||
|
|
||||||
// 10. Result
|
|
||||||
float max = 0;
|
|
||||||
int max_index = 0;
|
|
||||||
printf("\nPredictions: \n");
|
|
||||||
for(int i = 0; i < 10; i++)
|
|
||||||
{
|
|
||||||
printf("%f ", output[i]);
|
|
||||||
if(output[i] > max)
|
|
||||||
{
|
|
||||||
max = output[i];
|
|
||||||
max_index = i;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
printf("\n");
|
|
||||||
printf("\nThe number is %d\n", max_index);
|
|
||||||
|
|
||||||
free(dense2);
|
|
||||||
free(output);
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
MSH_CMD_EXPORT(mnist, mnist simple example)
|
|
File diff suppressed because one or more lines are too long
BIN
components/ai/onnx/pic/mnist_test.png
Normal file
BIN
components/ai/onnx/pic/mnist_test.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 39 KiB |
Reference in New Issue
Block a user