-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsketches_only.py
162 lines (143 loc) · 6.47 KB
/
sketches_only.py
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
"""
Created by José Vicente Egas López
on 2021. 01. 27. 11 57
"""
# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.
import sys
import time
import torch
import torch.nn as nn
import numpy as np
from feature_extraction import get_feats
from training import train_utils
from CustomDataset import CustomAudioDataset
# task (name of the dataset)
task = 'sleepiness'
# in and out dirs
corpora_dir = '/media/jose/hk-data/PycharmProjects/the_speech/audio/'
out_dir = 'data/' + task
task_audio_dir = corpora_dir + task + '/'
# labels
labels = 'data/sleepiness/labels/labels.csv'
# frame-level feats params
# params = {
# sample-frequency : 16000,
# frame-length: 25, # the default is 25
# low-freq: 20, # the default.
# high-freq: 0, # the default is zero meaning use the Nyquist (4k in this case).
# num-ceps: 20, # higher than the default which is 12.
# snip-edges: "false"
# }
params = {
"num_mel_bins": 40
}
# Get model params
parser = train_utils.getParams()
args = parser.parse_args()
# Loading the data
# train_set = CustomDataset(file_labels=labels, audio_dir=task_audio_dir, name_set='train', online=True)
train_set = CustomAudioDataset(file_labels='data/sleepiness/labels/labels.csv', audio_dir=task_audio_dir,
name_set='train', online=True,
# feats_info=['/media/jose/hk-data/PycharmProjects/the_speech/data/sleepiness/', 'mfcc'],
calc_flevel=get_feats.FLevelFeatsTorch(save=True, out_dir=out_dir, feat_type='mfcc',
deltas=1, **params)
)
# dev_set = CustomDataset(file_labels='data/sleepiness/labels/labels.csv', audio_dir=task_audio_dir, name_set='dev',
# online=False,
# feats_info=['/media/jose/hk-data/PycharmProjects/the_speech/data/sleepiness/', 'mfcc'],
# # calc_flevel=get_feats.FbanksKaltorch(**params)
# )
# dev_loader = DataLoader(dataset=train_set, batch_size=args.batchSize, shuffle=False, num_workers=0)
#
# test_set = CustomDataset(file_labels='data/sleepiness/labels/labels.csv', audio_dir=task_audio_dir, name_set='test',
# online=False,
# feats_info=['/media/jose/hk-data/PycharmProjects/the_speech/data/sleepiness/', 'mfcc'],
# # calc_flevel=get_feats.FbanksKaltorch(**params)
# )
# test_loader = DataLoader(dataset=train_set, batch_size=args.batchSize, shuffle=False, num_workers=0)
# Set the GPU
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
# Set Params
total_steps = args.numEpochs #* args.numArchives
numBatchesPerArk = int(args.numEgsPerFile/args.batchSize)
eps = args.noiseEps
# prepare the model
net, optimizer, step, saveDir = train_utils.prepare_model(args)
criterion = nn.CrossEntropyLoss()
# LR scheduler
cyclic_lr_scheduler = torch.optim.lr_scheduler.OneCycleLR(optimizer,
max_lr=args.maxLR,
cycle_momentum=False,
div_factor=5,
final_div_factor=1e+3,
total_steps=total_steps, # * numBatchesPerArk,
pct_start=0.15)
# Train model
while step < total_steps:
preFetchRatio = args.preFetchRatio
batchI, loggedBatch = 0, 0
loggingLoss = 0.0
start_time = time.time()
for _, sample_batched in enumerate(train_loader):
x_train = sample_batched['feature'].to(device)
x_train = torch.transpose(x_train, 1, -1)
y_train = sample_batched['label'].to(device)
accumulateStepSize = 4
preFetchBatchI = 0 # this counter within the prefetched batches only
while preFetchBatchI < int(len(y_train) / args.batchSize) - accumulateStepSize:
# zeroing the gradients
optimizer.zero_grad()
for _ in range(accumulateStepSize):
batchI += 1
preFetchBatchI += 1
# forward prop + backward prop + optimization
output = net(x_train, args.numEpochs)
loss = criterion(output, y_train)
if np.isnan(loss.item()): # checking for exploding gradient
print('Nan encountered at iter %d. Exiting...' % iter)
sys.exit(1)
loss.backward()
# stats
loggingLoss += loss.item()
optimizer.step() # updating weights
cyclic_lr_scheduler.step()
print(batchI, loggedBatch, args.logStepSize)
# Log
if batchI - loggedBatch >= args.logStepSize:
logStepTime = time.time() - start_time
print('Batch: (%d/%d) Avg Time/batch: %1.3f Avg Loss/batch: %1.3f' % (
batchI,
numBatchesPerArk,
logStepTime / (batchI - loggedBatch),
loggingLoss / (batchI - loggedBatch)))
loggingLoss = 0.0
start_time = time.time()
loggedBatch = batchI
# for args.numEpochs in range(args.numEpochs):
# loggingLoss = 0.0
# start_time = time.time()
# # net.train()
# for batch_idx, sample_batched in enumerate(train_loader):
# x_train = sample_batched['feature'].to(device)
# x_train = torch.transpose(x_train, 1, -1)
# y_train = sample_batched['label'].to(device)
# # zeroing the gradients
# optimizer.zero_grad()
# # forward prop + backward prop + optimization
# output = net(x_train, args.numEpochs)
# loss = criterion(output, y_train)
# if np.isnan(loss.item()): # checking for exploding gradient
# print('Nan encountered at iter %d. Exiting...' % iter)
# sys.exit(1)
# loss.backward()
# # stats
# loggingLoss += loss.item()
# optimizer.step() # updating weights
# cyclic_lr_scheduler.step()
#
# # if batch_idx % 20 == 0:
# logTime = time.time() - start_time
# print(loggingLoss)
# # print('Batch: %d Avg time/batch: %1.3f Avg loss/batch: %1.3f' %(
# # batch_idx, logTime/batch_idx, loggingLoss/300))