加载训练,验证和测试集的函数返回名称错误。

时间:2018-12-08 20:36:22

标签: python deep-learning

这是我的职能:

from torchvision import *
import numpy as np
from torch.utils.data.sampler import SubsetRandomSampler

def get_train_valid_test_loader(data_dir,
                       batch_size,
                       random_seed,
                       augment=True,
                       train_size = 0.6,
                       valid_size=0.2,
                       test_size = 0.2,
                       shuffle=True,
                       show_sample=False,
                       num_workers=0,
                       pin_memory=True):

#     error_msg = "[!] valid_size should be in the range [0, 1]."
#     assert ((valid_size >= 0) and (valid_size <= 1)), error_msg

normalize = torchvision.transforms.Normalize(
    mean=[0.4914, 0.4822, 0.4465],
    std=[0.2023, 0.1994, 0.2010],
)


# load the dataset
dataset_loader = torch.utils.data.DataLoader(data_dir)
trainset = dataset_loader

#slice the dataset into train, validation, and test partitions
num_train = len(dataset_loader)
indices = list(range(num_train))
train_split = int(np.floor(train_size * num_train))
validation_split = int(np.floor(valid_size * num_train)) + 1
test_split = int(np.floor(test_size * num_train)) + 1



if shuffle:
    np.random.seed(random_seed)
    np.random.shuffle(indices)

train_idx = indices[:train_split]
valid_idx = indices[train_split:validation_split]
test_idx =  indices[validation_split:test_split]

train_sampler = SubsetRandomSampler(train_idx)
valid_sampler = SubsetRandomSampler(valid_idx)
test_sampler = SubsetRandomSampler(test_idx)


#LOAD ALL THE SETS 

#TRAIN SET

#load the trainset

trainset = torch.utils.data.DataLoader(
    dataset_loader, batch_size=batch_size, sampler=train_sampler,
    num_workers=num_workers, pin_memory=pin_memory,
)

#Train: Data Augmentation, Resizing, and Normalization 

if augment:
    trainset = transforms.Compose([
        torchvision.transforms.Resize(224), #resize all the pictures 
        torchvision.transforms.RandomCrop(224,pad_if_needed=True),
        torchvision.transforms.RandomCrop(224,pad_if_needed=True),
        torchvision.transforms.RandomRotation(45, resample=False, 
                                              expand=False, center=None),
        torchvision.transforms.RandomCrop(224,pad_if_needed=True),
        torchvision.transforms.RandomCrop(224,pad_if_needed=True),
        torchvision.transforms.RandomRotation(30, resample=False, 
                                              expand=False, center=None),
        torchvision.transforms.RandomCrop(224,pad_if_needed=True),
        torchvision.transforms.RandomGrayscale(p=0.1),
        torchvision.transforms.RandomVerticalFlip(p=1),
        torchvision.transforms.RandomHorizontalFlip(p=1),
        torchvision.transforms.RandomRotation(90, resample=False, 
                                              expand=False, center=None),
        torchvision.transforms.ToTensor(),
        normalize,
    ])
else:
    trainset = torchvision.transforms.Compose([
        torchvision.transforms.Resize(224), #resize all the pictures 
        torchvision.transforms.ToTensor(),
        normalize
    ])


#VALIDATION SET

#load the validation set 

validset = torch.utils.data.DataLoader(
    dataset_loader, batch_size=batch_size, sampler=valid_sampler,
    num_workers=num_workers, pin_memory=pin_memory
)


#Validation: Data Augmentation, Resizing, and Normalization   

