Skip to content
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

APIs to import weights from external frameworks. #148

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

david-berthelot
Copy link
Contributor

@david-berthelot david-berthelot commented Nov 18, 2020

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:
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),
}

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),
}
```
Copy link
Collaborator

@kihyuks kihyuks left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

  1. One minor comment on the folder and file names:
    vgg_pytorch/pytorch_vgg.py --> maybe better to make them consistent?

  2. no separate test example?

@david-berthelot
Copy link
Contributor Author

LGTM.

  1. One minor comment on the folder and file names:
    vgg_pytorch/pytorch_vgg.py --> maybe better to make them consistent?
  2. no separate test example?

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.

@aterzis-google aterzis-google linked an issue Dec 1, 2020 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Cannot run imagenet_pretrained_vgg example
2 participants