-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_for_tflite.py
227 lines (184 loc) · 9.15 KB
/
test_for_tflite.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
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from datetime import time
import tensorflow as tf
import argparse
import cv2
import numpy as np
# import matplotlib.pyplot as plt
import os
import torch
import torch.backends.cudnn as cudnn
import torchvision.transforms as transforms
# from tensorflow import keras
import time
from copy import deepcopy
#from inplace_abn import InPlaceABN
from datasets import datasets_necklace_last
#from networks import dml_csr
from utils import miou_necklaceLast
torch.multiprocessing.set_start_method("spawn", force=True)
DATA_DIRECTORY = 'D:/Dataset/CelebA-HQ+ETRI_Mask+CAMask_256'
SAVE_DIRECTORY = './TFLite/best_res18_CACAM_4colors_112_bn_noMaxP_interp_float32'
IGNORE_LABEL = 255
NUM_CLASSES = 20 # 20
TFLITE_PATH = "./TFLite/best_res18_CACAM_4colors_112_bn_noMaxP_interp_float32.tflite"
INPUT_SIZE = [112, 112]
def get_arguments():
"""Parse all the arguments provided from the CLI.
Returns:
A list of parsed arguments.
"""
parser = argparse.ArgumentParser(description="DML_CSR Network")
parser.add_argument("--batch-size", type=int, default=1,
help="Number of images sent to the network in one step.")
parser.add_argument("--data-dir", type=str, default=DATA_DIRECTORY,
help="Path to the directory containing the PASCAL VOC datasets.")
parser.add_argument("--out-dir", type=str, default=SAVE_DIRECTORY,
help="Path to the directory containing the PASCAL VOC datasets.")
parser.add_argument("--dataset", type=str, default='test',
help="Path to the file listing the images in the datasets.")
parser.add_argument("--ignore-label", type=int, default=IGNORE_LABEL,
help="The index of the label to ignore during the training.")
parser.add_argument("--num-classes", type=int, default=NUM_CLASSES,
help="Number of classes to predict (including background).")
#parser.add_argument("--restore-from", type=str, default='./snapshots/best.pth',
#help="Where restore model parameters from.")
parser.add_argument("--gpu", type=str, default='0',
help="choose gpu device.")
parser.add_argument("--input-size", type=str, default=INPUT_SIZE,
help="Comma-separated string with height and width of images.")
parser.add_argument("--local_rank", type=int, default=0,
help="choose gpu numbers")
parser.add_argument('--dist-backend', default='nccl', type=str,
help='distributed backend')
parser.add_argument("--model_type", type=int, default=0,
help="choose model type")
return parser.parse_args()
def one_image_test(interpreter, input_size):
height = input_size[0]
width = input_size[1]
image = cv2.imread("./CelebAMask-HQ_256/test/images/00002.jpg", cv2.IMREAD_COLOR)
image = image.transpose(2, 1, 0) # CHW -> HWC
image = np.expand_dims(image, axis=0)
image = tf.cast(image, tf.float32)
# print(model.structured_outputs) -> {'output_0': TensorSpec(shape=(None, 20, 64, 64), dtype=tf.float32, name='output_0')}
# results = model(image)
# Get input and output tensors.
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()
# Test model on random input data.
# print(image)
s = time.time()
interpreter.set_tensor(input_details[0]['index'], image)
interpreter.invoke()
# The function `get_tensor()` returns a copy of the tensor data.
# Use `tensor()` in order to get a pointer to the tensor.
output_data = interpreter.get_tensor(output_details[0]['index'])
curr_time = (time.time()-s)
print(curr_time)
# print(output_data)
# parsing = output_data['output_0'].numpy()
parsing = output_data
# parsing = results['output_0'].numpy()
# parsing = cv2.resize(parsing, (height, width))
# interp = tf.keras.layers.UpSampling2D(size=(height, width), interpolation='bilinear')
parsing = parsing.transpose(0, 2, 3, 1) # NCHW NHWC
interp = torch.nn.Upsample(size=(height, width), mode='bilinear', align_corners=True)
parsing = interp(torch.tensor(parsing)).data.cpu().numpy()
print(parsing.shape)
parsing_preds = np.asarray(np.argmax(parsing, axis=3))
def valid_for_test(interpreter, valloader, input_size, num_samples, dir=None, dir_edge=None, dir_img=None):
height = input_size[0]
width = input_size[1]
with torch.autograd.profiler.profile(enabled=True, use_cuda=True, \
record_shapes=False, profile_memory=False) as prof:
#model.eval()
parsing_preds = np.zeros((num_samples, height, width), dtype=np.uint8)
scales = np.zeros((num_samples, 2), dtype=np.float32)
centers = np.zeros((num_samples, 2), dtype=np.int32)
idx = 0
interp = torch.nn.Upsample(size=(height, width), mode='bilinear', align_corners=True)
with torch.no_grad():
for index, batch in enumerate(valloader):
image, meta = batch
num_images = image.size(0)
if index % 10 == 0:
print('%d processd' % (index * num_images))
c = meta['center'].numpy()
s = meta['scale'].numpy()
scales[idx:idx + num_images, :] = s[:, :]
centers[idx:idx + num_images, :] = c[:, :]
image = image.transpose(3, 1) # CHW -> HWC
# print(image.shape)
image = tf.cast(image, tf.float32)
# print(model.structured_outputs) -> {'output_0': TensorSpec(shape=(None, 20, 64, 64), dtype=tf.float32, name='output_0')}
# results = model(image)
# Get input and output tensors.
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()
# Test model on random input data.
interpreter.set_tensor(input_details[0]['index'], image)
interpreter.invoke()
# The function `get_tensor()` returns a copy of the tensor data.
# Use `tensor()` in order to get a pointer to the tensor.
output_data = interpreter.get_tensor(output_details[0]['index'])
# print(output_data)
# parsing = output_data['output_0'].numpy()
parsing = output_data
print(parsing.shape)
# parsing = interp(torch.tensor(parsing)).data.cpu().numpy()
# parsing = parsing.transpose(0, 2, 3, 1) # NCHW NHWC
# parsing_preds[idx:idx + num_images, :, :] = np.asarray(np.argmax(parsing, axis=3), dtype=np.uint8)
parsing_preds[idx:idx + num_images, :, :] = np.asarray(parsing, dtype=np.uint8)
idx += num_images
if dir is not None:
for i in range(len(meta['name'])):
# cv2.imwrite(os.path.join(dir, meta['name'][i] + '.png'), np.asarray(np.argmax(parsing, axis=3))[i])
cv2.imwrite(os.path.join(dir, meta['name'][i] + '.png'), parsing[0]*10)
parsing_preds = parsing_preds[:num_samples, :, :]
return parsing_preds, scales, centers
def main():
"""Create the model and start the evaluation process."""
args = get_arguments()
os.environ["CUDA_VISIBLE_DEVICES"]=args.gpu
gpus = [int(i) for i in args.gpu.split(',')]
# print(args.gpu)
cudnn.benchmark = True
cudnn.enabled = True
# model = onnx.load(ONNX_PATH)
# model = dml_csr.DML_CSR(args.num_classes, InPlaceABN, False)
# model = tf.saved_model.load(PB_PATH)
# model = model.signatures["serving_default"]
#print(f(x=tf.constant([[1.]])))
# Load TFLite model and allocate tensors.
interpreter = tf.lite.Interpreter(TFLITE_PATH)
interpreter.allocate_tensors()
normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
transform = transforms.Compose([
transforms.ToTensor(),
normalize,
])
dataset = datasets_necklace_last.FaceDataSet(args.data_dir, args.dataset, crop_size=INPUT_SIZE, transform=transform)
num_samples = len(dataset)
valloader = torch.utils.data.DataLoader(dataset, batch_size=args.batch_size, shuffle=False, pin_memory=True)
#restore_from = args.restore_from
#print(restore_from)
#state_dict = torch.load(restore_from, map_location='cuda:0')
#model.load_state_dict(state_dict)
#model.cuda()
#model.eval()
save_path = os.path.join(args.out_dir, args.dataset, 'parsing')
if not os.path.exists(save_path):
os.makedirs(save_path)
# one_image_test(interpreter, INPUT_SIZE)
parsing_preds, scales, centers = valid_for_test(interpreter, valloader, INPUT_SIZE, num_samples, save_path)
mIoU, f1 = miou_necklaceLast.compute_mean_ioU(parsing_preds, scales, centers, args.num_classes, args.data_dir, INPUT_SIZE, args.dataset, reverse=True)
print(mIoU)
print(f1)
if __name__ == '__main__':
main()