<jupyter_start><jupyter_text>Model Building<jupyter_code>class Net(nn.Module):
    
    def __init__(self):
        super(Net, self).__init__()
        
        #Convolution layers
        self.conv1 = nn.Conv2d(3, 16, 3, stride=3)
        self.conv2 = nn.Conv2d(16, 32, 3, stride=3)
        self.conv3 = nn.Conv2d(32, 64, 3, stride=3)
        
        #Max pooling layers
        self.pool = nn.MaxPool2d(2, 2)
        
        #Fully connected layer
        self.fc1 = nn.Linear(64, 500)
        self.fc2 = nn.Linear(500, 256)
        self.fc3 = nn.Linear(256, 2)
        
        self.drop_out = nn.Dropout(0.25)
        
    def forward(self, x):
        x = self.pool(F.relu(self.conv1(x)))
        x = self.pool(F.relu(self.conv2(x)))
        x = self.pool(F.relu(self.conv3(x)))
        
        
        x = x.view(-1, 64)
        
        x = self.drop_out(x)
        
        x = F.relu(self.fc1(x))
        
        x = self.drop_out(x)

        x = F.relu(self.fc2(x))
        
        x = self.drop_out(x)

        x = self.fc3(x)
        return x
model = Net()
if train_on_gpu:
    model.cuda()

criterion = nn.CrossEntropyLoss()

optimizer = optim.Adam(model.parameters(), lr=0.001)<jupyter_output><empty_output><jupyter_text>Training the network<jupyter_code># number of epochs to train the model
# train_on_gpu = True
import numpy as np

n_epochs = 30

valid_loss_min = np.Inf # track change in validation loss

for epoch in range(1, n_epochs+1):

    # keep track of training and validation loss
    train_loss = 0.0
    valid_loss = 0.0
    
    ###################
    # train the model #
    ###################
    model.train()
    for data, target in data_loader:
        # move tensors to GPU if CUDA is available
        if train_on_gpu:
            data, target = data.cuda(), target.cuda()
        # clear the gradients of all optimized variables
        optimizer.zero_grad()
        # forward pass: compute predicted outputs by passing inputs to the model
        output = model(data)
        # calculate the batch loss
        loss = criterion(output, target)
        # backward pass: compute gradient of the loss with respect to model parameters
        loss.backward()
        # perform a single optimization step (parameter update)
        optimizer.step()
        # update training loss
        train_loss += loss.item()*data.size(0)
        
    ######################    
    # validate the model #
    ######################
    model.eval()
    for data, target in test_data_loader:
        # move tensors to GPU if CUDA is available
        if train_on_gpu:
            data, target = data.cuda(), target.cuda()
        # forward pass: compute predicted outputs by passing inputs to the model
        output = model(data)
        # calculate the batch loss
        loss = criterion(output, target)
        # update average validation loss 
        valid_loss += loss.item()*data.size(0)
    
    # calculate average losses
    train_loss = train_loss/len(data_loader.sampler)
    valid_loss = valid_loss/len(test_data_loader.sampler)
        
    # print training/validation statistics 
    print('Epoch: {} \tTraining Loss: {:.6f}\tValidation Loss: {:.6f}'.format(
        epoch, train_loss, valid_loss))
    
    # save model if validation loss has decreased
    if valid_loss <= valid_loss_min:
        print('Validation loss decreased ({:.6f} --> {:.6f}).  Saving model ...'.format(
        valid_loss_min,
        valid_loss))
        torch.save(model.state_dict(), 'cat_vs_dog_nn.pth')
        valid_loss_min = valid_loss
print("Welldone")
# model_s = torch.load('cat_vs_dog_nn.pt')
# model_s.eval()
saved_model = torch.load('cat_vs_dog_nn.pth')
print(saved_model)


test_transform = transforms.Compose([transforms.Resize(224),
                               transforms.CenterCrop(224),
                               transforms.ToTensor()])


# def predict_image(image):
#     image_tensor = test_transform(image).float()
#     image_tensor = image_tensor.unsqueeze_(0)
#     input = Variable(image_tensor)
#     input = input.cuda()
#     output = model(input)
#     index = output.data.cpu().numpy().argmax()
#     return index

import os
data_dir = 'D:\practice'
cat_data_dir = os.path.join(data_dir, '0')
print(cat_data_dir)

images = os.listdir(cat_data_dir)
images = images[0:100]
print(len(images))
from PIL import Image
import matplotlib.pyplot as plt
%matplotlib inline

test_image_dir = 'D:\practice'
sub_folder = os.path.join(test_image_dir, '0')
full_abs_path = os.path.join(sub_folder, 'cat.1121.jpg')
print(full_abs_path)
im = Image.open(full_abs_path)
plt.imshow(im)

def apply_test_transforms(inp):
    out = transforms.functional.resize(inp, [224,224])
    out = transforms.functional.to_tensor(out)
    out = transforms.functional.normalize(out, [0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
    return out
test_image = apply_test_transforms(im)
test_image.shape
def predict_image(image):
    image_tensor = apply_test_transforms(image).float()
    image_tensor = image_tensor.unsqueeze_(0)
    input = image_tensor.cuda()
    output = model(input)
    index = output.data.cpu().numpy().argmax()
    return index

predict_image(im)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12801377726750862, 'fraction_numerical': 0.023344814389590508, 'mean_word_length': 2.647592463363573, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 10, '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': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '653809', 'license': 'MIT', 'n_tokens_mistral': 1808, 'n_tokens_neox': 1706, 'n_words': 386, 'path': 'Cat and Dog classifier/cat vs dog classifier with CNN.ipynb', 'repo_name': 'prasad5141/DeepLearning-projects', 'size': '5226'}