<jupyter_start><jupyter_text>Dataset & Dataloader<jupyter_code>from torchvision import datasets, transforms
from torch.utils.data import DataLoader
bs = 128

transform = transforms.Compose([
    transforms.Grayscale(),
    transforms.ToTensor()    
])

train_set = datasets.ImageFolder("data/train/", transform=transform)
trainloader = DataLoader(train_set, batch_size=bs, shuffle=True, num_workers=2)

test_set = datasets.ImageFolder("data/test/", transform=transform)
testloader = DataLoader(test_set, batch_size=bs, shuffle=True, num_workers=2)
feature, target = next(iter(trainloader))
feature.shape
label2cat = train_set.classes
label2cat<jupyter_output><empty_output><jupyter_text>Arsitektur & Config<jupyter_code>from jcopdl.layers import linear_block
class FashionClassifier(nn.Module):
    def __init__(self, input_size, n1, n2, output_size, dropout=0.2):
        super().__init__()
        self.fc = nn.Sequential(
            nn.Flatten(),
            linear_block(input_size, n1, dropout=dropout),
            linear_block(n1, n2, dropout=dropout),
            linear_block(n2, output_size, activation="lsoftmax")
        )
        
    def forward(self, x):
        x = self.fc(x)
        return x
config = set_config({
    "input_size" : 784,
    "n1" : 256,
    "n2" : 64, 
    "dropout" : 0.2,
    "output_size" : len(train_set.classes)
})<jupyter_output><empty_output><jupyter_text>Training Preparation -> MCOC<jupyter_code>model = FashionClassifier(config.input_size, config.n1, config.n2, config.output_size, config.dropout).to(device)
criterion = nn.NLLLoss()  
optimizer = optim.AdamW(model.parameters(), lr=0.001)
callback = Callback(model, config, outdir="model")<jupyter_output><empty_output><jupyter_text>Training<jupyter_code>from tqdm.auto import tqdm

def loop_fn(mode, dataset, dataloader, model, criterion, optimizer, device):
    if mode == "train":
        model.train()
    elif mode == "test": 
        model.eval()
    
    cost = correct = 0
    
    for feature, target in tqdm(dataloader, desc=mode.title()):
        feature, target = feature.to(device), target.to(device)
        output = model(feature)
        loss = criterion(output, target)
        
        if mode == "train":
            loss.backward()
            optimizer.step()
            optimizer.zero_grad()
        
        cost += loss.item() * feature.shape[0]
        correct += (output.argmax(1) == target).sum().item()
        
    cost = cost / len(dataset)
    acc = correct / len(dataset)
    return cost, acc
while True:
    train_cost, train_score = loop_fn("train", train_set, trainloader, model, criterion, optimizer, device)
    with torch.no_grad():
        test_cost, test_score = loop_fn("test", test_set, testloader, model, criterion, optimizer, device)
        
    # Logging
    callback.log(train_cost, test_cost, train_score, test_score)
    
    # Checkpoint
    callback.save_checkpoint()
    
    # Runtime Plotting
    callback.cost_runtime_plotting()
    callback.score_runtime_plotting()
    
    # Early Stopping
    if callback.early_stopping(model, monitor='test_score'):
        callback.plot_cost()
        callback.plot_score()
        break<jupyter_output><empty_output><jupyter_text>Load best model<jupyter_code># weights = torch.load("model/weights_best.pth", map_location='cpu')
# config = torch.load("model/configs.pth", map_location='cpu')

# model = FashionClassifier(config.input_size, config.n1, config.n2, config.output_size, config.dropout).to(device)
# model.load_state_dict(weights)
# model = model.to(device)<jupyter_output><empty_output><jupyter_text>Predict<jupyter_code>feature, target = next(iter(testloader))
feature, target = feature.to(device), target.to(device)
with torch.no_grad():
    model.eval()
    output = model(feature)
    preds = output.argmax(1)
[label2cat[pred] for pred in preds]<jupyter_output><empty_output><jupyter_text>Visualisasi<jupyter_code>import matplotlib.pyplot as plt
fig, axes = plt.subplots(6, 6, figsize=(24, 24))
for img, label, pred, ax in zip(feature, target, preds, axes.flatten()):
    ax.imshow(img.view(28,28).cpu(), cmap='gray')
    font = {"color" : 'r'} if label != pred else {"color": 'g'}
    label, pred = label2cat[label.item()], label2cat[pred.item()]
    ax.set_title(f"Label: {label}) | Pred: {pred}", fontdict=font);
    ax.axis('off');<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13331808826892294, 'fraction_numerical': 0.011891150240109765, 'mean_word_length': 4.027586206896552, 'pattern_counts': {'":': 4, '<': 29, '<?xml version=': 0, '>': 30, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '224951', 'license': 'MIT', 'n_tokens_mistral': 1517, 'n_tokens_neox': 1471, 'n_words': 275, 'path': '13 - Neural Network/Part 8 - Callback and more ML engineering.ipynb', 'repo_name': 'rendywijayaa/Deep_Learning', 'size': '4373'}