forked from aminrd/LineamentLearning
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbridge.py
More file actions
312 lines (247 loc) · 9.49 KB
/
bridge.py
File metadata and controls
312 lines (247 loc) · 9.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
"""
Bridge module connecting original and modern pipelines.
This module provides adapters to use original DATASET, FILTER, and other
components with the modern ModelTrainer and configuration system.
"""
import numpy as np
from typing import Tuple, Optional, List
from pathlib import Path
from config import Config
from DATASET import DATASET
from FILTER import FILTER
from Utility import myNormalizer
class DatasetAdapter:
"""Adapter to use original DATASET class with modern pipeline.
This class bridges the gap between the original data loading
and the modern training infrastructure.
"""
def __init__(self, config: Config, dataset_path: str):
"""Initialize adapter.
Args:
config: Modern configuration object
dataset_path: Path to .mat dataset file
"""
self.config = config
self.dataset = DATASET(dataset_path)
self.dataset_path = dataset_path
def generate_training_data(self,
ratio: float = 1.0,
choosy: bool = False,
output_type: float = 0) -> Tuple[np.ndarray, np.ndarray, tuple]:
"""Generate training data using original DATASET class.
Args:
ratio: Ratio of samples to use
choosy: Whether to only pick fault locations
output_type: 0 for binary, np.pi/2.0 for angle detection
Returns:
Tuple of (X, Y, IDX) where:
- X: Input patches (N, W, W, layers)
- Y: Labels (N, 1)
- IDX: Indices of samples
"""
return self.dataset.generateDS(
output=self.dataset.OUTPUT,
mask=self.dataset.trainMask,
w=self.config.model.window_size,
choosy=choosy,
ratio=ratio,
output_type=output_type
)
def generate_validation_data(self,
ratio: float = 1.0) -> Tuple[np.ndarray, np.ndarray, tuple]:
"""Generate validation data.
Args:
ratio: Ratio of samples to use
Returns:
Tuple of (X, Y, IDX)
"""
return self.dataset.generateDS(
output=self.dataset.OUTPUT,
mask=self.dataset.testMask,
w=self.config.model.window_size,
choosy=False,
ratio=ratio,
output_type=0
)
def get_dataset_info(self) -> dict:
"""Get information about the dataset.
Returns:
Dictionary with dataset statistics
"""
return {
'shape': (self.dataset.x, self.dataset.y),
'layers': self.dataset.INPUTS.shape[2],
'train_mask_size': np.sum(self.dataset.trainMask),
'test_mask_size': np.sum(self.dataset.testMask),
'total_mask_size': np.sum(self.dataset.MASK),
'fault_pixels': np.sum(self.dataset.OUTPUT > 0)
}
class FilterAdapter:
"""Adapter to use original FILTER class for rotation augmentation.
Provides easy access to rotation matrices for data augmentation.
"""
def __init__(self, filter_path: str):
"""Initialize adapter.
Args:
filter_path: Path to filter .mat file
"""
self.filter = FILTER(filter_path)
self.n_filters = self.filter.N
def get_random_rotation(self) -> Tuple[int, np.ndarray]:
"""Get a random rotation filter.
Returns:
Tuple of (filter_number, filter_matrix)
"""
return self.filter.getFilter(n=1)
def get_rotation_by_index(self, index: int) -> Tuple[int, np.ndarray]:
"""Get specific rotation filter.
Args:
index: Filter index
Returns:
Tuple of (filter_number, filter_matrix)
"""
return self.filter.getFilterbyNumber(index)
def get_all_rotations(self) -> List[Tuple[int, np.ndarray]]:
"""Get all rotation filters.
Returns:
List of (filter_number, filter_matrix) tuples
"""
return [self.get_rotation_by_index(i) for i in range(self.n_filters)]
class LegacyTrainer:
"""Trainer that uses original data loading with modern architecture.
This class demonstrates how to use original DATASET and FILTER
classes with the modern model architectures.
"""
def __init__(self, config: Config, dataset_path: str):
"""Initialize legacy trainer.
Args:
config: Modern configuration
dataset_path: Path to dataset .mat file
"""
self.config = config
self.dataset_adapter = DatasetAdapter(config, dataset_path)
# Build modern model
from model_modern import build_model
self.model = build_model(config)
def train_simple(self,
ratio: float = 0.1,
epochs: int = 1) -> dict:
"""Simple training using original data loader.
Args:
ratio: Ratio of training data to use
epochs: Number of epochs
Returns:
Training history
"""
# Load data using original DATASET
print("Loading training data...")
X_train, Y_train, _ = self.dataset_adapter.generate_training_data(
ratio=ratio,
choosy=False,
output_type=0
)
print(f"Training data shape: {X_train.shape}")
print(f"Labels shape: {Y_train.shape}")
# Train using modern model
print("Training model...")
history = self.model.fit(
X_train, Y_train,
batch_size=self.config.model.batch_size,
epochs=epochs,
validation_split=0.2,
verbose=1
)
return history.history
def evaluate(self, ratio: float = 0.5) -> dict:
"""Evaluate model on test data.
Args:
ratio: Ratio of test data to use
Returns:
Evaluation metrics
"""
print("Loading test data...")
X_test, Y_test, _ = self.dataset_adapter.generate_validation_data(ratio=ratio)
print(f"Test data shape: {X_test.shape}")
print("Evaluating model...")
results = self.model.evaluate(X_test, Y_test, verbose=1)
# Get metric names
metric_names = self.model.metrics_names
return dict(zip(metric_names, results))
def train_with_original_pipeline(config: Config,
dataset_path: str,
output_dir: str,
epochs: int = 10) -> str:
"""Convenience function to train using original data pipeline.
This demonstrates the complete integration between original and
modern components.
Args:
config: Configuration object
dataset_path: Path to dataset .mat file
output_dir: Directory to save model
epochs: Number of training epochs
Returns:
Path to saved model
"""
import os
os.makedirs(output_dir, exist_ok=True)
# Create trainer
trainer = LegacyTrainer(config, dataset_path)
# Get dataset info
info = trainer.dataset_adapter.get_dataset_info()
print("\nDataset Information:")
print(f" Shape: {info['shape']}")
print(f" Layers: {info['layers']}")
print(f" Train mask size: {info['train_mask_size']}")
print(f" Test mask size: {info['test_mask_size']}")
print(f" Fault pixels: {info['fault_pixels']}")
# Train
print(f"\nTraining for {epochs} epochs...")
history = trainer.train_simple(ratio=0.1, epochs=epochs)
# Evaluate
print("\nEvaluating model...")
metrics = trainer.evaluate(ratio=0.5)
print("Test metrics:")
for name, value in metrics.items():
print(f" {name}: {value:.4f}")
# Save model
model_path = os.path.join(output_dir, 'model.h5')
trainer.model.save(model_path)
print(f"\nModel saved to: {model_path}")
return model_path
# Example usage
if __name__ == '__main__':
from config import Config
# Create configuration
config = Config()
config.model.architecture = 'RotateNet'
config.model.window_size = 45
config.model.epochs = 5
print("=" * 60)
print("Legacy Pipeline Integration Example")
print("=" * 60)
print("\nThis module demonstrates how to use original DATASET")
print("and FILTER classes with modern model architectures.")
print("\nUsage:")
print("------")
print("from bridge import DatasetAdapter, LegacyTrainer")
print("")
print("# Create adapter")
print("adapter = DatasetAdapter(config, 'path/to/data.mat')")
print("")
print("# Generate training data")
print("X, Y, IDX = adapter.generate_training_data(ratio=0.1)")
print("")
print("# Or use complete trainer")
print("trainer = LegacyTrainer(config, 'path/to/data.mat')")
print("history = trainer.train_simple(ratio=0.1, epochs=5)")
print("")
print("# Or use convenience function")
print("model_path = train_with_original_pipeline(")
print(" config=config,")
print(" dataset_path='path/to/data.mat',")
print(" output_dir='./models',")
print(" epochs=10")
print(")")
print("\n" + "=" * 60)
print("See PIPELINE_COVERAGE.md for complete integration details")
print("=" * 60)