2023-08-31 01:41:08 +08:00
|
|
|
from pathlib import Path
|
2023-11-14 12:18:40 +08:00
|
|
|
from extra.models.efficientnet import EfficientNet
|
2023-02-21 10:18:18 +08:00
|
|
|
from tinygrad.tensor import Tensor
|
2023-08-22 22:36:24 +08:00
|
|
|
from tinygrad.nn.state import safe_save
|
2023-08-02 00:35:48 +08:00
|
|
|
from extra.export_model import export_model
|
2023-11-24 06:16:17 +08:00
|
|
|
from tinygrad.helpers import getenv, fetch
|
2023-08-31 01:41:08 +08:00
|
|
|
import ast
|
2023-02-23 12:50:53 +08:00
|
|
|
|
2023-07-13 03:52:06 +08:00
|
|
|
if __name__ == "__main__":
|
|
|
|
model = EfficientNet(0)
|
|
|
|
model.load_from_pretrained()
|
2024-01-09 01:29:13 +08:00
|
|
|
mode = "clang" if getenv("CLANG", "") != "" else "webgpu" if getenv("WEBGPU", "") != "" else "webgl" if getenv("WEBGL", "") != "" else ""
|
2023-10-31 09:42:26 +08:00
|
|
|
prg, inp_sizes, out_sizes, state = export_model(model, mode, Tensor.randn(1,3,224,224))
|
2023-08-31 01:41:08 +08:00
|
|
|
dirname = Path(__file__).parent
|
2023-08-02 00:35:48 +08:00
|
|
|
if getenv("CLANG", "") == "":
|
2023-08-31 01:41:08 +08:00
|
|
|
safe_save(state, (dirname / "net.safetensors").as_posix())
|
2024-01-09 01:29:13 +08:00
|
|
|
ext = "js" if getenv("WEBGPU", "") != "" or getenv("WEBGL", "") != "" else "json"
|
2023-08-31 01:41:08 +08:00
|
|
|
with open(dirname / f"net.{ext}", "w") as text_file:
|
2023-08-02 00:35:48 +08:00
|
|
|
text_file.write(prg)
|
|
|
|
else:
|
|
|
|
cprog = [prg]
|
|
|
|
# image library!
|
2023-11-24 06:16:17 +08:00
|
|
|
cprog += ["#define STB_IMAGE_IMPLEMENTATION", fetch("https://raw.githubusercontent.com/nothings/stb/master/stb_image.h").read_text().replace("half", "_half")]
|
2023-08-02 00:35:48 +08:00
|
|
|
|
|
|
|
# imagenet labels, move to datasets?
|
2023-11-24 06:16:17 +08:00
|
|
|
lbls = ast.literal_eval(fetch("https://gist.githubusercontent.com/yrevar/942d3a0ac09ec9e5eb3a/raw/238f720ff059c1f82f368259d1ca4ffa5dd8f9f5/imagenet1000_clsidx_to_labels.txt").read_text())
|
2023-08-02 00:35:48 +08:00
|
|
|
lbls = ['"'+lbls[i]+'"' for i in range(1000)]
|
2023-10-07 19:13:34 +08:00
|
|
|
inputs = "\n".join([f"float {inp}[{inp_size}];" for inp,inp_size in inp_sizes.items()])
|
2023-10-31 09:42:26 +08:00
|
|
|
outputs = "\n".join([f"float {out}[{out_size}];" for out,out_size in out_sizes.items()])
|
2023-08-02 00:35:48 +08:00
|
|
|
cprog.append(f"char *lbls[] = {{{','.join(lbls)}}};")
|
2023-10-07 19:13:34 +08:00
|
|
|
cprog.append(inputs)
|
2023-10-31 09:42:26 +08:00
|
|
|
cprog.append(outputs)
|
2023-08-02 00:35:48 +08:00
|
|
|
|
|
|
|
# buffers (empty + weights)
|
|
|
|
cprog.append("""
|
|
|
|
int main(int argc, char* argv[]) {
|
|
|
|
int DEBUG = getenv("DEBUG") != NULL ? atoi(getenv("DEBUG")) : 0;
|
|
|
|
int X=0, Y=0, chan=0;
|
|
|
|
stbi_uc *image = (argc > 1) ? stbi_load(argv[1], &X, &Y, &chan, 3) : stbi_load_from_file(stdin, &X, &Y, &chan, 3);
|
|
|
|
assert(image != NULL);
|
|
|
|
if (DEBUG) printf("loaded image %dx%d channels %d\\n", X, Y, chan);
|
|
|
|
assert(chan == 3);
|
|
|
|
// resize to input[1,3,224,224] and rescale
|
|
|
|
for (int y = 0; y < 224; y++) {
|
|
|
|
for (int x = 0; x < 224; x++) {
|
|
|
|
// get sample position
|
|
|
|
int tx = (x/224.)*X;
|
|
|
|
int ty = (y/224.)*Y;
|
|
|
|
for (int c = 0; c < 3; c++) {
|
2023-10-07 19:13:34 +08:00
|
|
|
input0[c*224*224 + y*224 + x] = (image[ty*X*chan + tx*chan + c] / 255.0 - 0.45) / 0.225;
|
2023-08-02 00:35:48 +08:00
|
|
|
}
|
2023-02-21 10:18:18 +08:00
|
|
|
}
|
|
|
|
}
|
2023-10-31 09:42:26 +08:00
|
|
|
net(input0, output0);
|
2023-08-02 00:35:48 +08:00
|
|
|
float best = -INFINITY;
|
|
|
|
int best_idx = -1;
|
|
|
|
for (int i = 0; i < 1000; i++) {
|
2023-10-31 09:42:26 +08:00
|
|
|
if (output0[i] > best) {
|
|
|
|
best = output0[i];
|
2023-08-02 00:35:48 +08:00
|
|
|
best_idx = i;
|
|
|
|
}
|
2023-02-21 10:18:18 +08:00
|
|
|
}
|
2023-08-02 00:35:48 +08:00
|
|
|
if (DEBUG) printf("category : %d (%s) with %f\\n", best_idx, lbls[best_idx], best);
|
|
|
|
else printf("%s\\n", lbls[best_idx]);
|
|
|
|
}""")
|
2023-02-21 10:18:18 +08:00
|
|
|
|
2023-08-02 00:35:48 +08:00
|
|
|
# CLANG=1 python3 examples/compile_efficientnet.py | clang -O2 -lm -x c - -o recognize && DEBUG=1 time ./recognize docs/showcase/stable_diffusion_by_tinygrad.jpg
|
|
|
|
# category : 281 (tabby, tabby cat) with 9.452788
|
|
|
|
print('\n'.join(cprog))
|