Spaces:
Sleeping
Sleeping
| #!/usr/bin/env python | |
| # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved | |
| import pickle as pkl | |
| import sys | |
| import torch | |
| """ | |
| Usage: | |
| # download pretrained swin model: | |
| wget https://github.com/SwinTransformer/storage/releases/download/v1.0.0/swin_tiny_patch4_window7_224.pth | |
| # run the conversion | |
| ./convert-pretrained-model-to-d2.py swin_tiny_patch4_window7_224.pth swin_tiny_patch4_window7_224.pkl | |
| # Then, use swin_tiny_patch4_window7_224.pkl with the following changes in config: | |
| MODEL: | |
| WEIGHTS: "/path/to/swin_tiny_patch4_window7_224.pkl" | |
| INPUT: | |
| FORMAT: "RGB" | |
| """ | |
| if __name__ == "__main__": | |
| input = sys.argv[1] | |
| obj = torch.load(input, map_location="cpu")["model"] | |
| # Clean unused convnext weight | |
| if "norm.weight" in obj: | |
| del obj["norm.weight"] | |
| if "norm.bias" in obj: | |
| del obj["norm.bias"] | |
| res = {"model": obj, "__author__": "third_party", "matching_heuristics": True} | |
| with open(sys.argv[2], "wb") as f: | |
| pkl.dump(res, f) |