<jupyter_start><jupyter_text>Import<jupyter_code>import matplotlib.pyplot as plt
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import torchvision
import torchvision.transforms as transforms
from torch.utils.tensorboard import SummaryWriter

import torchvision.models as models<jupyter_output><empty_output><jupyter_text>Load data<jupyter_code># Prepare data transformator
transform = transforms.Compose(
    [
        transforms.Resize((256,256)),
        transforms.ToTensor(),
#         transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
        transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225))
    ]
)

# Load train set
trainset = torchvision.datasets.CIFAR10(root='./data', train=True, download=True, transform=transform)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=4, shuffle=True)

# Load test set
testset = torchvision.datasets.CIFAR10(root='./data', train=False, download=True, transform=transform)
testloader = torch.utils.data.DataLoader(testset, batch_size=4, shuffle=False)

# Prepare class labels
classes = ('plane', 'car', 'bird', 'cat',
           'deer', 'dog', 'frog', 'horse', 'ship', 'truck')<jupyter_output>Files already downloaded and verified
Files already downloaded and verified<jupyter_text>Show sample<jupyter_code># function to show an image
def imshow(img, convert_gpu=False):
    if convert_gpu:
        img = img.cpu()
        
    img = img.numpy().transpose((1, 2, 0))
    mean = np.array([0.485, 0.456, 0.406])
    std = np.array([0.229, 0.224, 0.225])
    img = std * img + mean
    img = np.clip(img, 0, 1)
    
    plt.imshow(img)
    plt.show()


# show some test images
dataiter = iter(testloader)
images, labels = dataiter.next()

# show images
imshow(torchvision.utils.make_grid(images))
# print labels
print(' '.join(f'{classes[labels[j]]}' for j in range(4)))<jupyter_output><empty_output><jupyter_text>Build architecture<jupyter_code># class LeNet(nn.Module):
#     def __init__(self):
#         super(LeNet, self).__init__()
#         self.conv1 = nn.Conv2d(3, 6, 5)
#         self.pool = nn.MaxPool2d(2, 2)
#         self.conv2 = nn.Conv2d(6, 16, 5)
#         self.fc1 = nn.Linear(16 * 5 * 5, 120)
#         self.fc2 = nn.Linear(120, 84)
#         self.fc3 = nn.Linear(84, 10)

#     def forward(self, x):
#         x = self.pool(F.relu(self.conv1(x)))
#         x = self.pool(F.relu(self.conv2(x)))
#         x = x.view(-1, 16 * 5 * 5)
#         x = F.relu(self.fc1(x))
#         x = F.relu(self.fc2(x))
#         x = self.fc3(x)
#         return x


# net = LeNet()
%env TORCH_HOME=./data
# import ResNet-18
net = models.resnet18(pretrained=True)
for param in net.parameters():
    param.requires_grad = False

number_of_features = net.fc.in_features

net.fc = nn.Linear(number_of_features, 10)
criterion = nn.CrossEntropyLoss()
# optimizer = optim.SGD(net.fc.parameters(), lr=0.001, momentum=0.9)

optimizer = optim.SGD(net.fc.parameters(), lr=0.001, momentum=0.9)<jupyter_output><empty_output><jupyter_text>GPU<jupyter_code>device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
if device.type == 'cuda':
    net.to(device)
    print('GPU Activated')<jupyter_output>GPU Activated<jupyter_text>Train<jupyter_code>writer = SummaryWriter('runs/lenet')
for epoch in range(5):  # loop over the dataset multiple times

    running_loss = 0.0
    for i, data in enumerate(trainloader):
        # get the inputs; data is a list of [inputs, labels]
        if device.type == 'cuda':
            inputs, labels = data[0].to(device), data[1].to(device)
        else:
            inputs, labels = data

        # zero the parameter gradients
        optimizer.zero_grad()

        # forward
        outputs = net(inputs)
        loss = criterion(outputs, labels)
        # backward
        loss.backward()
        # optimize
        optimizer.step()

        # print statistics
        running_loss += loss.item()
        if i % 2000 == 1999:    # print every 2000 mini-batches
            print(f'epoch: {epoch + 1} batches: {i + 1} loss: {running_loss / 2000:.3f}')
            writer.add_scalar('resnet train loss', running_loss / 2000, epoch * len(trainloader)//2000 + ((i+1) / 2000) - 1)
            running_loss = 0.0

print('Finished Training')<jupyter_output>epoch: 1 batches: 2000 loss: 1.480
epoch: 1 batches: 4000 loss: 1.259
epoch: 1 batches: 6000 loss: 1.218
epoch: 1 batches: 8000 loss: 1.182
epoch: 1 batches: 10000 loss: 1.230
epoch: 1 batches: 12000 loss: 1.180
epoch: 2 batches: 2000 loss: 1.167
epoch: 2 batches: 4000 loss: 1.169
epoch: 2 batches: 6000 loss: 1.152
epoch: 2 batches: 8000 loss: 1.183
epoch: 2 batches: 10000 loss: 1.179
epoch: 2 batches: 12000 loss: 1.137
epoch: 3 batches: 2000 loss: 1.163
epoch: 3 batches: 4000 loss: 1.186
epoch: 3 batches: 6000 loss: 1.176
epoch: 3 batches: 8000 loss: 1.157
epoch: 3 batches: 10000 loss: 1.147
epoch: 3 batches: 12000 loss: 1.184
epoch: 4 batches: 2000 loss: 1.138
epoch: 4 batches: 4000 loss: 1.167
epoch: 4 batches: 6000 loss: 1.173
epoch: 4 batches: 8000 loss: 1.140
epoch: 4 batches: 10000 loss: 1.180
epoch: 4 batches: 12000 loss: 1.180
epoch: 5 batches: 2000 loss: 1.158
epoch: 5 batches: 4000 loss: 1.160
epoch: 5 batches: 6000 loss: 1.162
epoch: 5 batches: 8000 loss: 1.155
epoch: 5 bat[...]<jupyter_text>Testing<jupyter_code>correct = 0
total = 0
with torch.no_grad():
    for i, data in enumerate(testloader):
        # get the inputs; data is a list of [inputs, labels]
        if device.type == 'cuda':
            images, labels = data[0].to(device), data[1].to(device)
        else:
            images, labels = data
        
        # get outputs
        outputs = net(images)
        
        # gather which class had highest prediction score
        _, predicted = torch.max(outputs.data, 1)
        total += labels.size(0)
        correct += (predicted == labels).sum().item()
        
        # Show prediction for 1st batch
        if i == 0:
            imshow(torchvision.utils.make_grid(images), device.type == 'cuda')
            print('GroundTruth: ', ' '.join(f'{classes[labels[j]]}' for j in range(4)))
            print('Predicted: ', ' '.join(f'{classes[predicted[j]]}' for j in range(4)))
            
            
print(f'Accuracy of the network on the 10000 test images: {100 * correct / total} %')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13711694809255784, 'fraction_numerical': 0.07176360225140713, 'mean_word_length': 3.148508430609598, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 26, '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': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '332171', 'license': 'MIT', 'n_tokens_mistral': 2575, 'n_tokens_neox': 2208, 'n_words': 593, 'path': 'transfer-main-finished.ipynb', 'repo_name': 'Pitirus/ML-Practical-Aspects', 'size': '6396'}