davesalvi commited on
Commit
637ed97
·
1 Parent(s): 2d22374

rawnet2 code fix

Browse files
.gitignore CHANGED
@@ -1,5 +1,6 @@
1
  # Ignore weights
2
  checkpoints/RAWNET_ASVSPOOF_FOR_INTHEWILD_PURDUE.pth
 
3
 
4
  .idea
5
 
 
1
  # Ignore weights
2
  checkpoints/RAWNET_ASVSPOOF_FOR_INTHEWILD_PURDUE.pth
3
+ debug.sh
4
 
5
  .idea
6
 
.idea/deployment.xml CHANGED
@@ -1,6 +1,6 @@
1
  <?xml version="1.0" encoding="UTF-8"?>
2
  <project version="4">
3
- <component name="PublishConfigData">
4
  <serverData>
5
  <paths name="Fermi">
6
  <serverdata>
@@ -10,5 +10,6 @@
10
  </serverdata>
11
  </paths>
12
  </serverData>
 
13
  </component>
14
  </project>
 
1
  <?xml version="1.0" encoding="UTF-8"?>
2
  <project version="4">
3
+ <component name="PublishConfigData" autoUpload="On explicit save action" autoUploadExternalChanges="true">
4
  <serverData>
5
  <paths name="Fermi">
6
  <serverdata>
 
10
  </serverdata>
11
  </paths>
12
  </serverData>
13
+ <option name="myAutoUpload" value="ON_EXPLICIT_SAVE" />
14
  </component>
15
  </project>
.idea/vcs.xml CHANGED
@@ -2,5 +2,6 @@
2
  <project version="4">
3
  <component name="VcsDirectoryMappings">
4
  <mapping directory="" vcs="Git" />
 
5
  </component>
6
  </project>
 
2
  <project version="4">
3
  <component name="VcsDirectoryMappings">
4
  <mapping directory="" vcs="Git" />
5
+ <mapping directory="$PROJECT_DIR$/safe-challenge-practice-dataset" vcs="Git" />
6
  </component>
7
  </project>
.idea/workspace.xml CHANGED
@@ -5,7 +5,19 @@
5
  </component>
6
  <component name="ChangeListManager">
7
  <list default="true" id="23565123-73ab-4f40-a9ef-1086e0c9e1ec" name="Changes" comment="">
 
 
 
 
 
 
 
 
 
 
8
  <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
 
 
9
  <change beforePath="$PROJECT_DIR$/script.py" beforeDir="false" afterPath="$PROJECT_DIR$/script.py" afterDir="false" />
10
  </list>
11
  <option name="SHOW_DIALOG" value="false" />
@@ -38,21 +50,50 @@
38
  <option name="hideEmptyMiddlePackages" value="true" />
39
  <option name="showLibraryContents" value="true" />
40
  </component>
41
- <component name="PropertiesComponent">{
42
- &quot;keyToString&quot;: {
43
- &quot;RunOnceActivity.OpenProjectViewOnStart&quot;: &quot;true&quot;,
44
- &quot;RunOnceActivity.ShowReadmeOnStart&quot;: &quot;true&quot;,
45
- &quot;WebServerToolWindowFactoryState&quot;: &quot;true&quot;,
46
- &quot;last_opened_file_path&quot;: &quot;/Users/davidesalvi/Desktop/PHD/projects/polimi-ispl/ispl_safe&quot;,
47
- &quot;node.js.detected.package.eslint&quot;: &quot;true&quot;,
48
- &quot;node.js.detected.package.tslint&quot;: &quot;true&quot;,
49
- &quot;node.js.selected.package.eslint&quot;: &quot;(autodetect)&quot;,
50
- &quot;node.js.selected.package.tslint&quot;: &quot;(autodetect)&quot;,
51
- &quot;nodejs_package_manager_path&quot;: &quot;npm&quot;,
52
- &quot;settings.editor.selected.configurable&quot;: &quot;web.server&quot;,
53
- &quot;vue.rearranger.settings.migration&quot;: &quot;true&quot;
54
  }
55
- }</component>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56
  <component name="SpellCheckerSettings" RuntimeDictionaries="0" Folders="0" CustomDictionaries="0" DefaultDictionary="application-level" UseSingleDictionary="true" transferred="true" />
57
  <component name="TaskManager">
58
  <task active="true" id="Default" summary="Default task">
@@ -62,11 +103,26 @@
62
  <option name="presentableId" value="Default" />
63
  <updated>1742573353560</updated>
64
  <workItem from="1742573355153" duration="587000" />
65
- <workItem from="1742806974298" duration="770000" />
 
