1 Star 0 Fork 1

张金来/ModelNet40-C

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
main_norm3.py 91.14 KB
一键复制 编辑 原始数据 按行查看 历史
cuge 提交于 2023-04-30 22:38 . add new
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim.lr_scheduler as lr_scheduler
import random
from dataloader import create_dataloader
from time import time
from datetime import datetime
from progressbar import ProgressBar
import models
from torch.autograd import Variable
from collections import defaultdict
import os
import numpy as np
import argparse
from all_utils import (
TensorboardManager, PerfTrackTrain,
PerfTrackVal, TrackTrain, smooth_loss, DATASET_NUM_CLASS,
rscnn_voting_evaluate_cls, pn2_vote_evaluate_cls)
from configs import get_cfg_defaults
import pprint
from pointnet_pyt.pointnet.model import feature_transform_regularizer
import sys
import aug_utils
import aug_utils3
import aug_utils4
from third_party import bn_helper, tent_helper
#add jieduan22 renwu3.1.2
from cdload import create_dataloader as create_dataloadercd
from pointcloudc_utils import eval_corrupt_wrapper
#end jieduan22 renwu3.1.2
DEVICE = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
if DEVICE.type == 'cpu':
print('WARNING: Using CPU')
class AverageMeter(object):
"""Computes and stores the average and current value"""
def __init__(self):
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count
def adapt_bn(data,model,cfg):
model = bn_helper.configure_model(model,eps=1e-5, momentum=0.1,reset_stats=False,no_stats=False)
for _ in range(cfg.ITER):
model(**data)
print("Adaptation Done ...")
model.eval()
return model
def adapt_tent(data,model,cfg):
model = tent_helper.configure_model(model,eps=1e-5, momentum=0.1)
parameter,_ = tent_helper.collect_params(model)
optimizer_tent = torch.optim.SGD(parameter, lr=0.001,momentum=0.9)
for _ in range(cfg.ITER):
# index = np.random.choice(args.number,args.batch_size,replace=False)
tent_helper.forward_and_adapt(data,model,optimizer_tent)
print("Adaptation Done ...")
model.eval()
return model
def check_inp_fmt(task, data_batch, dataset_name):
if task in ['cls', 'cls_trans']:
# assert set(data_batch.keys()) == {'pc', 'label'}
# print(data_batch['pc'],data_batch['label'])
pc, label = data_batch['pc'], data_batch['label']
# special case made for modelnet40_dgcnn to match the
# original implementation
# dgcnn loads torch.DoubleTensor for the test dataset
if dataset_name == 'modelnet40_dgcnn':
assert isinstance(pc, torch.FloatTensor) or isinstance(
pc, torch.DoubleTensor)
else:
if not isinstance(pc, torch.FloatTensor):
pc = pc.float()
assert isinstance(pc, torch.FloatTensor)
assert isinstance(label, torch.LongTensor)
assert len(pc.shape) == 3
assert len(label.shape) == 1
b1, _, y = pc.shape[0], pc.shape[1], pc.shape[2]
b2 = label.shape[0]
assert b1 == b2
assert y == 3
assert label.max().item() < DATASET_NUM_CLASS[dataset_name]
assert label.min().item() >= 0
else:
assert NotImplemented
def check_out_fmt(task, out, dataset_name):
if task == 'cls':
#print('out')
#print(out)
#print(out[0].shape)
#print(out[1].shape)
#print(out[2].shape)
#print(out.shape)
assert set(out.keys()) == {'logit'}
logit = out['logit']
assert isinstance(logit, torch.FloatTensor if DEVICE.type == 'cpu' else torch.cuda.FloatTensor)
assert len(logit.shape) == 2
# print(logit.shape[1])
# print(dataset_name)
# print(DATASET_NUM_CLASS[dataset_name], logit.shape[1])
assert DATASET_NUM_CLASS[dataset_name] == logit.shape[1]
elif task == 'cls_trans':
#print('out')
#print(out)
#print(out[0].shape)
#print(out[1].shape)
#print(out[2].shape)
#print(out.shape)
assert set(out.keys()) == {'logit', 'trans_feat'}
logit = out['logit']
trans_feat = out['trans_feat']
assert isinstance(logit, torch.FloatTensor if DEVICE.type == 'cpu' else torch.cuda.FloatTensor)
assert isinstance(trans_feat, torch.FloatTensor if DEVICE.type == 'cpu' else torch.cuda.FloatTensor)
assert len(logit.shape) == 2
assert len(trans_feat.shape) == 3
assert trans_feat.shape[0] == logit.shape[0]
# 64 coming from pointnet implementation
assert (trans_feat.shape[1] == trans_feat.shape[2]) and (trans_feat.shape[1] == 64)
assert DATASET_NUM_CLASS[dataset_name] == logit.shape[1]
else:
assert NotImplemented
def get_inp(task, model, data_batch, batch_proc, dataset_name):
check_inp_fmt(task, data_batch, dataset_name)
if not batch_proc is None:
data_batch = batch_proc(data_batch, DEVICE)
check_inp_fmt(task, data_batch, dataset_name)
if isinstance(model, nn.DataParallel):
model_type = type(model.module)
else:
model_type = type(model)
if task in ['cls', 'cls_trans']:
pc = data_batch['pc']
inp = {'pc': pc}
else:
assert False
return inp
#add jieduan20 renwu3.3
def get_target(task, model, data_batch, batch_proc, dataset_name):
if task in ['cls', 'cls_trans']:
label = data_batch['label']
add_target = {'label': label}
return add_target
#end jieduan20 renwu3.3
#add jieduan20 renwu3.3
#def get_target(task, model, data_batch, batch_proc, dataset_name):
# if task in ['cls', 'cls_trans']:
# add_target = data_batch['label']
# return add_target
#end jieduan20 renwu3.3
def get_loss(task, loss_name, data_batch, out, dataset_name):
"""
Returns the tensor loss function
:param task:
:param loss_name:
:param data_batch: batched data; note not applied data_batch
:param out: output from the model
:param dataset_name:
:return: tensor
"""
check_out_fmt(task, out, dataset_name)
if task == 'cls':
label = data_batch['label'].to(out['logit'].device)
if loss_name == 'cross_entropy':
if 'label_2' in data_batch.keys():
label_2 = data_batch['label_2'].to(out['logit'].device)
if isinstance(data_batch['lam'],torch.Tensor):
loss = 0
for i in range(data_batch['pc'].shape[0]):
loss_tmp = smooth_loss(out['logit'][i].unsqueeze(0), label[i].unsqueeze(0).long()) * (1 - data_batch['lam'][i]) + smooth_loss(out['logit'][i].unsqueeze(0), label_2[i].unsqueeze(0).long()) * data_batch['lam'][i]
loss += loss_tmp
loss = loss / data_batch['pc'].shape[0]
else:
loss = smooth_loss(out['logit'], label) * (1 - data_batch['lam']) + smooth_loss(out['logit'], label_2) * data_batch['lam']
else:
loss = F.cross_entropy(out['logit'], label)
# source: https://github.com/WangYueFt/dgcnn/blob/master/pytorch/util.py
elif loss_name == 'smooth':
if 'label_2' in data_batch.keys():
label_2 = data_batch['label_2'].to(out['logit'].device)
if isinstance(data_batch['lam'],torch.Tensor):
loss = 0
for i in range(data_batch['pc'].shape[0]):
loss_tmp = smooth_loss(out['logit'][i].unsqueeze(0), label[i].unsqueeze(0).long()) * (1 - data_batch['lam'][i]) + smooth_loss(out['logit'][i].unsqueeze(0), label_2[i].unsqueeze(0).long()) * data_batch['lam'][i]
loss += loss_tmp
loss = loss / data_batch['pc'].shape[0]
else:
loss = smooth_loss(out['logit'], label) * (1 - data_batch['lam']) + smooth_loss(out['logit'], label_2) * data_batch['lam']
else:
loss = smooth_loss(out['logit'], label)
else:
assert False
elif task == 'cls_trans':
label = data_batch['label'].to(out['logit'].device)
trans_feat = out['trans_feat']
logit = out['logit']
if loss_name == 'cross_entropy':
if 'label_2' in data_batch.keys():
label_2 = data_batch['label_2'].to(out['logit'].device)
if isinstance(data_batch['lam'],torch.Tensor):
loss = 0
for i in range(data_batch['pc'].shape[0]):
loss_tmp = smooth_loss(out['logit'][i].unsqueeze(0), label[i].unsqueeze(0).long()) * (1 - data_batch['lam'][i]) + smooth_loss(out['logit'][i].unsqueeze(0), label_2[i].unsqueeze(0).long()) * data_batch['lam'][i]
loss += loss_tmp
loss = loss / data_batch['pc'].shape[0]
else:
loss = smooth_loss(out['logit'], label) * (1 - data_batch['lam']) + smooth_loss(out['logit'], label_2) * data_batch['lam']
else:
loss = F.cross_entropy(out['logit'], label)
loss += feature_transform_regularizer(trans_feat) * 0.001
elif loss_name == 'smooth':
if 'label_2' in data_batch.keys():
label_2 = data_batch['label_2'].to(out['logit'].device)
if isinstance(data_batch['lam'],torch.Tensor):
loss = 0
for i in range(data_batch['pc'].shape[0]):
loss_tmp = smooth_loss(out['logit'][i].unsqueeze(0), label[i].unsqueeze(0).long()) * (1 - data_batch['lam'][i]) + smooth_loss(out['logit'][i].unsqueeze(0), label_2[i].unsqueeze(0).long()) * data_batch['lam'][i]
loss += loss_tmp
loss = loss / data_batch['pc'].shape[0]
else:
loss = smooth_loss(out['logit'], label) * (1 - data_batch['lam']) + smooth_loss(out['logit'], label_2) * data_batch['lam']
else:
loss = smooth_loss(out['logit'], label)
loss += feature_transform_regularizer(trans_feat) * 0.001
else:
assert False
else:
assert False
return loss
## other attacks
def aof_attack(model, pc, label, eps=0.05):
# pc = pc.transpose(2, 1)
# import sys
# sys.path.append('/data/data1/jinlai.zhang/AOF/baselines/')
# from util.utils import str2bool, set_seed
from attack import CWAOF
from attack import CrossEntropyAdvLoss, LogitsAdvLoss, UntargetedLogitsAdvLoss
from attack import ClipPointsLinf, ChamferkNNDist, L2Dist
#AutoEncoder model
clip_func = ClipPointsLinf(budget=eps)
adv_func = UntargetedLogitsAdvLoss(kappa=30.)
dist_func = L2Dist()
attacker = CWAOF(model, adv_func, dist_func,
attack_lr=1e-2,
binary_step=2,
num_iter=200, GAMMA=0.25,
low_pass = 100,
clip_func=clip_func)
with torch.no_grad():
pc, label = pc.float().cuda(non_blocking=True), \
label.long().cuda(non_blocking=True)
# attack!
# print(pc.size())
# pc = pc.transpose(1, 2)
_, best_pc, success_num = attacker.attack(pc, label)
best_pc = torch.from_numpy(best_pc) #.to(torch.float32).contiguous().cuda()
# print(best_pc.size())
# best_pc = best_pc.transpose(2, 1).float()
return best_pc
def tdadv_attack(model, pc, label, eps=0.05):
# pc = pc.transpose(2, 1)
# import sys
# sys.path.append('/data/data1/jinlai.zhang/AOF/baselines/')
# from util.utils import str2bool, set_seed
from attack import CWPerturb
from attack import CrossEntropyAdvLoss, UntargetedLogitsAdvLoss
from attack import L2Dist
from attack import ClipPointsLinf
#AutoEncoder model
adv_func = UntargetedLogitsAdvLoss(kappa=30)
clip_func = ClipPointsLinf(budget=eps)
dist_func = L2Dist()
attacker = CWPerturb(model, adv_func, dist_func,
attack_lr=1e-2,
init_weight=10., max_weight=80.,
binary_step=2,
num_iter=500, clip_func=clip_func)
with torch.no_grad():
pc, label = pc.float().cuda(non_blocking=True), \
label.long().cuda(non_blocking=True)
# attack!
_, best_pc, success_num = attacker.attack(pc, label)
# print(best_pc.shape)
best_pc = torch.from_numpy(best_pc) #.to(torch.float32).contiguous().cuda()
best_pc = best_pc.transpose(2, 1)
return best_pc
def knn_attack(model, pc, label, eps=0.05):
# pc = pc.transpose(2, 1)
# import sys
# sys.path.append('/data/data1/jinlai.zhang/AOF/baselines/')
# from util.utils import str2bool, set_seed
from attack import CWUKNN
from attack import CrossEntropyAdvLoss, UntargetedLogitsAdvLoss
from attack import ChamferkNNDist
from attack import ProjectInnerClipLinf
adv_func = UntargetedLogitsAdvLoss(kappa=15)
dist_func = ChamferkNNDist(chamfer_method='adv2ori',
knn_k=5, knn_alpha=1.05,
chamfer_weight=5., knn_weight=3.)
clip_func = ProjectInnerClipLinf(budget=eps)
attacker = CWUKNN(model, adv_func, dist_func, clip_func,
attack_lr=1e-2,
num_iter=2500)
with torch.no_grad():
pc, label = pc.float().cuda(non_blocking=True), \
label.long().cuda(non_blocking=True)
# attack!
# print(pc.size())
# pc = pc.transpose(1, 2)
best_pc, success_num = attacker.attack(pc, label)
best_pc = torch.from_numpy(best_pc) #.to(torch.float32).contiguous().cuda()
# print(best_pc.size())
# best_pc = best_pc.transpose(2, 1).float()
return best_pc
def advpc_attack(model, pc, label, eps=0.05):
# pc = pc.transpose(2, 1)
# import sys
# sys.path.append('/data/data1/jinlai.zhang/AOF/baselines/')
# from util.utils import str2bool, set_seed
from attack import CrossEntropyAdvLoss, UntargetedLogitsAdvLoss
from attack import ChamferDist, L2Dist
from attack import ClipPointsLinf
from latent_3d_points.src import encoders_decoders
from attack import CWUAdvPC
#AutoEncoder model
ae_model = encoders_decoders.AutoEncoder(3)
ae_state_dict = torch.load("./latent_3d_points/src/logs/mn40/AE/2021-12-31 15:15:52_1024/BEST_model9800_CD_0.0038.pth")
print('Loading ae weight {}'.format("latent_3d_points/src/logs/mn40/AE/2021-12-31 15:15:52_1024/BEST_model9800_CD_0.0038.pth"))
try:
ae_model.load_state_dict(ae_state_dict)
except RuntimeError:
ae_state_dict = {k[7:]: v for k, v in ae_state_dict.items()}
ae_model.load_state_dict(ae_state_dict)
ae_model = ae_model.cuda()
adv_func = UntargetedLogitsAdvLoss(kappa=30)
clip_func = ClipPointsLinf(budget=eps)
dist_func = ChamferDist()
attacker = CWUAdvPC(model, ae_model, adv_func, dist_func,
attack_lr=1e-2,
binary_step=2,
num_iter=200, GAMMA=0.25,
clip_func=clip_func)
with torch.no_grad():
pc, label = pc.float().cuda(non_blocking=True), \
label.long().cuda(non_blocking=True)
# attack!
# print(pc.size())
# pc = pc.transpose(1, 2)
_, best_pc, success_num = attacker.attack(pc, label)
best_pc = torch.from_numpy(best_pc)
# print(best_pc.size())
# best_pc = best_pc.transpose(2, 1)
return best_pc
def pladv_validate(task, loader, model, dataset_name, pladv = 'aof', adapt = None, confusion = False, eps=0.05):
model.eval()
# print("here", step)
def get_extra_param():
return None
perf = PerfTrackVal(task, extra_param=get_extra_param())
time_dl = 0
time_gi = 0
time_model = 0
time_upd = 0
# with torch.no_grad():
bar = ProgressBar(max_value=len(loader))
time5 = time()
if confusion:
pred = []
ground = []
for i, data_batch in enumerate(loader):
time1 = time()
if pladv == 'aof':
data_batch['pc'] = aof_attack(model, data_batch['pc'], data_batch['label'], eps)
elif pladv == 'advpc':
data_batch['pc'] = advpc_attack(model,data_batch['pc'], data_batch['label'], eps)
elif pladv == 'knn':
data_batch['pc'] = knn_attack(model, data_batch['pc'], data_batch['label'], eps)
elif pladv == '3dadv':
data_batch['pc'] = tdadv_attack(model, data_batch['pc'], data_batch['label'], eps)
# data_batch = aug_utils.pgd(data_batch,model, task, loss_name, dataset_name, step= int(step), eps=0.05, alpha=0.005)
# data_batch['pc'] = data_batch['pc'].float()
# print(data_batch['pc'].type)
inp = get_inp(task, model, data_batch, loader.dataset.batch_proc, dataset_name)
time2 = time()
if adapt.METHOD == 'bn':
model = adapt_bn(inp,model,adapt)
elif adapt.METHOD == 'tent':
model = adapt_tent(inp,model,adapt)
#out = model(**inp)
add_target = get_target(task, model, data_batch, loader.dataset.batch_proc, dataset_name)
out = model(**inp,**add_target)
if confusion:
pred.append(out['logit'].squeeze().cpu())
ground.append(data_batch['label'].squeeze().cpu())
time3 = time()
perf.update(data_batch=data_batch, out=out)
time4 = time()
time_dl += (time1 - time5)
time_gi += (time2 - time1)
time_model += (time3 - time2)
time_upd += (time4 - time3)
time5 = time()
bar.update(i)
print(f"Time DL: {time_dl}, Time Get Inp: {time_gi}, Time Model: {time_model}, Time Update: {time_upd}")
if not confusion:
return perf.agg()
else:
pred = np.argmax(torch.cat(pred).numpy(), axis=1)
# print(pred)
ground = torch.cat(ground).numpy()
# print(ground)
return perf.agg(), pred, ground
def robust_validate(task, loader, model, loss_name, dataset_name, adapt = None, confusion = False, step=20, eps=0.05):
model.eval()
# print("here", step)
def get_extra_param():
return None
perf = PerfTrackVal(task, extra_param=get_extra_param())
time_dl = 0
time_gi = 0
time_model = 0
time_upd = 0
# with torch.no_grad():
alpha = eps/step
bar = ProgressBar(max_value=len(loader))
time5 = time()
if confusion:
pred = []
ground = []
for i, data_batch in enumerate(loader):
time1 = time()
#data_batch = aug_utils4.pgd(data_batch,model, task, 'cross_entropy', dataset_name, step= step, eps=eps, alpha=alpha)
data_batch = data_batch
# model.eval()
inp = get_inp(task, model, data_batch, loader.dataset.batch_proc, dataset_name)
time2 = time()
#if adapt.METHOD == 'bn':
# model = adapt_bn(inp,model,adapt)
#elif adapt.METHOD == 'tent':
# model = adapt_tent(inp,model,adapt)
#out = model(**inp)
add_target = get_target(task, model, data_batch, loader.dataset.batch_proc, dataset_name)
out = model(**inp, **add_target)
if confusion:
pred.append(out['logit'].squeeze().cpu())
ground.append(data_batch['label'].squeeze().cpu())
time3 = time()
perf.update(data_batch=data_batch, out=out)
time4 = time()
time_dl += (time1 - time5)
time_gi += (time2 - time1)
time_model += (time3 - time2)
time_upd += (time4 - time3)
time5 = time()
bar.update(i)
print(f"Time DL: {time_dl}, Time Get Inp: {time_gi}, Time Model: {time_model}, Time Update: {time_upd}")
#add
from functools import partial
def to_status(m, status):
if hasattr(m, 'batch_type'):
m.batch_type = status
to_clean_status = partial(to_status, status='clean')
#to_adv_status = partial(to_status, status='adv')
to_adv_hf_status = partial(to_status, status='adv_hf')
to_adv_lf_status = partial(to_status, status='adv_lf')
to_mix_status = partial(to_status, status='mix')
#end
#add jieduan22 renwu3.1.2
def test_corrupt(cfg, model, split):
loader_test = create_dataloadercd(split=split, cfg=cfg)
return validate(cfg.EXP.TASK, loader_test, model, dataset_name='modelnet40_dgcnn')
model.apply(to_clean_status)
#eval_corrupt_wrapper(model, test_corrupt, {'cfg': cfg})
cleanbn_OA_clean, cleanbn_mCE, cleanbn_RmCE, cleanbn_mOA = eval_corrupt_wrapper(model, test_corrupt, {'cfg': cfg})
model.apply(to_adv_hf_status)
#eval_corrupt_wrapper(model, test_corrupt, {'cfg': cfg})
hfbn_OA_clean, hfbn_mCE, hfbn_RmCE, hfbn_mOA = eval_corrupt_wrapper(model, test_corrupt, {'cfg': cfg})
model.apply(to_adv_lf_status)
#eval_corrupt_wrapper(model, test_corrupt, {'cfg': cfg})
lfbn_OA_clean, lfbn_mCE, lfbn_RmCE, lfbn_mOA = eval_corrupt_wrapper(model, test_corrupt, {'cfg': cfg})
#end jieduan22 renwu3.1.2
return cleanbn_OA_clean, cleanbn_mCE, cleanbn_RmCE, cleanbn_mOA, hfbn_OA_clean, hfbn_mCE, hfbn_RmCE, hfbn_mOA, lfbn_OA_clean, lfbn_mCE, lfbn_RmCE, lfbn_mOA
#if not confusion:
# return perf.agg()
#else:
# pred = np.argmax(torch.cat(pred).numpy(), axis=1)
# # print(pred)
# ground = torch.cat(ground).numpy()
# # print(ground)
# return perf.agg(), pred, ground
def validate(task, loader, model, dataset_name, adapt = None, confusion = False):
model.eval()
def get_extra_param():
return None
perf = PerfTrackVal(task, extra_param=get_extra_param())
time_dl = 0
time_gi = 0
time_model = 0
time_upd = 0
with torch.no_grad():
bar = ProgressBar(max_value=len(loader))
time5 = time()
if confusion:
pred = []
ground = []
for i, data_batch in enumerate(loader):
time1 = time()
inp = get_inp(task, model, data_batch, loader.dataset.batch_proc, dataset_name)
time2 = time()
#if adapt.METHOD == 'bn':
# model = adapt_bn(inp,model,adapt)
#elif adapt.METHOD == 'tent':
# model = adapt_tent(inp,model,adapt)
#out = model(**inp)
add_target = get_target(task, model, data_batch, loader.dataset.batch_proc, dataset_name)
out = model(**inp, **add_target)
if confusion:
pred.append(out['logit'].squeeze().cpu())
ground.append(data_batch['label'].squeeze().cpu())
time3 = time()
perf.update(data_batch=data_batch, out=out)
time4 = time()
time_dl += (time1 - time5)
time_gi += (time2 - time1)
time_model += (time3 - time2)
time_upd += (time4 - time3)
time5 = time()
bar.update(i)
print(f"Time DL: {time_dl}, Time Get Inp: {time_gi}, Time Model: {time_model}, Time Update: {time_upd}")
if not confusion:
return perf.agg()
else:
pred = np.argmax(torch.cat(pred).numpy(), axis=1)
# print(pred)
ground = torch.cat(ground).numpy()
# print(ground)
return perf.agg(), pred, ground
def train(task, loader, model, optimizer, loss_name, dataset_name, cfg):
model.train()
def get_extra_param():
return None
perf = PerfTrackTrain(task, extra_param=get_extra_param())
time_forward = 0
time_backward = 0
time_data_loading = 0
time3 = time()
norms = []
norm_avg = AverageMeter()
for i, data_batch in enumerate(loader):
time1 = time()
x_natural = data_batch.copy()
if cfg.AUG.NAME == 'cutmix_r':
data_batch = aug_utils.cutmix_r(data_batch,cfg)
elif cfg.AUG.NAME == 'cutmix_k':
data_batch = aug_utils.cutmix_k(data_batch,cfg)
elif cfg.AUG.NAME == 'mixup':
data_batch = aug_utils.mixup(data_batch,cfg)
elif cfg.AUG.NAME == 'rsmix':
data_batch = aug_utils.rsmix(data_batch,cfg)
#elif cfg.AUG.NAME == 'pgd':
# data_batch = aug_utils.pgd(data_batch,model, task, loss_name, dataset_name)
# model.train()
#elif cfg.AUG.NAME == 'negcd_pgd':
# data_batch = aug_utils.negcd_pgd(data_batch,model, task, loss_name, dataset_name)
#elif cfg.AUG.NAME == 'negknn_pgd':
# data_batch = aug_utils.negknn_pgd(data_batch,model, task, loss_name, dataset_name)
# model.train()
#elif cfg.AUG.NAME == 'adaep_new_pgd':
# data_batch = aug_utils.adaep_new_pgd(data_batch,model, task, loss_name, dataset_name)
# model.train()
#elif cfg.AUG.NAME == 'gat_pgd':
# data_batch = aug_utils.gat_pgd(data_batch,model, task, loss_name, dataset_name)
# model.train()
# print(data_batch)
inp = get_inp(task, model, data_batch, loader.dataset.batch_proc, dataset_name)
#add jieduan20 renwu3.3
add_target = get_target(task, model, data_batch, loader.dataset.batch_proc, dataset_name)
#out = model(**inp)
out = model(**inp, **add_target)
#print(out['logit'].shape)
#print(out['trans_feat'].shape)
#out['logit'] = out['logit'].transpose(1, 0).contiguous().view(-1, 40)
#out['trans_feat'] = out['trans_feat'].transpose(1, 0).contiguous().view(-1, 40)
#print(out['logit'].shape)
#print(out['trans_feat'].shape)
#out = model(**inp, add_target) #bug
#end jieduan20 renwu3.3
#data_batch['pc'] = torch.cat([data_batch['pc'], data_batch['pc']], dim=0)
#data_batch['label'] = torch.cat([data_batch['label'], data_batch['label']], dim=0)
x_copy = data_batch.copy()
data_batch['pc'] = torch.cat([data_batch['pc'], data_batch['pc']], dim=0)
data_batch['pc'] = torch.cat([data_batch['pc'], x_copy['pc']], dim=0)
data_batch['label'] = torch.cat([data_batch['label'], data_batch['label']], dim=0)
data_batch['label'] = torch.cat([data_batch['label'], x_copy['label']], dim=0)
if 'label_2' in data_batch.keys():
data_batch['label_2'] = torch.cat([data_batch['label_2'], data_batch['label_2']], dim=0)
data_batch['label_2'] = torch.cat([data_batch['label_2'], x_copy['label_2']], dim=0)
if 'lam' in data_batch.keys():
if isinstance(data_batch['lam'],torch.Tensor):
data_batch['lam'] = torch.cat([data_batch['lam'], data_batch['lam']], dim=0)
data_batch['lam'] = torch.cat([data_batch['lam'], x_copy['lam']], dim=0)
loss = get_loss(task, loss_name, data_batch, out, dataset_name)
perf.update_all(data_batch=data_batch, out=out, loss=loss)
time2 = time()
## robust loss
if cfg.ADVT.NAME == 'trades':
criterion_kl = nn.KLDivLoss(size_average=False)
batch_size = len(data_batch['pc'])
data_batch['pc'] = Variable(data_batch['pc'], requires_grad=False)
natural_inp = get_inp(task, model, x_natural, loader.dataset.batch_proc, dataset_name)
adv_inp = get_inp(task, model, data_batch, loader.dataset.batch_proc, dataset_name)
loss_robust = (1.0 / batch_size) * criterion_kl(F.log_softmax(model(**adv_inp)['logit'], dim=1),
F.softmax(model(**natural_inp)['logit'], dim=1))
loss = loss + loss_robust
elif cfg.ADVT.NAME == 'mart':
criterion_kl = nn.KLDivLoss(reduction='none')
batch_size = len(data_batch['pc'])
data_batch['pc'] = Variable(data_batch['pc'], requires_grad=False)
natural_inp = get_inp(task, model, x_natural, loader.dataset.batch_proc, dataset_name)
adv_inp = get_inp(task, model, data_batch, loader.dataset.batch_proc, dataset_name)
logits = model(**natural_inp)['logit']
logits_adv = model(**adv_inp)['logit']
y = data_batch['label'].to(logits.device)
adv_probs = F.softmax(logits_adv, dim=1)
tmp1 = torch.argsort(adv_probs, dim=1)[:, -2:]
new_y = torch.where(tmp1[:, -1] == y, tmp1[:, -2], tmp1[:, -1])
loss_adv = F.cross_entropy(logits_adv, y) + F.nll_loss(torch.log(1.0001 - adv_probs + 1e-12), new_y)
nat_probs = F.softmax(logits, dim=1)
true_probs = torch.gather(nat_probs, 1, (y.unsqueeze(1)).long()).squeeze()
loss_robust = (1.0 / batch_size) * torch.sum(torch.sum(criterion_kl(torch.log(adv_probs + 1e-12), nat_probs), dim=1) * (1.0000001 - true_probs))
# loss_robust = (1.0 / batch_size) * criterion_kl(F.log_softmax(model(**adv_inp)['logit'], dim=1),
# F.softmax(model(**natural_inp)['logit'], dim=1))
loss = loss + 6.0*loss_robust
if loss.ne(loss).any():
print("WARNING: avoiding step as nan in the loss")
else:
optimizer.zero_grad()
loss.backward()
norm = 0
for name, param in model.named_parameters():
if param.grad is not None:
norm += param.grad.norm(2).item() ** 2
# ori_shape = data_batch['pc'].size()
# grad = data_batch['pc'].grad.data
# grad_norm = torch.norm(grad.view(ori_shape[0], -1), dim = 1, p = 2)
# norms.append(grad_norm)
norm_avg.update(norm)
bad_grad = False
for x in model.parameters():
if x.grad is not None:
if x.grad.ne(x.grad).any():
print("WARNING: nan in a gradient")
bad_grad = True
break
if ((x.grad == float('inf')) | (x.grad == float('-inf'))).any():
print("WARNING: inf in a gradient")
bad_grad = True
break
if bad_grad:
print("WARNING: avoiding step as bad gradient")
else:
optimizer.step()
time_data_loading += (time1 - time3)
time_forward += (time2 - time1)
time3 = time()
time_backward += (time3 - time2)
if i % 50 == 0:
print(
f"[{i}/{len(loader)}] avg_loss: {perf.agg_loss()}, FW time = {round(time_forward, 2)}, "
f"BW time = {round(time_backward, 2)}, DL time = {round(time_data_loading, 2)}")
# concatenated_tensor = torch.cat(norms, dim=0)
# # Compute the mean along the first dimension to get a tensor of size (1, 32)
# mean_tensor = torch.mean(concatenated_tensor, dim=0)
# Print the mean tensor
# print(mean_tensor)
print('the grad norm is:',norm_avg.avg)
return perf.agg(), perf.agg_loss()
def save_checkpoint(id, epoch, model, optimizer, lr_sched, bnm_sched, test_perf, cfg):
model.cpu()
path = f"./runs/{cfg.EXP.EXP_ID}/model_{id}.pth"
torch.save({
'cfg': vars(cfg),
'epoch': epoch,
'model_state': model.state_dict(),
'optimizer_state': optimizer.state_dict(),
'lr_sched_state': lr_sched.state_dict(),
'bnm_sched_state': bnm_sched.state_dict() if bnm_sched is not None else None,
'test_perf': test_perf,
}, path)
print('Checkpoint saved to %s' % path)
model.to(DEVICE)
def load_best_checkpoint(model, cfg):
path = f"./runs/{cfg.EXP.EXP_ID}/model_best.pth"
checkpoint = torch.load(path)
model.load_state_dict(checkpoint['model_state'])
print('Checkpoint loaded from %s' % path)
def load_model_opt_sched(model, optimizer, lr_sched, bnm_sched, model_path):
print(f'Recovering model and checkpoint from {model_path}')
checkpoint = torch.load(model_path)
try:
model.load_state_dict(checkpoint['model_state'])
except:
if isinstance(model, nn.DataParallel):
model.module.load_state_dict(checkpoint['model_state'])
else:
model = nn.DataParallel(model)
model.load_state_dict(checkpoint['model_state'])
model = model.module
optimizer.load_state_dict(checkpoint['optimizer_state'])
# for backward compatibility with saved models
if 'lr_sched_state' in checkpoint:
lr_sched.load_state_dict(checkpoint['lr_sched_state'])
if checkpoint['bnm_sched_state'] is not None:
bnm_sched.load_state_dict(checkpoint['bnm_sched_state'])
else:
print("WARNING: lr scheduler and bnm scheduler states are not loaded.")
return model
def get_model(cfg):
if cfg.EXP.MODEL_NAME == 'simpleview':
model = models.MVModel(
task=cfg.EXP.TASK,
dataset=cfg.EXP.DATASET,
**cfg.MODEL.MV)
elif cfg.EXP.MODEL_NAME == 'rscnn':
model = models.RSCNN(
task=cfg.EXP.TASK,
dataset=cfg.EXP.DATASET,
**cfg.MODEL.RSCNN)
elif cfg.EXP.MODEL_NAME == 'pointnet2':
model = models.PointNet2(
task=cfg.EXP.TASK,
dataset=cfg.EXP.DATASET,
**cfg.MODEL.PN2)
elif cfg.EXP.MODEL_NAME == 'dgcnn':
model = models.DGCNN(
task=cfg.EXP.TASK,
dataset=cfg.EXP.DATASET)
elif cfg.EXP.MODEL_NAME == 'pointnet':
model = models.PointNet(
task=cfg.EXP.TASK,
dataset=cfg.EXP.DATASET)
elif cfg.EXP.MODEL_NAME == 'pct':
model = models.Pct(
task=cfg.EXP.TASK,
dataset=cfg.EXP.DATASET)
elif cfg.EXP.MODEL_NAME == 'pointMLP':
model = models.pointMLP(
task=cfg.EXP.TASK,
dataset=cfg.EXP.DATASET)
elif cfg.EXP.MODEL_NAME == 'pointMLP2':
model = models.pointMLP2(
task=cfg.EXP.TASK,
dataset=cfg.EXP.DATASET)
elif cfg.EXP.MODEL_NAME == 'curvenet':
model = models.CurveNet(
task=cfg.EXP.TASK,
dataset=cfg.EXP.DATASET)
elif cfg.EXP.MODEL_NAME == 'gdanet':
model = models.GDANET(
task=cfg.EXP.TASK,
dataset=cfg.EXP.DATASET)
else:
assert False
return model
def get_metric_from_perf(task, perf, metric_name):
if task in ['cls', 'cls_trans']:
assert metric_name in ['acc']
metric = perf[metric_name]
else:
assert False
return metric
def get_optimizer(optim_name, tr_arg, model):
if optim_name == 'vanilla':
optimizer = torch.optim.Adam(
model.parameters(),
lr=tr_arg.learning_rate,
weight_decay=tr_arg.l2)
lr_sched = lr_scheduler.ReduceLROnPlateau(
optimizer,
mode='min',
factor=tr_arg.lr_decay_factor,
patience=tr_arg.lr_reduce_patience,
verbose=True,
min_lr=tr_arg.lr_clip)
bnm_sched = None
elif optim_name == 'pct':
pass
optimizer = torch.optim.Adam(
model.parameters(),
lr=tr_arg.learning_rate,
weight_decay=tr_arg.l2)
lr_sched = lr_scheduler.CosineAnnealingLR(
optimizer,
tr_arg.num_epochs,
eta_min=tr_arg.learning_rate)
bnm_sched = None
else:
assert False
return optimizer, lr_sched, bnm_sched
def entry_train(cfg, resume=False, model_path=""):
loader_train = create_dataloader(split='train', cfg=cfg)
loader_valid = create_dataloader(split='valid', cfg=cfg)
loader_test = create_dataloader(split='test', cfg=cfg)
model = get_model(cfg)
model.to(DEVICE)
print(model)
if torch.cuda.device_count() > 1:
model = nn.DataParallel(model)
optimizer, lr_sched, bnm_sched = get_optimizer(cfg.EXP.OPTIMIZER, cfg.TRAIN, model)
if resume:
model = load_model_opt_sched(model, optimizer, lr_sched, bnm_sched, model_path)
else:
assert model_path == ""
log_dir = f"./runs/{cfg.EXP.EXP_ID}"
if not os.path.exists(log_dir):
os.makedirs(log_dir)
tb = TensorboardManager(log_dir)
track_train = TrackTrain(early_stop_patience=cfg.TRAIN.early_stop)
cleanbn_OA_clean = 0
cleanbn_mCE = 100
cleanbn_RmCE = 100
cleanbn_mOA = 0
hfbn_OA_clean = 0
hfbn_mCE = 100
hfbn_RmCE = 100
hfbn_mOA = 0
lfbn_OA_clean = 0
lfbn_mCE = 100
lfbn_RmCE = 100
lfbn_mOA = 0
cleanbn_OA_clean_epoch = 0
cleanbn_mCE_epoch = 0
cleanbn_RmCE_epoch = 0
cleanbn_mOA_epoch = 0
hfbn_OA_clean_epoch = 0
hfbn_mCE_epoch = 0
hfbn_RmCE_epoch = 0
hfbn_mOA_epoch = 0
lfbn_OA_clean_epoch = 0
lfbn_mCE_epoch = 0
lfbn_RmCE_epoch = 0
lfbn_mOA_epoch = 0
best_cleanbn_OA_clean = 0
best_cleanbn_mCE = 100
best_cleanbn_RmCE = 100
best_cleanbn_mOA = 0
best_hfbn_OA_clean = 0
best_hfbn_mCE = 100
best_hfbn_RmCE = 100
best_hfbn_mOA = 0
best_lfbn_OA_clean = 0
best_lfbn_mCE = 100
best_lfbn_RmCE = 100
best_lfbn_mOA = 0
best_cleanbn_OA_clean_epoch = 0
best_cleanbn_mCE_epoch = 0
best_cleanbn_RmCE_epoch = 0
best_cleanbn_mOA_epoch = 0
best_hfbn_OA_clean_epoch = 0
best_hfbn_mCE_epoch = 0
best_hfbn_RmCE_epoch = 0
best_hfbn_mOA_epoch = 0
best_lfbn_OA_clean_epoch = 0
best_lfbn_mCE_epoch = 0
best_lfbn_RmCE_epoch = 0
best_lfbn_mOA_epoch = 0
###############################################00099
###############################################00099
restricted00099_best_cleanbn_OA_clean = 0
restricted00099_best_cleanbn_mCE = 100
restricted00099_best_cleanbn_RmCE = 100
restricted00099_best_cleanbn_mOA = 0
restricted00099_best_hfbn_OA_clean = 0
restricted00099_best_hfbn_mCE = 100
restricted00099_best_hfbn_RmCE = 100
restricted00099_best_hfbn_mOA = 0
restricted00099_best_lfbn_OA_clean = 0
restricted00099_best_lfbn_mCE = 100
restricted00099_best_lfbn_RmCE = 100
restricted00099_best_lfbn_mOA = 0
restricted00099_best_cleanbn_OA_clean_epoch = 0
restricted00099_best_cleanbn_mCE_epoch = 0
restricted00099_best_cleanbn_RmCE_epoch = 0
restricted00099_best_cleanbn_mOA_epoch = 0
restricted00099_best_hfbn_OA_clean_epoch = 0
restricted00099_best_hfbn_mCE_epoch = 0
restricted00099_best_hfbn_RmCE_epoch = 0
restricted00099_best_hfbn_mOA_epoch = 0
restricted00099_best_lfbn_OA_clean_epoch = 0
restricted00099_best_lfbn_mCE_epoch = 0
restricted00099_best_lfbn_RmCE_epoch = 0
restricted00099_best_lfbn_mOA_epoch = 0
###############################################00099
###############################################00099
###############################################00049
###############################################00049
restricted00049_best_cleanbn_OA_clean = 0
restricted00049_best_cleanbn_mCE = 100
restricted00049_best_cleanbn_RmCE = 100
restricted00049_best_cleanbn_mOA = 0
restricted00049_best_hfbn_OA_clean = 0
restricted00049_best_hfbn_mCE = 100
restricted00049_best_hfbn_RmCE = 100
restricted00049_best_hfbn_mOA = 0
restricted00049_best_lfbn_OA_clean = 0
restricted00049_best_lfbn_mCE = 100
restricted00049_best_lfbn_RmCE = 100
restricted00049_best_lfbn_mOA = 0
restricted00049_best_cleanbn_OA_clean_epoch = 0
restricted00049_best_cleanbn_mCE_epoch = 0
restricted00049_best_cleanbn_RmCE_epoch = 0
restricted00049_best_cleanbn_mOA_epoch = 0
restricted00049_best_hfbn_OA_clean_epoch = 0
restricted00049_best_hfbn_mCE_epoch = 0
restricted00049_best_hfbn_RmCE_epoch = 0
restricted00049_best_hfbn_mOA_epoch = 0
restricted00049_best_lfbn_OA_clean_epoch = 0
restricted00049_best_lfbn_mCE_epoch = 0
restricted00049_best_lfbn_RmCE_epoch = 0
restricted00049_best_lfbn_mOA_epoch = 0
###############################################00049
###############################################00049
###############################################00149
###############################################00149
restricted00149_best_cleanbn_OA_clean = 0
restricted00149_best_cleanbn_mCE = 100
restricted00149_best_cleanbn_RmCE = 100
restricted00149_best_cleanbn_mOA = 0
restricted00149_best_hfbn_OA_clean = 0
restricted00149_best_hfbn_mCE = 100
restricted00149_best_hfbn_RmCE = 100
restricted00149_best_hfbn_mOA = 0
restricted00149_best_lfbn_OA_clean = 0
restricted00149_best_lfbn_mCE = 100
restricted00149_best_lfbn_RmCE = 100
restricted00149_best_lfbn_mOA = 0
restricted00149_best_cleanbn_OA_clean_epoch = 0
restricted00149_best_cleanbn_mCE_epoch = 0
restricted00149_best_cleanbn_RmCE_epoch = 0
restricted00149_best_cleanbn_mOA_epoch = 0
restricted00149_best_hfbn_OA_clean_epoch = 0
restricted00149_best_hfbn_mCE_epoch = 0
restricted00149_best_hfbn_RmCE_epoch = 0
restricted00149_best_hfbn_mOA_epoch = 0
restricted00149_best_lfbn_OA_clean_epoch = 0
restricted00149_best_lfbn_mCE_epoch = 0
restricted00149_best_lfbn_RmCE_epoch = 0
restricted00149_best_lfbn_mOA_epoch = 0
###############################################00149
###############################################00149
for epoch in range(cfg.TRAIN.num_epochs):
print(f'Epoch {epoch}')
print('Training..')
train_perf, train_loss = train(cfg.EXP.TASK, loader_train, model, optimizer, cfg.EXP.LOSS_NAME, cfg.EXP.DATASET, cfg)
pprint.pprint(train_perf, width=80)
tb.update('train', epoch, train_perf)
if (not cfg.EXP_EXTRA.no_val) and epoch % cfg.EXP_EXTRA.val_eval_freq == 0:
print('\nValidating..')
val_perf = validate(cfg.EXP.TASK, loader_valid, model, cfg.EXP.DATASET, cfg.ADAPT)
pprint.pprint(val_perf, width=80)
tb.update('val', epoch, val_perf)
else:
val_perf = defaultdict(float)
if (not cfg.EXP_EXTRA.no_test) and (epoch % cfg.EXP_EXTRA.test_eval_freq == 0):
print('\nTesting..')
test_perf = validate(cfg.EXP.TASK, loader_test, model, cfg.EXP.DATASET, cfg.ADAPT)
pprint.pprint(test_perf, width=80)
tb.update('test', epoch, test_perf)
else:
test_perf = defaultdict(float)
#if cfg.EXP_EXTRA.robust_test:
# print('\nRobust Testing..')
# #test_perf = robust_validate(cfg.EXP.TASK, loader_test, model, cfg.EXP.LOSS_NAME, cfg.EXP.DATASET, cfg.ADAPT, step=cmd_args.pgd_step)
# #pprint.pprint(test_perf, width=80)
# #tb.update('test', epoch, test_perf)
# cleanbn_OA_clean, cleanbn_mCE, cleanbn_RmCE, cleanbn_mOA, hfbn_OA_clean, hfbn_mCE, hfbn_RmCE, hfbn_mOA, lfbn_OA_clean, lfbn_mCE, lfbn_RmCE, lfbn_mOA = robust_validate(cfg.EXP.TASK, loader_test, model, cfg.EXP.LOSS_NAME, cfg.EXP.DATASET, cfg.ADAPT, step=cmd_args.pgd_step)
#if ((epoch % 3 == 0) and (epoch >= 40)) or epoch == 0:
if ((epoch % 2 == 0) and (epoch >= 1)) or epoch == 0:
if cfg.EXP_EXTRA.robust_test:
print('\nRobust Testing..')
cleanbn_OA_clean, cleanbn_mCE, cleanbn_RmCE, cleanbn_mOA, hfbn_OA_clean, hfbn_mCE, hfbn_RmCE, hfbn_mOA, lfbn_OA_clean, lfbn_mCE, lfbn_RmCE, lfbn_mOA = robust_validate(cfg.EXP.TASK, loader_test, model, cfg.EXP.LOSS_NAME, cfg.EXP.DATASET, cfg.ADAPT, step=cmd_args.pgd_step)
path_best = f"./runs/{cfg.EXP.EXP_ID}/"
#with open(os.path.join(path_best, 'best_cleanbn_OA_clean_epoch.txt'), 'at') as f:
# f.write('best_cleanbn_OA_clean_epoch {}\n''best_cleanbn_OA_clean {}\n'.format(best_cleanbn_OA_clean_epoch, best_cleanbn_OA_clean))
with open(os.path.join(path_best, 'cleanbn_OA_clean_epoch.txt'), 'at') as f:
f.write('cleanbn_OA_clean_epoch {}\n''cleanbn_OA_clean {}\n'.format(epoch, cleanbn_OA_clean))
with open(os.path.join(path_best, 'cleanbn_mCE_epoch.txt'), 'at') as f:
f.write('cleanbn_mCE_epoch {}\n''cleanbn_mCE {}\n'.format(epoch, cleanbn_mCE))
with open(os.path.join(path_best, 'cleanbn_RmCE_epoch.txt'), 'at') as f:
f.write('cleanbn_RmCE_epoch {}\n''cleanbn_RmCE {}\n'.format(epoch, cleanbn_RmCE))
with open(os.path.join(path_best, 'cleanbn_mOA_epoch.txt'), 'at') as f:
f.write('cleanbn_mOA_epoch {}\n''cleanbn_mOA {}\n'.format(epoch, cleanbn_mOA))
with open(os.path.join(path_best, 'hfbn_OA_clean_epoch.txt'), 'at') as f:
f.write('hfbn_OA_clean_epoch {}\n''hfbn_OA_clean {}\n'.format(epoch, hfbn_OA_clean))
with open(os.path.join(path_best, 'hfbn_mCE_epoch.txt'), 'at') as f:
f.write('hfbn_mCE_epoch {}\n''hfbn_mCE {}\n'.format(epoch, hfbn_mCE))
with open(os.path.join(path_best, 'hfbn_RmCE_epoch.txt'), 'at') as f:
f.write('hfbn_RmCE_epoch {}\n''hfbn_RmCE {}\n'.format(epoch, hfbn_RmCE))
with open(os.path.join(path_best, 'hfbn_mOA_epoch.txt'), 'at') as f:
f.write('hfbn_mOA_epoch {}\n''hfbn_mOA {}\n'.format(epoch, hfbn_mOA))
with open(os.path.join(path_best, 'lfbn_OA_clean_epoch.txt'), 'at') as f:
f.write('lfbn_OA_clean_epoch {}\n''lfbn_OA_clean {}\n'.format(epoch, lfbn_OA_clean))
with open(os.path.join(path_best, 'lfbn_mCE_epoch.txt'), 'at') as f:
f.write('lfbn_mCE_epoch {}\n''lfbn_mCE {}\n'.format(epoch, lfbn_mCE))
with open(os.path.join(path_best, 'lfbn_RmCE_epoch.txt'), 'at') as f:
f.write('lfbn_RmCE_epoch {}\n''lfbn_RmCE {}\n'.format(epoch, lfbn_RmCE))
with open(os.path.join(path_best, 'lfbn_mOA_epoch.txt'), 'at') as f:
f.write('lfbn_mOA_epoch {}\n''lfbn_mOA {}\n'.format(epoch, lfbn_mOA))
if cleanbn_OA_clean > best_cleanbn_OA_clean or (cleanbn_OA_clean == best_cleanbn_OA_clean and cleanbn_mCE < best_cleanbn_mCE):
best_cleanbn_OA_clean = cleanbn_OA_clean
best_cleanbn_OA_clean_epoch = epoch
save_checkpoint('best_cleanbn_OA_clean', epoch, model, optimizer, lr_sched, bnm_sched, test_perf, cfg)
with open(os.path.join(path_best, 'best_cleanbn_OA_clean_epoch.txt'), 'at') as f:
f.write('best_cleanbn_OA_clean_epoch {}\n''best_cleanbn_OA_clean {}\n'.format(best_cleanbn_OA_clean_epoch, best_cleanbn_OA_clean))
if cleanbn_mCE < best_cleanbn_mCE or (cleanbn_mCE == best_cleanbn_mCE and cleanbn_OA_clean > best_cleanbn_OA_clean):
best_cleanbn_mCE = cleanbn_mCE
best_cleanbn_mCE_epoch = epoch
save_checkpoint('best_cleanbn_mCE', epoch, model, optimizer, lr_sched, bnm_sched, test_perf, cfg)
with open(os.path.join(path_best, 'best_cleanbn_mCE_epoch.txt'), 'at') as f:
f.write('best_cleanbn_mCE_epoch {}\n''best_cleanbn_mCE {}\n'.format(best_cleanbn_mCE_epoch, best_cleanbn_mCE))
if cleanbn_RmCE < best_cleanbn_RmCE or (cleanbn_RmCE == best_cleanbn_RmCE and cleanbn_OA_clean > best_cleanbn_OA_clean):
best_cleanbn_RmCE = cleanbn_RmCE
best_cleanbn_RmCE_epoch = epoch
save_checkpoint('best_cleanbn_RmCE', epoch, model, optimizer, lr_sched, bnm_sched, test_perf, cfg)
with open(os.path.join(path_best, 'best_cleanbn_RmCE_epoch.txt'), 'at') as f:
f.write('best_cleanbn_RmCE_epoch {}\n''best_cleanbn_RmCE {}\n'.format(best_cleanbn_RmCE_epoch, best_cleanbn_RmCE))
if cleanbn_mOA > best_cleanbn_mOA or (cleanbn_mOA == best_cleanbn_mOA and cleanbn_OA_clean > best_cleanbn_OA_clean):
best_cleanbn_mOA = cleanbn_mOA
best_cleanbn_mOA_epoch = epoch
save_checkpoint('best_cleanbn_mOA', epoch, model, optimizer, lr_sched, bnm_sched, test_perf, cfg)
with open(os.path.join(path_best, 'best_cleanbn_mOA_epoch.txt'), 'at') as f:
f.write('best_cleanbn_mOA_epoch {}\n''best_cleanbn_mOA {}\n'.format(best_cleanbn_mOA_epoch, best_cleanbn_mOA))
if hfbn_OA_clean > best_hfbn_OA_clean or (hfbn_OA_clean == best_hfbn_OA_clean and hfbn_mCE < best_hfbn_mCE):
best_hfbn_OA_clean = hfbn_OA_clean
best_hfbn_OA_clean_epoch = epoch
save_checkpoint('best_hfbn_OA_clean', epoch, model, optimizer, lr_sched, bnm_sched, test_perf, cfg)
with open(os.path.join(path_best, 'best_hfbn_OA_clean_epoch.txt'), 'at') as f:
f.write('best_hfbn_OA_clean_epoch {}\n''best_hfbn_OA_clean {}\n'.format(best_hfbn_OA_clean_epoch, best_hfbn_OA_clean))
if hfbn_mCE < best_hfbn_mCE or (hfbn_mCE == best_hfbn_mCE and hfbn_OA_clean > best_hfbn_OA_clean):
best_hfbn_mCE = hfbn_mCE
best_hfbn_mCE_epoch = epoch
save_checkpoint('best_hfbn_mCE', epoch, model, optimizer, lr_sched, bnm_sched, test_perf, cfg)
with open(os.path.join(path_best, 'best_hfbn_mCE_epoch.txt'), 'at') as f:
f.write('best_hfbn_mCE_epoch {}\n''best_hfbn_mCE {}\n'.format(best_hfbn_mCE_epoch, best_hfbn_mCE))
if hfbn_RmCE < best_hfbn_RmCE or (hfbn_RmCE == best_hfbn_RmCE and hfbn_OA_clean > best_hfbn_OA_clean):
best_hfbn_RmCE = hfbn_RmCE
best_hfbn_RmCE_epoch = epoch
save_checkpoint('best_hfbn_RmCE', epoch, model, optimizer, lr_sched, bnm_sched, test_perf, cfg)
with open(os.path.join(path_best, 'best_hfbn_RmCE_epoch.txt'), 'at') as f:
f.write('best_hfbn_RmCE_epoch {}\n''best_hfbn_RmCE {}\n'.format(best_hfbn_RmCE_epoch, best_hfbn_RmCE))
if hfbn_mOA > best_hfbn_mOA or (hfbn_mOA == best_hfbn_mOA and hfbn_OA_clean > best_hfbn_OA_clean):
best_hfbn_mOA = hfbn_mOA
best_hfbn_mOA_epoch = epoch
save_checkpoint('best_hfbn_mOA', epoch, model, optimizer, lr_sched, bnm_sched, test_perf, cfg)
with open(os.path.join(path_best, 'best_hfbn_mOA_epoch.txt'), 'at') as f:
f.write('best_hfbn_mOA_epoch {}\n''best_hfbn_mOA {}\n'.format(best_hfbn_mOA_epoch, best_hfbn_mOA))
if lfbn_OA_clean > best_lfbn_OA_clean or (lfbn_OA_clean == best_lfbn_OA_clean and lfbn_mCE < best_lfbn_mCE):
best_lfbn_OA_clean = lfbn_OA_clean
best_lfbn_OA_clean_epoch = epoch
save_checkpoint('best_lfbn_OA_clean', epoch, model, optimizer, lr_sched, bnm_sched, test_perf, cfg)
with open(os.path.join(path_best, 'best_lfbn_OA_clean_epoch.txt'), 'at') as f:
f.write('best_lfbn_OA_clean_epoch {}\n''best_lfbn_OA_clean {}\n'.format(best_lfbn_OA_clean_epoch, best_lfbn_OA_clean))
if lfbn_mCE < best_lfbn_mCE or (lfbn_mCE == best_lfbn_mCE and lfbn_OA_clean > best_lfbn_OA_clean):
best_lfbn_mCE = lfbn_mCE
best_lfbn_mCE_epoch = epoch
save_checkpoint('best_lfbn_mCE', epoch, model, optimizer, lr_sched, bnm_sched, test_perf, cfg)
with open(os.path.join(path_best, 'best_lfbn_mCE_epoch.txt'), 'at') as f:
f.write('best_lfbn_mCE_epoch {}\n''best_lfbn_mCE {}\n'.format(best_lfbn_mCE_epoch, best_lfbn_mCE))
if lfbn_RmCE < best_lfbn_RmCE or (lfbn_RmCE == best_lfbn_RmCE and lfbn_OA_clean > best_lfbn_OA_clean):
best_lfbn_RmCE = lfbn_RmCE
best_lfbn_RmCE_epoch = epoch
save_checkpoint('best_lfbn_RmCE', epoch, model, optimizer, lr_sched, bnm_sched, test_perf, cfg)
with open(os.path.join(path_best, 'best_lfbn_RmCE_epoch.txt'), 'at') as f:
f.write('best_lfbn_RmCE_epoch {}\n''best_lfbn_RmCE {}\n'.format(best_lfbn_RmCE_epoch, best_lfbn_RmCE))
if lfbn_mOA > best_lfbn_mOA or (lfbn_mOA == best_lfbn_mOA and lfbn_OA_clean > best_lfbn_OA_clean):
best_lfbn_mOA = lfbn_mOA
best_lfbn_mOA_epoch = epoch
save_checkpoint('best_lfbn_mOA', epoch, model, optimizer, lr_sched, bnm_sched, test_perf, cfg)
with open(os.path.join(path_best, 'best_lfbn_mOA_epoch.txt'), 'at') as f:
f.write('best_lfbn_mOA_epoch {}\n''best_lfbn_mOA {}\n'.format(best_lfbn_mOA_epoch, best_lfbn_mOA))
###############################################00099
###############################################00099
if cleanbn_mCE < (best_cleanbn_mCE+0.0099) and (cleanbn_OA_clean > restricted00099_best_cleanbn_OA_clean or (cleanbn_OA_clean == restricted00099_best_cleanbn_OA_clean and cleanbn_mCE < restricted00099_best_cleanbn_mCE)):
restricted00099_best_cleanbn_OA_clean = cleanbn_OA_clean
restricted00099_best_cleanbn_OA_clean_epoch = epoch
save_checkpoint('restricted00099_best_cleanbn_OA_clean', epoch, model, optimizer, lr_sched, bnm_sched, test_perf, cfg)
with open(os.path.join(path_best, 'restricted00099_best_cleanbn_OA_clean_epoch.txt'), 'at') as f:
f.write('restricted00099_best_cleanbn_OA_clean_epoch {}\n''restricted00099_best_cleanbn_OA_clean {}\n'.format(restricted00099_best_cleanbn_OA_clean_epoch, restricted00099_best_cleanbn_OA_clean))
if cleanbn_OA_clean > (best_cleanbn_OA_clean-0.0099) and (cleanbn_mCE < restricted00099_best_cleanbn_mCE or (cleanbn_mCE == restricted00099_best_cleanbn_mCE and cleanbn_OA_clean > restricted00099_best_cleanbn_OA_clean)):
restricted00099_best_cleanbn_mCE = cleanbn_mCE
restricted00099_best_cleanbn_mCE_epoch = epoch
save_checkpoint('restricted00099_best_cleanbn_mCE', epoch, model, optimizer, lr_sched, bnm_sched, test_perf, cfg)
with open(os.path.join(path_best, 'restricted00099_best_cleanbn_mCE_epoch.txt'), 'at') as f:
f.write('restricted00099_best_cleanbn_mCE_epoch {}\n''restricted00099_best_cleanbn_mCE {}\n'.format(restricted00099_best_cleanbn_mCE_epoch, restricted00099_best_cleanbn_mCE))
if cleanbn_OA_clean > (best_cleanbn_OA_clean-0.0099) and (cleanbn_RmCE < restricted00099_best_cleanbn_RmCE or (cleanbn_RmCE == restricted00099_best_cleanbn_RmCE and cleanbn_OA_clean > restricted00099_best_cleanbn_OA_clean)):
restricted00099_best_cleanbn_RmCE = cleanbn_RmCE
restricted00099_best_cleanbn_RmCE_epoch = epoch
save_checkpoint('restricted00099_best_cleanbn_RmCE', epoch, model, optimizer, lr_sched, bnm_sched, test_perf, cfg)
with open(os.path.join(path_best, 'restricted00099_best_cleanbn_RmCE_epoch.txt'), 'at') as f:
f.write('restricted00099_best_cleanbn_RmCE_epoch {}\n''restricted00099_best_cleanbn_RmCE {}\n'.format(restricted00099_best_cleanbn_RmCE_epoch, restricted00099_best_cleanbn_RmCE))
if cleanbn_OA_clean > (best_cleanbn_OA_clean-0.0099) and (cleanbn_mOA > restricted00099_best_cleanbn_mOA or (cleanbn_mOA == restricted00099_best_cleanbn_mOA and cleanbn_OA_clean > restricted00099_best_cleanbn_OA_clean)):
restricted00099_best_cleanbn_mOA = cleanbn_mOA
restricted00099_best_cleanbn_mOA_epoch = epoch
save_checkpoint('restricted00099_best_cleanbn_mOA', epoch, model, optimizer, lr_sched, bnm_sched, test_perf, cfg)
with open(os.path.join(path_best, 'restricted00099_best_cleanbn_mOA_epoch.txt'), 'at') as f:
f.write('restricted00099_best_cleanbn_mOA_epoch {}\n''restricted00099_best_cleanbn_mOA {}\n'.format(restricted00099_best_cleanbn_mOA_epoch, restricted00099_best_cleanbn_mOA))
if hfbn_mCE < (best_hfbn_mCE+0.0099) and (hfbn_OA_clean > restricted00099_best_hfbn_OA_clean or (hfbn_OA_clean == restricted00099_best_hfbn_OA_clean and hfbn_mCE < restricted00099_best_hfbn_mCE)):
restricted00099_best_hfbn_OA_clean = hfbn_OA_clean
restricted00099_best_hfbn_OA_clean_epoch = epoch
save_checkpoint('restricted00099_best_hfbn_OA_clean', epoch, model, optimizer, lr_sched, bnm_sched, test_perf, cfg)
with open(os.path.join(path_best, 'restricted00099_best_hfbn_OA_clean_epoch.txt'), 'at') as f:
f.write('restricted00099_best_hfbn_OA_clean_epoch {}\n''restricted00099_best_hfbn_OA_clean {}\n'.format(restricted00099_best_hfbn_OA_clean_epoch, restricted00099_best_hfbn_OA_clean))
if hfbn_OA_clean > (best_hfbn_OA_clean-0.0099) and (hfbn_mCE < restricted00099_best_hfbn_mCE or (hfbn_mCE == restricted00099_best_hfbn_mCE and hfbn_OA_clean > restricted00099_best_hfbn_OA_clean)):
restricted00099_best_hfbn_mCE = hfbn_mCE
restricted00099_best_hfbn_mCE_epoch = epoch
save_checkpoint('restricted00099_best_hfbn_mCE', epoch, model, optimizer, lr_sched, bnm_sched, test_perf, cfg)
with open(os.path.join(path_best, 'restricted00099_best_hfbn_mCE_epoch.txt'), 'at') as f:
f.write('restricted00099_best_hfbn_mCE_epoch {}\n''restricted00099_best_hfbn_mCE {}\n'.format(restricted00099_best_hfbn_mCE_epoch, restricted00099_best_hfbn_mCE))
if hfbn_OA_clean > (best_hfbn_OA_clean-0.0099) and (hfbn_RmCE < restricted00099_best_hfbn_RmCE or (hfbn_RmCE == restricted00099_best_hfbn_RmCE and hfbn_OA_clean > restricted00099_best_hfbn_OA_clean)):
restricted00099_best_hfbn_RmCE = hfbn_RmCE
restricted00099_best_hfbn_RmCE_epoch = epoch
save_checkpoint('restricted00099_best_hfbn_RmCE', epoch, model, optimizer, lr_sched, bnm_sched, test_perf, cfg)
with open(os.path.join(path_best, 'restricted00099_best_hfbn_RmCE_epoch.txt'), 'at') as f:
f.write('restricted00099_best_hfbn_RmCE_epoch {}\n''restricted00099_best_hfbn_RmCE {}\n'.format(restricted00099_best_hfbn_RmCE_epoch, restricted00099_best_hfbn_RmCE))
if hfbn_OA_clean > (best_hfbn_OA_clean-0.0099) and (hfbn_mOA > restricted00099_best_hfbn_mOA or (hfbn_mOA == restricted00099_best_hfbn_mOA and hfbn_OA_clean > restricted00099_best_hfbn_OA_clean)):
restricted00099_best_hfbn_mOA = hfbn_mOA
restricted00099_best_hfbn_mOA_epoch = epoch
save_checkpoint('restricted00099_best_hfbn_mOA', epoch, model, optimizer, lr_sched, bnm_sched, test_perf, cfg)
with open(os.path.join(path_best, 'restricted00099_best_hfbn_mOA_epoch.txt'), 'at') as f:
f.write('restricted00099_best_hfbn_mOA_epoch {}\n''restricted00099_best_hfbn_mOA {}\n'.format(restricted00099_best_hfbn_mOA_epoch, restricted00099_best_hfbn_mOA))
if lfbn_mCE < (best_lfbn_mCE+0.0099) and (lfbn_OA_clean > restricted00099_best_lfbn_OA_clean or (lfbn_OA_clean == restricted00099_best_lfbn_OA_clean and lfbn_mCE < restricted00099_best_lfbn_mCE)):
restricted00099_best_lfbn_OA_clean = lfbn_OA_clean
restricted00099_best_lfbn_OA_clean_epoch = epoch
save_checkpoint('restricted00099_best_lfbn_OA_clean', epoch, model, optimizer, lr_sched, bnm_sched, test_perf, cfg)
with open(os.path.join(path_best, 'restricted00099_best_lfbn_OA_clean_epoch.txt'), 'at') as f:
f.write('restricted00099_best_lfbn_OA_clean_epoch {}\n''restricted00099_best_lfbn_OA_clean {}\n'.format(restricted00099_best_lfbn_OA_clean_epoch, restricted00099_best_lfbn_OA_clean))
if lfbn_OA_clean > (best_lfbn_OA_clean-0.0099) and (lfbn_mCE < restricted00099_best_lfbn_mCE or (lfbn_mCE == restricted00099_best_lfbn_mCE and lfbn_OA_clean > restricted00099_best_lfbn_OA_clean)):
restricted00099_best_lfbn_mCE = lfbn_mCE
restricted00099_best_lfbn_mCE_epoch = epoch
save_checkpoint('restricted00099_best_lfbn_mCE', epoch, model, optimizer, lr_sched, bnm_sched, test_perf, cfg)
with open(os.path.join(path_best, 'restricted00099_best_lfbn_mCE_epoch.txt'), 'at') as f:
f.write('restricted00099_best_lfbn_mCE_epoch {}\n''restricted00099_best_lfbn_mCE {}\n'.format(restricted00099_best_lfbn_mCE_epoch, restricted00099_best_lfbn_mCE))
if lfbn_OA_clean > (best_lfbn_OA_clean-0.0099) and (lfbn_RmCE < restricted00099_best_lfbn_RmCE or (lfbn_RmCE == restricted00099_best_lfbn_RmCE and lfbn_OA_clean > restricted00099_best_lfbn_OA_clean)):
restricted00099_best_lfbn_RmCE = lfbn_RmCE
restricted00099_best_lfbn_RmCE_epoch = epoch
save_checkpoint('restricted00099_best_lfbn_RmCE', epoch, model, optimizer, lr_sched, bnm_sched, test_perf, cfg)
with open(os.path.join(path_best, 'restricted00099_best_lfbn_RmCE_epoch.txt'), 'at') as f:
f.write('restricted00099_best_lfbn_RmCE_epoch {}\n''restricted00099_best_lfbn_RmCE {}\n'.format(restricted00099_best_lfbn_RmCE_epoch, restricted00099_best_lfbn_RmCE))
if lfbn_OA_clean > (best_lfbn_OA_clean-0.0099) and (lfbn_mOA > restricted00099_best_lfbn_mOA or (lfbn_mOA == restricted00099_best_lfbn_mOA and lfbn_OA_clean > restricted00099_best_lfbn_OA_clean)):
restricted00099_best_lfbn_mOA = lfbn_mOA
restricted00099_best_lfbn_mOA_epoch = epoch
save_checkpoint('restricted00099_best_lfbn_mOA', epoch, model, optimizer, lr_sched, bnm_sched, test_perf, cfg)
with open(os.path.join(path_best, 'restricted00099_best_lfbn_mOA_epoch.txt'), 'at') as f:
f.write('restricted00099_best_lfbn_mOA_epoch {}\n''restricted00099_best_lfbn_mOA {}\n'.format(restricted00099_best_lfbn_mOA_epoch, restricted00099_best_lfbn_mOA))
###############################################00099
###############################################00099
###############################################00049
###############################################00049
if cleanbn_mCE < (best_cleanbn_mCE+0.0049) and (cleanbn_OA_clean > restricted00049_best_cleanbn_OA_clean or (cleanbn_OA_clean == restricted00049_best_cleanbn_OA_clean and cleanbn_mCE < restricted00049_best_cleanbn_mCE)):
restricted00049_best_cleanbn_OA_clean = cleanbn_OA_clean
restricted00049_best_cleanbn_OA_clean_epoch = epoch
save_checkpoint('restricted00049_best_cleanbn_OA_clean', epoch, model, optimizer, lr_sched, bnm_sched, test_perf, cfg)
with open(os.path.join(path_best, 'restricted00049_best_cleanbn_OA_clean_epoch.txt'), 'at') as f:
f.write('restricted00049_best_cleanbn_OA_clean_epoch {}\n''restricted00049_best_cleanbn_OA_clean {}\n'.format(restricted00049_best_cleanbn_OA_clean_epoch, restricted00049_best_cleanbn_OA_clean))
if cleanbn_OA_clean > (best_cleanbn_OA_clean-0.0049) and (cleanbn_mCE < restricted00049_best_cleanbn_mCE or (cleanbn_mCE == restricted00049_best_cleanbn_mCE and cleanbn_OA_clean > restricted00049_best_cleanbn_OA_clean)):
restricted00049_best_cleanbn_mCE = cleanbn_mCE
restricted00049_best_cleanbn_mCE_epoch = epoch
save_checkpoint('restricted00049_best_cleanbn_mCE', epoch, model, optimizer, lr_sched, bnm_sched, test_perf, cfg)
with open(os.path.join(path_best, 'restricted00049_best_cleanbn_mCE_epoch.txt'), 'at') as f:
f.write('restricted00049_best_cleanbn_mCE_epoch {}\n''restricted00049_best_cleanbn_mCE {}\n'.format(restricted00049_best_cleanbn_mCE_epoch, restricted00049_best_cleanbn_mCE))
if cleanbn_OA_clean > (best_cleanbn_OA_clean-0.0049) and (cleanbn_RmCE < restricted00049_best_cleanbn_RmCE or (cleanbn_RmCE == restricted00049_best_cleanbn_RmCE and cleanbn_OA_clean > restricted00049_best_cleanbn_OA_clean)):
restricted00049_best_cleanbn_RmCE = cleanbn_RmCE
restricted00049_best_cleanbn_RmCE_epoch = epoch
save_checkpoint('restricted00049_best_cleanbn_RmCE', epoch, model, optimizer, lr_sched, bnm_sched, test_perf, cfg)
with open(os.path.join(path_best, 'restricted00049_best_cleanbn_RmCE_epoch.txt'), 'at') as f:
f.write('restricted00049_best_cleanbn_RmCE_epoch {}\n''restricted00049_best_cleanbn_RmCE {}\n'.format(restricted00049_best_cleanbn_RmCE_epoch, restricted00049_best_cleanbn_RmCE))
if cleanbn_OA_clean > (best_cleanbn_OA_clean-0.0049) and (cleanbn_mOA > restricted00049_best_cleanbn_mOA or (cleanbn_mOA == restricted00049_best_cleanbn_mOA and cleanbn_OA_clean > restricted00049_best_cleanbn_OA_clean)):
restricted00049_best_cleanbn_mOA = cleanbn_mOA
restricted00049_best_cleanbn_mOA_epoch = epoch
save_checkpoint('restricted00049_best_cleanbn_mOA', epoch, model, optimizer, lr_sched, bnm_sched, test_perf, cfg)
with open(os.path.join(path_best, 'restricted00049_best_cleanbn_mOA_epoch.txt'), 'at') as f:
f.write('restricted00049_best_cleanbn_mOA_epoch {}\n''restricted00049_best_cleanbn_mOA {}\n'.format(restricted00049_best_cleanbn_mOA_epoch, restricted00049_best_cleanbn_mOA))
if hfbn_mCE < (best_hfbn_mCE+0.0049) and (hfbn_OA_clean > restricted00049_best_hfbn_OA_clean or (hfbn_OA_clean == restricted00049_best_hfbn_OA_clean and hfbn_mCE < restricted00049_best_hfbn_mCE)):
restricted00049_best_hfbn_OA_clean = hfbn_OA_clean
restricted00049_best_hfbn_OA_clean_epoch = epoch
save_checkpoint('restricted00049_best_hfbn_OA_clean', epoch, model, optimizer, lr_sched, bnm_sched, test_perf, cfg)
with open(os.path.join(path_best, 'restricted00049_best_hfbn_OA_clean_epoch.txt'), 'at') as f:
f.write('restricted00049_best_hfbn_OA_clean_epoch {}\n''restricted00049_best_hfbn_OA_clean {}\n'.format(restricted00049_best_hfbn_OA_clean_epoch, restricted00049_best_hfbn_OA_clean))
if hfbn_OA_clean > (best_hfbn_OA_clean-0.0049) and (hfbn_mCE < restricted00049_best_hfbn_mCE or (hfbn_mCE == restricted00049_best_hfbn_mCE and hfbn_OA_clean > restricted00049_best_hfbn_OA_clean)):
restricted00049_best_hfbn_mCE = hfbn_mCE
restricted00049_best_hfbn_mCE_epoch = epoch
save_checkpoint('restricted00049_best_hfbn_mCE', epoch, model, optimizer, lr_sched, bnm_sched, test_perf, cfg)
with open(os.path.join(path_best, 'restricted00049_best_hfbn_mCE_epoch.txt'), 'at') as f:
f.write('restricted00049_best_hfbn_mCE_epoch {}\n''restricted00049_best_hfbn_mCE {}\n'.format(restricted00049_best_hfbn_mCE_epoch, restricted00049_best_hfbn_mCE))
if hfbn_OA_clean > (best_hfbn_OA_clean-0.0049) and (hfbn_RmCE < restricted00049_best_hfbn_RmCE or (hfbn_RmCE == restricted00049_best_hfbn_RmCE and hfbn_OA_clean > restricted00049_best_hfbn_OA_clean)):
restricted00049_best_hfbn_RmCE = hfbn_RmCE
restricted00049_best_hfbn_RmCE_epoch = epoch
save_checkpoint('restricted00049_best_hfbn_RmCE', epoch, model, optimizer, lr_sched, bnm_sched, test_perf, cfg)
with open(os.path.join(path_best, 'restricted00049_best_hfbn_RmCE_epoch.txt'), 'at') as f:
f.write('restricted00049_best_hfbn_RmCE_epoch {}\n''restricted00049_best_hfbn_RmCE {}\n'.format(restricted00049_best_hfbn_RmCE_epoch, restricted00049_best_hfbn_RmCE))
if hfbn_OA_clean > (best_hfbn_OA_clean-0.0049) and (hfbn_mOA > restricted00049_best_hfbn_mOA or (hfbn_mOA == restricted00049_best_hfbn_mOA and hfbn_OA_clean > restricted00049_best_hfbn_OA_clean)):
restricted00049_best_hfbn_mOA = hfbn_mOA
restricted00049_best_hfbn_mOA_epoch = epoch
save_checkpoint('restricted00049_best_hfbn_mOA', epoch, model, optimizer, lr_sched, bnm_sched, test_perf, cfg)
with open(os.path.join(path_best, 'restricted00049_best_hfbn_mOA_epoch.txt'), 'at') as f:
f.write('restricted00049_best_hfbn_mOA_epoch {}\n''restricted00049_best_hfbn_mOA {}\n'.format(restricted00049_best_hfbn_mOA_epoch, restricted00049_best_hfbn_mOA))
if lfbn_mCE < (best_lfbn_mCE+0.0049) and (lfbn_OA_clean > restricted00049_best_lfbn_OA_clean or (lfbn_OA_clean == restricted00049_best_lfbn_OA_clean and lfbn_mCE < restricted00049_best_lfbn_mCE)):
restricted00049_best_lfbn_OA_clean = lfbn_OA_clean
restricted00049_best_lfbn_OA_clean_epoch = epoch
save_checkpoint('restricted00049_best_lfbn_OA_clean', epoch, model, optimizer, lr_sched, bnm_sched, test_perf, cfg)
with open(os.path.join(path_best, 'restricted00049_best_lfbn_OA_clean_epoch.txt'), 'at') as f:
f.write('restricted00049_best_lfbn_OA_clean_epoch {}\n''restricted00049_best_lfbn_OA_clean {}\n'.format(restricted00049_best_lfbn_OA_clean_epoch, restricted00049_best_lfbn_OA_clean))
if lfbn_OA_clean > (best_lfbn_OA_clean-0.0049) and (lfbn_mCE < restricted00049_best_lfbn_mCE or (lfbn_mCE == restricted00049_best_lfbn_mCE and lfbn_OA_clean > restricted00049_best_lfbn_OA_clean)):
restricted00049_best_lfbn_mCE = lfbn_mCE
restricted00049_best_lfbn_mCE_epoch = epoch
save_checkpoint('restricted00049_best_lfbn_mCE', epoch, model, optimizer, lr_sched, bnm_sched, test_perf, cfg)
with open(os.path.join(path_best, 'restricted00049_best_lfbn_mCE_epoch.txt'), 'at') as f:
f.write('restricted00049_best_lfbn_mCE_epoch {}\n''restricted00049_best_lfbn_mCE {}\n'.format(restricted00049_best_lfbn_mCE_epoch, restricted00049_best_lfbn_mCE))
if lfbn_OA_clean > (best_lfbn_OA_clean-0.0049) and (lfbn_RmCE < restricted00049_best_lfbn_RmCE or (lfbn_RmCE == restricted00049_best_lfbn_RmCE and lfbn_OA_clean > restricted00049_best_lfbn_OA_clean)):
restricted00049_best_lfbn_RmCE = lfbn_RmCE
restricted00049_best_lfbn_RmCE_epoch = epoch
save_checkpoint('restricted00049_best_lfbn_RmCE', epoch, model, optimizer, lr_sched, bnm_sched, test_perf, cfg)
with open(os.path.join(path_best, 'restricted00049_best_lfbn_RmCE_epoch.txt'), 'at') as f:
f.write('restricted00049_best_lfbn_RmCE_epoch {}\n''restricted00049_best_lfbn_RmCE {}\n'.format(restricted00049_best_lfbn_RmCE_epoch, restricted00049_best_lfbn_RmCE))
if lfbn_OA_clean > (best_lfbn_OA_clean-0.0049) and (lfbn_mOA > restricted00049_best_lfbn_mOA or (lfbn_mOA == restricted00049_best_lfbn_mOA and lfbn_OA_clean > restricted00049_best_lfbn_OA_clean)):
restricted00049_best_lfbn_mOA = lfbn_mOA
restricted00049_best_lfbn_mOA_epoch = epoch
save_checkpoint('restricted00049_best_lfbn_mOA', epoch, model, optimizer, lr_sched, bnm_sched, test_perf, cfg)
with open(os.path.join(path_best, 'restricted00049_best_lfbn_mOA_epoch.txt'), 'at') as f:
f.write('restricted00049_best_lfbn_mOA_epoch {}\n''restricted00049_best_lfbn_mOA {}\n'.format(restricted00049_best_lfbn_mOA_epoch, restricted00049_best_lfbn_mOA))
###############################################00049
###############################################00049
###############################################00149
###############################################00149
if cleanbn_mCE < (best_cleanbn_mCE+0.0149) and (cleanbn_OA_clean > restricted00149_best_cleanbn_OA_clean or (cleanbn_OA_clean == restricted00149_best_cleanbn_OA_clean and cleanbn_mCE < restricted00149_best_cleanbn_mCE)):
restricted00149_best_cleanbn_OA_clean = cleanbn_OA_clean
restricted00149_best_cleanbn_OA_clean_epoch = epoch
save_checkpoint('restricted00149_best_cleanbn_OA_clean', epoch, model, optimizer, lr_sched, bnm_sched, test_perf, cfg)
with open(os.path.join(path_best, 'restricted00149_best_cleanbn_OA_clean_epoch.txt'), 'at') as f:
f.write('restricted00149_best_cleanbn_OA_clean_epoch {}\n''restricted00149_best_cleanbn_OA_clean {}\n'.format(restricted00149_best_cleanbn_OA_clean_epoch, restricted00149_best_cleanbn_OA_clean))
if cleanbn_OA_clean > (best_cleanbn_OA_clean-0.0149) and (cleanbn_mCE < restricted00149_best_cleanbn_mCE or (cleanbn_mCE == restricted00149_best_cleanbn_mCE and cleanbn_OA_clean > restricted00149_best_cleanbn_OA_clean)):
restricted00149_best_cleanbn_mCE = cleanbn_mCE
restricted00149_best_cleanbn_mCE_epoch = epoch
save_checkpoint('restricted00149_best_cleanbn_mCE', epoch, model, optimizer, lr_sched, bnm_sched, test_perf, cfg)
with open(os.path.join(path_best, 'restricted00149_best_cleanbn_mCE_epoch.txt'), 'at') as f:
f.write('restricted00149_best_cleanbn_mCE_epoch {}\n''restricted00149_best_cleanbn_mCE {}\n'.format(restricted00149_best_cleanbn_mCE_epoch, restricted00149_best_cleanbn_mCE))
if cleanbn_OA_clean > (best_cleanbn_OA_clean-0.0149) and (cleanbn_RmCE < restricted00149_best_cleanbn_RmCE or (cleanbn_RmCE == restricted00149_best_cleanbn_RmCE and cleanbn_OA_clean > restricted00149_best_cleanbn_OA_clean)):
restricted00149_best_cleanbn_RmCE = cleanbn_RmCE
restricted00149_best_cleanbn_RmCE_epoch = epoch
save_checkpoint('restricted00149_best_cleanbn_RmCE', epoch, model, optimizer, lr_sched, bnm_sched, test_perf, cfg)
with open(os.path.join(path_best, 'restricted00149_best_cleanbn_RmCE_epoch.txt'), 'at') as f:
f.write('restricted00149_best_cleanbn_RmCE_epoch {}\n''restricted00149_best_cleanbn_RmCE {}\n'.format(restricted00149_best_cleanbn_RmCE_epoch, restricted00149_best_cleanbn_RmCE))
if cleanbn_OA_clean > (best_cleanbn_OA_clean-0.0149) and (cleanbn_mOA > restricted00149_best_cleanbn_mOA or (cleanbn_mOA == restricted00149_best_cleanbn_mOA and cleanbn_OA_clean > restricted00149_best_cleanbn_OA_clean)):
restricted00149_best_cleanbn_mOA = cleanbn_mOA
restricted00149_best_cleanbn_mOA_epoch = epoch
save_checkpoint('restricted00149_best_cleanbn_mOA', epoch, model, optimizer, lr_sched, bnm_sched, test_perf, cfg)
with open(os.path.join(path_best, 'restricted00149_best_cleanbn_mOA_epoch.txt'), 'at') as f:
f.write('restricted00149_best_cleanbn_mOA_epoch {}\n''restricted00149_best_cleanbn_mOA {}\n'.format(restricted00149_best_cleanbn_mOA_epoch, restricted00149_best_cleanbn_mOA))
if hfbn_mCE < (best_hfbn_mCE+0.0149) and (hfbn_OA_clean > restricted00149_best_hfbn_OA_clean or (hfbn_OA_clean == restricted00149_best_hfbn_OA_clean and hfbn_mCE < restricted00149_best_hfbn_mCE)):
restricted00149_best_hfbn_OA_clean = hfbn_OA_clean
restricted00149_best_hfbn_OA_clean_epoch = epoch
save_checkpoint('restricted00149_best_hfbn_OA_clean', epoch, model, optimizer, lr_sched, bnm_sched, test_perf, cfg)
with open(os.path.join(path_best, 'restricted00149_best_hfbn_OA_clean_epoch.txt'), 'at') as f:
f.write('restricted00149_best_hfbn_OA_clean_epoch {}\n''restricted00149_best_hfbn_OA_clean {}\n'.format(restricted00149_best_hfbn_OA_clean_epoch, restricted00149_best_hfbn_OA_clean))
if hfbn_OA_clean > (best_hfbn_OA_clean-0.0149) and (hfbn_mCE < restricted00149_best_hfbn_mCE or (hfbn_mCE == restricted00149_best_hfbn_mCE and hfbn_OA_clean > restricted00149_best_hfbn_OA_clean)):
restricted00149_best_hfbn_mCE = hfbn_mCE
restricted00149_best_hfbn_mCE_epoch = epoch
save_checkpoint('restricted00149_best_hfbn_mCE', epoch, model, optimizer, lr_sched, bnm_sched, test_perf, cfg)
with open(os.path.join(path_best, 'restricted00149_best_hfbn_mCE_epoch.txt'), 'at') as f:
f.write('restricted00149_best_hfbn_mCE_epoch {}\n''restricted00149_best_hfbn_mCE {}\n'.format(restricted00149_best_hfbn_mCE_epoch, restricted00149_best_hfbn_mCE))
if hfbn_OA_clean > (best_hfbn_OA_clean-0.0149) and (hfbn_RmCE < restricted00149_best_hfbn_RmCE or (hfbn_RmCE == restricted00149_best_hfbn_RmCE and hfbn_OA_clean > restricted00149_best_hfbn_OA_clean)):
restricted00149_best_hfbn_RmCE = hfbn_RmCE
restricted00149_best_hfbn_RmCE_epoch = epoch
save_checkpoint('restricted00149_best_hfbn_RmCE', epoch, model, optimizer, lr_sched, bnm_sched, test_perf, cfg)
with open(os.path.join(path_best, 'restricted00149_best_hfbn_RmCE_epoch.txt'), 'at') as f:
f.write('restricted00149_best_hfbn_RmCE_epoch {}\n''restricted00149_best_hfbn_RmCE {}\n'.format(restricted00149_best_hfbn_RmCE_epoch, restricted00149_best_hfbn_RmCE))
if hfbn_OA_clean > (best_hfbn_OA_clean-0.0149) and (hfbn_mOA > restricted00149_best_hfbn_mOA or (hfbn_mOA == restricted00149_best_hfbn_mOA and hfbn_OA_clean > restricted00149_best_hfbn_OA_clean)):
restricted00149_best_hfbn_mOA = hfbn_mOA
restricted00149_best_hfbn_mOA_epoch = epoch
save_checkpoint('restricted00149_best_hfbn_mOA', epoch, model, optimizer, lr_sched, bnm_sched, test_perf, cfg)
with open(os.path.join(path_best, 'restricted00149_best_hfbn_mOA_epoch.txt'), 'at') as f:
f.write('restricted00149_best_hfbn_mOA_epoch {}\n''restricted00149_best_hfbn_mOA {}\n'.format(restricted00149_best_hfbn_mOA_epoch, restricted00149_best_hfbn_mOA))
if lfbn_mCE < (best_lfbn_mCE+0.0149) and (lfbn_OA_clean > restricted00149_best_lfbn_OA_clean or (lfbn_OA_clean == restricted00149_best_lfbn_OA_clean and lfbn_mCE < restricted00149_best_lfbn_mCE)):
restricted00149_best_lfbn_OA_clean = lfbn_OA_clean
restricted00149_best_lfbn_OA_clean_epoch = epoch
save_checkpoint('restricted00149_best_lfbn_OA_clean', epoch, model, optimizer, lr_sched, bnm_sched, test_perf, cfg)
with open(os.path.join(path_best, 'restricted00149_best_lfbn_OA_clean_epoch.txt'), 'at') as f:
f.write('restricted00149_best_lfbn_OA_clean_epoch {}\n''restricted00149_best_lfbn_OA_clean {}\n'.format(restricted00149_best_lfbn_OA_clean_epoch, restricted00149_best_lfbn_OA_clean))
if lfbn_OA_clean > (best_lfbn_OA_clean-0.0149) and (lfbn_mCE < restricted00149_best_lfbn_mCE or (lfbn_mCE == restricted00149_best_lfbn_mCE and lfbn_OA_clean > restricted00149_best_lfbn_OA_clean)):
restricted00149_best_lfbn_mCE = lfbn_mCE
restricted00149_best_lfbn_mCE_epoch = epoch
save_checkpoint('restricted00149_best_lfbn_mCE', epoch, model, optimizer, lr_sched, bnm_sched, test_perf, cfg)
with open(os.path.join(path_best, 'restricted00149_best_lfbn_mCE_epoch.txt'), 'at') as f:
f.write('restricted00149_best_lfbn_mCE_epoch {}\n''restricted00149_best_lfbn_mCE {}\n'.format(restricted00149_best_lfbn_mCE_epoch, restricted00149_best_lfbn_mCE))
if lfbn_OA_clean > (best_lfbn_OA_clean-0.0149) and (lfbn_RmCE < restricted00149_best_lfbn_RmCE or (lfbn_RmCE == restricted00149_best_lfbn_RmCE and lfbn_OA_clean > restricted00149_best_lfbn_OA_clean)):
restricted00149_best_lfbn_RmCE = lfbn_RmCE
restricted00149_best_lfbn_RmCE_epoch = epoch
save_checkpoint('restricted00149_best_lfbn_RmCE', epoch, model, optimizer, lr_sched, bnm_sched, test_perf, cfg)
with open(os.path.join(path_best, 'restricted00149_best_lfbn_RmCE_epoch.txt'), 'at') as f:
f.write('restricted00149_best_lfbn_RmCE_epoch {}\n''restricted00149_best_lfbn_RmCE {}\n'.format(restricted00149_best_lfbn_RmCE_epoch, restricted00149_best_lfbn_RmCE))
if lfbn_OA_clean > (best_lfbn_OA_clean-0.0149) and (lfbn_mOA > restricted00149_best_lfbn_mOA or (lfbn_mOA == restricted00149_best_lfbn_mOA and lfbn_OA_clean > restricted00149_best_lfbn_OA_clean)):
restricted00149_best_lfbn_mOA = lfbn_mOA
restricted00149_best_lfbn_mOA_epoch = epoch
save_checkpoint('restricted00149_best_lfbn_mOA', epoch, model, optimizer, lr_sched, bnm_sched, test_perf, cfg)
with open(os.path.join(path_best, 'restricted00149_best_lfbn_mOA_epoch.txt'), 'at') as f:
f.write('restricted00149_best_lfbn_mOA_epoch {}\n''restricted00149_best_lfbn_mOA {}\n'.format(restricted00149_best_lfbn_mOA_epoch, restricted00149_best_lfbn_mOA))
###############################################00149
###############################################00149
track_train.record_epoch(
epoch_id=epoch,
train_metric=get_metric_from_perf(cfg.EXP.TASK, train_perf, cfg.EXP.METRIC),
val_metric=get_metric_from_perf(cfg.EXP.TASK, val_perf, cfg.EXP.METRIC),
test_metric=get_metric_from_perf(cfg.EXP.TASK, test_perf, cfg.EXP.METRIC))
if (not cfg.EXP_EXTRA.no_val) and track_train.save_model(epoch_id=epoch, split='val'):
print('Saving best model on the validation set')
save_checkpoint('best_val', epoch, model, optimizer, lr_sched, bnm_sched, test_perf, cfg)
if (not cfg.EXP_EXTRA.no_test) and track_train.save_model(epoch_id=epoch, split='test'):
print('Saving best model on the test set')
save_checkpoint('best_test', epoch, model, optimizer, lr_sched, bnm_sched, test_perf, cfg)
if (not cfg.EXP_EXTRA.no_val) and track_train.early_stop(epoch_id=epoch):
print(f"Early stopping at {epoch} as val acc did not improve for {cfg.TRAIN.early_stop} epochs.")
break
if (not (cfg.EXP_EXTRA.save_ckp == 0)) and (epoch % cfg.EXP_EXTRA.save_ckp == 0):
save_checkpoint(f'{epoch}', epoch, model, optimizer, lr_sched, bnm_sched, test_perf, cfg)
if cfg.EXP.OPTIMIZER == 'vanilla':
assert bnm_sched is None
lr_sched.step(train_loss)
else:
lr_sched.step()
print('Saving the final model')
save_checkpoint('final', epoch, model, optimizer, lr_sched, bnm_sched, test_perf, cfg)
print('\nTesting on the final model..')
last_test_perf = validate(cfg.EXP.TASK, loader_test, model, cfg.EXP.DATASET, cfg.ADAPT)
pprint.pprint(last_test_perf, width=80)
tb.close()
def entry_test(cfg, test_or_valid, model_path="", confusion = False):
split = "test" if test_or_valid else "valid"
loader_test = create_dataloader(split=split, cfg=cfg)
model = get_model(cfg)
model.to(DEVICE)
print(model)
if torch.cuda.device_count() > 1:
model = nn.DataParallel(model)
optimizer, lr_sched, bnm_sched = get_optimizer(cfg.EXP.OPTIMIZER, cfg.TRAIN, model)
model = load_model_opt_sched(model, optimizer, lr_sched, bnm_sched, model_path)
model.eval()
if confusion:
test_perf, pred, ground = validate(cfg.EXP.TASK, loader_test, model, cfg.EXP.DATASET, cfg.ADAPT, confusion)
print(pred.shape, ground.shape)
#### some hardcoding #######
np.save('./output/' + cfg.EXP.MODEL_NAME + '_' + cfg.DATALOADER.MODELNET40_C.corruption + '_' + str(cfg.DATALOADER.MODELNET40_C.severity) + '_pred.npy',pred )
np.save('./output/' + cfg.EXP.MODEL_NAME + '_' + cfg.DATALOADER.MODELNET40_C.corruption + '_' + str(cfg.DATALOADER.MODELNET40_C.severity) + '_ground.npy',ground)
#### #### #### #### #### ####
else:
test_perf = validate(cfg.EXP.TASK, loader_test, model, cfg.EXP.DATASET, cfg.ADAPT, confusion)
print("Model: {} Corruption: {} Severity: {} Acc: {} Class Acc: {}".format(cfg.EXP.MODEL_NAME, cfg.DATALOADER.MODELNET40_C.corruption, cfg.DATALOADER.MODELNET40_C.severity,test_perf['acc'],test_perf['class_acc']),file=file_object,flush=True)
pprint.pprint(test_perf, width=80)
return test_perf
def entry_pgdtest(cfg, test_or_valid, model_path="", confusion = False, step=20, eps=0.05):
split = "test" #if test_or_valid else "valid"
print(split)
loader_test = create_dataloader(split=split, cfg=cfg)
print(step)
model = get_model(cfg)
model.to(DEVICE)
# print(model)
if torch.cuda.device_count() > 1:
model = nn.DataParallel(model)
optimizer, lr_sched, bnm_sched = get_optimizer(cfg.EXP.OPTIMIZER, cfg.TRAIN, model)
model = load_model_opt_sched(model, optimizer, lr_sched, bnm_sched, model_path)
model.eval()
if confusion:
test_perf, pred, ground = validate(cfg.EXP.TASK, loader_test, model, cfg.EXP.DATASET, cfg.ADAPT, confusion)
print(pred.shape, ground.shape)
#### some hardcoding #######
np.save('./output/' + cfg.EXP.MODEL_NAME + '_' + cfg.DATALOADER.MODELNET40_C.corruption + '_' + str(cfg.DATALOADER.MODELNET40_C.severity) + '_pred.npy',pred )
np.save('./output/' + cfg.EXP.MODEL_NAME + '_' + cfg.DATALOADER.MODELNET40_C.corruption + '_' + str(cfg.DATALOADER.MODELNET40_C.severity) + '_ground.npy',ground)
#### #### #### #### #### ####
else:
test_perf = robust_validate(cfg.EXP.TASK, loader_test, model, cfg.EXP.LOSS_NAME, cfg.EXP.DATASET, cfg.ADAPT, confusion, step=step, eps=eps)
print("Model: {} Corruption: {} Severity: {} Acc: {} Class Acc: {}".format(cfg.EXP.MODEL_NAME, cfg.DATALOADER.MODELNET40_C.corruption, cfg.DATALOADER.MODELNET40_C.severity,test_perf['acc'],test_perf['class_acc']),file=file_object,flush=True)
pprint.pprint(test_perf, width=80)
return test_perf
def entry_pladvtest(cfg, test_or_valid, model_path="", pladv = 'aof', confusion = False, eps=0.05):
split = "test"
loader_test = create_dataloader(split=split, cfg=cfg)
print('----inside pl adv test')
model = get_model(cfg)
model.to(DEVICE)
# print(model)
if torch.cuda.device_count() > 1:
model = nn.DataParallel(model)
optimizer, lr_sched, bnm_sched = get_optimizer(cfg.EXP.OPTIMIZER, cfg.TRAIN, model)
model = load_model_opt_sched(model, optimizer, lr_sched, bnm_sched, model_path)
model.eval()
if confusion:
test_perf, pred, ground = validate(cfg.EXP.TASK, loader_test, model, cfg.EXP.DATASET, cfg.ADAPT, confusion)
print(pred.shape, ground.shape)
#### some hardcoding #######
np.save('./output/' + cfg.EXP.MODEL_NAME + '_' + cfg.DATALOADER.MODELNET40_C.corruption + '_' + str(cfg.DATALOADER.MODELNET40_C.severity) + '_pred.npy',pred )
np.save('./output/' + cfg.EXP.MODEL_NAME + '_' + cfg.DATALOADER.MODELNET40_C.corruption + '_' + str(cfg.DATALOADER.MODELNET40_C.severity) + '_ground.npy',ground)
#### #### #### #### #### ####
else:
test_perf = pladv_validate(cfg.EXP.TASK, loader_test, model, cfg.EXP.DATASET, pladv, cfg.ADAPT, confusion, eps=eps)
print("Model: {} Corruption: {} Severity: {} Acc: {} Class Acc: {}".format(cfg.EXP.MODEL_NAME, cfg.DATALOADER.MODELNET40_C.corruption, cfg.DATALOADER.MODELNET40_C.severity,test_perf['acc'],test_perf['class_acc']),file=file_object,flush=True)
pprint.pprint(test_perf, width=80)
return test_perf
def rscnn_vote_evaluation(cfg, model_path, log_file):
model = get_model(cfg)
checkpoint = torch.load(model_path)
try:
model.load_state_dict(checkpoint['model_state'])
except:
print("WARNING: using dataparallel to load data")
model = nn.DataParallel(model)
model.load_state_dict(checkpoint['model_state'])
print(f"Checkpoint loaded from {model_path}")
model.to(DEVICE)
model.eval()
assert cfg.EXP.DATASET in ["modelnet40_rscnn"]
loader_test = create_dataloader(split='test', cfg=cfg)
rscnn_voting_evaluate_cls(
loader=loader_test,
model=model,
data_batch_to_points_target=lambda x: (x['pc'], x['label']),
points_to_inp=lambda x: {'pc': x},
out_to_prob=lambda x: F.softmax(x['logit'], dim=1),
log_file=log_file
)
def pn2_vote_evaluation(cfg, model_path, log_file):
assert cfg.EXP.DATASET in ["modelnet40_pn2"]
loader_test = create_dataloader(split='test', cfg=cfg)
model = get_model(cfg)
checkpoint = torch.load(model_path)
try:
model.load_state_dict(checkpoint['model_state'])
except:
print("WARNING: using dataparallel to load data")
model = nn.DataParallel(model)
model.load_state_dict(checkpoint['model_state'])
print(f"Checkpoint loaded from {model_path}")
model.to(DEVICE)
model.eval()
pn2_vote_evaluate_cls(loader_test, model, log_file)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.set_defaults(entry=lambda cmd_args: parser.print_help())
parser.add_argument('--entry', type=str, default="train")
parser.add_argument('--exp-config', type=str, default="")
parser.add_argument('--model-path', type=str, default="")
parser.add_argument('--resume', action="store_true", default=False)
# parser.add_argument('--gpu',type=str,default='0',
# help="Which gpu to use")
parser.add_argument('--corruption',type=str,default='uniform',
help="Which corruption to use")
parser.add_argument('--pladv',type=str,default='aof',
help="Which point cloud attack to use")
parser.add_argument('--output',type=str,default='./test.txt',
help="path to output file")
parser.add_argument('--severity',type=int,default=1,
help="Which severity to use")
parser.add_argument('--pgd_step',type=int,default=20,
help="Which severity to use")
parser.add_argument('--eps',type=float,default=0.05,
help="Which severity to use")
parser.add_argument('--confusion', action="store_true", default=False,
help="whether to output confusion matrix data")
cmd_args = parser.parse_args()
# os.environ['CUDA_VISIBLE_DEVICES'] = cmd_args.gpu
if cmd_args.entry == "train":
assert not cmd_args.exp_config == ""
if not cmd_args.resume:
assert cmd_args.model_path == ""
cfg = get_cfg_defaults()
cfg.merge_from_file(cmd_args.exp_config)
if cfg.EXP.EXP_ID == "":
cfg.EXP.EXP_ID = str(datetime.now())[:-7].replace(' ', '-')
cfg.freeze()
print(cfg)
random.seed(cfg.EXP.SEED)
np.random.seed(cfg.EXP.SEED)
torch.manual_seed(cfg.EXP.SEED)
entry_train(cfg, cmd_args.resume, cmd_args.model_path)
elif cmd_args.entry in ["test", "valid"]:
file_object = open(cmd_args.output, 'a')
assert not cmd_args.exp_config == ""
assert not cmd_args.model_path == ""
cfg = get_cfg_defaults()
cfg.merge_from_file(cmd_args.exp_config)
if cfg.EXP.DATASET == "modelnet40_c":
cfg.DATALOADER.MODELNET40_C.corruption = cmd_args.corruption
cfg.DATALOADER.MODELNET40_C.severity = cmd_args.severity
cfg.freeze()
print(cfg)
random.seed(cfg.EXP.SEED)
np.random.seed(cfg.EXP.SEED)
torch.manual_seed(cfg.EXP.SEED)
test_or_valid = cmd_args.entry == "test"
entry_test(cfg, test_or_valid, cmd_args.model_path,cmd_args.confusion)
elif cmd_args.entry in ["pgdtest"]:
file_object = open(cmd_args.output, 'a')
assert not cmd_args.exp_config == ""
assert not cmd_args.model_path == ""
cfg = get_cfg_defaults()
cfg.merge_from_file(cmd_args.exp_config)
if cfg.EXP.DATASET == "modelnet40_c":
cfg.DATALOADER.MODELNET40_C.corruption = cmd_args.corruption
cfg.DATALOADER.MODELNET40_C.severity = cmd_args.severity
cfg.freeze()
print("----- in pgdtest")
random.seed(cfg.EXP.SEED)
np.random.seed(cfg.EXP.SEED)
torch.manual_seed(cfg.EXP.SEED)
test_or_valid = cmd_args.entry == "test"
entry_pgdtest(cfg, test_or_valid, cmd_args.model_path,cmd_args.confusion, step=cmd_args.pgd_step, eps=cmd_args.eps)
elif cmd_args.entry in ["pladvtest"]:
file_object = open(cmd_args.output, 'a')
assert not cmd_args.exp_config == ""
assert not cmd_args.model_path == ""
cfg = get_cfg_defaults()
cfg.merge_from_file(cmd_args.exp_config)
cfg.freeze()
# print(cfg)
random.seed(cfg.EXP.SEED)
np.random.seed(cfg.EXP.SEED)
torch.manual_seed(cfg.EXP.SEED)
test_or_valid = cmd_args.entry == "test"
entry_pladvtest(cfg, test_or_valid, cmd_args.model_path,cmd_args.pladv, cmd_args.confusion, cmd_args.eps)
elif cmd_args.entry in ["rscnn_vote", "pn2_vote"]:
assert not cmd_args.exp_config == ""
assert not cmd_args.model_path == ""
log_file = f"vote_log/{cmd_args.model_path.replace('/', '_')}_{cmd_args.entry.replace('/', '_')}.log"
cfg = get_cfg_defaults()
cfg.merge_from_file(cmd_args.exp_config)
cfg.freeze()
print(cfg)
seed = cfg.EXP.SEED
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
torch.backends.cudnn.enabled = True
torch.backends.cudnn.benchmark = True
torch.backends.cudnn.deterministic = True
if cmd_args.entry == "rscnn_vote":
rscnn_vote_evaluation(cfg, cmd_args.model_path, log_file)
elif cmd_args.entry == "pn2_vote":
pn2_vote_evaluation(cfg, cmd_args.model_path, log_file)
else:
assert False
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/cuge1995/ModelNet40-C.git
git@gitee.com:cuge1995/ModelNet40-C.git
cuge1995
ModelNet40-C
ModelNet40-C
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385