python3
1a_transform = transforms.Compose([ 2 transforms.RandomResizedCrop(size=224), 3 transforms.RandomHorizontalFlip(), 4 transforms.ToTensor(), 5 transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])]) 6 7e_transform = transforms.Compose([ 8 transforms.Resize(size=224), 9 transforms.CenterCrop(size=224), 10 transforms.ToTensor(), 11 transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])]) 12 13 14train_dataset = ImageFolder(f"{DATASETDIR}/Train", transform=augment_transform) 15val_dataset = ImageFolder(f"{DATASETDIR}/Validation", transform=evaluate_transform) 16test_dataset = ImageFolder(f"{DATASETDIR}/Test", transform=evaluate_transform) 17 18model = resnet34(pretrained=True) 19model.fc = nn.Sequential( 20 nn.Dropout(0.5), 21 nn.Linear(in_features=512, out_features=6, bias=True) 22) 23model = model.to("cuda") 24 25loss_func = torch.nn.CrossEntropyLoss() 26optimizer = torch.optim.SGD(model.parameters(), lr=1e-3, momentum=0.9) 27scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=3, gamma=0.9) 28 29def training(model, dataset, optimizer, loss_func): 30 model.train() 31 32 losses = [] 33 for img, target in DataLoader(dataset, shuffle=True, batch_size=32, num_workers=4): 34 img = img.to("cuda") 35 target = target.to("cuda") 36 pred = model(img) 37 loss = loss_func(pred, target) 38 losses.append(loss.item()) 39 40 optimizer.zero_grad() 41 loss.backward() 42 optimizer.step() 43 44 return np.mean(losses) 45 46def evaluation(model, dataset, loss_func): 47 model.eval() 48 49 losses = [] 50 correct = 0 51 with torch.no_grad(): 52 for img, target in DataLoader(dataset, shuffle=False, batch_size=64, num_workers=4): 53 img = img.to("cuda") 54 target = target.to("cuda") 55 pred = model(img) 56 loss = loss_func(pred, target) 57 losses.append(loss.item()) 58 59 predict = pred.argmax(dim=1) 60 correct += (predict == target).sum().item() 61 return np.mean(losses), correct / len(dataset) 62best_loss = 1e+10 63best_state = None 64earlystop_counter = 0 65for epoch in range(100): 66 train_loss = training(model, train_dataset, optimizer, loss_func) 67 val_loss, val_acc = evaluation(model, val_dataset, loss_func) 68 69 if val_loss < best_loss: 70 earlystop_counter = 0 71 best_loss = val_loss 72 best_state = model.cpu().state_dict() 73 model.cuda() 74 else: 75 earlystop_counter += 1 76 77 if earlystop_counter > 5: 78 break 79 80 scheduler.step() 81 print(f"epoch {str(epoch).zfill(2)}\ttrain_loss: {train_loss}\tval_loss: {val_loss}\tval_acc: {val_acc}") 82 83##以下出現したエラー 84--------------------------------------------------------------------------- 85RuntimeError Traceback (most recent call last) 86<ipython-input-8-2396f00cf9e1> in <module>() 87 3 earlystop_counter = 0 88 4 for epoch in range(100): 89----> 5 train_loss = training(model, train_dataset, optimizer, loss_func) 90 6 val_loss, val_acc = evaluation(model, val_dataset, loss_func) 91 7 92 936 frames 94<ipython-input-6-33676f519731> in training(model, dataset, optimizer, loss_func) 95 6 img = img.to("cuda") 96 7 target = target.to("cuda") 97----> 8 pred = model(img) 98 9 loss = loss_func(pred, target) 99 10 losses.append(loss.item()) 100 101/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs) 102 720 result = self._slow_forward(*input, **kwargs) 103 721 else: 104--> 722 result = self.forward(*input, **kwargs) 105 723 for hook in itertools.chain( 106 724 _global_forward_hooks.values(), 107 108/usr/local/lib/python3.6/dist-packages/torchvision/models/resnet.py in forward(self, x) 109 218 110 219 def forward(self, x): 111--> 220 return self._forward_impl(x) 112 221 113 222 114 115/usr/local/lib/python3.6/dist-packages/torchvision/models/resnet.py in _forward_impl(self, x) 116 201 def _forward_impl(self, x): 117 202 # See note [TorchScript super()] 118--> 203 x = self.conv1(x) 119 204 x = self.bn1(x) 120 205 x = self.relu(x) 121 122/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs) 123 720 result = self._slow_forward(*input, **kwargs) 124 721 else: 125--> 722 result = self.forward(*input, **kwargs) 126 723 for hook in itertools.chain( 127 724 _global_forward_hooks.values(), 128 129/usr/local/lib/python3.6/dist-packages/torch/nn/modules/conv.py in forward(self, input) 130 417 131 418 def forward(self, input: Tensor) -> Tensor: 132--> 419 return self._conv_forward(input, self.weight) 133 420 134 421 class Conv3d(_ConvNd): 135 136/usr/local/lib/python3.6/dist-packages/torch/nn/modules/conv.py in _conv_forward(self, input, weight) 137 414 _pair(0), self.dilation, self.groups) 138 415 return F.conv2d(input, weight, self.bias, self.stride, 139--> 416 self.padding, self.dilation, self.groups) 140 417 141 418 def forward(self, input: Tensor) -> Tensor: 142 143RuntimeError: Input type (torch.cuda.FloatTensor) and weight type (torch.FloatTensor) should be the same 144
google colabを使ってpytorchを活用したresnet34の転移学習を上記のコードで行うとしたのですが、エラーが出てしました。どなたか解決策をご教授していただけますか?
更新10/28
あなたの回答
tips
プレビュー