Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
for _ in range(n - 1):
v = linear_map(v)
cols.append(v)
return torch.stack(cols, dim=-1)
batch_size = 256
if use_cuda:
loader_args = {'num_workers': 8, 'pin_memory': True}
else:
loader_args = {'num_workers': 1, 'pin_memory': False}
def loader_from_dataset(dataset):
return torch.utils.data.DataLoader(dataset, batch_size=batch_size,
shuffle=True, **loader_args)
mnist_normalize = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.1307, ), (0.3081, ))
])
mnist_train = datasets.MNIST(
'../data', train=True, download=True, transform=mnist_normalize)
# Just for faster training on CPU
# mnist_train.train_data = mnist_train.train_data[:5000]
mnist_test = datasets.MNIST(
'data', train=False, download=True, transform=mnist_normalize)
mnist_train, mnist_valid = get_train_valid_datasets(mnist_train)
train_loader = loader_from_dataset(mnist_train)
valid_loader = loader_from_dataset(mnist_valid)
test_loader = loader_from_dataset(mnist_test)
n_features = 28 * 28
n_classes = 10
def classifier(img_path, model_name):
# load the image
img_pil = Image.open(img_path)
# define transforms
preprocess = transforms.Compose([
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
])
# preprocess the image
img_tensor = preprocess(img_pil)
# resize the tensor (add dimension for batch)
img_tensor.unsqueeze_(0)
# wrap input in variable, wrap input in variable - no longer needed for
# v 0.4 & higher code changed 04/26/2018 by Jennifer S. to handle PyTorch upgrade
pytorch_ver = __version__.split('.')
# pytorch versions 0.4 & hihger - Variable depreciated so that it returns
def transform_test(data_transforms=None, resize_size=256, crop_size=227):
normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
#ten crops for image when validation, input the data_transforms dictionary
start_first = 0
start_center = (resize_size - crop_size - 1) / 2
start_last = resize_size - crop_size - 1
if not data_transforms:
data_transforms = {}
data_transforms['val0'] = transforms.Compose([
ResizeImage(resize_size),ForceFlip(),
PlaceCrop(crop_size, start_first, start_first),
transforms.ToTensor(),
normalize
])
data_transforms['val1'] = transforms.Compose([
ResizeImage(resize_size),ForceFlip(),
PlaceCrop(crop_size, start_last, start_last),
transforms.ToTensor(),
normalize
])
data_transforms['val2'] = transforms.Compose([
ResizeImage(resize_size),ForceFlip(),
PlaceCrop(crop_size, start_last, start_first),
transforms.ToTensor(),
normalize
import random
import os
from PIL import Image
from torch.utils.data import Dataset, DataLoader
import torchvision.transforms as transforms
# borrowed from http://pytorch.org/tutorials/advanced/neural_style_tutorial.html
# and http://pytorch.org/tutorials/beginner/data_loading_tutorial.html
# define a training image loader that specifies transforms on images. See documentation for more details.
train_transformer = transforms.Compose([
transforms.Resize(64), # resize the image to 64x64 (remove if images are already 64x64)
transforms.RandomHorizontalFlip(), # randomly flip image horizontally
transforms.ToTensor()]) # transform it into a torch tensor
# loader for evaluation, no horizontal flip
eval_transformer = transforms.Compose([
transforms.Resize(64), # resize the image to 64x64 (remove if images are already 64x64)
transforms.ToTensor()]) # transform it into a torch tensor
class SIGNSDataset(Dataset):
"""
A standard PyTorch definition of Dataset which defines the functions __len__ and __getitem__.
"""
def __init__(self, data_dir, transform):
"""
Store the filenames of the jpgs to use. Specifies transforms to apply on images.
from load_test_dataset import *
import pdb
import csv
# user define variable
from user_define import Config as cf
from user_define import Hyperparams as hp
use_cuda = torch.cuda.is_available()
slide_fn = "t_4"
print('==> Preparing data..')
transform_test = transforms.Compose([
transforms.ToTensor(),
])
testset = get_test_dataset(transform_test)
testloader = torch.utils.data.DataLoader(testset,
hp.batch_size_for_eval,
shuffle=False,
num_workers=8)
print('==> Resuming from checkpoint..')
checkpoint = torch.load('./checkpoint/ckpt.pth.tar')
net = checkpoint['net']
if use_cuda:
net.cuda()
net = torch.nn.DataParallel(
net, device_ids=range(torch.cuda.device_count()))
cudnn.benchmark = True
# Init dataset
if not os.path.isdir(args.data_path):
os.makedirs(args.data_path)
if args.dataset == 'cifar10':
mean = [x / 255 for x in [125.3, 123.0, 113.9]]
std = [x / 255 for x in [63.0, 62.1, 66.7]]
elif args.dataset == 'cifar100':
mean = [x / 255 for x in [129.3, 124.1, 112.4]]
std = [x / 255 for x in [68.2, 65.4, 70.4]]
else:
assert False, "Unknow dataset : {}".format(args.dataset)
train_transform = transforms.Compose(
[transforms.RandomHorizontalFlip(), transforms.RandomCrop(32, padding=4), transforms.ToTensor(),
transforms.Normalize(mean, std)])
test_transform = transforms.Compose(
[transforms.ToTensor(), transforms.Normalize(mean, std)])
if args.dataset == 'cifar10':
train_data = dset.CIFAR10(args.data_path, train=True, transform=train_transform, download=True)
test_data = dset.CIFAR10(args.data_path, train=False, transform=test_transform, download=True)
num_classes = 10
elif args.dataset == 'cifar100':
train_data = dset.CIFAR100(args.data_path, train=True, transform=train_transform, download=True)
test_data = dset.CIFAR100(args.data_path, train=False, transform=test_transform, download=True)
num_classes = 100
elif args.dataset == 'svhn':
train_data = dset.SVHN(args.data_path, split='train', transform=train_transform, download=True)
test_data = dset.SVHN(args.data_path, split='test', transform=test_transform, download=True)
num_classes = 10
def __getitem__(self, index):
AB_path = self.AB_paths[index]
AB = Image.open(AB_path).convert('RGB')
w, h = AB.size
w2 = int(w / 2)
A = AB.crop((0, 0, w2, h)).resize((self.opt.loadSize, self.opt.loadSize), Image.BICUBIC)
B = AB.crop((w2, 0, w, h)).resize((self.opt.loadSize, self.opt.loadSize), Image.BICUBIC)
A = transforms.ToTensor()(A)
B = transforms.ToTensor()(B)
w_offset = random.randint(0, max(0, self.opt.loadSize - self.opt.fineSize - 1))
h_offset = random.randint(0, max(0, self.opt.loadSize - self.opt.fineSize - 1))
A = A[:, h_offset:h_offset + self.opt.fineSize, w_offset:w_offset + self.opt.fineSize]
B = B[:, h_offset:h_offset + self.opt.fineSize, w_offset:w_offset + self.opt.fineSize]
A = transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))(A)
B = transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))(B)
if self.opt.which_direction == 'BtoA':
input_nc = self.opt.output_nc
output_nc = self.opt.input_nc
else:
input_nc = self.opt.input_nc
output_nc = self.opt.output_nc
super(rnn_predictor, self).__init__()
self.hidden_size = hidden_size
self.norm_vision = norm_vision
self.feed_self = feed_self
self.normalize = tv.transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
self.train_transform = tv.transforms.Compose([
#tv.transforms.Scale(512),
tv.transforms.RandomCrop(256),
tv.transforms.RandomHorizontalFlip(),
tv.transforms.ToTensor(),
self.normalize,
])
self.dev_transform = tv.transforms.Compose([
#tv.transforms.Scale(256),
tv.transforms.CenterCrop(256),
tv.transforms.ToTensor(),
self.normalize,
])
self.n_verbs = encoding.n_verbs()
self.loss_n = nn.CrossEntropyLoss(ignore_index = encoding.pad_symbol())
self.loss_v = nn.CrossEntropyLoss()
self.encoding = encoding
#cnn
print(cnn_type)
if cnn_type == "resnet_101" :
# self.cnn_v = resnet_modified_large()
self.cnn_n = resnet_modified_large()
elif cnn_type == "resnet_50":