Commit
ยท
9ec0c69
1
Parent(s):
36340c3
initial push
Browse files- convert_and_push.py +0 -0
- pytorch_model.bin +2 -2
- tar2bin.py +19 -16
convert_and_push.py
ADDED
|
File without changes
|
pytorch_model.bin
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
-
size
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:3456c4d1c386b6fc57670f63ae1b8cfaccc3157528c4db99ed8fb8bf048959d4
|
| 3 |
+
size 44539159
|
tar2bin.py
CHANGED
|
@@ -1,23 +1,26 @@
|
|
| 1 |
import torch
|
| 2 |
-
from collections import OrderedDict
|
| 3 |
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
|
| 10 |
-
# - ์๋๋ฉด ๊ทธ๋ฅ ckpt ์ ์ฒด๊ฐ state_dict์ธ ๊ฒฝ์ฐ๋ ์์ด์ fallback
|
| 11 |
-
state_dict = ckpt.get("state_dict", ckpt)
|
| 12 |
-
# 2) DataParallel ์ผ์ผ๋ฉด key ์์ 'module.' ๋ถ์ด์์ ์ ์์ด์ ์ ๊ฑฐ
|
| 13 |
-
new_state_dict = OrderedDict()
|
| 14 |
for k, v in state_dict.items():
|
| 15 |
if k.startswith("module."):
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
new_k = k
|
| 19 |
-
new_state_dict[new_k] = v
|
| 20 |
|
| 21 |
-
#
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import torch
|
|
|
|
| 2 |
|
| 3 |
+
# 1) ์๋ ํ์ต๋ checkpoint ๋ถ๋ฌ์ค๊ธฐ
|
| 4 |
+
ckpt = torch.load("./CSAT_v2_ImageNet.pth.tar", map_location="cpu", weights_only=False)
|
| 5 |
|
| 6 |
+
# 2) state_dict ๊บผ๋ด๊ธฐ (ํฌ๋งท์ ๋ฐ๋ผ ๋ถ๊ธฐ)
|
| 7 |
+
if "state_dict" in ckpt:
|
| 8 |
+
state_dict = ckpt["state_dict"]
|
| 9 |
+
elif "model" in ckpt:
|
| 10 |
+
state_dict = ckpt["model"]
|
| 11 |
+
else:
|
| 12 |
+
state_dict = ckpt
|
| 13 |
|
| 14 |
+
# 3) Distributed / DataParallel์ธ ๊ฒฝ์ฐ "module." ์ ๊ฑฐ
|
| 15 |
+
new_sd = {}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
for k, v in state_dict.items():
|
| 17 |
if k.startswith("module."):
|
| 18 |
+
k = k[len("module."):]
|
| 19 |
+
new_sd[k] = v
|
|
|
|
|
|
|
| 20 |
|
| 21 |
+
# 4) HF ๋ํผ์ฉ์ผ๋ก "backbone." prefix ๋ถ์ด๊ธฐ
|
| 22 |
+
wrapped_sd = {f"backbone.{k}": v for k, v in new_sd.items()}
|
| 23 |
+
|
| 24 |
+
# 5) HF์์ ์ฌ์ฉํ ํ์ผ๋ก ์ ์ฅ
|
| 25 |
+
torch.save(wrapped_sd, "pytorch_model.bin")
|
| 26 |
+
print("saved HF-style weights to pytorch_model.bin")
|