I wrote a python script that given a JSON and a list of .npy files, reads each file and prints the numpy arrays. I like to convert it into Javascript but all my attempts so far fails. What is the proper way to convert it?
Here is my Python script:
import numpy as np
import json
with open('model_weights.json', 'r') as f:
json_data = json.load(f)
for layer_name, layer_data in json_data.items():
print(f"Layer: {layer_name}")
weight_file = layer_data['weight']
weight_data = np.load(weight_file)
print("Weight:")
print(weight_data)
# Read and print the bias data
bias_file = layer_data['bias']
bias_data = np.load(bias_file)
print("Bias:")
print(bias_data)
print()
Which prints like this:
Layer: conv_mid.0
Weight:
[[[[ 5.11894599e-02 6.01319224e-02 2.16415059e-02]
[-5.73022485e-01 6.34898171e-02 -5.68771400e-02]
[-4.38963100e-02 -9.82442126e-02 2.19287891e-02]]
...
...
Bias:
[-0.00059052 0.00416504 -0.00031364 0.00533698 -0.01785151 -0.00343998
0.00104868 0.00327141]
...
...
Here is the JSON:
{
"conv_mid.0": {
"weight": "conv_mid.0_weight.npy",
"bias": "conv_mid.0_bias.npy"
},
"conv_mid.1": {
"weight": "conv_mid.1_weight.npy",
"bias": "conv_mid.1_bias.npy"
},
"conv_mid.2": {
"weight": "conv_mid.2_weight.npy",
"bias": "conv_mid.2_bias.npy"
},
"conv_mid.3": {
"weight": "conv_mid.3_weight.npy",
"bias": "conv_mid.3_bias.npy"
}
}