def __call__(self, images, gts, w, h): if random.uniform(0., 1.) < self.p: i, j, new_h, new_w = self.get_params(w, h, self.scale, self.ratio) images = [ transforms.functional.resized_crop(image, i, j, new_h, new_w, self.size, self.interpolation) for image in images ] # gt transform offset = np.array([j, i, j, i, 0]) scale = np.array([ self.size[0] / new_w, self.size[1] / new_h, self.size[0] / new_w, self.size[1] / new_h, 1 ]) new_fullframe = np.array([0, 0, self.size[0], self.size[1]]) for i, frame in enumerate(gts): new_frame = (frame - offset) * scale # remove bbox out of the frame inside_mask = overlap_numpy(new_frame[:, :4], new_fullframe) - self.threshold inside_mask = inside_mask > 0. cleared_frame = np.compress(inside_mask, new_frame, axis=0) if len(cleared_frame) == 0: cleared_frame = np.array( [[0., 0., self.size[0], self.size[1], 0.]], dtype=float) # clip into the frame cleared_frame = cleared_frame.reshape(5, -1) np.clip(cleared_frame[0], 0., self.size[0], out=cleared_frame[0]) np.clip(cleared_frame[2], 0., self.size[0], out=cleared_frame[2]) np.clip(cleared_frame[1], 0., self.size[1], out=cleared_frame[1]) np.clip(cleared_frame[3], 0., self.size[1], out=cleared_frame[3]) cleared_frame = cleared_frame.reshape(-1, 5) gts[i] = cleared_frame else: # fall back to simple resize images = transforms.Lambda(lambda frames: [ transforms.Resize(self.size)(frame) for frame in frames ])(images) w_ratio = float(self.size[0]) / w h_ratio = float(self.size[1]) / h ratio = np.array([w_ratio, h_ratio, w_ratio, h_ratio, 1.], dtype=np.float32) for i, frame in enumerate(gts): new_frame = frame * ratio gts[i] = new_frame return images, gts, w, h
print('Best val Acc: {:4f}'.format(best_acc)) model.load_state_dict(best_model_wts) return model, val_acc_history crop_size = 44 transform_train = transforms.Compose([ transforms.RandomCrop(crop_size), transforms.RandomHorizontalFlip(), transforms.ToTensor() ]) transform_test = transforms.Compose([ transforms.TenCrop(crop_size), transforms.Lambda(lambda crops: torch.stack( [transforms.ToTensor()(crop) for crop in crops])) ]) if args.checkpoint is None: start_epoch = 0 model = VGG(args.model_name) optimizer = optim.SGD(model.parameters(), lr=args.lr, momentum=0.9, weight_decay=5e-4) else: checkpoint = torch.load(args.checkpoint) start_epoch = checkpoint['epoch'] + 1 print('\nLoaded checkpoint from epoch %d.\n' % start_epoch) model = VGG(args.model_name) model.load_state_dict(checkpoint["model_weights"])
VALIDATION_IMAGE_LIST, transform=transform_val) validation_dataloader = DataLoader(dataset=validation_dataset, batch_size=BATCH_SIZE, shuffle=False, num_workers=4, pin_memory=True, drop_last=True) test_dataset = ChestXrayDataSetMultiLabel( data_dir=DATA_DIR, image_list_file=TEST_IMAGE_LIST, transform=transforms.Compose([ transforms.Resize(256), transforms.TenCrop(224), transforms.Lambda(lambda crops: torch.stack( [transforms.ToTensor()(crop) for crop in crops])), transforms.Lambda(lambda crops: torch.stack([ transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]) (crop) for crop in crops ])) ])) test_dataloader = DataLoader(dataset=test_dataset, batch_size=BATCH_SIZE, shuffle=False, num_workers=4, pin_memory=True, drop_last=True) # cached_m = torch.load('/Users/haigangliu/Desktop/ML_model_cache/multiclass/multi_class_.pth.tar')
(len(dataset_list), len(activation_function_list), num_epochs)) validation_loss_array = np.zeros( (len(dataset_list), len(activation_function_list), num_epochs)) test_accuracy_array = np.zeros( (len(dataset_list), len(activation_function_list))) test_batch_size = 1000 num_parameters = np.zeros((len(activation_function_list))) criterion = nn.CrossEntropyLoss() for index_dataset, (dataset, dataset_name, train_range, validation_range, test_range, mean_std) in enumerate( zip(dataset_list, dataset_name_list, train_range_list, validation_range_list, test_range_list, mean_std_list)): transform = transforms.Compose([ transforms.ToTensor(), transforms.Lambda(lambda x: torch.cat([x, x, x], 0)), transforms.Normalize(mean_std[0], mean_std[1]) ]) train_dataset = dataset('tmp', train=True, transform=transform, download=True) test_dataset = dataset('tmp', train=False, transform=transform, download=True) train_dataloader = DataLoader(train_dataset, batch_size=batch_size, sampler=SubsetRandomSampler(train_range)) validation_dataloader = DataLoader( train_dataset,
PATH = 'squeezenet_model_2.pt' # Parametaers batch_size = 100 learning_rate = 0.001 num_epochs = 1 momentum = 0.5 transform = transforms.Compose([ transforms.Resize(224), transforms.ToTensor(), transforms.Lambda(lambda x: x.expand(3, -1, -1)) ]) # Data load dataset_train = datasets.MNIST('data', train=True, download=True, transform=transform) dataset_test = datasets.MNIST('data', train=False, download=True, transform=transform) train_loader = utils.data.DataLoader(dataset_train, batch_size=batch_size,
def get_mnist_train_valid_loader(data_dir, batch_size, random_seed, valid_size=0.2, shuffle=True, show_sample=False, num_workers=1, pin_memory=True): """ Utility function for loading and returning train and valid multi-process iterators over the MNIST dataset. A sample 9x9 grid of the images can be optionally displayed. If using CUDA, num_workers should be set to 1 and pin_memory to True. Params ------ - data_dir: path directory to the dataset. - batch_size: how many samples per batch to load. - augment: whether to apply the data augmentation scheme mentioned in the paper. Only applied on the train split. - random_seed: fix seed for reproducibility. - valid_size: percentage split of the training set used for the validation set. Should be a float in the range [0, 1]. - shuffle: whether to shuffle the train/validation indices. - show_sample: plot 9x9 sample grid of the dataset. - num_workers: number of subprocesses to use when loading the dataset. - pin_memory: whether to copy tensors into CUDA pinned memory. Set it to True if using GPU. Returns ------- - train_loader: training set iterator. - valid_loader: validation set iterator. """ error_msg = "[!] valid_size should be in the range [0, 1]." assert ((valid_size >= 0) and (valid_size <= 1)), error_msg # define transforms transform = transforms.Compose([ transforms.Pad(2), transforms.ToTensor(), transforms.Lambda(lambda x: x.repeat(3,1,1)), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))]) # load the dataset train_dataset = datasets.MNIST(root=data_dir, train=True, download=True, transform=transform) valid_dataset = datasets.MNIST(root=data_dir, train=True, download=True, transform=transform) num_train = len(train_dataset) indices = list(range(num_train)) split = int(np.floor(valid_size * num_train)) if shuffle == True: np.random.seed(random_seed) np.random.shuffle(indices) train_idx, valid_idx = indices[split:], indices[:split] train_sampler = SubsetRandomSampler(train_idx) valid_sampler = SubsetRandomSampler(valid_idx) train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=batch_size, sampler=train_sampler, num_workers=num_workers, pin_memory=pin_memory) valid_loader = torch.utils.data.DataLoader(valid_dataset, batch_size=1, sampler=valid_sampler, num_workers=num_workers, pin_memory=pin_memory) # visualize some images if show_sample: sample_loader = torch.utils.data.DataLoader(train_dataset, batch_size=9, shuffle=shuffle, num_workers=num_workers, pin_memory=pin_memory) data_iter = iter(sample_loader) images, labels = data_iter.next() X = images.numpy() plot_images(X, labels) return (train_loader, valid_loader) def get_svhn_train_valid_loader(data_dir, batch_size, random_seed, valid_size=0.2, shuffle=True, show_sample=False, num_workers=1, pin_memory=True): """ Utility function for loading and returning train and valid multi-process iterators over the MNIST dataset. A sample 9x9 grid of the images can be optionally displayed. If using CUDA, num_workers should be set to 1 and pin_memory to True. Params ------ - data_dir: path directory to the dataset. - batch_size: how many samples per batch to load. - augment: whether to apply the data augmentation scheme mentioned in the paper. Only applied on the train split. - random_seed: fix seed for reproducibility. - valid_size: percentage split of the training set used for the validation set. Should be a float in the range [0, 1]. - shuffle: whether to shuffle the train/validation indices. - show_sample: plot 9x9 sample grid of the dataset. - num_workers: number of subprocesses to use when loading the dataset. - pin_memory: whether to copy tensors into CUDA pinned memory. Set it to True if using GPU. Returns ------- - train_loader: training set iterator. - valid_loader: validation set iterator. """ error_msg = "[!] valid_size should be in the range [0, 1]." assert ((valid_size >= 0) and (valid_size <= 1)), error_msg # define transforms transform = transforms.Compose([ transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))]) # load the dataset train_dataset = datasets.SVHN(root=data_dir, split='train', download=True, transform=transform) valid_dataset = datasets.SVHN(root=data_dir, split='test', download=True, transform=transform) num_train = len(train_dataset) indices = list(range(num_train)) split = int(np.floor(valid_size * num_train)) if shuffle == True: np.random.seed(random_seed) np.random.shuffle(indices) train_idx, valid_idx = indices[split:], indices[:split] train_sampler = SubsetRandomSampler(train_idx) valid_sampler = SubsetRandomSampler(valid_idx) train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=batch_size, sampler=train_sampler, num_workers=num_workers, pin_memory=pin_memory) valid_loader = torch.utils.data.DataLoader(valid_dataset, batch_size=1, sampler=valid_sampler, num_workers=num_workers, pin_memory=pin_memory) # visualize some images if show_sample: sample_loader = torch.utils.data.DataLoader(train_dataset, batch_size=9, shuffle=shuffle, num_workers=num_workers, pin_memory=pin_memory) data_iter = iter(sample_loader) images, labels = data_iter.next() X = images.numpy() plot_images(X, labels) return (train_loader, valid_loader)
def main(): args = parser.parse_args() # create model print("Training model with backbone", args.backbone) model = make_backbone(args.dim, args.backbone) # SJH: uncomment to run on a selected GPU # b = True if args.backbone == "mobilenet": # mobilenet torch.cuda.set_device(args.gpu) b = False model = model.cuda() # SJH: uncomment to run on multiple GPUs - works for ResNet not MobileNet if b is True: # false for mobilenet model = torch.nn.DataParallel(model) # define loss function (criterion) and optimizer criterion = loss.SoftTriple(args.la, args.gamma, args.tau, args.margin, args.dim, args.C, args.K).cuda() optimizer = torch.optim.Adam([{ "params": model.parameters(), "lr": args.modellr }, { "params": criterion.parameters(), "lr": args.centerlr }], eps=args.eps, weight_decay=args.weight_decay) cudnn.benchmark = True # load data traindir = os.path.join(args.data, 'train') testdir = os.path.join(args.data, 'test') # Use this with BN-Inception if args.backbone == "BN-Inception": normalize = transforms.Normalize(mean=[104., 117., 128.], std=[1., 1., 1.]) # SJH for ResNet and EfficientNet else: normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) if args.backbone == "BN-Inception": input_dim_resize = 256 input_dim_crop = 224 train_dataset = datasets.ImageFolder( traindir, transforms.Compose([ transforms.Lambda(RGB2BGR), # SJH BN-Inception is BGR # SJH was 224 for bn-inception and mobilenet and 299 for pytorch inception transforms.RandomResizedCrop(input_dim_crop), transforms.RandomHorizontalFlip(), transforms.ToTensor(), # SJH BN-Inception needs scale transforms.Lambda(lambda x: x.mul(255)), normalize, ])) else: input_dim_resize = 1024 input_dim_crop = 598 # input_dim_resize = 512 # input_dim_crop = 299 # TODO: investigate this? # For EfficientNet with advprop # normalize = transforms.Lambda(lambda img: img * 2.0 - 1.0) train_dataset = datasets.ImageFolder( traindir, transforms.Compose([ transforms.RandomResizedCrop(input_dim_crop), transforms.RandomHorizontalFlip(), transforms.ToTensor(), normalize, ])) train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=args.batch_size, shuffle=True, num_workers=args.workers, pin_memory=True) if args.backbone == "BN-Inception": test_loader = torch.utils.data.DataLoader( datasets.ImageFolder( testdir, transforms.Compose([ transforms.Lambda(RGB2BGR), transforms.Resize(input_dim_resize), transforms.CenterCrop(input_dim_crop), transforms.ToTensor(), # SJH BN-Inception needs scale transforms.Lambda(lambda x: x.mul(255)), normalize, ])), batch_size=args.batch_size, shuffle=False, num_workers=args.workers, pin_memory=True) else: test_loader = torch.utils.data.DataLoader(datasets.ImageFolder( testdir, transforms.Compose([ transforms.Resize(input_dim_resize), transforms.CenterCrop(input_dim_crop), transforms.ToTensor(), normalize, ])), batch_size=args.batch_size, shuffle=False, num_workers=args.workers, pin_memory=True) best_nmi = 0 for epoch in range(args.start_epoch, args.epochs): print('Training in Epoch[{}]'.format(epoch)) adjust_learning_rate(optimizer, epoch, args) # train for one epoch train(train_loader, model, criterion, optimizer, args) # Run validation nmi, recall = validate(test_loader, model, args) print( 'Recall@1, 2, 4, 8: {recall[0]:.3f}, {recall[1]:.3f}, {recall[2]:.3f}, {recall[3]:.3f}; NMI: {nmi:.3f} \n' .format(recall=recall, nmi=nmi)) # Save the best model if nmi > best_nmi: best_nmi = nmi print("Saving new best model!") fn = "{}.pth".format(f"best_model_{epoch}_eflite") torch.save(model, fn) print("Model saved to", fn) # evaluate on validation set nmi, recall = validate(test_loader, model, args) print( 'Recall@1, 2, 4, 8: {recall[0]:.3f}, {recall[1]:.3f}, {recall[2]:.3f}, {recall[3]:.3f}; NMI: {nmi:.3f} \n' .format(recall=recall, nmi=nmi)) # Save the model print("Saving model!") fn = "{}.pth".format("last_model_eflite") torch.save(model, fn) print("Model saved to", fn)
from torchvision import transforms import numpy as np import pandas as pd from time import time from models.mtcnn import MTCNN, prewhiten from models.inception_resnet_v1 import InceptionResnetV1 trans = transforms.Compose([ transforms.Resize(512), np.int_, transforms.ToTensor(), torch.Tensor.byte ]) trans_cropped = transforms.Compose([ np.array, torch.tensor, torch.Tensor.float, transforms.Lambda(lambda x: x.permute(2, 0, 1)), prewhiten ]) def get_image(path, trans): img = Image.open(path) img = trans(img) return img mtcnn_pt = MTCNN() resnet_pt = InceptionResnetV1(pretrained='vggface2').eval() names = [ 'bradley_cooper', 'shea_whigham', 'paul_rudd', 'kate_siegel', 'angelina_jolie'
base_save_path = '%s_%s_cellSize%d/'%(backbone_type, opt_name, S) if not os.path.exists(base_save_path): os.makedirs(base_save_path) log_name = 'train' logger = create_logger(base_save_path, log_name) my_vis = Visual(base_save_path[:-1]) # backbone_net_p.load_state_dict(torch.load('densenet_sgd_S7_yolo.pth')) lossLayer = YOLOLossV1(batch_size, S, B, clsN, lbd_coord, lbd_no_obj, _logger=logger, _vis=my_vis) backbone_net_p.train() transform = transforms.Compose([ transforms.Lambda(cv_resize), transforms.ToTensor(), transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]), ]) data_base = 'datasets/' train_data_name = data_base + 'train.txt' # test_data_name = '2007_train.txt' test_data_name = data_base + '2007_test.txt' train_dataset = yoloDataset(list_file=train_data_name, train=True, transform = transform, device=device, little_train=False, S=S) train_loader = DataLoader(train_dataset,batch_size=batch_size,shuffle=True, num_workers=4) test_dataset = yoloDataset(list_file=test_data_name,train=False,transform = transform, device=device, little_train=False, with_file_path=True, S=S) test_loader = DataLoader(test_dataset,batch_size=batch_size,shuffle=False)#, num_workers=4)
import src.dataloader import src.train3d # - # ## Create Dataset and DataLoader # + import src.dataloader import importlib importlib.reload(src.dataloader) augmentor = transforms.Compose([ transforms.Lambda(lambda x: x.repeat(3, 1, 1, 1).permute(3, 0, 1, 2)), src.dataloader.Resize(256), ]) path = '/data/larson2/RCC_dl/new/clear_cell' train_ds = src.dataloader.RCCDataset_h5(path, mode='train', transform=augmentor) train_loader = DataLoader(train_ds, batch_size=1, shuffle=True, num_workers=4, drop_last=False) val_ds = src.dataloader.RCCDataset_h5(path, mode='val', transform=augmentor) val_loader = DataLoader(val_ds, batch_size=1, shuffle=True, num_workers=4, drop_last=False) print(train_ds[0][0].shape) print(val_ds[0][0].shape)
######################################################################################## torch.manual_seed(args.seed) if args.cuda and torch.cuda.is_available(): torch.cuda.manual_seed(args.seed) device = torch.device('cuda') else: device = torch.device('cpu') ######################################################################################## # LOAD DATA ######################################################################################## transform = trans.Compose( [trans.ToTensor(), trans.Lambda(lambda x: x.view(-1, args.input_size))]) # Load data data_path, batch_size = args.data, args.batch_size train_data = dataset.MNIST(root=data_path, train=True, transform=transform, download=True) # Split train data into training and validation sets N = len(train_data) val_size = int(N * 0.2) train_data, validation_data = torch.utils.data.random_split( train_data, [N - val_size, val_size])
def main(): global args args = parser.parse_args() #fix random seeds torch.manual_seed(args.seed) torch.cuda.manual_seed_all(args.seed) np.random.seed(args.seed) best_prec1 = 0 # load model model = load_model(args.model) model.cuda() cudnn.benchmark = True # freeze the features layers for param in model.features.parameters(): param.requires_grad = False # define loss function (criterion) and optimizer criterion = nn.CrossEntropyLoss().cuda() # data loading code traindir = os.path.join(args.data, 'train') valdir = os.path.join(args.data, 'val') normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) if args.tencrops: transformations_val = [ transforms.Resize(256), transforms.TenCrop(224), transforms.Lambda(lambda crops: torch.stack( [normalize(transforms.ToTensor()(crop)) for crop in crops])), ] else: transformations_val = [ transforms.Resize(256), transforms.CenterCrop(224), transforms.ToTensor(), normalize ] transformations_train = [ transforms.Resize(256), transforms.CenterCrop(256), transforms.RandomCrop(224), transforms.RandomHorizontalFlip(), transforms.ToTensor(), normalize ] train_dataset = datasets.ImageFolder( traindir, transform=transforms.Compose(transformations_train)) val_dataset = datasets.ImageFolder( valdir, transform=transforms.Compose(transformations_val)) 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(val_dataset, batch_size=int(args.batch_size / 2), shuffle=False, num_workers=args.workers) # logistic regression reglog = RegLog(args.conv, len(train_dataset.classes)).cuda() optimizer = torch.optim.SGD(filter(lambda x: x.requires_grad, reglog.parameters()), args.lr, momentum=args.momentum, weight_decay=10**args.weight_decay) # create logs exp_log = os.path.join(args.exp, 'log') if not os.path.isdir(exp_log): os.makedirs(exp_log) loss_log = Logger(os.path.join(exp_log, 'loss_log')) prec1_log = Logger(os.path.join(exp_log, 'prec1')) prec5_log = Logger(os.path.join(exp_log, 'prec5')) for epoch in range(args.epochs): end = time.time() # train for one epoch train(train_loader, model, reglog, criterion, optimizer, epoch) # evaluate on validation set prec1, prec5, loss = validate(val_loader, model, reglog, criterion) loss_log.log(loss) prec1_log.log(prec1) prec5_log.log(prec5) # remember best prec@1 and save checkpoint is_best = prec1 > best_prec1 best_prec1 = max(prec1, best_prec1) if is_best: filename = 'model_best.pth.tar' else: filename = 'checkpoint.pth.tar' torch.save( { 'epoch': epoch + 1, 'arch': 'alexnet', 'state_dict': model.state_dict(), 'prec5': prec5, 'best_prec1': best_prec1, 'optimizer': optimizer.state_dict(), }, os.path.join(args.exp, filename))
def __init__(self, root: str, normal_class: int, preproc: str, nominal_label: int, supervise_mode: str, noise_mode: str, oe_limit: int, online_supervision: bool, logger: Logger = None): """ AD dataset for Cifar-10. :param root: root directory where data is found or is to be downloaded to :param normal_class: the class considered nominal :param preproc: the kind of preprocessing pipeline :param nominal_label: the label that marks nominal samples in training. The scores in the heatmaps always rate label 1, thus usually the nominal label is 0, s.t. the scores are anomaly scores. :param supervise_mode: the type of generated artificial anomalies. See :meth:`fcdd.datasets.bases.TorchvisionDataset._generate_artificial_anomalies_train_set`. :param noise_mode: the type of noise used, see :mod:`fcdd.datasets.noise_mode`. :param oe_limit: limits the number of different anomalies in case of Outlier Exposure (defined in noise_mode) :param online_supervision: whether to sample anomalies online in each epoch, or offline before training (same for all epochs in this case). :param logger: logger """ super().__init__(root, logger=logger) self.n_classes = 2 # 0: normal, 1: outlier self.shape = (3, 32, 32) self.raw_shape = (3, 32, 32) self.normal_classes = tuple([normal_class]) self.outlier_classes = list(range(0, 10)) self.outlier_classes.remove(normal_class) assert nominal_label in [0, 1] self.nominal_label = nominal_label self.anomalous_label = 1 if self.nominal_label == 0 else 0 if self.nominal_label != 0: print('Swapping labels, i.e. anomalies are 0 and nominals are 1.') # Pre-computed min and max values (after applying LCN) from train data per class min_max_l1 = [(-28.94083453598571, 13.802961825439636), (-6.681770233365245, 9.158067708230273), (-34.924463588638204, 14.419298165027628), (-10.599172931391799, 11.093187820377565), (-11.945022995801637, 10.628045447867583), (-9.691969487694928, 8.948326776180823), (-9.174940012342555, 13.847014686472365), (-6.876682005899029, 12.282371383343161), (-15.603507135507172, 15.2464923804279), (-6.132882973622672, 8.046098172351265)] # mean and std of original images per class mean = [ [0.5256516933441162, 0.5603281855583191, 0.5888723731040955], [0.4711322784423828, 0.45446228981018066, 0.4471212327480316], [0.48923906683921814, 0.49146366119384766, 0.423904687166214], [0.4954785108566284, 0.45636114478111267, 0.4154069721698761], [0.47155335545539856, 0.46515223383903503, 0.37797248363494873], [0.49992093443870544, 0.4646056592464447, 0.4164286255836487], [0.47001829743385315, 0.43829214572906494, 0.34500396251678467], [0.5019531846046448, 0.47983652353286743, 0.4167139232158661], [0.4902143180370331, 0.5253947973251343, 0.5546804070472717], [0.4986417591571808, 0.4852965474128723, 0.4780091941356659] ] std = [[0.2502202093601227, 0.24083486199378967, 0.2659735083580017], [0.26806357502937317, 0.2658274173736572, 0.2749459445476532], [0.22705480456352234, 0.2209445983171463, 0.24337927997112274], [0.2568431496620178, 0.25227081775665283, 0.25799375772476196], [0.21732737123966217, 0.20652702450752258, 0.21182335913181305], [0.2504253387451172, 0.24374878406524658, 0.2489463835954666], [0.22888341546058655, 0.21856172382831573, 0.2204199582338333], [0.2430490106344223, 0.243973046541214, 0.25171563029289246], [0.24962472915649414, 0.24068884551525116, 0.25149762630462646], [0.2680525481700897, 0.26910799741744995, 0.2810165584087372]] # different types of preprocessing pipelines, 'lcn' is for using LCN, 'aug{X}' for augmentations # also contains options for the black center experiments all_transform = [] if preproc == 'lcn': test_transform = transform = transforms.Compose([ transforms.ToTensor(), transforms.Lambda( lambda x: local_contrast_normalization(x, scale='l1')), transforms.Normalize([min_max_l1[normal_class][0]] * 3, [ min_max_l1[normal_class][1] - min_max_l1[normal_class][0] ] * 3) ]) elif preproc in ['', None, 'default', 'none']: test_transform = transform = transforms.Compose([ transforms.ToTensor(), transforms.Normalize(mean[normal_class], std[normal_class]) ]) elif preproc in ['aug1']: test_transform = transforms.Compose([ transforms.ToTensor(), transforms.Normalize(mean[normal_class], std[normal_class]) ]) transform = transforms.Compose([ transforms.ColorJitter(brightness=0.01, contrast=0.01, saturation=0.01, hue=0.01), transforms.RandomCrop(32, padding=4), transforms.RandomHorizontalFlip(), transforms.ToTensor(), transforms.Lambda(lambda x: x + 0.001 * torch.randn_like(x)), transforms.Normalize(mean[normal_class], std[normal_class]) ]) elif preproc in ['aug1_blackcenter']: test_transform = transforms.Compose([ transforms.ToTensor(), transforms.Normalize(mean[normal_class], std[normal_class]) ]) transform = transforms.Compose([ transforms.ColorJitter(brightness=0.01, contrast=0.01, saturation=0.01, hue=0.01), transforms.RandomCrop(32, padding=4), transforms.RandomHorizontalFlip(), transforms.ToTensor(), transforms.Lambda(lambda x: x + 0.001 * torch.randn_like(x)), BlackCenter(0.6), transforms.Normalize(mean[normal_class], std[normal_class]) ]) elif preproc in ['aug1_blackcenter_inverted']: test_transform = transforms.Compose([ transforms.ToTensor(), transforms.Normalize(mean[normal_class], std[normal_class]) ]) transform = transforms.Compose([ transforms.ColorJitter(brightness=0.01, contrast=0.01, saturation=0.01, hue=0.01), transforms.RandomCrop(32, padding=4), transforms.RandomHorizontalFlip(), transforms.ToTensor(), transforms.Lambda(lambda x: x + 0.001 * torch.randn_like(x)), BlackCenter(0.6, inverse=True), transforms.Normalize(mean[normal_class], std[normal_class]) ]) else: raise ValueError( 'Preprocessing pipeline {} is not known.'.format(preproc)) target_transform = transforms.Lambda( lambda x: self.anomalous_label if x in self.outlier_classes else self.nominal_label) if online_supervision: all_transform = MultiCompose([ OnlineSupervisor(self, supervise_mode, noise_mode, oe_limit), *all_transform ]) else: all_transform = MultiCompose(all_transform) train_set = MYCIFAR10(root=self.root, train=True, download=True, normal_classes=self.normal_classes, transform=transform, target_transform=target_transform, all_transform=all_transform) train_set.targets = torch.from_numpy(np.asarray(train_set.targets)) train_set.data = torch.from_numpy(train_set.data).transpose( 1, 3).transpose(2, 3) self._generate_artificial_anomalies_train_set( supervise_mode if not online_supervision else 'unsupervised', noise_mode, oe_limit, train_set, normal_class) self._test_set = MYCIFAR10(root=self.root, train=False, download=True, normal_classes=self.normal_classes, transform=test_transform, target_transform=target_transform)
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, override_params={'dropout_rate': opt.dropout, 'drop_connect_rate': opt.dropconnect}) model.to('cuda') cudnn.benchmark = True best_acc = 0 data_dir = opt.source_dataset train_dir = os.path.join(data_dir, 'train') train_aug = transforms.Compose([ transforms.Lambda(lambda img: data_augment(img, opt)), transforms.Resize(opt.size), transforms.RandomHorizontalFlip(), transforms.ToTensor(), # transforms.RandomErasing(p=0.3, scale=(0.02, 0.10), ratio=(0.3, 3.3), value=0, inplace=True), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]), ]) train_loader = DataLoader(datasets.ImageFolder(train_dir, train_aug), batch_size=opt.train_batch, shuffle=True, num_workers=opt.num_workers, pin_memory=True) val_dir = os.path.join(data_dir, 'val') val_aug = transforms.Compose([ transforms.Resize(opt.size), transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]), ])
network.add_monitor(voltages["O"], name="O_voltages") # Directs network to GPU if gpu: network.to("cuda") # Get MNIST training images and labels. # Load MNIST data. dataset = MNIST( PoissonEncoder(time=time, dt=dt), None, root=os.path.join("..", "..", "data", "MNIST"), download=True, transform=transforms.Compose( [transforms.ToTensor(), transforms.Lambda(lambda x: x * intensity)]), ) inpt_axes = None inpt_ims = None spike_axes = None spike_ims = None weights_im = None weights_im2 = None voltage_ims = None voltage_axes = None # Create a dataloader to iterate and batch data dataloader = torch.utils.data.DataLoader(dataset, batch_size=1, shuffle=True,
def train(args): check_paths(args) np.random.seed(args.seed) torch.manual_seed(args.seed) if args.cuda: torch.cuda.manual_seed(args.seed) kwargs = {'num_workers': 0, 'pin_memory': False} else: kwargs = {} transform = transforms.Compose([ transforms.Scale(args.image_size), transforms.CenterCrop(args.image_size), transforms.ToTensor(), transforms.Lambda(lambda x: x.mul(255)) ]) train_dataset = datasets.ImageFolder(args.dataset, transform) train_loader = DataLoader(train_dataset, batch_size=args.batch_size, **kwargs) style_model = Net(ngf=args.ngf) if args.resume is not None: print('Resuming, initializing using weight from {}.'.format( args.resume)) style_model.load_state_dict(torch.load(args.resume)) print(style_model) optimizer = Adam(style_model.parameters(), args.lr) mse_loss = torch.nn.MSELoss() vgg = Vgg16() utils.init_vgg16(args.vgg_model_dir) vgg.load_state_dict( torch.load(os.path.join(args.vgg_model_dir, "vgg16.weight"))) if args.cuda: style_model.cuda() vgg.cuda() style_loader = utils.StyleLoader(args.style_folder, args.style_size) tbar = trange(args.epochs) for e in tbar: style_model.train() agg_content_loss = 0. agg_style_loss = 0. count = 0 for batch_id, (x, _) in enumerate(train_loader): n_batch = len(x) count += n_batch optimizer.zero_grad() x = Variable(utils.preprocess_batch(x)) if args.cuda: x = x.cuda() style_v = style_loader.get(batch_id) style_model.setTarget(style_v) style_v = utils.subtract_imagenet_mean_batch(style_v) features_style = vgg(style_v) gram_style = [utils.gram_matrix(y) for y in features_style] y = style_model(x) xc = Variable(x.data.clone()) y = utils.subtract_imagenet_mean_batch(y) xc = utils.subtract_imagenet_mean_batch(xc) features_y = vgg(y) features_xc = vgg(xc) f_xc_c = Variable(features_xc[1].data, requires_grad=False) content_loss = args.content_weight * mse_loss( features_y[1], f_xc_c) style_loss = 0. for m in range(len(features_y)): gram_y = utils.gram_matrix(features_y[m]) gram_s = Variable(gram_style[m].data, requires_grad=False).repeat( args.batch_size, 1, 1, 1) style_loss += args.style_weight * mse_loss( gram_y, gram_s[:n_batch, :, :]) total_loss = content_loss + style_loss total_loss.backward() optimizer.step() agg_content_loss += content_loss.data[0] agg_style_loss += style_loss.data[0] if (batch_id + 1) % args.log_interval == 0: mesg = "{}\tEpoch {}:\t[{}/{}]\tcontent: {:.6f}\tstyle: {:.6f}\ttotal: {:.6f}".format( time.ctime(), e + 1, count, len(train_dataset), agg_content_loss / (batch_id + 1), agg_style_loss / (batch_id + 1), (agg_content_loss + agg_style_loss) / (batch_id + 1)) tbar.set_description(mesg) if (batch_id + 1) % (4 * args.log_interval) == 0: # save model style_model.eval() style_model.cpu() save_model_filename = "Epoch_" + str(e) + "iters_" + str(count) + "_" + \ str(time.ctime()).replace(' ', '_') + "_" + str( args.content_weight) + "_" + str(args.style_weight) + ".model" save_model_path = os.path.join(args.save_model_dir, save_model_filename) torch.save(style_model.state_dict(), save_model_path) style_model.train() style_model.cuda() tbar.set_description("\nCheckpoint, trained model saved at", save_model_path) # save model style_model.eval() style_model.cpu() save_model_filename = "Final_epoch_" + str(args.epochs) + "_" + \ str(time.ctime()).replace(' ', '_') + "_" + str( args.content_weight) + "_" + str(args.style_weight) + ".model" save_model_path = os.path.join(args.save_model_dir, save_model_filename) torch.save(style_model.state_dict(), save_model_path) print("\nDone, trained model saved at", save_model_path)
def set_seed(SEED): SEED = 0 torch.cuda.manual_seed(SEED) torch.manual_seed(SEED) np.random.seed(SEED) random.seed(SEED) torch.backends.cudnn.deterministic = True torch.backends.cudnn.benchmark = False source_transform = transforms.Compose([ # 轉灰階: Canny 不吃 RGB。 transforms.Grayscale(), # cv2 不吃 skimage.Image,因此轉成np.array後再做cv2.Canny transforms.Lambda(lambda x: cv2.Canny(np.array(x), 170, 300)), #transforms.Lambda(lambda x: cv2.bitwise_or(np.uint8(np.absolute(cv2.Sobel(np.array(x), cv2.CV_64F, 1, 0))), \ #np.uint8(np.absolute(cv2.Sobel(np.array(x), cv2.CV_64F, 0, 1))))), # 重新將np.array 轉回 skimage.Image transforms.ToPILImage(), # 水平翻轉 (Augmentation) transforms.RandomHorizontalFlip(), # 旋轉15度內 (Augmentation),旋轉後空的地方補0 transforms.RandomRotation(15), # 最後轉成Tensor供model使用。 transforms.ToTensor(), ]) target_transform = transforms.Compose([ # 轉灰階: 將輸入3維壓成1維。 transforms.Grayscale(), # 縮放: 因為source data是32x32,我們將target data的28x28放大成32x32。
parser.add_argument("--base_path", type=str, help="top level directory where all repos, etc. live", default=None) args = parser.parse_args() device = 'cuda' transform_to_faceseg = transforms.Compose([ transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225], inplace=False) ]) transform_uint8 = transforms.Compose([ # transforms.Resize(256), transforms.Lambda(convert_to_uint8) ]) # Instantiate models - generation, segmentation, landmarks 2d, landmarks 3d # tddfa = load_3ddfa(args, args.base_path) fa = face_alignment.FaceAlignment(face_alignment.LandmarksType._2D, flip_input=False) face_seg = load_facesegment(args.base_path, device) g_ema = load_generator(args.base_path, device) if args.save_dir is None: path_train_data = f'{base_path}/training_data_dfr' else: path_train_data = args.save_dir
def test_model(pathDirData, pathFileTest, pathModel, nnArchitecture, nnClassCount, nnIsTrained, trBatchSize, transResize, transCrop, launchTimeStamp): CLASS_NAMES = [ 'Atelectasis', 'Cardiomegaly', 'Effusion', 'Infiltration', 'Mass', 'Nodule', 'Pneumonia', 'Pneumothorax', 'Consolidation', 'Edema', 'Emphysema', 'Fibrosis', 'Pleural_Thickening', 'Hernia' ] cudnn.benchmark = True model = None #-------------------- SETTINGS: NETWORK ARCHITECTURE, MODEL LOAD if nnArchitecture == 'DENSE-NET-121': model = DenseNet121(nnClassCount, nnIsTrained).cuda() elif nnArchitecture == 'DENSE-NET-169': model = DenseNet169(nnClassCount, nnIsTrained).cuda() elif nnArchitecture == 'RES-NET-50': model = ResNet50(nnClassCount, nnIsTrained).cuda() elif nnArchitecture == 'SE-RES-NET-50': model = SE_ResNet50(nnClassCount, nnIsTrained).cuda() elif nnArchitecture == 'SE-DENSE-NET-121': model = SE_DenseNet121(nnClassCount, nnIsTrained).cuda() if model is not None and DEVICE == 'cuda': model = torch.nn.DataParallel(model).cuda() modelCheckpoint = torch.load(pathModel) model.load_state_dict(modelCheckpoint['state_dict']) #-------------------- SETTINGS: DATA TRANSFORMS, TEN CROPS normalize = transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]) #-------------------- SETTINGS: DATASET BUILDERS transformList = [] transformList.append(transforms.Resize(transResize)) transformList.append(transforms.TenCrop(transCrop)) transformList.append( transforms.Lambda(lambda crops: torch.stack( [transforms.ToTensor()(crop) for crop in crops]))) transformList.append( transforms.Lambda(lambda crops: torch.stack( [normalize(crop) for crop in crops]))) transformSequence = transforms.Compose(transformList) datasetTest = DatasetGenerator(pathImageDirectory=pathDirData, pathDatasetFile=pathFileTest, transform=transformSequence) dataLoaderTest = DataLoader(dataset=datasetTest, batch_size=trBatchSize, num_workers=8, shuffle=False, pin_memory=True) outGT = torch.FloatTensor() outPRED = torch.FloatTensor() if DEVICE == 'cuda': outGT = cuda() outPRED = cuda() model.eval() for i, (input, target) in enumerate(dataLoaderTest): if DEVICE == 'cuda': target = target.cuda() outGT = torch.cat((outGT, target), 0) bs, n_crops, c, h, w = input.size() varInput = torch.autograd.Variable(input.view(-1, c, h, w).cuda(), volatile=True) out = model(varInput) outMean = out.view(bs, n_crops, -1).mean(1) outPRED = torch.cat((outPRED, outMean.data), 0) aurocIndividual = ChexnetTrainer.computeAUROC(outGT, outPRED, nnClassCount) aurocMean = np.array(aurocIndividual).mean() print('AUROC mean ', aurocMean) for i in range(0, len(aurocIndividual)): print(CLASS_NAMES[i], ' ', aurocIndividual[i]) return
import time import os import PIL from google.colab import files import matplotlib import matplotlib.animation as animation import matplotlib.pyplot as plt import torch from torchvision import transforms # Always use html5 for animations so they can be rendered inline on Colab. matplotlib.rcParams['animation.html'] = 'html5' _IMAGE_UNLOADER = transforms.Compose([ transforms.Lambda(lambda x: x.cpu().clone().squeeze(0)), transforms.ToPILImage() ]) def get_device(): """Returns the appropriate device depending on what's available.""" return torch.device("cuda" if torch.cuda.is_available() else "cpu") def upload_files(): """Creates a widget to upload files from your local machine to Colab. The files are saved in '/tmp/<file_name>'. """ uploaded = files.upload()
def data_loader(args): mean_vals = [0.485, 0.456, 0.406] std_vals = [0.229, 0.224, 0.225] tsfm_train = transforms.Compose([ transforms.Resize((args.resize_size, args.resize_size)), transforms.RandomCrop(args.crop_size), transforms.RandomHorizontalFlip(), transforms.ToTensor(), transforms.Normalize(mean_vals, std_vals) ]) if args.tencrop == 'True': func_transforms = [ transforms.Resize(args.resize_size), transforms.TenCrop(args.crop_size), transforms.Lambda(lambda crops: torch.stack([ transforms.Normalize(mean_vals, std_vals) (transforms.ToTensor()(crop)) for crop in crops ])), ] else: func_transforms = [] # print input_size, crop_size if args.resize_size == 0 or args.crop_size == 0: pass else: func_transforms.append( transforms.Resize((args.resize_size, args.resize_size))) func_transforms.append(transforms.CenterCrop(args.crop_size)) func_transforms.append(transforms.ToTensor()) func_transforms.append(transforms.Normalize(mean_vals, std_vals)) tsfm_test = transforms.Compose(func_transforms) if args.dataset == 'ILSVRC': with_image = False else: with_image = True img_train = CUBClsDataset(root=args.data, datalist=args.train_list, transform=tsfm_train, with_image=with_image) img_test = CUBCamDataset(root=args.data, datalist=args.test_list, transform=tsfm_test, with_image=with_image) if args.distributed: train_sampler = torch.utils.data.distributed.DistributedSampler( img_train) else: train_sampler = None train_loader = DataLoader(img_train, batch_size=args.batch_size, shuffle=(train_sampler is None), sampler=train_sampler, num_workers=args.workers) val_loader = DataLoader(img_test, batch_size=args.batch_size, shuffle=False, num_workers=args.workers) return train_loader, val_loader, train_sampler
def main_worker(gpu, ngpus_per_node, args): writer = SummaryWriter(f'{args.out_dir}/gpu-{gpu}') global best_acc1 args.gpu = gpu params = {'num_classes':args.num_classes} if args.pretrained: params.update(pretrained=args.pretrained) if args.finetune: params.update(finetune=args.finetune) if args.advprop: params.update(advprop=args.advprop) if args.drop_fc: params.update(load_fc=not args.drop_fc) grayscale = 'gray' in args.arch model_name = args.arch.split('-gray', 1)[0] os.makedirs(args.out_dir, exist_ok=True) checkpoint_name = f"{args.out_dir}/{args.arch}.checkpoint.pth.tar" if args.gpu is not None: print("Use GPU: {} for training".format(args.gpu)) if args.distributed: if args.dist_url == "env://" and args.rank == -1: args.rank = int(os.environ["RANK"]) if args.multiprocessing_distributed: # For multiprocessing distributed training, rank needs to be the # global rank among all the processes args.rank = args.rank * ngpus_per_node + gpu dist.init_process_group(backend=args.dist_backend, init_method=args.dist_url, world_size=args.world_size, rank=args.rank) # create model if 'efficientnet' in model_name: # NEW if args.pretrained: model = EfficientNet.from_pretrained(model_name, **params) print("=> using pre-trained model '{}'".format(model_name)) else: print("=> creating model '{}'".format(model_name)) model = EfficientNet.from_name(model_name, override_params=params) else: if args.pretrained == 'imagenet': print("=> using pre-trained model '{}'".format(model_name)) model = models.__dict__[model_name](pretrained=True, **params) else: print("=> creating model '{}'".format(model_name)) model = models.__dict__[model_name](**params) if args.finetune: print(f'freezing layers') for layer, param in model.named_parameters(): if 'fc' in layer : param.requires_grad = True else: param.requires_grad = False if args.distributed: # For multiprocessing distributed, DistributedDataParallel constructor # should always set the single device scope, otherwise, # DistributedDataParallel will use all available devices. if args.gpu is not None: torch.cuda.set_device(args.gpu) model.cuda(args.gpu) # When using a single GPU per process and per # DistributedDataParallel, we need to divide the batch size # ourselves based on the total number of GPUs we have args.batch_size = int(args.batch_size / ngpus_per_node) #args.workers = int(args.workers / ngpus_per_node) model = torch.nn.parallel.DistributedDataParallel(model, device_ids=[args.gpu], find_unused_parameters=True) else: model.cuda() # DistributedDataParallel will divide and allocate batch_size to all # available GPUs if device_ids are not set model = torch.nn.parallel.DistributedDataParallel(model) elif args.gpu is not None: torch.cuda.set_device(args.gpu) model = model.cuda(args.gpu) else: # DataParallel will divide and allocate batch_size to all available GPUs if model_name.startswith('alexnet') or model_name.startswith('vgg'): model.features = torch.nn.DataParallel(model.features) model.cuda() else: model = torch.nn.DataParallel(model).cuda() # if args.pretrained: # load_pretrained_weights(model, model_name, args.pretrained, load_fc=not args.drop_fc) # define loss function (criterion) and optimizer criterion = nn.CrossEntropyLoss().cuda(args.gpu) optimizer = torch.optim.SGD(model.parameters(), args.lr, momentum=args.momentum, weight_decay=args.weight_decay) # optionally resume from a checkpoint if args.resume: if os.path.isfile(args.resume): print("=> loading checkpoint '{}'".format(args.resume)) checkpoint = torch.load(args.resume) args.start_epoch = checkpoint['epoch'] best_acc1 = checkpoint['best_acc1'] if args.gpu is not None: # best_acc1 may be from a checkpoint from a different GPU best_acc1 = best_acc1.to(args.gpu) model.load_state_dict(checkpoint['state_dict']) optimizer.load_state_dict(checkpoint['optimizer']) print("=> loaded checkpoint '{}' (epoch {})" .format(args.resume, checkpoint['epoch'])) else: print("=> no checkpoint found at '{}'".format(args.resume)) cudnn.benchmark = True # Data loading code traindir = os.path.join(args.data, 'train') valdir = os.path.join(args.data, 'val') testdir = os.path.join(args.data, 'test') normalize_rgb = transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) normalize_gray = transforms.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5]) normalize = normalize_gray if grayscale else normalize_rgb convert = transforms.Grayscale(num_output_channels=3) if grayscale else transforms.Lambda(lambda x : x) if 'efficientnet' in model_name: image_size = image_crop = EfficientNet.get_image_size(args.arch) else: image_size = 256 image_crop = 224 print('Using image size', image_crop) train_dataset = datasets.ImageFolder( traindir, transforms.Compose([ convert, transforms.Resize([image_size, image_size]), transforms.RandomHorizontalFlip(), transforms.RandomVerticalFlip(), transforms.ToTensor(), normalize, ])) val_transforms = transforms.Compose([ convert, transforms.Resize([image_size, image_size], interpolation=PIL.Image.BICUBIC), transforms.ToTensor(), normalize, ]) val_dataset = datasets.ImageFolder(valdir, val_transforms)#, loader=cache_img) if args.distributed: train_sampler = torch.utils.data.distributed.DistributedSampler(train_dataset) val_sampler = torch.utils.data.distributed.DistributedSampler(val_dataset) else: train_sampler = None val_dataset = None train_loader = torch.utils.data.DataLoader( train_dataset, batch_size=args.batch_size, shuffle=(train_sampler is None), num_workers=args.workers, pin_memory=True, sampler=train_sampler) val_loader = torch.utils.data.DataLoader( val_dataset, batch_size=args.batch_size, shuffle=(val_sampler is None), num_workers=args.workers, pin_memory=True) if args.evaluate: if args.advprop: normalize = transforms.Lambda(adv_prop_transform) test_transforms = transforms.Compose([ convert, transforms.Resize([image_size, image_size], interpolation=PIL.Image.BICUBIC), transforms.ToTensor(), normalize, ]) test_loader = torch.utils.data.DataLoader( datasets.ImageFolder(testdir, test_transforms), batch_size=args.batch_size, shuffle=False, num_workers=args.workers, pin_memory=True) res = validate(test_loader, model, criterion, args) with open('res.txt', 'w') as f: f.write(str(res)) return with open(f"{args.out_dir}/{args.arch}.params.txt", 'w') as f: f.write(f"Training {args.arch} with params {args}") for epoch in range(args.start_epoch, args.epochs): if args.distributed: train_sampler.set_epoch(epoch) adjust_learning_rate(optimizer, epoch, args) # train for one epoch train(train_loader, model, criterion, optimizer, epoch, args, writer) # evaluate on validation set acc1 = validate(val_loader, model, criterion, args, epoch, writer) # remember best acc@1 and save checkpoint is_best = acc1 > best_acc1 best_acc1 = max(acc1, best_acc1) if not args.multiprocessing_distributed or (args.multiprocessing_distributed and args.rank % ngpus_per_node == 0): save_checkpoint({ 'epoch': epoch + 1, 'arch': args.arch, 'state_dict': model.state_dict(), 'acc1': acc1, 'best_acc1': best_acc1, 'optimizer' : optimizer.state_dict(), }, is_best, checkpoint_name)
def main(): # Training settings parser = argparse.ArgumentParser(description='PyTorch DWT-MEC OfficeHome') parser.add_argument('--num_workers', default=2, type=int) parser.add_argument( '--source_batch_size', type=int, default=18, help='input source batch size for training (default: 20)') parser.add_argument( '--target_batch_size', type=int, default=18, help='input target batch size for training (default: 20)') parser.add_argument('--test_batch_size', type=int, default=10, help='input batch size for testing (default: 10)') parser.add_argument('--s_dset_path', type=str, default='../data/OfficeHomeDataset_10072016/Art', help="The source dataset path") parser.add_argument('--t_dset_path', type=str, default='../data/OfficeHomeDataset_10072016/Clipart', help="The target dataset path") parser.add_argument('--resnet_path', type=str, default='../data/models/model_best_gr_4.pth.tar', help="The pre-trained model path") parser.add_argument('--img_resize', type=int, default=256, help='size of the input image') parser.add_argument('--img_crop_size', type=int, default=224, help='size of the cropped image') parser.add_argument('--num_iters', type=int, default=10000, help='number of iterations to train (default: 10000)') parser.add_argument( '--check_acc_step', type=int, default=100, help= 'number of iterations steps to check validation accuracy (default: 10)' ) parser.add_argument('--lr_change_step', type=int, default=1000) parser.add_argument('--lr', type=float, default=1e-2, help='learning rate (default: 0.01)') parser.add_argument('--num_classes', type=int, default=65, help='number of classes in the dataset') parser.add_argument('--sgd_momentum', type=float, default=0.5, help='SGD momentum (default: 0.5)') parser.add_argument( '--running_momentum', type=float, default=0.1, help='Running momentum for domain statistics(default: 0.1)') parser.add_argument( '--lambda_mec_loss', type=float, default=0.1, help='Value of lambda for the entropy loss (default: 0.1)') parser.add_argument( '--log_interval', type=int, default=10, help='how many batches to wait before logging training status') parser.add_argument('--seed', type=int, default=1, help='random seed (default: 1)') args = parser.parse_args() # set the seed torch.manual_seed(args.seed) device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") # transformation on the source data during training and test data during test data_transform = transforms.Compose([ transforms.Resize( (args.img_resize, args.img_resize)), # spatial size of vgg-f input transforms.RandomCrop((args.img_crop_size, args.img_crop_size)), transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]), ]) # transformation on the target data data_transform_dup = transforms.Compose([ transforms.Resize((args.img_resize, args.img_resize)), transforms.RandomCrop((args.img_crop_size, args.img_crop_size)), transforms.RandomHorizontalFlip(), transforms.ToTensor(), transforms.Lambda(lambda x: _random_affine_augmentation(x)), transforms.Lambda(lambda x: _gaussian_blur(x)), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]), ]) # train data sets source_dataset = folder.ImageFolder(root=args.s_dset_path, transform=data_transform) target_dataset = folder.ImageFolder(root=args.t_dset_path, transform=data_transform, transform_aug=data_transform_dup) # test data sets target_dataset_test = folder.ImageFolder(root=args.t_dset_path, transform=data_transform) # '''''''''''' Train loaders ''''''''''''''' # source_trainloader = torch.utils.data.DataLoader( source_dataset, batch_size=args.source_batch_size, shuffle=True, num_workers=args.num_workers, drop_last=True) target_trainloader = torch.utils.data.DataLoader( target_dataset, batch_size=args.source_batch_size, shuffle=True, num_workers=args.num_workers, drop_last=True) # '''''''''''' Test loader ''''''''''''''' # target_testloader = torch.utils.data.DataLoader( target_dataset_test, batch_size=args.test_batch_size, shuffle=True, num_workers=args.num_workers) model = resnet50(args.resnet_path, device).to(device) final_layer_params = [] rest_of_the_net_params = [] for name, param in model.named_parameters(): if name.startswith('fc_out'): final_layer_params.append(param) else: rest_of_the_net_params.append(param) optimizer = optim.SGD([{ 'params': rest_of_the_net_params }, { 'params': final_layer_params, 'lr': args.lr }], lr=args.lr * 0.1, momentum=0.9, weight_decay=5e-4) train_infinite_collect_stats(args=args, model=model, device=device, source_train_loader=source_trainloader, target_train_loader=target_trainloader, optimizer=optimizer, lambda_mec_loss=args.lambda_mec_loss, target_test_loader=target_testloader)
print(args) assert args.source != args.target, "source and target can not be the same!!!" # svhn-like transforms dset_transforms = list() dset_transforms.append(transforms.ToTensor()) dset_transforms.append( transforms.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5])) dset_transforms = transforms.Compose(dset_transforms) # mnist-like grayscale dataset transforms gray_transforms = list() gray_transforms.append(transforms.Resize((32, 32))) gray_transforms.append(transforms.ToTensor()) gray_transforms.append(transforms.Lambda(lambda x: x.repeat(3, 1, 1))) gray_transforms.append( transforms.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5])) gray_transforms = transforms.Compose(gray_transforms) # mnistm transforms mnistm_transforms = list() mnistm_transforms.append(transforms.Resize((32, 32))) mnistm_transforms.append(transforms.ToTensor()) mnistm_transforms.append( transforms.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5])) mnistm_transforms = transforms.Compose(mnistm_transforms) # create the dataloaders dataloader = {} if args.source == 'svhn':
testset = SVHN(root='./svhn_data', split='test', download=True, transform=transform_test) test_loader = torch.utils.data.DataLoader(testset, batch_size=1000, shuffle=False) # Cifar-100 dataset if cifar100: 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.ToTensor(), transforms.Lambda(lambda x: F.pad(Variable( x.unsqueeze(0), requires_grad=False, volatile=True), (4, 4, 4, 4), mode='reflect').data.squeeze()), transforms.ToPILImage(), transforms.RandomCrop(32), transforms.RandomHorizontalFlip(), transforms.ToTensor(), normalize, ]) # ~ data prep for test set (CIFAR100) transform_test = transforms.Compose([transforms.ToTensor(), normalize]) # ~ load training and test set here (CIFAR100): training_set = datasets.CIFAR100(root='./cifar100_data', train=True, download=True, transform=transform_train) train_loader = torch.utils.data.DataLoader(training_set,
def train(args): device = torch.device("cuda" if args.cuda else "cpu") np.random.seed(args.seed) torch.manual_seed(args.seed) transform = transforms.Compose([ transforms.Resize(args.image_size), transforms.CenterCrop(args.image_size), transforms.ToTensor(), transforms.Lambda(lambda x: x.mul(255)) ]) train_dataset = datasets.ImageFolder(args.dataset, transform) train_loader = DataLoader(train_dataset, batch_size=args.batch_size) transformer = TransformerNet().to(device) optimizer = Adam(transformer.parameters(), args.lr) mse_loss = torch.nn.MSELoss() vgg = Vgg16(requires_grad=False).to(device) style_transform = transforms.Compose( [transforms.ToTensor(), transforms.Lambda(lambda x: x.mul(255))]) style = utils.load_image(args.style_image, size=args.style_size) style = style_transform(style) style = style.repeat(args.batch_size, 1, 1, 1).to(device) features_style = vgg(utils.normalize_batch(style)) gram_style = [utils.gram_matrix(y) for y in features_style] for e in range(args.epochs): transformer.train() agg_content_loss = 0. agg_style_loss = 0. count = 0 for batch_id, (x, _) in enumerate(train_loader): n_batch = len(x) count += n_batch optimizer.zero_grad() x = x.to(device) y = transformer(x) y = utils.normalize_batch(y) x = utils.normalize_batch(x) features_y = vgg(y) features_x = vgg(x) content_loss = args.content_weight * mse_loss( features_y.relu2_2, features_x.relu2_2) style_loss = 0. for ft_y, gm_s in zip(features_y, gram_style): gm_y = utils.gram_matrix(ft_y) style_loss += mse_loss(gm_y, gm_s[:n_batch, :, :]) style_loss *= args.style_weight total_loss = content_loss + style_loss total_loss.backward() optimizer.step() agg_content_loss += content_loss.item() agg_style_loss += style_loss.item() if (batch_id + 1) % args.log_interval == 0: mesg = "{}\tEpoch {}:\t[{}/{}]\tcontent: {:.6f}\tstyle: {:.6f}\ttotal: {:.6f}".format( time.ctime(), e + 1, count, len(train_dataset), agg_content_loss / (batch_id + 1), agg_style_loss / (batch_id + 1), (agg_content_loss + agg_style_loss) / (batch_id + 1)) print(mesg) if args.checkpoint_model_dir is not None and ( batch_id + 1) % args.checkpoint_interval == 0: transformer.eval().cpu() ckpt_model_filename = "ckpt_epoch_" + str( e) + "_batch_id_" + str(batch_id + 1) + ".pth" ckpt_model_path = os.path.join(args.checkpoint_model_dir, ckpt_model_filename) torch.save(transformer.state_dict(), ckpt_model_path) transformer.to(device).train() # save model transformer.eval().cpu() save_model_filename = "epoch_" + str(args.epochs) + "_" + str( time.ctime()).replace(' ', '_') + "_" + str( args.content_weight) + "_" + str(args.style_weight) + ".model" save_model_path = os.path.join(args.save_model_dir, save_model_filename) torch.save(transformer.state_dict(), save_model_path) print("\nDone, trained model saved at", save_model_path)
optimizer = torch.optim.RMSprop(mdrnn.parameters(), lr=1e-3, alpha=.9) scheduler = ReduceLROnPlateau(optimizer, 'min', factor=0.5, patience=5) earlystopping = EarlyStopping('min', patience=30) if exists(rnn_file) and not args.noreload: rnn_state = torch.load(rnn_file) logger.info("Loading MDRNN at epoch {} " "with test error {}".format(rnn_state["epoch"], rnn_state["precision"])) mdrnn.load_state_dict(rnn_state["state_dict"]) optimizer.load_state_dict(rnn_state["optimizer"]) scheduler.load_state_dict(state['scheduler']) earlystopping.load_state_dict(state['earlystopping']) # Data Loading transform = transforms.Lambda(lambda x: np.transpose(x, (0, 3, 1, 2)) / 255) train_loader = DataLoader(RolloutSequenceDataset('datasets/carracing', SEQ_LEN, transform, buffer_size=30), batch_size=BSIZE, num_workers=8, shuffle=True, drop_last=True) test_loader = DataLoader(RolloutSequenceDataset('datasets/carracing', SEQ_LEN, transform, train=False, buffer_size=10), batch_size=BSIZE, num_workers=8,
def __getitem__(self, index): """Read an image from a file and preprocesses it and returns.""" image_path = self.image_paths[index % len(self.image_paths)] image = Image.open(image_path) if np.array(image).shape == 4: image = np.array(image)[:, :, :3] image = Image.fromarray(image) # target (high-resolution image) # Random Crop: #transform = transforms.RandomCrop(self.image_size * self.scale_factor) #hr_image = transform(image) # Face Crop: face_width = face_height = 128 j = (image.size[0] - face_width) // 2 i = (image.size[1] - face_height) // 2 image = image.crop([j, i, j + face_width, i + face_height]) hr_image = image # HR_image --> [0,1] --> torch hr_image_scaled = Scaling(hr_image) hr_image_scaled = torch.from_numpy(hr_image_scaled).float().to( self.device) # NUMPY to TORCH # get HR residuals --> [-1,1] --> torch transform_HR = transforms.Compose([ #random blur transforms.Lambda(lambda x: self.randkern.RandomBlur(x)), #downscale BICUBIC pro tamanho LR transforms.Resize((self.image_size, self.image_size), Image.BICUBIC), #upscale BICUBIC pro tamanho HR transforms.Resize((self.image_size * self.scale_factor, self.image_size * self.scale_factor), Image.BICUBIC) ]) hr_image_hat = transform_HR(hr_image) hr_residual = np.array(hr_image).astype(float) - np.array( hr_image_hat).astype(float) hr_residual_scaled = Scaling(hr_residual) hr_residual_scaled = torch.from_numpy(hr_residual_scaled).float().to( self.device) # NUMPY to TORCH # get LR_IMAGE --> [0,1] transform_to_lr = transforms.Compose([ transforms.Lambda(lambda x: self.randkern.RandomBlur(x)), transforms.Resize((self.image_size, self.image_size), Image.BICUBIC) ]) lr_image = transform_to_lr(hr_image) lr_image_scaled = Scaling(lr_image) # get LR_RESIDUAL --> [-1,1] transform_to_vlr = transforms.Compose([ transforms.Lambda( lambda x: self.randkern.RandomBlur(x)), #random blur transforms.Lambda(lambda x: random_downscale(x, self.scale_factor) ), #random downscale transforms.Resize((self.image_size, self.image_size), Image.BICUBIC) #upscale pro tamanho LR ]) lr_image_hat = transform_to_vlr(lr_image) lr_residual = np.array(lr_image).astype( np.float32) - np.array(lr_image_hat).astype(np.float32) lr_residual_scaled = Scaling(lr_residual) # LR_image_scaled + kernel_proj + LR_residual_scaled (CONCAT) ---> TO TORCH lr_image_without_kernel = lr_image_scaled #self.randkern.ConcatDegraInfo(lr_image_scaled) lr_image_with_resid = np.concatenate( (lr_image_without_kernel, lr_residual_scaled), axis=-1) lr_image_with_resid = torch.from_numpy(lr_image_with_resid).float().to( self.device) # NUMPY to TORCH # LR_image to torch lr_image_scaled = torch.from_numpy(lr_image_scaled).float().to( self.device) # NUMPY to TORCH #Transpose - Permute since for model we need input with channels first lr_image_scaled = lr_image_scaled.permute(2, 0, 1) hr_image_scaled = hr_image_scaled.permute(2, 0, 1) lr_image_with_resid = lr_image_with_resid.permute(2, 0, 1) hr_residual_scaled = hr_residual_scaled.permute(2, 0, 1) return image_path,\ lr_image_scaled,\ hr_image_scaled, \ lr_image_with_resid, \ hr_residual_scaled
def stylize(args): device = torch.device("cuda" if args.cuda else "cpu") content_image = utils.load_image(args.content_image, scale=args.content_scale) content_transform = transforms.Compose( [transforms.ToTensor(), transforms.Lambda(lambda x: x.mul(255))]) content_image = content_transform(content_image) content_image = content_image.unsqueeze(0).to(device) cap = cv2.VideoCapture() #cap = cv2.VideoCapture(0) #cap.open("rtsp://*****:*****@192.168.80.241/video1+audio1") # 싸움터 #cap.open("rtsp://*****:*****@192.168.190.241/video1+audio1") # #cap.open("G:\\0000001.mp4") cap.open("rtsp://172.26.19.202:554/h264") fourcc = cv2.VideoWriter_fourcc(*'XVID') out = cv2.VideoWriter('style_transfer_001.avi', fourcc, 30.0, (1920, 1080)) if args.model.endswith(".onnx"): output = stylize_onnx_caffe2(content_image, args) else: with torch.no_grad(): style_model = TransformerNet() state_dict = torch.load(args.model) # remove saved deprecated running_* keys in InstanceNorm from the checkpoint for k in list(state_dict.keys()): if re.search(r'in\d+\.running_(mean|var)$', k): del state_dict[k] style_model.load_state_dict(state_dict) style_model.to(device) if args.export_onnx: assert args.export_onnx.endswith( ".onnx"), "Export model file should end with .onnx" output = torch.onnx._export(style_model, content_image, args.export_onnx).cpu() else: #output = style_model(content_image).cuda() count = 0 while (cap.isOpened()): count += 1 try: if count % 2 == 0: ret, frame1 = cap.read() frame1 = cv2.resize(frame1, (640, 480)) frame1 = cv2.cvtColor(frame1, cv2.COLOR_BGR2RGB) frame = Image.fromarray(frame1) frame = content_transform(frame) frame = frame.unsqueeze(0).to(device) # output = style_model(frame).cuda() output = style_model(frame).to(device) img = output[0].clone().cpu().clamp( 0, 255).cpu().numpy() img = img.transpose(1, 2, 0).astype("uint8") # print(np.shape(img)) # Multi-Window img = cv2.resize(img, (640, 480)) numpy_horizontal_concat = np.concatenate( (frame1, img), axis=1) out.write(img) cv2.imshow('frame', numpy_horizontal_concat) if cv2.waitKey(1) & 0xFF == ord('q'): break except Exception as e: print(str(e)) cap.release() cv2.destroyAllWindows()
def main(model): # print the experiment configuration model.eval() root_dir = '/home/xuzhang/project/Medifor/data/' + args.test_set + '/' world_list = image_processing.load_csv(root_dir + 'indexes/' + args.test_set + '-provenancefiltering-world.csv','|') world_file_list = world_list["WorldFileName"] world_id_list = world_list["WorldFileID"] subset = 'world' kwargs = {'num_workers': args.num_workers, 'pin_memory': args.pin_memory} if args.cuda else {} if args.cuda: model.cuda() transform = transforms.Compose([ transforms.Lambda(centerCrop), #transforms.Lambda(cv2_scale), transforms.Lambda(np_reshape), transforms.ToTensor(), #transforms.Normalize((args.mean_image,), (args.std_image,)) transforms.Normalize((args.mean_image,args.mean_image,args.mean_image), (args.std_image,args.std_image,args.std_image)) ]) test_batch_size = args.test_batch_size descriptor_dim = 128 detector_name = 'DOG' if args.resume: if os.path.isfile(args.resume): print('=> loading checkpoint {}'.format(args.resume)) checkpoint = torch.load(args.resume) args.start_epoch = checkpoint['epoch'] checkpoint = torch.load(args.resume) model.load_state_dict(checkpoint['state_dict']) else: print('=> no checkpoint found at {}'.format(args.resume)) for idx,(filename,fileid) in tqdm(enumerate(zip(world_file_list,world_id_list))): meta = np.array([]) features = np.array([]) feature_file = root_dir+'/'+detector_name+'_prov_desc_'+subset+'/'+fileid+'.npz' try: gray_image, ratio = image_processing.read_image_from_name(root_dir,filename) color_image, ratio = image_processing.read_color_image_from_name(root_dir,filename) except: print(filename) np.savez(feature_file, meta, features) kp_list, sift_desc = image_processing.extract_sift(gray_image) patches_list = [] for kp_idx, kp in enumerate(kp_list): tmp_patch = image_processing.extract_patch(color_image, kp) patches_list.append(tmp_patch) patches_list = np.array(patches_list) patches_list = torch.ByteTensor(patches_list) test_loader = torch.utils.data.DataLoader( testDataset(train=False, data = patches_list, batch_size=args.test_batch_size, root=args.dataroot, name=args.test_set, download=False, transform=transform), batch_size=args.test_batch_size, shuffle=False, **kwargs) patch_number = len(kp_list) if patch_number == 0: np.savez(feature_file, meta=meta, features = features) continue offset = 0 meta = kp_list#np.zeros((patch_number,4)) features = [] for data_a in test_loader: if args.cuda: data_a = data_a.cuda() data_a = Variable(data_a, volatile=True) out_a = model(data_a) features.append(out_a.data.cpu().numpy()) features = np.vstack(features).reshape(patch_number,descriptor_dim) try: os.stat(feature_file) os.remove(feature_file) except: feature_file = feature_file np.savez(feature_file, meta=meta, features = features)