oliverlevn commited on
Commit
a4b2002
·
verified ·
1 Parent(s): 4d65552

update the transformer lib cannot detect

Browse files
Files changed (4) hide show
  1. README.md +76 -16
  2. __init__.py +23 -0
  3. configuration_time_rcd.py +7 -0
  4. modeling_time_rcd.py +7 -0
README.md CHANGED
@@ -14,13 +14,27 @@ pipeline_tag: time-series-classification
14
 
15
  Time-RCD is a transformer-based model for zero-shot anomaly detection in time series data.
16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  ## Quick Start
18
 
19
  ```python
20
  from transformers import AutoModel, AutoConfig
21
  import numpy as np
22
 
23
- # Load model
24
  model = AutoModel.from_pretrained(
25
  "thu-sail-lab/Time_RCD",
26
  trust_remote_code=True
@@ -72,35 +86,44 @@ anomaly_scores = outputs.anomaly_scores.numpy()
72
  from transformers import AutoModel
73
  import numpy as np
74
 
 
75
  model = AutoModel.from_pretrained("thu-sail-lab/Time_RCD", trust_remote_code=True)
76
 
77
  # Your time series (n_samples, n_features)
78
  data = np.random.randn(10000, 1)
79
 
80
- # Get anomaly scores
81
- outputs = model.zero_shot(data)
82
- scores = outputs.anomaly_scores.numpy()
 
 
 
83
 
84
  # Detect anomalies (e.g., top 5%)
85
- threshold = np.percentile(scores, 95)
86
- anomalies = scores > threshold
87
  ```
88
 
89
  ### With Custom Processing
90
 
91
  ```python
92
- from transformers import AutoModel, AutoProcessor
 
 
93
 
 
94
  model = AutoModel.from_pretrained("thu-sail-lab/Time_RCD", trust_remote_code=True)
95
- processor = AutoProcessor.from_pretrained("thu-sail-lab/Time_RCD", trust_remote_code=True)
96
 
97
- # Configure processor
98
- processor.win_size = 5000
99
- processor.normalize = True
100
 
101
- # Process and detect
 
102
  processed = processor(data, return_tensors="pt")
 
 
103
  outputs = model(**processed)
 
104
  ```
105
 
106
  ## Configuration
@@ -124,13 +147,50 @@ Evaluated on various time series anomaly detection benchmarks.
124
  - Performance varies by domain
125
  - High-dimensional data may need preprocessing
126
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
127
  ## Citation
128
 
129
  ```bibtex
130
- @article{time-rcd,
131
- title={Time-RCD: Zero-Shot Time Series Anomaly Detection},
132
- author={Your Name},
133
- year={2025}
 
 
 
 
134
  }
135
  ```
136
 
 
14
 
15
  Time-RCD is a transformer-based model for zero-shot anomaly detection in time series data.
16
 
17
+ ## ⚠️ IMPORTANT: Custom Model Loading
18
+
19
+ **This model uses a custom architecture not built into transformers.**
20
+ You **MUST** include `trust_remote_code=True` when loading:
21
+
22
+ ```python
23
+ # ✅ CORRECT - Will work
24
+ from transformers import AutoModel
25
+ model = AutoModel.from_pretrained("your-repo/Time-RCD", trust_remote_code=True)
26
+
27
+ # ❌ WRONG - Will throw KeyError: 'time_rcd'
28
+ model = AutoModel.from_pretrained("your-repo/Time-RCD") # Missing trust_remote_code=True
29
+ ```
30
+
31
  ## Quick Start
32
 
33
  ```python
34
  from transformers import AutoModel, AutoConfig
35
  import numpy as np
36
 
37
+ # Load model (trust_remote_code=True is REQUIRED!)
38
  model = AutoModel.from_pretrained(
39
  "thu-sail-lab/Time_RCD",
40
  trust_remote_code=True
 
86
  from transformers import AutoModel
87
  import numpy as np
88
 
89
+ # IMPORTANT: trust_remote_code=True is required!
90
  model = AutoModel.from_pretrained("thu-sail-lab/Time_RCD", trust_remote_code=True)
91
 
92
  # Your time series (n_samples, n_features)
93
  data = np.random.randn(10000, 1)
94
 
95
+ # Get anomaly scores using the zero_shot method
96
+ scores, logits = model.zero_shot(data)
97
+
98
+ # Flatten scores from list of batches
99
+ import numpy as np
100
+ all_scores = np.concatenate(scores, axis=0).flatten()
101
 
102
  # Detect anomalies (e.g., top 5%)
103
+ threshold = np.percentile(all_scores, 95)
104
+ anomalies = all_scores > threshold
105
  ```
106
 
107
  ### With Custom Processing
108
 
