def __init__(self, arch, pretrained=True): super().__init__() # load EfficientNet if arch == 'se_resnext50_32x4d': if pretrained: self.base = se_resnext50_32x4d() else: self.base = se_resnext50_32x4d(pretrained=None) self.nc = self.base.last_linear.in_features elif arch == 'inceptionv4': if pretrained: self.base = inceptionv4() else: self.base = inceptionv4(pretrained=None) self.nc = self.base.last_linear.in_features elif arch == 'inceptionresnetv2': if pretrained: self.base = inceptionresnetv2() else: self.base = inceptionresnetv2(pretrained=None) self.nc = self.base.last_linear.in_features elif 'efficientnet' in arch: if pretrained: self.base = EfficientNet.from_pretrained(model_name=arch) else: self.base = EfficientNet.from_name(model_name=arch) self.nc = self.base._fc.in_features self.logit = nn.Sequential(AdaptiveConcatPool2d(1), Flatten(), nn.BatchNorm1d(2 * self.nc), nn.Dropout(0.5), nn.Linear(2 * self.nc, 512), Mish(), nn.BatchNorm1d(512), nn.Dropout(0.5), nn.Linear(512, 1))
]) trainset = torchvision.datasets.ImageFolder(root + 'train', transform=train_transform) trainloader = torch.utils.data.DataLoader(trainset, batch_size=batch_size, shuffle=True, num_workers=32) testset = torchvision.datasets.ImageFolder(root + 'val', transform=val_transform) testloader = torch.utils.data.DataLoader(testset, batch_size=batch_size, shuffle=False, num_workers=8) model = EfficientNet.from_name('efficientnet-b0') model = nn.DataParallel(model) model.cuda() # checkpoint = torch.load('/home/root1/PycharmProjects/ptt/checkpoint/69/box-can_0_0.8873.torch') # model.load_state_dict(checkpoint['net']) # del checkpoint criterion = nn.CrossEntropyLoss().cuda() optimizer = torch.optim.SGD(model.parameters(), 1e-2, momentum=0.9, weight_decay=1e-5) lr_schudule = ExponentialLR(optimizer, 0.97) def train(train_loader, model, criterion, optimizer):
transform=train_transform) trainloader = torch.utils.data.DataLoader(trainset, batch_size=batch_size, shuffle=True, num_workers=2) testset = torchvision.datasets.CIFAR10(root=root, train=False, download=True, transform=transform) testloader = torch.utils.data.DataLoader(testset, batch_size=batch_size, shuffle=False, num_workers=2) model = EfficientNet.from_name('efficientnet-b0', first_stride=False, ktype='oneone', cifar=False) model._fc = nn.Linear(model._fc.in_features, 10) model = nn.DataParallel(model) model.cuda() # checkpoint = torch.load('/home/root1/PycharmProjects/ptt/checkpoint/69/box-can_0_0.8873.torch') # model.load_state_dict(checkpoint['net']) # del checkpoint criterion = nn.CrossEntropyLoss().cuda() optimizer = torch.optim.SGD(model.parameters(), init_lr, momentum=0.9, weight_decay=1e-5) lr_schudule = MultiStepLR(optimizer, lr_step)
from sklearn.metrics import accuracy_score, roc_auc_score from tqdm import tqdm import time from PIL import ImageFile ImageFile.LOAD_TRUNCATED_IMAGES = True opt = TestOptions().parse(print_options=False) print("{} from {} model testing on {}".format(opt.arch, opt.source_dataset, opt.target_dataset)) gpu_id = opt.gpu_id os.environ['CUDA_VISIBLE_DEVICES'] = str(gpu_id) use_cuda = torch.cuda.is_available() print("GPU device %d:" % (gpu_id), use_cuda) model = EfficientNet.from_name(opt.arch, num_classes=opt.classes) if opt.resume: pretrained = opt.resume print("=> using pre-trained model '{}'".format(pretrained)) model.load_state_dict(torch.load(pretrained)['state_dict']) model.to('cuda') cudnn.benchmark = True print('Total params: %.2fM' % (sum(p.numel() for p in model.parameters()) / 1000000.0)) criterion = nn.CrossEntropyLoss().cuda() optimizer = optim.SGD(model.parameters(), lr=opt.lr, momentum=opt.momentum)