-
Notifications
You must be signed in to change notification settings - Fork 41
/
inference.py
executable file
·84 lines (73 loc) · 3.02 KB
/
inference.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
from __future__ import division
from sklearn.model_selection import train_test_split
# import dataloader
from data import MyDatasetSTFT
import torch
import torch.nn as nn
import argparse
import copy
import random
from torchvision import transforms
import torch.backends.cudnn as cudnn
import os, sys
import config as cf
from time import time, strftime
from nets import MyNet, predict, softmax_stable
import predicts
from utils import *
import pandas as pd
parser = argparse.ArgumentParser(description='Zalo Voice Gender')
parser.add_argument('--net_type', default='resnet', type=str, help='model')
parser.add_argument('--depth', default=18, choices = [18, 50, 152], type=int, help='depth of model')
parser.add_argument('--model_path', type=str, default = ' ')
parser.add_argument('--batch_size', type=int, default = 63)
parser.add_argument('--num_tests', type=int, default = 10, help = 'number of tested windows in each file')
args = parser.parse_args()
################# model preparation ########################
model_path_fns = ['./saved_model/model_' + str(i) + '.t7' for i in range(5)]
############ load models ######################
models = []
clippeds = [] ## each model might received a different input lengh
for model_path_fn in model_path_fns:
# print("| Load pretrained at %s..." % model_path_fn)
checkpoint = torch.load(model_path_fn, map_location=lambda storage, loc: storage)
model = checkpoint['model']
model = unparallelize_model(model)
model = parallelize_model(model)
best_acc = checkpoint['acc']
clipped = checkpoint['args'].duration
print('model {}, acc on CV: {}'.format(model_path_fn, best_acc))
# print('previous acc\t%.4f'% best_acc)
models.append(model)
clippeds.append(clipped)
#############################################################
############# build dset_loaders
print('Data preparation')
fns = [os.path.join(cf.BASE_PRIVATE_TEST, fn)
for fn in os.listdir(cf.BASE_PRIVATE_TEST)]
print('Total provided files: {}'.format(len(fns)))
lbs = [-1]*len(fns) # random labels, we won't use this
dset_loaders = []
for clipped in clippeds:
dset = MyDatasetSTFT(fns, lbs, duration = clipped, test=True)
dset_loaders.append(torch.utils.data.DataLoader(dset,
batch_size=args.batch_size,
shuffle= False, num_workers=cf.NUM_WORKERS))
############# make predictions based on models and loaders
pred_score, pred_probs, fns = \
predicts.multimodels_multiloaders_class(models, dset_loaders, num_tests = args.num_tests)
#############################################################
############# write to csv file
def gen_outputline(fn, pred):
s = fn.split('/')[-1][:-4] + ','+ str(pred//3) + ',' + str(pred%3) + '\n'
print(s)
return s
# submission_fn = './result/submission_' + strftime('%m%d_%H%M') + '.csv'
submission_fn = './result/submission.csv'
f = open(submission_fn, 'w')
header = 'id,gender,accent\n'
f.write(header)
for i, fn in enumerate(fns):
f.write(gen_outputline(fn, pred_score[i]))
f.close()
print('done')