Skip to content

Commit eade39f

Browse files
[pre-commit.ci] auto fixes from pre-commit hooks
1 parent db9ebc0 commit eade39f

File tree

2 files changed

+50
-51
lines changed

2 files changed

+50
-51
lines changed

nn_utils.py

Lines changed: 34 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,23 @@
1-
2-
3-
from io import BytesIO
41
import json
52
import os
3+
import pickle as pkl
64
import random
7-
85
from glob import glob
6+
from io import BytesIO
97
from pathlib import Path
10-
import pickle as pkl
118
from typing import Callable
129

10+
import h5py as h5
1311
import numpy as np
14-
1512
import tensorflow as tf
16-
from tensorflow import keras
17-
18-
from tqdm.auto import tqdm
19-
from matplotlib import pyplot as plt
20-
2113
import zstd
22-
import h5py as h5
23-
24-
from keras.src.saving.legacy import hdf5_format
25-
from keras.src.layers.convolutional.base_conv import Conv
26-
from keras.layers import Dense
27-
2814
from HGQ.bops import trace_minmax
15+
from keras.layers import Dense
16+
from keras.src.layers.convolutional.base_conv import Conv
17+
from keras.src.saving.legacy import hdf5_format
18+
from matplotlib import pyplot as plt
19+
from tensorflow import keras
20+
from tqdm.auto import tqdm
2921

3022

3123
class NumpyFloatValuesEncoder(json.JSONEncoder):
@@ -36,14 +28,15 @@ def default(self, obj):
3628

3729

3830
class SaveTopN(keras.callbacks.Callback):
39-
def __init__(self,
40-
metric_fn: Callable[[dict], float],
41-
n: int,
42-
path: str | Path,
43-
side: str = 'max',
44-
fname_format='epoch={epoch}-metric={metric:.4e}.h5',
45-
cond_fn: Callable[[dict], bool] = lambda x: True,
46-
):
31+
def __init__(
32+
self,
33+
metric_fn: Callable[[dict], float],
34+
n: int,
35+
path: str | Path,
36+
side: str = 'max',
37+
fname_format='epoch={epoch}-metric={metric:.4e}.h5',
38+
cond_fn: Callable[[dict], bool] = lambda x: True,
39+
):
4740
self.n = n
4841
self.metric_fn = metric_fn
4942
self.path = Path(path)
@@ -188,9 +181,11 @@ def absorb_batchNorm(model_target, model_original):
188181
if layer.__class__.__name__ == 'Functional':
189182
absorb_batchNorm(layer, model_original.get_layer(layer.name))
190183
continue
191-
if (isinstance(layer, Dense) or isinstance(layer, Conv)) and \
192-
len(nodes := model_original.get_layer(layer.name)._outbound_nodes) > 0 and \
193-
isinstance(nodes[0].outbound_layer, keras.layers.BatchNormalization):
184+
if (
185+
(isinstance(layer, Dense) or isinstance(layer, Conv))
186+
and len(nodes := model_original.get_layer(layer.name)._outbound_nodes) > 0
187+
and isinstance(nodes[0].outbound_layer, keras.layers.BatchNormalization)
188+
):
194189
_gamma, _beta, _mu, _var = model_original.get_layer(layer.name)._outbound_nodes[0].outbound_layer.get_weights()
195190
_ratio = _gamma / np.sqrt(0.001 + _var)
196191
_bias = -_gamma * _mu / np.sqrt(0.001 + _var) + _beta
@@ -213,7 +208,7 @@ def absorb_batchNorm(model_target, model_original):
213208
weights = layer.get_weights()
214209
new_weights = model_original.get_layer(layer.name).get_weights()
215210
l = len(new_weights)
216-
layer.set_weights([*new_weights, *weights[l:]][:len(weights)])
211+
layer.set_weights([*new_weights, *weights[l:]][: len(weights)])
217212

218213

219214
def set_seed(seed):
@@ -225,9 +220,10 @@ def set_seed(seed):
225220
tf.config.experimental.enable_op_determinism()
226221

227222

228-
import h5py as h5
229223
import json
230224

225+
import h5py as h5
226+
231227

232228
def get_best_ckpt(save_path: Path, take_min=False):
233229
ckpts = list(save_path.glob('*.h5'))
@@ -245,13 +241,14 @@ def rank(ckpt: Path):
245241

246242

