-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
32 lines (28 loc) · 1.03 KB
/
model.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import torch
import torch.nn as nn
import os
from datetime import datetime
from torchvision.models import alexnet, AlexNet_Weights
class MyModel(object):
def __init__(self, model_dir):
self.model_dir = model_dir
self.net = alexnet(weights=AlexNet_Weights.DEFAULT)
self.net.classifier[6] = nn.Linear(in_features=4096, out_features=21, bias=True)
self.DEVICE = "cuda:1" if torch.cuda.is_available() else "cpu"
self.net.to(self.DEVICE)
print('Device is :' + str(self.DEVICE))
def eval(self):
self.net.eval()
model_path = os.path.join(self.model_dir, 'cnn_model.pth')
try:
self.net.load_state_dict(torch.load(model_path, map_location=self.DEVICE))
print('load model : cnn_model.pth')
self.net.classifier[6] = nn.Identity()
except Exception as e:
print('load model error')
dt_now = datetime.now()
with open('error.out', mode='a+') as f:
f.write(f'{dt_now}\nload model error\n{e}\n')
def save_model(self, model_path):
self.net.classifier[6] = nn.Identity()
torch.save(self.net.net.state_dict(), model_path)