Files
circomlib-ml/models/sumPooling2d.ipynb

212 lines
5.5 KiB
Plaintext

{
"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",
"=================================================================\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,
"metadata": {},
"outputs": [],
"source": [
"out_json = {\n",
" \"out\": (y*1000).round().astype(int).flatten().tolist()\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 8,
"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": 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
}