247243
class PeratoFront(keras.callbacks.Callback):
248-
def __init__(self,
249-
path: str | Path,
250-
fname_format: str,
251-
metrics_names: list[str],
252-
sides: list[int],
253-
cond_fn: Callable[[dict], bool] = lambda x: True,
254-
):
244+
def __init__(
245+
self,
246+
path: str | Path,
247+
fname_format: str,
248+
metrics_names: list[str],
249+
sides: list[int],
250+
cond_fn: Callable[[dict], bool] = lambda x: True,
251+
):
255252
self.path = Path(path)
256253
self.fname_format = fname_format
257254
os.makedirs(path, exist_ok=True)

part4.1_HG_quantization.ipynb

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575
"classes = np.load('classes.npy', allow_pickle=True)\n",
7676
"\n",
7777
"# Convert everything to tf.Tensor to avoid casting\n",
78-
"with tf.device('/cpu:0'): # type: ignore\n",
78+
"with tf.device('/cpu:0'): # type: ignore\n",
7979
" _X_train_val = tf.convert_to_tensor(X_train_val, dtype=tf.float32)\n",
8080
" # We don't make explicit y categorical tensor:\n",
8181
" # Use SparseCategoricalCrossentropy loss instead.\n",
@@ -128,13 +128,15 @@
128128
"beta = 3e-6\n",
129129
"# The bigger the beta, the smaller the models is, at the cost of accuracy.\n",
130130
"\n",
131-
"model = Sequential([\n",
132-
" HQuantize(beta=beta),\n",
133-
" HDense(64, activation='relu', beta=beta),\n",
134-
" HDense(32, activation='relu', beta=beta),\n",
135-
" HDense(32, activation='relu', beta=beta),\n",
136-
" HDense(5, beta=beta),\n",
137-
"])"
131+
"model = Sequential(\n",
132+
" [\n",
133+
" HQuantize(beta=beta),\n",
134+
" HDense(64, activation='relu', beta=beta),\n",
135+
" HDense(32, activation='relu', beta=beta),\n",
136+
" HDense(32, activation='relu', beta=beta),\n",
137+
" HDense(5, beta=beta),\n",
138+
" ]\n",
139+
")"
138140
]
139141
},
140142
{
@@ -218,7 +220,7 @@
218220
" opt = Adam(learning_rate=0)\n",
219221
" loss = SparseCategoricalCrossentropy(from_logits=True)\n",
220222
" model.compile(optimizer=opt, loss=loss, metrics=['accuracy'])\n",
221-
" \n",
223+
"\n",
222224
" model.fit(\n",
223225
" _X_train_val,\n",
224226
" _y_train_val,\n",
@@ -227,13 +229,14 @@
227229
" validation_split=0.25,\n",
228230
" shuffle=True,\n",
229231
" callbacks=callbacks,\n",
230-
" verbose=0, # type: ignore\n",
232+
" verbose=0, # type: ignore\n",
231233
" )\n",
232234
" model.save('model_3.1/model.h5')\n",
233235
"else:\n",
234236
" from keras.models import load_model\n",
237+
"\n",
235238
" # No need to use custom_objects as the custom layers are already registered\n",
236-
" model:keras.Model = load_model('model_3.1/model.h5') # type: ignore\n"
239+
" model: keras.Model = load_model('model_3.1/model.h5') # type: ignore"
237240
]
238241
},
239242
{
@@ -256,6 +259,7 @@
256259
"outputs": [],
257260
"source": [
258261
"from HGQ import trace_minmax, to_proxy_model\n",
262+
"\n",
259263
"trace_minmax(model, X_train_val)"
260264
]
261265
},
@@ -303,9 +307,7 @@
303307
"import hls4ml\n",
304308
"import plotting\n",
305309
"\n",
306-
"hls_model = hls4ml.converters.convert_from_keras_model(\n",
307-
" proxy, output_dir='model_3.1/hls4ml_prj', part='xcu250-figd2104-2L-e'\n",
308-
")\n",
310+
"hls_model = hls4ml.converters.convert_from_keras_model(proxy, output_dir='model_3.1/hls4ml_prj', part='xcu250-figd2104-2L-e')\n",
309311
"hls_model.compile()\n",
310312
"\n",
311313
"X_test = np.ascontiguousarray(X_test)\n",

0 commit comments

Comments
 (0)