text_plain_output_2.png text_plain_output_1.png image_output_1.png
learning_rate = 5e-05
weight_decay = 1e-06
patience = 10
verbose_ct = 1
epochs = 2500
model_conv = models.resnet18(pretrained=True)
num_ftrs = model_conv.fc.in_features
model_conv.fc = nn.Linear(num_ftrs, 256)
model_conv.fc2 = nn.Linear(256, 20)
model_conv.sfact = nn.Softmax(1)
model_conv = model_conv.to(device)
loss_function = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model_conv.parameters(), lr=learning_rate, weight_decay=weight_decay)
t_losses, v_losses = train(model_conv, train_loader, val_loader, optimizer, epochs, device=device, patience=patience, verbose_ct=verbose_ct)
sns.set(style='darkgrid')
plot_losses(t_losses, v_losses, 'CNN_Training_Loss_transfer.pdf')
torch.save(model_conv.state_dict(), 'ResNet-Trained.pt')
predictions, labels = evaluate(model_conv, test_loader, device=device)
y_true = np.concatenate(labels, axis=0)
y_pred = np.concatenate(predictions, axis=0)
print(classification_report(y_true, y_pred))
matplotlib.rc_file_defaults()
cfmatrix = confusion_matrix(y_true, y_pred)
plot_cm(cfmatrix, 'CNN Confusion Matrix - Transfer', artists)