Update models.py
Browse files
models.py
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
|
|
| 1 |
import torch
|
| 2 |
import torch.nn as nn
|
| 3 |
from transformers import PreTrainedModel, PretrainedConfig, AutoConfig
|
| 4 |
from transformers.modeling_outputs import SequenceClassifierOutput
|
| 5 |
-
from huggingface_hub import hf_hub_download
|
| 6 |
-
import os
|
| 7 |
|
| 8 |
-
#
|
| 9 |
DIM_ECG = 46
|
| 10 |
DIM_RR = 62
|
| 11 |
DIM_EDA = 24
|
|
@@ -22,24 +21,16 @@ class TeacherNet(nn.Module):
|
|
| 22 |
hidden = TEACHER_FEAT_DIM
|
| 23 |
dropout = 0.4
|
| 24 |
self.ecg_encoder = nn.Sequential(
|
| 25 |
-
nn.Linear(DIM_ECG, hidden),
|
| 26 |
-
nn.ReLU(inplace=True),
|
| 27 |
-
nn.Dropout(p=dropout)
|
| 28 |
)
|
| 29 |
self.rr_encoder = nn.Sequential(
|
| 30 |
-
nn.Linear(DIM_RR, hidden),
|
| 31 |
-
nn.ReLU(inplace=True),
|
| 32 |
-
nn.Dropout(p=dropout)
|
| 33 |
)
|
| 34 |
self.eda_encoder = nn.Sequential(
|
| 35 |
-
nn.Linear(DIM_EDA, hidden),
|
| 36 |
-
nn.ReLU(inplace=True),
|
| 37 |
-
nn.Dropout(p=dropout)
|
| 38 |
)
|
| 39 |
self.video_encoder = nn.Sequential(
|
| 40 |
-
nn.Linear(DIM_VIDEO, hidden),
|
| 41 |
-
nn.ReLU(inplace=True),
|
| 42 |
-
nn.Dropout(p=dropout)
|
| 43 |
)
|
| 44 |
self.classifier = nn.Sequential(
|
| 45 |
nn.Linear(4 * hidden, hidden),
|
|
@@ -87,6 +78,8 @@ class StudentNet(nn.Module):
|
|
| 87 |
logits = self.classifier(feat)
|
| 88 |
return logits, feat
|
| 89 |
|
|
|
|
|
|
|
| 90 |
class StressConfig(PretrainedConfig):
|
| 91 |
model_type = "audio-classification"
|
| 92 |
def __init__(self, **kwargs):
|
|
@@ -114,7 +107,6 @@ class StudentForAudioClassification(PreTrainedModel):
|
|
| 114 |
trust_remote_code=False,
|
| 115 |
**kwargs
|
| 116 |
):
|
| 117 |
-
# 1) Config ๋ก๋
|
| 118 |
config = AutoConfig.from_pretrained(
|
| 119 |
pretrained_model_name_or_path,
|
| 120 |
trust_remote_code=trust_remote_code,
|
|
@@ -122,33 +114,16 @@ class StudentForAudioClassification(PreTrainedModel):
|
|
| 122 |
)
|
| 123 |
model = cls(config)
|
| 124 |
|
| 125 |
-
#
|
| 126 |
if os.path.isdir(pretrained_model_name_or_path):
|
| 127 |
-
# safetensors ์ฐ์ , ์์ผ๋ฉด pytorch_model.bin
|
| 128 |
-
safetensor_path = os.path.join(pretrained_model_name_or_path, "model.safetensors")
|
| 129 |
bin_path = os.path.join(pretrained_model_name_or_path, "pytorch_model.bin")
|
| 130 |
-
if os.path.isfile(safetensor_path):
|
| 131 |
-
from safetensors.torch import load_file as safetensors_load
|
| 132 |
-
sd = safetensors_load(safetensor_path)
|
| 133 |
-
else:
|
| 134 |
-
sd = torch.load(bin_path, map_location="cpu", weights_only=True)
|
| 135 |
else:
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
)
|
| 143 |
-
sd = safetensors_load(safetensor_path)
|
| 144 |
-
except Exception:
|
| 145 |
-
bin_path = hf_hub_download(
|
| 146 |
-
repo_id=pretrained_model_name_or_path,
|
| 147 |
-
filename="pytorch_model.bin"
|
| 148 |
-
)
|
| 149 |
-
sd = torch.load(bin_path, map_location="cpu", weights_only=True)
|
| 150 |
-
|
| 151 |
-
# 3) state_dict prefix
|
| 152 |
prefixed_sd = {f"student.{k}": v for k, v in sd.items()}
|
| 153 |
model.load_state_dict(prefixed_sd, strict=True)
|
| 154 |
return model
|
|
|
|
| 1 |
+
import os
|
| 2 |
import torch
|
| 3 |
import torch.nn as nn
|
| 4 |
from transformers import PreTrainedModel, PretrainedConfig, AutoConfig
|
| 5 |
from transformers.modeling_outputs import SequenceClassifierOutput
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
# ===================== ํ๋์ฝ๋ฉ๋ ์ค์ ========================
|
| 8 |
DIM_ECG = 46
|
| 9 |
DIM_RR = 62
|
| 10 |
DIM_EDA = 24
|
|
|
|
| 21 |
hidden = TEACHER_FEAT_DIM
|
| 22 |
dropout = 0.4
|
| 23 |
self.ecg_encoder = nn.Sequential(
|
| 24 |
+
nn.Linear(DIM_ECG, hidden), nn.ReLU(inplace=True), nn.Dropout(p=dropout)
|
|
|
|
|
|
|
| 25 |
)
|
| 26 |
self.rr_encoder = nn.Sequential(
|
| 27 |
+
nn.Linear(DIM_RR, hidden), nn.ReLU(inplace=True), nn.Dropout(p=dropout)
|
|
|
|
|
|
|
| 28 |
)
|
| 29 |
self.eda_encoder = nn.Sequential(
|
| 30 |
+
nn.Linear(DIM_EDA, hidden), nn.ReLU(inplace=True), nn.Dropout(p=dropout)
|
|
|
|
|
|
|
| 31 |
)
|
| 32 |
self.video_encoder = nn.Sequential(
|
| 33 |
+
nn.Linear(DIM_VIDEO, hidden), nn.ReLU(inplace=True), nn.Dropout(p=dropout)
|
|
|
|
|
|
|
| 34 |
)
|
| 35 |
self.classifier = nn.Sequential(
|
| 36 |
nn.Linear(4 * hidden, hidden),
|
|
|
|
| 78 |
logits = self.classifier(feat)
|
| 79 |
return logits, feat
|
| 80 |
|
| 81 |
+
# ==== Transformers Compatibility ==== #
|
| 82 |
+
|
| 83 |
class StressConfig(PretrainedConfig):
|
| 84 |
model_type = "audio-classification"
|
| 85 |
def __init__(self, **kwargs):
|
|
|
|
| 107 |
trust_remote_code=False,
|
| 108 |
**kwargs
|
| 109 |
):
|
|
|
|
| 110 |
config = AutoConfig.from_pretrained(
|
| 111 |
pretrained_model_name_or_path,
|
| 112 |
trust_remote_code=trust_remote_code,
|
|
|
|
| 114 |
)
|
| 115 |
model = cls(config)
|
| 116 |
|
| 117 |
+
# ๐ข [ํต์ฌ] ๊ฒฝ๋ก๊ฐ ํด๋(๋ก์ปฌ)๋ฉด ์ง์ ํ์ผ ์ฐพ๊ธฐ
|
| 118 |
if os.path.isdir(pretrained_model_name_or_path):
|
|
|
|
|
|
|
| 119 |
bin_path = os.path.join(pretrained_model_name_or_path, "pytorch_model.bin")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
else:
|
| 121 |
+
from huggingface_hub import hf_hub_download
|
| 122 |
+
bin_path = hf_hub_download(
|
| 123 |
+
repo_id=pretrained_model_name_or_path,
|
| 124 |
+
filename="pytorch_model.bin",
|
| 125 |
+
)
|
| 126 |
+
sd = torch.load(bin_path, map_location="cpu", weights_only=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
prefixed_sd = {f"student.{k}": v for k, v in sd.items()}
|
| 128 |
model.load_state_dict(prefixed_sd, strict=True)
|
| 129 |
return model
|