66
  </task>
67
  <servers />
68
  </component>
69
  <component name="TypeScriptGeneratedFilesManager">
70
  <option name="version" value="3" />
71
  </component>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72
  </project>
 
5
  </component>
6
  <component name="ChangeListManager">
7
  <list default="true" id="23565123-73ab-4f40-a9ef-1086e0c9e1ec" name="Changes" comment="">
8
+ <change afterPath="$PROJECT_DIR$/Dockerfile" afterDir="false" />
9
+ <change afterPath="$PROJECT_DIR$/data/06704fa4-5a0c-540c-86e6-c98af1528478.wav" afterDir="false" />
10
+ <change afterPath="$PROJECT_DIR$/data/07bd0843-74a6-53ec-a3f0-00dfc31d6e2a.wav" afterDir="false" />
11
+ <change afterPath="$PROJECT_DIR$/data/9712245a-548d-584c-a82d-a543f1ea21ac.wav" afterDir="false" />
12
+ <change afterPath="$PROJECT_DIR$/data/c3e008aa-e4ba-5d2a-b37e-dd6d0ae640cb.wav" afterDir="false" />
13
+ <change afterPath="$PROJECT_DIR$/debug.sh" afterDir="false" />
14
+ <change afterPath="$PROJECT_DIR$/requirements.txt" afterDir="false" />
15
+ <change beforePath="$PROJECT_DIR$/.gitignore" beforeDir="false" afterPath="$PROJECT_DIR$/.gitignore" afterDir="false" />
16
+ <change beforePath="$PROJECT_DIR$/.idea/deployment.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/deployment.xml" afterDir="false" />
17
+ <change beforePath="$PROJECT_DIR$/.idea/vcs.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/vcs.xml" afterDir="false" />
18
  <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
19
+ <change beforePath="$PROJECT_DIR$/checkpoints/RAWNET_ASVSPOOF_FOR_INTHEWILD_PURDUE.pth" beforeDir="false" />
20
+ <change beforePath="$PROJECT_DIR$/preprocess.py" beforeDir="false" afterPath="$PROJECT_DIR$/preprocess.py" afterDir="false" />
21
  <change beforePath="$PROJECT_DIR$/script.py" beforeDir="false" afterPath="$PROJECT_DIR$/script.py" afterDir="false" />
22
  </list>
23
  <option name="SHOW_DIALOG" value="false" />
 
50
  <option name="hideEmptyMiddlePackages" value="true" />
51
  <option name="showLibraryContents" value="true" />
52
  </component>
53
+ <component name="PropertiesComponent"><![CDATA[{
54
+ "keyToString": {
55
+ "RunOnceActivity.OpenProjectViewOnStart": "true",
56
+ "RunOnceActivity.ShowReadmeOnStart": "true",
57
+ "WebServerToolWindowFactoryState": "true",
58
+ "last_opened_file_path": "/Users/davidesalvi/Desktop/PHD/projects/polimi-ispl/ispl_safe",
59
+ "node.js.detected.package.eslint": "true",
60
+ "node.js.detected.package.tslint": "true",
61
+ "node.js.selected.package.eslint": "(autodetect)",
62
+ "node.js.selected.package.tslint": "(autodetect)",
63
+ "nodejs_package_manager_path": "npm",
64
+ "settings.editor.selected.configurable": "com.jetbrains.python.configuration.PyActiveSdkModuleConfigurable",
65
+ "vue.rearranger.settings.migration": "true"
66
  }
67
+ }]]></component>
68
+ <component name="RunManager">
69
+ <configuration name="script" type="PythonConfigurationType" factoryName="Python" temporary="true" nameIsGenerated="true">
70
+ <module name="ispl_safe" />
71
+ <option name="INTERPRETER_OPTIONS" value="" />
72
+ <option name="PARENT_ENVS" value="true" />
73
+ <envs>
74
+ <env name="PYTHONUNBUFFERED" value="1" />
75
+ </envs>
76
+ <option name="SDK_HOME" value="" />
77
+ <option name="WORKING_DIRECTORY" value="$PROJECT_DIR$" />
78
+ <option name="IS_MODULE_SDK" value="true" />
79
+ <option name="ADD_CONTENT_ROOTS" value="true" />
80
+ <option name="ADD_SOURCE_ROOTS" value="true" />
81
+ <EXTENSION ID="PythonCoverageRunConfigurationExtension" runner="coverage.py" />
82
+ <option name="SCRIPT_NAME" value="$PROJECT_DIR$/script.py" />
83
+ <option name="PARAMETERS" value="" />
84
+ <option name="SHOW_COMMAND_LINE" value="false" />
85
+ <option name="EMULATE_TERMINAL" value="false" />
86
+ <option name="MODULE_MODE" value="false" />
87
+ <option name="REDIRECT_INPUT" value="false" />
88
+ <option name="INPUT_FILE" value="" />
89
+ <method v="2" />
90
+ </configuration>
91
+ <recent_temporary>
92
+ <list>
93
+ <item itemvalue="Python.script" />
94
+ </list>
95
+ </recent_temporary>
96
+ </component>
97
  <component name="SpellCheckerSettings" RuntimeDictionaries="0" Folders="0" CustomDictionaries="0" DefaultDictionary="application-level" UseSingleDictionary="true" transferred="true" />
