Skip to content

Commit

Permalink
cleanup(json generation)
Browse files Browse the repository at this point in the history
  • Loading branch information
dcbuild3r committed Oct 1, 2022
1 parent a2b5df0 commit 4dacdbd
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 56 deletions.
68 changes: 13 additions & 55 deletions ref_cnn/generate_cnn_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,6 @@ class Layer(Enum):
"data": initial
}

json_data = json.dumps(data, cls=Encoder)

layers_json = {"initial": json_data}

# conv layer
shape = (32,5,5,3)
f = np.random.randint(low=-10, high=+10, size=shape)
Expand All @@ -178,9 +174,6 @@ class Layer(Enum):
}

model = [conv]
json_data = json.dumps(data, cls=Encoder)

layers_json["conv1"] = json_data

x, n_params, n_multiplications, name = conv_layer(x, f)
print(p.format(name, str(x.shape), n_params, n_multiplications))
Expand Down Expand Up @@ -229,10 +222,6 @@ class Layer(Enum):

model.append(conv)

json_data = json.dumps(data, cls=Encoder)

layers_json["conv2"] = json_data

x, n_params, n_multiplications, name = conv_layer(x, f)
print(p.format(name, str(x.shape), n_params, n_multiplications))

Expand Down Expand Up @@ -272,31 +261,24 @@ class Layer(Enum):

# fully connected
shape = (1000, x.shape[0])
weights = np.random.randint(low=-10, high=+10, size=shape)
weights = np.random.randint(low=-10, high=+10, size=shape)

# weights json
weights1 = weights.flatten().astype(np.float32, copy=False)

data = {
"v": 1,
"dim": shape,
"data": weights1
"data": weights.flatten().astype(np.float32, copy=False)
}

json_data = json.dumps(data, cls=Encoder)

layers_json["weights1"] = json_data

# biases json
shape = [1000]
biases = np.random.randint(low=-10, high=+10, size=(1000))
biases1 = biases.flatten().astype(np.float32, copy=False)

data2 = {
"v": 1,
# ndarray can't take a single value, needs to be in json array
"dim": shape,
"data": biases1
"data": biases.flatten().astype(np.float32, copy=False)
}

fully_connected = {
Expand All @@ -308,10 +290,6 @@ class Layer(Enum):

model.append(fully_connected)

json_data = json.dumps(data2, cls=Encoder)

layers_json["biases1"] = json_data

x, n_params, n_multiplications, name = fully_connected_layer(x, weights, biases)
print(p.format(name, str(x.shape), n_params, n_multiplications))

Expand All @@ -328,32 +306,22 @@ class Layer(Enum):

# fully connected
shape = (5, x.shape[0])
weights = np.random.randint(low=-10, high=+10, size=shape)

weights2 = weights.flatten().astype(np.float32, copy=False)
weights = np.random.randint(low=-10, high=+10, size=shape)

data = {
"v": 1,
"dim": shape,
"data": weights2
"data": weights.flatten().astype(np.float32, copy=False)
}

json_data = json.dumps(data, cls=Encoder)

layers_json["weights2"] = json_data


shape = [5]
biases = np.random.randint(low=-10, high=+10, size=shape)


biases2 = biases.flatten().astype(np.float32, copy=False)
biases = np.random.randint(low=-10, high=+10, size=shape)

data2 = {
"v": 1,
# ndarray can't take a single value, needs to be in json array
"dim": [5],
"data": biases2
"data": biases.flatten().astype(np.float32, copy=False)
}

fully_connected = {
Expand All @@ -365,17 +333,6 @@ class Layer(Enum):

model.append(fully_connected)

json_data = json.dumps(data2, cls=Encoder)

layers_json["biases2"] = json_data

# create files for arrays
for layer, json_data in layers_json.items():
with open(f'../src/json/{layer}.json', "w") as f:
print(f'created {layer}.json in the proto-neural-zkp/src/json folder')
f.write(json_data)


x, n_params, n_multiplications, name = fully_connected_layer(x, weights, biases)
print(p.format(name, str(x.shape), n_params, n_multiplications))

Expand All @@ -396,10 +353,11 @@ class Layer(Enum):
x, n_params, n_multiplications, name = normalize(x)
print(p.format(name, str(x.shape), n_params, n_multiplications))

model_data = json.dumps(model, cls=Encoder)
with open('../src/json/model.json', "w") as f:
print('created model.json in the proto-neural-zkp/src/json folder')
f.write(model_data)

print("\nfinal output:", x)

print("\nfinal output:", x)

model_data = json.dumps(model, cls=Encoder)
with open('../src/json/model.json', "w") as f:
print('\ncreated model.json in the proto-neural-zkp/src/json folder')
f.write(model_data)
13 changes: 12 additions & 1 deletion ref_cnn/vanilla_cnn.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,17 @@
"metadata": {},
"outputs": [],
"source": [
"import json\n",
"\n",
"# Encoder\n",
"from json import JSONEncoder\n",
"\n",
"class Encoder(JSONEncoder):\n",
" def default(self, obj):\n",
" if isinstance(obj, np.ndarray):\n",
" return obj.tolist()\n",
" return JSONEncoder.default(self, obj)\n",
"\n",
"np.random.seed(12345)\n",
"\n",
"shape = (3,3,3)\n",
Expand All @@ -240,7 +251,7 @@
" \"data\": x\n",
" }\n",
"\n",
"json_data = json.dumps(data, cls=NumpyArrayEncoder)\n",
"json_data = json.dumps(data, cls=Encoder)\n",
"\n",
"with open(\"../src/json/test.json\", \"w\") as f:\n",
" f.write(json_data)"
Expand Down

0 comments on commit 4dacdbd

Please sign in to comment.