def __init__(self, root, config, crop_size, scale_size, baseline=False): super(MultiTaskClsDataSet, self).__init__() self.root = root self.config = config self.crop_size = crop_size self.scale_size = scale_size self.baseline = baseline df = pd.DataFrame.from_csv(config) self.images_list = [] for index, row in df.iterrows(): self.images_list.append(row) with open('info.json', 'r') as fp: info = json.load(fp) mean_values = torch.from_numpy(np.array(info['mean'], dtype=np.float32) / 255) std_values = torch.from_numpy(np.array(info['std'], dtype=np.float32) / 255) eigen_values = torch.from_numpy(np.array(info['eigval'], dtype=np.float32)) eigen_vectors = torch.from_numpy(np.array(info['eigvec'], dtype=np.float32)) if baseline: self.transform = transforms.Compose([ transforms.RandomCrop(crop_size), transforms.RandomHorizontalFlip(), transforms.ToTensor(), transforms.Normalize(mean=mean_values, std=std_values), ]) else: self.transform = transforms.Compose([ transforms.RandomCrop(crop_size), transforms.RandomHorizontalFlip(), PILColorJitter(), transforms.ToTensor(), Lighting(alphastd=0.01, eigval=eigen_values, eigvec=eigen_values), transforms.Normalize(mean=mean_values, std=std_values), ])
def __init__(self, crop_size, scale_size, baseline): super(kaggleClsTrain1, self).__init__() self.image = ['data/kaggle1/train_images/train/' + line.strip() + '_' + str(scale_size) + '.png' for line in open('data/kaggle1/train_images/train/train_images.txt', 'r')] self.label = torch.from_numpy(np.array(np.loadtxt('data/kaggle1/train_images/train/train_labels.txt'), np.int)) with open('data/kaggle/info.json', 'r') as fp: info = json.load(fp) mean_values = torch.from_numpy(np.array(info['mean'], dtype=np.float32) / 255) std_values = torch.from_numpy(np.array(info['std'], dtype=np.float32) / 255) eigen_values = torch.from_numpy(np.array(info['eigval'], dtype=np.float32)) eigen_vectors = torch.from_numpy(np.array(info['eigvec'], dtype=np.float32)) if baseline: self.transform = transforms.Compose([ transforms.RandomCrop(crop_size), transforms.RandomHorizontalFlip(), transforms.ToTensor(), transforms.Normalize(mean=mean_values, std=std_values), ]) else: self.transform = transforms.Compose([ transforms.RandomSizedCrop(crop_size), transforms.RandomHorizontalFlip(), PILColorJitter(), transforms.ToTensor(), #ColorJitter(), Lighting(alphastd=0.1, eigval=eigen_values, eigvec=eigen_vectors), #Affine(rotation_range=180, translation_range=None, shear_range=None, zoom_range=None), transforms.Normalize(mean=mean_values, std=std_values), ])
def main(): global args, best_err1, best_err5, start_epoch args = parser.parse_args() test_id = args.net_type + '_' + str(args.depth) + '_' + args.dataset csv_path = 'logs/' csv_path = csv_path + test_id + '/' checkpoint_path = 'runs/' if not os.path.exists(csv_path): os.makedirs(csv_path) if not os.path.exists(checkpoint_path): os.makedirs(checkpoint_path) if args.dataset.startswith('cifar'): # Preprocessing normalize = transforms.Normalize( mean=[x / 255.0 for x in [125.3, 123.0, 113.9]], std=[x / 255.0 for x in [63.0, 62.1, 66.7]]) transform_train = transforms.Compose([ transforms.RandomCrop(32, padding=4), transforms.RandomHorizontalFlip(), transforms.ToTensor(), normalize, ]) transform_test = transforms.Compose([transforms.ToTensor(), normalize]) # Dataloader if args.dataset == 'cifar100': train_loader = torch.utils.data.DataLoader( datasets.CIFAR100('./data', train=True, download=True, transform=transform_train), batch_size=args.batch_size, shuffle=True, num_workers=args.workers, pin_memory=True) val_loader = torch.utils.data.DataLoader( datasets.CIFAR100('./data', train=False, transform=transform_test), batch_size=args.batch_size, shuffle=True, num_workers=args.workers, pin_memory=True) numberofclass = 100 elif args.dataset == 'cifar10': train_loader = torch.utils.data.DataLoader( datasets.CIFAR10('./data', train=True, download=True, transform=transform_train), batch_size=args.batch_size, shuffle=True, num_workers=args.workers, pin_memory=True) val_loader = torch.utils.data.DataLoader( datasets.CIFAR10('./data', train=False, transform=transform_test), batch_size=args.batch_size, shuffle=True, num_workers=args.workers, pin_memory=True) numberofclass = 10 else: raise Exception('unknown dataset: {}'.format(args.dataset)) elif args.dataset == 'tiny': # Image path traindir = os.path.join('../../imagenet/train') valdir = os.path.join('../../imagenet/val') # Preprocessing normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) jittering = ColorJitter(brightness=0.4, contrast=0.4, saturation=0.4) lighting = Lighting(alphastd=0.1, eigval=[0.2175, 0.0188, 0.0045], eigvec=[[-0.5675, 0.7192, 0.4009], [-0.5808, -0.0045, -0.8140], [-0.5836, -0.6948, 0.4203]]) # Dataloader train_dataset = datasets.ImageFolder( traindir, transforms.Compose([ transforms.RandomResizedCrop(64), transforms.RandomHorizontalFlip(), transforms.ToTensor(), jittering, lighting, normalize, ])) train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=args.batch_size, shuffle=True, num_workers=args.workers, pin_memory=True) val_loader = torch.utils.data.DataLoader(datasets.ImageFolder( valdir, transforms.Compose([ transforms.Resize(64), transforms.CenterCrop(64), transforms.ToTensor(), normalize, ])), batch_size=args.batch_size, shuffle=False, num_workers=args.workers, pin_memory=True) numberofclass = 200 else: raise Exception('unknown dataset: {}'.format(args.dataset)) print("=> creating model '{}'".format(args.net_type)) if args.net_type == 'resnet': model = RN.ResNet(args.dataset, args.depth, numberofclass, args.bottleneck) # for ResNet elif args.net_type == 'wide_resnet': model = WN.WideResNet(args.dataset, depth=28, num_classes=numberofclass, widen_factor=10, dropRate=0.3) else: raise Exception('unknown network architecture: {}'.format( args.net_type)) #model = torch.nn.DataParallel(model).cuda() model = model.cuda() optimizer = torch.optim.SGD(model.parameters(), args.lr, momentum=args.momentum, weight_decay=args.weight_decay, nesterov=True) if args.resume: checkpoint_path = checkpoint_path + csv_path checkpoint = torch.load(checkpoint_path + 'model_best.pth.tar') model.load_state_dict(checkpoint['state_dict']) optimizer.load_state_dict(checkpoint['optimizer']) start_epoch = checkpoint['epoch'] + 1 filename = csv_path + 'log.csv' csv_logger = CSVLogger(csv_path, args=args, fieldnames=[ 'epoch', 'train_loss', 'train_err1', 'test_loss', 'test_err1', 'test_err5' ], filename=filename) else: start_epoch = 0 filename = csv_path + '/' + test_id + '.csv' csv_logger = CSVLogger(csv_path, args=args, fieldnames=[ 'epoch', 'train_loss', 'train_err1', 'test_loss', 'test_err1', 'test_err5' ], filename=filename) print(model) print('the number of model parameters: {}'.format( sum([p.data.nelement() for p in model.parameters()]))) # define loss function (criterion) and optimizer criterion = nn.CrossEntropyLoss().cuda() for epoch in range(start_epoch, start_epoch + args.epochs): adjust_learning_rate(args, optimizer, epoch) # train for one epoch train_loss, train_err1 = train(train_loader, model, criterion, optimizer, epoch) # evaluate on validation set err1, err5, val_loss = validate(val_loader, model, criterion, epoch) # loss train_loss = '%.4f' % (train_loss) train_err1 = '%.4f' % (train_err1) val_loss = '%.4f' % (val_loss) test_err1 = '%.4f' % (err1) test_err5 = '%.4f' % (err5) # remember best prec@1 and save checkpoint is_best = err1 <= best_err1 best_err1 = min(err1, best_err1) if is_best: best_err5 = err5 print('Current best error (top-1 and 5 error):', best_err1, best_err5) save_checkpoint( { 'epoch': epoch, 'arch': args.net_type, 'state_dict': model.state_dict(), 'best_err1': best_err1, 'best_err5': best_err5, 'optimizer': optimizer.state_dict(), }, is_best, test_id) row = { 'epoch': str(epoch), 'train_loss': str(train_loss), 'train_err1': str(train_err1), 'test_loss': str(val_loss), 'test_err1': str(test_err1), 'test_err5': str(test_err5) } csv_logger.writerow(row) print('Best accuracy (top-1 and 5 error):', best_err1, best_err5) csv_logger.close()
def get_loaders(root, batch_size, resolution, num_workers=32, val_batch_size=200, prefetch=False, color_jitter=0.4, pca=False, crop_pct=0.875): normalize = transforms.Normalize(mean=IMAGENET_DEFAULT_MEAN, std=IMAGENET_DEFAULT_STD) scale_size = int(math.floor(resolution / crop_pct)) transform_train = [] transform_eval = [] transform_train += [ transforms.RandomResizedCrop(resolution, interpolation=InterpolationMode.BICUBIC), transforms.RandomHorizontalFlip(), transforms.ColorJitter(*(color_jitter, color_jitter, color_jitter)), ] transform_eval += [ transforms.Resize(scale_size, interpolation=InterpolationMode.BICUBIC), transforms.CenterCrop(resolution), ] if not prefetch: transform_train += [transforms.ToTensor()] if pca: transform_train += [ Lighting(0.1, IMAGENET_PCA['eigval'], IMAGENET_PCA['eigvec']) ] transform_train += [ normalize, ] transform_eval += [ transforms.ToTensor(), normalize, ] else: transform_train += [ToNumpy()] transform_eval += [ToNumpy()] transform_train = transforms.Compose(transform_train) transform_eval = transforms.Compose(transform_eval) train_dataset = ImageFolder(root + "/train", transform_train) train_sampler = torch.utils.data.distributed.DistributedSampler( train_dataset) val_dataset = ImageFolder(root + "/val", transform_eval) collate_fn = fast_collate if prefetch else torch.utils.data.dataloader.default_collate train_loader = DataLoader(train_dataset, batch_size=batch_size, shuffle=(train_sampler is None), num_workers=num_workers, pin_memory=True, sampler=train_sampler, collate_fn=collate_fn, persistent_workers=True) val_loader = DataLoader(val_dataset, batch_size=val_batch_size, shuffle=False, num_workers=num_workers, pin_memory=True, collate_fn=collate_fn, persistent_workers=True) if prefetch: train_loader = PrefetchLoader(train_loader) val_loader = PrefetchLoader(val_loader) return train_loader, val_loader