-
-
Notifications
You must be signed in to change notification settings - Fork 14
Added TensorFlow-free npy and h5 weight conversions #36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
anushka-cseatmnc
wants to merge
14
commits into
unicode-org:main
Choose a base branch
from
anushka-cseatmnc:fix-h5-weights
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
210dbc0
Added TensorFlow-free npy and h5 weight conversions
anushka-cseatmnc c4382fb
Added .h5 model saving in save_model()
anushka-cseatmnc 6368fa4
Fixed save_model function in word_segmenter.py
anushka-cseatmnc 7048ff7
Refactored save_model function in word_segmenter.py to inline weight β¦
anushka-cseatmnc daac099
Fixed line endings (LF) in word_segmenter.py
anushka-cseatmnc a2f6686
Removed unused import: convert_weights
anushka-cseatmnc b7bb378
Ignore virtual environment
anushka-cseatmnc aef71fb
Deleted outdated convert_weights.py
anushka-cseatmnc 1b8c94b
Verified weights_tf_free.h5 is up to date
anushka-cseatmnc 12ea6d3
Deleted outdated files and regenerated weights_tf_free files
anushka-cseatmnc f9f9e1f
Restored convert_weights.py
anushka-cseatmnc 0371915
Fix h5 weights issue
anushka-cseatmnc 392b1bc
Fix: Added H5 model saving and TensorFlow-free conversion
anushka-cseatmnc a50a857
Fully inlined save_model
anushka-cseatmnc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,3 +14,5 @@ Models/Burmese_temp_genvec/ | |
Models/Burmese_model4_version2/ | ||
Models/Other/ | ||
*~ | ||
venv/ | ||
convert_weights.py |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import os | ||
import numpy as np | ||
import tensorflow as tf # Needed to handle TF tensors | ||
import h5py | ||
|
||
def convert_to_numpy(value): | ||
""" | ||
Convert TensorFlow tensors/variables to NumPy arrays (float32). | ||
Ensures we remove any TensorFlow-specific data. | ||
""" | ||
if isinstance(value, tf.Tensor) or isinstance(value, tf.Variable): | ||
return value.numpy().astype(np.float32) | ||
elif isinstance(value, np.ndarray) and np.issubdtype(value.dtype, np.number): | ||
return value.astype(np.float32) | ||
else: | ||
return None # Ignore non-numeric data | ||
|
||
def convert_weights(npy_path): | ||
"""Convert `weights.npy` to a TensorFlow-free HDF5 format.""" | ||
if not os.path.exists(npy_path): | ||
print(f"β Error: {npy_path} not found!") | ||
return | ||
|
||
h5_path = npy_path.replace(".npy", "_tf_free.h5") | ||
|
||
# Load the weights | ||
print(f"π Loading {npy_path}...") | ||
weights = np.load(npy_path, allow_pickle=True) | ||
|
||
# Convert all elements to NumPy arrays (remove TensorFlow dtypes) | ||
converted_weights = [convert_to_numpy(w) for w in weights if convert_to_numpy(w) is not None] | ||
|
||
# Save to HDF5 format | ||
with h5py.File(h5_path, "w") as hf: | ||
for i, w in enumerate(converted_weights): | ||
hf.create_dataset(f"weight_{i}", data=w) | ||
|
||
print(f"β Converted: {npy_path} -> {h5_path}") | ||
|
||
if __name__ == "__main__": | ||
# Search for all `weights.npy` files and convert them | ||
for root, _, files in os.walk("."): | ||
for file in files: | ||
if file == "weights.npy": | ||
convert_weights(os.path.join(root, file)) | ||
|
||
print("π All weight files converted successfully!") | ||
|
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.