socathie / keras2circom

python tool to transpile a tf.keras model into a circom circuit
MIT License
295 stars 52 forks source link

how to obtain input.json #7

Closed aldhosutra closed 3 months ago

aldhosutra commented 3 months ago

Hey, I’m new to zkML. I want to try compiling my Keras model into Circom, and I found this repository. Great work!

I’ve successfully obtained circuit.circom, circuit.json, circuit.wasm, and generate_witness.js. However, I’m now struggling with creating the witness because I don’t understand what input.json should look like.

My model is LeNet-5, with the following configuration:

def build_lenet5_relu(input_shape=(32, 32, 1), num_classes=4):
    model = models.Sequential()

    # Input layer
    model.add(layers.Input(shape=input_shape))

    # First Convolutional Layer
    model.add(layers.Conv2D(6, kernel_size=(5, 5), strides=(1, 1), padding='same', activation='relu'))
    model.add(layers.AveragePooling2D(pool_size=(2, 2), strides=(2, 2)))

    # Second Convolutional Layer
    model.add(layers.Conv2D(16, kernel_size=(5, 5), strides=(1, 1), activation='relu'))
    model.add(layers.AveragePooling2D(pool_size=(2, 2), strides=(2, 2)))

    # Third Convolutional Layer
    model.add(layers.Conv2D(120, kernel_size=(5, 5), strides=(1, 1), activation='relu'))

    # Flattening Layer
    model.add(layers.Flatten())

    # Second Fully Connected Layer
    model.add(layers.Dense(num_classes, activation='softmax'))

    model.output_names=['output']

    return model

I assumed that I should merge your circom.json with "in" as the input, so I did:

image = Image.open(<input-image-path>)
image_array = np.array(image)

x = np.expand_dims(image_array, axis=0)

data_array = x.reshape([32, 32, 1]).tolist()

with open(<path-to-circuit-json>, 'r') as circuit_file:
    input_json = json.load(circuit_file)

input_json["in"] = data_array

with open(<path-to-input-json>, 'w') as input_file:
    json.dump(input_json, input_file, indent=4)

But when I generate the witness using generate_witness.js, I get this error:

Error: Not all inputs have been set. Only 53640 out of 85137

I suspect there might be an issue with the signals in my input.json, probably wrong format?. Do you have any clue?

Thanks for your response.

aldhosutra commented 3 months ago

Hey, I managed to find the solution.

It turns out I need to first create output.json by running circuit.py, then merge circuit.json, output.json, and the input data under the key "in" to create input.json

Thanks for this amazing work, I'm closing this now