109
  ```python
110
+ from transformers import AutoModel
111
+ from processing_time_rcd import TimeRCDProcessor
112
+ import numpy as np
113
 
114
+ # IMPORTANT: trust_remote_code=True is required!
115
  model = AutoModel.from_pretrained("thu-sail-lab/Time_RCD", trust_remote_code=True)
 
116
 
117
+ # Create and configure processor
118
+ processor = TimeRCDProcessor(win_size=5000, normalize=True)
 
119
 
120
+ # Process data
121
+ data = np.random.randn(10000, 1)
122
  processed = processor(data, return_tensors="pt")
123
+
124
+ # Get predictions
125
  outputs = model(**processed)
126
+ anomaly_scores = outputs.anomaly_scores
127
  ```
128
 
129
  ## Configuration
 
147
  - Performance varies by domain
148
  - High-dimensional data may need preprocessing
149
 
150
+ ## Troubleshooting
151
+
152
+ ### KeyError: 'time_rcd'
153
+
154
+ If you see this error:
155
+ ```
156
+ KeyError: 'time_rcd'
157
+ The checkpoint you are trying to load has model type `time_rcd` but Transformers does not recognize this architecture.
158
+ ```
159
+
160
+ **Solution:** Add `trust_remote_code=True` to your loading code:
161
+
162
+ ```python
163
+ # This will fix the error
164
+ model = AutoModel.from_pretrained("your-repo/Time-RCD", trust_remote_code=True)
165
+ ```
166
+
167
+ This is required because Time-RCD is a custom architecture not built into the transformers library. The `trust_remote_code=True` flag tells transformers to load and execute the custom model code from the repository.
168
+
169
+ ### Other Common Issues
170
+
171
+ **Issue:** `ModuleNotFoundError: No module named 'einops'`
172
+ **Solution:** Install einops: `pip install einops`
173
+
174
+ **Issue:** Model runs slowly on CPU
175
+ **Solution:** Move model to GPU: `model = model.to('cuda')`
176
+
177
+ **Issue:** Out of memory errors
178
+ **Solution:** Reduce window size or batch size in the processor:
179
+ ```python
180
+ processor = TimeRCDProcessor(win_size=2000, batch_size=32)
181
+ ```
182
+
183
  ## Citation
184
 
185
  ```bibtex
186
+ @misc{lan2025foundationmodelszeroshottime,
187
+ title={Towards Foundation Models for Zero-Shot Time Series Anomaly Detection: Leveraging Synthetic Data and Relative Context Discrepancy},
188
+ author={Tian Lan and Hao Duong Le and Jinbo Li and Wenjun He and Meng Wang and Chenghao Liu and Chen Zhang},
189
+ year={2025},
190
+ eprint={2509.21190},
191
+ archivePrefix={arXiv},
192
+ primaryClass={cs.LG},
193
+ url={https://arxiv.org/abs/2509.21190},
194
  }
195
  ```
196
 
__init__.py ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Time-RCD: Zero-Shot Time Series Anomaly Detection
3
+
4
+ This package provides the Time-RCD model for zero-shot anomaly detection in time series data.
5
+
6
+ Usage:
7
+ >>> from transformers import AutoModel
8
+ >>> model = AutoModel.from_pretrained("your-repo/Time-RCD", trust_remote_code=True)
9
+ """
10
+
11
+ from .configuration_time_rcd import TimeRCDConfig
12
+ from .modeling_time_rcd import Time_RCD, TimeRCDOutput
13
+ from .processing_time_rcd import TimeRCDProcessor
14
+
15
+ __version__ = "1.0.0"
16
+
17
+ __all__ = [
18
+ "TimeRCDConfig",
19
+ "Time_RCD",
20
+ "TimeRCDOutput",
21
+ "TimeRCDProcessor",
22
+ ]
23
+
configuration_time_rcd.py CHANGED
@@ -102,3 +102,10 @@ class TimeRCDConfig(PretrainedConfig):
102
 
103
  # Backward compatibility alias
104
  AnomalyCLIPConfig = TimeRCDConfig
 
 
 
 
 
 
 
 
102
 
103
  # Backward compatibility alias
104
  AnomalyCLIPConfig = TimeRCDConfig
105
+
106
+ # Register config with AutoConfig when using trust_remote_code
107
+ try:
108
+ from transformers import AutoConfig
109
+ AutoConfig.register("time_rcd", TimeRCDConfig)
110
+ except Exception:
111
+ pass # Silently fail if already registered or in restricted environment
modeling_time_rcd.py CHANGED
@@ -738,3 +738,10 @@ class LlamaMLP(nn.Module):
738
  # For backward compatibility, create aliases
739
  TimeRCDModel = Time_RCD # Alias for consistency
740
  AnomalyCLIPModel = Time_RCD # For existing code that uses this name
 
 
 
 
 
 
 
 
738
  # For backward compatibility, create aliases
739
  TimeRCDModel = Time_RCD # Alias for consistency
740
  AnomalyCLIPModel = Time_RCD # For existing code that uses this name
741
+
742
+ # Register model with AutoModel when using trust_remote_code
743
+ try:
744
+ from transformers import AutoModel
745
+ AutoModel.register(TimeRCDConfig, Time_RCD)
746
+ except Exception:
747
+ pass # Silently fail if already registered or in restricted environment