def evaluate(): ## calculate the average IOU between pred and gt if os.path.exists(resume): checkoint = torch.load(resume) start_epoch = checkoint['epoch'] model.load = model.load_state_dict(checkoint['model']) #optimizer.load_state_dict(checkoint['optim']) print('load the resume checkpoint from epoch{}'.format(start_epoch)) else: print("no resume checkpoint to load") model.eval() IOUs = 0 total_correct = 0 data_eval = multiDataset(data_rootpath, test=True) eval_loader = torch.utils.data.DataLoader(data_eval, batch_size=args.batch_size, shuffle=True, collate_fn=multi_collate) # model.SingleNet if os.path.exists(singlemodel_path): t1 = time.time() if is_GPU: checkoint = torch.load(singlemodel_path) else: checkoint = torch.load(singlemodel_path, map_location=lambda storage, loc: storage) ## notice :here we should map the order of GPU ## when the SingleModel is trained in GPU-1,we should map the model to GPU-0 model.SingleNet.load = model.SingleNet.load_state_dict( checkoint['model']) t2 = time.time() print('singleNetwork load resume model from epoch{} use {}s'.format( checkoint['epoch'], t2 - t1)) if is_GPU: model.SingleNet = model.SingleNet.cuda() for batch_idx, (img1s, img2s, targets, _, v12s) in enumerate(eval_loader): ## when predict,the target is target1 (img1) if is_GPU: img1s = Variable(img1s.cuda(), volatile=True) img2s = Variable(img2s.cuda(), volatile=True) targets = Variable(targets.cuda()) else: img1s = Variable(img1s, volatile=True) img2s = Variable(img2s, volatile=True) targets = Variable(targets) t1 = time.time() # print v12s outputs = model.predict(img1s, img2s, v12s, iter=args.iter) t2 = time.time() print('in batch{}/{} use {}s'.format(batch_idx * args.batch_size, len(eval_loader.dataset), t2 - t1)) #occupy=(outputs.data>0.5)if is_GPU else (outputs.data>0.5).type(torch.FloatTensor) #print occupy.sum() occupy = (outputs.data > 0.5) for idx, target in enumerate(targets): #correct+=(occupy[idx].eq(target.data)).sum() insect = (target.data[occupy[idx]]).sum() union = target.data.sum() + occupy[idx].sum() - insect iou = insect * 1.0 / union IOUs += iou total_correct += insect #print 'correct num:{}'.format(total_correct) print('the average correct rate:{}'.format(total_correct * 1.0 / (len(eval_loader.dataset)))) print('the average iou:{}'.format(IOUs * 1.0 / (len(eval_loader.dataset))))
singlemodel_path = './single_model/csg_single_train_ce_server_87.pth' #model=singleNet() model = MulitUpdateNet_deeper() if os.path.exists(resume): if is_GPU: model.load_state_dict(torch.load(resume)['model']) else: model.load_state_dict( torch.load(resume, map_location=lambda storage, loc: storage)['model']) print('load trained model success') data_rootpath = './dataset/CsgData' dataset = multiDataset(data_rootpath, 'csg', test=True) def generate_binvox(num1, num2, demo_path='./demo2/'): if os.path.exists(singlemodel_path): t1 = time.time() if is_GPU: checkoint = torch.load(singlemodel_path) else: checkoint = torch.load(singlemodel_path, map_location=lambda storage, loc: storage) ## notice :here we should map the order of GPU ## when the SingleModel is trained in GPU-1,we should map the model to GPU-0 model.SingleNet.load = model.SingleNet.load_state_dict( checkoint['model'])
''' a=np.random.randint(0,2,(2,64,64,64)) a=Variable(torch.from_numpy(a).type(torch.FloatTensor)) if is_GPU: a = Variable(torch.from_numpy(a).cuda().type(torch.FloatTensor)) b= dense2sparse(a) print (b[0].data.size()) ##list a=sparse2dense(b) print a ''' data_rootpath = '../dataset/CubeData' dataset = multiDataset(data_rootpath) img_id, test_img = dataset.pull_img(10, 2) test_img = np.array(test_img) #print test_img #cv2.imshow('mat',test_img) #cv2.waitKey(2000) #print img_id ## img read bingo ''' img1,img2,target1,v12 = dataset.pull_item(10,2,7) cv2.imshow('img1',img1.numpy()) cv2.imshow('img2',img2.numpy()) cv2.waitKey(2000) print target1.numpy() target_data= target1.numpy().astype(np.bool)