ChromBPNet-ONNX / conversion /trial-tensorflow_reference.py
okunator's picture
Preserve pinned ChromBPNet source metadata and conversion provenance
7fb826c verified
Raw History Blame Contribute Delete
1.51 kB
"""Evaluate the original reviewed Keras graphs without executing serialized code."""
import json
from pathlib import Path
import h5py
import numpy as np
import tensorflow as tf
import tf_keras as keras
from sequence_designer.models.keras_topology import canonical_topology
tf.config.set_visible_devices([], 'GPU')
tf.config.threading.set_intra_op_parallelism_threads(4)
tf.config.threading.set_inter_op_parallelism_threads(1)
root = Path('.cache/chrombpnet-onnx')
report = json.loads((root / 'cpu-report.json').read_text())
for row in report['models']:
name = row['name']
if name == 'k562':
path = root / 'models/k562.h5'
else:
path = next((Path('apps/sequence-designer/.cache/tcell-qualification') / name / 'chrombpnet/fold_0').glob('*.h5'))
with h5py.File(path) as file:
config = json.loads(file.attrs['model_config'])
assert canonical_topology(config) == 'b3702f28baf6f1b3db8344ba2a0b248e2c7b93fdc99d097c4109a3e546e6dac3'
model = keras.models.model_from_json(json.dumps(config))
model.load_weights(str(path))
ref = json.loads((root / 'browser' / (name + '-reference.json')).read_text())
x = np.array(ref['cases'][-1]['sequence'], dtype=np.float32).reshape(6, 4, 2114)
logits, counts = model(x.transpose(0, 2, 1), training=False)
np.savez(root / (name + '-tensorflow.npz'), input=x, logits=logits.numpy()[:, None, :], counts=counts.numpy())
print(name, tf.__version__, keras.__version__, flush=True)
keras.backend.clear_session()