Created
July 26, 2022 15:44
-
-
Save ashhadulislam/45b421c126baa1e490c01c9138585d61 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PATH = './resnet18_net.pth' | |
#setup model | |
model_ft = models.resnet18(pretrained=True) | |
num_ftrs = model_ft.fc.in_features | |
model_ft.fc = nn.Linear(num_ftrs, len(classes)) | |
model_ft = model_ft.to(device) | |
criterion = nn.CrossEntropyLoss() | |
# Observe that all parameters are being optimized | |
optimizer_ft = optim.SGD(model_ft.parameters(), lr=0.001, momentum=0.9) | |
# Decay LR by a factor of 0.1 every 7 epochs | |
exp_lr_scheduler = lr_scheduler.StepLR(optimizer_ft, step_size=7, gamma=0.1) | |
# training | |
model_ft = train_model(model_ft, criterion, optimizer_ft, exp_lr_scheduler, | |
num_epochs=num_epochs) | |
torch.save(model_ft.state_dict(), PATH) | |
# load model | |
model_ft3 = models.resnet18(pretrained=True) | |
num_ftrs = model_ft3.fc.in_features | |
model_ft3.fc = nn.Linear(num_ftrs, len(classes)) | |
model_ft3.to(device) | |
model_ft3.load_state_dict(torch.load(PATH,map_location=device)) | |
model_ft3.eval() | |
# test accuracy | |
print(accuracy(model_ft3, test_loader)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment