前提・実現したいこと
Python初心者です。
"dataset.py"のload_data()でADNI2というフォルダを指定しデータを読み込みたいのですが、
エラーになってしまいます。
データを読み込んだのち、テストと訓練を8:2に分けようと考えています
"AlexNet.py"で実行しています。
クラスは'CN', 'AD', 'LMCI', 'MCI'の4つに分けたいのでnum_workers=4としています。
欠けている情報ございましたら追加いたします。
どうぞよろしくお願いいたします。
発生している問題・エラーメッセージ
$ python AlexNet.py Traceback (most recent call last): File "AlexNet.py", line 15, in <module> train_loader = torch.utils.data.DataLoader(train_set, batch_size=1, shuffle=True, num_workers=4) File "/home/selen/.pyenv/versions/3.7.3/lib/python3.7/site-packages/torch/utils/data/dataloader.py", line 176, in __init__ sampler = RandomSampler(dataset) File "/home/selen/.pyenv/versions/3.7.3/lib/python3.7/site-packages/torch/utils/data/sampler.py", line 66, in __init__ "value, but got num_samples={}".format(self.num_samples)) ValueError: num_samples should be a positive integer value, but got num_samples=0
該当のソースコード
dataset.py
Python
1def load_data( 2 kinds=['ADNI2'], 3 classes=['CN', 'AD', 'LMCI', 'MCI'], 4 size='half', 5 csv=False, 6 pids=[], 7 uids=[], 8 unique=False, 9 blacklist=False, 10 dryrun=False, 11):
AlexNet.py
Python
1import dataset 2import torch 3from torchvision import transforms 4 5dataset = dataset.load_data(['ADNI2']) 6transform = transforms.Compose([transforms.ToTensor()]) 7 8n_train = int(len(dataset) * 0.8) 9n_test = len(dataset) - n_train 10 11train_set, test_set = torch.utils.data.random_split( 12 dataset, [n_train, n_test] 13) 14 15train_loader = torch.utils.data.DataLoader(train_set, batch_size=1, shuffle=True, num_workers=4) 16test_loader = torch.utils.data.DataLoader(test_set, batch_size=1, shuffle=False, num_workers=4) 17 18 19class AlexNet(nn.Module): 20 21 def __init__(self, num_classes): 22 super(AlexNet, self).__init__() 23 self.features = nn.Sequential( 24 nn.Conv2d(3, 64, kernel_size=3, padding=1), 25 nn.ReLU(inplace=True), 26 nn.MaxPool2d(kernel_size=2, stride=2), 27 nn.Conv2d(64, 192, kernel_size=5, padding=2), 28 nn.ReLU(inplace=True), 29 nn.MaxPool2d(kernel_size=2, stride=2), 30 nn.Conv2d(192, 384, kernel_size=3, padding=1), 31 nn.ReLU(inplace=True), 32 nn.Conv2d(384, 256, kernel_size=3, padding=1), 33 nn.ReLU(inplace=True), 34 nn.Conv2d(256, 256, kernel_size=3, padding=1), 35 nn.ReLU(inplace=True), 36 nn.MaxPool2d(kernel_size=2, stride=2), 37 ) 38 self.classifier = nn.Sequential( 39 nn.Dropout(), 40 nn.Linear(256 * 4 * 4, 4096), 41 nn.ReLU(inplace=True), 42 nn.Dropout(), 43 nn.Linear(4096, 4096), 44 nn.ReLU(inplace=True), 45 nn.Linear(4096, num_classes), 46 ) 47 48 def forward(self, x): 49 x = self.features(x) 50 x = x.view(x.size(0), 256 * 4 * 4) 51 x = self.classifier(x) 52 return x 53 54# select device 55num_classes = 4 56device = 'cuda' if torch.cuda.is_available() else 'cpu' 57net = AlexNet(num_classes).to(device) 58 59# optimizing 60criterion = nn.CrossEntropyLoss() 61optimizer = optim.SGD(net.parameters(), lr=0.01, momentum=0.9, weight_decay=5e-4) 62 63# training 64num_epochs = 20 65train_loss_list, train_acc_list, val_loss_list, val_acc_list = [], [], [], [] 66 67### training 68for epoch in range(num_epochs): 69 train_loss, train_acc, val_loss, val_acc = 0, 0, 0, 0 70 71 # ====== train_mode ====== 72 net.train() 73 for i, (images, labels) in enumerate(train_loader): 74 images, labels = images.to(device), labels.to(device) 75 optimizer.zero_grad() 76 outputs = net(images) 77 loss = criterion(outputs, labels) 78 train_loss += loss.item() 79 train_acc += (outputs.max(1)[1] == labels).sum().item() 80 loss.backward() 81 optimizer.step() 82 83 avg_train_loss = train_loss / len(train_loader.dataset) 84 avg_train_acc = train_acc / len(train_loader.dataset) 85 86 # ====== val_mode ====== 87 net.eval() 88 with torch.no_grad(): 89 for images, labels in test_loader: 90 images = images.to(device) 91 labels = labels.to(device) 92 outputs = net(images) 93 loss = criterion(outputs, labels) 94 val_loss += loss.item() 95 val_acc += (outputs.max(1)[1] == labels).sum().item() 96 avg_val_loss = val_loss / len(test_loader.dataset) 97 avg_val_acc = val_acc / len(test_loader.dataset) 98 99 print ('Epoch [{}/{}], Loss: {loss:.4f}, val_loss: {val_loss:.4f}, val_acc: {val_acc:.4f}' 100 .format(epoch+1, num_epochs, i+1, loss=avg_train_loss, val_loss=avg_val_loss, val_acc=avg_val_acc)) 101 train_loss_list.append(avg_train_loss) 102 train_acc_list.append(avg_train_acc) 103 val_loss_list.append(avg_val_loss) 104 val_acc_list.append(avg_val_acc) 105 106 107# plot graph 108plt.figure() 109plt.plot(range(num_epochs), train_loss_list, color='blue', linestyle='-', label='train_loss') 110plt.plot(range(num_epochs), val_loss_list, color='green', linestyle='--', label='val_loss') 111plt.legend() 112plt.xlabel('epoch') 113plt.ylabel('loss') 114plt.title('Training and validation loss') 115plt.grid() 116plt.show() 117 118plt.figure() 119plt.plot(range(num_epochs), train_acc_list, color='blue', linestyle='-', label='train_acc') 120plt.plot(range(num_epochs), val_acc_list, color='green', linestyle='--', label='val_acc') 121plt.legend() 122plt.xlabel('epoch') 123plt.ylabel('acc') 124plt.title('Training and validation accuracy') 125plt.grid() 126plt.show() 127
あなたの回答
tips
プレビュー