Nicolas Denier commited on
Commit
0388c00
·
1 Parent(s): 360633d

update readme

Browse files
Files changed (4) hide show
  1. README.md +2 -2
  2. requirements.txt +0 -1
  3. tasks/audio.py +2 -2
  4. tasks/utils/preprocess.py +31 -31
README.md CHANGED
@@ -82,8 +82,8 @@ Training code can be found in [training/](training/) directory (not used for inf
82
  - **Recall**: 89.97%
83
  - **F-score**: 91,71%
84
  - **Environmental Impact**:
85
- - Emissions tracked in gCO2eq
86
- - Energy consumption tracked in Wh
87
  - **Mistakes**
88
  - False positive represents 38.35% of mistakes
89
  - False negative represents 61.65% of mistakes
 
82
  - **Recall**: 89.97%
83
  - **F-score**: 91,71%
84
  - **Environmental Impact**:
85
+ - Emissions tracked in gCO2eq: 5.19
86
+ - Energy consumption tracked in Wh: 14.07
87
  - **Mistakes**
88
  - False positive represents 38.35% of mistakes
89
  - False negative represents 61.65% of mistakes
requirements.txt CHANGED
@@ -2,7 +2,6 @@ fastapi>=0.68.0
2
  uvicorn>=0.15.0
3
  codecarbon>=2.3.1
4
  datasets>=2.14.0
5
- scikit-learn>=1.0.2
6
  pydantic>=1.10.0
7
  python-dotenv>=1.0.0
8
  gradio>=4.0.0
 
2
  uvicorn>=0.15.0
3
  codecarbon>=2.3.1
4
  datasets>=2.14.0
 
5
  pydantic>=1.10.0
6
  python-dotenv>=1.0.0
7
  gradio>=4.0.0
tasks/audio.py CHANGED
@@ -1,7 +1,6 @@
1
  from fastapi import APIRouter
2
  from datetime import datetime
3
  from datasets import load_dataset
4
- #from sklearn.metrics import accuracy_score
5
  import os
6
  import torch
7
 
@@ -15,7 +14,7 @@ load_dotenv()
15
 
16
  router = APIRouter()
17
 
18
- DESCRIPTION = "Chainsaw goes brrr ⇒ GPU goes brrr"
19
  ROUTE = "/audio"
20
 
21
 
@@ -35,6 +34,7 @@ async def evaluate_audio(request: AudioEvaluationRequest):
35
  "chainsaw": 0,
36
  "environment": 1
37
  }
 
38
  # Load and prepare the dataset
39
  # Because the dataset is gated, we need to use the HF_TOKEN environment variable to authenticate
40
  batch_size = 16
 
1
  from fastapi import APIRouter
2
  from datetime import datetime
3
  from datasets import load_dataset
 
4
  import os
5
  import torch
6
 
 
14
 
15
  router = APIRouter()
16
 
17
+ DESCRIPTION = "ChainsawDetector"
18
  ROUTE = "/audio"
19
 
20
 
 
34
  "chainsaw": 0,
35
  "environment": 1
36
  }
37
+
38
  # Load and prepare the dataset
39
  # Because the dataset is gated, we need to use the HF_TOKEN environment variable to authenticate
40
  batch_size = 16
tasks/utils/preprocess.py CHANGED
@@ -4,7 +4,7 @@ from math import floor
4
  import torch
5
  from torch.nn.functional import pad
6
  from torchaudio.transforms import Resample
7
- #from random import randint
8
 
9
 
10
  def get_dataloader(dataset, device, batch_size=16, shuffle=True):
@@ -43,9 +43,7 @@ def preprocess(X, newsr, n_fft, win_length, hop_length, gain=0.8, bias=10, power
43
  return X
44
 
45
 
46
-
47
  def prepare_batch(samples):
48
- #maxlen=60
49
  newsr = 4000
50
  n_fft = 2**10 # power of 2
51
  win_length = 2**10
@@ -66,34 +64,36 @@ def prepare_batch(samples):
66
  labels = torch.tensor(labels, dtype=float)
67
  return batch, labels
68
 
69
- # def random_mask(sample):
70
- # # random rectangular mask
71
- # B, H, W = sample.shape
72
- # for b in range(B):
73
- # for _ in range(randint(3,12)):
74
- # w = randint(5, 15)
75
- # h = randint(10, 100)
76
- # x1 = randint(0, W-w)
77
- # y1 = randint(0, H-h)
78
- # sample[b, y1:y1+h, x1:x1+w] = 0
79
- # return sample
 
 
80
 
81
- # def timeshift(sample):
82
- # padsize = randint(0, 6)
83
- # length = sample.size(2)
84
- # randpad = torch.zeros((sample.size(0), sample.size(1), padsize), dtype=torch.float32)
85
- # sample = torch.cat((randpad, sample), dim=2)
86
- # sample = sample[:,:,:length]
87
- # return sample
88
 
89
- # def add_noise(sample):
90
- # #noise = np.random.normal(0, 0.05*sample.max(), sample.shape)
91
- # noise = 0.05*sample.max()*torch.randn(sample.shape, dtype=torch.float32)
92
- # sample = sample + noise
93
- # return sample
94
 
95
- # def augment(sample):
96
- # sample = timeshift(sample)
97
- # sample = random_mask(sample)
98
- # sample = add_noise(sample)
99
- # return sample
 
4
  import torch
5
  from torch.nn.functional import pad
6
  from torchaudio.transforms import Resample
7
+ from random import randint
8
 
9
 
10
  def get_dataloader(dataset, device, batch_size=16, shuffle=True):
 
43
  return X
44
 
45
 
 
46
  def prepare_batch(samples):
 
47
  newsr = 4000
48
  n_fft = 2**10 # power of 2
49
  win_length = 2**10
 
64
  labels = torch.tensor(labels, dtype=float)
65
  return batch, labels
66
 
67
+ # Data augmentation
68
+
69
+ def random_mask(sample):
70
+ # random rectangular mask
71
+ B, H, W = sample.shape
72
+ for b in range(B):
73
+ for _ in range(randint(3,12)):
74
+ w = randint(5, 15)
75
+ h = randint(10, 100)
76
+ x1 = randint(0, W-w)
77
+ y1 = randint(0, H-h)
78
+ sample[b, y1:y1+h, x1:x1+w] = 0
79
+ return sample
80
 
81
+ def timeshift(sample):
82
+ padsize = randint(0, 6)
83
+ length = sample.size(2)
84
+ randpad = torch.zeros((sample.size(0), sample.size(1), padsize), dtype=torch.float32)
85
+ sample = torch.cat((randpad, sample), dim=2)
86
+ sample = sample[:,:,:length]
87
+ return sample
88
 
89
+ def add_noise(sample):
90
+ #noise = np.random.normal(0, 0.05*sample.max(), sample.shape)
91
+ noise = 0.05*sample.max()*torch.randn(sample.shape, dtype=torch.float32)
92
+ sample = sample + noise
93
+ return sample
94
 
95
+ def augment(sample):
96
+ sample = timeshift(sample)
97
+ sample = random_mask(sample)
98
+ sample = add_noise(sample)
99
+ return sample