def set_transformers(self): self.transforms = { self.augmentation_name[0]: transforms.Compose([ToTensor()]), self.augmentation_name[1]: transforms.Compose([ # Choose from tranforms_list randomly transforms.RandomChoice(self.randomchoice['choice1']), ToTensor() ]), self.augmentation_name[2]: transforms.Compose([ transforms.RandomChoice(self.randomchoice['choice2']), ToTensor() ]), self.augmentation_name[3]: transforms.Compose([ transforms.RandomChoice(self.randomchoice['choice3']), ToTensor() ]), self.augmentation_name[4]: transforms.Compose([ transforms.RandomChoice(self.randomchoice['choice4']), ToTensor() ]) }
def preprocess(self, phase): if phase == 'train': preprocess = transforms.Compose([ FixedResize(self.output_size), Translate(50, 255), # RandomScale(), CutOut(64), RandomHorizontalFlip(), AdjustColor(self.factor), RandomGaussianBlur(self.radius), Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)), ToTensor(), ]) elif phase == 'val': preprocess = transforms.Compose([ FixedResize(self.output_size), Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)), ToTensor(), ]) elif phase == 'test': preprocess = transforms.Compose([ FixedResize(self.output_size, is_resize=False), Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)), ToTensor(), ]) else: raise NotImplementedError return preprocess
def load_dataset(self): self.dataset = self.dataset_class(txt_file='testing.txt', root_dir=self.root_dir, transform=transforms.Compose([ Resize((64, 64)), ToTensor() ]) ) self.dataloader = DataLoader(self.dataset, batch_size=self.batch_size, shuffle=self.is_shuffle, num_workers=self.num_workers)
def __init__(self, clean_file_path, noisy_file_path, frame_size, frame_shift): self.clean_test_name = os.listdir(clean_file_path) self.noisy_test_name = os.listdir(noisy_file_path) self.noisy_file_path = noisy_file_path self.clean_file_path = clean_file_path self.get_frames = SignalToFrames(frame_size=frame_size, frame_shift=frame_shift) self.to_tensor = ToTensor()
def set_transformers(self): self.transforms = { self.augmentation_name[0]: transforms.Compose( [ToPILImage(), ToTensor(), Resize(self.resize), OrigPad()]), self.augmentation_name[1]: transforms.Compose([ ToPILImage(), # Choose from tranforms_list randomly transforms.RandomChoice(self.randomchoice['choice1']), ToTensor(), Resize(self.resize), OrigPad() ]), self.augmentation_name[2]: transforms.Compose([ ToPILImage(), transforms.RandomChoice(self.randomchoice['choice2']), ToTensor(), Resize(self.resize), OrigPad() ]), self.augmentation_name[3]: transforms.Compose([ ToPILImage(), transforms.RandomChoice(self.randomchoice['choice3']), ToTensor(), Resize(self.resize), OrigPad() ]), self.augmentation_name[4]: transforms.Compose([ ToPILImage(), transforms.RandomChoice(self.randomchoice['choice4']), ToTensor(), Resize(self.resize), OrigPad() ]) }
def __init__(self, file_path, frame_size=512, frame_shift=256): #self.filename = filename with open(file_path, 'r') as validation_file_list: self.file_list = [ line.strip() for line in validation_file_list.readlines() ] self.get_frames = SignalToFrames(frame_size=frame_size, frame_shift=frame_shift) self.to_tensor = ToTensor()
def __init__(self, video_path, txt_path, max_frame_len=14, trainflag=True): ''' video_path:视频路径 txt_path:文本路径 trainflag:数据集类型:决定样本上的可用的可选变换 ''' self.video_path = video_path self.txt_path = txt_path self.max_frame_len = max_frame_len self.trainflag = trainflag self.common_transform = transforms.Compose([ Rescale(114), CenterCrop((112, 224)), ToTensor(), ColorNormalize() ]) self.data = self._load_data(self.txt_path)
def __init__(self, file_path, frame_size=512, frame_shift=256, nsamples=64000): # file_path is the path of training dataset # option1: '/data/KaiWang/pytorch_learn/pytorch_for_speech/dataset/timit_mix/trainset/two_data' # option2 : .txt file format file_path='/data/KaiWang/pytorch_learn/pytorch_for_speech/DDAEC/train_file_list' #self.file_list = glob.glob(os.path.join(file_path, '*')) with open(file_path, 'r') as train_file_list: self.file_list = [ line.strip() for line in train_file_list.readlines() ] self.nsamples = nsamples self.get_frames = SignalToFrames(frame_size=frame_size, frame_shift=frame_shift) self.to_tensor = ToTensor()
help='path to save output') args = parser.parse_args() print(args) device = torch.device("cuda:2" if torch.cuda.is_available() else "cpu") model = Hybird(args).to(device) path = "/home/yinzi/data4/fcn_res/exp_stage2/checkpoints7fc8d2bc/best.pth.tar" state = torch.load(path, map_location=device) model.load_state_dict(state['model']) print(path + " loaded") root_dir = {'test': "/home/yinzi/data4/warped/test"} transforms_list = {'test': transforms.Compose([ToTensor()])} # DataLoader Dataset = Warped_HelenDataset(root_dir=root_dir['test'], mode='test', transform=transforms_list['test']) dataloader = DataLoader(Dataset, batch_size=16, shuffle=True, num_workers=4) for batch in dataloader: image = batch['image'].to(device) name = batch['name'] boxes = batch['boxes'] warp_box = batch['warp_boxes'] N = image.shape[0] inverse_box = torch.round(warp_box * 256. + 256.).float().numpy() assert boxes.shape == (N, 4, 4)
# Dataset Read_in Part root_dir = { 'train': "/home/yinzi/data4/warped/train", 'val': "/home/yinzi/data4/warped/val", 'test': "/home/yinzi/data4/warped/test" } class DataLoaderX(DataLoader): def __iter__(self): return BackgroundGenerator(super().__iter__()) transforms_list = { 'train': transforms.Compose([PrepareLabels((128, 128)), ToTensor()]), 'val': transforms.Compose([PrepareLabels((128, 128)), ToTensor()]), 'test': transforms.Compose([PrepareLabels((128, 128)), ToTensor()]) } Dataset = { 'val': Warped_HelenDataset(root_dir=root_dir['val'], mode='val', transform=transforms_list['val']) } if args.datamore: # Stage 1 augmentation stage1_augmentation = Stage2DataAugmentation(dataset=Warped_HelenDataset,
device = torch.device("cuda") else: device = torch.device("cpu") # Load data def make_dataset(file, dir_name, trans, fg_indexs=None, bg_indexs=set([])): return ImageDataset(txt_file=file, root_dir='data/facial_parts/'+dir_name, fg_indexs=fg_indexs, bg_indexs=bg_indexs, transform=transforms.Compose(trans)) train_datasets = {} valid_datasets = {} test_datasets = {} ## Training set train_datasets['eyebrow'] = ConcatDataset([make_dataset('exemplars.txt', 'eyebrow1', fg_indexs=set([2]), trans=[DataArg(),ToTensor()]), make_dataset('exemplars.txt', 'eyebrow2', fg_indexs=set([3]), trans=[Invert(), DataArg(),ToTensor()])]) train_datasets['eye'] = ConcatDataset([make_dataset('exemplars.txt', 'eye1', fg_indexs=set([4]), trans=[DataArg(),ToTensor()]), make_dataset('exemplars.txt', 'eye2', fg_indexs=set([5]), trans=[Invert(), DataArg(), ToTensor()])]) train_datasets['nose'] = make_dataset('exemplars.txt', 'nose', fg_indexs=set([6]), trans=[DataArg(),ToTensor()]) train_datasets['mouth'] = make_dataset('exemplars.txt', 'mouth', fg_indexs=set([7,8,9]), trans=[DataArg(), ToTensor()]) ## Validation set valid_datasets['eyebrow'] = ConcatDataset([make_dataset('tuning.txt', 'eyebrow1', fg_indexs=set([2]), trans=[ToTensor()]), make_dataset('tuning.txt', 'eyebrow2', fg_indexs=set([3]), trans=[Invert(), ToTensor()])]) valid_datasets['eye'] = ConcatDataset([make_dataset('tuning.txt', 'eye1', fg_indexs=set([4]), trans=[ToTensor()]), make_dataset('tuning.txt', 'eye2', fg_indexs=set([5]), trans=[Invert(), ToTensor()])])
if torch.cuda.is_available(): device = torch.device("cuda") else: device = torch.device("cpu") resize_num = 64 part_width = 64 part_mouth = 80 ## Load test data test_dataset = ImageDataset(txt_file='testing.txt', root_dir='data/SmithCVPR2013_dataset_resized', bg_indexs=set([0,1,10]), transform=transforms.Compose([ Rescale((resize_num,resize_num)), ToTensor(), ])) test_loader = DataLoader(test_dataset, batch_size=args.batch_size, shuffle=True, num_workers=4) unresized_dataset = ImageDataset(txt_file='testing.txt', root_dir='data/SmithCVPR2013_dataset_resized', transform=ToTensor(), calc_bg=False) ## Load models model = pickle.load(open('res/saved-model.pth', 'rb')) model = model.to(device) names = ['eyebrow', 'eye', 'nose', 'mouth'] models={}
args = parser.parse_args() print(args) # Dataset Read_in Part img_root_dir = "/data1/yinzi/datas" # root_dir = '/home/yinzi/Downloads/datas' part_root_dir = "/data1/yinzi/facial_parts" root_dir = {'image': img_root_dir, 'parts': part_root_dir} txt_file_names = {'train': "exemplars.txt", 'val': "tuning.txt"} twostage_Dataset = { x: TwoStepData(txt_file=txt_file_names[x], root_dir=root_dir, transform=transforms.Compose( [Resize((64, 64)), ToTensor(), Normalize()])) for x in ['train', 'val'] } two_dataloader = { x: DataLoader(twostage_Dataset[x], batch_size=args.batch_size, shuffle=True, num_workers=16) for x in ['train', 'val'] } class TrainModel(TemplateModel): def __init__(self): super(TrainModel, self).__init__()
def main(): valid_datasets = {} ## Validation set valid_datasets['eyebrow'] = ConcatDataset([ make_dataset('tuning.txt', 'eyebrow1', fg_indexs=set([2]), trans=[ToTensor()]), make_dataset('tuning.txt', 'eyebrow2', fg_indexs=set([3]), trans=[ToTensor(), Invert()]) ]) valid_datasets['eye'] = ConcatDataset([ make_dataset('tuning.txt', 'eye1', fg_indexs=set([4]), trans=[ToTensor()]), make_dataset('tuning.txt', 'eye2', fg_indexs=set([5]), trans=[ToTensor(), Invert()]) ]) valid_datasets['nose'] = make_dataset('tuning.txt', 'nose', fg_indexs=set([6]), trans=[ToTensor()]) valid_datasets['mouth'] = make_dataset('tuning.txt', 'mouth', fg_indexs=set([7, 8, 9]), trans=[ToTensor()]) names = ['eyebrow', 'eye', 'nose', 'mouth'] l = {'eyebrow': 2, 'eye': 2, 'nose': 2, 'mouth': 4} for name in names: print(name, "...") model = pickle.load(open('res/saved-model-%s.pth' % name, 'rb')) #model model.to(device) mod = Modulator(l[name]) mod = mod.to(device) valid_loader = DataLoader(valid_datasets[name], batch_size=230, shuffle=True, num_workers=4) batch = [b for b in valid_loader][0] image, labels = batch['image'].to(device), batch['labels'].to(device) observations = model(image) loss_before = mod.loss_fn(observations, labels) mod.fit(observations, labels) loss_after = mod.loss_fn(mod.forward(observations), labels) print("Loss Before", loss_before) print("Loss After", loss_after) pickle.dump(mod, open('res/saved-modulator-%s.pth' % name, 'wb'))
# Dataset and Dataloader # Dataset Read_in Part root_dir = "/data1/yinzi/datas" parts_root_dir = "/home/yinzi/data3/recroped_parts" txt_file_names = { 'train': "exemplars.txt", 'val': "tuning.txt", 'test': "testing.txt" } transforms_list = { 'train': transforms.Compose([ ToTensor(), Resize((128, 128)), OrigPad() ]), 'val': transforms.Compose([ ToTensor(), Resize((128, 128)), OrigPad() ]), 'test': transforms.Compose([ ToTensor(), Resize((128, 128)), OrigPad() ])
if torch.cuda.is_available(): device = torch.device("cuda") else: device = torch.device("cpu") resize_num = 64 part_width = 64 part_mouth = 80 ## Load test data test_dataset = ImageDataset(txt_file='testing.txt', root_dir='data/SmithCVPR2013_dataset_resized', bg_indexs=set([0, 1, 10]), transform=transforms.Compose([ Rescale((resize_num, resize_num)), ToTensor(), ])) test_loader = DataLoader(test_dataset, batch_size=args.batch_size, shuffle=True, num_workers=4) unresized_dataset = ImageDataset(txt_file='testing.txt', root_dir='data/SmithCVPR2013_dataset_resized', transform=ToTensor(), calc_bg=False) ## Load models model = pickle.load(open('res/saved-model.pth', 'rb')) model = model.to(device)
'train': "/home/yinzi/data4/warped/train", 'val': "/home/yinzi/data4/warped/val", 'test': "/home/yinzi/data4/warped/test" } class DataLoaderX(DataLoader): def __iter__(self): return BackgroundGenerator(super().__iter__()) transforms_list = { 'train': transforms.Compose([ PrepareLabels((128, 128)), ToTensor() ]), 'val': transforms.Compose([ PrepareLabels((128, 128)), ToTensor() ]), 'test': transforms.Compose([ PrepareLabels((128, 128)), ToTensor() ]) } Dataset = {'val': Warped_HelenDataset(root_dir=root_dir['val'], mode='val',
args = parser.parse_args() print(args) # Dataset and Dataloader # Dataset Read_in Part root_dir = "/data1/yinzi/datas" parts_root_dir = "/home/yinzi/data3/recroped_parts" txt_file_names = { 'train': "exemplars.txt", 'val': "tuning.txt", 'test': "testing.txt" } transforms_list = { 'train': transforms.Compose([ToTensor(), Resize((128, 128)), OrigPad()]), 'val': transforms.Compose([ToTensor(), Resize((128, 128)), OrigPad()]), 'test': transforms.Compose([ToTensor(), Resize((128, 128)), OrigPad()]) } # DataLoader Dataset = { x: HelenDataset(txt_file=txt_file_names[x], root_dir=root_dir, parts_root_dir=parts_root_dir,
args = parser.parse_args() print(args) if torch.cuda.is_available(): device = torch.device("cuda") else: device = torch.device("cpu") resize_num = 64 # Load data train_dataset = ImageDataset(txt_file='exemplars.txt', root_dir='data/SmithCVPR2013_dataset_resized_' + str(resize_num), bg_indexs=set([0, 1, 10]), transform=transforms.Compose([ToTensor()])) train_loader = DataLoader(train_dataset, batch_size=args.batch_size, shuffle=True, num_workers=4) valid_dataset = ImageDataset(txt_file='tuning.txt', root_dir='data/SmithCVPR2013_dataset_resized_' + str(resize_num), bg_indexs=set([0, 1, 10]), transform=transforms.Compose([ToTensor()])) valid_loader = DataLoader(valid_dataset, batch_size=args.batch_size, shuffle=True, num_workers=4)