def main(): global is_resnet global is_densenet global is_senet global pretrain_logical is_resnet = False is_densenet = False is_senet = True pretrain_logical = True model = define_model() # model = torch.nn.DataParallel(model).cuda() # model.load_state_dict(torch.load('./pretrained_model/model_senet')) model = model.cuda().float() model.load_state_dict( torch.load('model_output/senet_trained/model_epoch_1.pth')) test_loader = loaddata.getTestingData(1) test(test_loader, model, 0.25)
def main(): global args args = parser.parse_args() model_selection = 'resnet' model = define_model(encoder=model_selection) original_model2 = net_mask.drn_d_22(pretrained=True) model2 = net_mask.AutoED(original_model2) model = torch.nn.DataParallel(model).cuda() model2 = torch.nn.DataParallel(model2).cuda() model.load_state_dict( torch.load('./pretrained_model/model_' + model_selection)) model2.load_state_dict(torch.load('./net_mask/mask_' + model_selection)) test_loader = loaddata.getTestingData(1) test(test_loader, model, model2, 'mask_' + model_selection)
def main(): Encoder = modules.E_resnet(resnet.resnet50(pretrained=True)) N = net.model(Encoder, num_features=2048, block_channel=[256, 512, 1024, 2048]) N = torch.nn.DataParallel(N).cuda() N.load_state_dict(torch.load('./models/N')) N_adv = copy.deepcopy(N) N_adv.load_state_dict(torch.load('./models/N_adv')) cudnn.benchmark = True test_loader = loaddata.getTestingData(8) #test for N_adv(x*) test_N_adv(test_loader, N, N_adv, epsilon=0.05, iteration=10) test_N_adv(test_loader, N, N_adv, epsilon=0.1, iteration=10) test_N_adv(test_loader, N, N_adv, epsilon=0.15, iteration=10) test_N_adv(test_loader, N, N_adv, epsilon=0.2, iteration=10)
def main(): global is_resnet global is_densenet global is_senet global pretrain_logical is_resnet = True is_densenet = False is_senet = False pretrain_logical = False # cuda options # model = torch.nn.DataParallel(model).cuda() # model.load_state_dict(torch.load('./pretrained_model/model_senet')) #model = model.cuda().float() model = define_model() #model = Resnet18_md(3) #model.load_state_dict(torch.load('/home/doragu/Dropbox/school/michigan/19w/3d-estimation-cnn/data/models/monodepth_resnet18_001.pth', map_location='cpu' )) #model.load_state_dict(torch.load('model_output/resnet_untrained/model_epoch_4.pth', map_location='cpu' )) #model.load_state_dict(torch.load('/home/doragu/Dropbox/school/michigan/19w/3d-estimation-cnn/data/models/monodepth_resnet18_001.pth', map_location='cpu' )) test_loader = loaddata.getTestingData(1) test(test_loader, model, 0.25)
def test(thre): model = define_test_model() test_loader = loaddata.getTestingData(1) #test_loader = loaddata.getStyleTestingData(1) model.eval() totalNumber = 0 Ae = 0 Pe = 0 Re = 0 Fe = 0 errorSum = { 'MSE': 0, 'RMSE': 0, 'ABS_REL': 0, 'LG10': 0, 'MAE': 0, 'DELTA1': 0, 'DELTA2': 0, 'DELTA3': 0 } with torch.no_grad(): for i, sample_batched in enumerate(test_loader): image, depth = sample_batched['image'], sample_batched['depth'] # depth = depth.cuda(async=True) if use_cuda: depth = depth.cuda() image = image.cuda() else: pass #image = torch.autograd.Variable(image, volatile=True) #depth = torch.autograd.Variable(depth, volatile=True) #print(image.size()) #print(image[0][3]) image = image[:, 0:3, :, :] #image.squeeze() #print(image.size()) b_output = model(image) #output = torch.nn.functional.upsample(output, size=[depth.size(2),depth.size(3)], mode='bilinear') output = torch.nn.functional.interpolate( b_output, size=[depth.size(2), depth.size(3)], mode='bilinear', align_corners=False) #visualize_image(image, b_output, output, depth) depth_edge = edge_detection(depth) output_edge = edge_detection(output) batchSize = depth.size(0) totalNumber = totalNumber + batchSize errors = util.evaluateError(output, depth) errorSum = util.addErrors(errorSum, errors, batchSize) averageError = util.averageErrors(errorSum, totalNumber) edge1_valid = (depth_edge > thre) edge2_valid = (output_edge > thre) #print(output_edge) #exit() nvalid = np.sum( torch.eq(edge1_valid, edge2_valid).float().data.cpu().numpy()) A = nvalid / (depth.size(2) * depth.size(3)) nvalid2 = np.sum( ((edge1_valid + edge2_valid) == 2).float().data.cpu().numpy()) P = nvalid2 / (np.sum(edge2_valid.data.cpu().numpy())) R = nvalid2 / (np.sum(edge1_valid.data.cpu().numpy())) F = (2 * P * R) / (P + R) Ae += A Pe += P Re += R Fe += F print('Epoch: [{0}/{1}]\t'.format(i, len(test_loader))) Av = Ae / totalNumber Pv = Pe / totalNumber Rv = Re / totalNumber Fv = Fe / totalNumber print('PV', Pv) print('RV', Rv) print('FV', Fv) averageError['RMSE'] = np.sqrt(averageError['MSE']) print(averageError) if is_resnet: if pretrain_logical: save_name = 'resnet_pretrained' else: save_name = 'renet_untrained' elif is_densenet: if pretrain_logical: save_name = 'densenet_pretrained' else: save_name = 'densenet_untrained' else: if pretrain_logical: save_name = 'senet_pretrained' else: save_name = 'senet_untrained' dir_path = os.path.dirname(os.path.realpath(__file__)) result_out_path = Path(dir_path + '/csvs') if not result_out_path.exists(): result_out_path.mkdir() with open('csvs/' + save_name + '.csv', 'w') as sub: sub.write('RV' + str(Rv) + '\n') sub.write('FV' + str(Fv) + '\n') sub.write('RMSE' + str(averageError['RMSE']) + '\n') print('Done!') return