File size: 5,464 Bytes
5523920 |
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 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
#!/usr/bin/env python
#
# file: $ISIP_EXP/tuh_dpath/exp_0074/scripts/decode.py
#
# revision history:
# 20190925 (TE): first version
#
# usage:
# python decode.py odir mfile data
#
# arguments:
# odir: the directory where the hypotheses will be stored
# mfile: input model file
# data: the input data list to be decoded
#
# This script decodes data using a simple MLP model.
#------------------------------------------------------------------------------
# import pytorch modules
#
import torch
import torch.nn as nn
from tqdm import tqdm
# import the model and all of its variables/functions
#
from model import *
# visualize:
import numpy as np
# import modules
#
import sys
import os
from sklearn.metrics import explained_variance_score, mean_squared_error
#-----------------------------------------------------------------------------
#
# global variables are listed here
#
#-----------------------------------------------------------------------------
model_dir = './model/semantic_SemanticCNN_model.pth' # the path of model storage: 1400 is the best one
NUM_ARGS = 3
HYP_EXT = ".hyp"
GRT_EXT = ".grt"
# general global values
#
SPACE = " "
NEW_LINE = "\n"
#------------------------------------------------------------------------------
#
# the main program starts here
#
#------------------------------------------------------------------------------
### explained variance:
def explained_variance(input, target):
ev = 1 - np.var(target - input) / np.var(input)
return ev
# function: main
#
# arguments: none
#
# return: none
#
# This method is the main function.
#
def main(argv):
# ensure we have the correct amount of arguments
#
#global cur_batch_win
if(len(argv) != NUM_ARGS):
print("usage: python nedc_train_mdl.py [MDL_PATH] [TRAIN_PATH] [DEV_PATH]")
exit(-1)
# define local variables
#
# define local variables:
odir = argv[0]
mdl_path = argv[1]
pTest = argv[2]
# if the odir doesn't exist, we make it
#
if not os.path.exists(odir):
os.makedirs(odir)
# get the hyp file name
#
hyp_name = os.path.splitext(os.path.basename(pTest))[0] + HYP_EXT
grt_name = os.path.splitext(os.path.basename(pTest))[0] + GRT_EXT
# set the device to use GPU if available
#
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# get array of the data
# data: [[0, 1, ... 26], [27, 28, ...] ...]
# labels: [0, 0, 1, ...]
#
eval_dataset = NavDataset(pTest,'test')
eval_dataloader = torch.utils.data.DataLoader(eval_dataset, batch_size=1, \
shuffle=False, drop_last=True) #, pin_memory=True)
# instantiate a model:
model = SemanticCNN(Bottleneck, [2, 1, 1])
# moves the model to device (cpu in our case so no change):
model.to(device)
# set the model to evaluate
#
model.eval()
# set the loss criterion:
criterion = nn.MSELoss(reduction='sum')
criterion.to(device)
# load the weights
#
checkpoint = torch.load(mdl_path, map_location=device)
model.load_state_dict(checkpoint['model'])
# the output file
#
try:
ofile = open(os.path.join(odir, hyp_name), 'w+')
vel_file = open(os.path.join(odir, grt_name), 'w+')
except IOError as e:
print(os.path.join(odir, hyp_name))
print("[%s]: %s" % (hyp_name, e.strerror))
exit(-1)
# for each batch in increments of batch size:
counter = 0
running_loss = 0
# get the number of batches (ceiling of train_data/batch_size):
num_batches = int(len(eval_dataset)/eval_dataloader.batch_size)
with torch.no_grad():
for i, batch in tqdm(enumerate(eval_dataloader), total=num_batches):
#for i, batch in enumerate(dataloader, 0):
counter += 1
# collect the samples as a batch:
scan_maps = batch['scan_map']
scan_maps = scan_maps.to(device)
semantic_maps = batch['semantic_map']
semantic_maps = semantic_maps.to(device)
sub_goals = batch['sub_goal']
sub_goals = sub_goals.to(device)
velocities = batch['velocity']
velocities = velocities.to(device)
# feed the network the batch
#
output = model(scan_maps, semantic_maps, sub_goals)
#writer.add_graph(model,[batch_ped_pos_t, batch_scan_t, batch_goal_t])
# get the loss
#
loss = criterion(output, velocities)
# get the loss:
# multiple GPUs:
if torch.cuda.device_count() > 1:
loss = loss.mean()
running_loss += loss.item()
# write the highest probablity to the file
#
ofile.write(str(float(output.data.cpu().numpy()[0,0])) + \
SPACE + str(float(output.data.cpu().numpy()[0,1])) + NEW_LINE)
vel_file.write(str(float(velocities[0,0])) + \
SPACE + str(float(velocities[0,1])) + NEW_LINE)
# loss:
val_loss = running_loss / counter
print('Validation set: Average loss: {:.4f}'.format(val_loss))
# close the file
#
ofile.close()
vel_file.close()
# exit gracefully
#
return True
#
# end of function
# begin gracefully
#
if __name__ == '__main__':
main(sys.argv[1:])
#
# end of file
|