def RetModel()->Tuple[Model,Model,Model,Model,Model,Model,Model]: ''' To get all training model returns: load vgg,resnet and densenet model ''' return [VGG16(input_shape=(256,256,7),classes=2),VGG19(input_shape=(256,256,7),classes=2), ResNet50(input_shape=(256, 256, 7), classes=2),InceptionResNetV2(input_shape=(256, 256, 7), classes=2), DenseNet121(input_shape=(256, 256, 7),classes=2),DenseNet169(input_shape=(256, 256, 7), classes=2), DenseNet201(input_shape=(256, 256, 7),classes=2)]
def RetModel(): return [ VGG16(input_shape=(256, 256, 7), classes=2), VGG19(input_shape=(256, 256, 7), classes=2), ResNet50(input_shape=(256, 256, 7), classes=2), InceptionResNetV2(input_shape=(256, 256, 7), classes=2), DenseNet121(input_shape=(256, 256, 7), classes=2), DenseNet169(input_shape=(256, 256, 7), classes=2), DenseNet201(input_shape=(256, 256, 7), classes=2) ]
def densenet121(self): l_in, out = DenseNet121(input_shape=(self.size, self.size, self.num_channels), pooling='avg', activation=self.__activation) out = Dense(2048, kernel_initializer=keras.initializers.he_normal())(out) out = self.__activation()(out) out = keras.layers.Dropout(0.3)(out) out = Dense(self.num_classes, kernel_initializer=keras.initializers.he_normal())(out) out = Activation('softmax')(out) return Model(l_in, out, name='DenseNet121')
def get_network(name, num_classes, pretrained): if name == 'densenet-base': assert not pretrained return DenseNetBase(growthRate=12, depth=100, reduction=0.5, bottleneck=True, nClasses=num_classes) elif name == 'densenet-121': return DenseNet121(num_classes, pretrained) elif name == 'densenet-100': assert not pretrained return DenseNet100(num_classes)
def __init__(self, pathModel, num_classes, transCrop): model = DenseNet121(num_classes).cuda() modelCheckpoint = torch.load(pathModel) model.load_state_dict(modelCheckpoint['state_dict']) self.model = model self.model.eval() self.weights = list( self.model._modules['densenet121'].features.parameters())[-2] #---- Initialize the image transform normalize = transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]) transformList = [] transformList.append(transforms.Resize((transCrop, transCrop))) transformList.append(transforms.ToTensor()) transformList.append(normalize) self.transformSequence = transforms.Compose(transformList)
if NUM_CLASSES == None: NUM_CLASSES = dataset_train.get_number_of_classes() NUM_TRAIN_SAMPLES = dataset_train.get_number_of_samples() NUM_TEST_SAMPLES= dataset_test.get_number_of_samples() print(f"train_samples {NUM_TRAIN_SAMPLES} test_samples {NUM_TEST_SAMPLES}") print(f'num_classes {NUM_CLASSES}') NUM_EPOCHS = 10000 BATCH_SIZE = 8 LEARNING_RATE = 1e-4 #DEVICE = 'cuda' DEVICE = 'cpu' model_densenet = DenseNet121(class_num = NUM_CLASSES).to(DEVICE) optimizer = torch.optim.Adam(params = model_densenet.parameters(), lr = LEARNING_RATE) def layers_debug(optim): print("yes") layer_count = 0 for var_name in optim.state_dict(): shape = optim.state_dict()[var_name].shape if len(optim.state_dict()[var_name].shape)>1: layer_count += 1 print(f"{var_name}\t\t{optim.state_dict()[var_name].shape}") print(layer_count) layers_debug(model_densenet)
def main(): args = get_args() if not os.path.exists(args.fname): os.makedirs(args.fname) logger = logging.getLogger(__name__) logging.basicConfig( format='[%(asctime)s] - %(message)s', datefmt='%Y/%m/%d %H:%M:%S', level=logging.DEBUG, handlers=[ logging.FileHandler( os.path.join(args.fname, 'eval.log' if args.eval else 'output.log')), logging.StreamHandler() ]) logger.info(args) np.random.seed(args.seed) torch.manual_seed(args.seed) torch.cuda.manual_seed(args.seed) transforms = [Crop(32, 32), FlipLR()] # transforms = [Crop(32, 32)] if args.cutout: transforms.append(Cutout(args.cutout_len, args.cutout_len)) if args.val: try: dataset = torch.load("cifar10_validation_split.pth") except: print( "Couldn't find a dataset with a validation split, did you run " "generate_validation.py?") return val_set = list( zip(transpose(dataset['val']['data'] / 255.), dataset['val']['labels'])) val_batches = Batches(val_set, args.batch_size, shuffle=False, num_workers=2) else: dataset = cifar10(args.data_dir) train_set = list( zip(transpose(pad(dataset['train']['data'], 4) / 255.), dataset['train']['labels'])) train_set_x = Transform(train_set, transforms) train_batches = Batches(train_set_x, args.batch_size, shuffle=True, set_random_choices=True, num_workers=2) test_set = list( zip(transpose(dataset['test']['data'] / 255.), dataset['test']['labels'])) test_batches = Batches(test_set, args.batch_size, shuffle=False, num_workers=2) trn_epsilon = (args.trn_epsilon / 255.) trn_pgd_alpha = (args.trn_pgd_alpha / 255.) tst_epsilon = (args.tst_epsilon / 255.) tst_pgd_alpha = (args.tst_pgd_alpha / 255.) if args.model == 'PreActResNet18': model = PreActResNet18() elif args.model == 'WideResNet': model = WideResNet(34, 10, widen_factor=args.width_factor, dropRate=0.0) elif args.model == 'DenseNet121': model = DenseNet121() elif args.model == 'ResNet18': model = ResNet18() else: raise ValueError("Unknown model") ### temp testing ### model = model.cuda() # model = nn.DataParallel(model).cuda() model.train() ################################## # load pretrained model if needed if args.trn_adv_models != 'None': if args.trn_adv_arch == 'PreActResNet18': trn_adv_model = PreActResNet18() elif args.trn_adv_arch == 'WideResNet': trn_adv_model = WideResNet(34, 10, widen_factor=args.width_factor, dropRate=0.0) elif args.trn_adv_arch == 'DenseNet121': trn_adv_model = DenseNet121() elif args.trn_adv_arch == 'ResNet18': trn_adv_model = ResNet18() trn_adv_model = nn.DataParallel(trn_adv_model).cuda() trn_adv_model.load_state_dict( torch.load( os.path.join('./adv_models', args.trn_adv_models, 'model_best.pth'))['state_dict']) logger.info(f'loaded adv_model: {args.trn_adv_models}') else: trn_adv_model = None if args.tst_adv_models != 'None': if args.tst_adv_arch == 'PreActResNet18': tst_adv_model = PreActResNet18() elif args.tst_adv_arch == 'WideResNet': tst_adv_model = WideResNet(34, 10, widen_factor=args.width_factor, dropRate=0.0) elif args.tst_adv_arch == 'DenseNet121': tst_adv_model = DenseNet121() elif args.tst_adv_arch == 'ResNet18': tst_adv_model = ResNet18() ### temp testing ### tst_adv_model = tst_adv_model.cuda() tst_adv_model.load_state_dict( torch.load( os.path.join('./adv_models', args.tst_adv_models, 'model_best.pth'))) # tst_adv_model = nn.DataParallel(tst_adv_model).cuda() # tst_adv_model.load_state_dict(torch.load(os.path.join('./adv_models',args.tst_adv_models, 'model_best.pth'))['state_dict']) logger.info(f'loaded adv_model: {args.tst_adv_models}') else: tst_adv_model = None ################################## if args.l2: decay, no_decay = [], [] for name, param in model.named_parameters(): if 'bn' not in name and 'bias' not in name: decay.append(param) else: no_decay.append(param) params = [{ 'params': decay, 'weight_decay': args.l2 }, { 'params': no_decay, 'weight_decay': 0 }] else: params = model.parameters() opt = torch.optim.SGD(params, lr=args.lr_max, momentum=0.9, weight_decay=5e-4) criterion = nn.CrossEntropyLoss() if args.trn_attack == 'free': delta = torch.zeros(args.batch_size, 3, 32, 32).cuda() delta.requires_grad = True elif args.trn_attack == 'fgsm' and args.trn_fgsm_init == 'previous': delta = torch.zeros(args.batch_size, 3, 32, 32).cuda() delta.requires_grad = True if args.trn_attack == 'free': epochs = int(math.ceil(args.epochs / args.trn_attack_iters)) else: epochs = args.epochs if args.lr_schedule == 'superconverge': lr_schedule = lambda t: np.interp([t], [ 0, args.epochs * 2 // 5, args.epochs ], [0, args.lr_max, 0])[0] elif args.lr_schedule == 'piecewise': def lr_schedule(t): if t / args.epochs < 0.5: return args.lr_max elif t / args.epochs < 0.75: return args.lr_max / 10. else: return args.lr_max / 100. elif args.lr_schedule == 'linear': lr_schedule = lambda t: np.interp([t], [ 0, args.epochs // 3, args.epochs * 2 // 3, args.epochs ], [args.lr_max, args.lr_max, args.lr_max / 10, args.lr_max / 100])[0] elif args.lr_schedule == 'onedrop': def lr_schedule(t): if t < args.lr_drop_epoch: return args.lr_max else: return args.lr_one_drop elif args.lr_schedule == 'multipledecay': def lr_schedule(t): return args.lr_max - (t // (args.epochs // 10)) * (args.lr_max / 10) elif args.lr_schedule == 'cosine': def lr_schedule(t): return args.lr_max * 0.5 * (1 + np.cos(t / args.epochs * np.pi)) best_test_robust_acc = 0 best_val_robust_acc = 0 if args.resume: ### temp testing ### model.load_state_dict( torch.load(os.path.join(args.fname, 'model_best.pth'))) start_epoch = args.resume # model.load_state_dict(torch.load(os.path.join(args.fname, f'model_{start_epoch-1}.pth'))) # opt.load_state_dict(torch.load(os.path.join(args.fname, f'opt_{start_epoch-1}.pth'))) # logger.info(f'Resuming at epoch {start_epoch}') # best_test_robust_acc = torch.load(os.path.join(args.fname, f'model_best.pth'))['test_robust_acc'] if args.val: best_val_robust_acc = torch.load( os.path.join(args.fname, f'model_val.pth'))['val_robust_acc'] else: start_epoch = 0 if args.eval: if not args.resume: logger.info( "No model loaded to evaluate, specify with --resume FNAME") return logger.info("[Evaluation mode]") logger.info( 'Epoch \t Train Time \t Test Time \t LR \t \t Train Loss \t Train Acc \t Train Robust Loss \t Train Robust Acc \t Test Loss \t Test Acc \t Test Robust Loss \t Test Robust Acc' ) for epoch in range(start_epoch, epochs): model.train() start_time = time.time() train_loss = 0 train_acc = 0 train_robust_loss = 0 train_robust_acc = 0 train_n = 0 for i, batch in enumerate(train_batches): if args.eval: break X, y = batch['input'], batch['target'] if args.mixup: X, y_a, y_b, lam = mixup_data(X, y, args.mixup_alpha) X, y_a, y_b = map(Variable, (X, y_a, y_b)) lr = lr_schedule(epoch + (i + 1) / len(train_batches)) opt.param_groups[0].update(lr=lr) if args.trn_attack == 'pgd': # Random initialization if args.mixup: delta = attack_pgd(model, X, y, trn_epsilon, trn_pgd_alpha, args.trn_attack_iters, args.trn_restarts, args.trn_norm, mixup=True, y_a=y_a, y_b=y_b, lam=lam, adv_models=trn_adv_model) else: delta = attack_pgd(model, X, y, trn_epsilon, trn_pgd_alpha, args.trn_attack_iters, args.trn_restarts, args.trn_norm, adv_models=trn_adv_model) delta = delta.detach() elif args.trn_attack == 'fgsm': delta = attack_pgd(model, X, y, trn_epsilon, args.trn_fgsm_alpha * trn_epsilon, 1, 1, args.trn_norm, adv_models=trn_adv_model, rand_init=args.trn_fgsm_init) delta = delta.detach() # Standard training elif args.trn_attack == 'none': delta = torch.zeros_like(X) # The Momentum Iterative Attack elif args.trn_attack == 'tmim': if trn_adv_model is None: adversary = MomentumIterativeAttack( model, nb_iter=args.trn_attack_iters, eps=trn_epsilon, loss_fn=nn.CrossEntropyLoss(reduction="sum"), eps_iter=trn_pgd_alpha, clip_min=0, clip_max=1, targeted=False) else: trn_adv_model = nn.Sequential( NormalizeByChannelMeanStd(CIFAR10_MEAN, CIFAR10_STD), trn_adv_model) adversary = MomentumIterativeAttack( trn_adv_model, nb_iter=args.trn_attack_iters, eps=trn_epsilon, loss_fn=nn.CrossEntropyLoss(reduction="sum"), eps_iter=trn_pgd_alpha, clip_min=0, clip_max=1, targeted=False) data_adv = adversary.perturb(X, y) delta = data_adv - X delta = delta.detach() robust_output = model( normalize( torch.clamp(X + delta[:X.size(0)], min=lower_limit, max=upper_limit))) if args.mixup: robust_loss = mixup_criterion(criterion, robust_output, y_a, y_b, lam) else: robust_loss = criterion(robust_output, y) if args.l1: for name, param in model.named_parameters(): if 'bn' not in name and 'bias' not in name: robust_loss += args.l1 * param.abs().sum() opt.zero_grad() robust_loss.backward() opt.step() output = model(normalize(X)) if args.mixup: loss = mixup_criterion(criterion, output, y_a, y_b, lam) else: loss = criterion(output, y) train_robust_loss += robust_loss.item() * y.size(0) train_robust_acc += (robust_output.max(1)[1] == y).sum().item() train_loss += loss.item() * y.size(0) train_acc += (output.max(1)[1] == y).sum().item() train_n += y.size(0) train_time = time.time() model.eval() test_loss = 0 test_acc = 0 test_robust_loss = 0 test_robust_acc = 0 test_n = 0 for i, batch in enumerate(test_batches): X, y = batch['input'], batch['target'] # Random initialization if args.tst_attack == 'none': delta = torch.zeros_like(X) elif args.tst_attack == 'pgd': delta = attack_pgd(model, X, y, tst_epsilon, tst_pgd_alpha, args.tst_attack_iters, args.tst_restarts, args.tst_norm, adv_models=tst_adv_model, rand_init=args.tst_fgsm_init) elif args.tst_attack == 'fgsm': delta = attack_pgd(model, X, y, tst_epsilon, tst_epsilon, 1, 1, args.tst_norm, rand_init=args.tst_fgsm_init, adv_models=tst_adv_model) # The Momentum Iterative Attack elif args.tst_attack == 'tmim': if tst_adv_model is None: adversary = MomentumIterativeAttack( model, nb_iter=args.tst_attack_iters, eps=tst_epsilon, loss_fn=nn.CrossEntropyLoss(reduction="sum"), eps_iter=tst_pgd_alpha, clip_min=0, clip_max=1, targeted=False) else: tmp_model = nn.Sequential( NormalizeByChannelMeanStd(cifar10_mean, cifar10_std), tst_adv_model).to(device) adversary = MomentumIterativeAttack( tmp_model, nb_iter=args.tst_attack_iters, eps=tst_epsilon, loss_fn=nn.CrossEntropyLoss(reduction="sum"), eps_iter=tst_pgd_alpha, clip_min=0, clip_max=1, targeted=False) data_adv = adversary.perturb(X, y) delta = data_adv - X # elif args.tst_attack == 'pgd': # if tst_adv_model is None: # tmp_model = nn.Sequential(NormalizeByChannelMeanStd(cifar10_mean, cifar10_std), model).to(device) # adversary = PGDAttack(tmp_model, nb_iter=args.tst_attack_iters, # eps = tst_epsilon, # loss_fn=nn.CrossEntropyLoss(reduction="sum"), # eps_iter=tst_pgd_alpha, clip_min = 0, clip_max = 1, targeted=False) # else: # tmp_model = nn.Sequential(NormalizeByChannelMeanStd(cifar10_mean, cifar10_std), tst_adv_model).to(device) # adversary = PGDAttack(tmp_model, nb_iter=args.tst_attack_iters, # eps = tst_epsilon, # loss_fn=nn.CrossEntropyLoss(reduction="sum"), # eps_iter=tst_pgd_alpha, clip_min = 0, clip_max = 1, targeted=False) # data_adv = adversary.perturb(X, y) # delta = data_adv - X delta = delta.detach() robust_output = model( normalize( torch.clamp(X + delta[:X.size(0)], min=lower_limit, max=upper_limit))) robust_loss = criterion(robust_output, y) output = model(normalize(X)) loss = criterion(output, y) test_robust_loss += robust_loss.item() * y.size(0) test_robust_acc += (robust_output.max(1)[1] == y).sum().item() test_loss += loss.item() * y.size(0) test_acc += (output.max(1)[1] == y).sum().item() test_n += y.size(0) test_time = time.time() if args.val: val_loss = 0 val_acc = 0 val_robust_loss = 0 val_robust_acc = 0 val_n = 0 for i, batch in enumerate(val_batches): X, y = batch['input'], batch['target'] # Random initialization if args.tst_attack == 'none': delta = torch.zeros_like(X) elif args.tst_attack == 'pgd': delta = attack_pgd(model, X, y, tst_epsilon, tst_pgd_alpha, args.tst_attack_iters, args.tst_restarts, args.tst_norm, early_stop=args.eval) elif args.tst_attack == 'fgsm': delta = attack_pgd(model, X, y, tst_epsilon, tst_epsilon, 1, 1, args.tst_norm, early_stop=args.eval, rand_init=args.tst_fgsm_init) delta = delta.detach() robust_output = model( normalize( torch.clamp(X + delta[:X.size(0)], min=lower_limit, max=upper_limit))) robust_loss = criterion(robust_output, y) output = model(normalize(X)) loss = criterion(output, y) val_robust_loss += robust_loss.item() * y.size(0) val_robust_acc += (robust_output.max(1)[1] == y).sum().item() val_loss += loss.item() * y.size(0) val_acc += (output.max(1)[1] == y).sum().item() val_n += y.size(0) if not args.eval: logger.info( '%d \t %.1f \t \t %.1f \t \t %.4f \t %.4f \t %.4f \t %.4f \t \t %.4f \t \t %.4f \t %.4f \t %.4f \t \t %.4f', epoch, train_time - start_time, test_time - train_time, lr, train_loss / train_n, train_acc / train_n, train_robust_loss / train_n, train_robust_acc / train_n, test_loss / test_n, test_acc / test_n, test_robust_loss / test_n, test_robust_acc / test_n) if args.val: logger.info('validation %.4f \t %.4f \t %.4f \t %.4f', val_loss / val_n, val_acc / val_n, val_robust_loss / val_n, val_robust_acc / val_n) if val_robust_acc / val_n > best_val_robust_acc: torch.save( { 'state_dict': model.state_dict(), 'test_robust_acc': test_robust_acc / test_n, 'test_robust_loss': test_robust_loss / test_n, 'test_loss': test_loss / test_n, 'test_acc': test_acc / test_n, 'val_robust_acc': val_robust_acc / val_n, 'val_robust_loss': val_robust_loss / val_n, 'val_loss': val_loss / val_n, 'val_acc': val_acc / val_n, }, os.path.join(args.fname, f'model_val.pth')) best_val_robust_acc = val_robust_acc / val_n # save checkpoint if (epoch + 1) % args.chkpt_iters == 0 or epoch + 1 == epochs: torch.save(model.state_dict(), os.path.join(args.fname, f'model_{epoch}.pth')) torch.save(opt.state_dict(), os.path.join(args.fname, f'opt_{epoch}.pth')) # save best if test_robust_acc / test_n > best_test_robust_acc: torch.save( { 'state_dict': model.state_dict(), 'test_robust_acc': test_robust_acc / test_n, 'test_robust_loss': test_robust_loss / test_n, 'test_loss': test_loss / test_n, 'test_acc': test_acc / test_n, }, os.path.join(args.fname, f'model_best.pth')) best_test_robust_acc = test_robust_acc / test_n else: logger.info( '%d \t %.1f \t \t %.1f \t \t %.4f \t %.4f \t %.4f \t %.4f \t \t %.4f \t \t %.4f \t %.4f \t %.4f \t \t %.4f', epoch, train_time - start_time, test_time - train_time, -1, -1, -1, -1, -1, test_loss / test_n, test_acc / test_n, test_robust_loss / test_n, test_robust_acc / test_n) return
k2 = kk[1,:,:] k3 = kk[2,:,:] k1 = np.around((k1*std[0]+mean[0])*255) k2 = np.around((k2*std[1]+mean[1])*255) k3 = np.around((k3*std[2]+mean[2])*255) r = Image.fromarray(k1).convert('L') g = Image.fromarray(k2).convert('L') b = Image.fromarray(k3).convert('L') raw = Image.merge('RGB', (r, g, b)) raw.save(path+name+'.png') net1 = torch.nn.DataParallel(ResNet18().cuda()).eval() net1.load_state_dict(torch.load('./pytorch_cifar/checkpoint/ckpt4.t7')['net']) net2 = torch.nn.DataParallel(SENet18().cuda()).eval() net2.load_state_dict(torch.load('./pytorch_cifar/checkpoint/ckpt_senet.t7')['net']) net3 = torch.nn.DataParallel(DenseNet121().cuda()).eval() net3.load_state_dict(torch.load('./pytorch_cifar/checkpoint/ckpt_dense.t7')['net']) net4 = torch.nn.DataParallel(ResNet50().cuda()).eval() net4.load_state_dict(torch.load('./pytorch_cifar/checkpoint/ckpt_resnet50.t7')['net']) net5 = torch.nn.DataParallel(VGG('VGG19').cuda()).eval() net5.load_state_dict(torch.load('./pytorch_cifar/checkpoint/ckpt_vgg.t7')['net']) net_dict={'resnet50:'net4} confidences= [50.0] normalize = transforms.Normalize(mean=[0.4914, 0.4822, 0.4465], std=[0.2023, 0.1994, 0.2010]) fo = open('CW/info.txt', 'a') bs = 32 class testdata(Dataset): def __init__(self): self.imgpath_list = sorted(os.listdir('t_sample_testdata'),key=lambda x:int(x[:-6]))
# Training Settings train_batch_size = 4 train_max_epoch = 3 # File Imports from densenet import DenseNet121 from dataset import CheXpertDataset from train import CheXpertTrainer from dataset import dataLoaderTrain, dataLoaderVal, dataLoaderTest CLASS_NAMES = ['No Finding', 'Enlarged Cardiomediastinum', 'Cardiomegaly', 'Lung Opacity', 'Lung Lesion', 'Edema', 'Consolidation', 'Pneumonia', 'Atelectasis', 'Pneumothorax', 'Pleural Effusion', 'Pleural Other', 'Fracture', 'Support Devices'] if __name__ == "__main__": model = DenseNet121(num_classes).cuda() batch, losst, losse = CheXpertTrainer.train(model, dataLoaderTrain, dataLoaderVal, num_classes, train_max_epoch, checkpoint=None) print('Model Trained') losstn = [] for i in range(0, len(losst), 35): losstn.append(np.mean(losst[i:i + 35])) print(losstn) print(losse) lt = losstn[0] + losstn[2] + losstn[3] le = losse[0] + losse[2] + losse[3] batch = [i*35 for i in range(len(lt))] plt.plot(batch, lt, label = "train")
for i in range(num_classes): aucs.append(roc_auc_score(target[:, i], output[:, i])) return aucs if __name__ == '__main__': config = Config() resume = False workers = config.test_workers n_gpu = pytorch_utils.setgpu(config.test_gpus) batch_size = config.test_batch_size_per_gpu * n_gpu test_data = CXR2(config.data_dir, 'test') print('Test sample number: %d' % test_data.size()) net = DenseNet121(num_classes=len(test_data.labels)) checkpoint = torch.load(os.path.join(config.proj_dir, 'checkpoints', 'densenet_024.ckpt')) # must before cuda net.load_state_dict(checkpoint['state_dict']) net = net.cuda() cudnn.benchmark = True net = DataParallel(net).cuda() test_generator = DataGenerator(config, test_data, phase='test') test_loader = DataLoader(test_generator, batch_size=batch_size, shuffle=False, num_workers=workers, pin_memory=True) net.eval() with torch.no_grad():
n_gpu = pytorch_utils.setgpu(config.train_gpus) batch_size = config.train_batch_size_per_gpu * n_gpu epochs = config.epochs base_lr = config.base_lr save_dir = config.proj_dir + 'checkpoints/' if not os.path.exists(save_dir): os.mkdir(save_dir) train_data = CXR2(config.data_dir, 'train') val_data = CXR2(config.data_dir, 'val') print('Train sample number: %d' % train_data.size()) print('Val sample number: %d' % val_data.size()) pos = train_data.get_occurrences() neg = [train_data.size() - x for x in pos] net = DenseNet121(num_classes=len(train_data.labels)) # loss = WeightedBCE(pos, neg) loss = UnWeightedBCE() net = net.cuda() loss = loss.cuda() cudnn.benchmark = True start_epoch = 1 lr = base_lr best_val_loss = float('inf') log_mode = 'w' if resume: checkpoint = torch.load(save_dir + 'densenet_016.ckpt') start_epoch = checkpoint['epoch'] + 1 lr = checkpoint['lr'] best_val_loss = checkpoint['best_val_loss']
input = input.cuda() output = self.net(input) pred = output.data.max(1)[1].cpu().numpy() preds = np.concatenate((preds, pred)) return preds # In[58]: # Builde model # Load model made by myself #net = ResNet50(num_classes=len(class_index)) # Pretrained model #net = torchvision.models.densenet121(pretrained=True) net = DenseNet121() #net.classifier = nn.Sequential( # nn.Dropout(0.5), # nn.Linear(1024, 512, True), # nn.BatchNorm1d(512), # nn.ReLU(), # nn.Dropout(0.5), # nn.Linear(512, 256, True), # nn.BatchNorm1d(256), # nn.ReLU(), # nn.Linear(256, len(class_index)) #) model = MyModel(net) # In[59]:
idx, capacity_of_gallery, feat.shape)) idx_filename[idx] = '{}'.format(img_f) feats.append(feat.ravel().tolist()) idx += 1 print('[INFO] finish extracting features') with open('feats_%s.pkl' % CLS_NAME, mode='wb') as f: pickle.dump(np.array(feats).astype('float32'), f) with open('idx_%s.pkl' % CLS_NAME, mode='wb') as f: pickle.dump(idx_filename, f) if __name__ == '__main__': densenet121 = DenseNet121(num_cls=161) state_dict = torch.load( '/data/lucasxu/ModelZoo/DenseNet121_%s_Embedding_CenterLoss.pth' % CLS_NAME) try: from collections import OrderedDict new_state_dict = OrderedDict() for k, v in state_dict.items(): name = k[7:] # remove `module.` new_state_dict[name] = v densenet121.load_state_dict(new_state_dict) except: densenet121.load_state_dict(state_dict)
print('[INFO] {0}/{1} extracting deep features, feat size = {2}, latency = {3}'.format(idx, capacity_of_gallery, feat.shape, tok - tik)) idx_filename[idx] = '{}'.format(img_f) feats.append(feat.ravel().tolist()) idx += 1 print('[INFO] finish extracting features') with open('feats_%s.pkl'%CLS_NAME, mode='wb') as f: pickle.dump(np.array(feats).astype('float32'), f) with open('idx_%s.pkl'%CLS_NAME, mode='wb') as f: pickle.dump(idx_filename, f) if __name__ == '__main__': densenet121 = DenseNet121(num_cls=362) state_dict = torch.load('/data/lucasxu/ModelZoo/DenseNet121_{}_Embedding_ASoftmaxLoss.pth'.format(CLS_NAME)) try: from collections import OrderedDict new_state_dict = OrderedDict() for k, v in state_dict.items(): name = k[7:] # remove `module.` new_state_dict[name] = v densenet121.load_state_dict(new_state_dict) except: densenet121.load_state_dict(state_dict) ext_feats_in_dir(densenet121, '/data/lucasxu/Dataset/{}Crops/{}'.format(CLS_NAME, CLS_NAME))