-
Notifications
You must be signed in to change notification settings - Fork 6
/
generate_test_file
123 lines (91 loc) · 3.37 KB
/
generate_test_file
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
############################################################################
# Generates test data by loading the trained model
# April 2020
# Ravi Konkimalla, Neil Rodrigues | University of Pennsylvania
############################################################################
import os
import time
import numpy
import torch
import math
import pdb
import sys
import numpy as np
import torch.nn as nn
from PIL import Image, ImageOps
import torchvision.models as models
from torch.optim import SGD, Adam
from torch.autograd import Variable
from dataloader import Squeeze_Seg
from torch.utils.data import DataLoader
from torchvision.transforms import ToTensor, ToPILImage, Resize
import matplotlib.pyplot as plt
from utils.util_iou_eval import iouEval
from dataloader import Squeeze_Seg
from infer_config import *
sys.path.append(os.path.join(ARGS_ROOT,'models',ARGS_MODEL_NAME+'/'))
print(ARGS_MODEL_NAME)
module = __import__(ARGS_MODEL_NAME)
Network = getattr(module,"Net")
from utils.util_iou_eval import iouEval, getColorEntry
from utils.calculate_weights import load_class_weights
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
def load_my_state_dict(model, state_dict):
own_state = model.state_dict()
for name, param in state_dict.items():
if name not in own_state:
print("[weight not copied for %s]"%(name))
continue
own_state[name].copy_(param)
return model
def test(model,output_dir):
print('Total Number of classes is {}'.format(NUM_CLASSES))
dataset_val = Squeeze_Seg(ROOT_DIR,'08',ARGS_INPUT_TYPE_1,ARGS_INPUT_TYPE_2)
loader_val = DataLoader(
dataset_val,
num_workers = ARGS_NUM_WORKERS,
batch_size = ARGS_VAL_BATCH_SIZE,
shuffle = False)
weight = load_class_weights()
iouEvalVal = iouEval(NUM_CLASSES)
labels = np.array([(0,0,0),(255,0,0),(0,255,0),(0,0,255)] )
model.eval()
total_time=0
iters = 00
for step, (image,image_2,mask,label) in enumerate(loader_val):
if ARGS_CUDA:
image = image.cuda()
image_2 = image_2.cuda()
label = label.cuda()
mask = mask.cuda()
image = Variable(image)
label = Variable(label)
mask = Variable(mask)
image_2 = Variable(image_2)
print(image.shape)
output = model(image,image_2,mask)
label_out = output[0].max(0)[1].data
image = image[0,:,:,:].permute(1,2,0)
image = image[:,:,:3]
image = torch.cat((image,label_out.view(64,512,1).float()),axis = 2)
output_path = output_dir + str(iters)
np.save(output_path, image.cpu().numpy())
print(iters)
iters += 1
if iters >= 400:
break
if __name__ == "__main__":
print(device)
model_type = 'res_fcn'
models_pth = 'E:/project_522/train_models/'
model_pth = models_pth + model_type + '.pth'
model = Network(data_dict)
torch.set_num_threads(ARGS_NUM_WORKERS)
model.load_state_dict(torch.load(model_pth))
data_type = 0
seq_str_num = '08'
pgm_dir = 'E:/project_522/Kitti/pgm_output/' + seq_str_num +'/'
output_dir = 'E:/project_522/Kitti/pgm_test/' + model_type + '/updated' + '/'
if not os.path.exists(output_dir):
os.makedirs(output_dir)
test(model,output_dir)