-
-
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 4 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
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.
anushka-cseatmnc marked this conversation as resolved.
Show resolved
Hide resolved
|
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!") | ||
|
anushka-cseatmnc marked this conversation as resolved.
Show resolved
Hide resolved
|
Large diffs are not rendered by default.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rerun the scripts, these file are not generated anymore
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Verified weights_tf_free.h5 is up to date
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The script generates
weights.h5
. It does not generateweights_tf_free.h5
orweights_tf_free.npz
.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
delete these files and regenerate them
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@robertbastian I have deleted the outdated weights_tf_free.h5 as requested and regenerated the necessary files. The new weights_tf_free.h5 files are now up-to-date. Kindly review the changes again. here is screenshot i'm attaching for reference - If there is any more changes required kindly let me know .

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Again, this is not the output of the
save_model
method. In fact, you have reverted the changes to that method.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you give me insights what I'm exactly supposed to do??
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this what you r asking about ?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sffc @robertbastian please review changes . And guidence will be helpful for further changes.