-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_benchmark.py
168 lines (135 loc) · 5.85 KB
/
test_benchmark.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
161
162
163
164
165
166
167
168
#author :alexhu
#time: 2020.4.30
#欢迎各位大佬指点交流:qq-》2473992731
import sys
sys.path.insert(0, '..')
import torch
import torch.nn as nn
from torch.autograd import Variable
import torch.nn.functional as F
from collections import OrderedDict
import numpy as np
import time
import pickle
import copy
from datasets import TestloadedDataset
import pandas as pd
#from models.modelconv3d import Network
import os
import glob
import argparse
import matplotlib
matplotlib.use("Pdf")
from matplotlib import colors
import matplotlib.pyplot as plt
from tqdm import tqdm
import pandas as pd
from utils import AverageMeter, save_checkpoint, adjust_learning_rate
#from losses import RMSE_Loss
import ipdb
import os
from models.PytorchUNet.unet.unet_model import UNet
import scipy.misc
import PIL.Image as Image
#####################
"""
文档说明
同train.py
测试时顺序加载数据,batchsize为1,不shuffle
请同样严格遵循数据接口
需要改路径或名称的地方将插入changepoint,顺序搜索即可
"""
#####################
parser = argparse.ArgumentParser(description = 'Test')
parser.add_argument('--batch_size', default=1, type=int, help='mini-batch size')
args = parser.parse_args()
##### the modelparameter path is :
#changepoint
unetsave_dir = '/media/workdir/hujh/hujh-new/huaweirader_baseline/model_parameters/classify_test/unet_model/'
test_npy_path = '/media/workdir/hujh/hujh-new/huaweirader_baseline/data_eda/test_demo.npy'
#convgrusave_dir = '/media/workdir/hujh/hujh-new/rader-baseline-alexhumaster/model_saving/convgru_bestmodel/'
valid_result_dir ='/media/workdir/hujh/hujh-new/huaweitest/'
valid_dir = ''
valid_path = '/media/workdir/hujh/hujh-new/huaweirader_baseline/data_eda/test_all_pkl.pkl'
valid_datasets = []
valid_loaders = []
all_datasets = TestloadedDataset(valid_dir, valid_path,test_npy_path )
valid_loaders = torch.utils.data.DataLoader(all_datasets,batch_size =1,shuffle=False)
#v_sample = torch.utils.data.SubsetRandomSampler(indices)
#valid_loaders = torch.utils.data.DataLoader(valid_datasets,batch_size =args.batch_size,sampler=v_sample)
print("the Testing loader is finish")
###### load the model to compare###############################################################
device0 = torch.device('cuda:0')
device1 = torch.device('cuda:1')
device2 = torch.device('cuda:2')
device3 = torch.device('cuda:3')
device = ['cuda:1','cuda:3','cuda:2','cuda:0']
model_unet = UNet(10,4).to(device1)
############## 请以如下方式加载集成模型################
#changepoint
if os.path.exists(os.path.join(unetsave_dir, 'unetcheckpoint.pth.tar')):
# load existing model
print('==> loading existing model')
g_info = torch.load(os.path.join(unetsave_dir, 'unetcheckpoint.pth.tar'))
model_unet.load_state_dict(g_info['state_dict'])
else:
print('==> No test model detected!')
exit(1)
"""
convgru_encoder = encoder(convgru_encoder_params[0], convgru_encoder_params[1]).to(device3)
convgru_decoder = decoder(convgru_forecaster_params[0], convgru_forecaster_params[1]).to(device3)
model_convgru = EF(convgru_encoder, convgru_decoder).to(device3)
if os.path.exists(os.path.join(convgrusave_dir, 'convgrucheckpoint.pth.tar')):
# load existing model
print('==> loading existing model')
g_info = torch.load(os.path.join(convgrusave_dir, 'convgrucheckpoint.pth.tar'))
model_convgru.load_state_dict(g_info['state_dict'])
else:
print('==> No test model detected!')
exit(1)
"""
#####################################请严格以如下有序字典方式添加集合成员###################################################
models = OrderedDict({
'unet': model_unet
})
######### mian #############
#model_run_avarage_time = dict()
with torch.no_grad():
current = 0
for name,model in models.items():
is_deeplearning_model = (torch.nn.Module in model.__class__.__bases__)
if is_deeplearning_model:
model.eval()
for ind, (filename, inputframes) in enumerate(valid_loaders):
print(filename)
ind =ind+1
savepath = os.path.join(valid_result_dir,filename[0])
if not os.path.isdir(savepath):
os.makedirs(savepath)
##################### 请严格按照集合模型的输入格式进行转换-bscwh ###########
##### 数据入口#######
if name =='unet':
test_inputs = inputframes.squeeze(2)
############################################################################
test_inputs = test_inputs.type(torch.FloatTensor).to(device1)
predict = model_unet(test_inputs)
###################### 数据出口bscwh即(1,4,256,256) ############
if name =='unet':
predict = predict.unsqueeze(2)
#################################################################
predict_np = np.clip(predict.detach().cpu().numpy(), 0, 1)
predict_np = predict_np * 80.0
predict_np = np.squeeze(predict_np)
#predict_np[predict_np<0.01] = 255
timefilename = [30,60,90,120]
#print(predict_np.shape)
for time in range(predict_np.shape[0]):
#print(time)
picnumber =timefilename[time]
picname = str(picnumber)+'.png'
pic_savepath = os.path.join(savepath,picname)
#scipy.misc.toimage(predict_np, high=255, low=0, cmin=0, cmax=255).save(pic_savepath)
predict_np_save = predict_np[time,:,:]
#print(predict_np_save.shape)
Image.fromarray(np.uint8(predict_np_save)).save(pic_savepath)
print('passing unity testing')