<jupyter_start><jupyter_text>15. CNN with Dogs and Cats<jupyter_code>import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import DataLoader
from torch.utils.data import Subset, random_split

import torchvision.utils
import torchvision.datasets as dsets
import torchvision.transforms as transforms

import numpy as np
import os
import matplotlib.pyplot as plt
%matplotlib inline<jupyter_output><empty_output><jupyter_text>15.1 Prepare Data<jupyter_code># 사이즈가 각각 모두 다르므로 통일해주어야함.
transform = transforms.Compose([
    transforms.Resize((96, 96)),
    transforms.ToTensor()
])

data = dsets.ImageFolder('data/dogcat/', transform)
data.class_to_idx
train_data, test_data = random_split(data, [20000, 5000])
print("Train data : ", len(train_data))
print("Test data : ", len(test_data))
# 혹은
# train_data = Subset(data, [0, ...., 20000-1])
# test_data = Subset(data, [20000-1, ...., 25000-1])
batch_size = 128

train_loader = DataLoader(train_data, 
                          batch_size=batch_size,
                          shuffle=True)

test_loader = DataLoader(test_data, 
                         batch_size=batch_size,
                         shuffle=False)
def imshow(img, title):
    img = torchvision.utils.make_grid(img, normalize=True)
    npimg = img.numpy()
    fig = plt.figure(figsize = (5, 15))
    plt.imshow(np.transpose(npimg,(1,2,0)))
    plt.title(title)
    plt.axis('off')
    plt.show()
train_iter = iter(train_loader)
images, labels = train_iter.next()

imshow(images, "Train Image")
images.shape<jupyter_output><empty_output><jupyter_text>15.2 Define Model<jupyter_code>class CNN(nn.Module):
    def __init__(self):
        super(CNN, self).__init__()
        
        self.conv_layer = nn.Sequential(
            nn.Conv2d(3, 32, 5), # 32 * 92 * 92
            nn.ReLU(),
            nn.MaxPool2d(2,2), # 32 * 46 * 46
            nn.Conv2d(32, 64, 5), # 64 * 42 * 42
            nn.ReLU(),
            nn.MaxPool2d(2,2), # 64 * 21 * 21
            nn.Conv2d(64, 128, 4), # 128 * 18 * 18
            nn.ReLU(),
            nn.MaxPool2d(2,2) # 128 * 9 * 9
        )

        self.fc_layer = nn.Sequential(
            nn.Linear(128*9*9, 100),
            nn.ReLU(),
            nn.Linear(100, 2)              
        )
        
    def forward(self, x):
        out = self.conv_layer(x)
        out = out.view(-1, 128*9*9)
        out = self.fc_layer(out)
        
        return out
    
model = CNN().cuda()
loss = nn.CrossEntropyLoss()
optimizer = optim.SGD(model.parameters(), lr=0.1)<jupyter_output><empty_output><jupyter_text>15.3 Train Model<jupyter_code>num_epochs = 5
for epoch in range(num_epochs):

    total_batch = len(train_data) // batch_size
    
    for i, (batch_images, batch_labels) in enumerate(train_loader):
        
        X = batch_images.cuda()
        Y = batch_labels.cuda()

        pre = model(X)
        cost = loss(pre, Y)

        optimizer.zero_grad()
        cost.backward()
        optimizer.step()

        if (i+1) % 50 == 0:
            print('Epoch [%d/%d], lter [%d/%d], Loss: %.4f'
                 %(epoch+1, num_epochs, i+1, total_batch, cost.item()))<jupyter_output>Epoch [1/5], lter [50/156], Loss: 0.6859
Epoch [1/5], lter [100/156], Loss: 0.6896
Epoch [1/5], lter [150/156], Loss: 0.6814
Epoch [2/5], lter [50/156], Loss: 0.6995
Epoch [2/5], lter [100/156], Loss: 0.6806
Epoch [2/5], lter [150/156], Loss: 0.7262
Epoch [3/5], lter [50/156], Loss: 0.6430
Epoch [3/5], lter [100/156], Loss: 0.6455
Epoch [3/5], lter [150/156], Loss: 0.6120
Epoch [4/5], lter [50/156], Loss: 0.6064
Epoch [4/5], lter [100/156], Loss: 0.6017
Epoch [4/5], lter [150/156], Loss: 0.5771
Epoch [5/5], lter [50/156], Loss: 0.5936
Epoch [5/5], lter [100/156], Loss: 0.5737
Epoch [5/5], lter [150/156], Loss: 0.5591<jupyter_text>15.4 Test Model<jupyter_code>correct = 0
total = 0

for images, labels in test_loader:
    
    images = images.cuda()
    outputs = model(images)
    
    _, predicted = torch.max(outputs.data, 1)
    
    total += labels.size(0)
    correct += (predicted == labels.cuda()).sum()
    
print('Accuracy of test images: %f %%' % (100 * float(correct) / total))<jupyter_output>Accuracy of test images: 67.680000 %
{'fraction_non_alphanumeric': 0.15001186802753383, 'fraction_numerical': 0.08331355328744362, 'mean_word_length': 2.6997366110623355, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, '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': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '552063', 'license': 'MIT', 'n_tokens_mistral': 1818, 'n_tokens_neox': 1602, 'n_words': 351, 'path': 'Week4/15. CNN with Dogs and Cats_seungju.ipynb', 'repo_name': 'Seungju182/pytorch-basic', 'size': '4213'}