<jupyter_start><jupyter_text>Load Model<jupyter_code>model_pth = os.path.join(dataset_location, 'model.pth')
model_pth
chpt = torch.load(model_pth)
model = models.densenet201(pretrained=True)

for param in model.parameters():
    param.requires_grad = False
classifier = nn.Sequential(OrderedDict([
    ('bn1', nn.BatchNorm1d(1920)),
    ('fc1', nn.Linear(1920, 128)),
    ('elu1', nn.ELU()),
    ('drp1', nn.Dropout(0.75)),
    
#     ('fc2', nn.Linear(512, 512)),
#     ('elu2', nn.ELU()),
#     ('drp2', nn.Dropout(0.75)),
    
    ('fcf', nn.Linear(128, 102)),
]))
model.classifier = classifier
model.load_state_dict(chpt['state_dict'])
model.to(device);<jupyter_output><empty_output><jupyter_text>Test Model **Create Dataloaders**<jupyter_code>transforms_valid = transforms.Compose([
    transforms.Resize(256),
    transforms.CenterCrop(224),
    transforms.ToTensor(),
    transforms.Normalize([0.485, 0.456, 0.406], 
                         [0.229, 0.224, 0.225])
])
dataset_valid = datasets.ImageFolder(os.path.join(dataset_location, 'valid'), transforms_valid)
dataset_test = datasets.ImageFolder(os.path.join(dataset_location, 'test'), transforms_valid)
print('Number of valid images:', len(dataset_valid))
print('Number of test images:', len(dataset_test))
dataloader_valid = torch.utils.data.DataLoader(dataset_valid, batch_size=16, shuffle=True,
                                               num_workers=6, pin_memory=True)
dataloader_test = torch.utils.data.DataLoader(dataset_test, batch_size=16, shuffle=True,
                                               num_workers=6, pin_memory=True)<jupyter_output><empty_output><jupyter_text>**Test Model**<jupyter_code>def accuracy(logits, labels): 
    predictions = torch.argmax(logits, dim=1)
    return (predictions == labels).float().mean()  # tensor!!
hist = { 'valid_loss':[], 'valid_acc':[] }        # valid set loss/acc every epoch
criterion = nn.CrossEntropyLoss()
batch_size = 16

model.eval()

### Evaluate Valid Set ###
loss_sum = 0
acc_sum = 0
for images, labels in dataloader_valid:

    # Push to GPU
    x = images.to(device)
    y = labels.to(device)

    with torch.no_grad():
        outputs = model(x)
        loss = criterion(outputs, y)
        acc = accuracy(outputs, y)
        loss_sum += loss.item() * len(images)
        acc_sum += acc.item() * len(images)

valid_loss = loss_sum / len(dataset_valid)
valid_acc = acc_sum / len(dataset_valid)

### Evaluate Test Set ###
loss_sum = 0
acc_sum = 0
for images, labels in dataloader_test:

    # Push to GPU
    x = images.to(device)
    y = labels.to(device)

    with torch.no_grad():
        outputs = model(x)
        loss = criterion(outputs, y)
        acc = accuracy(outputs, y)
        loss_sum += loss.item() * len(images)
        acc_sum += acc.item() * len(images)

test_loss = loss_sum / len(dataset_test)
test_acc = acc_sum / len(dataset_test)
print( '         loss / acc')
print(f'Valid: {valid_loss:6.4f} / {valid_acc:4.4f}')
print(f'Test:  {test_loss:6.4f} / {test_acc:4.4f}')<jupyter_output>loss / acc
Valid: 0.1063 / 0.9804
Test:  0.1039 / 0.9719
{'fraction_non_alphanumeric': 0.15051679586563307, 'fraction_numerical': 0.035852713178294575, 'mean_word_length': 3.5477239353891337, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '324799', 'license': 'MIT', 'n_tokens_mistral': 1168, 'n_tokens_neox': 1088, 'n_words': 204, 'path': 'PyTorchNN/1450_PT_DenseNet121_Transfer_2_Load.ipynb', 'repo_name': 'marcinbogdanski/ai-sketchpad', 'size': '3096'}