<jupyter_start><jupyter_text>CNN<jupyter_code>import cv2
import torch
import torch.nn as nn
import torchvision.transforms as transforms
import torchvision
import torchvision.datasets as datasets
from torch.autograd import Variable
import matplotlib.pyplot as plt
from PIL import Image
import numpy as np
import time<jupyter_output><empty_output><jupyter_text>Load Data<jupyter_code>#Transformation for image
transform_ori = transforms.Compose([transforms.Resize((64, 64)),   #create 64x64 image
                                    #transforms.RandomCrop(32, padding=4),
                                    transforms.RandomHorizontalFlip(),    #flipping the image horizontally
                                    transforms.ToTensor(),                 #convert the image to a Tensor
                                    transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))#normalize the image
                                   ])  
                                    
#Load our dataset
train_dataset = datasets.ImageFolder(root = './train',
                                     transform = transform_ori)

test_dataset = datasets.ImageFolder(root = './test',
                                    transform = transform_ori)

#Make the dataset iterable
batch_size = 10
train_load = torch.utils.data.DataLoader(dataset = train_dataset, 
                                         batch_size = batch_size,
                                         shuffle = True)      #Shuffle to create a mixed batches of 100 of images

test_load = torch.utils.data.DataLoader(dataset = test_dataset, 
                                         batch_size = batch_size,
                                         shuffle = False)<jupyter_output><empty_output><jupyter_text>Show some of image<jupyter_code>labels_map = {0 : 'Bedroom', 1 : 'Coast', 2 : 'Forest', 3 : 'Highway', 4 : 'Industrial', 5 : 'InsideCity', 6 : 'Kitchen',
              7 : 'LivingRoom', 8 : 'Mountain', 9 : 'Office', 10 : 'OpenCountry', 11 : 'Store', 12 : 'Street', 13 : 'Suburb',
              14 : 'TallBuilding'};
fig = plt.figure(figsize=(8,8));
columns = 4;
rows = 5;
for i in range(1, columns*rows +1):
    img_xy = np.random.randint(len(train_dataset));
    img = train_dataset[img_xy][0][0,:,:]
    fig.add_subplot(rows, columns, i)
    plt.title(labels_map[train_dataset[img_xy][1]])
    plt.axis('off')
    plt.imshow(img, cmap='gray')
plt.show()
class CNN(nn.Module):
    def __init__(self):
        super(CNN, self).__init__()
        self.layer1 = nn.Sequential(
            nn.Conv2d(in_channels=3, out_channels=64, kernel_size=3, stride=1, padding=1),#kernel_size=3/5,padding=1/2
            nn.BatchNorm2d(num_features =64),
            nn.ReLU(),
            #nn.MaxPool2d(4)) #Maxpooling reduces the size by kernel size. 64/4 = 16
            nn.MaxPool2d(2)) #Maxpooling reduces the size by kernel size. 64/2 = 32
        self.layer2 = nn.Sequential(
            nn.Conv2d(in_channels=64, out_channels=64, kernel_size=3, stride=1, padding=1),
            nn.BatchNorm2d(num_features =64),
            nn.ReLU(),
            #nn.MaxPool2d(4)) #Maxpooling reduces the size by kernel size. 16/4 = 4
            nn.MaxPool2d(2)) #Maxpooling reduces the size by kernel size. 32/2 = 16
        
#         self.fc = nn.Sequential( #have 32 feature mapsfrom cnn2. Each of the feature is of size 16x16 --> 32*16*16 = 8192
#             nn.Linear(16*16*64, 2000),
#             nn.Dropout(p=0.5),                    #Dropout used to reduce overfitting
#             nn.Linear(2000, 500),
#             nn.Dropout(p=0.5), 
#             nn.Linear(500, 10)
#         )
        
        #self.fc = nn.Linear(8*8*64, 10)
        self.fc = nn.Linear(16*16*64, 10)
        
    def forward(self, x):
        out = self.layer1(x)
        out = self.layer2(out)
        out = out.view(out.size(0), -1)
        out = self.fc(out)
        return out
model = CNN()
CUDA = torch.cuda.is_available()
if CUDA:
    model = model.cuda()    
loss_fn = nn.CrossEntropyLoss()        
#optimizer = torch.optim.SGD(model.parameters(), lr = 0.01)
optimizer = torch.optim.Adam(model.parameters())
num_epochs = 50

#Define the lists to store the results of loss and accuracy
train_loss = []
test_loss = []
train_accuracy = []
test_accuracy = []
start = time.time()

