mirror of
https://github.com/socathie/circomlib-ml.git
synced 2026-01-10 06:28:08 -05:00
Add Reshape2D circuit and test files
This commit is contained in:
15
circuits/Reshape2D.circom
Normal file
15
circuits/Reshape2D.circom
Normal file
@@ -0,0 +1,15 @@
|
||||
pragma circom 2.0.0;
|
||||
|
||||
// Reshape layer with that accepts a 1D input
|
||||
template Reshape2D (nRows, nCols, nChannels) {
|
||||
signal input in[nRows*nCols*nChannels];
|
||||
signal input out[nRows][nCols][nChannels];
|
||||
|
||||
for (var i=0; i<nRows; i++) {
|
||||
for (var j=0; j<nCols; j++) {
|
||||
for (var k=0; k<nChannels; k++) {
|
||||
out[i][j][k] === in[i*nCols*nChannels + j*nChannels + k];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
1
models/reshape2D_input.json
Normal file
1
models/reshape2D_input.json
Normal file
@@ -0,0 +1 @@
|
||||
{"in": [9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807], "out": [9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807]}
|
||||
205
models/reshape2d.ipynb
Normal file
205
models/reshape2d.ipynb
Normal file
@@ -0,0 +1,205 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from tensorflow.keras.layers import Input, Reshape\n",
|
||||
"from tensorflow.keras import Model\n",
|
||||
"import numpy as np"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"inputs = Input(shape=(75,))\n",
|
||||
"x = Reshape((5, 5, 3))(inputs)\n",
|
||||
"model = Model(inputs, x)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Model: \"model\"\n",
|
||||
"_________________________________________________________________\n",
|
||||
" Layer (type) Output Shape Param # \n",
|
||||
"=================================================================\n",
|
||||
" input_1 (InputLayer) [(None, 75)] 0 \n",
|
||||
" \n",
|
||||
" reshape (Reshape) (None, 5, 5, 3) 0 \n",
|
||||
" \n",
|
||||
"=================================================================\n",
|
||||
"Total params: 0\n",
|
||||
"Trainable params: 0\n",
|
||||
"Non-trainable params: 0\n",
|
||||
"_________________________________________________________________\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"model.summary()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"array([[0.72327659, 0.97464849, 0.2592479 , 0.63799774, 0.89013732,\n",
|
||||
" 0.95867971, 0.06431743, 0.55685192, 0.77031965, 0.09982323,\n",
|
||||
" 0.10704737, 0.40713332, 0.57294341, 0.21883552, 0.22967276,\n",
|
||||
" 0.6221842 , 0.64159904, 0.684413 , 0.59126341, 0.88438877,\n",
|
||||
" 0.56715972, 0.93006015, 0.85704814, 0.79864291, 0.39604054,\n",
|
||||
" 0.30495253, 0.38333952, 0.69453548, 0.59207958, 0.30889659,\n",
|
||||
" 0.17302571, 0.41351124, 0.37527957, 0.43118255, 0.31526054,\n",
|
||||
" 0.12925303, 0.30129156, 0.73921834, 0.98336451, 0.03352392,\n",
|
||||
" 0.27839826, 0.6811155 , 0.96320785, 0.16882282, 0.68572833,\n",
|
||||
" 0.20924115, 0.30604142, 0.09080768, 0.63244108, 0.55914947,\n",
|
||||
" 0.60870048, 0.49377892, 0.9710362 , 0.12959508, 0.62162852,\n",
|
||||
" 0.26827067, 0.84771621, 0.40895646, 0.52476578, 0.48532215,\n",
|
||||
" 0.27144489, 0.19194784, 0.85410267, 0.11912042, 0.37034274,\n",
|
||||
" 0.25759208, 0.88306728, 0.98917787, 0.61814043, 0.49141046,\n",
|
||||
" 0.74162286, 0.81722887, 0.4728493 , 0.19955073, 0.42201694]])"
|
||||
]
|
||||
},
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"X = np.random.rand(1,75)\n",
|
||||
"X"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"1/1 [==============================] - 0s 44ms/step\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"2024-02-04 00:14:24.910151: W tensorflow/core/platform/profile_utils/cpu_utils.cc:128] Failed to get CPU frequency: 0 Hz\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"array([[[[0.7232766 , 0.9746485 , 0.2592479 ],\n",
|
||||
" [0.63799775, 0.8901373 , 0.9586797 ],\n",
|
||||
" [0.06431743, 0.5568519 , 0.77031964],\n",
|
||||
" [0.09982323, 0.10704737, 0.4071333 ],\n",
|
||||
" [0.5729434 , 0.21883552, 0.22967276]],\n",
|
||||
"\n",
|
||||
" [[0.6221842 , 0.64159906, 0.684413 ],\n",
|
||||
" [0.5912634 , 0.88438874, 0.5671597 ],\n",
|
||||
" [0.93006015, 0.85704815, 0.79864293],\n",
|
||||
" [0.39604053, 0.30495253, 0.38333952],\n",
|
||||
" [0.6945355 , 0.5920796 , 0.3088966 ]],\n",
|
||||
"\n",
|
||||
" [[0.17302571, 0.41351125, 0.37527958],\n",
|
||||
" [0.43118253, 0.31526053, 0.12925303],\n",
|
||||
" [0.30129156, 0.73921835, 0.9833645 ],\n",
|
||||
" [0.03352392, 0.27839825, 0.6811155 ],\n",
|
||||
" [0.96320784, 0.16882282, 0.6857283 ]],\n",
|
||||
"\n",
|
||||
" [[0.20924115, 0.30604142, 0.09080768],\n",
|
||||
" [0.6324411 , 0.55914944, 0.60870045],\n",
|
||||
" [0.4937789 , 0.9710362 , 0.12959507],\n",
|
||||
" [0.6216285 , 0.26827067, 0.8477162 ],\n",
|
||||
" [0.40895647, 0.5247658 , 0.48532215]],\n",
|
||||
"\n",
|
||||
" [[0.2714449 , 0.19194785, 0.8541027 ],\n",
|
||||
" [0.11912042, 0.37034273, 0.25759208],\n",
|
||||
" [0.8830673 , 0.9891779 , 0.6181404 ],\n",
|
||||
" [0.49141046, 0.74162287, 0.81722885],\n",
|
||||
" [0.4728493 , 0.19955073, 0.42201695]]]], dtype=float32)"
|
||||
]
|
||||
},
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"y = model.predict(X)\n",
|
||||
"y"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"in_json = {\n",
|
||||
" \"in\": (X*1e36).round().astype(int).flatten().tolist(),\n",
|
||||
" \"out\": (X*1e36).round().astype(int).flatten().tolist()\n",
|
||||
"}"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import json"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"with open(\"reshape2D_input.json\", \"w\") as f:\n",
|
||||
" json.dump(in_json, f)"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "keras2circom",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.9.13"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
||||
25
test/Reshape2D.js
Normal file
25
test/Reshape2D.js
Normal file
@@ -0,0 +1,25 @@
|
||||
const chai = require("chai");
|
||||
const path = require("path");
|
||||
|
||||
const wasm_tester = require("circom_tester").wasm;
|
||||
|
||||
const F1Field = require("ffjavascript").F1Field;
|
||||
const Scalar = require("ffjavascript").Scalar;
|
||||
exports.p = Scalar.fromString("21888242871839275222246405745257275088548364400416034343698204186575808495617");
|
||||
const Fr = new F1Field(exports.p);
|
||||
|
||||
const assert = chai.assert;
|
||||
|
||||
describe("Reshape2D layer test", function () {
|
||||
this.timeout(100000000);
|
||||
|
||||
it("75 -> (5,5,3)", async () => {
|
||||
const INPUT = require("../models/reshape2D_input.json");
|
||||
|
||||
const circuit = await wasm_tester(path.join(__dirname, "circuits", "Reshape2D_test.circom"));
|
||||
|
||||
const witness = await circuit.calculateWitness(INPUT, true);
|
||||
|
||||
assert(Fr.eq(Fr.e(witness[0]),Fr.e(1)));
|
||||
});
|
||||
});
|
||||
5
test/circuits/Reshape2D_test.circom
Normal file
5
test/circuits/Reshape2D_test.circom
Normal file
@@ -0,0 +1,5 @@
|
||||
pragma circom 2.0.0;
|
||||
|
||||
include "../../circuits/Reshape2D.circom";
|
||||
|
||||
component main = Reshape2D(5, 5, 3);
|
||||
Reference in New Issue
Block a user