validset = torchvision.transforms.Compose([
        torchvision.transforms.Resize(224), #resize all the pictures 
        torchvision.transforms.RandomCrop(224,pad_if_needed=True),
        torchvision.transforms.RandomCrop(224,pad_if_needed=True),
        torchvision.transforms.RandomRotation(45, resample=False, 
                                              expand=False, center=None),
        torchvision.transforms.RandomCrop(224,pad_if_needed=True),
        torchvision.transforms.RandomCrop(224,pad_if_needed=True),
        torchvision.transforms.RandomRotation(30, resample=False, 
                                              expand=False, center=None),
        torchvision.transforms.RandomCrop(224,pad_if_needed=True),
        torchvision.transforms.RandomGrayscale(p=0.1),
        torchvision.transforms.RandomVerticalFlip(p=1),
        torchvision.transforms.RandomHorizontalFlip(p=1),
        torchvision.transforms.RandomRotation(90, resample=False, 
                                              expand=False, center=None),
        torchvision.transforms.ToTensor(),
        normalize,
])


#TEST SET

#load the test set

testset = torch.utils.data.DataLoader(
    dataset_loader, batch_size=batch_size, sampler=test_sampler, 
    num_workers=num_workers, pin_memory=pin_memory,
)

#Test: Data Augmentation, Resizing, and Normalization 

testset = torchvision.transforms.Compose([
        torchvision.transforms.Resize(224), #resize all the pictures 
        torchvision.transforms.RandomCrop(224,pad_if_needed=True),
        torchvision.transforms.RandomCrop(224,pad_if_needed=True),
        torchvision.transforms.RandomRotation(45, resample=False, 
                                              expand=False, center=None),
        torchvision.transforms.RandomCrop(224,pad_if_needed=True),
        torchvision.transforms.RandomCrop(224,pad_if_needed=True),
        torchvision.transforms.RandomRotation(30, resample=False, 
                                              expand=False, center=None),
        torchvision.transforms.RandomCrop(224,pad_if_needed=True),
        torchvision.transforms.RandomGrayscale(p=0.1),
        torchvision.transforms.RandomVerticalFlip(p=1),
        torchvision.transforms.RandomHorizontalFlip(p=1),
        torchvision.transforms.RandomRotation(90, resample=False, 
                                              expand=False, center=None),
        torchvision.transforms.ToTensor(),
        normalize,
])

return trainset, validset, testset

在这里我调用函数:

 if __name__ == "__main__":

    random_seed = random.seed(1000)
    the_dir = '/content/drive/My Drive/Deep Learning/data'
    get_train_valid_test_loader(the_dir,32, random_seed, True)

    print(len(trainset))
    print(len(validset))
    print(len(testset))


    main()

我不知道为什么会出现以下错误:

 NameError                         Traceback (most recent call   last)
 <ipython-input-36-8b4c3d896768> in <module>()
  6   get_train_valid_test_loader(the_dir,32, random_seed, True)
  7 
  8   print(len(trainset)). <----------- Error
  9   print(len(validset))
 10   print(len(testset))

 NameError: name 'trainset' is not defined

我以为我在定义它之前就已经使用过“ trainset”,但事实并非如此。我试图将“ dataset_loader”分配给“ transet”,但仍然没有结果。 我也尝试使用与我的dataset_loader相同的确切行来加载“ trainset”。

1 个答案:

答案 0 :(得分:0)

调用函数将返回函数return中的,但这与在函数范围内定义的那些值的 names 没有关系。 。重现错误的情况很简单:

def SimpleExample(a):
    return a

if __name__ == '__main__':
    SimpleExample(1)
    print(a)

当然还有预期的错误:

NameError: name 'a' is not defined

Process finished with exit code 1

因此,如果要捕获函数返回的值,则需要显式地进行捕获,然后可以将这些值返回到所需的任何变量名称中:

def SimpleExample(a):     返回一个

if __name__ == '__main__':
    a = SimpleExample(1)
    print(a)
    b = SimpleExample(2)
    print(b)
    x = SimpleExample([0, 0, 0])
    print(x)

然后一切都很好:

1
2
[0, 0, 0]

Process finished with exit code 0

回到案例,尝试简单的方法:

if __name__ == "__main__":

    random_seed = random.seed(1000)
    the_dir = '/content/drive/My Drive/Deep Learning/data'
    trainset, validset, testset = get_train_valid_test_loader(the_dir,32, random_seed, True)

    print(len(trainset))
    print(len(validset))
    print(len(testset))

祝你好运!