TorchGeo
isaaccorley commited on
Commit
b79ade3
·
verified ·
1 Parent(s): e4adb0c

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +46 -3
README.md CHANGED
@@ -1,3 +1,46 @@
1
- ---
2
- license: mit
3
- ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: mit
3
+ library_name: torchgeo
4
+ ---
5
+
6
+
7
+ Model Weights extracted below:
8
+
9
+ ```python
10
+ import os
11
+ import hashlib
12
+
13
+ import torch
14
+ import timm
15
+
16
+
17
+ # download weights
18
+ url = "https://huggingface.co/eplekh/secoeco/resolve/main/ablation_B12_weights.ckpt"
19
+ ckpt = torch.hub.load_state_dict_from_url(url, map_location="cpu", progress=True)
20
+ arch, image_size, bands = ckpt["hyper_parameters"]["arch"], ckpt["hyper_parameters"]["in_size"], ckpt["hyper_parameters"]["bands"]
21
+ print(arch, image_size, bands)
22
+
23
+ # Bands correspond to B9 from https://github.com/PlekhanovaElena/ssl4eco/blob/7445e048035f7ae31c0eb45e1ed8426c9989fe56/pretraining/pretrain_seco_3heads.py#L220
24
+ bands = ['B1', 'B2', 'B3', 'B4', 'B5', 'B6', 'B7', 'B8', 'B8A', 'B9', 'B11', 'B12']
25
+
26
+ # map weights to timm and torchvision compatible
27
+ layer_mapping = {
28
+ "0" : "conv1",
29
+ "1" : "bn1",
30
+ "4" : "layer1",
31
+ "5" : "layer2",
32
+ "6" : "layer3",
33
+ "7" : "layer4",
34
+ }
35
+ state_dict = {k.replace("encoder_q.", ""): v for k, v in ckpt["state_dict"].items() if k.startswith("encoder_q.")}
36
+ state_dict = {k.replace(k.split(".")[0], layer_mapping[k.split(".")[0]]): v for k, v in state_dict.items()}
37
+
38
+ model = timm.create_model("resnet50", pretrained=False, in_chans=len(bands), num_classes=0)
39
+ model.load_state_dict(state_dict, strict=True)
40
+
41
+ # save and compute hash
42
+ filename = "resnet50_sentinel2_all_seco_eco.pth"
43
+ torch.save(model.state_dict(), filename)
44
+ md5 = hashlib.md5(open(filename, "rb").read()).hexdigest()[:8]
45
+ os.rename(filename, filename.replace(".pth", f"-{md5}.pth"))
46
+ ```