def run_video(): timer = Timer() sum = 0 cap = cv2.VideoCapture('ne1.avi') # capture from camera predictor = load_model() fourcc = cv2.VideoWriter_fourcc(*'H264') out = cv2.VideoWriter('output.mp4', fourcc, 30.0, (1920, 1080)) while True: ret, orig_image = cap.read() if orig_image is None: break image = cv2.cvtColor(orig_image, cv2.COLOR_BGR2RGB) timer.start() start = time.time() boxes, labels, probs = predictor.predict(image, candidate_size / 2, threshold) # if labels.size(0) == 0: # continue # else: interval = timer.end() fps = 1 / (time.time() - start) print('FPS: {} Time: {:.6f}s, Detect Objects: {:d}.'.format( fps, interval, labels.size(0))) for i in range(boxes.size(0)): box = boxes[i, :] score = f" {probs[i]:.2f}" label = labels[i] cv2.rectangle(orig_image, (box[0], box[1]), (box[2], box[3]), (0, 255, 0), 4) cv2.putText( orig_image, "{}-{}".format(score, class_names[label]), (box[0], box[1] - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, # font scale (0, 0, 255), 2) # line type orig_image = cv2.resize(orig_image, None, None, fx=0.8, fy=0.8) sum += boxes.size(0) cv2.imshow('annotated', orig_image) if cv2.waitKey(1) & 0xFF == ord('q'): break # out.write(orig_image) cap.release() out.release() cv2.destroyAllWindows() print("all face num:{}".format(sum))
def main(args): net_type = args.net_type img_folder = args.img_folder model_path = args.weights_path label_path = args.label_path class_names = [name.strip() for name in open(label_path).readlines()] out_path = args.out_path if not os.path.exists(out_path): os.mkdir(out_path) num_gpus = torch.cuda.device_count() device = 'cuda' if num_gpus else 'cpu' if net_type == 'vgg16-ssd': net = create_vgg_ssd(len(class_names), is_test=True) elif net_type == 'mb1-ssd': net = create_mobilenetv1_ssd(len(class_names), is_test=True) elif net_type == 'mb1-ssd-lite': net = create_mobilenetv1_ssd_lite(len(class_names), is_test=True) elif net_type == 'mb2-ssd-lite': net = create_mobilenetv2_ssd_lite(len(class_names), is_test=True) elif net_type == 'sq-ssd-lite': net = create_squeezenet_ssd_lite(len(class_names), is_test=True) #elif net_type == 'mb3-ssd-lite': # net = create_mobilenetv3_ssd_lite(len(class_names), is_test=True) else: print( "The net type is wrong. It should be one of vgg16-ssd, mb1-ssd and mb1-ssd-lite." ) sys.exit(1) net.load(model_path) if net_type == 'vgg16-ssd': predictor = create_vgg_ssd_predictor(net, candidate_size=20, device=device) elif net_type == 'mb1-ssd': predictor = create_mobilenetv1_ssd_predictor(net, candidate_size=20, device=device) elif net_type == 'mb1-ssd-lite': predictor = create_mobilenetv1_ssd_lite_predictor(net, candidate_size=20, device=device) elif net_type == 'mb2-ssd-lite': predictor = create_mobilenetv2_ssd_lite_predictor(net, candidate_size=20, device=device) elif net_type == 'sq-ssd-lite': predictor = create_squeezenet_ssd_lite_predictor(net, candidate_size=20, device=device) #elif net_type == 'mb3-ssd-lite': # predictor = create_mobilenetv3_ssd_lite_predictor(net, candidate_size=10) else: print( "The net type is wrong. It should be one of vgg16-ssd, mb1-ssd and mb1-ssd-lite." ) sys.exit(1) timer = Timer() img_names = glob.glob(img_folder + os.sep + '*.jpg') #result_csv=os.path.join(out_path,'rest_result.csv') if len(img_names) == 0: print('No imagesfound in {}'.format(img_folder)) exit(-1) for img_name in img_names: image = cv2.imread(img_name) timer.start() boxes, labels, probs = predictor.predict(image, 10, 0.3) interval = timer.end() print('Time: {:.2f}s, Detect Objects: {:d}.'.format( interval, labels.size(0))) label_text = [] for i in range(boxes.size(0)): box = boxes[i, :] label = f"{class_names[labels[i]]}: {probs[i]:.2f}" label_text.append(label) cv2.rectangle(image, (box[0], box[1]), (box[2], box[3]), (255, 255, 0), 4) cv2.putText(image, label, (box[0] + 20, box[1] + 40), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 255), 2) if args.store_result: new_name = '{}/{}'.format(out_path, img_name.split('/')[-1]) cv2.imwrite(new_name, image) if not label_text: result_label = 'empty' else: result_label = label_text[0] with open(os.path.join(out_path, 'rest_result.csv'), 'a+') as result_writer: result_writer.write( img_name.split('/')[-1] + ',' + result_label + '\n') cv2.imshow('result', image) if cv2.waitKey(1) & 0xFF == ord('q'): break cv2.destroyAllWindows()
class WagonDetector: def __init__(self, net_type, label_path, model_path, top_k=10, prob_threshold=0.4): self.class_names = ['BACKGROUND'] + [ name.strip() for name in open(label_path).readlines() ] self.net = self._create_network(net_type) self.net.load(model_path) self.predictor = self._create_predictor(net_type) self.top_k = top_k self.prob_threshold = prob_threshold self.timer = Timer() def _create_network(self, net_type): if net_type == 'vgg16-ssd': return create_vgg_ssd(len(self.class_names), is_test=True) elif net_type == 'mb1-ssd': return create_mobilenetv1_ssd(len(self.class_names), is_test=True) elif net_type == 'mb1-ssd-lite': return create_mobilenetv1_ssd_lite(len(self.class_names), is_test=True) elif net_type == 'mb2-ssd-lite': return create_mobilenetv2_ssd_lite(len(self.class_names), is_test=True) elif net_type == 'sq-ssd-lite': return create_squeezenet_ssd_lite(len(self.class_names), is_test=True) else: raise RuntimeError( "The net type is wrong. It should be one of vgg16-ssd, mb1-ssd and mb1-ssd-lite." ) def _create_predictor(self, net_type): if net_type == 'vgg16-ssd': return create_vgg_ssd_predictor(self.net, candidate_size=200) elif net_type == 'mb1-ssd': return create_mobilenetv1_ssd_predictor(self.net, candidate_size=200) elif net_type == 'mb1-ssd-lite': return create_mobilenetv1_ssd_lite_predictor(self.net, candidate_size=200) elif net_type == 'mb2-ssd-lite': return create_mobilenetv2_ssd_lite_predictor(self.net, candidate_size=200) elif net_type == 'sq-ssd-lite': return create_squeezenet_ssd_lite_predictor(self.net, candidate_size=200) else: raise RuntimeError( "The net type is wrong. It should be one of vgg16-ssd, mb1-ssd and mb1-ssd-lite." ) def __call__(self, image): image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) self.timer.start() boxes, labels, probs = self.predictor.predict(image, self.top_k, self.prob_threshold) interval = self.timer.end() print('Time: {:.2f}s, Detect Objects: {:d}.'.format( interval, labels.size(0))) return boxes, labels, probs
params = itertools.chain(net.regression_headers.parameters(), net.classification_headers.parameters()) logging.info("Freeze all the layers except prediction heads.") else: params = [ {'params': net.base_net.parameters(), 'lr': base_net_lr}, {'params': itertools.chain( net.source_layer_add_ons.parameters(), net.extras.parameters() ), 'lr': extra_layers_lr}, {'params': itertools.chain( net.regression_headers.parameters(), net.classification_headers.parameters() )} ] timer.start("Load Model") if args.resume: logging.info(f"Resume from the model {args.resume}") net.load(args.resume) elif args.base_net: logging.info(f"Init from base net {args.base_net}") net.init_from_base_net(args.base_net) elif args.pretrained_ssd: logging.info(f"Init from pretrained ssd {args.pretrained_ssd}") net.init_from_pretrained_ssd(args.pretrained_ssd) logging.info(f'Took {timer.end("Load Model"):.2f} seconds to load the model.') net.to(DEVICE) criterion = MultiboxLoss(config.priors, iou_threshold=0.5, neg_pos_ratio=3, center_variance=0.1, size_variance=0.2, device=DEVICE,args.keepHNM)
logging.info(val_dataset) elif args.dataset_type == 'tfrecord': val_dataset = RecordDataset(args.validation_dataset, transform=test_transform, target_transform=target_transform, is_test=True, batch_size=args.batch_size, shuffle=False) else: logging.critical("Dataset type is unsupported. Choose on of voc, open_images, or tfrecord") raise SystemExit(-1) logging.info("validation dataset size: {}".format(val_dataset.num_records)) """ Build the network """ logging.info("Build network.") timer.start("Create Model") net = create_net(num_classes, is_train=True) logging.info(f'Took {timer.end("Create Model"):.2f} seconds to create the model.') last_epoch = 0 if args.freeze_base_net: logging.info("Freeze base net.") net.base_net.trainable = False elif args.freeze_net: for layer in net.ssd.layers: if layer not in net.regression_headers and layer not in net.classification_headers: layer.trainable = False logging.info("Freeze all the layers except prediction heads.") """ Set up loss, optimizer, and callbacks
timer = Timer() args.eval_epoch = 199 dataset = VOCDataset(args.dataset_path, is_test=True) checkpoint_dir = 'output/voc-320-{}-cosine-e200-lr0.010'.format(args.net) args.eval_dir = "{}/eval_results".format(checkpoint_dir) eval_path = pathlib.Path(args.eval_dir) eval_path.mkdir(exist_ok=True) args.model = '{}/{}-Epoch-{}.pth'.format(checkpoint_dir, args.net, args.eval_epoch) args.label_file = './voc-model-labels.txt' class_names = [name.strip() for name in open(args.label_file).readlines()] if not os.path.isdir(args.eval_dir): os.makedirs(args.eval_dir) logger = create_logger(args.eval_dir, is_train=False) timer.start("Load Model") net = create_ssd_lite(len(class_names), arch=args.net, is_test=True) net.eval() params = count_parameters_in_MB(net) logger.info("Params = %.2fMB" % params) mult_adds = comp_multadds(net, input_size=(3, 320, 320)) logger.info("Mult-Adds = %.2fMB" % mult_adds) net.load(args.model) net = net.to(DEVICE) predictor = create_ssd_lite_predictor(net, nms_method=args.nms_method, device=DEVICE) logger.info( f'It took {timer.end("Load Model")} seconds to load the model.') results = []
elif args.net == 'mb1-ssd-lite': net = create_mobilenetv1_ssd_lite(len(class_names), is_test=True) elif args.net == 'sq-ssd-lite': net = create_squeezenet_ssd_lite(len(class_names), is_test=True) elif args.net == 'mb2-ssd-lite': net = create_mobilenetv2_ssd_lite(len(class_names), width_mult=args.mb2_width_mult, is_test=True) else: logging.fatal( "The net type is wrong. It should be one of vgg16-ssd, mb1-ssd and mb1-ssd-lite." ) parser.print_help(sys.stderr) sys.exit(1) timer.start("Load Model") # net.load(args.trained_model) net.load_checkpoint(args.trained_model) net = net.to(DEVICE) print(f'It took {timer.end("Load Model")} seconds to load the model.') if args.net == 'vgg16-ssd': predictor = create_vgg_ssd_predictor(net, nms_method=args.nms_method, device=DEVICE) elif args.net == 'mb1-ssd': predictor = create_mobilenetv1_ssd_predictor( net, nms_method=args.nms_method, device=DEVICE) elif args.net == 'mb1-ssd-lite': predictor = create_mobilenetv1_ssd_lite_predictor( net, nms_method=args.nms_method, device=DEVICE) elif args.net == 'sq-ssd-lite':
predictor = create_mobilenetv2_ssd_lite_predictor(net, candidate_size=200) elif net_type == 'sq-ssd-lite': predictor = create_squeezenet_ssd_lite_predictor(net, candidate_size=200) else: print( "The net type is wrong. It should be one of vgg16-ssd, mb1-ssd and mb1-ssd-lite." ) sys.exit(1) timer = Timer() while True: ret, orig_image = cap.read() if orig_image is None: continue image = cv2.cvtColor(orig_image, cv2.COLOR_BGR2RGB) timer.start() boxes, labels, probs = predictor.predict(image, 10, 0.4) interval = timer.end() print('Time: {:.2f}s, Detect Objects: {:d}.'.format( interval, labels.size(0))) for i in range(boxes.size(0)): box = boxes[i, :].int() label = f"{class_names[labels[i]]}: {probs[i]:.2f}" cv2.rectangle(orig_image, (box[0], box[1]), (box[2], box[3]), (255, 255, 0), 4) cv2.putText( orig_image, label, (box[0] + 20, box[1] + 40), cv2.FONT_HERSHEY_SIMPLEX,
def get_map(trained_model, label_file): eval_dir = "eval_results" eval_path = pathlib.Path(eval_dir) eval_path.mkdir(exist_ok=True) timer = Timer() class_names = [name.strip() for name in open(label_file).readlines()] dataset_path = "/home/qindanfeng//work/YOLOv3/datasets/VOC/VOCtest_06-Nov-2007/VOCdevkit/VOC2007" dataset = VOCDataset(dataset_path, is_test=True) true_case_stat, all_gb_boxes, all_difficult_cases = group_annotation_by_class( dataset) net = create_mobilenetv2_ssd_lite(len(class_names), width_mult=1.0, is_test=True) timer.start("Load Model") net.load(trained_model) net = net.to(DEVICE) predictor = create_mobilenetv2_ssd_lite_predictor(net, nms_method="hard", device=DEVICE) results = [] for i in range(len(dataset)): print("process image", i) timer.start("Load Image") image = dataset.get_image(i) print("Load Image: {:4f} seconds.".format(timer.end("Load Image"))) timer.start("Predict") boxes, labels, probs = predictor.predict(image) print("Prediction: {:4f} seconds.".format(timer.end("Predict"))) indexes = torch.ones(labels.size(0), 1, dtype=torch.float32) * i results.append( torch.cat( [ indexes.reshape(-1, 1), labels.reshape(-1, 1).float(), probs.reshape(-1, 1), boxes + 1.0 # matlab's indexes start from 1 ], dim=1)) results = torch.cat(results) for class_index, class_name in enumerate(class_names): if class_index == 0: continue # ignore background prediction_path = eval_path / f"det_test_{class_name}.txt" with open(prediction_path, "w") as f: sub = results[results[:, 1] == class_index, :] for i in range(sub.size(0)): prob_box = sub[i, 2:].numpy() image_id = dataset.ids[int(sub[i, 0])] print(image_id + " " + " ".join([str(v) for v in prob_box]), file=f) aps = [] print("\n\nAverage Precision Per-class:") for class_index, class_name in enumerate(class_names): if class_index == 0: continue prediction_path = eval_path / f"det_test_{class_name}.txt" ap = compute_average_precision_per_class( true_case_stat[class_index], all_gb_boxes[class_index], all_difficult_cases[class_index], prediction_path, 0.5, True) aps.append(ap) print(f"{class_name}: {ap}") print(f"\nAverage Precision Across All Classes:{sum(aps) / len(aps)}") return sum(aps) / len(aps)
def PersonDetector(orig_image, net_type="mb1-ssd"): class_names = [name.strip() for name in open(label_path).readlines()] num_classes = len(class_names) if net_type == 'vgg16-ssd': net = create_vgg_ssd(len(class_names), is_test=True) elif net_type == 'mb1-ssd': net = create_mobilenetv1_ssd(len(class_names), is_test=True) elif net_type == 'mb1-ssd-lite': net = create_mobilenetv1_ssd_lite(len(class_names), is_test=True) elif net_type == 'mb2-ssd-lite': net = create_mobilenetv2_ssd_lite(len(class_names), is_test=True) elif net_type == 'sq-ssd-lite': net = create_squeezenet_ssd_lite(len(class_names), is_test=True) else: print( "The net type is wrong. It should be one of vgg16-ssd, mb1-ssd and mb1-ssd-lite." ) sys.exit(1) net.load(model_path) if net_type == 'vgg16-ssd': predictor = create_vgg_ssd_predictor(net, candidate_size=200) elif net_type == 'mb1-ssd': predictor = create_mobilenetv1_ssd_predictor(net, candidate_size=200) elif net_type == 'mb1-ssd-lite': predictor = create_mobilenetv1_ssd_lite_predictor(net, candidate_size=200) elif net_type == 'mb2-ssd-lite': predictor = create_mobilenetv2_ssd_lite_predictor(net, candidate_size=200) elif net_type == 'sq-ssd-lite': predictor = create_squeezenet_ssd_lite_predictor(net, candidate_size=200) else: print( "The net type is wrong. It should be one of vgg16-ssd, mb1-ssd and mb1-ssd-lite." ) sys.exit(1) timer = Timer() image = cv2.cvtColor(orig_image, cv2.COLOR_BGR2RGB) timer.start() boxes, labels, probs = predictor.predict(image, 10, 0.4) interval = timer.end() print('Time: {:.2f}s, Detect Objects: {:d}.'.format( interval, labels.size(0))) max_width = -1 x, y, w, h = None, None, None, None for i in range(boxes.size(0)): box = boxes[i, :] label = f"{class_names[labels[i]]}: {probs[i]:.2f}" if (max_width < box[2] - box[0]): x, y = box[0], box[1] w, h = box[2] - box[0], box[3] - box[1] max_width = w if (x is not None and y is not None and w is not None and h is not None): cv2.rectangle(orig_image, (x, y), (w + x, h + y), (255, 255, 0), 4) return (x, y, w, h) # img = cv2.imread("Image/img.jpg") # PersonDetector(img)
def main(): timer = Timer() DEVICE = torch.device("cuda:0" if args.use_cuda and torch.cuda. is_available() and args.use_cuda else "cpu") print("=== Use ", DEVICE) classes = [] with open("./models/char_obj_EN.names", "r") as f: lines = f.readlines() for i in lines: if "\n" in i: classes.append(i.split("\n")[0]) else: classes.append(i) class_names = classes print("label size is ", len(class_names)) net = create_mobilenetv2_ssd_lite(len(class_names), width_mult=args.mb2_width_mult, is_test=True) timer.start("Load Model") net.load(args.trained_model) net = net.to(DEVICE) print(f'{timer.end("Load Model")} sec to load the model.') predictor = create_mobilenetv2_ssd_lite_predictor( net, nms_method=args.nms_method, device=DEVICE) test_list = [ x for x in os.listdir(args.test_folder) if x.split(".")[1] == "jpg" ] total_time = 0.0 for i in test_list: # Read Images img_path = args.test_folder + "/" + i image = cv2.imread(img_path) cvt_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) # Predict timer.start("Predict") boxes, labels, probs = predictor.predict(cvt_image) end_time = timer.end("Predict") print("Prediction: {:4f} sec.".format(end_time)) total_time += end_time # to numpy bb = boxes.numpy() lb = labels.numpy() pb = probs.numpy() save_name = "" # resize s_factor = 4 rows, cols = image.shape[:2] image = cv2.resize(image, (cols * s_factor, rows * s_factor)) # score > 0.5 for b in range(pb.size): if pb[b] > args.prob_thresh: cv2.rectangle( image, (int(bb[b][0] * s_factor), int(bb[b][1] * s_factor)), (int(bb[b][2] * s_factor), int(bb[b][3] * s_factor)), (0, 255, 0), 2) cv2.putText( image, class_names[int(lb[b]) + 1], (int(bb[b][0] * s_factor), int(bb[b][1] * s_factor)), cv2.FONT_HERSHEY_SIMPLEX, 1.5, (0, 0, 255), 2) save_name += class_names[int(lb[b]) + 1] cv2.imwrite("./output/Final_Testset/" + save_name + ".jpg", image) print("Input Image : {} ====> Predict : {}".format(i, save_name)) print("Avg Time is {:4f} sec.".format(total_time / len(test_list)))
def main(): if len(sys.argv) < 4: print( 'Usage: python run_ssd_example.py <net type> <model path> <label path> [video file]' ) sys.exit(0) net_type = sys.argv[1] model_path = sys.argv[2] label_path = sys.argv[3] if len(sys.argv) >= 5: cap = cv2.VideoCapture(sys.argv[4]) # capture from file else: cap = cv2.VideoCapture(0) # capture from camera cap.set(3, 1920) cap.set(4, 1080) class_names = [name.strip() for name in open(label_path).readlines()] net = net_select(model_type=net_type, class_names=class_names) predictor = create_predictor(model=net, model_type=net_type) net.load(model_path) count = 0 max_count = 10 fps = 0 timer = Timer() tm = cv2.TickMeter() tm.start() while True: ret, orig_image = cap.read() if orig_image is None: continue # for fps count if count == max_count: tm.stop() fps = max_count / tm.getTimeSec() tm.reset() tm.start() count = 0 image = cv2.cvtColor(orig_image, cv2.COLOR_BGR2RGB) timer.start() boxes, labels, probs = predictor.predict(image, 10, 0.4) interval = timer.end() print('Time: {:.3f}s, Detect Objects: {:d}.'.format( interval, labels.size(0))) # fpsの書き込み cv2.putText(orig_image, f'{fps:.2f}', (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 0), 2) for i in range(boxes.size(0)): # ボックスの書き込み box = boxes[i, :] label = f"{class_names[labels[i]]}: {probs[i]:.3f}" # ラベルと確信度の書き込み cv2.rectangle(orig_image, (box[0], box[1]), (box[2], box[3]), (255, 255, 0), 4) cv2.putText( orig_image, label, (box[0] + 20, box[1] + 40), cv2.FONT_HERSHEY_SIMPLEX, 1, # font scale (255, 0, 255), 2) # line type cv2.imshow('Capture Demo', orig_image) count += 1 if cv2.waitKey(1) & 0xFF == ord('q'): break cap.release() cv2.destroyAllWindows()
def main(args): DEVICE = torch.device( "cuda:0" if torch.cuda.is_available() and args.use_cuda else "cpu") #DEVICE = torch.device("cpu") if args.use_cuda and torch.cuda.is_available(): torch.backends.cudnn.benchmark = True logging.info("Use Cuda.") timer = Timer() logging.info(args) if args.net == 'vgg16-ssd': create_net = create_vgg_ssd config = vgg_ssd_config elif args.net == 'mb1-ssd': create_net = create_mobilenetv1_ssd config = mobilenetv1_ssd_config elif args.net == 'mb1-ssd-lite': create_net = create_mobilenetv1_ssd_lite config = mobilenetv1_ssd_config elif args.net == 'sq-ssd-lite': create_net = create_squeezenet_ssd_lite config = squeezenet_ssd_config elif args.net == 'mb2-ssd-lite': create_net = lambda num: create_mobilenetv2_ssd_lite( num, width_mult=args.mb2_width_mult) config = mobilenetv1_ssd_config else: logging.fatal("The net type is wrong.") parser.print_help(sys.stderr) sys.exit(1) train_transform = TrainAugmentation(config.image_size, config.image_mean, config.image_std) target_transform = MatchPrior(config.priors, config.center_variance, config.size_variance, 0.5) test_transform = TestTransform(config.image_size, config.image_mean, config.image_std) logging.info("Prepare training datasets.") datasets = [] for dataset_path in args.datasets: if args.dataset_type == 'voc': dataset = VOCDataset(dataset_path, transform=train_transform, target_transform=target_transform) label_file = os.path.join(args.checkpoint_folder, "voc-model-labels.txt") store_labels(label_file, dataset.class_names) num_classes = len(dataset.class_names) elif args.dataset_type == 'open_images': dataset = OpenImagesDataset(dataset_path, transform=train_transform, target_transform=target_transform, dataset_type="train", balance_data=args.balance_data) label_file = os.path.join(args.checkpoint_folder, "open-images-model-labels.txt") store_labels(label_file, dataset.class_names) logging.info(dataset) num_classes = len(dataset.class_names) elif args.dataset_type == 'coco': # root, annFile, transform=None, target_transform=None, transforms=None) # dataset_type="train", balance_data=args.balance_data) dataset = CocoDetection( "/home/wenyen4desh/datasets/coco/train2017", "/home/wenyen4desh/datasets/coco/annotations/instances_train2017.json", transform=train_transform, target_transform=target_transform) label_file = os.path.join(args.checkpoint_folder, "open-images-model-labels.txt") store_labels(label_file, dataset.class_names) logging.info(dataset) num_classes = len(dataset.class_names) # raise ValueError("Dataset type {} yet implement.".format(args.dataset_type)) else: raise ValueError("Dataset type {} is not supported.".format( args.dataset_type)) datasets.append(dataset) logging.info("Stored labels into file {}.".format(label_file)) train_dataset = ConcatDataset(datasets) logging.info("Train dataset size: {}".format(len(train_dataset))) train_loader = DataLoader(train_dataset, args.batch_size, num_workers=args.num_workers, shuffle=True) logging.info("Prepare Validation datasets.") if args.dataset_type == "voc": val_dataset = VOCDataset(args.validation_dataset, transform=test_transform, target_transform=target_transform, is_test=True) elif args.dataset_type == 'open_images': val_dataset = OpenImagesDataset(dataset_path, transform=test_transform, target_transform=target_transform, dataset_type="test") logging.info(val_dataset) elif args.dataset_type == "coco": val_dataset = CocoDetection( "/home/wenyen4desh/datasets/coco/val2017", "/home/wenyen4desh/datasets/coco/annotations/instances_val2017.json", transform=test_transform, target_transform=target_transform) logging.info(val_dataset) logging.info("validation dataset size: {}".format(len(val_dataset))) val_loader = DataLoader(val_dataset, args.batch_size, num_workers=args.num_workers, shuffle=False) logging.info("Build network.") net = create_net(num_classes) min_loss = -10000.0 last_epoch = -1 base_net_lr = args.base_net_lr if args.base_net_lr is not None else args.lr extra_layers_lr = args.extra_layers_lr if args.extra_layers_lr is not None else args.lr if args.freeze_base_net: logging.info("Freeze base net.") freeze_net_layers(net.base_net) params = itertools.chain(net.source_layer_add_ons.parameters(), net.extras.parameters(), net.regression_headers.parameters(), net.classification_headers.parameters()) params = [{ 'params': itertools.chain(net.source_layer_add_ons.parameters(), net.extras.parameters()), 'lr': extra_layers_lr }, { 'params': itertools.chain(net.regression_headers.parameters(), net.classification_headers.parameters()) }] elif args.freeze_net: freeze_net_layers(net.base_net) freeze_net_layers(net.source_layer_add_ons) freeze_net_layers(net.extras) params = itertools.chain(net.regression_headers.parameters(), net.classification_headers.parameters()) logging.info("Freeze all the layers except prediction heads.") else: params = [{ 'params': net.base_net.parameters(), 'lr': base_net_lr }, { 'params': itertools.chain(net.source_layer_add_ons.parameters(), net.extras.parameters()), 'lr': extra_layers_lr }, { 'params': itertools.chain(net.regression_headers.parameters(), net.classification_headers.parameters()) }] timer.start("Load Model") if args.resume: logging.info("Resume from the model {}".format(args.resume)) net.load(args.resume) elif args.base_net: logging.info("Init from base net {}".format(args.base_net)) net.init_from_base_net(args.base_net) elif args.pretrained_ssd: logging.info("Init from pretrained ssd {}".format(args.pretrained_ssd)) net.init_from_pretrained_ssd(args.pretrained_ssd) logging.info('Took {:.2f} seconds to load the model.'.format( timer.end("Load Model"))) net.to(DEVICE) criterion = MultiboxLoss(config.priors, iou_threshold=0.5, neg_pos_ratio=3, center_variance=0.1, size_variance=0.2, device=DEVICE) optimizer = torch.optim.SGD(params, lr=args.lr, momentum=args.momentum, weight_decay=args.weight_decay) logging.info("Learning rate: {}, Base net learning rate: {}, ".format( args.lr, base_net_lr) + "Extra Layers learning rate: {}.".format(extra_layers_lr)) if args.scheduler == 'multi-step': logging.info("Uses MultiStepLR scheduler.") milestones = [int(v.strip()) for v in args.milestones.split(",")] scheduler = MultiStepLR(optimizer, milestones=milestones, gamma=0.1, last_epoch=last_epoch) elif args.scheduler == 'cosine': logging.info("Uses CosineAnnealingLR scheduler.") scheduler = CosineAnnealingLR(optimizer, args.t_max, last_epoch=last_epoch) else: logging.fatal("Unsupported Scheduler: {}.".format(args.scheduler)) parser.print_help(sys.stderr) sys.exit(1) logging.info("Start training from epoch {}.".format(last_epoch + 1)) for epoch in range(last_epoch + 1, args.num_epochs): scheduler.step() train(train_loader, net, criterion, optimizer, device=DEVICE, debug_steps=args.debug_steps, epoch=epoch) if epoch % args.validation_epochs == 0 or epoch == args.num_epochs - 1: val_loss, val_regression_loss, val_classification_loss = test( val_loader, net, criterion, DEVICE) logging.info("Epoch: {}, ".format(epoch) + "Validation Loss: {:.4f}, ".format(val_loss) + "Validation Regression Loss {:.4f}, ".format( val_regression_loss) + "Validation Classification Loss: {:.4f}".format( val_classification_loss)) model_path = os.path.join( args.checkpoint_folder, "{}-Epoch-{}-Loss-{}.pth".format(args.net, epoch, val_loss)) net.save(model_path) logging.info("Saved model {}".format(model_path))
def setup_and_start_training(self): logging.basicConfig( stream=sys.stdout, level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s') DEVICE = torch.device("cuda:0" if torch.cuda.is_available() and self. system_dict["params"]["use_cuda"] else "cpu") if self.system_dict["params"]["use_cuda"] and torch.cuda.is_available( ): torch.backends.cudnn.benchmark = True logging.info("Using gpu.") else: logging.info("Using cpu.") timer = Timer() logging.info(self.system_dict) if self.system_dict["params"]["net"] == 'vgg16-ssd': create_net = create_vgg_ssd config = vgg_ssd_config elif self.system_dict["params"]["net"] == 'mb1-ssd': create_net = create_mobilenetv1_ssd config = mobilenetv1_ssd_config elif self.system_dict["params"]["net"] == 'mb1-ssd-lite': create_net = create_mobilenetv1_ssd_lite config = mobilenetv1_ssd_config elif self.system_dict["params"]["net"] == 'sq-ssd-lite': create_net = create_squeezenet_ssd_lite config = squeezenet_ssd_config elif self.system_dict["params"]["net"] == 'mb2-ssd-lite': create_net = lambda num: create_mobilenetv2_ssd_lite( num, width_mult=self.system_dict["params"]["mb2_width_mult"]) config = mobilenetv1_ssd_config else: logging.fatal("The net type is wrong.") sys.exit(1) train_transform = TrainAugmentation(config.image_size, config.image_mean, config.image_std) target_transform = MatchPrior(config.priors, config.center_variance, config.size_variance, 0.5) test_transform = TestTransform(config.image_size, config.image_mean, config.image_std) logging.info("Prepare training datasets.") datasets = [] dataset = VOCDataset( self.system_dict["dataset"]["val"]["img_dir"], self.system_dict["dataset"]["val"]["label_dir"], transform=train_transform, target_transform=target_transform, label_file=self.system_dict["params"]["label_file"]) label_file = self.system_dict["params"]["label_file"] #store_labels(label_file, dataset.class_names) num_classes = len(dataset.class_names) datasets.append(dataset) logging.info(f"Stored labels into file {label_file}.") train_dataset = ConcatDataset(datasets) logging.info("Train dataset size: {}".format(len(train_dataset))) train_loader = DataLoader( train_dataset, self.system_dict["params"]["batch_size"], num_workers=self.system_dict["params"]["num_workers"], shuffle=True) if (self.system_dict["dataset"]["val"]["status"]): val_dataset = VOCDataset( self.system_dict["dataset"]["val"]["img_dir"], self.system_dict["dataset"]["val"]["label_dir"], transform=test_transform, target_transform=target_transform, is_test=True, label_file=self.system_dict["params"]["label_file"]) logging.info("validation dataset size: {}".format( len(val_dataset))) val_loader = DataLoader( val_dataset, self.system_dict["params"]["batch_size"], num_workers=self.system_dict["params"]["num_workers"], shuffle=False) logging.info("Build network.") net = create_net(num_classes) min_loss = -10000.0 last_epoch = -1 base_net_lr = self.system_dict["params"][ "base_net_lr"] if self.system_dict["params"][ "base_net_lr"] is not None else self.system_dict["params"]["lr"] extra_layers_lr = self.system_dict["params"][ "extra_layers_lr"] if self.system_dict["params"][ "extra_layers_lr"] is not None else self.system_dict["params"][ "lr"] if self.system_dict["params"]["freeze_base_net"]: logging.info("Freeze base net.") freeze_net_layers(net.base_net) params = itertools.chain(net.source_layer_add_ons.parameters(), net.extras.parameters(), net.regression_headers.parameters(), net.classification_headers.parameters()) params = [{ 'params': itertools.chain(net.source_layer_add_ons.parameters(), net.extras.parameters()), 'lr': extra_layers_lr }, { 'params': itertools.chain(net.regression_headers.parameters(), net.classification_headers.parameters()) }] elif self.system_dict["params"]["freeze_net"]: freeze_net_layers(net.base_net) freeze_net_layers(net.source_layer_add_ons) freeze_net_layers(net.extras) params = itertools.chain(net.regression_headers.parameters(), net.classification_headers.parameters()) logging.info("Freeze all the layers except prediction heads.") else: params = [{ 'params': net.base_net.parameters(), 'lr': base_net_lr }, { 'params': itertools.chain(net.source_layer_add_ons.parameters(), net.extras.parameters()), 'lr': extra_layers_lr }, { 'params': itertools.chain(net.regression_headers.parameters(), net.classification_headers.parameters()) }] timer.start("Load Model") resume = self.system_dict["params"]["resume"] base_net = self.system_dict["params"]["base_net"] pretrained_ssd = self.system_dict["params"]["pretrained_ssd"] if self.system_dict["params"]["resume"]: logging.info(f"Resume from the model {resume}") net.load(self.system_dict["params"]["resume"]) elif self.system_dict["params"]["base_net"]: logging.info(f"Init from base net {base_net}") net.init_from_base_net(self.system_dict["params"]["base_net"]) elif self.system_dict["params"]["pretrained_ssd"]: logging.info(f"Init from pretrained ssd {pretrained_ssd}") net.init_from_pretrained_ssd( self.system_dict["params"]["pretrained_ssd"]) logging.info( f'Took {timer.end("Load Model"):.2f} seconds to load the model.') net.to(DEVICE) criterion = MultiboxLoss(config.priors, iou_threshold=0.5, neg_pos_ratio=3, center_variance=0.1, size_variance=0.2, device=DEVICE) optimizer = torch.optim.SGD( params, lr=self.system_dict["params"]["lr"], momentum=self.system_dict["params"]["momentum"], weight_decay=self.system_dict["params"]["weight_decay"]) lr = self.system_dict["params"]["lr"] logging.info( f"Learning rate: {lr}, Base net learning rate: {base_net_lr}, " + f"Extra Layers learning rate: {extra_layers_lr}.") if (not self.system_dict["params"]["milestones"]): self.system_dict["params"]["milestones"] = "" self.system_dict["params"]["milestones"] += str( int(self.system_dict["params"]["num_epochs"] / 3)) + "," self.system_dict["params"]["milestones"] += str( int(2 * self.system_dict["params"]["num_epochs"] / 3)) if self.system_dict["params"]["scheduler"] == 'multi-step': logging.info("Uses MultiStepLR scheduler.") milestones = [ int(v.strip()) for v in self.system_dict["params"]["milestones"].split(",") ] scheduler = MultiStepLR(optimizer, milestones=milestones, gamma=0.1, last_epoch=last_epoch) elif self.system_dict["params"]["scheduler"] == 'cosine': logging.info("Uses CosineAnnealingLR scheduler.") scheduler = CosineAnnealingLR(optimizer, self.system_dict["params"]["t_max"], last_epoch=last_epoch) logging.info(f"Start training from epoch {last_epoch + 1}.") for epoch in range(last_epoch + 1, self.system_dict["params"]["num_epochs"]): scheduler.step() self.base_train( train_loader, net, criterion, optimizer, device=DEVICE, debug_steps=self.system_dict["params"]["debug_steps"], epoch=epoch) if ((self.system_dict["dataset"]["val"]["status"]) and (epoch % self.system_dict["params"]["validation_epochs"] == 0 or epoch == self.system_dict["params"]["num_epochs"] - 1)): val_loss, val_regression_loss, val_classification_loss = self.base_test( val_loader, net, criterion, DEVICE) logging.info( f"Epoch: {epoch}, " + f"Validation Loss: {val_loss:.4f}, " + f"Validation Regression Loss {val_regression_loss:.4f}, " + f"Validation Classification Loss: {val_classification_loss:.4f}" ) net_name = self.system_dict["params"]["net"] model_path = os.path.join( self.system_dict["params"]["checkpoint_folder"], f"{net_name}-Epoch-{epoch}-Loss-{val_loss}.pth") net.save(model_path) logging.info(f"Saved model {model_path}") if (not self.system_dict["dataset"]["val"]["status"]): model_path = os.path.join( self.system_dict["params"]["checkpoint_folder"], f"{net_name}-Epoch-{epoch}.pth") net.save(model_path) logging.info(f"Saved model {model_path}")
def main(): eval_path = pathlib.Path(args.eval_dir) eval_path.mkdir(exist_ok=True) timer = Timer() class_names = { 'BACKGROUND', 'aeroplane', 'bicycle', 'bird', 'boat', 'bottle', 'bus', 'car', 'cat', 'chair', 'cow', 'diningtable', 'dog', 'horse', 'motorbike', 'person', 'pottedplant', 'sheep', 'sofa', 'train', 'tvmonitor' } if args.dataset_type == "voc": dataset = VOCDataset(args.dataset, is_test=True) elif args.dataset_type == 'open_images': dataset = OpenImagesDataset(args.dataset, dataset_type="test") # true_case_stat, all_gb_boxes, all_difficult_cases = group_annotation_by_class(dataset) if args.net == 'vgg16-ssd': net = create_vgg_ssd(len(class_names), is_test=True) elif args.net == 'mb1-ssd': net = create_mobilenetv1_ssd(len(class_names), is_test=True) elif args.net == 'mb1-ssd-lite': net = create_mobilenetv1_ssd_lite(len(class_names), is_test=True) elif args.net == 'sq-ssd-lite': net = create_squeezenet_ssd_lite(len(class_names), is_test=True) elif args.net == 'mb2-ssd-lite': net = create_mobilenetv2_ssd_lite(len(class_names), width_mult=args.mb2_width_mult, is_test=True) else: logging.fatal( "The net type is wrong. It should be one of vgg16-ssd, mb1-ssd and mb1-ssd-lite." ) parser.print_help(sys.stderr) sys.exit(1) timer.start("Load Model") net.load(args.trained_model) net = net.to(DEVICE) print('It took {timer.end("Load Model")} seconds to load the model.') if args.net == 'vgg16-ssd': predictor = create_vgg_ssd_predictor(net, nms_method=args.nms_method, device=DEVICE) elif args.net == 'mb1-ssd': predictor = create_mobilenetv1_ssd_predictor( net, nms_method=args.nms_method, device=DEVICE) elif args.net == 'mb1-ssd-lite': predictor = create_mobilenetv1_ssd_lite_predictor( net, nms_method=args.nms_method, device=DEVICE) elif args.net == 'sq-ssd-lite': predictor = create_squeezenet_ssd_lite_predictor( net, nms_method=args.nms_method, device=DEVICE) elif args.net == 'mb2-ssd-lite': predictor = create_mobilenetv2_ssd_lite_predictor( net, nms_method=args.nms_method, device=DEVICE) else: logging.fatal( "The net type is wrong. It should be one of vgg16-ssd, mb1-ssd and mb1-ssd-lite." ) parser.print_help(sys.stderr) sys.exit(1) print( ssd_eval(predictor=predictor, dataset=dataset, data_path=eval_path, class_names=class_names))
predictor = create_mobilenetv1_ssd_predictor(net, candidate_size=200) elif net_type == 'mb1-ssd-lite': predictor = create_mobilenetv1_ssd_lite_predictor(net, candidate_size=200) elif net_type == 'mb2-ssd-lite': predictor = create_mobilenetv2_ssd_lite_predictor(net, candidate_size=200) elif net_type == 'sq-ssd-lite': predictor = create_squeezenet_ssd_lite_predictor(net, candidate_size=200) else: predictor = create_vgg_ssd_predictor(net, candidate_size=200) orig_image = cv2.imread(image_path) image = cv2.cvtColor(orig_image, cv2.COLOR_BGR2RGB) t_sum = 0 timer = Timer() for i in range(100): start = timer.start() boxes, labels, probs = predictor.predict(image, -1, 0.5) end = timer.end() t_sum += end # break print("time: ", t_sum / 100) for i in range(boxes.size(0)): box = boxes[i, :] cv2.rectangle(orig_image, (box[0], box[1]), (box[2], box[3]), (255, 255, 0), 4) #label = f"""{voc_dataset.class_names[labels[i]]}: {probs[i]:.2f}""" label = f"{class_names[labels[i]]}: {probs[i]:.2f}" cv2.putText( orig_image, label, (box[0] + 20, box[1] + 40),
def main(args): DEVICE = torch.device("cuda:0" if torch.cuda.is_available() and args['flow_control']['use_cuda'] else "cpu") # eval_path = pathlib.Path(args.eval_dir) # eval_path.mkdir(exist_ok=True) if not os.path.exists(args['flow_control']['eval_dir']): os.mkdir(args['flow_control']['eval_dir']) timer = Timer() class_names = [ name.strip() for name in open(args['flow_control']['label_file']).readlines() ] _net = args['flow_control']['net'] _dataset_type = args['flow_control']['dataset_type'] if _dataset_type == "voc": raise NotImplementedError("Not implement error") dataset = VOCDataset(args['flow_control']['dataset'], is_test=True) elif _dataset_type == 'open_images': raise NotImplementedError("Not implement error") dataset = OpenImagesDataset(args['flow_control']['dataset'], dataset_type="test") elif _dataset_type == "coco": # dataset = CocoDetection("/home/wenyen4desh/datasets/coco/test2017","/home/wenyen4desh/datasets/annotations/image_info_test2017.json") #dataset = CocoDetection("../../dataset/datasets/coco/val2017","../../dataset/datasets/coco/annotations/instances_val2017.json") # dataset = CocoDetection("/home/wenyen4desh/datasets/coco/train2017","/home/wenyen4desh/datasets/coco/annotations/instances_train2017.json") dataset = CocoDetection(args['Datasets']['coco']['val_image_path'], args['Datasets']['coco']['val_anno_path']) elif _dataset_type == "ecp": dataset = EuroCity_Dataset(args['Datasets']['ecp']['val_image_path'], args['Datasets']['ecp']['val_anno_path']) true_case_stat, all_gb_boxes, all_difficult_cases = group_annotation_by_class( dataset) if _net == 'vgg16-ssd': net = create_vgg_ssd(len(class_names), is_test=True) elif _net == 'mb1-ssd': net = create_mobilenetv1_ssd(len(class_names), is_test=True) elif _net == 'mb1-ssd-lite': net = create_mobilenetv1_ssd_lite(len(class_names), is_test=True) elif _net == 'sq-ssd-lite': net = create_squeezenet_ssd_lite(len(class_names), is_test=True) elif _net == 'mb2-ssd-lite': net = create_mobilenetv2_ssd_lite( len(class_names), width_mult=args['flow_control']['mb2_width_mult'], is_test=True) else: logging.fatal( "The net type is wrong. It should be one of vgg16-ssd, mb1-ssd and mb1-ssd-lite." ) parser.print_help(sys.stderr) sys.exit(1) #train_transform = MatchPrior(config.priors, config.center_variance, # config.size_variance, 0.5) #test_transform = TestTransform(config.image_size, config.image_mean, config.image_std) import pdb pdb.set_trace() ############################## automatically validation ############################################ timer.start("Load Model") net.load(args['flow_control']['trained_model']) net = net.to(DEVICE) print('It took {} seconds to load the model.'.format( timer.end("Load Model"))) _nms_method = args['flow_control']['nms_method'] if _net == 'vgg16-ssd': predictor = create_vgg_ssd_predictor(net, nms_method=_nms_method, device=DEVICE) elif _net == 'mb1-ssd': predictor = create_mobilenetv1_ssd_predictor(net, nms_method=_nms_method, device=DEVICE) elif _net == 'mb1-ssd-lite': predictor = create_mobilenetv1_ssd_lite_predictor( net, nms_method=_nms_method, device=DEVICE) elif _net == 'sq-ssd-lite': predictor = create_squeezenet_ssd_lite_predictor( net, nms_method=_nms_method, device=DEVICE) elif _net == 'mb2-ssd-lite': predictor = create_mobilenetv2_ssd_lite_predictor( net, nms_method=_nms_method, device=DEVICE) else: logging.fatal( "The net type is wrong. It should be one of vgg16-ssd, mb1-ssd and mb1-ssd-lite." ) parser.print_help(sys.stderr) sys.exit(1) results = [] # Predict Bounding Box for i in range(len(dataset)): print("process image {}", i) timer.start("Load Image") image = dataset.get_image(i) print("Load Image: {:4f} seconds.".format(timer.end("Load Image"))) timer.start("Predict") boxes, labels, probs = predictor.predict(image) print("Prediction: {:4f} seconds.".format(timer.end("Predict"))) indexes = torch.ones(labels.size(0), 1, dtype=torch.float32) * i results.append( torch.cat( [ indexes.reshape(-1, 1), labels.reshape(-1, 1).float(), probs.reshape(-1, 1), boxes + 1.0 # matlab's indexes start from 1 ], dim=1)) results = torch.cat(results) # Write the result to file for class_index, class_name in enumerate(class_names): if class_index == 0: continue # ignore background file_name = "det_test_{}.txt".format(class_name) prediction_path = os.path.join(args['flow_control']['eval_dir'], file_name) with open(prediction_path, "w") as f: sub = results[results[:, 1] == class_index, :] for i in range(sub.size(0)): prob_box = sub[i, 2:].numpy() image_id, _ = dataset.get_annotation(int(sub[i, 0])) f.write( str(image_id) + " " + " ".join([str(v) for v in prob_box]) + "\n") # image_id = dataset.ids[int(sub[i, 0])] # print(str(image_id) + " " + " ".join([str(v) for v in prob_box]), file=f) aps = [] prcs = [] recalls = [] print("\n\nAverage Precision Per-class:") for class_index, class_name in enumerate(class_names): if class_index == 0: continue file_name = "det_test_{}.txt".format(class_name) prediction_path = os.path.join(args['flow_control']['eval_dir'], file_name) # [email protected] evaluation method ap, precision, recall = compute_average_precision_per_class( args, true_case_stat[class_index], all_gb_boxes[class_index], all_difficult_cases[class_index], prediction_path, args['flow_control']['iou_threshold'], args['flow_control']['use_2007_metric']) # # COCO eval # ap, precision, recall = coco_ap_per_class( # true_case_stat[class_index], # all_gb_boxes[class_index], # all_difficult_cases[class_index], # prediction_path, # args.use_2007_metric # ) aps.append(ap) prcs.append(precision) recalls.append(recall) print("{}: {}".format(class_name, ap)) print("\nAverage Precision Across All Classes:{}".format( sum(aps[0:5]) / len(aps[0:5]))) print("\nAverage Precision :{}".format(sum(prcs[0:5]) / len(prcs[0:5]))) print("\nAverage Recall :{}".format(sum(recalls[0:5]) / len(recalls[0:5])))
def main(args): DEVICE = torch.device( "cuda:0" if torch.cuda.is_available() and args.use_cuda else "cpu") if not os.path.exists(args.eval_dir): os.mkdir(args.eval_dir) timer = Timer() class_names = [name.strip() for name in open(args.label_file).readlines()] # dataset = Folder_image_set() # true_case_stat, all_gb_boxes, all_difficult_cases = group_annotation_by_class(dataset) if args.net == 'vgg16-ssd': net = create_vgg_ssd(len(class_names), is_test=True) elif args.net == 'mb1-ssd': net = create_mobilenetv1_ssd(len(class_names), is_test=True) elif args.net == 'mb1-ssd-lite': net = create_mobilenetv1_ssd_lite(len(class_names), is_test=True) elif args.net == 'sq-ssd-lite': net = create_squeezenet_ssd_lite(len(class_names), is_test=True) elif args.net == 'mb2-ssd-lite': net = create_mobilenetv2_ssd_lite(len(class_names), width_mult=args.mb2_width_mult, is_test=True) else: logging.fatal( "The net type is wrong. It should be one of vgg16-ssd, mb1-ssd and mb1-ssd-lite." ) parser.print_help(sys.stderr) sys.exit(1) #train_transform = MatchPrior(config.priors, config.center_variance, # config.size_variance, 0.5) #test_transform = TestTransform(config.image_size, config.image_mean, config.image_std) # test_transform = TestTransform(config.image_size, config.image_mean, config.image_std) # dataset = FolderDataset("/media/wy_disk/wy_file/Detection/dataset/datasets/ECP_Golden_pattern", transform = test_transform) # dataset = FolderDataset("/media/wy_disk/wy_file/Detection/dataset/datasets/ECP_Golden_pattern") dataset = FolderDataset("/media/wy_disk/ChenYen/VIRAT/dataset_orgnize/val") timer.start("Load Model") net.load(args.trained_model) net = net.to(DEVICE) print('It took {} seconds to load the model.'.format( timer.end("Load Model"))) if args.net == 'vgg16-ssd': predictor = create_vgg_ssd_predictor(net, nms_method=args.nms_method, device=DEVICE) elif args.net == 'mb1-ssd': predictor = create_mobilenetv1_ssd_predictor( net, nms_method=args.nms_method, device=DEVICE) elif args.net == 'mb1-ssd-lite': predictor = create_mobilenetv1_ssd_lite_predictor( net, nms_method=args.nms_method, device=DEVICE) elif args.net == 'sq-ssd-lite': predictor = create_squeezenet_ssd_lite_predictor( net, nms_method=args.nms_method, device=DEVICE) elif args.net == 'mb2-ssd-lite': predictor = create_mobilenetv2_ssd_lite_predictor( net, nms_method=args.nms_method, device=DEVICE) else: logging.fatal( "The net type is wrong. It should be one of vgg16-ssd, mb1-ssd and mb1-ssd-lite." ) parser.print_help(sys.stderr) sys.exit(1) results = [] eval_path = "eval_results" # eval_whole_image(dataset,5, predictor) eval_subblock_image(dataset, 5, predictor) import pdb pdb.set_trace() for i in range(len(dataset)): print("process image", i) timer.start("Load Image") import pdb pdb.set_trace() image = dataset.get_image(i) print("Load Image: {:4f} seconds.".format(timer.end("Load Image"))) timer.start("Predict") boxes, labels, probs = predictor.predict(image, 10, 0.5) print("Prediction: {:4f} seconds.".format(timer.end("Predict"))) # indexes = torch.ones(labels.size(0), 1, dtype=torch.float32) * i # print("index:p\t{}".format(sum(probs>0.5))) # import pdb;pdb.set_trace() boxes, labels, probs = boxes.data.numpy(), labels.data.numpy( ), probs.data.numpy() image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR) for box, _label, _prob in zip(boxes, labels, probs): if _prob < 0.7: continue print(box) box = box.astype(int) # import pdb;pdb.set_trace() print(box) cv2.rectangle(image, (box[0], box[1]), (box[2], box[3]), (255, 255, 0), 4) # str(str.split(class_names[_label]," ")[1]) cv2.putText( image, dataset.class_names[_label], (box[0] + 20, box[1] + 40), cv2.FONT_HERSHEY_SIMPLEX, 1, # font scale (255, 0, 255), 2) # line type print(boxes.shape[0]) cv2.imshow('annotated', image) # key = cv2.waitKey(0) if cv2.waitKey(0) & 0xFF == ord('q'): break
def main(args): net_type = args.net_type model_path = args.weights_path label_path = args.label_path class_names = [name.strip() for name in open(label_path).readlines()] num_classes = len(class_names) if args.live: cap = cv2.VideoCapture(0) cap.set(3, 640) cap.set(4, 480) else: cap = cv2.VideoCapture(args.video_path) Fourcc = cv2.VideoWriter_fourcc('M', 'P', '4', 'V') writer = cv2.VideoWriter('result.mp4', fourcc=Fourcc, fps=15, frameSize=(640, 480)) num_gpus = torch.cuda.device_count() device = 'cuda' if num_gpus else 'cpu' if net_type == 'vgg16-ssd': net = create_vgg_ssd(len(class_names), is_test=True) elif net_type == 'mb1-ssd': net = create_mobilenetv1_ssd(len(class_names), is_test=True) elif net_type == 'mb1-ssd-lite': net = create_mobilenetv1_ssd_lite(len(class_names), is_test=True) elif net_type == 'mb2-ssd-lite': net = create_mobilenetv2_ssd_lite(len(class_names), is_test=True) elif net_type == 'sq-ssd-lite': net = create_squeezenet_ssd_lite(len(class_names), is_test=True) #elif net_type == 'mb3-ssd-lite': # net = create_mobilenetv3_ssd_lite(len(class_names), is_test=True) else: print( "The net type is wrong. It should be one of vgg16-ssd, mb1-ssd and mb1-ssd-lite." ) sys.exit(1) net.load(model_path) if net_type == 'vgg16-ssd': predictor = create_vgg_ssd_predictor(net, candidate_size=20, device=device) elif net_type == 'mb1-ssd': predictor = create_mobilenetv1_ssd_predictor(net, candidate_size=20, device=device) elif net_type == 'mb1-ssd-lite': predictor = create_mobilenetv1_ssd_lite_predictor(net, candidate_size=20, device=device) elif net_type == 'mb2-ssd-lite': predictor = create_mobilenetv2_ssd_lite_predictor(net, candidate_size=20, device=device) elif net_type == 'sq-ssd-lite': predictor = create_squeezenet_ssd_lite_predictor(net, candidate_size=20, device=device) #elif net_type == 'mb3-ssd-lite': # predictor = create_mobilenetv3_ssd_lite_predictor(net, candidate_size=10) else: print( "The net type is wrong. It should be one of vgg16-ssd, mb1-ssd and mb1-ssd-lite." ) sys.exit(1) timer = Timer() while True: _, orig_image = cap.read() if orig_image is None: print('END') break image = cv2.cvtColor(orig_image, cv2.COLOR_BGR2RGB) timer.start() boxes, labels, probs = predictor.predict(image, 10, 0.4) interval = timer.end() print('Time: {:.2f}s, Detect Objects: {:d}.'.format( interval, labels.size(0))) for i in range(boxes.size(0)): box = boxes[i, :] label = f"{class_names[labels[i]]}: {probs[i]:.2f}" cv2.rectangle(orig_image, (box[0], box[1]), (box[2], box[3]), (255, 255, 0), 4) cv2.putText( orig_image, label, (box[0] + 20, box[1] + 40), cv2.FONT_HERSHEY_SIMPLEX, 1, # font scale (255, 0, 255), 2) # line type writer.write(orig_image) cv2.imshow('annotated', orig_image) if cv2.waitKey(1) & 0xFF == ord('q'): break cap.release() writer.release() cv2.destroyAllWindows() if args.out_video: shutil.move('result.mp4', args.out_video) else: os.remove('result.mp4')
elif args.net == 'mb1-ssd-lite': net = create_mobilenetv1_ssd_lite(len(class_names), is_test=True) elif args.net == 'sq-ssd-lite': net = create_squeezenet_ssd_lite(len(class_names), is_test=True) elif args.net == 'mb2-ssd-lite': net = create_mobilenetv2_ssd_lite(len(class_names), width_mult=args.mb2_width_mult, is_test=True) else: logging.fatal( "The net type is wrong. It should be one of vgg16-ssd, mb1-ssd and mb1-ssd-lite." ) parser.print_help(sys.stderr) sys.exit(1) timer.start("Load Model") net.load(args.trained_model) net = net.to(DEVICE) print(f'It took {timer.end("Load Model")} seconds to load the model.') if args.net == 'vgg16-ssd': predictor = create_vgg_ssd_predictor(net, nms_method=args.nms_method, device=DEVICE) elif args.net == 'mb1-ssd': predictor = create_mobilenetv1_ssd_predictor( net, nms_method=args.nms_method, device=DEVICE) elif args.net == 'mb1-ssd-lite': predictor = create_mobilenetv1_ssd_lite_predictor( net, nms_method=args.nms_method, device=DEVICE) elif args.net == 'sq-ssd-lite': predictor = create_squeezenet_ssd_lite_predictor(
elif args.freeze_net: freeze_net_layers(net.base_net) freeze_net_layers(net.extra_layers) params = itertools.chain(net.regression_headers.parameters(), net.classification_headers.parameters()) logging.info("Freeze all the layers except prediction heads.") else: params = [ {'params': net.base_net.parameters(), 'lr': base_net_lr}, {'params': itertools.chain( net.regression_headers.parameters(), net.classification_headers.parameters(), net.extra_layers.parameters() )} ] timer.start("Load Model") if args.resume: logging.info(f"Resume from the model {args.resume}") net.load_state_dict(torch.load(args.resume, map_location=lambda storage, loc: storage)) elif args.base_net: logging.info(f"Init from base net {args.base_net}") net.init_from_base_net(args.base_net) elif args.pretrained_ssd: logging.info(f"Init from pretrained ssd {args.pretrained_ssd}") net.init_from_pretrained_ssd(args.pretrained_ssd) logging.info(f'Took {timer.end("Load Model"):.2f} seconds to load the model.') net.to(DEVICE) criterion = MultiboxLoss(config.priors, iou_threshold=0.45, neg_pos_ratio=3, center_variance=0.1, size_variance=0.2, device=DEVICE)
def train_network(dataset_path, model_path, net_type): args.datasets = dataset_path args.validation_dataset = dataset_path args.checkpoint_folder = model_path args.log_dir = os.path.join(args.checkpoint_folder, 'log') args.net = net_type timer = Timer() logging.info(args) if args.net == 'slim': create_net = create_mb_tiny_fd config = fd_config elif args.net == 'RFB': create_net = create_Mb_Tiny_RFB_fd config = fd_config else: logging.fatal("The net type is wrong.") parser.print_help(sys.stderr) sys.exit(1) train_transform = TrainAugmentation(config.image_size, config.image_mean, config.image_std) target_transform = MatchPrior(config.priors, config.center_variance, config.size_variance, args.overlap_threshold) test_transform = TestTransform(config.image_size, config.image_mean_test, config.image_std) if not os.path.exists(args.checkpoint_folder): os.makedirs(args.checkpoint_folder) logging.info("Prepare training datasets.") datasets = [] # voc datasets dataset = VOCDataset(dataset_path, transform=train_transform, target_transform=target_transform) label_file = os.path.join(args.checkpoint_folder, "voc-model-labels.txt") store_labels(label_file, dataset.class_names) num_classes = len(dataset.class_names) print('num_classes: ', num_classes) logging.info(f"Stored labels into file {label_file}.") # train_dataset = ConcatDataset(datasets) train_dataset = dataset logging.info("Train dataset size: {}".format(len(train_dataset))) train_loader = DataLoader(train_dataset, args.batch_size, num_workers=args.num_workers, shuffle=True) logging.info("Prepare Validation datasets.") val_dataset = VOCDataset(args.validation_dataset, transform=test_transform, target_transform=target_transform, is_test=True) logging.info("validation dataset size: {}".format(len(val_dataset))) val_loader = DataLoader(val_dataset, args.batch_size, num_workers=args.num_workers, shuffle=False) logging.info("Build network.") net = create_net(num_classes) timer.start("Load Model") if args.resume: logging.info(f"Resume from the model {args.resume}") net.load(args.resume) logging.info( f'Took {timer.end("Load Model"):.2f} seconds to load the model.') # add multigpu_train if torch.cuda.device_count() >= 1: cuda_index_list = [int(v.strip()) for v in args.cuda_index.split(",")] net = nn.DataParallel(net, device_ids=cuda_index_list) logging.info("use gpu :{}".format(cuda_index_list)) min_loss = -10000.0 last_epoch = -1 base_net_lr = args.base_net_lr if args.base_net_lr is not None else args.lr extra_layers_lr = args.extra_layers_lr if args.extra_layers_lr is not None else args.lr params = [{ 'params': net.module.base_net.parameters(), 'lr': base_net_lr }, { 'params': itertools.chain(net.module.source_layer_add_ons.parameters(), net.module.extras.parameters()), 'lr': extra_layers_lr }, { 'params': itertools.chain(net.module.regression_headers.parameters(), net.module.classification_headers.parameters()) }] net.to(DEVICE) criterion = MultiboxLoss(config.priors, iou_threshold=args.iou_threshold, neg_pos_ratio=5, center_variance=0.1, size_variance=0.2, device=DEVICE, num_classes=num_classes, loss_type=args.loss_type) if args.optimizer_type == "SGD": optimizer = torch.optim.SGD(params, lr=args.lr, momentum=args.momentum, weight_decay=args.weight_decay) elif args.optimizer_type == "Adam": optimizer = torch.optim.Adam(params, lr=args.lr) logging.info("use Adam optimizer") else: logging.fatal(f"Unsupported optimizer: {args.scheduler}.") parser.print_help(sys.stderr) sys.exit(1) logging.info( f"Learning rate: {args.lr}, Base net learning rate: {base_net_lr}, " + f"Extra Layers learning rate: {extra_layers_lr}.") if args.optimizer_type != "Adam": if args.scheduler == 'multi-step': logging.info("Uses MultiStepLR scheduler.") milestones = [int(v.strip()) for v in args.milestones.split(",")] scheduler = MultiStepLR(optimizer, milestones=milestones, gamma=0.1, last_epoch=last_epoch) elif args.scheduler == 'poly': logging.info("Uses PolyLR scheduler.") else: logging.fatal(f"Unsupported Scheduler: {args.scheduler}.") parser.print_help(sys.stderr) sys.exit(1) logging.info(f"Start training from epoch {last_epoch + 1}.") for epoch in range(last_epoch + 1, args.num_epochs): if args.optimizer_type != "Adam": if args.scheduler != "poly": if epoch != 0: scheduler.step() train(train_loader, net, criterion, optimizer, device=DEVICE, debug_steps=args.debug_steps, epoch=epoch) if args.scheduler == "poly": adjust_learning_rate(optimizer, epoch) logging.info("epoch: {} lr rate :{}".format( epoch, optimizer.param_groups[0]['lr'])) if epoch % args.validation_epochs == 0 or epoch == args.num_epochs - 1: logging.info("validation epoch: {} lr rate :{}".format( epoch, optimizer.param_groups[0]['lr'])) val_loss, val_regression_loss, val_classification_loss = test( val_loader, net, criterion, DEVICE) logging.info( f"Epoch: {epoch}, " + f"Validation Loss: {val_loss:.4f}, " + f"Validation Regression Loss {val_regression_loss:.4f}, " + f"Validation Classification Loss: {val_classification_loss:.4f}" ) model_path = os.path.join( args.checkpoint_folder, f"{args.net}-Epoch-{epoch}-Loss-{val_loss:.4f}.pth") net.module.save(model_path) logging.info(f"Saved model {model_path}")