def main(opt): torch.backends.cudnn.benchmark = True device = select_device(opt.device, batch_size=opt.batchsize) dice = Dice_th_pred(np.arange(0.2, 0.7, 0.01)) for fold in range(opt.nfolds): ds_t = MY_HuBMAPDataset(data_csv=opt.train_data, nfolds=opt.nfolds, fold=fold, train=True, tfms=get_aug()) ds_v = MY_HuBMAPDataset(data_csv=opt.train_data, nfolds=opt.nfolds, fold=fold, train=False, tfms=get_aug()) data = ImageDataLoaders.from_dsets(ds_t,ds_v,bc=opt.batchsize,num_workers=opt.num_workers,pin_memory=False).to(device) model = UneXt50().to(device) learn = Learner(data,model,loss_func = symmetric_lovasz,metrics = [Dice_soft(),Dice_th()],splitter = split_layers).to_fp16(clip=0.5) #start with training the head learn.freeze_to(-1) #doesn't work for param in learn.opt.param_groups[0]['params']: param.requires_grad = False learn.fit_one_cycle(6, lr_max=0.5e-2) #continue training full model learn.unfreeze() learn.fit_one_cycle(32, lr_max=slice(2e-4,2e-3), cbs=SaveModelCallback(monitor='dice_th', comp=np.greater)) save_name = f'model_{fold}.pth' save_path = os.path.join( opt.model_save_path,save_name) torch.save(learn.model.state_dict(),save_path) #model evaluation on val and saving the masks mp = Model_pred(learn.model,learn.dls.loaders[1]) with zipfile.ZipFile('val_masks_tta.zip', 'a') as out: for p in progress_bar(mp): dice.accumulate(p[0],p[1]) save_img(p[0],p[2],out) gc.collect() dices = dice.value noise_ths = dice.ths best_dice = dices.max() best_thr = noise_ths[dices.argmax()] plt.figure(figsize=(8,4)) plt.plot(noise_ths, dices, color='blue') plt.vlines(x=best_thr, ymin=dices.min(), ymax=dices.max(), colors='black') d = dices.max() - dices.min() plt.text(noise_ths[-1]-0.1, best_dice-0.1*d, f'DICE = {best_dice:.3f}', fontsize=12); plt.text(noise_ths[-1]-0.1, best_dice-0.2*d, f'TH = {best_thr:.3f}', fontsize=12); # plt.show() plt.savefig('train_dice.png')
# parser.add_argument('--weights', type=str, default='weights/resnet50.pth', help='path to weights file') #parser.add_argument('--weights', type=str, default='weights/resnet101.pth', help='path to weights file') #parser.add_argument('--weights', type=str, default='weights/yolov3-spp.pt', help='path to weights file') #parser.add_argument('--weights', type=str, default='weights/yolov3.pt', help='path to weights file') #parser.add_argument('--weights', type=str, default='weights/yolov3.weights', help='path to weights file') parser.add_argument('--weights', type=str, default='weights/darknet53.conv.74', help='path to weights file') #parser.add_argument('--weights', type=str, default='', help='path to weights file') #parser.add_argument('--weights', type=str, default='', help='path to weights file') parser.add_argument('--img-size', type=int, default=416, help='resize to this size square and detect') parser.add_argument('--epochs', type=int, default=20) parser.add_argument('--batch-size', type=int, default=64) # effective bs = batch_size * accumulate = 16 * 4 = 64 #parser.add_argument('--batch-size', type=int, default=16) opt = parser.parse_args() print(opt) device = select_device(opt.device) if device == 'cpu': mixed_precision = False # 0、Initialize parameters( set random seed, get cfg info, ) cfg = opt.cfg weights = opt.weights img_size = opt.img_size batch_size = opt.batch_size total_epochs = opt.epochs init_seeds() data = parse_data_cfg(opt.data) train_txt_path = data['train'] valid_txt_path = data['valid'] nc = int(data['classes'])
def test( data, weights=None, batch_size=16, imgsz=640, conf_thres=0.001, iou_thres=0.6, # for NMS save_json=False, single_cls=False, augment=False, verbose=False, model=None, dataloader=None, save_dir='', merge=False, save_txt=False): # Initialize/load model and set device training = model is not None if training: # called by train.py device = next(model.parameters()).device # get model device else: # called directly set_logging() device = select_device(opt.device, batch_size=batch_size) merge, save_txt = opt.merge, opt.save_txt # use Merge NMS, save *.txt labels if save_txt: out = Path('inference/output') if os.path.exists(out): shutil.rmtree(out) # delete output folder os.makedirs(out) # make new output folder # Remove previous for f in glob.glob(str(Path(save_dir) / 'test_batch*.jpg')): os.remove(f) # Load model model = download(weights, map_location=device) # load FP32 model imgsz = check_img_size(imgsz, s=model.stride.max()) # check img_size # Multi-GPU disabled, incompatible with .half() https://github.com/ultralytics/yolov5/issues/99 # if device.type != 'cpu' and torch.cuda.device_count() > 1: # model = nn.DataParallel(model) # Half half = device.type != 'cpu' # half precision only supported on CUDA if half: model.half() # Configure model.eval() with open(data) as f: data = yaml.load(f, Loader=yaml.FullLoader) # model dict check_dataset(data) # check nc = 1 if single_cls else int(data['nc']) # number of classes iouv = torch.linspace(0.5, 0.95, 10).to(device) # iou vector for [email protected]:0.95 niou = iouv.numel() # Dataloader if not training: img = torch.zeros((1, 3, imgsz, imgsz), device=device) # init img _ = model(img.half() if half else img ) if device.type != 'cpu' else None # run once path = data['test'] if opt.task == 'test' else data[ 'val'] # path to val/test images dataloader = create_dataloader(path, imgsz, batch_size, model.stride.max(), opt, hyp=None, augment=False, cache=False, pad=0.5, rect=True)[0] seen = 0 names = model.names if hasattr(model, 'names') else model.module.names coco91class = coco80_to_coco91_class() s_prints = ('%20s' + '%12s' * 6) % ('Class', 'Images', 'Targets', 'P', 'R', '[email protected]', '[email protected]:.95') p, r, f1, mp, mr, map50, map, t0, t1 = 0., 0., 0., 0., 0., 0., 0., 0., 0. loss = torch.zeros(3, device=device) jdict, stats, ap, ap_class = [], [], [], [] for batch_i, (img, targets, paths, shapes) in enumerate(tqdm(dataloader)): img = img.to(device, non_blocking=True) img = img.half() if half else img.float() # uint8 to fp16/32 img /= 255.0 # 0 - 255 to 0.0 - 1.0 targets = targets.to(device) nb, _, height, width = img.shape # batch size, channels, height, width whwh = torch.Tensor([width, height, width, height]).to(device) # Disable gradients with torch.no_grad(): # Run model t = time_synchronized() inf_out, train_out = model( img, augment=augment) # inference and training outputs t0 += time_synchronized() - t # Compute loss if training: # if model has loss hyperparameters loss += compute_loss([x.float() for x in train_out], targets, model)[1][:3] # GIoU, obj, cls # Run NMS t = time_synchronized() output = non_max_suppression(inf_out, conf_thres=conf_thres, iou_thres=iou_thres, merge=merge) t1 += time_synchronized() - t # Statistics per image for si, pred in enumerate(output): labels = targets[targets[:, 0] == si, 1:] nl = len(labels) tcls = labels[:, 0].tolist() if nl else [] # target class seen += 1 if pred is None: if nl: stats.append((torch.zeros(0, niou, dtype=torch.bool), torch.Tensor(), torch.Tensor(), tcls)) continue # Append to text file if save_txt: gn = torch.tensor(shapes[si][0])[[1, 0, 1, 0 ]] # normalization gain whwh x = pred.clone() x[:, :4] = scale_coords(img[si].shape[1:], x[:, :4], shapes[si][0], shapes[si][1]) # to original for *xyxy, conf, cls in x: xywh = (xyxy2xywh(torch.tensor(xyxy).view(1, 4)) / gn).view(-1).tolist() # normalized xywh with open(str(out / Path(paths[si]).stem) + '.txt', 'a') as f: f.write( ('%g ' * 5 + '\n') % (cls, *xywh)) # label format # Clip boxes to image bounds clip_coords(pred, (height, width)) # Append to pycocotools JSON dictionary if save_json: # [{"image_id": 42, "category_id": 18, "bbox": [258.15, 41.29, 348.26, 243.78], "score": 0.236}, ... image_id = Path(paths[si]).stem box = pred[:, :4].clone() # xyxy scale_coords(img[si].shape[1:], box, shapes[si][0], shapes[si][1]) # to original shape box = xyxy2xywh(box) # xywh box[:, :2] -= box[:, 2:] / 2 # xy center to top-left corner for p, b in zip(pred.tolist(), box.tolist()): jdict.append({ 'image_id': int(image_id) if image_id.isnumeric() else image_id, 'category_id': coco91class[int(p[5])], 'bbox': [round(x, 3) for x in b], 'score': round(p[4], 5) }) # Assign all predictions as incorrect correct = torch.zeros(pred.shape[0], niou, dtype=torch.bool, device=device) if nl: detected = [] # target indices tcls_tensor = labels[:, 0] # target boxes tbox = xywh2xyxy(labels[:, 1:5]) * whwh # Per target class for cls in torch.unique(tcls_tensor): ti = (cls == tcls_tensor).nonzero(as_tuple=False).view( -1) # prediction indices pi = (cls == pred[:, 5]).nonzero(as_tuple=False).view( -1) # target indices # Search for detections if pi.shape[0]: # Prediction to target ious ious, i = box_iou(pred[pi, :4], tbox[ti]).max( 1) # best ious, indices # Append detections detected_set = set() for j in (ious > iouv[0]).nonzero(as_tuple=False): d = ti[i[j]] # detected target if d.item() not in detected_set: detected_set.add(d.item()) detected.append(d) correct[ pi[j]] = ious[j] > iouv # iou_thres is 1xn if len( detected ) == nl: # all targets already located in image break # Append statistics (correct, conf, pcls, tcls) stats.append( (correct.cpu(), pred[:, 4].cpu(), pred[:, 5].cpu(), tcls)) # Plot images if batch_i < 1: f = Path(save_dir) / ('test_batch%g_gt.jpg' % batch_i) # filename plot_images(img, targets, paths, str(f), names) # ground truth f = Path(save_dir) / ('test_batch%g_pred.jpg' % batch_i) plot_images(img, output_to_target(output, width, height), paths, str(f), names) # predictions # Compute statistics stats = [np.concatenate(x, 0) for x in zip(*stats)] # to numpy if len(stats) and stats[0].any(): p, r, ap, f1, ap_class = ap_per_class(*stats) p, r, ap50, ap = p[:, 0], r[:, 0], ap[:, 0], ap.mean( 1) # [P, R, [email protected], [email protected]:0.95] mp, mr, map50, map = p.mean(), r.mean(), ap50.mean(), ap.mean() nt = np.bincount(stats[3].astype(np.int64), minlength=nc) # number of targets per class else: nt = torch.zeros(1) # Print results pf = '%20s' + '%12.3g' * 6 # print format #print(pf % ('all', seen, nt.sum(), mp, mr, map50, map)) print('>>Testing @mAP 0.5 {}, mAP 0.95 {}\n'.format(map50, map)) # Print results per class if verbose and nc > 1 and len(stats): for i, c in enumerate(ap_class): print('') #print(pf % (names[c], seen, nt[c], p[i], r[i], ap50[i], ap[i])) # Print speeds t = tuple(x / seen * 1E3 for x in (t0, t1, t0 + t1)) + (imgsz, imgsz, batch_size) # tuple if not training: print( 'Speed: %.1f/%.1f/%.1f ms inference/NMS/total per %gx%g image at batch-size %g' % t) # Save JSON if save_json and len(jdict): f = 'detections_val2017_%s_results.json' % \ (weights.split(os.sep)[-1].replace('.pt', '') if isinstance(weights, str) else '') # filename print('\nCOCO mAP with pycocotools... saving %s_prints...' % f) with open(f, 'w') as file: json.dump(jdict, file) try: # https://github.com/cocodataset/cocoapi/blob/master/PythonAPI/pycocoEvalDemo.ipynb from pycocotools.coco import COCO from pycocotools.cocoeval import COCOeval imgIds = [int(Path(x).stem) for x in dataloader.dataset.img_files] cocoGt = COCO( glob.glob('../coco/annotations/instances_val*.json') [0]) # initialize COCO ground truth api cocoDt = cocoGt.loadRes(f) # initialize COCO pred api cocoEval = COCOeval(cocoGt, cocoDt, 'bbox') cocoEval.params.imgIds = imgIds # image IDs to evaluate cocoEval.evaluate() cocoEval.accumulate() cocoEval.summarize() map, map50 = cocoEval.stats[: 2] # update results ([email protected]:0.95, [email protected]) except Exception as e: print('ERROR: pycocotools unable to run: %s_prints' % e) # Return results model.float() # for training maps = np.zeros(nc) + map for i, c in enumerate(ap_class): maps[c] = ap[i] return (mp, mr, map50, map, *(loss.cpu() / len(dataloader)).tolist()), maps, t
def detect(): # 0、初始化一些参数 cfg = opt.cfg weights = opt.weights src_txt_path = opt.src_txt_path img_size = opt.img_size batch_size = opt.batch_size dst_path = opt.dst_path if not os.path.exists(dst_path): os.mkdir(dst_path) device = select_device(opt.device) classes = load_classes(parse_data_cfg(opt.data)['names']) # 1、加载网络 model = Darknet(cfg) if weights.endswith('.pt'): # TODO: .weights权重格式 model.load_state_dict( torch.load(weights)['model']) # TODO:map_location=device ? model.to(device).eval() # 2、加载数据集 test_dataset = VocDataset(src_txt_path, img_size, with_label=False) dataloader = DataLoader( test_dataset, batch_size=batch_size, shuffle=False, num_workers=8, # TODO collate_fn=test_dataset.test_collate_fn) # TODO # 3、预测,前向传播 start = time.time() pbar = tqdm(dataloader) for i, (img_tensor, img0, img_name) in enumerate(pbar): pbar.set_description("Already Processed %d image: " % (i + 1)) # print('clw: Already Processed %d image' % (i+1)) img_tensor = img_tensor.to(device) # (bs, 3, 416, 416) output = model(img_tensor)[ 0] # (x1, y1, x2, y2, obj_conf, class_conf, class_pred) # NMS nms_output = non_max_suppression(output, opt.conf_thres, opt.nms_thres) # 可视化 for batch_idx, det in enumerate(nms_output): # detections per image if det is not None: # and len(det): # clw note: important ! #or box in det: for *box, conf, _, cls in det: # det: tensor.Size (bs, 7) box: list orig_h, orig_w = img0[batch_idx].shape[:2] # 坐标变换 new_h = new_w = img_tensor.size()[ 2] # 绘图,resize后的图的框 -> 原图的框,new -> orig ratio_h = orig_h / new_h ratio_w = orig_w / new_w x1 = int(ratio_w * box[0]) y1 = int(ratio_h * box[1]) x2 = int(ratio_w * (box[2])) y2 = int(ratio_h * (box[3])) label = '%s %.2f' % (classes[int(cls)], conf) # 预测结果可视化 plot_one_box([x1, y1, x2, y2], img0[batch_idx], label=label, color=(255, 0, 0)) #cv2.rectangle(img0[batch_idx], (x1, y1), (x2, y2), (0, 0, 255), 1) # 如果报错 TypeError: an integer is required (got type tuple),检查是不是传入了img_tensor if SAVE: # 保存结果 cv2.imwrite(os.path.join(dst_path, img_name[batch_idx]), img0[batch_idx]) if SHOW: cv2.imshow('aaa', img0[batch_idx]) cv2.waitKey(0) print('time use: %.3fs' % (time.time() - start))
parser.add_argument("--name", default="parallel", type=str, help="Append to logdir name") parser.add_argument("--config", default=None, type=str, help="Config file path") args = parser.parse_args() if torch.cuda.is_available(): index = args.device if args.device else str( 0 if gm is None else gm.auto_choice()) else: index = 'cpu' device = select_device(index) hparams = HParam(args.config) \ if args.config else HParam(osp.join(osp.abspath(os.getcwd()), "config", "default.yaml")) logdir = osp.join(hparams.trainer.logdir, f"%s-%s" % (hparams.data.dataset, args.name)) checkpoint = args.checkpoint or get_last_chkpt_path(logdir) normalizer = StandardNorm(hparams.audio.spec_mean, hparams.audio.spec_std) processor = TextProcessor(hparams.text) text2mel = ParallelText2Mel(hparams.parallel) text2mel.eval() synthesizer = Synthesizer(model=text2mel, checkpoint=checkpoint,
def test(cfg, data, batch_size, img_size, conf_thres, iou_thres, nms_thres, src_txt_path, weights, log_file_path=None, model=None): # 0、初始化一些参数 data = parse_data_cfg(data) nc = int(data['classes']) # number of classes names = load_classes(data['names']) # 1、加载网络 if model is None: device = select_device('0') model = Darknet(cfg) if weights.endswith('.pt'): # TODO: .weights权重格式 model.load_state_dict( torch.load(weights, map_location=device)['model'] ) # 20200704_50epoch_modify_noobj # TODO:map_location=device ? if torch.cuda.device_count() > 1: model = nn.DataParallel(model) # clw note: 多卡 else: device = next(model.parameters()).device # get model device model.to(device).eval() # 2、加载数据集 test_dataset = VocDataset(src_txt_path, img_size, with_label=True, is_training=False) dataloader = DataLoader( test_dataset, batch_size=batch_size, shuffle=False, num_workers=8, # TODO collate_fn=test_dataset.test_collate_fn, # TODO pin_memory=True) # 3、预测,前向传播 image_nums = 0 s = ('%20s' + '%10s' * 6) % ('Class', 'Images', 'Targets', 'P', 'R', 'mAP@{}'.format(iou_thres), 'F1') #s = ('%20s' + '%10s' * 6) % ('Class', 'ImgNum', 'Target', 'P', 'R', '[email protected]', 'F1') p, r, f1, mp, mr, map, mf1 = 0., 0., 0., 0., 0., 0., 0. jdict, stats, ap, ap_class = [], [], [], [] pbar = tqdm(dataloader) for i, (img_tensor, target_tensor, _, _) in enumerate(pbar): img_tensor = img_tensor.to(device) # (bs, 3, 416, 416) target_tensor = target_tensor.to(device) height, width = img_tensor.shape[2:] start = time.time() # Disable gradients with torch.no_grad(): # (1) Run model output = model( img_tensor ) # (x1, y1, x2, y2, obj_conf, class_conf, class_pred) # (2) NMS nms_output = non_max_suppression(output, conf_thres, nms_thres) s = 'time use per batch: %.3fs' % (time.time() - start) pbar.set_description(s) for batch_idx, pred in enumerate(nms_output): # pred: (bs, 7) labels = target_tensor[target_tensor[:, 0] == batch_idx, 1:] nl = len(labels) # len of label tcls = labels[:, 0].tolist() if nl else [] # target class image_nums += 1 # 考虑一个预测 box 都没有的情况,比如 conf 太高 if pred is None: if nl: stats.append(([], torch.Tensor(), torch.Tensor(), tcls)) continue # Clip boxes to image bounds TODO:有必要,因为 label 都是经过clip的,所以如果去掉clip,mAP应该会有所降低 clip_coords(pred, (height, width)) # mAP is the same # Assign all predictions as incorrect correct = [0] * len(pred) if nl: detected = [] tcls_tensor = labels[:, 0] # target boxes tbox = xywh2xyxy(labels[:, 1:5]) tbox[:, [0, 2]] *= img_tensor[batch_idx].size()[2] # w tbox[:, [1, 3]] *= img_tensor[batch_idx].size()[1] # h # Search for correct predictions for i, (*pbox, pconf, pcls_conf, pcls) in enumerate(pred): # Break if all targets already located in image if len(detected) == nl: break # Continue if predicted class not among image classes if pcls.item() not in tcls: continue # Best iou, index between pred and targets m = (pcls == tcls_tensor).nonzero().view(-1) iou, bi = bbox_iou(pbox, tbox[m]).max(0) # If iou > threshold and class is correct mark as correct if iou > iou_thres and m[ bi] not in detected: # and pcls == tcls[bi]: correct[i] = 1 detected.append(m[bi]) # print('stats.append: ', (correct, pred[:, 4].cpu(), pred[:, 6].cpu(), tcls)) ''' pred flag ( [1, 0, 1, 0, 0, 1, 0, 0, 1], pred conf tensor([0.17245, 0.14642, 0.07215, 0.07138, 0.07069, 0.06449, 0.06222, 0.05580, 0.05452]), pred cls tensor([2., 2., 2., 2., 2., 2., 2., 2., 2.]), lb_cls [2.0, 2.0, 2.0, 2.0, 2.0]) stats is a [] ''' stats.append( (correct, pred[:, 4].cpu(), pred[:, 6].cpu(), tcls)) # Append statistics (correct, conf, pcls, tcls) # after get stats for all images , ... # Compute statistics stats = [np.concatenate(x, 0) for x in list(zip(*stats))] # to numpy if len(stats): p, r, ap, f1, ap_class = ap_per_class(*stats) mp, mr, map, mf1 = p.mean(), r.mean(), ap.mean(), f1.mean() nt = np.bincount(stats[3].astype(np.int64), minlength=nc) # number of targets per class else: nt = torch.zeros(1) # Print results # time.sleep(0.01) # clw note: 防止前面 tqdm 还没输出,但是这里已经打印了 #pf = '%20s' + '%10.3g' * 6 # print format pf = '%20s' + '%10s' + '%10.3g' * 5 pf_value = pf % ('all', str(image_nums), nt.sum(), mp, mr, map, mf1) print(pf_value) if __name__ != '__main__': write_to_file(s, log_file_path) write_to_file(pf_value, log_file_path) results = [] results.append({"all": (mp, mr, map, mf1)}) # Print results per class #if verbose and nc > 1 and len(stats): if nc > 1 and len(stats): for i, c in enumerate(ap_class): #print(pf % (names[c], seen, nt[c], p[i], r[i], ap[i], f1[i])) print(pf % (names[c], '', nt[c], p[i], r[i], ap[i], f1[i])) if __name__ != '__main__': write_to_file( pf % (names[c], '', nt[c], p[i], r[i], ap[i], f1[i]), log_file_path) results.append({names[c]: (p[i], r[i], ap[i], f1[i])}) # Return results maps = np.zeros(nc) + map for i, c in enumerate(ap_class): maps[c] = ap[i] return (mp, mr, map, mf1), maps
parser.add_argument('--batch-size', type=int, default=4, help='total batch size for all GPUs') parser.add_argument('--patch-size', type=int, default=800, help='patch sizes') parser.add_argument('--patch-stride', type=int, default=400, help='patch sizes') parser.add_argument('--resume', nargs='?', const=True, default=False, help='resume most recent training') parser.add_argument('--device', default='', help='cuda device, i.e. 0 or 0,1,2,3 or cpu') # parser.add_argument('--local_rank', type=int, default=-1, help='DDP parameter, do not modify') parser.add_argument('--adam', action='store_true', help='use torch.optim.Adam() optimizer') parser.add_argument('--logdir', type=str, default='runs/', help='logging directory') parser.add_argument('--weight-path', type=str, default='./weights', help='path for storing weights') parser.add_argument('--name', default='kari_seg', help='name for the run') opt = parser.parse_args() opt.global_rank = -1 set_logging(level=logging.INFO) logger.info(opt) # Load hyper-parameters with open(opt.hyp) as f: hyp = yaml.load(f, Loader=yaml.FullLoader) # Device device = select_device(opt.device, batch_size=opt.batch_size) # Tensorboard log_dir = increment_dir(Path(opt.logdir) / 'run', opt.name) tb_writer = SummaryWriter(log_dir=log_dir) # Train train(hyp, opt, device, tb_writer)
def test(cfg, data, batch_size, img_size, conf_thres, iou_thres, nms_thres, src_txt_path='./valid.txt', dst_path='./output', weights=None, model=None, log_file_path='log.txt'): # 0、初始化一些参数 if not os.path.exists(dst_path): os.mkdir(dst_path) data = parse_data_cfg(data) nc = int(data['classes']) # number of classes class_names = load_classes(data['names']) # 1、加载网络 if model is None: device = select_device(opt.device) model = Darknet(cfg) if weights.endswith('.pt'): # TODO: .weights权重格式 model.load_state_dict( torch.load(weights)['model']) # TODO:map_location=device ? if torch.cuda.device_count() > 1: model = nn.DataParallel(model) # clw note: 多卡 else: device = next(model.parameters()).device # get model device model.to(device).eval() # 2、加载数据集 test_dataset = VocDataset(src_txt_path, img_size, with_label=True, is_training=False) dataloader = DataLoader( test_dataset, batch_size=batch_size, shuffle=False, num_workers=8, # TODO collate_fn=test_dataset.test_collate_fn, # TODO pin_memory=True) # 3、预测,前向传播 s = ('%20s' + '%10s' * 6) % ('Class', 'Images', 'Targets', 'P', 'R', 'mAP@{}'.format(iou_thres), 'F1') pbar = tqdm(dataloader) for i, (img_tensor, _, img_path, shapes) in enumerate(pbar): start = time.time() img_tensor = img_tensor.to(device) # (bs, 3, 416, 416) # Disable gradients with torch.no_grad(): # (1) Run model output = model(img_tensor) # [0] # (2) NMS nms_output = non_max_suppression(output, conf_thres, nms_thres) # list (64,) s = 'time use per batch: %.3fs' % (time.time() - start) pbar.set_description(s) for batch_idx, pred in enumerate( nms_output ): # pred: (bs, 7) -> xyxy, obj_conf*class_conf, class_conf, cls_idx ################################################ if pred is None: continue bboxes_prd = torch.cat((pred[:, 0:5], pred[:, 6].unsqueeze(1)), dim=1).cpu().numpy() ###### clw note: coord transform to origin size(because of resize and so on....) is really important !!! scale_coords(img_tensor[batch_idx].shape[1:], bboxes_prd, shapes[batch_idx][0], shapes[batch_idx][1]) # to original shape ###### for bbox in bboxes_prd: coor = np.array(bbox[:4], dtype=np.int32) score = bbox[4] class_ind = int(bbox[5]) class_name = class_names[class_ind] classes_pred.add(class_name) score = '%.4f' % score xmin, ymin, xmax, ymax = map(str, coor) s = ' '.join([ str(img_path[batch_idx]), str(score), xmin, ymin, xmax, ymax ]) + '\n' with open( os.path.join(result_path, 'comp4_det_test_' + class_name + '.txt'), 'a') as f: f.write(s) ################################################ return calc_APs()
import torch import cv2 from model.backbone.darknet53 import Darknet53 from model.neck.fpn import Fpn from yolov3 import YOLOv3 from model.model_utils import model_info from utils.utils import select_device device = select_device( ) # 如果是单卡,可以改成最简单的一句 device = torch.device("cuda" if torch.cuda.is_available() else "cpu") if __name__ == '__main__': ### 输入一张416x416的图片,测试归一化之后准备送入backbone的值 img_np = cv2.imread( 'C:/Users/62349/Desktop/111/20191105_1732_0002_GC2_0000_2048.jpg' ) # img_np: [nh, nw, nc] img_np = cv2.resize(img_np, (416, 416)) # Resize to the input dimension img_tensor = torch.from_numpy(img_np.transpose((2, 0, 1))).to( device ) # TODO: img = img[:,:,::-1].transpose((2,0,1)) # BGR -> RGB | H X W C -> C X H X W # img_tensor = img_tensor.unsqueeze(0) img_tensor = img_tensor.float().div(255).unsqueeze( 0) # clw note: img_tensor: [batch_size, nc, nh, nw] # clw note:如果我们希望按批次处理图像(批量图像由 GPU 并行处理,这样可以提升速度),我们就需要固定所有图像的高度和宽度。 # 这就需要将多个图像整合进一个大的批次(将许多 PyTorch 张量合并成一个)。 ### 1、调试backbone # net1 = Darknet53()
def main(opt): # cudnn.benchmark = True device = select_device(opt.device, batch_size=opt.batchsize) # device = torch.device('cuda') # print(opt) ######### 数据加载 ######### train_loader = get_loader(opt.train_data,opt.batchsize,shuffle=True, num_workers=4, pin_memory=False) if opt.val_data: val_loader = get_loader(opt.val_data,opt.val_bs,shuffle=False, num_workers=1, pin_memory=False) ########## 模型加载 ######## model = build_model(opt) model = torch.nn.DataParallel(model) model = model.to(device) ######## 预训练参数加载 ######## if opt.preweight_path: model.load_state_dict(torch.load(opt.preweight_path)) ####### 定义模型优化器 ############# if opt.optimizer_type == 'Adam': optimizer = torch.optim.Adam(model.parameters(), opt.lr) elif opt.optimizer_type == 'SGD': optimizer = torch.optim.SGD(model.parameters(), lr=opt.lr, momentum=0.9, weight_decay=1e-8, nesterov=True) ####### 学习率 衰减方式 if opt.lr_scheduler == 'CosineAnnealingWarmRestarts': scheduler = torch.optim.lr_scheduler.CosineAnnealingWarmRestarts(optimizer, T_0=50, T_mult=2, eta_min=0, last_epoch=-1) elif opt.lr_scheduler == 'LambdaLR': lambda1 = lambda epoch: epoch // 30 lambda2 = lambda epoch: 0.95 ** epoch scheduler = torch.optim.lr_scheduler.LambdaLR(optimizer, lr_lambda=[lambda1, lambda2]) elif opt.lr_scheduler == 'MultiplicativeLR': lmbda = lambda epoch: 0.95 scheduler = torch.optim.lr_scheduler.MultiplicativeLR(optimizer, lr_lambda=lmbda) elif opt.lr_scheduler == 'StepLR': lmbda = lambda epoch: 0.95 scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=30, gamma=0.1) elif opt.lr_scheduler == 'MultiStepLR': scheduler = torch.optim.lr_scheduler.MultiStepLR(optimizer,milestones=[30,80,120,200], gamma=0.1) elif opt.lr_scheduler == 'ExponentialLR': scheduler = torch.optim.lr_scheduler.ExponentialLR(optimizer, gamma=0.99) elif opt.lr_scheduler == 'CosineAnnealingLR': scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(optimizer, T_max=50, eta_min=0,) elif opt.lr_scheduler == 'ReduceLROnPlateau': optimizer = torch.optim.SGD(model.parameters(), lr=0.1, momentum=0.9) scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(optimizer,'min') elif opt.lr_scheduler == 'ReduceLROnPlateau': optimizer = torch.optim.SGD(model.parameters(), lr=0.1, momentum=0.9) scheduler = torch.optim.lr_scheduler.CyclicLR(optimizer, base_lr=0.01, max_lr=0.1) #### ---- loss function ---- if opt.lossfunction == 'BCE': opt.criterion = torch.nn.BCELoss() elif opt.lossfunction == 'structure_loss': opt.criterion = structure_loss elif opt.lossfunction == 'symmetric_lovasz' : opt.criterion = symmetric_lovasz print('##########################################\n\n\ntrain start') for epoch in range(0, opt.epoch): if opt.model_type == 'PraNet': parnet_train(epoch,model,optimizer,train_loader,val_loader,device) else: train(epoch,model,optimizer,train_loader,val_loader,device) scheduler.step() print('all train done')
return LambdaLR(optim, sche_with_warmup) if __name__ == "__main__": params_file = 'params.yml' params_file = check_file(params_file) params = Params('params.yml') debug = True if params.mode == 'debug' else False params.save_dir = os.path.join(os.getcwd(), params.save_dir) os.makedirs(params.save_dir, exist_ok=True) device = select_device(params.device, batch_size=params.batch_size) init_seeds(10086) loaders = get_loaders(params.input_dir, params.batch_size, params.num_workers, params.frames, params.img_size, debug=debug) net = get_model(params.weights, params.num_classes) # net = nn.DataParallel(net).to(device, non_blocking=True) net = net.to(device, non_blocking=True) loss = get_loss() params.accumulate = max(round(params.nbs / params.batch_size), 1) params.weight_decay_nbs = (params.batch_size * params.accumulate / params.nbs) * params.weight_decay pg0, pg1, pg2 = [], [], [] for k, v in net.named_modules(): if hasattr(v, 'bias') and isinstance(v.bias, nn.Parameter): pg2.append(v.bias)
train_dataset = torchvision.datasets.MNIST(root=data_root, train=True, download=True, transform=data_transform) train_loader = DataLoader(train_dataset, shuffle=True, batch_size=train_bs, pin_memory=True, num_workers=train_nworkers) test_dataset = torchvision.datasets.MNIST(root=data_root, train=False, download=True, transform=data_transform) test_loader = DataLoader(test_dataset, shuffle=True, batch_size=test_bs, pin_memory=True, num_workers=test_nworkers) device = utils.select_device(force_cpu=False) # instantiate network net = SimpleConvNet(1, 10).to(device) # criterion loss_fn = CustomLoss() # optimer optimizer = torch.optim.SGD(net.parameters(), lr=learning_rate, momentum=0.5) # load checkpoint if needed start_epoch = 0 start_niter = 0 if resume: ckpt = utils.load_checkpoint(ckpt_path) # custom method for loading last checkpoint net.load_state_dict(ckpt['model_state'])