text
stringlengths 1
2.05k
|
---|
b[38;5;241;43m.\u001b[39;49m\u001b[43mround\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mastype\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mint\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mdepthBias\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mastype\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mint\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mpointBias\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mastype\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mint\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 25\u001b[0m actual_scaled \u001b[38;5;241m=\u001b[39m [[[point_out[i][j][k] \u001b[38;5;241m/\u001b[39m \u001b[38;5;241m10\u001b[39m\u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mEXPONENT \u001b[38;5;28;01mfor\u001b[39;00m k \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mrange\u001b[39m(\u001b[38;5;241m6\u001b[39m)] \u001b[38;5;28;01mfor\u001b[39;00m j \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mrange\u001b[39m(\u001b[38;5;241m5\u001b[39m)] \u001b[38;5;28;01mfor\u001b[39;00m i \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mrange\u001b[39m(\u001b[38;5;241m5\u001b[39m)]\n\u001b[1;32m 27\u001b[0m expected \u001b[38;5;241m=\u001b[39m expected\u001b[38;5;241m.\u001b[39msqueeze()\u001b[38;5;241m.\u001b[39mtranspose((\u001b[38;5;241m1\u001b[39m, \u001b[38;5;241m2\u001b[39m, \u001b[38;5;241m0\u001b[39m))\n",
"Cell \u001b[0;32mIn[5], line 60\u001b[0m, in \u001b[0;36mSeparableConvImpl\u001b[0;34m(nRows, nCols, nChannels, nDepthFilters, nPointFilters, kernelSize, strides, n, input, depthWeights, pointWeights, depthBias, pointBias)\u001b[0m\n\u001b[1;32m 57\u001b[0m outCols \u001b[38;5;241m=\u001b[39m (nCols \u001b[38;5;241m-\u001b[39m kernelSize)\u001b[38;5;241m/\u001b[39m\u001b[38;5;241m/\u001b[39mstrides \u001b[38;5;241m+\u001b[39m \u001b[38;5;241m1\u001b[39m\n\u001b[1;32m 59\u001b[0m depth_out, depth_remainder \u001 |
b[38;5;241m=\u001b[39m DepthwiseConv(nRows, nCols, nChannels, nDepthFilters, kernelSize, strides, n, \u001b[38;5;28minput\u001b[39m, depthWeights, depthBias)\n\u001b[0;32m---> 60\u001b[0m point_out, point_str_out, point_remainder \u001b[38;5;241m=\u001b[39m PointwiseConv2d(outRows, outCols, nChannels, nPointFilters, strides, n, \u001b[43mdepthOut\u001b[49m, pointWeights, pointBias)\n\u001b[1;32m 61\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m depth_out, depth_remainder, point_out, point_str_out, point_remainder\n",
"\u001b[0;31mNameError\u001b[0m: name 'depthOut' is not defined"
]
}
],
"source": [
"depthWeights = model.dw_conv.weight.squeeze().detach().numpy()\n",
"depthBias = torch.zeros(depthWeights.shape[0]).numpy()\n",
"\n",
"depthWeights = depthWeights.transpose((1, 2, 0))\n",
"\n",
"pointWeights = model.pw_conv.weight.detach().numpy()\n",
"print(f\"{depthWeights.shape=}\")\n",
"pointBias = torch.zeros(pointWeights.shape[0]).numpy()\n",
"pointWeights = pointWeights.transpose((2, 3, 1, 0)).squeeze()\n",
"\n",
"expected = model(input).detach().numpy()\n",
"print(f\"{expected.shape=}\")\n",
"\n",
"padded = F.pad(input, (1,1,1,1), \"constant\", 0)
"padded = padded.squeeze().numpy().transpose((1, 2, 0))\n",
"\n",
"print(pointBias.shape)\n",
"
"quantized_image = padded * 10**EXPONENT\n",
"quantized_depth_weights = depthWeights * 10**EXPONENT\n",
"quantized_point_weights = pointWeights * 10**EXPONENT\n",
"\n",
"depth_out, depth_remainder, point_out, point_str_out, point_remainder = SeparableConvImpl(7, 7, 3, 3, 6, 3, 1, 10**EXPONENT, quantized_image.round().astype(int), quantized_depth_weights.round().astype(int), quantized_point_weights.round().astype(int), depthBias.astype(int), pointBias.astype(int))\n",
"\n",
"actual_scaled = [[[point_out[i][j][k] / 10**EXPONENT for k in range(6)] for j in range(5)] for i in range(5)]\n",
"\n",
"expected = expected.squeeze().transpose((1, 2, 0) |
)\n",
"\n",
"assert(np.allclose(expected, actual_scaled, atol=0.00001))\n",
"\n",
"\n",
"
"\n",
"circuit_in = quantized_image.round().astype(int).astype(str).tolist()\n",
"circuit_depth_weights = quantized_depth_weights.round().astype(int).astype(str).tolist()\n",
"circuit_point_weights = quantized_point_weights.round().astype(int).astype(str).tolist()\n",
"circuit_depth_bias = depthBias.round().astype(int).astype(str).tolist()\n",
"circuit_point_bias = pointBias.round().astype(int).astype(str).tolist()\n",
"\n",
"\n",
"input_json_path = \"separableConv2D_input.json\"\n",
"with open(input_json_path, \"w\") as input_file:\n",
" json.dump({\"in\": circuit_in,\n",
" \"depthWeights\": circuit_depth_weights,\n",
" \"depthBias\": circuit_depth_bias,\n",
" \"depthRemainder\": depth_remainder,\n",
" \"depthOut\": depth_out,\n",
" \n",
" \"pointWeights\": circuit_point_weights,\n",
" \"pointBias\": circuit_point_bias,\n",
" \"pointRemainder\": point_remainder,\n",
" \"pointOut\": point_str_out,\n",
" },\n",
" input_file)\n"
]
},
{
"cell_type": "code",
"execution_count": 44,
"id": "f0dcbca8-7f2f-47ea-b662-29a8560774b1",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 289246014266912, 458852178050435, -104551710192411,\n",
" -85286796832706, 70991076566637, -373719950995314])"
]
},
"execution_count": 44,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"test = np.array(actual)\n",
"test[0][0]"
]
},
{
"cell_type": "code",
"execution_count": 45,
"id": "3a652322-2fa0-4f51-bb2f-ed49bd94c65a",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 0.289246 , 0.45885214, -0.10455 |
171, -0.0852868 , 0.07099106,\n",
" -0.37371993], dtype=float32)"
]
},
"execution_count": 45,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"expected[0][0]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "56d3dda8-af64-402e-9c86-b333ca6782ba",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.11.5"
}
},
"nbformat": 4,
"nbformat_minor": 5
} |
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from tensorflow.keras.layers |
import Input, AveragePooling2D, Lambda\n",
"from tensorflow.keras |
import Model\n",
" |
import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"inputs = Input(shape=(5,5,3))\n",
"x = AveragePooling2D(pool_size=2)(inputs)\n",
"x = Lambda(lambda x: x*4)(x)\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",
"input_1 (InputLayer) [(None, 5, 5, 3)] 0 \n",
"_________________________________________________________________\n",
"average_pooling2d (AveragePo (None, 2, 2, 3) 0 \n",
"_________________________________________________________________\n",
"lambda (Lambda) (None, 2, 2, 3) 0 \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.83128186, 0.15650764, 0.23798145],\n",
" [0.00277366, 0.8374127 , 0.95278315],\n",
" [0.3074389 , 0.21931738, 0.14886067],\n",
" [0.13590018, 0.98728255, 0.12085182],\n",
" [0.47212572, 0.51380922, 0.74891219]],\n",
"\n",
" [[0.74680338, 0.2533205 , 0.5039968 ],\n",
" [0.14475403, 0.00791911, 0.4361197 ],\n",
" [0.69925568, 0.77507624, 0.40388991],\n",
" |
[0.29508251, 0.99375606, 0.84959701],\n",
" [0.88844918, 0.33910189, 0.9617212 ]],\n",
"\n",
" [[0.76480625, 0.591287 , 0.0714191 ],\n",
" [0.94371681, 0.1695303 , 0.4476252 ],\n",
" [0.54372616, 0.83818804, 0.95211573],\n",
" [0.30485104, 0.15165265, 0.94709317],\n",
" [0.90827137, 0.58854675, 0.01857002]],\n",
"\n",
" [[0.70123418, 0.43090173, 0.7096038 ],\n",
" [0.20637783, 0.20096581, 0.22956612],\n",
" [0.81978383, 0.16775403, 0.67412096],\n",
" [0.1011535 , 0.35596916, 0.36702071],\n",
" [0.5874605 , 0.79341372, 0.93292159]],\n",
"\n",
" [[0.77997124, 0.46311399, 0.5465576 ],\n",
" [0.20406287, 0.37547625, 0.59862253],\n",
" [0.52933135, 0.84249092, 0.02969684],\n",
" [0.29114617, 0.10405779, 0.5359062 ],\n",
" [0.25197146, 0.83297465, 0.67025403]]]])"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"X = np.random.rand(1,5,5,3)\n",
"X"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[[[1.7256129, 1.2551599, 2.1308813],\n",
" [1.4376774, 2.9754324, 1.5231993]],\n",
"\n",
" [[2.6161351, 1.3926848, 1.4582142],\n",
" [1.7695144, 1.5135639, 2.9403505]]]], dtype=float32)"
]
},
"execution_count": 12,
"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*1000).round().astype(int).flatten().tolist()\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metad |
ata": {},
"outputs": [],
"source": [
"out_json = {\n",
" \"out\": (y*1000).round().astype(int).flatten().tolist()\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
" |
import json"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"with open(\"sumPooling2D_input.json\", \"w\") as f:\n",
" json.dump(in_json, f)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"with open(\"sumPooling2D_output.json\", \"w\") as f:\n",
" json.dump(out_json, f)"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [],
"source": [
"inputs = Input(shape=(10,10,3))\n",
"x = AveragePooling2D(pool_size=2, strides=3)(inputs)\n",
"x = Lambda(lambda x: x*4)(x)\n",
"model = Model(inputs, x)"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Model: \"model_2\"\n",
"_________________________________________________________________\n",
"Layer (type) Output Shape Param
"=================================================================\n",
"input_3 (InputLayer) [(None, 10, 10, 3)] 0 \n",
"_________________________________________________________________\n",
"average_pooling2d_2 (Average (None, 3, 3, 3) 0 \n",
"_________________________________________________________________\n",
"lambda_2 (Lambda) (None, 3, 3, 3) 0 \n",
"=================================================================\n",
"Total params: 0\n",
"Trainable params: 0\n",
"Non-trainable params: 0\n",
"_________________________________________________________________\n"
]
}
],
"source": [
"model.summary()"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[[[0.31514958, 0.03200121, 0.29129004],\n",
" |
[0.35725668, 0.87252739, 0.77162311],\n",
" [0.61707883, 0.7945887 , 0.48907944],\n",
" [0.98197306, 0.88814753, 0.69652672],\n",
" [0.2518265 , 0.82753267, 0.57464263],\n",
" [0.62115028, 0.76805041, 0.65967975],\n",
" [0.32491691, 0.93353364, 0.74831234],\n",
" [0.45570461, 0.96830864, 0.8476189 ],\n",
" [0.02149766, 0.27808247, 0.18207897],\n",
" [0.15949257, 0.69372265, 0.43455872]],\n",
"\n",
" [[0.70915807, 0.60410698, 0.94721792],\n",
" [0.5621233 , 0.65546021, 0.27357865],\n",
" [0.00414209, 0.08635782, 0.30528659],\n",
" [0.11492599, 0.15002234, 0.58496289],\n",
" [0.72848003, 0.55169839, 0.91708802],\n",
" [0.43479205, 0.08069621, 0.68404234],\n",
" [0.3946513 , 0.20447291, 0.10467492],\n",
" [0.78817621, 0.63518792, 0.00827133],\n",
" [0.0853401 , 0.75656605, 0.95034115],\n",
" [0.92239164, 0.06871402, 0.37783711]],\n",
"\n",
" [[0.36000095, 0.35659628, 0.6507159 ],\n",
" [0.29486935, 0.49464939, 0.40502335],\n",
" [0.09509896, 0.08726498, 0.39326876],\n",
" [0.38827707, 0.50908505, 0.63443643],\n",
" [0.27030144, 0.67783072, 0.09309034],\n",
" [0.76360544, 0.67003754, 0.28767228],\n",
" [0.55305299, 0.60216561, 0.3544107 ],\n",
" [0.55839884, 0.86964781, 0.26053367],\n",
" [0.87306012, 0.78756102, 0.04817508],\n",
" [0.72406774, 0.67679246, 0.82272016]],\n",
"\n",
" [[0.71765743, 0.50852032, 0.52047892],\n",
" [0.7484707 , 0.97207503, 0.08778545],\n",
" [0.15780167, 0.73192822, 0.40718403],\n",
" [0.93263197, 0.8772701 , 0.34486053],\n",
" [0.42436095, 0.80504181, 0.39139203],\n",
" |
[0.81358273, 0.56754054, 0.12608038],\n",
" [0.11843567, 0.61136361, 0.81339895],\n",
" [0.27636648, 0.57453166, 0.10632468],\n",
" [0.53090786, 0.14594835, 0.08140653],\n",
" [0.34118642, 0.27554414, 0.19515355]],\n",
"\n",
" [[0.12974003, 0.6264065 , 0.56250089],\n",
" [0.05655555, 0.93847961, 0.71849845],\n",
" [0.57644684, 0.37077012, 0.53949152],\n",
" [0.45904117, 0.30854737, 0.73517714],\n",
" [0.64076017, 0.59373326, 0.83758554],\n",
" [0.80707699, 0.79461191, 0.69655474],\n",
" [0.79872758, 0.26420269, 0.29237624],\n",
" [0.45087863, 0.28258419, 0.50447663],\n",
" [0.29494657, 0.31770288, 0.49309187],\n",
" [0.82460949, 0.3940875 , 0.33865267]],\n",
"\n",
" [[0.1108653 , 0.35294351, 0.44014634],\n",
" [0.4988099 , 0.34405962, 0.77622373],\n",
" [0.76444373, 0.88689451, 0.05756076],\n",
" [0.57160174, 0.0752442 , 0.5098132 ],\n",
" [0.22539676, 0.47741414, 0.28993556],\n",
" [0.43298235, 0.58710277, 0.69306001],\n",
" [0.9521223 , 0.87239108, 0.10672981],\n",
" [0.93125144, 0.19405455, 0.95483289],\n",
" [0.91030892, 0.85961313, 0.67439157],\n",
" [0.09377237, 0.75818836, 0.61985122]],\n",
"\n",
" [[0.4344654 , 0.97297157, 0.89560878],\n",
" [0.91664946, 0.68966445, 0.0530751 ],\n",
" [0.72099738, 0.05779864, 0.30259649],\n",
" [0.55598956, 0.11611106, 0.24856552],\n",
" [0.40690072, 0.66148966, 0.22159354],\n",
" [0.53035294, 0.23237414, 0.82781172],\n",
" [0.20375017, 0.23486322, 0.36461596],\n",
" [0.05525619, 0.59671011, 0.08001122],\n",
" [0.11250979, 0.98519728, 0.57553523],\n",
" |
[0.6117834 , 0.65811775, 0.78386287]],\n",
"\n",
" [[0.39532528, 0.78660638, 0.37617851],\n",
" [0.86246711, 0.59398046, 0.50843286],\n",
" [0.41395181, 0.96399598, 0.8374128 ],\n",
" [0.76981858, 0.41760042, 0.17438256],\n",
" [0.05937649, 0.93289121, 0.63833505],\n",
" [0.97571178, 0.06364159, 0.34572432],\n",
" [0.42278241, 0.52111442, 0.62746908],\n",
" [0.0401781 , 0.80713288, 0.26990436],\n",
" [0.21850787, 0.19009324, 0.04497292],\n",
" [0.16176602, 0.20893733, 0.34094974]],\n",
"\n",
" [[0.13094336, 0.90151022, 0.82541695],\n",
" [0.73192844, 0.24791076, 0.3587372 ],\n",
" [0.88818939, 0.92023872, 0.69098959],\n",
" [0.08104613, 0.6361497 , 0.42552169],\n",
" [0.44517886, 0.99055202, 0.15580116],\n",
" [0.78742252, 0.04735346, 0.46423316],\n",
" [0.53474903, 0.79917168, 0.33019955],\n",
" [0.31087978, 0.65384266, 0.77275665],\n",
" [0.56393354, 0.5761927 , 0.4287843 ],\n",
" [0.57457285, 0.67154059, 0.52881047]],\n",
"\n",
" [[0.78373278, 0.15164648, 0.92791502],\n",
" [0.0999971 , 0.47319914, 0.44424683],\n",
" [0.74758969, 0.04583226, 0.0579972 ],\n",
" [0.37325021, 0.12464474, 0.61199188],\n",
" [0.07404238, 0.65504221, 0.18787021],\n",
" [0.16955187, 0.12750002, 0.48252436],\n",
" [0.17829354, 0.85701326, 0.41402596],\n",
" [0.21677806, 0.18949005, 0.27735136],\n",
" [0.06721647, 0.16941253, 0.46916978],\n",
" [0.39921131, 0.17705116, 0.94534667]]]])"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"X = np.random.rand(1,10,10,3)\n",
"X"
]
},
{ |
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[[[1.9436876 , 2.1640959 , 2.2837098 ],\n",
" [2.0772057 , 2.4174008 , 2.7732203 ],\n",
" [1.963449 , 2.741503 , 1.7088774 ]],\n",
"\n",
" [[1.6524236 , 3.0454814 , 1.8892637 ],\n",
" [2.4567943 , 2.5845926 , 2.3090153 ],\n",
" [1.6444083 , 1.7326821 , 1.7165766 ]],\n",
"\n",
" [[2.6089072 , 3.043223 , 1.8332952 ],\n",
" [1.7920854 , 2.1280923 , 1.2828767 ],\n",
" [0.72196686, 2.1598206 , 1.3420006 ]]]], dtype=float32)"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"y = model.predict(X)\n",
"y"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [],
"source": [
"in_json = {\n",
" \"in\": (X*1000).round().astype(int).flatten().tolist()\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [],
"source": [
"out_json = {\n",
" \"out\": (y*1000).round().astype(int).flatten().tolist()\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [],
"source": [
"with open(\"sumPooling2D_stride_input.json\", \"w\") as f:\n",
" json.dump(in_json, f)"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [],
"source": [
"with open(\"sumPooling2D_stride_output.json\", \"w\") as f:\n",
" json.dump(out_json, f)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "tf24",
"language": "python",
"name": "tf24"
},
"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.8.6"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
} |
{
"cells": [
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"from tensorflow.keras.layers |
import Input, UpSampling2D\n",
"from tensorflow.keras |
import Model\n",
" |
import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"inputs = Input(shape=(1,2,3))\n",
"x = UpSampling2D(size=2)(inputs)\n",
"model = Model(inputs, x)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Model: \"model_1\"\n",
"_________________________________________________________________\n",
" Layer (type) Output Shape Param
"=================================================================\n",
" input_2 (InputLayer) [(None, 1, 2, 3)] 0 \n",
" \n",
" up_sampling2d_1 (UpSampling (None, 2, 4, 3) 0 \n",
" 2D) \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": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[[[842674, 497907, 66624],\n",
" [875287, 832625, 34934]]]])"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"X = (np.random.rand(1,1,2,3)*1e6).astype(int)\n",
"X"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1/1 [==============================] - 0s 27ms/step\n"
]
},
{ |
"data": {
"text/plain": [
"array([[[[842674., 497907., 66624.],\n",
" [842674., 497907., 66624.],\n",
" [875287., 832625., 34934.],\n",
" [875287., 832625., 34934.]],\n",
"\n",
" [[842674., 497907., 66624.],\n",
" [842674., 497907., 66624.],\n",
" [875287., 832625., 34934.],\n",
" [875287., 832625., 34934.]]]], dtype=float32)"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"y = model.predict(X)\n",
"y"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
"def UpSampling2DInt(nRows, nCols, nChannels, size, input):\n",
" out = [[[None for _ in range(nChannels)] for _ in range(nCols*size)] for _ in range(nRows*size)]\n",
" for i in range(nRows):\n",
" for j in range(nCols):\n",
" for c in range(nChannels):\n",
" for k in range(size):\n",
" for l in range(size):\n",
" out[i*size+k][j*size+l][c] = input[i][j][c]\n",
" return out\n"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [],
"source": [
"X_in = [[[int(X[0][i][j][k]) for k in range(3)] for j in range(2)] for i in range(1)]"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[[[842674, 497907, 66624],\n",
" [842674, 497907, 66624],\n",
" [875287, 832625, 34934],\n",
" [875287, 832625, 34934]],\n",
" [[842674, 497907, 66624],\n",
" [842674, 497907, 66624],\n",
" [875287, 832625, 34934],\n",
" [875287, 832625, 34934]]]"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"out = Up |
Sampling2DInt(1, 2, 3, 2, X_in)\n",
"out"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [],
"source": [
"assert np.all(y[0].astype(int) == np.array(out))"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [],
"source": [
"in_json = {\n",
" \"in\": X_in,\n",
" \"out\": out\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [],
"source": [
" |
import json\n",
"with open(\"upSampling2D_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
} |
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("AveragePooling2D layer test", function () {
this.timeout(100000000);
// AveragePooling with strides==poolSize
it("(5,5,3) -> (2,2,3)", async () => {
const INPUT = require("../models/averagePooling2D_input.json");
const circuit = await wasm_tester(path.join(__dirname, "circuits", "AveragePooling2D_test.circom"));
const witness = await circuit.calculateWitness(INPUT, true);
assert(Fr.eq(Fr.e(witness[0]),Fr.e(1)));
});
// AveragePooling with strides!=poolSize
it("(10,10,3) -> (4,4,3)", async () => {
const INPUT = require("../models/averagePooling2D_stride_input.json");
const circuit = await wasm_tester(path.join(__dirname, "circuits", "AveragePooling2D_stride_test.circom"));
const witness = await circuit.calculateWitness(INPUT, true);
assert(Fr.eq(Fr.e(witness[0]),Fr.e(1)));
});
}); |
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("BatchNormalization layer test", function () {
this.timeout(100000000);
it("(5,5,3) -> (5,5,3)", async () => {
const INPUT = require("../models/batchNormalization_input.json");
const circuit = await wasm_tester(path.join(__dirname, "circuits", "BatchNormalization_test.circom"));
const witness = await circuit.calculateWitness(INPUT, true);
assert(Fr.eq(Fr.e(witness[0]),Fr.e(1)));
});
});
|
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;
const INPUT = require("../models/conv1D_input.json");
describe("Conv1D layer test", function () {
this.timeout(100000000);
it("(20,3) -> (6,2)", async () => {
const circuit = await wasm_tester(path.join(__dirname, "circuits", "Conv1D_test.circom"));
const witness = await circuit.calculateWitness(INPUT, true);
assert(Fr.eq(Fr.e(witness[0]),Fr.e(1)));
});
}); |
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("Conv2D layer test", function () {
this.timeout(100000000);
it("(5,5,3) -> (3,3,2)", async () => {
const INPUT = require("../models/conv2D_input.json");
const circuit = await wasm_tester(path.join(__dirname, "circuits", "Conv2D_test.circom"));
const witness = await circuit.calculateWitness(INPUT, true);
assert(Fr.eq(Fr.e(witness[0]),Fr.e(1)));
});
it("(10,10,3) -> (3,3,2)", async () => {
const INPUT = require("../models/conv2D_stride_input.json");
const circuit = await wasm_tester(path.join(__dirname, "circuits", "Conv2D_stride_test.circom"));
const witness = await circuit.calculateWitness(INPUT, true);
assert(Fr.eq(Fr.e(witness[0]),Fr.e(1)));
});
}); |
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("Conv2Dsame layer test", function () {
this.timeout(100000000);
it("(5,5,3) -> (5,5,2)", async () => {
const INPUT = require("../models/Conv2Dsame_input.json");
const circuit = await wasm_tester(path.join(__dirname, "circuits", "Conv2Dsame_test.circom"));
const witness = await circuit.calculateWitness(INPUT, true);
assert(Fr.eq(Fr.e(witness[0]),Fr.e(1)));
});
it("(10,10,3) -> (4,4,2)", async () => {
const INPUT = require("../models/Conv2Dsame_stride_input.json");
const circuit = await wasm_tester(path.join(__dirname, "circuits", "Conv2Dsame_stride_test.circom"));
const witness = await circuit.calculateWitness(INPUT, true);
assert(Fr.eq(Fr.e(witness[0]),Fr.e(1)));
});
}); |
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("Dense layer test", function () {
this.timeout(100000000);
it("20 nodes -> 10 nodes", async () => {
const INPUT = require("../models/dense_input.json");
const circuit = await wasm_tester(path.join(__dirname, "circuits", "Dense_test.circom"));
const witness = await circuit.calculateWitness(INPUT, true);
assert(Fr.eq(Fr.e(witness[0]),Fr.e(1)));
});
}); |
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("DepthwiseConv2D layer test", function () {
this.timeout(100000000);
it("(7,7,3) -> (5,5,3)", async () => {
const INPUT = require("../models/depthwiseConv2D_input.json");
const circuit = await wasm_tester(path.join(__dirname, "circuits", "DepthwiseConv2D_test.circom"));
const witness = await circuit.calculateWitness(INPUT, true);
assert(Fr.eq(Fr.e(witness[0]),Fr.e(1)));
});
});
|
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("Flatten2D layer test", function () {
this.timeout(100000000);
it("(5,5,3) -> 75", async () => {
const INPUT = require("../models/flatten2D_input.json");
const circuit = await wasm_tester(path.join(__dirname, "circuits", "Flatten2D_test.circom"));
const witness = await circuit.calculateWitness(INPUT, true);
assert(Fr.eq(Fr.e(witness[0]),Fr.e(1)));
});
});
|
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("GlobalAveragePooling2D layer test", function () {
this.timeout(100000000);
// GlobalAveragePooling with strides==poolSize
it("(5,5,3) -> (3,)", async () => {
const INPUT = require("../models/globalAveragePooling2D_input.json");
const circuit = await wasm_tester(path.join(__dirname, "circuits", "GlobalAveragePooling2D_test.circom"));
const witness = await circuit.calculateWitness(INPUT, true);
assert(Fr.eq(Fr.e(witness[0]),Fr.e(1)));
});
}); |
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("GlobalMaxPooling2D layer test", function () {
this.timeout(100000000);
// GlobalMaxPooling with strides==poolSize
it("(5,5,3) -> (3,)", async () => {
const INPUT = require("../models/globalMaxPooling2D_input.json");
const circuit = await wasm_tester(path.join(__dirname, "circuits", "GlobalMaxPooling2D_test.circom"));
const witness = await circuit.calculateWitness(INPUT, true);
assert(Fr.eq(Fr.e(witness[0]),Fr.e(1)));
});
}); |
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("IsNegative test", function () {
this.timeout(100000000);
it("Negative -> 1", async () => {
const circuit = await wasm_tester(path.join(__dirname, "circuits", "IsNegative_test.circom"));
//await circuit.loadConstraints();
//assert.equal(circuit.nVars, 516);
//assert.equal(circuit.constraints.length, 516);
const INPUT = {
"in": Fr.e(-1)
}
const witness = await circuit.calculateWitness(INPUT, true);
//console.log(witness);
assert(Fr.eq(Fr.e(witness[0]),Fr.e(1)));
assert(Fr.eq(Fr.e(witness[1]),Fr.e(1)));
});
it("Positive -> 0", async () => {
const circuit = await wasm_tester(path.join(__dirname, "circuits", "IsNegative_test.circom"));
//await circuit.loadConstraints();
//assert.equal(circuit.nVars, 516);
//assert.equal(circuit.constraints.length, 516);
const INPUT = {
"in": "1"
}
const witness = await circuit.calculateWitness(INPUT, true);
//console.log(witness);
assert(Fr.eq(Fr.e(witness[0]),Fr.e(1)));
assert(Fr.eq(Fr.e(witness[1]),Fr.e(0)));
});
}); |
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("IsPositive test", function () {
this.timeout(100000000);
it("Positive -> 1", async () => {
const circuit = await wasm_tester(path.join(__dirname, "circuits", "IsPositive_test.circom"));
//await circuit.loadConstraints();
//assert.equal(circuit.nVars, 516);
//assert.equal(circuit.constraints.length, 516);
const INPUT = {
"in": "1"
}
const witness = await circuit.calculateWitness(INPUT, true);
//console.log(witness);
assert(Fr.eq(Fr.e(witness[0]),Fr.e(1)));
assert(Fr.eq(Fr.e(witness[1]),Fr.e(1)));
});
it("Negative -> 0", async () => {
const circuit = await wasm_tester(path.join(__dirname, "circuits", "IsPositive_test.circom"));
//await circuit.loadConstraints();
//assert.equal(circuit.nVars, 516);
//assert.equal(circuit.constraints.length, 516);
const INPUT = {
"in": Fr.e(-1)
}
const witness = await circuit.calculateWitness(INPUT, true);
//console.log(witness);
assert(Fr.eq(Fr.e(witness[0]),Fr.e(1)));
assert(Fr.eq(Fr.e(witness[1]),Fr.e(0)));
});
}); |
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("LeakyReLU layer test", function () {
this.timeout(100000000);
it("3 nodes", async () => {
const circuit = await wasm_tester(path.join(__dirname, "circuits", "LeakyReLU_test.circom"));
const INPUT = {
"in": [Fr.e(-11),"0","3"],
"out": [Fr.e(-4),"0","3"],
"remainder": ["7","0","0"]
}
const witness = await circuit.calculateWitness(INPUT, true);
assert(Fr.eq(Fr.e(witness[0]),Fr.e(1)));
});
}); |
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("Max test", function () {
this.timeout(100000000);
it("Maximum of 4 numbers", async () => {
const circuit = await wasm_tester(path.join(__dirname, "circuits", "Max_test.circom"));
//await circuit.loadConstraints();
//assert.equal(circuit.nVars, 516);
//assert.equal(circuit.constraints.length, 516);
const INPUT = {
"in": ["1","4","2","3"]
}
const witness = await circuit.calculateWitness(INPUT, true);
//console.log(witness);
assert(Fr.eq(Fr.e(witness[0]),Fr.e(1)));
assert(Fr.eq(Fr.e(witness[1]),Fr.e(4)));
});
}); |
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("MaxPooling2D layer test", function () {
this.timeout(100000000);
// MaxPooling with strides==poolSize
it("(5,5,3) -> (2,2,3)", async () => {
const INPUT = require("../models/maxPooling2D_input.json");
const circuit = await wasm_tester(path.join(__dirname, "circuits", "MaxPooling2D_test.circom"));
const witness = await circuit.calculateWitness(INPUT, true);
assert(Fr.eq(Fr.e(witness[0]),Fr.e(1)));
});
// MaxPooling with strides!=poolSize
it("(10,10,3) -> (3,3,3)", async () => {
const INPUT = require("../models/maxPooling2D_stride_input.json");
const circuit = await wasm_tester(path.join(__dirname, "circuits", "MaxPooling2D_stride_test.circom"));
const witness = await circuit.calculateWitness(INPUT, true);
assert(Fr.eq(Fr.e(witness[0]),Fr.e(1)));
});
}); |
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("MaxPooling2Dsame layer test", function () {
this.timeout(100000000);
// MaxPooling with strides==poolSize
it("(5,5,3) -> (3,3,3)", async () => {
const INPUT = require("../models/maxPooling2Dsame_input.json");
const circuit = await wasm_tester(path.join(__dirname, "circuits", "MaxPooling2Dsame_test.circom"));
const witness = await circuit.calculateWitness(INPUT, true);
assert(Fr.eq(Fr.e(witness[0]),Fr.e(1)));
});
// MaxPooling with strides!=poolSize
it("(10,10,3) -> (4,4,3)", async () => {
const INPUT = require("../models/maxPooling2Dsame_stride_input.json");
const circuit = await wasm_tester(path.join(__dirname, "circuits", "MaxPooling2Dsame_stride_test.circom"));
const witness = await circuit.calculateWitness(INPUT, true);
assert(Fr.eq(Fr.e(witness[0]),Fr.e(1)));
});
}); |
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("PointwiseConv2D layer test", function () {
this.timeout(100000000);
it("(7,7,3) -> (5,5,3)", async () => {
const INPUT = require("../models/pointwiseConv2D_input.json");
const circuit = await wasm_tester(path.join(__dirname, "circuits", "PointwiseConv2D_test.circom"));
const witness = await circuit.calculateWitness(INPUT, true);
assert(Fr.eq(Fr.e(witness[0]),Fr.e(1)));
});
});
|
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("ReLU layer test", function () {
this.timeout(100000000);
it("3 nodes", async () => {
const circuit = await wasm_tester(path.join(__dirname, "circuits", "ReLU_test.circom"));
const INPUT = {
"in": [Fr.e(-3),"0","3"],
"out": ["0","0","3"]
}
const witness = await circuit.calculateWitness(INPUT, true);
assert(Fr.eq(Fr.e(witness[0]),Fr.e(1)));
});
}); |
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)));
});
});
|
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("SeparableConv2D layer test", function () {
this.timeout(100000000);
it("(7,7,3) -> (5,5,3)", async () => {
const INPUT = require("../models/separableConv2D_input.json");
const circuit = await wasm_tester(path.join(__dirname, "circuits", "SeparableConv2D_test.circom"));
const witness = await circuit.calculateWitness(INPUT, true);
assert(Fr.eq(Fr.e(witness[0]),Fr.e(1)));
});
});
|
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("SumPooling2D layer test", function () {
this.timeout(100000000);
it("(5,5,3) -> (2,2,3)", async () => {
const json = require("../models/sumPooling2D_input.json");
const OUTPUT = require("../models/sumPooling2D_output.json");
const circuit = await wasm_tester(path.join(__dirname, "circuits", "SumPooling2D_test.circom"));
const INPUT = {
"in": json.in
}
const witness = await circuit.calculateWitness(INPUT, true);
assert(Fr.eq(Fr.e(witness[0]),Fr.e(1)));
let ape = 0;
for (var i=0; i<OUTPUT.out.length; i++) {
ape += Math.abs((OUTPUT.out[i]-parseInt(Fr.toString(witness[i+1])))/OUTPUT.out[i]);
}
const mape = ape/OUTPUT.out.length;
console.log("mean absolute % error", mape);
assert(mape < 0.01);
});
it("(10,10,3) -> (3,3,3)", async () => {
const json = require("../models/sumPooling2D_stride_input.json");
const OUTPUT = require("../models/sumPooling2D_stride_output.json");
const circuit = await wasm_tester(path.join(__dirname, "circuits", "SumPooling2D_stride_test.circom"));
const INPUT = {
"in": json.in
}
const witness = await circuit.calculateWitness(INPUT, true);
assert(Fr.eq(Fr.e(witness[0]),Fr.e(1)));
let ape = 0;
for (var i=0; i<OUTPUT.out.length; i++) {
ape += Math.abs((OUTPUT.out[i]-parseInt(Fr.toString(witness[i+1])))/OUTPUT.out[i]);
}
const mape = ape/OUTPUT.out.length;
console.log |
("mean absolute % error", mape);
assert(mape < 0.01);
});
}); |
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("UpSampling2D layer test", function () {
this.timeout(100000000);
// UpSampling with strides==poolSize
it("(1,2,3) -> (2,4,3)", async () => {
const INPUT = require("../models/upSampling2D_input.json");
const circuit = await wasm_tester(path.join(__dirname, "circuits", "UpSampling2D_test.circom"));
const witness = await circuit.calculateWitness(INPUT, true);
assert(Fr.eq(Fr.e(witness[0]),Fr.e(1)));
});
}); |
pragma circom 2.0.0;
include "../../circuits/AveragePooling2D.circom";
// poolSize!=strides
component main = AveragePooling2D(10, 10, 3, 3, 2); |
pragma circom 2.0.0;
include "../../circuits/AveragePooling2D.circom";
// poolSize=strides - default Keras settings
component main = AveragePooling2D(5, 5, 3, 2, 2); |
pragma circom 2.0.0;
include "../../circuits/BatchNormalization2D.circom";
component main = BatchNormalization2D(5,5,3,10**36); |
pragma circom 2.0.0;
include "../../circuits/Conv1D.circom";
component main = Conv1D(20, 3, 2, 4, 3, 10**36); |
pragma circom 2.0.0;
include "../../circuits/Conv2D.circom";
component main = Conv2D(10, 10, 3, 2, 4, 3, 10**36); |
pragma circom 2.0.0;
include "../../circuits/Conv2D.circom";
component main = Conv2D(5, 5, 3, 2, 3, 1, 10**36); |
pragma circom 2.0.0;
include "../../circuits/Conv2Dsame.circom";
component main = Conv2Dsame(10, 10, 3, 2, 4, 3, 10**36); |
pragma circom 2.0.0;
include "../../circuits/Conv2Dsame.circom";
component main = Conv2Dsame(5, 5, 3, 2, 3, 1, 10**36); |
pragma circom 2.0.0;
include "../../circuits/Dense.circom";
component main = Dense(20,10,10**36); |
pragma circom 2.0.0;
include "../../circuits/DepthwiseConv2D.circom";
component main = DepthwiseConv2D(7, 7, 3, 3, 3, 1, 10**15);
|
pragma circom 2.0.0;
include "../../circuits/Flatten2D.circom";
component main = Flatten2D(5, 5, 3); |
pragma circom 2.0.0;
include "../../circuits/GlobalAveragePooling2D.circom";
component main = GlobalAveragePooling2D(5, 5, 3); |
pragma circom 2.0.0;
include "../../circuits/GlobalMaxPooling2D.circom";
component main = GlobalMaxPooling2D(5, 5, 3); |
pragma circom 2.0.0;
include "../../circuits/util.circom";
component main = IsNegative(); |
pragma circom 2.0.0;
include "../../circuits/util.circom";
component main = IsPositive(); |
pragma circom 2.0.0;
include "../../circuits/LeakyReLU.circom";
template leaky_relu_test() {
signal input in[3];
signal input out[3];
signal input remainder[3];
component leaky_relu[3];
for (var i=0; i<3; i++) {
leaky_relu[i] = LeakyReLU(3);
leaky_relu[i].in <== in[i];
leaky_relu[i].out <== out[i];
leaky_relu[i].remainder <== remainder[i];
}
}
component main = leaky_relu_test(); |
pragma circom 2.0.0;
include "../../circuits/MaxPooling2D.circom";
component main = MaxPooling2D(10, 10, 3, 2, 3); |
pragma circom 2.0.0;
include "../../circuits/MaxPooling2D.circom";
// poolSize=strides - default Keras settings
component main = MaxPooling2D(5, 5, 3, 2, 2); |
pragma circom 2.0.0;
include "../../circuits/MaxPooling2Dsame.circom";
component main = MaxPooling2Dsame(10, 10, 3, 2, 3); |
pragma circom 2.0.0;
include "../../circuits/MaxPooling2Dsame.circom";
// poolSize=strides - default Keras settings
component main = MaxPooling2Dsame(5, 5, 3, 2, 2); |
pragma circom 2.0.0;
include "../../circuits/util.circom";
component main = Max(4); |
pragma circom 2.0.0;
include "../../circuits/PointwiseConv2D.circom";
component main = PointwiseConv2D(5, 5, 3, 6, 10**15);
|
pragma circom 2.0.0;
include "../../circuits/ReLU.circom";
template relu_test() {
signal input in[3];
signal input out[3];
component relu[3];
for (var i=0; i<3; i++) {
relu[i] = ReLU();
relu[i].in <== in[i];
relu[i].out <== out[i];
}
}
component main = relu_test(); |
pragma circom 2.0.0;
include "../../circuits/Reshape2D.circom";
component main = Reshape2D(5, 5, 3); |
pragma circom 2.0.0;
include "../../circuits/SeparableConv2D.circom";
component main = SeparableConv2D(7, 7, 3, 3, 6, 3, 1, 10**15);
|
pragma circom 2.0.0;
include "../../circuits/SumPooling2D.circom";
component main = SumPooling2D(10, 10, 3, 2, 3); |
pragma circom 2.0.0;
include "../../circuits/SumPooling2D.circom";
// poolSize=strides - default Keras settings
component main = SumPooling2D(5, 5, 3, 2, 2); |
pragma circom 2.0.0;
include "../../circuits/UpSampling2D.circom";
component main = UpSampling2D(1, 2, 3, 2); |
pragma circom 2.0.0;
include "../../circuits/crypto/encrypt.circom";
component main = DecryptBits(1000); |
pragma circom 2.0.0;
include "../../circuits/crypto/encrypt.circom";
component main = Decrypt(); |
pragma circom 2.0.0;
include "../../circuits/crypto/ecdh.circom";
component main = Ecdh(); |
pragma circom 2.0.0;
include "../../circuits/crypto/encrypt.circom";
include "../../circuits/crypto/ecdh.circom";
// from zk-ml/linear-regression-demo
template Test() {
signal input message;
signal input shared_key;
signal output out;
signal input private_key;
signal input public_key[2];
component ecdh = Ecdh();
ecdh.private_key <== private_key;
ecdh.public_key[0] <== public_key[0];
ecdh.public_key[1] <== public_key[1];
log(ecdh.shared_key);
log(shared_key);
log(private_key);
log(public_key[0]);
log(public_key[1]);
shared_key === ecdh.shared_key;
component enc = Encrypt();
component dec = Decrypt();
message ==> enc.plaintext;
shared_key ==> enc.shared_key;
shared_key ==> dec.shared_key;
enc.out[0] ==> dec.message[0];
enc.out[1] ==> dec.message[1];
log(dec.out);
dec.out === message;
out <== 1;
}
component main = Test(); |
pragma circom 2.0.0;
include "../../circuits/crypto/encrypt.circom";
component main = EncryptBits(1000); |
pragma circom 2.0.0;
include "../../circuits/crypto/encrypt.circom";
component main = Encrypt(); |
pragma circom 2.0.0; |
include "../../circuits/Conv2D.circom"; |
include "../../circuits/Dense.circom"; |
include "../../circuits/ArgMax.circom"; |
include "../../circuits/Poly.circom"; |
include "../../circuits/AveragePooling2D.circom"; |
include "../../circuits/BatchNormalization2D.circom"; |
include "../../circuits/Flatten2D.circom"; |
include "../../circuits/crypto/encrypt.circom"; |
include "../../circuits/crypto/ecdh.circom";
template encrypted_mnist_latest() {
signal input in[28][28][1];
signal input conv2d_1_weights[3][3][1][4];
signal input conv2d_1_bias[4];
signal input bn_1_a[4];
signal input bn_1_b[4];
signal input conv2d_2_weights[3][3][4][8];
signal input conv2d_2_bias[8];
signal input bn_2_a[8];
signal input bn_2_b[8];
signal input dense_weights[200][10];
signal input dense_bias[10];
signal output out;
signal input private_key;
signal input public_key[2];
component ecdh = Ecdh();
ecdh.private_key <== private_key;
ecdh.public_key[0] <== public_key[0];
ecdh.public_key[1] <== public_key[1];
signal output message[3*3*1*4+4+4+4+3*3*4*8+8+8+8+200*10+10+1];
component enc = EncryptBits(3*3*1*4+4+4+4+3*3*4*8+8+8+8+200*10+10);
enc.shared_key <== ecdh.shared_key;
var idx = 0;
component conv2d_1 = Conv2D(28,28,1,4,3,1);
component bn_1 = BatchNormalization2D(26,26,4);
component poly_1[26][26][4];
component avg2d_1 = AveragePooling2D(26,26,4,2,2,25);
component conv2d_2 = Conv2D(13,13,4,8,3,1);
component bn_2 = BatchNormalization2D(11,11,8);
component poly_2[11][11][8];
component avg2d_2 = AveragePooling2D(11,11,8,2,2,25);
component flatten = Flatten2D(5,5,8);
component dense = Dense(200,10);
component argmax = ArgMax(10);
for (var i=0; i<28; i++) {
for (var j=0; j<28; j++) {
conv2d_1.in[i][j][0] <== in[i][j][0];
}
}
for (var i=0; i<3; i++) {
for (var j=0; j<3; j++) {
for (var m=0; m<4; m++) {
conv2d_1.weights[i][j][0][m] <== conv2d_1_weights[i][j][0][m];
enc.plaintext[idx] <== conv2d_1_weights[i][j][0][m];
idx++;
}
}
}
for (var m=0; m<4; m++) {
conv2d_1.bias[m] <== conv2d_1_bias[m];
enc.plaintext[idx] <== conv2d_1_bias[m];
idx++;
}
for (var k=0; k<4; k++) {
bn_1.a[k] <== bn_1_a |
[k];
enc.plaintext[idx] <== bn_1_a[k];
idx++;
}
for (var k=0; k<4; k++) {
bn_1.b[k] <== bn_1_b[k];
enc.plaintext[idx] <== bn_1_b[k];
idx++;
for (var i=0; i<26; i++) {
for (var j=0; j<26; j++) {
bn_1.in[i][j][k] <== conv2d_1.out[i][j][k];
}
}
}
for (var i=0; i<26; i++) {
for (var j=0; j<26; j++) {
for (var k=0; k<4; k++) {
poly_1[i][j][k] = Poly(10**6);
poly_1[i][j][k].in <== bn_1.out[i][j][k];
avg2d_1.in[i][j][k] <== poly_1[i][j][k].out;
}
}
}
for (var i=0; i<13; i++) {
for (var j=0; j<13; j++) {
for (var k=0; k<4; k++) {
conv2d_2.in[i][j][k] <== avg2d_1.out[i][j][k];
}
}
}
for (var i=0; i<3; i++) {
for (var j=0; j<3; j++) {
for (var k=0; k<4; k++) {
for (var m=0; m<8; m++) {
conv2d_2.weights[i][j][k][m] <== conv2d_2_weights[i][j][k][m];
enc.plaintext[idx] <== conv2d_2_weights[i][j][k][m];
idx++;
}
}
}
}
for (var m=0; m<8; m++) {
conv2d_2.bias[m] <== conv2d_2_bias[m];
enc.plaintext[idx] <== conv2d_2_bias[m];
idx++;
}
for (var k=0; k<8; k++) {
bn_2.a[k] <== bn_2_a[k];
enc.plaintext[idx] <== bn_2_a[k];
idx++;
}
for (var k=0; k<8; k++) {
bn_2.b[k] <== bn_2_b[k];
enc.plaintext[idx] <== bn_2_b[k];
idx++;
for (var i=0; i<11; i++) {
for (var j=0; j<11; j++) {
bn_2.in[i][j][k] <== conv2d_2.out[i][j][k];
}
}
}
for (var i=0; i<11; i++) {
for (var j=0; j<11; j++) {
for (var k=0; k<8; k++) {
poly_2[i][j][k] = Poly(10**18);
poly_2[i][j][k].in <== bn_2.out[i][j][k];
avg2d_2.in[i][j][k] <== poly_2 |
[i][j][k].out;
}
}
}
for (var i=0; i<5; i++) {
for (var j=0; j<5; j++) {
for (var k=0; k<8; k++) {
flatten.in[i][j][k] <== avg2d_2.out[i][j][k];
}
}
}
for (var i=0; i<200; i++) {
dense.in[i] <== flatten.out[i];
for (var j=0; j<10; j++) {
dense.weights[i][j] <== dense_weights[i][j];
enc.plaintext[idx] <== dense_weights[i][j];
idx++;
}
}
for (var i=0; i<10; i++) {
dense.bias[i] <== dense_bias[i];
enc.plaintext[idx] <== dense_bias[i];
idx++;
}
for (var i=0; i<10; i++) {
argmax.in[i] <== dense.out[i];
}
out <== argmax.out;
for (var i=0; i<3*3*1*4+4+4+4+3*3*4*8+8+8+8+200*10+10+1; i++) {
message[i] <== enc.out[i];
}
}
component main = encrypted_mnist_latest(); |
pragma circom 2.0.0; |
include "../../circuits/Conv2D.circom"; |
include "../../circuits/Dense.circom"; |
include "../../circuits/ArgMax.circom"; |
include "../../circuits/ReLU.circom"; |
include "../../circuits/AveragePooling2D.circom"; |
include "../../circuits/BatchNormalization2D.circom"; |
include "../../circuits/Flatten2D.circom";
template mnist() {
signal input in[28][28][1];
signal input conv2d_1_weights[3][3][1][4];
signal input conv2d_1_bias[4];
signal input conv2d_1_out[26][26][4];
signal input conv2d_1_remainder[26][26][4];
signal input bn_1_a[4];
signal input bn_1_b[4];
signal input bn_1_out[26][26][4];
signal input bn_1_remainder[26][26][4];
signal input relu_1_out[26][26][4];
signal input avg2d_1_out[13][13][4];
signal input avg2d_1_remainder[13][13][4];
signal input conv2d_2_weights[3][3][4][8];
signal input conv2d_2_bias[8];
signal input conv2d_2_out[11][11][8];
signal input conv2d_2_remainder[11][11][8];
signal input bn_2_a[8];
signal input bn_2_b[8];
signal input bn_2_out[11][11][8];
signal input bn_2_remainder[11][11][8];
signal input relu_2_out[11][11][8];
signal input avg2d_2_out[5][5][8];
signal input avg2d_2_remainder[5][5][8];
signal input flatten_out[200];
signal input dense_weights[200][10];
signal input dense_bias[10];
signal input dense_out[10];
signal input dense_remainder[10];
signal input argmax_out;
signal output out;
component conv2d_1 = Conv2D(28,28,1,4,3,1,10**18);
component bn_1 = BatchNormalization2D(26,26,4,10**18);
component relu_1[26][26][4];
component avg2d_1 = AveragePooling2D(26,26,4,2,2);
component conv2d_2 = Conv2D(13,13,4,8,3,1,10**18);
component bn_2 = BatchNormalization2D(11,11,8,10**18);
component relu_2[11][11][8];
component avg2d_2 = AveragePooling2D(11,11,8,2,2);
component flatten = Flatten2D(5,5,8);
component dense = Dense(200,10,10**18);
component argmax = ArgMax(10);
for (var i=0; i<28; i++) {
for (var j=0; j<28; j++) {
conv2d_1.in[i][j][0] <== in[i][j][0];
}
}
for (var m=0; m<4; m++) {
for (var i=0; i<3; i++) {
for (var j=0; j<3; j++) {
conv2d_1.weights[i][j][0][m] <== conv2d_1_weights[i][j][0][m |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.