【Python】Pytorchのモデル保存・読み込みをもっと簡単にしたい!
PyTorchで訓練されたモデルを保存するための最良の方法は?
上記のサイトを参考にpytorchのモデルの保存・読み込みのコードを書いて実行し、保存した重みを使って読み込みをしようとしたところ下記のようなエラーが出て実行できません。対処法を教えていただきたいです。
(base) xxxxxxx python % /opt/anaconda3/bin/python /Users/xxxxxx/Downloads/python/pytourch_study/weights/sumple_cnn.py test Traceback (most recent call last): File "/Users/xxxxxx/Downloads/python/pytourch_study/weights/sumple_cnn.py", line 98, in <module> model.load_state_dict(torch.load("weights/weights_2.pth")) File "/opt/anaconda3/lib/python3.7/site-packages/torch/serialization.py", line 584, in load with _open_file_like(f, 'rb') as opened_file: File "/opt/anaconda3/lib/python3.7/site-packages/torch/serialization.py", line 234, in _open_file_like return _open_file(name_or_buffer, mode) File "/opt/anaconda3/lib/python3.7/site-packages/torch/serialization.py", line 215, in __init__ super(_open_file, self).__init__(open(name, mode)) FileNotFoundError: [Errno 2] No such file or directory: 'weights/weights_2.pth'
階層は下記です。
書いたコードは下記に示してあります。重みを使用する際にはtrainのところをコメントアウトして実行を試そうと思っているのですが、うまくいきません。
環境は
vs code
python3.7.6
tensorflow1.14.0
です。
import torch from torchvision.datasets import MNIST from torchvision import transforms from torch.utils.data import DataLoader from torch import nn import cloudpickle class cnn(nn.Module): def __init__(self): super().__init__() self.layers = nn.Sequential( nn.Conv2d(1, 6, kernel_size=5, padding=2), nn.ReLU(inplace=True), nn.MaxPool2d(2), nn.Conv2d(6, 16, kernel_size=5), nn.ReLU(inplace=True), nn.MaxPool2d(2), nn.Flatten(), nn.Linear(16*5*5, 120), nn.ReLU(inplace=True), nn.Linear(120, 84), nn.ReLU(inplace=True), nn.Linear(84, 10), nn.Softmax(dim=1), ) # weight init for m in self.layers.children(): if isinstance(m, nn.Conv2d): nn.init.kaiming_normal_(m.weight) if isinstance(m, nn.Linear): nn.init.kaiming_normal_(m.weight) def forward(self, x): return self.layers(x) if __name__ == '__main__': # GPU or CPUの自動判別 device = 'cuda' if torch.cuda.is_available() else 'cpu' # modelの定義 model = cnn().to(device) opt = torch.optim.Adam(model.parameters()) # datasetの読み出し bs = 128 # batch size transform = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.5,), (0.5,))]) trainset = MNIST(root='./data', train=True, download=True, transform=transform) trainloader = DataLoader(trainset, batch_size=bs, shuffle=True) testset = MNIST(root='./data', train=False, download=True, transform=transform) testloader = DataLoader(testset, batch_size=bs, shuffle=False) # training print('train') model = model.train() for iepoch in range(3): for iiter, (x, y) in enumerate(trainloader, 0): # toGPU (CPUの場合はtoCPU) x = x.to(device) y = torch.eye(10)[y].to(device) # 推定 y_ = model.forward(x) # y_.shape = (bs, 84) # loss: cross-entropy eps = 1e-7 loss = -torch.mean(y*torch.log(y_+eps)) opt.zero_grad() # 勾配初期化 loss.backward() # backward (勾配計算) opt.step() # パラメータの微小移動 # 100回に1回進捗を表示(なくてもよい) if iiter % 100 == 0: print('%03d epoch, %05d, loss=%.5f' % (iepoch, iiter, loss.item())) if iepoch % 2 == 0: torch.save(model.state_dict(), "weights_{}.pth".format(iepoch)) torch.save(model.state_dict(), PATH) # test print('test') total, tp = 0, 0 model.load_state_dict(torch.load("weights/weights_2.pth")) model.eval() # the_model = TheModelClass(*args, **kwargs) # model.load_state_dict(torch.load(PATH)) for (x, label) in testloader: # to GPU x = x.to(device) # 推定 y_ = model.forward(x) label_ = y_.argmax(1).to('cpu') # 結果集計 total += label.shape[0] tp += (label_ == label).sum().item() acc = tp/total print('test accuracy = %.3f' % acc)
回答1件
あなたの回答
tips
プレビュー