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

Verified MNIST performance #3

Open
wants to merge 1 commit 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: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
nu-lagoon*
.DS_STORE
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ Run a pretrained MNIST model from your urbit ship using ONNX weights!
![alt-text](https://github.com/zorp-corp/mnist-hoon/blob/master/pred.jpeg)

## Instructions
NOTE: While writing this library, we discovered a bug in `+de:base64:mimes:html`, to run this demo you must apply the fix in this PR [here](https://github.com/urbit/urbit/pull/6826).

1. Copy the contents of `desk` into a desk named `%mnist` on a fakezod.
2. This repo comes preloaded with 9 MNIST images taken from the test set. The filename of each image serves as its label.
To run inference on the first image: `0.mnist` do:

```hoon
+mnist!run-onnx /=mnist=/data/net-quant/json /=mnist=/data/0/mnist
+mnist!run-onnx /=mnist=/data/net-float/json /=mnist=/data/0/mnist
```

Where `/=mnist=/data/net-quant/json` is the path of the ONNX weight file. The generator should output `[%pred 0]`, making its prediction for the first image the number `0`.
Expand Down
190 changes: 190 additions & 0 deletions desk/data/float-net.json

Large diffs are not rendered by default.

83 changes: 0 additions & 83 deletions desk/gen/mnist-q.hoon

This file was deleted.

14 changes: 7 additions & 7 deletions desk/gen/mnist.hoon
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,25 @@
=/ w1
%- spac:la
:- [~[500 784] 5 %float]
.^(@ux %cx /(scot %p ship)/tensor/(scot %da now)/data/fc1-weight/mnist)
.^(@ux %cx /(scot %p ship)/(scot %tas desk)/(scot %da now)/data/fc1-weight/mnist)
=/ w2
%- spac:la
:- [~[10 500] 5 %float]
.^(@ux %cx /(scot %p ship)/tensor/(scot %da now)/data/fc2-weight/mnist)
.^(@ux %cx /(scot %p ship)/(scot %tas desk)/(scot %da now)/data/fc2-weight/mnist)
=/ b1
%- spac:la
:- [~[500 1] 5 %float]
.^(@ux %cx /(scot %p ship)/tensor/(scot %da now)/data/fc1-bias/mnist)
.^(@ux %cx /(scot %p ship)/(scot %tas desk)/(scot %da now)/data/fc1-bias/mnist)
=/ b2
%- spac:la
:- [~[10 1] 5 %float]
.^(@ux %cx /(scot %p ship)/tensor/(scot %da now)/data/fc2-bias/mnist)
.^(@ux %cx /(scot %p ship)/(scot %tas desk)/(scot %da now)/data/fc2-bias/mnist)
~[(linear:nn w1 b1) relu:nn (linear:nn w2 b2)]
::
++ load-image
|= i=@
^- ray:la
=/ dat .^(@ %cx /(scot %p ship)/tensor/(scot %da now)/data/(scot %ud i)/mnist)
=/ dat .^(@ %cx /(scot %p ship)/(scot %tas desk)/(scot %da now)/data/(scot %ud i)/mnist)
%- spac:la
:- [~[784 1] 5 %float]
%+ rep 5
Expand All @@ -49,6 +49,6 @@
(sun:rs e)
++ load-labels
^- (list @)
=/ dat .^(@ %cx /(scot %p ship)/tensor/(scot %da now)/data/labels/mnist)
=/ dat .^(@ %cx /(scot %p ship)/(scot %tas desk)/(scot %da now)/data/mnist)
(rip 3 dat)
--
--
23 changes: 8 additions & 15 deletions desk/gen/run-onnx.hoon
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
/+ ox=onnx
/+ *nu-lagoon
/- *lagoon
/+ ox=onnx,
*lagoon
:- %say
|= [[now=@da eny=@uv [=ship =desk =case]] [onnx-path=path img-path=path ~] ~]
:- %noun
|^
~> %bout
=/ x (malt `(list [@t tensor:ox])`~[[%input1 [%array (load-image img-path)]]])
=/ onnx-file=json .^(json %cx onnx-path)
=/ output-map=(map @t tensor:ox) ((run-onnx:ox (preprocess-onnx:ox onnx-file)) x)
Expand All @@ -14,17 +16,8 @@
::
++ load-image
|= =path
^- ray:la
=/ img .^(@ %cx path)
=/ missing (sub 784 (met 5 img))
^- ray
%- spac:la
:- [~[784 1] 5 %float ~]
::
:: lsh to recover zeros lost by flop-rep
%+ lsh
[5 missing]
%+ rep
5
:: flop since lagoon is big endian
(flop (rip 5 img))
--
:- [~[784 1] 5 %real ~]
.^(@ %cx path)
--
Loading