This repository was archived by the owner on Mar 31, 2025. It is now read-only.
APIs to import weights from external frameworks.#148
Draft
david-berthelot wants to merge 3 commits intomasterfrom
Draft
APIs to import weights from external frameworks.#148david-berthelot wants to merge 3 commits intomasterfrom
david-berthelot wants to merge 3 commits intomasterfrom
Conversation
Demonstrated on PyTorch VGG.
The core function is objax.util.convert.import_weights:
- It takes a variable collection `target_vc` (basically the model variables for which to set the weights).
- Then `source_numpy` is a dictionary of numpy values and their names (in a possibly different naming convention than Objax', say for example PyTorch).
- `source_names` maps objax names to the `source_numpy` names, so that from a variable we can find the numpy value to set its weight.
- `numpy_convert` is a dictionary that maps module variables names to actions (functions). The actions are used to perform conversions (like transpositions or reshaping for example). Here are some examples:
```python
ARRAY_CONVERT = {
'(BatchNorm2D).beta': assign(),
'(BatchNorm2D).gamma': assign,
'(BatchNorm2D).running_mean': assign,
'(BatchNorm2D).running_var': assign,
'(Conv2D).b': assign,
'(Conv2D).w': lambda x, y: assign(x, y.transpose((2, 3, 1, 0))),
'(Linear).b': assign,
'(Linear).w': lambda x, y: assign(x, y.T),
}
```
kihyuks
reviewed
Nov 19, 2020
Contributor
kihyuks
left a comment
There was a problem hiding this comment.
LGTM.
-
One minor comment on the folder and file names:
vgg_pytorch/pytorch_vgg.py--> maybe better to make them consistent? -
no separate test example?
Contributor
Author
Thanks, it's just a draft, like I didn't do a lot of things yet, like docs/tests. It's just to explore a design idea. Whatever we decide in the end, I'll make everything polished. |
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
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
Demonstrated on PyTorch VGG.
The core function is
objax.util.convert.import_weights:target_vc(basically the model variables for which to set the weights).source_numpyis a dictionary of numpy values and their names (in a possibly different naming convention than Objax', say for example PyTorch).source_namesmaps objax names to thesource_numpynames, so that from a variable we can find the numpy value to set its weight.numpy_convertis a dictionary that maps module variables names to actions (functions). The actions are used to perform conversions (like transpositions or reshaping for example). Here are some examples: