IlayMalinyak commited on
Commit
707b3a3
·
1 Parent(s): 9ff87ee
tasks/audio.py CHANGED
@@ -14,6 +14,11 @@ from .utils.models import DualEncoder
14
  from .utils.train import Trainer
15
  from .utils.data_utils import collate_fn, Container
16
  import yaml
 
 
 
 
 
17
 
18
  from dotenv import load_dotenv
19
  load_dotenv()
@@ -60,7 +65,7 @@ async def evaluate_audio(request: AudioEvaluationRequest):
60
  # Update the code below to replace the random baseline by your model inference within the inference pass where the energy consumption and emissions are tracked.
61
  #--------------------------------------------------------------------------------------------
62
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
63
- args_path = 'utils/config.yaml'
64
  data_args = Container(**yaml.safe_load(open(args_path, 'r'))['Data'])
65
  model_args = Container(**yaml.safe_load(open(args_path, 'r'))['CNNEncoder'])
66
  model_args_f = Container(**yaml.safe_load(open(args_path, 'r'))['CNNEncoder_f'])
@@ -71,7 +76,13 @@ async def evaluate_audio(request: AudioEvaluationRequest):
71
 
72
  model = DualEncoder(model_args, model_args_f, conformer_args)
73
  model = model.to(device)
74
- missing, unexpected = model.load_state_dict(torch.load(model_args.checkpoint_path))
 
 
 
 
 
 
75
 
76
  loss_fn = torch.nn.BCEWithLogitsLoss()
77
  optimizer = torch.optim.Adam(model.parameters(), lr=5e-4)
@@ -83,9 +94,9 @@ async def evaluate_audio(request: AudioEvaluationRequest):
83
  range_update=None,
84
  accumulation_step=1, max_iter=np.inf,
85
  exp_name=f"frugal_cnnencoder_inference")
86
- predictions, acc = trainer.predict(test_dl, device=device)
87
  # Make random predictions (placeholder for actual model inference)
88
- true_labels = test_dataset["label"]
89
 
90
  #--------------------------------------------------------------------------------------------
91
  # YOUR MODEL INFERENCE STOPS HERE
@@ -115,4 +126,17 @@ async def evaluate_audio(request: AudioEvaluationRequest):
115
  }
116
  }
117
 
118
- return results
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  from .utils.train import Trainer
15
  from .utils.data_utils import collate_fn, Container
16
  import yaml
17
+ import asyncio
18
+ from huggingface_hub import login
19
+ from collections import OrderedDict
20
+
21
+
22
 
23
  from dotenv import load_dotenv
24
  load_dotenv()
 
65
  # Update the code below to replace the random baseline by your model inference within the inference pass where the energy consumption and emissions are tracked.
66
  #--------------------------------------------------------------------------------------------
67
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
68
+ args_path = 'tasks/utils/config.yaml'
69
  data_args = Container(**yaml.safe_load(open(args_path, 'r'))['Data'])
70
  model_args = Container(**yaml.safe_load(open(args_path, 'r'))['CNNEncoder'])
71
  model_args_f = Container(**yaml.safe_load(open(args_path, 'r'))['CNNEncoder_f'])
 
76
 
77
  model = DualEncoder(model_args, model_args_f, conformer_args)
78
  model = model.to(device)
79
+ state_dict = torch.load(model_args.checkpoint_path)
80
+ new_state_dict = OrderedDict()
81
+ for key, value in state_dict.items():
82
+ if key.startswith('module.'):
83
+ key = key[7:]
84
+ new_state_dict[key] = value
85
+ missing, unexpected = model.load_state_dict(new_state_dict)
86
 
87
  loss_fn = torch.nn.BCEWithLogitsLoss()
88
  optimizer = torch.optim.Adam(model.parameters(), lr=5e-4)
 
94
  range_update=None,
95
  accumulation_step=1, max_iter=np.inf,
96
  exp_name=f"frugal_cnnencoder_inference")
97
+ predictions, true_labels, acc = trainer.predict(test_dl, device=device)
98
  # Make random predictions (placeholder for actual model inference)
99
+ print("accuracy: ", acc)
100
 
101
  #--------------------------------------------------------------------------------------------
102
  # YOUR MODEL INFERENCE STOPS HERE
 
126
  }
127
  }
128
 
129
+ return results
130
+
131
+ # if __name__ == "__main__":
132
+ # with open("../logs//token.txt", "r") as f:
133
+ # api_key = f.read()
134
+ # login(api_key)
135
+ # # Create a sample request object
136
+ # sample_request = AudioEvaluationRequest(
137
+ # dataset_name="rfcx/frugalai", # Replace with actual dataset name
138
+ # test_size=0.2, # Example values
139
+ # test_seed=42
140
+ # )
141
+ #
142
+ # asyncio.run(evaluate_audio(sample_request))
tasks/utils/config.yaml CHANGED
@@ -28,7 +28,7 @@ CNNEncoder:
28
  activation: "silu"
29
  sine_w0: 1.0
30
  avg_output: True
31
- checkpoint_path: 'models/frugal_2025-01-10/frugal_cnnencoder_2.pth'
32
 
33
  CNNEncoder_f:
34
  # Model
 
28
  activation: "silu"
29
  sine_w0: 1.0
30
  avg_output: True
31
+ checkpoint_path: 'tasks/models/frugal_2025-01-10/frugal_cnnencoder_2.pth'
32
 
33
  CNNEncoder_f:
34
  # Model
tasks/utils/train.py CHANGED
@@ -274,7 +274,8 @@ class Trainer(object):
274
  total = 0
275
  all_accs = 0
276
  predictions = []
277
- pbar = tqdm(self.val_dl)
 
278
  for i,batch in enumerate(pbar):
279
  x, fft, y = batch['audio']['array'], batch['audio']['fft'], batch['label']
280
  x = x.to(device).float()
@@ -286,7 +287,11 @@ class Trainer(object):
286
  probs = torch.sigmoid(y_pred)
287
  cls_pred = (probs > 0.5).float()
288
  acc = (cls_pred == y).sum()
289
- predictions.append(cls_pred)
 
290
  all_accs += acc
291
  total += len(y)
292
- return predictions, all_accs/total
 
 
 
 
274
  total = 0
275
  all_accs = 0
276
  predictions = []
277
+ true_labels = []
278
+ pbar = tqdm(test_dataloader)
279
  for i,batch in enumerate(pbar):
280
  x, fft, y = batch['audio']['array'], batch['audio']['fft'], batch['label']
281
  x = x.to(device).float()
 
287
  probs = torch.sigmoid(y_pred)
288
  cls_pred = (probs > 0.5).float()
289
  acc = (cls_pred == y).sum()
290
+ predictions.append(cls_pred.cpu().numpy())
291
+ true_labels.append(y.cpu().numpy())
292
  all_accs += acc
293
  total += len(y)
294
+ pbar.set_description("acc: {:.4f}".format(acc))
295
+ if i > self.max_iter:
296
+ break
297
+ return predictions, true_labels, all_accs/total