Sanath2709 commited on
Commit
24fe966
·
verified ·
1 Parent(s): ea9fe93

Upload folder using huggingface_hub

Browse files
README.md ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Hugging Face Deployment Checklist
2
+
3
+ Welcome to the `hf_deployment` folder! This directory contains everything needed to wrap your `ChronoGridFusionNet` model into a native Hugging Face model and push it to the Hub.
4
+
5
+ ## What is in here?
6
+ 1. `configuration_chronogrid.py`: The Hugging Face config class (stores hyperparameters like image size and num classes).
7
+ 2. `modeling_chronogrid.py`: Your PyTorch architecture, wrapped in `PreTrainedModel`.
8
+ 3. `push_to_hf.py`: The script you will run to package everything and push it to your Hugging Face account.
9
+
10
+ ## What you need to do BEFORE publishing:
11
+
12
+ ### 1. Authenticate with Hugging Face
13
+ You need to be logged into your Hugging Face account on your terminal so the script can push files.
14
+ - Open your terminal and run: `huggingface-cli login`
15
+ - Paste your Hugging Face **Access Token** (you can generate one with `write` permissions in your account Settings -> Access Tokens).
16
+
17
+ ### 2. Update the `push_to_hf.py` script
18
+ Open `push_to_hf.py` and modify two things:
19
+ 1. **Line 13:** Change `repo_id = "your-username/chronogrid-fusionnet"` to your actual HF username and desired repo name.
20
+ 2. **Line 21:** Ensure `model_path = '../models/Chronogrid_fold5.pt'` points to the exact `.pt` file you saved after training.
21
+
22
+ ### 3. Provide your Preprocessing logic
23
+ Right now, if someone downloads your model, they won't know how to turn a raw grid signal into a 227x227 image.
24
+ - **Action:** Create a `preprocess.py` file in this folder. It should contain whatever Python code you used (like S-Transform functions) to generate the `.jpg` heatmaps you used for training.
25
+ - You will upload this `preprocess.py` file manually to your Hugging Face repo later, or add an `api.upload_file(...)` line in `push_to_hf.py` for it.
26
+
27
+ ### 4. Run the Push Script!
28
+ When you are ready:
29
+ ```bash
30
+ cd /Users/sanathbs/02_College_UG/IEEE-9bus/hf_deployment
31
+ pip install transformers huggingface_hub
32
+ python push_to_hf.py
33
+ ```
34
+
35
+ ### 5. Finalize the Model Card (README.md)
36
+ Once the model is pushed, go to the model's page on huggingface.co and edit the `README.md` (Model Card).
37
+ You should explain:
38
+ - **What this is:** Fault detection for WSCC 9-Bus system.
39
+ - **How to use it:** Provide a small code snippet showing how to use `AutoModel.from_pretrained(...)`.
40
+ - **Metrics:** Paste your final test accuracy and confusion matrix stats.
config.json ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "architectures": [
3
+ "ChronoGridModelForSequenceClassification"
4
+ ],
5
+ "dtype": "float32",
6
+ "img_size": 227,
7
+ "model_type": "chronogrid",
8
+ "num_classes": 11,
9
+ "transformers_version": "5.12.1"
10
+ }
configuration_chronogrid.py ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import PretrainedConfig
2
+
3
+ class ChronoGridConfig(PretrainedConfig):
4
+ model_type = "chronogrid"
5
+
6
+ def __init__(
7
+ self,
8
+ img_size=227,
9
+ num_classes=11,
10
+ **kwargs
11
+ ):
12
+ self.img_size = img_size
13
+ self.num_classes = num_classes
14
+ super().__init__(**kwargs)
model.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:3089767ab1ad0620926a5c9c179f53f228a9c6beb1b0a2e9b397a34aef4b411f
3
+ size 5098476
modeling_chronogrid.py ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torch.nn as nn
3
+ from transformers import PreTrainedModel
4
+ from configuration_chronogrid import ChronoGridConfig
5
+
6
+ def make_cnn_backbone(img_size):
7
+ return nn.Sequential(
8
+ # Block 1 — kernel-3 (captures short transients)
9
+ nn.Conv1d(img_size, 64, kernel_size=3, padding=1),
10
+ nn.BatchNorm1d(64), nn.ReLU(),
11
+ # Block 2 — kernel-5 (captures broader waveform distortions)
12
+ nn.Conv1d(64, 128, kernel_size=5, padding=2),
13
+ nn.BatchNorm1d(128), nn.ReLU(), nn.MaxPool1d(2), # → [B, 128, 113]
14
+ # Block 3 — kernel-3 (deep feature abstraction)
15
+ nn.Conv1d(128, 256, kernel_size=3, padding=1),
16
+ nn.BatchNorm1d(256), nn.ReLU(), nn.MaxPool1d(2), # → [B, 256, 56]
17
+ )
18
+
19
+ class _ScaledDotAttn(nn.Module):
20
+ '''Scaled dot-product multi-head self-attention with residual + LayerNorm.'''
21
+ def __init__(self, d_model=256, num_heads=8, dropout=0.1):
22
+ super().__init__()
23
+ self.mha = nn.MultiheadAttention(d_model, num_heads, dropout=dropout,
24
+ batch_first=True)
25
+ self.norm = nn.LayerNorm(d_model)
26
+
27
+ def forward(self, x):
28
+ out, w = self.mha(x, x, x, need_weights=True, average_attn_weights=True)
29
+ return self.norm(x + out), w # w: [B, seq, seq]
30
+
31
+ class ChronoGridModelForSequenceClassification(PreTrainedModel):
32
+ config_class = ChronoGridConfig
33
+
34
+ def __init__(self, config):
35
+ super().__init__(config)
36
+ self.num_classes = config.num_classes
37
+
38
+ # 1. Initialize your architecture
39
+ self.cnn = make_cnn_backbone(config.img_size)
40
+ self.bilstm = nn.LSTM(256, 128, num_layers=2, batch_first=True, bidirectional=True, dropout=0.3)
41
+ self.attention = _ScaledDotAttn(d_model=256, num_heads=8)
42
+ self.head = nn.Sequential(
43
+ nn.Dropout(0.3),
44
+ nn.Linear(256, 128), nn.ReLU(),
45
+ nn.Linear(128, self.num_classes),
46
+ )
47
+
48
+ def forward(self, x, labels=None):
49
+ # x shape: [B, 227, 227]
50
+ x = self.cnn(x).permute(0, 2, 1)
51
+ x, _ = self.bilstm(x)
52
+ x, attn_w = self.attention(x)
53
+ feat = x.mean(dim=1)
54
+ logits = self.head(feat)
55
+
56
+ loss = None
57
+ if labels is not None:
58
+ loss_fct = nn.CrossEntropyLoss()
59
+ loss = loss_fct(logits.view(-1, self.num_classes), labels.view(-1))
60
+
61
+ return (loss, logits) if loss is not None else (logits,)