YutaroOgawa / pytorch_advanced

書籍「つくりながら学ぶ! PyTorchによる発展ディープラーニング」の実装コードを配置したリポジトリです
MIT License
838 stars 336 forks source link

1-3_transfer_learning.ipynbをgoogle colaboratoryで実行すると学習のセルでエラーが発生. #109

Open paprikamario opened 4 years ago

paprikamario commented 4 years ago

1-3_transfer_learning.ipynbをgoogle colaboratoryで,試しに動作させたのですが, 学習の途中で止まってしまいます. 1-1_load_vgg.ipynbは,問題無く動作しました.

バーションは, PyTorch Version: 1.6.0+cu101 Torchvision Version: 0.7.0+cu101 と表示されています.

1-3_transfer_learning.ipynbの最後のセルになります. 画像もダウンロードできているようです.解決策をおしえていただけますと たすかります.よろしくお願いします.

ここからーー

学習・検証を実行する

num_epochs=2 train_model(net, dataloaders_dict, criterion, optimizer, num_epochs=num_epochs) を実行したら,下記のエラーがでてとまってしまいます.解決方法を教えてください.

エラー ===================================== 0%| | 0/5 [00:00<?, ?it/s]

Epoch 1/2


AttributeError Traceback (most recent call last)

in () 1 # 学習・検証を実行する 2 num_epochs=2 ----> 3 train_model(net, dataloaders_dict, criterion, optimizer, num_epochs=num_epochs) 4 frames in train_model(net, dataloaders_dict, criterion, optimizer, num_epochs) 32 with torch.set_grad_enabled(phase == 'train'): 33 outputs = net(inputs) ---> 34 loss = criterion(outputs, labels) # 損失を計算 35 _, preds = torch.max(outputs, 1) # ラベルを予測 36 /usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs) 720 result = self._slow_forward(*input, **kwargs) 721 else: --> 722 result = self.forward(*input, **kwargs) 723 for hook in itertools.chain( 724 _global_forward_hooks.values(), /usr/local/lib/python3.6/dist-packages/torch/nn/modules/loss.py in forward(self, input, target) 946 def forward(self, input: Tensor, target: Tensor) -> Tensor: 947 return F.cross_entropy(input, target, weight=self.weight, --> 948 ignore_index=self.ignore_index, reduction=self.reduction) 949 950 /usr/local/lib/python3.6/dist-packages/torch/nn/functional.py in cross_entropy(input, target, weight, size_average, ignore_index, reduce, reduction) 2420 if size_average is not None or reduce is not None: 2421 reduction = _Reduction.legacy_get_string(size_average, reduce) -> 2422 return nll_loss(log_softmax(input, 1), target, weight, None, ignore_index, None, reduction) 2423 2424 /usr/local/lib/python3.6/dist-packages/torch/nn/functional.py in nll_loss(input, target, weight, size_average, ignore_index, reduce, reduction) 2212 raise ValueError('Expected 2 or more dimensions (got {})'.format(dim)) 2213 -> 2214 if input.size(0) != target.size(0): 2215 raise ValueError('Expected input batch_size ({}) to match target batch_size ({}).' 2216 .format(input.size(0), target.size(0))) AttributeError: 'tuple' object has no attribute 'size'
YutaroOgawa commented 4 years ago

@paprikamario さま

ご質問ありがとうございます。 皆のために、とても役立つので嬉しいです。

うまくデータローダーが作れていない気がします。 Google Colaboratoryですと、ファイルパスがうまく本書と合わずにこのエラーに遭遇している人が多いように感じます。

実行環境はAWSやクラウドのマシンでしょうか? Google Colaboratoryでしょうか?

paprikamario commented 4 years ago

Google Colaboratoryになります.ファイル添付します. 1-3_transfer_learning.zip

paprikamario commented 4 years ago
    # 画像のラベルをファイル名から抜き出す
    # google colabの時はfull path になるのでants, beesの場所の変更が必要
    if self.phase == "train":
        # label = img_path[30:34]
        label = img_path[139:143]
    elif self.phase == "val":
        # label = img_path[28:32]
        label = img_path[137:141]

ここの修正が必要のようでした.パスの位置からラベルをとっているようでした.

YutaroOgawa commented 4 years ago

@paprikamario さま

解決策をありがとうございます。

これが、PyTorch1.6にバージョンが上がって(一緒に配布データセットもパスが変化した?)のか、 今後も皆が遭遇するエラーなのか、後日確認いたします。

重要なご投稿、および解決策を誠にありがとうございます。