def build_transform( resize: tuple = (2048, 2048), crop_sz: tuple = (512, 512), crop_type: str = 'random', method: str = 'scale_center', flips: bool = True, to_tensor: bool = True, ): '''Build a torchvision transform Parameters ---------- resize : tuple (int H, int W) for image resizing. crop_sz : tuple (int H, int W) for cropped windows. crop_type : str type of cropping to apply. method : str method for processing after normalization. ['scale_center',] flips : bool apply random flipping. to_tensor : bool convert results to `torch.Tensor`. Returns ------- transform : Callable ''' from torchvision import transforms, utils import data_loader fxns = [] if resize is not None: rsz = data_loader.Resize(size=resize + (1, )) fxns.append(rsz) if crop_type is not None: crop = data_loader.RandomCrop(crop_sz=crop_sz, min_mask_sum=1) fxns.append(crop) if method.lower() == 'scale_center': fxns.append(data_loader.RescaleUnit()) fxns.append(data_loader.SampleWiseCenter()) elif method is None: pass else: raise ValueError('method argument is invalid.') if flips: fxns.append(data_loader.RandomFlip()) if to_tensor: fxns.append(data_loader.ToTensor()) T = transforms.Compose(txns) return T
def new_compress_curriculum(args, cur_factor, csv='train', stc=False): transformed_dataset = data.Compress_Dataset(csv_file=data.compress_csv_path(csv), transform=data.Compose([ transforms.RandomCrop((args.patch_size, args.patch_size)), transforms.RandomHorizontalFlip(), transforms.RandomVerticalFlip(), data.Rescale((args.patch_size, args.patch_size), up_factor=cur_factor, stc=stc), data.ToTensor() ])) dataloader = DataLoader(transformed_dataset, batch_size=args.batch_size, shuffle=True, num_workers=args.num_workers) return dataloader
def train(): # transform # get train set transform = transforms.Compose( [data_loader.Rescale((224, 224)), data_loader.ToTensor()]) train_set = data_loader.PascalVocLoader(transform=transform) train_loader = torch.utils.data.DataLoader(train_set, batch_size=1, shuffle=True, num_workers=2) # get net | loss function | SGD net = AlexNet(num_classes=21).to(device) net = nn.DataParallel(net, device_ids=[0, 1, 2]) loss_function = nn.NLLLoss() optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9) # train print("alex_net training......") for epoch in range(2): running_loss = 0.0 for i, data in enumerate(train_loader, 0): inputs, labels = data inputs = inputs.float().to(device) labels = labels.to(device) optimizer.zero_grad() outputs = net(inputs) print(outputs, labels) loss = loss_function(outputs, labels) loss.backward() optimizer.step() running_loss += loss.item() if i % 10 == 9: print(running_loss / 10) running_loss = 0.0 break return net
def p1a(): args = get_args() if not args: print("empty inputs") return if args.save is not None: print "--Training Mode--" trans_train = transforms.Compose( [dl.random_augmetaion(), dl.ToTensor()]) trans_test = transforms.Compose([dl.ToTensor()]) N = 20 if (args.augment == "N"): print("Non data augmentation Mode") face_train_dataset = dl.FacePairsDataset(txt_file='lfw/train.txt', root_dir='lfw/', transform=trans_test) else: print("Augmentation Mode") face_train_dataset = dl.FacePairsDataset(txt_file='lfw/train.txt', root_dir='lfw/', transform=trans_train) face_test_dataset = dl.FacePairsDataset(txt_file='lfw/test.txt', root_dir='lfw/', transform=trans_test) train_loader = DataLoader(dataset=face_train_dataset, batch_size=N, shuffle=True, num_workers=4) test_loader = DataLoader(dataset=face_test_dataset, batch_size=N, shuffle=False, num_workers=4) print("Loaded Data") print("Loss and Loading the Model") if torch.cuda.is_available(): snet = models.Siamese_Net().cuda() criterion = nn.BCELoss().cuda() else: snet = models.Siamese_Net() criterion = nn.BCELoss() print("Optimizer ") optimizer = optim.Adam(snet.parameters()) a = torch.randn(2, 3, 128, 128) b = torch.randn(2, 3, 128, 128) print(snet) if torch.cuda.is_available(): temp = snet(Variable(a).cuda(), Variable(b).cuda()) else: temp = snet(Variable(a), Variable(b)) print("sanity check on 2 random variables ", temp.size()) ac_list = [] loss_list = [] local_loss_list = [0] print("Started Training") for epoch in range(12): # loop over the dataset multiple times for i, sample_batched in enumerate(train_loader): # get the inputs faces_1_batch, faces_2_batch = sample_batched[ 'face_1'], sample_batched['face_2'] labels_batch = sample_batched['label'] # wrap them in Variable if torch.cuda.is_available(): input1, input2 = Variable( faces_1_batch.float().cuda()), Variable( faces_2_batch.float().cuda()) labels_batch = Variable(labels_batch.float().cuda()) else: input1, input2 = Variable(faces_1_batch.float()), Variable( faces_2_batch.float()) labels_batch = Variable(labels_batch.float()) # zero the parameter gradients optimizer.zero_grad() # forward + backward + optimize outputs = snet(input1, input2) outputs_flat = outputs.view(outputs.numel()) loss = criterion(outputs_flat, labels_batch) loss_list.append(loss.data) if i % 20 == 0: local_loss_list_ar = np.asarray(local_loss_list) loss_list.append(local_loss_list_ar.mean()) local_loss_list = [] loss.backward() optimizer.step() ac_list.append(dl.curr_accuracy(test_loader, snet)) print("Epoch number ", epoch) print('Finished Training') c, = plt.plot(loss_list, label="Loss") plt.savefig(args.save + 'plot') save_file_path = args.save torch.save(snet.state_dict(), save_file_path) else: print "--Testing Mode---" N = 20 if torch.cuda.is_available(): snet_load = models.Siamese_Net().cuda() else: snet_load = models.Siamese_Net() snet_load.load_state_dict(torch.load(args.load)) trans_test = transforms.Compose([dl.ToTensor()]) face_test_dataset = dl.FacePairsDataset(txt_file='lfw/train.txt', root_dir='lfw/', transform=trans_test) test_loader = DataLoader(dataset=face_test_dataset, batch_size=N, shuffle=False, num_workers=4) dl.curr_accuracy(test_loader, snet_load)