98
  <component name="TaskManager">
99
  <task active="true" id="Default" summary="Default task">
 
103
  <option name="presentableId" value="Default" />
104
  <updated>1742573353560</updated>
105
  <workItem from="1742573355153" duration="587000" />
106
+ <workItem from="1742806974298" duration="2741000" />
107
+ <workItem from="1742810431420" duration="7010000" />
108
  </task>
109
  <servers />
110
  </component>
111
  <component name="TypeScriptGeneratedFilesManager">
112
  <option name="version" value="3" />
113
  </component>
114
+ <component name="XDebuggerManager">
115
+ <breakpoint-manager>
116
+ <breakpoints>
117
+ <line-breakpoint enabled="true" suspend="THREAD" type="python-line">
118
+ <url>file://$PROJECT_DIR$/script.py</url>
119
+ <line>30</line>
120
+ <option name="timeStamp" value="1" />
121
+ </line-breakpoint>
122
+ </breakpoints>
123
+ </breakpoint-manager>
124
+ </component>
125
+ <component name="com.intellij.coverage.CoverageDataManagerImpl">
126
+ <SUITE FILE_PATH="coverage/ispl_safe$script.coverage" NAME="script Coverage Results" MODIFIED="1742814252166" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="true" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="$PROJECT_DIR$" />
127
+ </component>
128
  </project>
Dockerfile ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ FROM huggingface/competitions:latest
2
+ WORKDIR /tmp
3
+ COPY requirements.txt .
4
+ RUN pip install --progress-bar off -r requirements.txt
5
+ COPY . .
6
+ CMD bash debug.sh
checkpoints/RAWNET_ASVSPOOF_FOR_INTHEWILD_PURDUE.pth DELETED
@@ -1,3 +0,0 @@
1
- version https://git-lfs.github.com/spec/v1
2
- oid sha256:abd0803e944f98176c98a8b3d53f7ca0c5623cd4cf0566aba7efb0a09a8bc2f7
3
- size 70534111
 
 
 
 
data/06704fa4-5a0c-540c-86e6-c98af1528478.wav ADDED
Binary file (530 kB). View file
 
data/07bd0843-74a6-53ec-a3f0-00dfc31d6e2a.wav ADDED
Binary file (672 kB). View file
 
data/9712245a-548d-584c-a82d-a543f1ea21ac.wav ADDED
Binary file (660 kB). View file
 
data/c3e008aa-e4ba-5d2a-b37e-dd6d0ae640cb.wav ADDED
Binary file (683 kB). View file
 
preprocess.py CHANGED
@@ -1,10 +1,41 @@
1
 
2
  import librosa
 
3
  import torch
4
 
 
 
 
 
 
 
 
 
 
 
5
  def preprocess(audio_file):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
  # Load the audio file
7
  y, sr = librosa.load(audio_file, sr=None)
8
  mfccs = librosa.feature.mfcc(y=y, sr=sr, n_mfcc=13)
9
- tensor = torch.from_numpy(mfccs)[None]
10
  return tensor
 
1
 
2
  import librosa
3
+ import numpy as np
4
  import torch
5
 
6
+ def pad_audio(x, max_len=48000):
7
+ x_len = x.shape[0]
8
+ if x_len >= max_len:
9
+ return x[:max_len]
10
+ # need to pad
11
+ num_repeats = int(max_len / x_len) + 1
12
+ padded_x = np.tile(x, (1, num_repeats))[:, :max_len][0]
13
+ return padded_x
14
+
15
+
16
  def preprocess(audio_file):
