Skip to content
This repository was archived by the owner on Mar 31, 2025. It is now read-only.

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
Contributor

@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 subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cannot run imagenet_pretrained_vgg example

2 participants