def main(hparams): if hparams.log: wandb.init('vlr-project-dqn') wandb.config.update(hparams) # setup RL environment and burn in data #env = gym.make('Pong-v0') #env = gym.make('VideoPinball-v0') env = gym.make('Breakout-v0') #env = gym.make('Skiing-v0') #env = gym.make('CartPole-v0') meaning = env.get_action_meanings() train_loader = get_dataloader(hparams, is_train=True) burn_in(hparams, env, train_loader) if hparams.model == 'base': model = DQNBase(env.action_space.n, hparams.history_size) # else: # model = AutoEncoder.load_from_checkpoint(args.ae_path) model_t = copy.deepcopy(model) model_t.eval() if hparams.cuda: model.cuda() model_t.cuda() criterion = torch.nn.MSELoss(reduction='none') optim = torch.optim.Adam(list(model.parameters())) gamma = 0.99 print_freq = 50 # ignore for now # scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(optim, mode='min', factor=0.5, patience=2, min_lr=1e-6, verbose=True) transform = transforms.Compose([ transforms.ToPILImage(), transforms.Resize((224, 224)), transforms.ToTensor() ]) history = deque() state = env.reset() done = True num_evals = 0 step = 0 best_val = -float('inf') for epoch in range(hparams.max_epochs): eps = 1 - epoch / hparams.max_epochs eps = max(eps, 0.1) for batch_nb, batch in tqdm(enumerate(train_loader)): ########### # ROLLOUT # ########### model.eval() for t in range(hparams.rollout_steps_per_iteration): if done: # reset if needed done = False history = deque([torch.zeros(1, 224, 224)] * hparams.history_size) state = env.reset() last_action = env.action_space.sample() if hparams.render: env.render() # prepare inputs if step % hparams.k == 0: _input = np.array(Image.fromarray(state).convert("L")) history.pop() history.appendleft(transform(_input)) input = list(history) input = torch.cat(input, dim=0).unsqueeze(0) if hparams.cuda: input = input.cuda() # retrieve action and step with torch.no_grad(): q_values = model(input) action = eps_greedy(eps=eps, env=env, q_values=q_values) else: action = last_action new_state, reward, done, info = env.step(action) reward = min(max(reward, -1), 1) # clip to [-1,1] # add data train_loader.dataset.add_experience(state, action, reward, done) state = new_state last_action = action ############ # TRAINING # ############ metrics = {} model.train() if hparams.cuda: for i, item in enumerate(batch[:-1]): # excluding info batch[i] = item.cuda() _state, _action, _reward, _next_state, _done, _info = batch # retrieve Q(s,a) and Q(ns, na) Qs = model(_state) # N,3 Qsa = torch.gather(Qs, -1, _action.long()) with torch.no_grad(): nQs = model_t(_next_state) # N,3 nQsa, _next_action = torch.max(nQs, dim=-1, keepdim=True) target = _reward + (1 - _done) * gamma * nQsa batch_loss = criterion(Qsa, target) loss = batch_loss.mean() metrics['train_loss'] = loss.item() optim.zero_grad() loss.backward() optim.step() if step % print_freq == 0 and not hparams.log: tqdm.write("Loss: {}".format(loss.item())) if step % hparams.target_update_freq == 0: model_t.load_state_dict(model.state_dict()) if step % hparams.visual_freq == 0 and (hparams.log or hparams.render): meta = { 'Q': Qsa, 'nQ': nQsa, 'meaning': meaning, 'next_action': _next_action, 'batch_loss': batch_loss } images = visualize(_state, _action, _reward, _next_state, _done, meta, hparams) metrics['image'] = wandb.Image(images) if hparams.log: wandb.log(metrics, step) ############## # EVALUATION # ############## if step % hparams.val_freq == 0: tqdm.write(f'epoch {epoch} step {batch_nb} validation') model.eval() total_reward = 0 val_done = False for i in range(hparams.num_eval_episodes): val_done = False val_history = deque([torch.zeros(1, 224, 224)] * hparams.history_size) val_state = env.reset() val_step = 0 while not val_done: if hparams.render: env.render() _val_state = Image.fromarray(val_state).convert("L") _val_state = np.expand_dims(np.array(_val_state), -1) val_history.pop() val_history.appendleft(transform(_val_state)) val_input = list(val_history) val_input = torch.cat(val_input, dim=0).unsqueeze(0) if hparams.cuda: val_input = val_input.cuda() with torch.no_grad(): q_values = model(val_input) val_action = eps_greedy(eps=0.05, env=env, q_values=q_values) val_new_state, val_reward, val_done, val_info = env.step( val_action) total_reward += val_reward val_step += 1 if val_step > 2500: val_done = True tqdm.write('eval timeout') mean_reward = total_reward / hparams.num_eval_episodes if mean_reward > best_val: torch.save(model.state_dict(), str(hparams.save_dir / 'dqn.pt')) best_val = mean_reward tqdm.write("Mean val reward: {}".format(mean_reward)) if hparams.log: wandb.log({'val_reward': mean_reward}, num_evals) num_evals += 1 done = True # reset at beginning of next train iter model.train() # increment train step counter step += 1
def display_transform(): return T.Compose( [T.ToPILImage(), T.Resize(400), T.CenterCrop(400), T.ToTensor()])
def __init__(self, args): super(Trainer, self).__init__() self.args = args self.dev = torch.device( "cuda:0" if torch.cuda.is_available() else "cpu") self.to_pil = transforms.ToPILImage() pprint(self.args) self.data_mode = self.args["data_mode"] if self.args["suffix"]: self.model_name = self.args["model"] + "_" + self.args["suffix"] else: self.model_name = self.args["model"] self.path = construct_path_dict(proj_root=proj_root, exp_name=self.model_name) pre_mkdir(path_config=self.path) shutil.copy(f"{proj_root}/config.py", self.path["cfg_log"]) shutil.copy(f"{proj_root}/train.py", self.path["trainer_log"]) if self.data_mode == "RGBD": self.tr_data_path = self.args["rgbd_data"]["tr_data_path"] self.te_data_list = self.args["rgbd_data"]["te_data_list"] elif self.data_mode == "RGB": self.tr_data_path = self.args["rgb_data"]["tr_data_path"] self.te_data_list = self.args["rgb_data"]["te_data_list"] else: raise NotImplementedError self.save_path = self.path["save"] self.save_pre = self.args["save_pre"] self.tr_loader = create_loader( data_path=self.tr_data_path, mode="train", get_length=False, data_mode=self.data_mode, ) self.net = getattr(network, self.args["model"])(pretrained=True).to(self.dev) # 损失函数 self.loss_funcs = [ BCELoss(reduction=self.args["reduction"]).to(self.dev) ] if self.args["use_aux_loss"]: self.loss_funcs.append(HEL().to(self.dev)) # 设置优化器 self.opti = self.make_optim() # 训练相关 self.end_epoch = self.args["epoch_num"] if self.args["resume"]: try: self.resume_checkpoint(load_path=self.path["final_full_net"], mode="all") except: print( f"{self.path['final_full_net']} does not exist and we will load {self.path['final_state_net']}" ) self.resume_checkpoint(load_path=self.path["final_state_net"], mode="onlynet") self.start_epoch = self.end_epoch else: self.start_epoch = 0 self.iter_num = self.end_epoch * len(self.tr_loader)
from torch import optim import torch.utils.data as Data import os from torchvision.utils import save_image import torch.nn as nn import numpy as np import numpy as npnvid import cv2 import scipy.misc import torch.nn.functional as F import argparse from PIL import Image from torchvision import transforms import torchvision.transforms as transforms from config import args to_pil = transforms.ToPILImage() os.environ["CUDA_VISIBLE_DEVICES"] = args['gpu_num'] def gpu_test(): dataset = shadow_test_triplets_loader() data_loader = Data.DataLoader(dataset, batch_size=args['test_batch_size']) model = AFFPNet(args['fb_num_steps']) model = nn.DataParallel(model) model = model.cuda() checkpoint = torch.load(args['ck_path']) model.load_state_dict(checkpoint['model_state_dict'])
# size of the images imsize = 256 # loading the test images to visualize them loader = transforms.Compose([ # scale images / resize images transforms.Resize(imsize), # randomly crop regions of size 256 transforms.RandomCrop(256), # transform the image into a torch tensor transforms.ToTensor() ]) # un-loading the test images to visualize them un_loader = transforms.ToPILImage() def get_images(image_path): """ get all images at the specified image_path (! no check for actual image files) :param image_path: the path that is searched :return: number of images, file paths """ images = os.listdir(image_path) number_images = len(images) image_file_paths = [ '{}/{}'.format(image_path, images[i]) for i in range(number_images) ] return number_images, image_file_paths
def main(): # args = parse_args() torch.backends.cudnn.benchmark = True os.environ["CUDA_VISIBLE_DEVICES"] = '0,1' device = torch.device('cuda:0' if torch.cuda.is_available() else "cpu") # # if args.seed: # random.seed(args.seed) # np.random.seed(args.seed) # torch.manual_seed(args.seed) # # if args.gpu: # torch.cuda.manual_seed_all(args.seed) seed = 63 random.seed(seed) np.random.seed(seed) torch.manual_seed(seed) # if args.gpu: torch.cuda.manual_seed_all(seed) mean_std = ([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]) # train_transforms = transforms.Compose([ # transforms.RandomCrop(args['crop_size']), # transforms.RandomRotation(90), # transforms.RandomHorizontalFlip(p=0.5), # transforms.RandomVerticalFlip(p=0.5), # ]) short_size = int(min(args['input_size']) / 0.875) # val_transforms = transforms.Compose([ # transforms.Scale(short_size, interpolation=Image.NEAREST), # # joint_transforms.Scale(short_size), # transforms.CenterCrop(args['input_size']) # ]) train_joint_transform = joint_transforms.Compose([ # joint_transforms.Scale(short_size), joint_transforms.RandomCrop(args['crop_size']), joint_transforms.RandomHorizontallyFlip(), joint_transforms.RandomRotate(90) ]) val_joint_transform = joint_transforms.Compose([ joint_transforms.Scale(short_size), joint_transforms.CenterCrop(args['input_size']) ]) input_transform = transforms.Compose( [transforms.ToTensor(), transforms.Normalize(*mean_std)]) target_transform = extended_transforms.MaskToTensor() restore_transform = transforms.Compose( [extended_transforms.DeNormalize(*mean_std), transforms.ToPILImage()]) visualize = transforms.ToTensor() train_set = cityscapes.CityScapes('train', joint_transform=train_joint_transform, transform=input_transform, target_transform=target_transform) # train_set = cityscapes.CityScapes('train', transform=train_transforms) train_loader = DataLoader(train_set, batch_size=args['train_batch_size'], num_workers=8, shuffle=True) val_set = cityscapes.CityScapes('val', joint_transform=val_joint_transform, transform=input_transform, target_transform=target_transform) # val_set = cityscapes.CityScapes('val', transform=val_transforms) val_loader = DataLoader(val_set, batch_size=args['val_batch_size'], num_workers=8, shuffle=True) print(len(train_loader), len(val_loader)) # sdf vgg_model = VGGNet(requires_grad=True, remove_fc=True) net = FCN8s(pretrained_net=vgg_model, n_class=cityscapes.num_classes, dropout_rate=0.4) # net.apply(init_weights) criterion = nn.CrossEntropyLoss(ignore_index=cityscapes.ignore_label) optimizer = optim.Adam(net.parameters(), lr=1e-4) check_mkdir(ft_ckpt_path) check_mkdir(os.path.join(ft_ckpt_path, ft_exp_name)) open( os.path.join(ft_ckpt_path, ft_exp_name, str(datetime.datetime.now()) + '.txt'), 'w').write(str(args) + '\n\n') scheduler = optim.lr_scheduler.ReduceLROnPlateau( optimizer, 'min', patience=args['lr_patience'], min_lr=1e-10) vgg_model = vgg_model.to(device) net = net.to(device) if torch.cuda.device_count() > 1: net = nn.DataParallel(net) # if len(args['snapshot']) == 0: # curr_epoch = 1 # args['best_record'] = {'epoch': 0, 'val_loss': 1e10, 'acc': 0, 'acc_cls': 0, 'mean_iu': 0} # else: # print('training resumes from ' + args['snapshot']) net.load_state_dict( torch.load(os.path.join(ckpt_path, exp_name, args['snapshot']))) # split_snapshot = args['snapshot'].split('_') # curr_epoch = int(split_snapshot[1]) + 1 curr_epoch = 1 args['best_record'] = { 'epoch': 0, 'val_loss': 1e10, 'acc': 0, 'acc_cls': 0, 'mean_iu': 0 } # args['best_record'] = {'epoch': int(split_snapshot[1]), 'val_loss': float(split_snapshot[3]), # 'acc': float(split_snapshot[5]), 'acc_cls': float(split_snapshot[7]), # 'mean_iu': float(split_snapshot[9][:-4])} criterion.to(device) for epoch in range(curr_epoch, args['epoch_num'] + 1): train(train_loader, net, device, criterion, optimizer, epoch, args) val_loss = validate(val_loader, net, device, criterion, optimizer, epoch, args, restore_transform, visualize) scheduler.step(val_loss)
""" from time import time import click import cv2 import torch from PIL import Image import numpy as np import vfin.ssm.model as model from torchvision import transforms from torch.functional import F torch.set_grad_enabled(False) device = torch.device("cuda" if torch.cuda.is_available() else "cpu") trans_forward = transforms.ToTensor() trans_backward = transforms.ToPILImage() if device != "cpu": mean = [0.429, 0.431, 0.397] mea0 = [-m for m in mean] std = [1] * 3 trans_forward = transforms.Compose( [trans_forward, transforms.Normalize(mean=mean, std=std)]) trans_backward = transforms.Compose( [transforms.Normalize(mean=mea0, std=std), trans_backward]) flow = model.UNet(6, 4).to(device) interp = model.UNet(20, 5).to(device) back_warp = None
def train(train_gen, model, criterion, optimizer, epoch): epoch_loss = 0 train_gen = train_gen.load_data() ############### for iteration, batch in enumerate(train_gen): Sharpx128 = batch['B1x128'] Sharpx64 = batch['B2x64'] Sharpx32 = batch['B3x32'] Blurx128 = batch['A1x128'] Blurx64 = batch['A2x64'] Blurx32 = batch['A3x32'] Sharpx128 = Sharpx128.to(device) Sharpx64 = Sharpx64.to(device) Sharpx32 = Sharpx32.to(device) Blurx128 = Blurx128.to(device) Blurx64 = Blurx64.to(device) Blurx32 = Blurx32.to(device) # # show the pictures # shx128 = transforms.ToPILImage()(Sharpx128.cpu()[0]) # shx128.save('./pictureShow/sharpX128.jpg') # shx64 = transforms.ToPILImage()(Sharpx64.cpu()[0]) # shx64.save('./pictureShow/sharpX64.jpg') # shx32 = transforms.ToPILImage()(Sharpx32.cpu()[0]) # shx32.save('./pictureShow/sharpX32.jpg') # Blx128 = transforms.ToPILImage()(Blurx128.cpu()[0]) # Blx128.save('./pictureShow/Blurx128.jpg') # Blx64 = transforms.ToPILImage()(Blurx64.cpu()[0]) # Blx64.save('./pictureShow/Blurx64.jpg') # Blx32 = transforms.ToPILImage()(Blurx32.cpu()[0]) # Blx32.save('./pictureShow/Blurx32.jpg') outx128, outx64, outx32 = model(Blurx128, Blurx64, Blurx32) l1 = criterion(outx128, Sharpx128) l2 = criterion(outx64, Sharpx64) l3 = criterion(outx32, Sharpx32) loss = l1 + l2 + l3 epoch_loss += loss optimizer.zero_grad() loss.backward() torch.nn.utils.clip_grad_norm_( model_GFN_deblur.module.convlstm.parameters(), 3) optimizer.step() if iteration % 100 == 0: print("===> Epoch[{}]: loss:{:.4f}".format(epoch, loss)) f = open(FilePath, 'a') f.write("===> Epoch[{}]: loss:{:.4f}".format(epoch, loss) + '\n') f.close() shx128 = transforms.ToPILImage()(Sharpx128.cpu()[0]) shx128.save('./pictureShow/sharpX128.jpg') # shx64 = transforms.ToPILImage()(Sharpx64.cpu()[0]) # shx64.save('./pictureShow/sharpX64.jpg') # shx32 = transforms.ToPILImage()(Sharpx32.cpu()[0]) # shx32.save('./pictureShow/sharpX32.jpg') Blx128 = transforms.ToPILImage()(Blurx128.cpu()[0]) Blx128.save('./pictureShow/Blurx128.jpg') # Blx64 = transforms.ToPILImage()(Blurx64.cpu()[0]) # Blx64.save('./pictureShow/Blurx64.jpg') # Blx32 = transforms.ToPILImage()(Blurx32.cpu()[0]) # Blx32.save('./pictureShow/Blurx32.jpg') outx128 = torch.clamp(outx128, min=0, max=1) outx128 = transforms.ToPILImage()(outx128.cpu()[0]) outx128.save('./pictureShow/outx128.jpg') # outx64 = transforms.ToPILImage()(outx64.cpu()[0]) # outx64.save('./pictureShow/outx64.jpg') # outx32 = transforms.ToPILImage()(outx32.cpu()[0]) # outx32.save('./pictureShow/outx64.jpg') print("===>Epoch{} Complete: Avg loss is :{:4f}".format( epoch, epoch_loss / len(trainloader))) f = open(FilePath, 'a') f.write("===>Epoch{} Complete: Avg loss is :{:4f}\n".format( epoch, epoch_loss / len(trainloader))) f.close()
def prepare_features(): # create model # model = cse_resnet50(num_classes = args.num_classes, pretrained=None) # model = CSEResnetModel_KD(args.arch, args.num_classes, ems=args.ems_loss) model = CSEResnetModel_KDHashing(args.arch, args.num_hashing, args.num_classes) # model.cuda() model = nn.DataParallel(model).cuda() print(str(datetime.datetime.now()) + ' model inited.') # define loss function (criterion) and optimizer criterion = nn.CrossEntropyLoss().cuda() # resume from a checkpoint if args.resume_file: resume = os.path.join(args.resume_dir, args.resume_file) else: resume = os.path.join(args.resume_dir, 'model_best.pth.tar') if os.path.isfile(resume): print("=> loading checkpoint '{}'".format(resume)) checkpoint = torch.load(resume) args.start_epoch = checkpoint['epoch'] save_dict = checkpoint['state_dict'] model_dict = model.state_dict() trash_vars = [ k for k in save_dict.keys() if k not in model_dict.keys() ] print('trashed vars from resume dict:') print(trash_vars) resume_dict = {k: v for k, v in save_dict.items() if k in model_dict} # resume_dict['module.linear.cpars'] = save_dict['module.linear.weight'] model_dict.update(resume_dict) model.load_state_dict(model_dict) # model.load_state_dict(checkpoint['state_dict']) print("=> loaded checkpoint '{}' (epoch {})".format( resume, checkpoint['epoch'])) else: print("=> no checkpoint found at '{}'".format(resume)) # return cudnn.benchmark = True # load data immean = [0.485, 0.456, 0.406] # RGB channel mean for imagenet imstd = [0.229, 0.224, 0.225] transformations = transforms.Compose([ transforms.ToPILImage(), transforms.Resize([224, 224]), transforms.ToTensor(), transforms.Normalize(immean, imstd) ]) tuberlin_zero_ext = TUBerlinDataset(split='zero', version='ImageResized_ready', zero_version = args.zero_version, \ transform=transformations, aug=False) zero_loader_ext = DataLoader(dataset=tuberlin_zero_ext, \ batch_size=args.batch_size, shuffle=False, num_workers=0) tuberlin_zero = TUBerlinDataset(split='zero', zero_version=args.zero_version, transform=transformations, aug=False) zero_loader = DataLoader(dataset=tuberlin_zero, batch_size=args.batch_size, shuffle=False, num_workers=0) print(str(datetime.datetime.now()) + ' data loaded.') predicted_features_gallery, gt_labels_gallery = get_features( zero_loader_ext, model) predicted_features_query, gt_labels_query = get_features( zero_loader, model, 0) scores = -cdist(predicted_features_query, predicted_features_gallery) print('euclidean distance calculated') with open(os.path.join(args.resume_dir, 'features_zero.pickle'), 'wb') as fh: pickle.dump([predicted_features_gallery, gt_labels_gallery, \ predicted_features_query, gt_labels_query, \ None],fh) return predicted_features_gallery, gt_labels_gallery, predicted_features_query, gt_labels_query, scores
class MNISTDataset(SupervisedDataset): """""" def __init__(self, data_dir: Path, split: SplitEnum = SplitEnum.training): super().__init__() if split == SplitEnum.training: self._dataset = datasets.MNIST( str(data_dir), train=True, download=True, transform=self.trans ) else: self._dataset = datasets.MNIST( str(data_dir), train=False, download=True, transform=self.trans ) def __getitem__(self, index): return self._dataset.__getitem__(index) def __len__(self): return self._dataset.__len__() @property def predictor_shape(self) -> Tuple[int, ...]: """ :return: :rtype:""" return 1, 28, 28 @property def response_shape(self) -> Tuple[int, ...]: """ :return: :rtype:""" return (10,) trans = transforms.Compose( [transforms.ToTensor(), transforms.Normalize((0.1307,), (0.3081,))] ) inverse_transform = transforms.Compose( [ # transforms.Normalize((-mean / std).tolist(), (1.0 / std).tolist()), # TODO: imp transforms.ToPILImage() ] ) @staticmethod def get_train_valid_loader( data_dir: Path, *, batch_size: int, random_seed: int, valid_size: float = 0.1, shuffle: bool = True, num_workers: int = 0, pin_memory: bool = False, using_cuda: bool = True, ) -> Tuple[torch.utils.data.DataLoader, torch.utils.data.DataLoader]: """Train and validation data loaders. If using CUDA, num_workers should be set to 1 and pin_memory to True. Args: data_dir: path directory to the dataset. batch_size: how many samples per batch to load. 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]. In the paper, this number is set to 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. :param data_dir: :type data_dir: :param batch_size: :type batch_size: :param random_seed: :type random_seed: :param valid_size: :type valid_size: :param shuffle: :type shuffle: :param num_workers: :type num_workers: :param pin_memory: :type pin_memory: :param using_cuda: :type using_cuda:""" error_msg = "[!] valid_size should be in the range [0, 1]." assert (valid_size >= 0) and (valid_size <= 1), error_msg if using_cuda: pass # assert num_workers == 1 # assert pin_memory == True dataset = MNISTDataset(data_dir) num_train = len(dataset) indices = list(range(num_train)) split = int(numpy.floor(valid_size * num_train)) if shuffle: numpy.random.seed(random_seed) numpy.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( dataset, batch_size=batch_size, sampler=train_sampler, num_workers=num_workers, pin_memory=pin_memory, ) valid_loader = torch.utils.data.DataLoader( dataset, batch_size=batch_size, sampler=valid_sampler, num_workers=num_workers, pin_memory=pin_memory, ) return train_loader, valid_loader @staticmethod def get_test_loader( data_dir: Path, batch_size: int, *, num_workers: int = 0, pin_memory: bool = False, using_cuda: bool = True, ) -> torch.utils.data.DataLoader: """Test datalaoder. If using CUDA, num_workers should be set to 1 and pin_memory to True. Args: data_dir: path directory to the dataset. batch_size: how many samples per batch to load. 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. :param data_dir: :type data_dir: :param batch_size: :type batch_size: :param num_workers: :type num_workers: :param pin_memory: :type pin_memory: :param using_cuda: :type using_cuda:""" # define transforms if using_cuda: pass # assert num_workers == 1 # assert pin_memory == True dataset = MNISTDataset(data_dir, split=SplitEnum.testing) data_loader = torch.utils.data.DataLoader( dataset, batch_size=batch_size, shuffle=False, num_workers=num_workers, pin_memory=pin_memory, ) return data_loader def sample(self) -> None: """""" images, labels = next( iter( torch.utils.data.DataLoader( self, batch_size=9, shuffle=True, num_workers=0, pin_memory=global_pin_memory(0), ) ) ) X = images.numpy() X = numpy.transpose(X, [0, 2, 3, 1]) MNISTDataset.plot_images(X, labels) @staticmethod def plot_images(images: numpy.ndarray, label: Sequence) -> None: """ :param images: :type images: :param label: :type label:""" images = images.squeeze() assert len(images) == len(label) == 9 fig, axes = pyplot.subplots(3, 3) for i, ax in enumerate(axes.flat): ax.imshow(images[i], cmap="Greys_r") xlabel = f"{label[i]}" ax.set_xlabel(xlabel) ax.set_xticks([]) ax.set_yticks([]) pyplot.show()
def train(resume = False): learning_rate = 3e-4 epoches = 50 batch_size = 8 train_transform = transforms.Compose([ transforms.ToPILImage(), transforms.RandomHorizontalFlip(p=0.5), transforms.RandomCrop((500,500), pad_if_needed = True, padding_mode = "reflect"), transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) ]) val_transform = transforms.Compose([ transforms.ToPILImage(), transforms.CenterCrop(500), transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) ]) val_data = CassavaDataset(mode="val", transform=val_transform) val_loader = DataLoader(val_data, batch_size=batch_size, shuffle=False) train_data = CassavaDataset(mode="train", transform=train_transform) train_loader = DataLoader(train_data, batch_size=batch_size, shuffle=True, num_workers=4) if resume: learning_rate = 3e-5 model = torch.load("densenet201.pt", map_location=lambda storage, loc: storage) else: # model = inceptionresnetv2(num_classes=5, pretrained='imagenet') # model = resnext50_32x4d(pretrained=True, num_classes = 5) # model = se_resnext101_32x4d(num_classes=5, pretrained='imagenet') # model = senet154(num_classes=5, pretrained='imagenet') model = densenet201(pretrained=True, num_classes = 5) model = torch.nn.DataParallel(model) model.cuda() # optimizer = optim.SGD(model.parameters(), lr= learning_rate, momentum=0.9, weight_decay=0.0005) optimizer = optim.Adam(model.parameters(), lr=learning_rate, weight_decay=0.0005) # exp_lr_scheduler = lr_scheduler.CosineAnnealingLR(optimizer, epoches, eta_min=1e-7) # exp_lr_scheduler = CosineWithRestarts(optimizer, T_max = 10, factor = 2) # exp_lr_scheduler = lr_scheduler.ReduceLROnPlateau(optimizer, mode = "min", verbose = True) exp_lr_scheduler = lr_scheduler.MultiStepLR(optimizer, milestones=[20,35,45], gamma=0.5) # exp_lr_scheduler = lr_scheduler.StepLR(optimizer, step_size = 10, gamma=0.5) running_loss_list = [] best_acc = 0 # criterion = nn.CrossEntropyLoss() criterion = FocalLoss() if train: print("start training") for i in range(epoches): running_loss = 0. running_dice = 0. start_time = time.time() count = 0 current_best = 100 model.train() for data in train_loader: count +=1 img , label = data img = Variable(img).cuda() label = Variable(label).cuda() batch_size = train_loader.batch_size optimizer.zero_grad() output = model(img) loss = criterion(output, label) loss.backward() optimizer.step() running_loss += loss.item() running_loss = running_loss / count print(np.floor(time.time() - start_time)) print("[%d/%d] Loss: %.5f" % (i + 1, epoches, running_loss)) running_loss_list.append(running_loss) result_list = [] label_list = [] running_loss = 0 model.eval() for idx, data in enumerate(val_loader): img, label = data img = Variable(img).cuda() label = Variable(label).cuda() batch_size = val_loader.batch_size output = model(img) output = torch.max(F.softmax(output, dim = 1), dim = 1)[1] result_list += list(output.cpu().numpy()) label_list += list(label.cpu().numpy()) acc_count = 0 for r in range(len(result_list)): if result_list[r] == label_list[r]: acc_count += 1 acc = acc_count/len(result_list) if acc > best_acc: print("current best", acc) torch.save(model,'densenet201.pt') best_acc = acc exp_lr_scheduler.step() if i %10 == 0: torch.save(model, 'densenet201-May31.pt') file = open("densenet201.txt","w") for l in running_loss_list: file.write("%s\n" % l) file.close() torch.save(model, 'densenet201-May31.pt')
def main(train_args): net = FCN8s(num_classes=voc.num_classes).cuda() if len(train_args['snapshot']) == 0: curr_epoch = 1 train_args['best_record'] = {'epoch': 0, 'val_loss': 1e10, 'acc': 0, 'acc_cls': 0, 'mean_iu': 0, 'fwavacc': 0} else: print('training resumes from ' + train_args['snapshot']) net.load_state_dict(torch.load(os.path.join(ckpt_path, exp_name, train_args['snapshot']))) split_snapshot = train_args['snapshot'].split('_') curr_epoch = int(split_snapshot[1]) + 1 train_args['best_record'] = {'epoch': int(split_snapshot[1]), 'val_loss': float(split_snapshot[3]), 'acc': float(split_snapshot[5]), 'acc_cls': float(split_snapshot[7]), 'mean_iu': float(split_snapshot[9]), 'fwavacc': float(split_snapshot[11])} net.train() mean_std = ([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]) input_transform = standard_transforms.Compose([ standard_transforms.ToTensor(), standard_transforms.Normalize(*mean_std) ]) target_transform = extended_transforms.MaskToTensor() restore_transform = standard_transforms.Compose([ extended_transforms.DeNormalize(*mean_std), standard_transforms.ToPILImage(), ]) visualize = standard_transforms.Compose([ standard_transforms.Scale(400), standard_transforms.CenterCrop(400), standard_transforms.ToTensor() ]) train_set = voc.VOC('train', transform=input_transform, target_transform=target_transform) train_loader = DataLoader(train_set, batch_size=1, num_workers=4, shuffle=True) val_set = voc.VOC('val', transform=input_transform, target_transform=target_transform) val_loader = DataLoader(val_set, batch_size=1, num_workers=4, shuffle=False) criterion = CrossEntropyLoss2d(size_average=False, ignore_index=voc.ignore_label).cuda() optimizer = optim.Adam([ {'params': [param for name, param in net.named_parameters() if name[-4:] == 'bias'], 'lr': 2 * train_args['lr']}, {'params': [param for name, param in net.named_parameters() if name[-4:] != 'bias'], 'lr': train_args['lr'], 'weight_decay': train_args['weight_decay']} ], betas=(train_args['momentum'], 0.999)) if len(train_args['snapshot']) > 0: optimizer.load_state_dict(torch.load(os.path.join(ckpt_path, exp_name, 'opt_' + train_args['snapshot']))) optimizer.param_groups[0]['lr'] = 2 * train_args['lr'] optimizer.param_groups[1]['lr'] = train_args['lr'] check_mkdir(ckpt_path) check_mkdir(os.path.join(ckpt_path, exp_name)) open(os.path.join(ckpt_path, exp_name, str(datetime.datetime.now()) + '.txt'), 'w').write(str(train_args) + '\n\n') scheduler = ReduceLROnPlateau(optimizer, 'min', patience=train_args['lr_patience'], min_lr=1e-10, verbose=True) for epoch in range(curr_epoch, train_args['epoch_num'] + 1): train(train_loader, net, criterion, optimizer, epoch, train_args) val_loss = validate(val_loader, net, criterion, optimizer, epoch, train_args, restore_transform, visualize) scheduler.step(val_loss)
def normalize_output(img): img = img - img.min() img = img / img.max() img = transforms.ToPILImage()(img) return img
def build_dataset(dataset, num_meta): 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(x.unsqueeze(0), (4, 4, 4, 4), mode='reflect').squeeze()), transforms.ToPILImage(), transforms.RandomCrop(32), transforms.RandomHorizontalFlip(), transforms.ToTensor(), normalize, ]) transform_test = transforms.Compose([transforms.ToTensor(), normalize]) if dataset == 'cifar10': train_dataset = torchvision.datasets.CIFAR10(root='../data', train=True, download=True, transform=transform_train) test_dataset = torchvision.datasets.CIFAR10('../data', train=False, transform=transform_test) img_num_list = [num_meta] * 10 num_classes = 10 if dataset == 'cifar100': train_dataset = torchvision.datasets.CIFAR100( root='../data', train=True, download=True, transform=transform_train) test_dataset = torchvision.datasets.CIFAR100('../data', train=False, transform=transform_test) img_num_list = [num_meta] * 100 num_classes = 100 data_list_val = {} for j in range(num_classes): data_list_val[j] = [ i for i, label in enumerate(train_dataset.targets) if label == j ] idx_to_meta = [] idx_to_train = [] print(img_num_list) for cls_idx, img_id_list in data_list_val.items(): np.random.shuffle(img_id_list) img_num = img_num_list[int(cls_idx)] idx_to_meta.extend(img_id_list[:img_num]) idx_to_train.extend(img_id_list[img_num:]) train_data = copy.deepcopy(train_dataset) train_data_meta = copy.deepcopy(train_dataset) train_data_meta.data = np.delete(train_dataset.data, idx_to_train, axis=0) train_data_meta.targets = np.delete(train_dataset.targets, idx_to_train, axis=0) train_data.data = np.delete(train_dataset.data, idx_to_meta, axis=0) train_data.targets = np.delete(train_dataset.targets, idx_to_meta, axis=0) return train_data_meta, train_data, test_dataset
import pdb from config import get_config from Learner import face_learner from data.data_pipe import get_val_pair from torchvision import transforms as trans conf = get_config(training=False) learner = face_learner(conf, inference=True) learner.load_state(conf, 'final.pth', model_only=True, from_save_folder=True) lfw, lfw_issame = get_val_pair(conf.emore_folder, 'lfw') accuracy, best_threshold, roc_curve_tensor = learner.evaluate(conf, lfw, lfw_issame, nrof_folds=10, tta=False) print('lfw - accuray:{}, threshold:{}'.format(accuracy, best_threshold)) trans.ToPILImage()(roc_curve_tensor)
model.eval() transformer = data_transforms['val'] lines = [] print('evaluating...') num_files = len(files) for i in tqdm(range(num_files)): file = 'word_{}.png'.format(i + 1) im_fn = os.path.join(image_folder, file) img = cv.imread(im_fn) img = cv.resize(img, (imgW, imgH), cv.INTER_CUBIC) img = img[..., ::-1] # RGB img = transforms.ToPILImage()(img) img = transformer(img) img = img.to(device) img = img.unsqueeze(0) preds = model(img) _, preds = preds.max(2) preds = preds.transpose(1, 0).contiguous().view(-1) preds_size = Variable(torch.IntTensor([preds.size(0)])) sim_pred = converter.decode(preds.data, preds_size.data, raw=False) lines.append('{}, \"{}\"\n'.format(file, sim_pred)) with open('submit.txt', 'w') as file:
def main(): global args, best_prec1 args = parser.parse_args() if args.tensorboard: configure("runs/%s"%(args.name)) # Data loading code 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]]) if args.augment: transform_train = transforms.Compose([ transforms.ToTensor(), transforms.Lambda(lambda x: F.pad(x.unsqueeze(0), (4,4,4,4),mode='reflect').squeeze()), transforms.ToPILImage(), transforms.RandomCrop(32), transforms.RandomHorizontalFlip(), transforms.ToTensor(), normalize, ]) else: transform_train = transforms.Compose([ transforms.ToTensor(), normalize, ]) transform_test = transforms.Compose([ transforms.ToTensor(), normalize ]) kwargs = {'num_workers': 1, 'pin_memory': True} assert(args.dataset == 'cifar10' or args.dataset == 'cifar100') train_loader = torch.utils.data.DataLoader( datasets.__dict__[args.dataset.upper()]('../data', train=True, download=True, transform=transform_train), batch_size=args.batch_size, shuffle=True, **kwargs) val_loader = torch.utils.data.DataLoader( datasets.__dict__[args.dataset.upper()]('../data', train=False, transform=transform_test), batch_size=args.batch_size, shuffle=True, **kwargs) # create model model = WideResNet(args.layers, args.dataset == 'cifar10' and 10 or 100, args.widen_factor, dropRate=args.droprate) # get the number of model parameters print('Number of model parameters: {}'.format( sum([p.data.nelement() for p in model.parameters()]))) # for training on multiple GPUs. # Use CUDA_VISIBLE_DEVICES=0,1 to specify which GPUs to use # model = torch.nn.DataParallel(model).cuda() model = model.cuda() # 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_prec1 = checkpoint['best_prec1'] model.load_state_dict(checkpoint['state_dict']) print("=> loaded checkpoint '{}' (epoch {})" .format(args.resume, checkpoint['epoch'])) else: print("=> no checkpoint found at '{}'".format(args.resume)) cudnn.benchmark = True # define loss function (criterion) and optimizer criterion = nn.CrossEntropyLoss().cuda() optimizer = torch.optim.SGD(model.parameters(), args.lr, momentum=args.momentum, nesterov = args.nesterov, weight_decay=args.weight_decay) for epoch in range(args.start_epoch, args.epochs): adjust_learning_rate(optimizer, epoch+1) # train for one epoch train(train_loader, model, criterion, optimizer, epoch) # evaluate on validation set prec1 = validate(val_loader, model, criterion, epoch) # remember best prec@1 and save checkpoint is_best = prec1 > best_prec1 best_prec1 = max(prec1, best_prec1) save_checkpoint({ 'epoch': epoch + 1, 'state_dict': model.state_dict(), 'best_prec1': best_prec1, }, is_best) print('Best accuracy: ', best_prec1)
def load_grayscale_from_colored(path): # Crop so that both dimensions are multiples of 32 original = io.imread(path)[4:-4] PIL_image = transforms.ToPILImage()(original) return colored_to_grayscale(PIL_image)
import cv2 # set up matplotlib is_ipython = 'inline' in matplotlib.get_backend() if is_ipython: from IPython import display plt.ion() # if gpu is to be used device = torch.device("cuda" if torch.cuda.is_available() else "cpu") Transition = namedtuple('Transition', ('state', 'action', 'next_state', 'reward')) resize = T.Compose([T.ToPILImage(), T.Resize(40, interpolation=Image.CUBIC), T.ToTensor()]) class DQN(nn.Module): def __init__(self, num_actions): super(DQN, self).__init__() self.conv1 = nn.Conv2d(3, 32, kernel_size=3, stride=1) self.maxpool1 = nn.MaxPool2d(2) self.bn1 = nn.BatchNorm2d(32) self.conv2 = nn.Conv2d(32, 32, kernel_size=3, stride=1) self.maxpool2 = nn.MaxPool2d(2) self.bn2 = nn.BatchNorm2d(32)
def load_grayscale(path): # Crop so that both dimensions are multiples of 32 original = io.imread(path)[4:-4] PIL_image = transforms.ToPILImage()( original) # Image already has 3 channels. Just normalize return normalize(PIL_image)
def aug(self, image, mask, aug_range): # transform pil image pil_image = transforms.ToPILImage() image = pil_image(image) mask = pil_image(mask) if aug_range == 'aug6': random_factor1 = random.random() random_factor2 = random.random() if random_factor1 > 0.5: # brightness b_factor = np.round((random.uniform(0.3, 1.1)), 1) image = tF.adjust_brightness(image, b_factor) if b_factor > 0.8: # contrast c_factor = np.round((random.uniform(0.8, 1.1)), 1) image = tF.adjust_contrast(image, c_factor) else: # contrast c_factor = np.round((random.uniform(0.3, 1.1)), 1) image = tF.adjust_contrast(image, c_factor) else: if random_factor2 > 0.5: b_factor = np.round((random.uniform(0.3, 1.1)), 1) image = tF.adjust_brightness(image,b_factor) else: c_factor = np.round((random.uniform(0.3, 1.1)), 1) image = tF.adjust_contrast(image, c_factor) elif aug_range == 'aug7': b_factor = random.uniform(0.4, 1.4) image = tF.adjust_brightness(image, b_factor) c_factor = random.uniform(0.4, 1.4) image = tF.adjust_contrast(image, c_factor) elif aug_range == 'aug9': color_jitter = transforms.ColorJitter(brightness=0.2, contrast=0.2) image = color_jitter(image) elif aug_range == 'aug11': #resized_crop = transforms.RandomResizedCrop(256, scale=(0.8,1.0)) #color_jitter = transforms.ColorJitter(brightness=0.4, contrast=0.4) color_jitter = transforms.ColorJitter(brightness=0.2, contrast=0.2) #transform = transforms.Compose([resized_crop, color_jitter]) image = color_jitter(image) i, j, h, w = transforms.RandomResizedCrop.get_params(image, scale=(0.8,1.0), ratio=(0.9,1.1)) image = transforms.functional.resized_crop(image, i, j, h, w, (256,256)) mask = transforms.functional.resized_crop(mask, i, j, h, w, (256,256)) image = np.array(image) mask = np.array(mask) return image, mask image = np.array(image) return image
# hpc train_df_path = 'chest_xray_origin/train_3.csv' val_df_path = 'chest_xray_origin/val_3.csv' test_df_path = 'chest_xray_origin/test_3.csv' root_dir = 'chest_xray_origin/all/' bs = 10 epochs = 100 ################################### ############ load data ############ ################################### train_transform = transforms.Compose([ transforms.ToPILImage(), transforms.Resize([364, 364]), transforms.RandomResizedCrop(320), transforms.RandomHorizontalFlip(), transforms.ToTensor(), transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]) ]) validation_transform = transforms.Compose([ transforms.ToPILImage(), transforms.Resize([364, 364]), transforms.CenterCrop(320), transforms.ToTensor(), transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]) ])
def train_CNN_bad_data_split(params): # Initialize the model print('Do transfer learning with existed model trained on ImageNet!\n') print('The chosen network is %s !' % params.model) # most of the imagenet pretrained model has this input size trg_size = (224, 224) # All pre-trained models expect input images normalized in the same way, # i.e. mini-batches of 3-channel RGB images of shape (3 x H x W), where H # and W are expected to be at least 224. The images have to be loaded in to # a range of [0, 1] and then normalized using mean = [0.485, 0.456, 0.406] # and std = [0.229, 0.224, 0.225]. transformations = transforms.Compose([ MinMaxNormalization(), transforms.ToPILImage(), transforms.Resize(trg_size), transforms.ToTensor() ]) params.dropout = 0.8 total_time = time() if params.split is None: fold_iterator = range(params.n_splits) else: fold_iterator = [params.split] for fi in fold_iterator: print("Running for the %d-th fold" % fi) training_sub_df, valid_sub_df = load_data(params.tsv_path, params.diagnoses, fi, n_splits=params.n_splits, baseline=params.baseline) # split the training + validation by slice training_df, valid_df = mix_slices(training_sub_df, valid_sub_df, mri_plane=params.mri_plane) data_train = MRIDatasetSlice(params.caps_directory, training_df, transformations=transformations, mri_plane=params.mri_plane, prepare_dl=params.prepare_dl, mixed=True) data_valid = MRIDatasetSlice(params.caps_directory, valid_df, transformations=transformations, mri_plane=params.mri_plane, prepare_dl=params.prepare_dl, mixed=True) # Use argument load to distinguish training and testing train_loader = DataLoader(data_train, batch_size=params.batch_size, shuffle=True, num_workers=params.num_workers, pin_memory=True) valid_loader = DataLoader(data_valid, batch_size=params.batch_size, shuffle=False, num_workers=params.num_workers, pin_memory=True) # Initialize the model print('Initialization of the model') model = init_model(params.model, gpu=params.gpu, dropout=params.dropout) # Define criterion and optimizer criterion = torch.nn.CrossEntropyLoss() optimizer = eval("torch.optim." + params.optimizer)( filter(lambda x: x.requires_grad, model.parameters()), lr=params.learning_rate, weight_decay=params.weight_decay) setattr(params, 'beginning_epoch', 0) # Define output directories log_dir = os.path.join(params.output_dir, 'fold-%i' % fi, 'tensorboard_logs') model_dir = os.path.join(params.output_dir, 'fold-%i' % fi, 'models') print('Beginning the training task') train(model, train_loader, valid_loader, criterion, optimizer, False, log_dir, model_dir, params) test_cnn(train_loader, "train", fi, criterion, options) test_cnn(valid_loader, "validation", fi, criterion, options) total_time = time() - total_time print("Total time of computation: %d s" % total_time)
def __init__(self, data_root): self.transform = T.ToTensor() self.transform1 = T.ToPILImage() self.data_root = data_root
def __init__(self, dataset_dir, images_path, list=[], numpatches=900, numneg=3, pos_thr=50.0, reject=True, mode='train', rejection_radius=3000, dist_type='3D', patch_radius=None, use_depth=False, use_normals=False, use_silhouettes=False, color_jitter=False, greyscale=False, maxres=4096, scale_jitter=False, photo_jitter=False, uniform_negatives=False, needles=0, render_only=False): """Loads the patches dataset. @param dataset_dir String directory where the dataset of sampled points is located @param images_path path to the images to sample patches from @param list List of subdirectory names to be loaded with this loader. Use this to specify train/test/val splits. @param numneg Int number of generated negatives per positive pair. @param pos_thr Float threshold in meters used to define negatives. If the distance of two 3D points exceeds this threshold, the correspondence is considered negative. The lower the threshold, the harder the negatives are. @param reject [bool] True turns on rejetion sampling - for each patch we calculate density of 3D reprojected point cloud within 1km radius. Then the probability of rejection is calculated as num_points_1km_radius/max_num_points, where max_num_points is maximum taken across all queried samples until the current one. @param mode options: train|eval, default: train. If train is used, then the additional metadata per patch (which are used for some plots during validation are not generated and therefore the training shall be faster. @type string @param dist_type type of the distance used to generate positives and negatives. Can be `2D` or `3D`. Default: 3D. @type int @param patch_radius when set to None, the patch radius will be loaded from the patches dataset. Otherwise the defined patch radius will be used. Please note that if you use larger patch_radius than the one defined within the patches dataset, the source image will be padded automatically and so the patch may contain black edges. @param needles If number greater than zero is used, then instead of a single patch a whole needle of patches will be extracted. Our network then takes several patches in a form of a needle encoded to channels of the input. This approach is described here: Lotan and Irani: Needle-Match: Reliable Patch Matching under High Uncertainty, CVPR 2016. """ self.item_idx = -1 self.dataset_dir = dataset_dir self.images_path = images_path self.numneg = numneg self.pos_thr = pos_thr self.loaded_imgs_pts = [] self.all_coords3d = [] self.max_num_points = 0 self.reject = reject self.query_radius = rejection_radius self.dist_type = dist_type self.use_depth = use_depth self.use_normals = use_normals self.use_silhouettes = use_silhouettes self.color_jitter = color_jitter self.greyscale = greyscale self.left_maxres = maxres self.right_maxres = maxres self.scale_jitter = scale_jitter self.photo_jitter = photo_jitter self.uniform_negatives = uniform_negatives self.needles = needles self.render_only = render_only scene_info_file = os.path.join(os.path.dirname(images_path), "scene_info.txt") self.scene_center = MultimodalPatchesDataset.getSceneCenter( scene_info_file) self.numch_1 = 3 self.numch_2 = 3 if self.greyscale: self.numch_1 = 1 self.numch_2 = 1 if self.use_depth: self.numch_2 += 1 if self.use_normals: self.numch_2 += 3 if self.use_silhouettes: self.numch_2 += 1 self.transform = transforms.Compose([ transforms.ToPILImage(), transforms.ColorJitter(0.5, 0.5, 1.0, 0.5), transforms.ToTensor() ]) print("Rejection radius: ", self.query_radius, "mode", mode) self.mode = mode if len(list) == 0: self.dataset_items = [ d for d in os.listdir(self.dataset_dir) if os.path.isdir(os.path.join(self.dataset_dir, d)) ] else: self.dataset_items = [] if self.mode == 'eval': # choose only pairs where left view does not repeat print("Choosing non-repeating photographs for validation...") keyset = set() for item in tqdm(list): item_path = os.path.join(self.dataset_dir, item) info_path = os.path.join(item_path, "info.npy") info = np.load(info_path, encoding='latin1', allow_pickle=True).flatten()[0] img1_base = os.path.basename(info['img1_name']) key = os.path.splitext(img1_base)[0] if key in keyset: continue keyset.add(key) self.dataset_items.append(item) else: self.dataset_items = list if (len(self.dataset_items) > 0): item_path = os.path.join(self.dataset_dir, self.dataset_items[0]) info_path = os.path.join(item_path, "info.npy") self.info = np.load(info_path, encoding='latin1', allow_pickle=True).flatten()[0] self.numpatches = self.info['coords2d_1'].shape[0] if patch_radius is not None: self.patch_radius = patch_radius else: self.patch_radius = self.info['patch_radius'] if numpatches != self.numpatches: raise RuntimeError("Wrong number of patches in the first \ item of the dataset. Expected: " + str(numpatches) + ", obtained: " + str(self.numpatches)) self.load3DPoints() self.kdt = KDTree(self.all_coords3d[:, :3], leaf_size=40, metric='euclidean') translation_frac = np.sqrt(5) / (self.patch_radius * 2 ) # at most 5px self.photo_transform = transforms.Compose([ transforms.ToPILImage(), transforms.ColorJitter(0.2, (0.9, 1.001), 0.2, 0.2), transforms.RandomAffine(22.5, (translation_frac, translation_frac), shear=5), transforms.CenterCrop(self.patch_radius * 2), transforms.ToTensor() ]) if self.photo_jitter: self.prcoef = 1.25 else: self.prcoef = 1 # FIXME: remove since this is unneeded for training and is slow. Just for research. #self.saveDensityPointcloud() else: raise RuntimeError("No dataset items at specified location.")
convh = conv2d_size_out(conv2d_size_out(conv2d_size_out(h))) linear_input_size = convw * convh * 32 self.head = nn.Linear(linear_input_size, outputs) # Called with either one element to determine next action, or a batch # during optimization. Returns tensor([[left0exp,right0exp]...]). def forward(self, x): x = F.relu(self.bn1(self.conv1(x))) x = F.relu(self.bn2(self.conv2(x))) x = F.relu(self.bn3(self.conv3(x))) return self.head(x.view(x.size(0), -1)) ###### Input extraction ######### resize = T.Compose( [T.ToPILImage(), T.Resize(40, interpolation=Image.CUBIC), T.ToTensor()]) def get_cart_location(screen_width): world_width = env.x_threshold * 2 scale = screen_width / world_width return int(env.state[0] * scale + screen_width / 2.0) # MIDDLE OF CART def get_screen(): # Returned screen requested by gym is 400x600x3, but is sometimes larger # such as 800x1200x3. Transpose it into torch order (CHW). screen = env.render(mode='rgb_array').transpose((2, 0, 1)) # Cart is in the lower half, so strip off the top and bottom of the screen
torch.backends.cudnn.benchmark = True mean_std = ([0.446139603853, 0.409515678883, 0.395083993673], [0.288205742836, 0.278144598007, 0.283502370119]) img_transform = standard_transforms.Compose([ standard_transforms.ToTensor(), standard_transforms.Normalize(*mean_std) ]) dot_transform = standard_transforms.Compose([ standard_transforms.ToTensor(), own_transforms.tensormul(255.0) ]) restore = standard_transforms.Compose([ own_transforms.DeNormalize(*mean_std), standard_transforms.ToPILImage() ]) pil_to_tensor = standard_transforms.ToTensor() LOG_PARA = 100.0 dataRoot = '../ProcessedData/Data.2019.11/NWPU/1204_min_576x768_mod16_2048' model_path = 'exp/12-06_15-03_NWPU_Res101_SFCN_1e-05/latest_state.pth' os.makedirs('pred', exist_ok=True) def main(): txtpath = os.path.join(dataRoot, 'txt_list', 'val.txt') with open(txtpath) as f: lines = f.readlines()
"Enter the path to the image you would like to use: ") image_index = dataset.search_for_item(image_path) if image_index < 0: print("Invalid image selection. Please try again.") data_sample = dataset[image_index] """ Get the predictions from the saved model on the input image """ with torch.no_grad(): best_network = Network() best_network.cuda() best_network.load_state_dict( torch.load('/home/arjun/Desktop/cv/pa3/src/model/model_state.pth')) best_network.eval() image = data_sample[0] landmarks = np.array((data_sample[1] + 0.5) * 224) crops = dataset.crops[image_index] batch = torch.stack(tuple(image for i in range(0, 64))) batch = batch.cuda() prediction = (best_network(batch).cpu()[0] + 0.5) * 224 plt.imshow(transforms.ToPILImage()(image), cmap='gray') #plt.scatter(landmarks[:,0], landmarks[:,1], s = 5, c = 'g') plt.scatter(prediction[::2], prediction[1::2], s=5, c='r') plt.show()
self.fc1 = nn.Linear(500, 10) self.fc2 = nn.Linear(10, 2) def forward(self, x): x = F.relu(F.max_pool2d(self.conv1(x), 2)) x = F.relu(F.max_pool2d(self.conv2_drop(self.conv2(x)), 2)) x = x.view(x.shape[0],-1) x = F.relu(self.fc1(x)) x = F.dropout(x, training=self.training) x = self.fc2(x) return x means = np.array([0.485, 0.456, 0.406]) std = np.array([0.229, 0.224, 0.225]) test_transform = transforms.Compose([transforms.ToPILImage(), transforms.Resize([28,28]), transforms.ToTensor(), transforms.Normalize(means,std), ]) # test_data = CustomDataset(pd.read_csv("test.csv"), './Data/Cancer/Test', test_transform ) # test_loader = DataLoader(dataset = test_data, batch_size = 25, shuffle=False, num_workers=0) device = "cuda" if torch.cuda.is_available() else "cpu" # model = torch.hub.load('pytorch/vision:v0.9.0', 'googlenet', pretrained=False) # model.load_state_dict(torch.load("modelwithoutpreprocessing.ckpt")) # model.eval() # it-disables-dropout # with torch.no_grad(): # correct = 0 # total = 0 # for i,(images, labels) in enumerate(test_loader):
den_frame.spines['right'].set_visible(False) plt.savefig(den_path.replace('den_test','datu') + '/' + '99_fakel_label' + '_gt_' + str(int(gt)) + '.png', \ bbox_inches='tight', pad_inches=0, dpi=600) plt.close() if __name__ == '__main__': from dataloader.setting import cfg_data mean_std = cfg_data.MEAN_STD img_transform = standard_transforms.Compose([ standard_transforms.ToTensor(), standard_transforms.Normalize(*mean_std) ]) restore = standard_transforms.Compose([standard_transforms.ToPILImage()]) pil_to_tensor = standard_transforms.ToTensor() if dataset == 'GCC2SHHA': tarRoot = cfg_data.SHHA_DATA_PATH tar_test_list = os.path.join(cfg_data.SHHA_scene_dir + '/test', 'test.txt') cc_path = './exp/SHHA/02-27_09-35_SHHA_vgg16__lr1e-05_gamma0.98_step_10%DA/all_ep_287_mae_119.1_mse_211.4.pth' if dataset == 'GCC2SHHB': tarRoot = cfg_data.SHHB_DATA_PATH tar_test_list = os.path.join(cfg_data.SHHB_scene_dir + '/train', 'train_.txt') cc_path = './exp/SHHB/04-01_06-55_SHHB_vgg16__lr1e-05_DA_train/all_ep_45_mae_12.7_mse_20.0.pth' if dataset == 'GCC2QNRF': tarRoot = cfg_data.QNRF_DATA_PATH tar_test_list = os.path.join(cfg_data.QNRF_scene_dir + '/test',