17
+ print(f'Preprocessing {audio_file}')
18
+ # Load the audio file
19
+ y, sr = librosa.load(audio_file, sr=16000)
20
+
21
+ # Evaluate N windows of the audio file
22
+ num_eval = 5
23
+ win_len = int(3*sr)
24
+ start_sample_list = np.linspace(0, len(y) - win_len, num=num_eval)
25
+
26
+ frames = []
27
+ for start_sample in start_sample_list:
28
+ frames += [y[int(start_sample):int(start_sample) + win_len]]
29
+ y_win = np.stack(frames, axis=0)
30
+ tensor = torch.from_numpy(y_win)
31
+
32
+ print(f'preprocessed track - shape {tensor.shape}')
33
+ return tensor
34
+
35
+
36
+ def preprocess_old(audio_file):
37
  # Load the audio file
38
  y, sr = librosa.load(audio_file, sr=None)
39
  mfccs = librosa.feature.mfcc(y=y, sr=sr, n_mfcc=13)
40
+ tensor = torch.from_numpy(mfccs)[None]
41
  return tensor
requirements.txt ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ --extra-index-url https://aiinfra.pkgs.visualstudio.com/PublicPackages/_packaging/onnxruntime-cuda-12/pypi/simple/
2
+ onnxruntime-gpu==1.18.0
3
+ av==14.2.0
4
+ tqdm==4.66.4
5
+ torchaudio==2.4.0
6
+ transformers==4.44.1
7
+ h5py==3.13.0
8
+ webrtcvad==2.0.10
9
+ scipy==1.14.1
10
+ torch==2.4.0
11
+ soundfile==0.13.1
12
+ librosa==0.10.2.post1
13
+ omegaconf==2.3.0
14
+ hydra-core==1.3.2
script.py CHANGED
@@ -2,19 +2,22 @@ import io
2
  import time
3
  import os
4
 
 
 
 
 
 
5
  import torch
6
  import tqdm.auto as tqdm
 
7
  from datasets import load_dataset
8
 
9
  import numpy as np
10
  import pandas as pd
11
 
12
- from models import Model
13
  from preprocess import preprocess
14
- from pathlib import Path
15
-
16
  from src.rawnet_model import RawNet
17
- # from src.utils import *
18
 
19
 
20
  # Import your model and anything else you want
@@ -25,20 +28,18 @@ from src.rawnet_model import RawNet
25
  # For testing, this is the docker image that will be used https://github.com/huggingface/competitions/blob/main/Dockerfile
26
  # It can be pulled here https://hub.docker.com/r/huggingface/competitions/tags
27
 
28
-
29
  # load the dataset. dataset will be automatically downloaded to /tmp/data during evaluation
30
  DATASET_PATH = "/tmp/data"
31
- dataset_remote = load_dataset(DATASET_PATH,split = "test",streaming = True)
32
-
 
33
 
34
- # load your model
35
- device = "cuda:0"
36
 
