Skip to main content

voirs_conversion/style_transfer/
models.rs

1//! Style model structures and metadata
2
3use serde::{Deserialize, Serialize};
4use std::collections::HashMap;
5use std::time::Instant;
6
7use super::characteristics::StyleCharacteristics;
8
9/// Style model for style transfer
10#[derive(Debug, Clone, Serialize, Deserialize)]
11pub struct StyleModel {
12    /// Model identifier
13    pub id: String,
14
15    /// Model name
16    pub name: String,
17
18    /// Style characteristics
19    pub style_characteristics: StyleCharacteristics,
20
21    /// Model parameters
22    pub parameters: StyleModelParameters,
23
24    /// Training information
25    pub training_info: StyleTrainingInfo,
26
27    /// Quality metrics
28    pub quality_metrics: StyleModelQualityMetrics,
29
30    /// Creation timestamp
31    #[serde(skip)]
32    pub created: Option<Instant>,
33
34    /// Last updated timestamp
35    #[serde(skip)]
36    pub last_updated: Option<Instant>,
37}
38
39/// Style model parameters
40#[derive(Debug, Clone, Serialize, Deserialize)]
41pub struct StyleModelParameters {
42    /// Encoder parameters
43    pub encoder_params: EncoderParameters,
44
45    /// Decoder parameters
46    pub decoder_params: DecoderParameters,
47
48    /// Discriminator parameters
49    pub discriminator_params: Option<DiscriminatorParameters>,
50
51    /// Model architecture
52    pub architecture: ModelArchitecture,
53}
54
55/// Encoder parameters
56#[derive(Debug, Clone, Serialize, Deserialize)]
57pub struct EncoderParameters {
58    /// Input dimension
59    pub input_dim: usize,
60
61    /// Hidden dimensions
62    pub hidden_dims: Vec<usize>,
63
64    /// Output dimension
65    pub output_dim: usize,
66
67    /// Layer types
68    pub layer_types: Vec<LayerType>,
69
70    /// Activation functions
71    pub activations: Vec<ActivationType>,
72}
73
74/// Decoder parameters
75#[derive(Debug, Clone, Serialize, Deserialize)]
76pub struct DecoderParameters {
77    /// Input dimension
78    pub input_dim: usize,
79
80    /// Hidden dimensions
81    pub hidden_dims: Vec<usize>,
82
83    /// Output dimension
84    pub output_dim: usize,
85
86    /// Layer types
87    pub layer_types: Vec<LayerType>,
88
89    /// Activation functions
90    pub activations: Vec<ActivationType>,
91}
92
93/// Discriminator parameters
94#[derive(Debug, Clone, Serialize, Deserialize)]
95pub struct DiscriminatorParameters {
96    /// Input dimension
97    pub input_dim: usize,
98
99    /// Hidden dimensions
100    pub hidden_dims: Vec<usize>,
101
102    /// Number of classes
103    pub num_classes: usize,
104
105    /// Layer types
106    pub layer_types: Vec<LayerType>,
107}
108
109/// Layer type
110#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
111pub enum LayerType {
112    /// Linear layer
113    Linear,
114
115    /// Convolutional layer
116    Convolutional,
117
118    /// LSTM layer
119    LSTM,
120
121    /// GRU layer
122    GRU,
123
124    /// Transformer layer
125    Transformer,
126
127    /// Attention layer
128    Attention,
129}
130
131/// Activation type
132#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
133pub enum ActivationType {
134    /// ReLU activation
135    ReLU,
136
137    /// Leaky ReLU activation
138    LeakyReLU,
139
140    /// Tanh activation
141    Tanh,
142
143    /// Sigmoid activation
144    Sigmoid,
145
146    /// GELU activation
147    GELU,
148
149    /// Swish activation
150    Swish,
151}
152
153/// Model architecture
154#[derive(Debug, Clone, Serialize, Deserialize)]
155pub struct ModelArchitecture {
156    /// Architecture name
157    pub name: String,
158
159    /// Architecture type
160    pub architecture_type: ArchitectureType,
161
162    /// Model components
163    pub components: Vec<ModelComponent>,
164
165    /// Connection patterns
166    pub connections: Vec<ConnectionPattern>,
167}
168
169/// Architecture type
170#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
171pub enum ArchitectureType {
172    /// Autoencoder architecture
173    Autoencoder,
174
175    /// GAN architecture
176    GAN,
177
178    /// VAE architecture
179    VAE,
180
181    /// Transformer architecture
182    Transformer,
183
184    /// Diffusion model architecture
185    Diffusion,
186}
187
188/// Model component
189#[derive(Debug, Clone, Serialize, Deserialize)]
190pub struct ModelComponent {
191    /// Component name
192    pub name: String,
193
194    /// Component type
195    pub component_type: ComponentType,
196
197    /// Input shapes
198    pub input_shapes: Vec<Vec<usize>>,
199
200    /// Output shapes
201    pub output_shapes: Vec<Vec<usize>>,
202}
203
204/// Component type
205#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
206pub enum ComponentType {
207    /// Encoder component
208    Encoder,
209
210    /// Decoder component
211    Decoder,
212
213    /// Discriminator component
214    Discriminator,
215
216    /// Generator component
217    Generator,
218
219    /// Attention component
220    Attention,
221}
222
223/// Connection pattern
224#[derive(Debug, Clone, Serialize, Deserialize)]
225pub struct ConnectionPattern {
226    /// Source component
227    pub source: String,
228
229    /// Target component
230    pub target: String,
231
232    /// Connection type
233    pub connection_type: ConnectionType,
234
235    /// Connection weight
236    pub weight: f32,
237}
238
239/// Connection type
240#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
241pub enum ConnectionType {
242    /// Direct connection
243    Direct,
244
245    /// Residual connection
246    Residual,
247
248    /// Skip connection
249    Skip,
250
251    /// Attention connection
252    Attention,
253}
254
255/// Style training information
256#[derive(Debug, Clone, Serialize, Deserialize)]
257pub struct StyleTrainingInfo {
258    /// Training dataset information
259    pub dataset_info: DatasetInfo,
260
261    /// Training hyperparameters
262    pub hyperparameters: TrainingHyperparameters,
263
264    /// Training metrics
265    pub training_metrics: TrainingMetrics,
266
267    /// Validation metrics
268    pub validation_metrics: ValidationMetrics,
269}
270
271/// Dataset information
272#[derive(Debug, Clone, Serialize, Deserialize)]
273pub struct DatasetInfo {
274    /// Dataset name
275    pub name: String,
276
277    /// Dataset size
278    pub size: usize,
279
280    /// Number of speakers
281    pub num_speakers: usize,
282
283    /// Total duration (hours)
284    pub total_duration: f32,
285
286    /// Languages
287    pub languages: Vec<String>,
288
289    /// Speaking styles
290    pub speaking_styles: Vec<String>,
291}
292
293/// Training hyperparameters
294#[derive(Debug, Clone, Serialize, Deserialize)]
295pub struct TrainingHyperparameters {
296    /// Learning rate
297    pub learning_rate: f32,
298
299    /// Batch size
300    pub batch_size: usize,
301
302    /// Number of epochs
303    pub num_epochs: usize,
304
305    /// Optimizer type
306    pub optimizer: OptimizerType,
307
308    /// Loss function weights
309    pub loss_weights: HashMap<String, f32>,
310
311    /// Regularization parameters
312    pub regularization: RegularizationParameters,
313}
314
315/// Optimizer type
316#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
317pub enum OptimizerType {
318    /// Adam optimizer
319    Adam,
320
321    /// AdamW optimizer
322    AdamW,
323
324    /// SGD optimizer
325    SGD,
326
327    /// RMSprop optimizer
328    RMSprop,
329
330    /// AdaGrad optimizer
331    AdaGrad,
332}
333
334/// Regularization parameters
335#[derive(Debug, Clone, Serialize, Deserialize)]
336pub struct RegularizationParameters {
337    /// L1 regularization weight
338    pub l1_weight: f32,
339
340    /// L2 regularization weight
341    pub l2_weight: f32,
342
343    /// Dropout rate
344    pub dropout_rate: f32,
345
346    /// Batch normalization
347    pub batch_norm: bool,
348
349    /// Layer normalization
350    pub layer_norm: bool,
351}
352
353/// Training metrics
354#[derive(Debug, Clone, Serialize, Deserialize)]
355pub struct TrainingMetrics {
356    /// Training loss history
357    pub loss_history: Vec<f32>,
358
359    /// Training accuracy history
360    pub accuracy_history: Vec<f32>,
361
362    /// Training time per epoch
363    pub time_per_epoch: Vec<f32>,
364
365    /// Convergence information
366    pub convergence_info: ConvergenceInfo,
367}
368
369/// Validation metrics
370#[derive(Debug, Clone, Serialize, Deserialize)]
371pub struct ValidationMetrics {
372    /// Validation loss history
373    pub loss_history: Vec<f32>,
374
375    /// Validation accuracy history
376    pub accuracy_history: Vec<f32>,
377
378    /// Best validation score
379    pub best_score: f32,
380
381    /// Early stopping information
382    pub early_stopping: EarlyStoppingInfo,
383}
384
385/// Convergence information
386#[derive(Debug, Clone, Serialize, Deserialize)]
387pub struct ConvergenceInfo {
388    /// Converged flag
389    pub converged: bool,
390
391    /// Convergence epoch
392    pub convergence_epoch: Option<usize>,
393
394    /// Convergence criteria
395    pub criteria: ConvergenceCriteria,
396}
397
398/// Convergence criteria
399#[derive(Debug, Clone, Serialize, Deserialize)]
400pub struct ConvergenceCriteria {
401    /// Loss tolerance
402    pub loss_tolerance: f32,
403
404    /// Patience epochs
405    pub patience: usize,
406
407    /// Minimum improvement
408    pub min_improvement: f32,
409}
410
411/// Early stopping information
412#[derive(Debug, Clone, Serialize, Deserialize)]
413pub struct EarlyStoppingInfo {
414    /// Early stopped flag
415    pub early_stopped: bool,
416
417    /// Stopping epoch
418    pub stopping_epoch: Option<usize>,
419
420    /// Stopping reason
421    pub stopping_reason: Option<String>,
422}
423
424/// Style model quality metrics
425#[derive(Debug, Clone, Serialize, Deserialize)]
426pub struct StyleModelQualityMetrics {
427    /// Overall quality score
428    pub overall_quality: f32,
429
430    /// Style transfer accuracy
431    pub transfer_accuracy: f32,
432
433    /// Content preservation score
434    pub content_preservation: f32,
435
436    /// Style consistency score
437    pub style_consistency: f32,
438
439    /// Perceptual quality scores
440    pub perceptual_scores: PerceptualQualityScores,
441
442    /// Objective quality metrics
443    pub objective_metrics: ObjectiveQualityMetrics,
444}
445
446/// Perceptual quality scores
447#[derive(Debug, Clone, Serialize, Deserialize)]
448pub struct PerceptualQualityScores {
449    /// Naturalness score
450    pub naturalness: f32,
451
452    /// Similarity to target style
453    pub style_similarity: f32,
454
455    /// Intelligibility score
456    pub intelligibility: f32,
457
458    /// Overall preference score
459    pub preference: f32,
460
461    /// Confidence intervals
462    pub confidence_intervals: HashMap<String, (f32, f32)>,
463}
464
465/// Objective quality metrics
466#[derive(Debug, Clone, Serialize, Deserialize)]
467pub struct ObjectiveQualityMetrics {
468    /// Mel-cepstral distortion
469    pub mcd: f32,
470
471    /// Fundamental frequency RMSE
472    pub f0_rmse: f32,
473
474    /// Voicing decision error
475    pub voicing_error: f32,
476
477    /// Spectral distortion
478    pub spectral_distortion: f32,
479
480    /// Prosodic feature correlation
481    pub prosodic_correlation: f32,
482}
483
484/// Style model metadata
485#[derive(Debug, Clone, Serialize, Deserialize)]
486pub struct StyleModelMetadata {
487    /// Model creation date
488    #[serde(skip)]
489    pub created: Option<Instant>,
490
491    /// Model version
492    pub version: String,
493
494    /// Model author
495    pub author: String,
496
497    /// Model description
498    pub description: String,
499
500    /// Model tags
501    pub tags: Vec<String>,
502
503    /// Model license
504    pub license: String,
505
506    /// Model file size
507    pub file_size: u64,
508
509    /// Model checksum
510    pub checksum: String,
511}
512
513/// Model performance metrics
514#[derive(Debug, Clone, Serialize, Deserialize)]
515pub struct ModelPerformanceMetrics {
516    /// Inference time (ms)
517    pub inference_time: f32,
518
519    /// Memory usage (MB)
520    pub memory_usage: f32,
521
522    /// GPU utilization (%)
523    pub gpu_utilization: f32,
524
525    /// Throughput (samples/second)
526    pub throughput: f32,
527
528    /// Real-time factor
529    pub real_time_factor: f32,
530}
531
532/// Model usage statistics
533#[derive(Debug, Clone, Serialize, Deserialize)]
534pub struct ModelUsageStatistics {
535    /// Number of times used
536    pub usage_count: u64,
537
538    /// Average quality rating
539    pub avg_quality_rating: f32,
540
541    /// Success rate
542    pub success_rate: f32,
543
544    /// Last used timestamp
545    #[serde(skip)]
546    pub last_used: Option<Instant>,
547
548    /// Usage contexts
549    pub usage_contexts: HashMap<String, u32>,
550}
551
552/// Repository configuration
553#[derive(Debug, Clone, Serialize, Deserialize)]
554pub struct RepositoryConfig {
555    /// Maximum number of models
556    pub max_models: usize,
557
558    /// Cache size limit (MB)
559    pub cache_size_limit: u64,
560
561    /// Auto-cleanup enabled
562    pub auto_cleanup: bool,
563
564    /// Cleanup threshold
565    pub cleanup_threshold: f32,
566
567    /// Model versioning enabled
568    pub versioning_enabled: bool,
569}