Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sciwork demo #6

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
uTensor
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"python.pythonPath": ".venv/bin/python"
}
2 changes: 1 addition & 1 deletion Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ utensor-cgen = "*"
jupyter = "*"

[requires]
python_version = "3.7"
python_version = "3.6"
727 changes: 446 additions & 281 deletions Pipfile.lock

Large diffs are not rendered by default.

13 changes: 10 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,18 @@ tflm_keras_export(
$ mbed deploy
$ mbed compile -m auto -t GCC_ARM -f --sterm
```
Expected output:

```
Expected output:
```bash
Simple MNIST end-to-end uTensor cli example (device)
Predicted label: 7
pred label: 8, expecting: 8
pred label: 3, expecting: 3
pred label: 5, expecting: 5
pred label: 5, expecting: 5
pred label: 1, expecting: 1
pred label: 9, expecting: 9
pred label: 3, expecting: 3
pred label: 1, expecting: 1
```

## Join Us
Expand Down
48 changes: 48 additions & 0 deletions gen_inputs_header.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import tensorflow as tf
import numpy as np
import argparse


def main(num_samples=5, seed=None):
mnist = tf.keras.datasets.mnist
(_, _), (x_test, y_test) = mnist.load_data()
x_test = x_test / 255.0

# Add a channels dimension
x_test = x_test[..., np.newaxis]
total_N = y_test.shape[0]
num_samples = min(num_samples, total_N)
np.random.seed(seed)
idxs = np.random.choice(range(total_N), num_samples, replace=False)
x_selected = x_test[idxs].reshape(num_samples, -1)
y_selected = y_test[idxs]
with open("input_image.h", "w") as fid:
fid.write("// clang-format off\n")
fid.write(
"const float arr_input_image[{}][{}] = {{\n".format(
x_selected.shape[0], x_selected.shape[1]
)
)
for i in range(x_selected.shape[0]):
arr = x_selected[i]
fid.write(" {{ {}".format(", ".join(map(str, arr))))
fid.write("},\n")
fid.write("};\n")
fid.write("const int ref_labels[{}] = {{\n".format(y_selected.shape[0]))
fid.write(" " + ", ".join(map(str, y_selected)) + "\n")
fid.write("};\n\n")


if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"--num-samples",
dest="num_samples",
default=5,
help="the number of inpute samples [default: %(default)s]",
type=int,
metavar="INTEGER",
)
parser.add_argument("--seed", default=None, help="the random seed", type=int)
args = vars(parser.parse_args())
main(**args)
66 changes: 13 additions & 53 deletions input_image.h

Large diffs are not rendered by default.

30 changes: 16 additions & 14 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,26 @@ int argmax(const Tensor &logits) {
return max_index;
}

static My_model model;
static MyModel model;

int main(void) {
int main(int argc, char **argv) {
printf("\n");
printf("Simple MNIST end-to-end uTensor cli example (device)\n");

// create the input/output tensor
Tensor input_image = new RomTensor({1, 28, 28, 1}, flt, arr_input_image);
Tensor logits = new RamTensor({1, 10}, flt);
size_t num_samples = *(&ref_labels + 1) - ref_labels;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A macro define or a const variable is a better idea here.

for (size_t i = 0; i < num_samples; ++i) {
// create the input/output tensor
Tensor input_image = new RomTensor({1, 28, 28, 1}, flt, arr_input_image[i]);
Tensor logits = new RamTensor({1, 10}, flt);

model.set_inputs({{My_model::input_0, input_image}})
.set_outputs({{My_model::output_0, logits}})
.eval();

int max_index = argmax(logits);
input_image.free();
logits.free();

printf("pred label: %d\r\n", max_index);
model.set_inputs({{MyModel::input_0, input_image}})
.set_outputs({{MyModel::output_0, logits}})
.eval();
int max_index = argmax(logits);
input_image.free();
logits.free();

printf("pred label: %d, expecting: %d\r\n", max_index, ref_labels[i]);
}
return 0;
}
15 changes: 10 additions & 5 deletions mnist_conv.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
"execution_count": 1,
"metadata": {
"ExecuteTime": {
"end_time": "2020-06-30T04:08:49.781015Z",
"start_time": "2020-06-30T04:08:23.608609Z"
"end_time": "2020-08-27T12:41:36.312890Z",
"start_time": "2020-08-27T12:41:31.842215Z"
}
},
"outputs": [],
Expand All @@ -33,8 +33,8 @@
"execution_count": 2,
"metadata": {
"ExecuteTime": {
"end_time": "2020-06-30T04:08:49.810814Z",
"start_time": "2020-06-30T04:08:49.783918Z"
"end_time": "2020-08-27T12:41:37.748636Z",
"start_time": "2020-08-27T12:41:37.713586Z"
},
"tags": []
},
Expand Down Expand Up @@ -72,7 +72,12 @@
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"metadata": {
"ExecuteTime": {
"end_time": "2020-08-27T12:41:42.661682Z",
"start_time": "2020-08-27T12:41:42.146260Z"
}
},
"outputs": [],
"source": [
"mnist = tf.keras.datasets.mnist\n",
Expand Down
Loading