37
- # # DEFINE RAWNET2 MODEL
38
- this_folder = Path(__file__).parent
39
- # config_path = this_folder / 'config' / 'rawnet_config.yaml'
40
- # config = read_yaml(config_path)
41
 
 
42
  config = {
43
  "first_conv": 1024,
44
  "in_channels": 1,
@@ -50,19 +51,20 @@ config = {
50
  "nb_classes": 2
51
  }
52
 
53
- model_rawnet = RawNet(config['model'], device)
54
 
55
- model_path = this_folder / 'checkpoints' / 'RAWNET_ASVSPOOF_FOR_INTHEWILD_PURDUE.pth'
56
- model_rawnet.load_state_dict(torch.load(model_path, map_location=device))
 
57
 
58
- del model_rawnet
59
 
60
- # Define old model
61
- model = Model().to(device)
62
 
63
  # iterate over the dataset
64
  out = []
65
- for el in tqdm.tqdm(dataset_remote):
 
66
 
67
  start_time = time.time()
68
 
@@ -72,27 +74,38 @@ for el in tqdm.tqdm(dataset_remote):
72
  # el["audio"]["path"] containts the filename. This is just for reference and you cant actually load it
73
 
74
  # if you are using libraries that expect a file. You can use BytesIO object
75
- try:
76
- file_like = io.BytesIO(el["audio"]["bytes"])
77
- tensor = preprocess(file_like)
78
 
79
- with torch.no_grad():
80
- # soft decision (such as log likelihood score)
81
- # positive score correspond to synthetic prediction
82
- # negative score correspond to pristine prediction
83
- score = model(tensor.to(device)).cpu().item()
 
 
 
 
 
 
 
 
 
 
 
 
 
84
 
85
- # we require a hard decision to be submited. so you need to pick a threshold
86
- pred = "generated" if score > model.threshold else "pristine"
87
 
88
- # append your prediction
89
- # "id" and "pred" are required. "score" will not be used in scoring but we encourage you to include it. We'll use it for analysis of the results
90
 
91
- out.append(dict(id = el["id"], pred = pred, score = score, time = time.time() - start_time))
92
- except Exception as e:
93
- print(e)
94
- print("failed", el["id"])
95
- out.append(dict(id = el["id"], pred = "none", score = None))
 
96
 
97
  # save the final result and that's it
98
- pd.DataFrame(out).to_csv("submission.csv",index = False)
 
2
  import time
3
  import os
4
 
5
+ os.environ['OMP_NUM_THREADS'] = '1'
6
+ os.environ['OPENBLAS_NUM_THREADS'] = '1'
7
+ os.environ['MKL_NUM_THREADS'] = '1'
8
+ os.environ['NUMEXPR_NUM_THREADS'] = '1'
9
+
10
  import torch
11
  import tqdm.auto as tqdm
12
+ from glob import glob
13
  from datasets import load_dataset
14
 
15
  import numpy as np
16
  import pandas as pd
17
 
18
+ # from models import Model
19
  from preprocess import preprocess
 
 
20
  from src.rawnet_model import RawNet
 
21
 
22
 
23
  # Import your model and anything else you want
 
28
  # For testing, this is the docker image that will be used https://github.com/huggingface/competitions/blob/main/Dockerfile
29
  # It can be pulled here https://hub.docker.com/r/huggingface/competitions/tags
30
 
31
+ print('imported packages')
32
  # load the dataset. dataset will be automatically downloaded to /tmp/data during evaluation
33
  DATASET_PATH = "/tmp/data"
34
+ dataset_remote = glob(os.path.join(DATASET_PATH, '*'))
35
+ # dataset_remote = load_dataset('safe-challenge/safe-challenge-practice-dataset', split="test", streaming=True)
36
+ print('loaded dataset')
37
 
 
 
38
 
39
+ # device = "cuda:0"
40
+ device = "cpu"
 
 
41
 
42
+ # DEFINE RAWNET2 MODEL
43
  config = {
44
  "first_conv": 1024,
45
  "in_channels": 1,
 
51
  "nb_classes": 2
52
  }
53
 
54
+ model = RawNet(config, device)
55
 
56
+ model_path = '/tmp/checkpoints/RAWNET_ASVSPOOF_FOR_INTHEWILD_PURDUE.pth'
57
+ model.load_state_dict(torch.load(model_path, map_location=device))
58
+ # model = model.float()
59
 
60
+ print('Loaded RawNet2 Weights')
61
 
62
+ # model = Model().to(device) # OLD MODEL
 
63
 
64
  # iterate over the dataset
65
  out = []
66
+ # for el in tqdm.tqdm(dataset_remote):
67
+ for el in dataset_remote:
68
 
69
  start_time = time.time()
70
 
 
74
  # el["audio"]["path"] containts the filename. This is just for reference and you cant actually load it
75
 
76
  # if you are using libraries that expect a file. You can use BytesIO object
 
 
 
77
 
78
+ # try:
79
+
80
+ # file_like = io.BytesIO(el["audio"]["bytes"])
81
+ # tensor = preprocess(file_like)
82
+ tensor = preprocess(el)
83
+
84
+ with torch.no_grad():
85
+ # soft decision (such as log likelihood score)
86
+ # positive score correspond to synthetic prediction
87
+ # negative score correspond to pristine prediction
88
+ # score = model(tensor.to(device)).cpu().item()
89
+ score = model(tensor.to(device))[:, 1].cpu()
90
+ print(f'SCORE OUT: {score}')
91
+ score = score.mean().item()
92
+ print(f'SCORE FINAL: {score}')
93
+
94
+ # we require a hard decision to be submited. so you need to pick a threshold
95
+ pred = "generated" if score > model.threshold else "pristine"
96
 
97
+ # append your prediction
98
+ # "id" and "pred" are required. "score" will not be used in scoring but we encourage you to include it. We'll use it for analysis of the results
99
 
100
+ # out.append(dict(id = el["id"], pred = pred, score = score, time = time.time() - start_time))
101
+ out.append(dict(id=el, pred=pred, score=score, time=time.time() - start_time))
102
 
103
+ # except Exception as e:
104
+ # print(e)
105
+ # # print("failed", el["id"])
106
+ # # out.append(dict(id = el["id"], pred = "none", score = None))
107
+ # print("failed", el)
108
+ # out.append(dict(id = el, pred = "none", score = None))
109
 
110
  # save the final result and that's it
111
+ pd.DataFrame(out).to_csv("submission.csv", index=False)