#Training
for epoch in range(num_epochs): 
    #Reset these below variables to 0 at the begining of every epoch
    correct = 0
    iterations = 0
    iter_loss = 0.0
    
    model.train()                   # Put the network into training mode
    
    for i, (inputs, labels) in enumerate(train_load):
        # Convert torch tensor to Variable
        inputs = Variable(inputs)
        labels = Variable(labels)
        
        # If we have GPU, shift the data to GPU
        CUDA = torch.cuda.is_available()
        if CUDA:
            inputs = inputs.cuda()
            labels = labels.cuda()
        
        optimizer.zero_grad()            # Clear off the gradient in (w = w - gradient)
        outputs = model(inputs)         
        loss = loss_fn(outputs, labels)  
        iter_loss += loss.item()       # Accumulate the loss
        loss.backward()                 # Backpropagation 
        optimizer.step()                # Update the weights
        
        # Record the correct predictions for training data 
        _, predicted = torch.max(outputs, 1)
        correct += (predicted == labels).sum().item() 
        iterations += 1
    
    # Record the training loss
#     print(iterations,len(train_dataset))
    train_loss.append(iter_loss/iterations)
    # Record the training accuracy
    train_accuracy.append((correct / len(train_dataset)))
   
    #Testing
    loss = 0.0
    correct = 0
    iterations = 0

    model.eval()                    # Put the network into evaluation mode
    
    for i, (inputs, labels) in enumerate(test_load):
        
        # Convert torch tensor to Variable
        inputs = Variable(inputs)
        labels = Variable(labels)
        
        CUDA = torch.cuda.is_available()
        if CUDA:
            inputs = inputs.cuda()
            labels = labels.cuda()
        
        outputs = model(inputs)     
        # Record the correct predictions for training data
        _, predicted = torch.max(outputs, 1)
        correct += (predicted == labels).sum().item()
        
        iterations += 1

    # Record the Testing accuracy
    test_accuracy.append((correct / len(test_dataset)))
    
    if epoch % 5 == 0 or epoch+1 == num_epochs:
        print ('Epoch {}/{}, Training Loss: {:.3f}, Training Accuracy: {:.3f}, Testing Acc: {:.3f}'
           .format(epoch+1, num_epochs, train_loss[-1], train_accuracy[-1], test_accuracy[-1]))

end = time.time()
print('Time: {}s'.format(end-start))
def SaveModel():
    #Run this if you want to save the model
    torch.save(model.state_dict(),'animal-10_3.pth')
    
def LoadModel():
    #Run this if you want to load the model
    model.load_state_dict(torch.load('animal-10.pth'))
# Loss
f = plt.figure(figsize=(10, 10))
plt.plot(train_loss, label='Training Loss')
#plt.plot(test_loss, label='Testing Loss')
plt.legend()
plt.show()

# Accuracy
f = plt.figure(figsize=(10, 10))
plt.plot(train_accuracy, label='Training Accuracy')
plt.plot(test_accuracy, label='Testing Accuracy')
plt.legend()
plt.show()



def predict(img_name,model):
    image = Image.open(img_name).convert('RGB')
#     img = Image.fromarray(image)      #Convert the image to an array
#     img = transforms_photo(img)     #Apply the transformations 
    img = transform_ori(image)
    img = img.view(1,3,64,64)       #Add batch size 
    img = Variable(img)      
#     #Wrap the tensor to a variable
    
    
    model.eval()

    if torch.cuda.is_available():
        model = model.cuda()
        img = img.cuda()

    output = model(img)
    print(output)
    print(output.data)
    _, predicted = torch.max(output,1)
    p = labels_map[predicted.item()]

    plt.subplot(1,2,1),plt.imshow(image),plt.axis('off')
    plt.subplot(1,2,2),plt.imshow(img[0][0,:,:].cpu(), cmap='gray'),plt.axis('off')
    return  p
    

# pred = predict('./animal-10/val/cat/16.jpeg', model)
# print("The Predicted Label is {}".format(pred))    
Save = False
if Save == True: 
    SaveModel()
Load = False
if Load == True: 
    LoadModel()
pred = predict('./test/Bedroom/image_0003.jpg', model)
print("The Predicted Label is {}".format(pred))<jupyter_output>tensor([[-0.5217, -1.4249, -0.6579, -0.1773,  0.0835, -3.4457, -3.3071, -1.2948,
         -2.3416, -2.5373]], device='cuda:0', grad_fn=<AddmmBackward>)
tensor([[-0.5217, -1.4249, -0.6579, -0.1773,  0.0835, -3.4457, -3.3071, -1.2948,
         -2.3416, -2.5373]], device='cuda:0')
The Predicted Label is Industrial
{'fraction_non_alphanumeric': 0.1264591439688716, 'fraction_numerical': 0.03879606317235065, 'mean_word_length': 2.252326014142166, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '438145', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 2994, 'n_tokens_neox': 2741, 'n_words': 674, 'path': '.ipynb_checkpoints/CNN-checkpoint.ipynb', 'repo_name': 'YuanSyun/CV-CategorizeImages', 'size': '8738'}