Giter Site home page Giter Site logo

keras2circom's Introduction

keras2circom

keras2circom is a python tool that transpiles a tf.keras model into a circom circuit.

Installation

First, clone the repository:

git clone https://github.com/socathie/keras2circom.git

Then, install the dependencies. You can use pip:

pip install -r requirements.txt

If you use conda, you can also create a new environment with the following command:

conda env create -f environment.yml

You will also need to install circom and snarkjs. You can run the following commands to install them:

bash setup-circom.sh

Last but not least, run

npm install

Usage

To use the package, you can run the following command:

python main.py <model_path> [-o <output_dir>] [--raw]

For example, to transpile the model in models/model.h5 into a circom circuit, you can run:

python main.py models/model.h5

The output will be in the output directory.

If you want to transpile the model into a circom circuit with "raw" output, i.e. no ArgMax at the end, you can run:

python main.py models/model.h5 --raw

Testing

To test the package, you can run the following command:

npm test

keras2circom's People

Contributors

socathie avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

keras2circom's Issues

witeness of the output circuit.circom file generation failed

Hi, i am a new learner in zkml and i am using this keras2circom with dokcer python3.9.16, and i got this problem,
when i tried to generate witness with the output circuit.circom and circuit.json, i had this error:

~/Desktop/PrivacyComputing/proof-of-sql/practices/keras2circom/output]$ node circuit_js/generate_witness.js circuit_js/circuit.wasm circuit.json circuit.wtns /Desktop/PrivacyComputing/proof-of-sql/practices/keras2circom/output/circuit_js/witness_calculator.js:167 throw new Error(Not all inputs have been set. Only ${input_counter} out of ${this.instance.exports.getInputSize()}); ^ Error: Not all inputs have been set. Only 674 out of 1458 at WitnessCalculator._doCalculateWitness (/Desktop/PrivacyComputing/proof-of-sql/practices/keras2circom/output/circuit_js/witness_calculator.js:167:12) at WitnessCalculator.calculateWTNSBin (/Desktop/PrivacyComputing/proof-of-sql/practices/keras2circom/output/circuit_js/witness_calculator.js:212:20) at /Desktop/PrivacyComputing/proof-of-sql/practices/keras2circom/output/circuit_js/generate_witness.js:15:38

and here are the logs when i generated the circom file:

root@d4a5c9f0cdc9:/usr/wzj# python main.py models/alt_model.h5 2023-04-11 02:38:43.782470: I tensorflow/core/platform/cpu_feature_guard.cc:193] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: AVX2 AVX512F AVX512_VNNI FMA To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags. 2023-04-11 02:38:44.014700: I tensorflow/core/util/util.cc:169] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable TF_ENABLE_ONEDNN_OPTS=0. 2023-04-11 02:38:44.023624: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libcudart.so.11.0'; dlerror: libcudart.so.11.0: cannot open shared object file: No such file or directory 2023-04-11 02:38:44.023696: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine. 2023-04-11 02:38:44.064497: E tensorflow/stream_executor/cuda/cuda_blas.cc:2981] Unable to register cuBLAS factory: Attempting to register factory for plugin cuBLAS when one has already been registered 2023-04-11 02:38:45.111990: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer.so.7'; dlerror: libnvinfer.so.7: cannot open shared object file: No such file or directory 2023-04-11 02:38:45.112222: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer_plugin.so.7'; dlerror: libnvinfer_plugin.so.7: cannot open shared object file: No such file or directory 2023-04-11 02:38:45.112262: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Cannot dlopen some TensorRT libraries. If you would like to use Nvidia GPU with TensorRT, please make sure the missing libraries mentioned above are installed properly. 2023-04-11 02:38:46.222013: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libcuda.so.1'; dlerror: libcuda.so.1: cannot open shared object file: No such file or directory 2023-04-11 02:38:46.222106: W tensorflow/stream_executor/cuda/cuda_driver.cc:263] failed call to cuInit: UNKNOWN ERROR (303) 2023-04-11 02:38:46.222317: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:156] kernel driver does not appear to be running on this host (d4a5c9f0cdc9): /proc/driver/nvidia/version does not exist 2023-04-11 02:38:46.222715: I tensorflow/core/platform/cpu_feature_guard.cc:193] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: AVX2 AVX512F AVX512_VNNI FMA To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.

am i using keras2circom wrong? looking forward to your anser, thank you.

python main.py failed

Hello sir, sorry to bother, but i got an error when i run python main.py keras2circom/test.h5 with test.h5 generated by my mnist.py which is copied from your best_practice.ipynb.

Here is the mnist.py file:

import json
import os
from tensorflow.keras import Model
from tensorflow.keras.layers import (
    Input,
    AveragePooling2D,
    BatchNormalization,
    Conv2D,
    Dense,
    GlobalAveragePooling2D,
    Lambda,  # only for polynomial activation in the form of `Lambda(lambda x: x**2+x)`
    Softmax,
)
from tensorflow.keras.utils import to_categorical
current_dir = os.getcwd()
from tensorflow.keras.datasets import mnist
(X_train, y_train), (X_test, y_test) = mnist.load_data(path=current_dir + '/mnist.npz')
y_train = to_categorical(y_train)
y_test = to_categorical(y_test)
X_train = X_train.reshape(X_train.shape[0], 28, 28, 1)
X_test = X_test.reshape(X_test.shape[0], 28, 28, 1)
inputs = Input(shape=(28,28,1))
out = Conv2D(4, 3, use_bias=False)(inputs)
out = BatchNormalization()(out)
out = Lambda(lambda x: x**2+x)(out) # best practice: use polynomial activation instead of ReLU
out = AveragePooling2D()(out) # best practice: use AveragePooling2D instead of MaxPooling2D
out = Conv2D(16, 3, use_bias=False)(out)
out = BatchNormalization()(out)
out = Lambda(lambda x: x**2+x)(out)
out = AveragePooling2D()(out)
out = GlobalAveragePooling2D()(out) # best practice: use GlobalAveragePooling2D instead of Flatten
out = Dense(10, activation=None)(out)
out = Softmax()(out)
model = Model(inputs, out)
model.summary()
model.compile(
    loss='categorical_crossentropy',
    optimizer='adam',
    metrics=['acc']
)
model.fit(X_train, y_train, epochs=100, batch_size=128, validation_data=(X_test, y_test))
model.save('test.h5')
model2 = Model(model.input, model.layers[-2].output)
model2.layers[-1]
X = X_test[[0]]
y = model2.predict(X)
y
for layer in model.layers:
    print(layer.__class__.__name__, layer.get_config())
    try:
        print(layer.get_config()['function'])
    except:
        pass
    print(layer.get_input_shape_at(0), layer.get_output_shape_at(0))
    try:
        print(layer.get_weights()[0].shape)
        print(layer.get_weights()[1].shape)
    except:
        pass
with open("test.json", "w") as f:
    json.dump({'X': X.flatten().tolist(), 'y': y.flatten().tolist()}, f)

and here is the error:

root@984cb5a21ce9:/usr/wzj# python main.py keras2circom/test.h5                                                                                             
2023-04-12 02:16:30.752955: I tensorflow/core/platform/cpu_feature_guard.cc:193] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  AVX2 AVX512F AVX512_VNNI FMA                                            
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.                                                                 
2023-04-12 02:16:31.020164: I tensorflow/core/util/util.cc:169] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable `TF_ENABLE_ONEDNN_OPTS=0`.                    
2023-04-12 02:16:31.045363: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libcudart.so.11.0'; dlerror: libcudart.so.11.0: cannot open shared object file: No such file or directory                                                                                    
2023-04-12 02:16:31.045446: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
2023-04-12 02:16:31.094098: E tensorflow/stream_executor/cuda/cuda_blas.cc:2981] Unable to register cuBLAS factory: Attempting to register factory for plugin cuBLAS when one has already been registered
2023-04-12 02:16:32.528631: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer.so.7'; dlerror: libnvinfer.so.7: cannot open shared object file: No such file or directory
2023-04-12 02:16:32.528909: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer_plugin.so.7'; dlerror: libnvinfer_plugin.so.7: cannot open shared object file: No such file or directory
2023-04-12 02:16:32.528941: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Cannot dlopen some TensorRT libraries. If you would like to use Nvidia GPU with TensorRT, please make sure the missing libraries mentioned above are installed properly.
2023-04-12 02:16:33.942876: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libcuda.so.1'; dlerror: libcuda.so.1: cannot open shared object file: No such file or directory
2023-04-12 02:16:33.942948: W tensorflow/stream_executor/cuda/cuda_driver.cc:263] failed call to cuInit: UNKNOWN ERROR (303)
2023-04-12 02:16:33.943119: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:156] kernel driver does not appear to be running on this host (984cb5a21ce9): /proc/driver/nvidia/version does not exist
2023-04-12 02:16:33.943566: I tensorflow/core/platform/cpu_feature_guard.cc:193] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  AVX2 AVX512F AVX512_VNNI FMA
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
Traceback (most recent call last):
  File "/usr/wzj/main.py", line 25, in <module>
    main()
  File "/usr/wzj/main.py", line 21, in main
    transpiler.transpile(args['<model.h5>'], args['--output'], args['--raw'])
  File "/usr/wzj/keras2circom/transpiler.py", line 16, in transpile
    circuit.add_components(transpile_layer(layer))
  File "/usr/wzj/keras2circom/transpiler.py", line 80, in transpile_layer
    raise ValueError('Only polynomial activation functions are supported')
ValueError: Only polynomial activation functions are supported

and i printed the value of s.ratio() in transpiler.py:

0.6615384615384615

is this some kind of accuracy problem?

Not all inputs have been set

Hello. I attempted to run the example you provided and got a circuit.circom and a circuit.json file. But I found the number of input signals in circuit.json is different from the number needed by circuits.circom. So i got the following error when generating witness.

Not all inputs have been set. Only 1074 out of 17155
circuit: main.cpp:278: int main(int, char**): Assertion `false' failed.
Aborted (core dumped)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.