Skip to content

Commit 2fe226d

Browse files
committed
major update
1 parent bb2cf74 commit 2fe226d

12 files changed

+122
-113
lines changed

README.md

+5-2
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,12 @@ python main.py --mode onnx --cfg ./cfg/yolov3.cfg --gpus 0 --checkpoint ${saved_
9494

9595
target tensorrt version is 5.x
9696

97-
1. change upsample layer
97+
1. **ONNX_EXPORT = True** in 'model/yolov3.py'
98+
9899
tensorrt(v5.x) is not support upsample scale factor, so you have to change upsample layer not using scale factor.
99100

100101
```{r, engine='bash', count_lines}
101-
python main.py --mode onnx --cfg ./cfg/yolov3.cfg --gpus 0 --pretrained ./darknet53.conv.74 --checkpoint ${saved_checkpoint_path}
102+
python main.py --mode onnx --cfg ./cfg/yolov3.cfg --gpus 0 --checkpoint ${saved_checkpoint_path}
102103
```
103104

104105
### option
@@ -111,6 +112,8 @@ python main.py --mode onnx --cfg ./cfg/yolov3.cfg --gpus 0 --pretrained ./darkne
111112

112113
--checkpoint (optional) : the path of saved model checkpoint. Use it when you want to load the previous train, or you want to test(evaluate) the model.
113114

115+
--pretrained (optional) : the path of darknet pretrained weights. Use it when you want to fine-tuning the model.
116+
114117

115118

116119
## Visualize training graph

dataloader/data_transforms.py

+1-5
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,11 @@ def get_transformations(cfg_param = None, is_train = None):
1414
if is_train:
1515
data_transform = tf.Compose([AbsoluteLabels(),
1616
DefaultAug(),
17-
PadSquare(),
1817
RelativeLabels(),
1918
ResizeImage(new_size = (cfg_param['in_width'], cfg_param['in_height'])),
2019
ToTensor(),])
2120
elif not is_train:
2221
data_transform = tf.Compose([AbsoluteLabels(),
23-
PadSquare(),
2422
RelativeLabels(),
2523
ResizeImage(new_size = (cfg_param['in_width'], cfg_param['in_height'])),
2624
ToTensor(),])
@@ -51,12 +49,10 @@ def __init__(self, max_objects=50, is_debug=False):
5149
def __call__(self, data):
5250
image, labels = data
5351
if self.is_debug == False:
54-
image = torch.tensor(np.transpose(np.array(image, dtype=float) / 255,(2,0,1)),dtype=torch.float32)
52+
image = torch.tensor(np.ascontiguousarray(np.transpose(np.array(image, dtype=float) / 255,(2,0,1))),dtype=torch.float32)
5553
elif self.is_debug == True:
5654
image = torch.tensor(np.array(image, dtype=float),dtype=torch.float32)
5755
labels = torch.FloatTensor(np.array(labels))
58-
# filled_labels = np.zeros((self.max_objects, 5), np.float32)
59-
# filled_labels[range(len(labels))[:self.max_objects]] = labels[:self.max_objects]
6056
return image, labels
6157

6258
class KeepAspect(object):

dataloader/yolodata.py

+3-12
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,9 @@
55
import matplotlib
66
import matplotlib.pyplot as plt
77
from PIL import Image
8-
import matplotlib.patches as patches
98
import numpy as np
109
from util.tools import *
11-
from . import data_transforms
1210
import cv2
13-
import torchvision
14-
1511
class Yolodata(Dataset):
1612
file_dir = ""
1713
anno_dir = ""
@@ -59,7 +55,8 @@ def __getitem__(self, index):
5955

6056
with open(img_path, 'rb') as f:
6157
img = np.array(Image.open(img_path).convert('RGB'), dtype=np.uint8)
62-
#img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
58+
#img = cv2.imread(img_path)
59+
#img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
6360
img_origin_h, img_origin_w = img.shape[:2]
6461

6562
#if anno_dir is didnt exist, Test dataset
@@ -79,12 +76,7 @@ def __getitem__(self, index):
7976
#skip when no data
8077
if len(gt_data) < 5:
8178
continue
82-
#ignore very tiny GTs
8379
cx, cy, w, h = float(gt_data[1]), float(gt_data[2]), float(gt_data[3]), float(gt_data[4])
84-
#trunc = float(gt_data[5]) if len(gt_data) > 5 else 0
85-
#occ = float(gt_data[6]) if len(gt_data) > 6 else 0
86-
# if w <= 20 or float(gt_data[7]) - float(gt_data[5]) <= 20:
87-
# continue
8880
bbox.append([float(gt_data[0]), cx, cy, w, h])
8981

9082
#Change gt_box type
@@ -107,10 +99,9 @@ def __getitem__(self, index):
10799
return
108100
return img, target_data, anno_path
109101
else:
110-
bbox = np.array([[0,0,0,0,0]])
102+
bbox = np.array([[0,0,0,0,0]], dtype=np.float64)
111103
img, _ = self.transform((img, bbox))
112104
return img, None, None
113105

114-
115106
def __len__(self):
116107
return len(self.img_data)

demo/demo.py

+7-6
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,28 @@ def run(self):
1818
continue
1919
input_img, _, _ = batch
2020

21+
#drawBox(input_img.detach().numpy()[0])
22+
#np.save("torch_input.npy",input_img.detach().numpy())
23+
2124
input_img = input_img.to(self.device, non_blocking=True)
2225

2326
num_batch = input_img.shape[0]
2427
with torch.no_grad():
2528
start_time = time.time()
2629

2730
output = self.model(input_img)
28-
29-
# _, output_list = self.yololoss.compute_loss(output, targets = None, nw = nw, nh = nh, yolo_layers = self.model.yolo_layers)
30-
best_box_list = non_max_suppression(output, conf_thres=0.25, iou_thres=0.45)
31+
best_box_list = non_max_suppression(output,
32+
conf_thres=0.4,
33+
iou_thres=0.45)
3134

3235
for b in range(num_batch):
33-
3436
if best_box_list[b] is None:
3537
continue
38+
print(best_box_list[b])
3639
final_box_list = [bbox for bbox in best_box_list[b] if bbox[4] > 0.5]
3740
print("final :", final_box_list)
3841

3942
if final_box_list is None:
4043
continue
4144
show_img = input_img[b].detach().cpu().numpy()
4245
drawBoxlist(show_img, final_box_list, mode=1)
43-
44-

eval/evaluator.py

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import PIL, time
44
from train.loss import *
55
import csv
6-
import pandas as pd
76
from terminaltables import AsciiTable
87

98
class Evaluator:

main.py

+32-17
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os,sys
2+
from pandas import cut
23
# os.environ['CUDA_LAUNCH_BLOCKING'] = "1"
34
# os.environ["CUDA_VISIBLE_DEVICES"] = "0"
45

@@ -58,9 +59,12 @@ def collate_fn(batch):
5859
imgs, targets, anno_path = list(zip(*batch))
5960

6061
imgs = torch.stack([img for img in imgs])
62+
63+
if targets[0] is None or anno_path[0] is None:
64+
return imgs, None, None
65+
6166
for i, boxes in enumerate(targets):
6267
boxes[:, 0] = i
63-
6468
targets = torch.cat(targets,0)
6569

6670
return imgs, targets, anno_path
@@ -202,6 +206,21 @@ def demo(cfg_param = None, using_gpus = None):
202206
demo_loader = DataLoader(data, batch_size = 1, num_workers = 0, pin_memory = True, drop_last = False, shuffle = False, collate_fn=collate_fn)
203207

204208
model = DarkNet53(args.cfg, cfg_param)
209+
model.eval()
210+
211+
#load pre-trained darknet weights
212+
if args.pretrained is not None:
213+
print("load pretrained model")
214+
model.load_darknet_weights(args.pretrained)
215+
chkpt = {'epoch': -1,
216+
'best_fitness': None,
217+
'training_results': None,
218+
'model': model.state_dict(),
219+
'optimizer': None}
220+
target = args.pretrained.replace(".weights", ".pth")
221+
torch.save(chkpt, target)
222+
else:
223+
model.initialize_weights()
205224

206225
if args.checkpoint is not None:
207226
print("load pretrained model ", args.checkpoint)
@@ -220,6 +239,13 @@ def demo(cfg_param = None, using_gpus = None):
220239

221240
model = model.to(device)
222241
model.eval()
242+
243+
if args.pretrained is not None:
244+
darknet_weights_name = args.pretrained.replace(".weights", "_new.weights")
245+
elif args.checkpoint is not None:
246+
darknet_weights_name = args.checkpoint.replace(".pth", ".weights")
247+
model.save_darknet_weights(darknet_weights_name, cutoff=-1)
248+
223249
torch.backends.cudnn.benchmark = True
224250

225251
demo = Demo(model, data, demo_loader, device, cfg_param)
@@ -233,6 +259,8 @@ def torch2onnx(cfg_param = None, using_gpus = None):
233259
cfg_param['batch'] = 1
234260
model = DarkNet53(args.cfg, cfg_param)
235261

262+
if args.pretrained is not None:
263+
model.load_darknet_weights(args.pretrained)
236264
#Set the device what you use, GPU or CPU
237265
for i in using_gpus:
238266
print("GPU total memory : {} free memory : {}".format(get_memory_total_MiB(i), get_memory_free_MiB(i)))
@@ -260,28 +288,15 @@ def torch2onnx(cfg_param = None, using_gpus = None):
260288
#If checkpoint is existed, load the previous checkpoint.
261289
checkpoint = None
262290
if args.checkpoint is not None:
263-
print("load pretrained model ", args.checkpoint)
264-
checkpoint = torch.load(args.checkpoint)
265-
if len(using_gpus) == 0:
266-
post_name = ""
267-
else:
268-
pass
269-
# post_name = "module."
270-
# for key, value in checkpoint['model_state_dict'].copy().items():
271-
# new_key = post_name + key
272-
# checkpoint['model_state_dict'][new_key] = checkpoint['model_state_dict'].pop(key)
291+
print("load checkpoint model ", args.checkpoint)
292+
checkpoint = torch.load(args.checkpoint, map_location=torch.device('cpu'))
273293
model.load_state_dict(checkpoint['model_state_dict'])
274-
275-
# if len(using_gpus) > 0:
276-
# yolo_model = model.module
277-
# else:
278-
# yolo_model = model
279294

280295
model.eval()
281296

282297
darknet_weights_name = args.checkpoint.replace(".pth", ".weights")
283298
onnx_weights_name = args.checkpoint.replace(".pth", ".onnx")
284-
model.save_darknet_weights(darknet_weights_name)
299+
model.save_darknet_weights(darknet_weights_name, cutoff=-1)
285300

286301
#Pre-check the model structure and size of parameters
287302
#summary.summary(yolo_model, input_size=(3, cfg_param["in_width"], cfg_param["in_height"]), device='cuda' if device == torch.device('cuda') else 'cpu')
-137 Bytes
Binary file not shown.
-12.4 KB
Binary file not shown.

0 commit comments

Comments
 (0)