Commit
·
9cf6c45
1
Parent(s):
f8cea41
initial push
Browse files- .idea/CSATv2.iml +1 -1
- .idea/misc.xml +1 -1
- CSAT_ImageNet.bin +3 -0
- CSAT_RCKD.bin +3 -0
- CSAT_v2_ImageNet.bin +3 -0
- ResNet18_RCKD.bin +3 -0
- config.json +18 -0
- image_processor.json +15 -0
- modeling_csatv2.py +113 -0
- tar2bin.py +23 -0
.idea/CSATv2.iml
CHANGED
|
@@ -2,7 +2,7 @@
|
|
| 2 |
<module type="PYTHON_MODULE" version="4">
|
| 3 |
<component name="NewModuleRootManager">
|
| 4 |
<content url="file://$MODULE_DIR$" />
|
| 5 |
-
<orderEntry type="jdk" jdkName="
|
| 6 |
<orderEntry type="sourceFolder" forTests="false" />
|
| 7 |
</component>
|
| 8 |
<component name="PyDocumentationSettings">
|
|
|
|
| 2 |
<module type="PYTHON_MODULE" version="4">
|
| 3 |
<component name="NewModuleRootManager">
|
| 4 |
<content url="file://$MODULE_DIR$" />
|
| 5 |
+
<orderEntry type="jdk" jdkName="alpha_evolve" jdkType="Python SDK" />
|
| 6 |
<orderEntry type="sourceFolder" forTests="false" />
|
| 7 |
</component>
|
| 8 |
<component name="PyDocumentationSettings">
|
.idea/misc.xml
CHANGED
|
@@ -3,5 +3,5 @@
|
|
| 3 |
<component name="Black">
|
| 4 |
<option name="sdkName" value="Python 3.6" />
|
| 5 |
</component>
|
| 6 |
-
<component name="ProjectRootManager" version="2" project-jdk-name="
|
| 7 |
</project>
|
|
|
|
| 3 |
<component name="Black">
|
| 4 |
<option name="sdkName" value="Python 3.6" />
|
| 5 |
</component>
|
| 6 |
+
<component name="ProjectRootManager" version="2" project-jdk-name="alpha_evolve" project-jdk-type="Python SDK" />
|
| 7 |
</project>
|
CSAT_ImageNet.bin
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:81369c70ebd59a707a20d4e0c354f92a7056b1ac61ce41d9765d97fad6c36a37
|
| 3 |
+
size 12383563
|
CSAT_RCKD.bin
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:b4f44c33e0f30446bb28f41a0ef27d4f6a4018df9975a69795f598bdf0944036
|
| 3 |
+
size 12381963
|
CSAT_v2_ImageNet.bin
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:104253a41517faf75704853c207082c20b31264a7e9566a9a1ca22a9e088a729
|
| 3 |
+
size 44535575
|
ResNet18_RCKD.bin
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:243dc88aa8390386ef558f4f1202de86c6b2c5132722d080bac0380d9dcefa01
|
| 3 |
+
size 46833867
|
config.json
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"_name_or_path": "Hyunil/CSATv2",
|
| 3 |
+
"model_type": "csatv2",
|
| 4 |
+
"architectures": ["CSATv2ForImageClassification"],
|
| 5 |
+
"image_size": 512,
|
| 6 |
+
"num_channels": 3,
|
| 7 |
+
"num_labels": 1000,
|
| 8 |
+
"drop_path_rate": 0.0,
|
| 9 |
+
"head_init_scale": 1.0,
|
| 10 |
+
|
| 11 |
+
"torch_dtype": "float32",
|
| 12 |
+
|
| 13 |
+
// Auto 클래스들이 이 레포 안의 어떤 코드를 써야 하는지 알려주는 부분
|
| 14 |
+
"auto_map": {
|
| 15 |
+
"AutoConfig": "modeling_csatv2.CSATv2Config",
|
| 16 |
+
"AutoModelForImageClassification": "modeling_csatv2.CSATv2ForImageClassification"
|
| 17 |
+
}
|
| 18 |
+
}
|
image_processor.json
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"feature_extractor_type": "ImageFeatureExtraction",
|
| 3 |
+
|
| 4 |
+
"do_resize": true,
|
| 5 |
+
"size": 512,
|
| 6 |
+
|
| 7 |
+
"do_center_crop": false,
|
| 8 |
+
|
| 9 |
+
"do_rescale": true,
|
| 10 |
+
"rescale_factor": 0.00392156862745098,
|
| 11 |
+
|
| 12 |
+
"do_normalize": true,
|
| 13 |
+
"image_mean": [0.485, 0.456, 0.406],
|
| 14 |
+
"image_std": [0.229, 0.224, 0.225]
|
| 15 |
+
}
|
modeling_csatv2.py
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# modeling_csatv2.py
|
| 2 |
+
#
|
| 3 |
+
# Hugging Face Transformers용 CSATv2 래퍼
|
| 4 |
+
# - Config: CSATv2Config
|
| 5 |
+
# - Model: CSATv2ForImageClassification
|
| 6 |
+
#
|
| 7 |
+
# 사용 예:
|
| 8 |
+
# from transformers import AutoImageProcessor, AutoModelForImageClassification
|
| 9 |
+
# model = AutoModelForImageClassification.from_pretrained(
|
| 10 |
+
# "Hyunil/CSATv2", trust_remote_code=True
|
| 11 |
+
# )
|
| 12 |
+
|
| 13 |
+
from typing import Optional, Union, Tuple
|
| 14 |
+
|
| 15 |
+
import torch
|
| 16 |
+
import torch.nn as nn
|
| 17 |
+
|
| 18 |
+
from transformers import PreTrainedModel, PretrainedConfig
|
| 19 |
+
from transformers.modeling_outputs import ImageClassifierOutput
|
| 20 |
+
|
| 21 |
+
from .CSATv2 import CSATv2 # 네가 올린 백본 클래스 사용
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
class CSATv2Config(PretrainedConfig):
|
| 25 |
+
model_type = "csatv2"
|
| 26 |
+
|
| 27 |
+
def __init__(
|
| 28 |
+
self,
|
| 29 |
+
image_size: int = 224,
|
| 30 |
+
num_channels: int = 3,
|
| 31 |
+
num_labels: int = 1000,
|
| 32 |
+
drop_path_rate: float = 0.0,
|
| 33 |
+
head_init_scale: float = 1.0,
|
| 34 |
+
**kwargs,
|
| 35 |
+
):
|
| 36 |
+
"""
|
| 37 |
+
HF가 사용할 설정 값들.
|
| 38 |
+
"""
|
| 39 |
+
super().__init__(num_labels=num_labels, **kwargs)
|
| 40 |
+
self.image_size = image_size
|
| 41 |
+
self.num_channels = num_channels
|
| 42 |
+
self.drop_path_rate = drop_path_rate
|
| 43 |
+
self.head_init_scale = head_init_scale
|
| 44 |
+
|
| 45 |
+
# label 매핑이 안 들어오면 기본값 생성
|
| 46 |
+
if self.id2label is None or self.label2id is None:
|
| 47 |
+
self.id2label = {i: f"LABEL_{i}" for i in range(num_labels)}
|
| 48 |
+
self.label2id = {v: k for k, v in self.id2label.items()}
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
class CSATv2ForImageClassification(PreTrainedModel):
|
| 52 |
+
"""
|
| 53 |
+
Hugging Face용 ImageNet 분류 모델 래퍼
|
| 54 |
+
- backbone: CSATv2 (네가 구현한 모델)
|
| 55 |
+
- forward(pixel_values, labels=None)
|
| 56 |
+
"""
|
| 57 |
+
|
| 58 |
+
config_class = CSATv2Config
|
| 59 |
+
|
| 60 |
+
def __init__(self, config: CSATv2Config):
|
| 61 |
+
super().__init__(config)
|
| 62 |
+
self.num_labels = config.num_labels
|
| 63 |
+
|
| 64 |
+
# 네가 만든 CSATv2 백본을 그대로 사용
|
| 65 |
+
self.backbone = CSATv2(
|
| 66 |
+
img_size=config.image_size,
|
| 67 |
+
num_classes=config.num_labels,
|
| 68 |
+
drop_path_rate=config.drop_path_rate,
|
| 69 |
+
head_init_scale=config.head_init_scale,
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
# transformers 권장: 내부 가중치 등록 후 post_init 호출
|
| 73 |
+
self.post_init()
|
| 74 |
+
|
| 75 |
+
def forward(
|
| 76 |
+
self,
|
| 77 |
+
pixel_values: torch.Tensor = None,
|
| 78 |
+
labels: Optional[torch.Tensor] = None,
|
| 79 |
+
output_hidden_states: Optional[bool] = None,
|
| 80 |
+
output_attentions: Optional[bool] = None,
|
| 81 |
+
return_dict: Optional[bool] = None,
|
| 82 |
+
) -> Union[ImageClassifierOutput, Tuple]:
|
| 83 |
+
"""
|
| 84 |
+
Args:
|
| 85 |
+
pixel_values: (batch, 3, H, W), ImageNet 정규화까지 된 이미지
|
| 86 |
+
labels: (batch,) 0~999 class index
|
| 87 |
+
"""
|
| 88 |
+
return_dict = return_dict if return_dict is not None else self.config.use_return_dict
|
| 89 |
+
|
| 90 |
+
if pixel_values is None:
|
| 91 |
+
raise ValueError("You must provide pixel_values")
|
| 92 |
+
|
| 93 |
+
# CSATv2는 이미 logits를 반환함
|
| 94 |
+
logits = self.backbone(pixel_values)
|
| 95 |
+
|
| 96 |
+
loss = None
|
| 97 |
+
if labels is not None:
|
| 98 |
+
loss_fct = nn.CrossEntropyLoss()
|
| 99 |
+
loss = loss_fct(
|
| 100 |
+
logits.view(-1, self.num_labels),
|
| 101 |
+
labels.view(-1),
|
| 102 |
+
)
|
| 103 |
+
|
| 104 |
+
if not return_dict:
|
| 105 |
+
output = (logits,)
|
| 106 |
+
return ((loss,) + output) if loss is not None else output
|
| 107 |
+
|
| 108 |
+
return ImageClassifierOutput(
|
| 109 |
+
loss=loss,
|
| 110 |
+
logits=logits,
|
| 111 |
+
hidden_states=None,
|
| 112 |
+
attentions=None,
|
| 113 |
+
)
|
tar2bin.py
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from collections import OrderedDict
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
ckpt_path = "./CSAT_RCKD.pth.tar"
|
| 6 |
+
ckpt = torch.load(ckpt_path, map_location="cpu", weights_only=False)
|
| 7 |
+
|
| 8 |
+
# 1) state_dict 꺼내기
|
| 9 |
+
# - 보통 {'state_dict': ...} 형태니까 먼저 이걸 시도하고,
|
| 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 |
+
new_k = k[len("module."):]
|
| 17 |
+
else:
|
| 18 |
+
new_k = k
|
| 19 |
+
new_state_dict[new_k] = v
|
| 20 |
+
|
| 21 |
+
# 3) HuggingFace 관례대로 파일명 저장
|
| 22 |
+
torch.save(new_state_dict, "CSAT_RCKD.bin")
|
| 23 |
+
print("saved to pytorch_model.bin")
|