<jupyter_start><jupyter_text>Utility functions<jupyter_code>def imshow(inp,cmap=None):
    """Imshow for Tensor."""
    inp = inp.numpy().transpose((1, 2, 0))
    mean = np.array([0.485, 0.456, 0.406])
    std = np.array([0.229, 0.224, 0.225])
    inp = std * inp + mean
    inp = np.clip(inp, 0, 1)
    plt.imshow(inp,cmap)
    
class FeaturesDataset(Dataset):
    
    def __init__(self,features_list,labels_list):
        self.features_list = features_list
        self.labels_list = labels_list
        
    def __getitem__(self,index):
        return (self.features_list[index],self.labels_list[index])
    
    def __len__(self):
        return len(self.labels_list)
    
def fit(epoch, model,data_loader,phase='training',volatile=False):
    if phase == 'training':
        model.train()
    if phase == 'validation':
        model.eval()
        volatile=True
    cummulative_loss = 0.0
    cummulative_correct = 0
    for batch_idx , (data,target) in enumerate(data_loader):
        if is_cuda:
            data,target = data.cuda(),target.cuda()
        data , target = Variable(data,volatile),Variable(target)
        if phase == 'training':
            optimizer.zero_grad()
        output = model(data)
        loss = F.cross_entropy(output,target)
        
        cummulative_loss += F.cross_entropy(output,target,size_average=False).data[0]
        preds = output.data.max(dim=1,keepdim=True)[1]
        cummulative_correct += preds.eq(target.data.view_as(preds)).cpu().sum()
        if phase == 'training':
            loss.backward()
            optimizer.step()
    
    loss = cummulative_loss/len(data_loader.dataset)
    accuracy = 100. * cummulative_correct/len(data_loader.dataset)
    
    print(f'{phase} loss is {loss} and {phase} accuracy is {cummulative_correct}/{len(data_loader.dataset)}{accuracy}')
    return loss,accuracy<jupyter_output><empty_output><jupyter_text>Creating PyTorch datasets<jupyter_code>transform_data = transforms.Compose([
        transforms.Resize((299,299)),
        transforms.ToTensor(),
        transforms.Normalize([0.30, 0.40, 0.40], [0.20, 0.20, 0.20])
    ])
train_dataset = ImageFolder('../Chapter03/Dog-Cat-Classifier/Data/Train_Data/train/',transform=transform_data)
validation_dataset = ImageFolder('../Chapter03/Dog-Cat-Classifier/Data/Train_Data/valid/',transform=transform_data)
classes=2
imshow(train_dataset[150][0])<jupyter_output><empty_output><jupyter_text>Creating data loader for training and validation datasets<jupyter_code>training_data_loader = DataLoader(train_dataset,batch_size=32,shuffle=False,num_workers=4)
validation_data_loader = DataLoader(validation_dataset,batch_size=32,shuffle=False,num_workers=4)<jupyter_output><empty_output><jupyter_text>Creating ResNet 34 model<jupyter_code>resnet_model = resnet34(pretrained=True)

if is_cuda:
    resnet_model = resnet_model.cuda()
m = nn.Sequential(*list(resnet_model.children())[:-1])<jupyter_output><empty_output><jupyter_text>Extracting Convolutional features<jupyter_code># Stores the labels of the train data
training_data_labels = [] 

# Stores the pre convoluted features of the train data
training_features = [] 

#Iterate through the train data and store the calculated features and the labels
for d,la in training_data_loader:
    with torch.no_grad(): # To help with memory issues
        o = m(Variable(d.cuda()))
        o = o.view(o.size(0),-1)
        training_data_labels.extend(la)
        training_features.extend(o.cpu().data)

#For validation data

#Iterate through the validation data and store the calculated features and the labels

validation_data_labels = []
validation_features = []
for d,la in validation_data_loader:
    with torch.no_grad(): # To help with memory issues
        o = m(Variable(d.cuda()))
        o = o.view(o.size(0),-1)
        validation_data_labels.extend(la)
        validation_features.extend(o.cpu().data)<jupyter_output><empty_output><jupyter_text>Creating train and validation feature dataset<jupyter_code>#Creating dataset for train and validation
train_features_dataset = FeaturesDataset(training_features,training_data_labels)
validation_features_dataset = FeaturesDataset(validation_features,validation_data_labels)

#Creating data loader for train and validation
train_features_loader = DataLoader(train_features_dataset,batch_size=64,shuffle=True)
validation_features_loader = DataLoader(validation_features_dataset,batch_size=64)<jupyter_output><empty_output><jupyter_text>A Fully connected network<jupyter_code>class FullyConnectedLinearModel(nn.Module):
    
    def __init__(self,input_size,output_size):
        super().__init__()
        self.fc = nn.Linear(input_size,output_size)

    def forward(self,inp):
        out = self.fc(inp)
        return out
fully_connected_in_size = 8192
fc = FullyConnectedLinearModel(fully_connected_in_size,classes)
if is_cuda:
    fc = fc.cuda()
optimizer = optim.Adam(fc.parameters(),lr=0.0001)<jupyter_output><empty_output><jupyter_text>Train and validate the model<jupyter_code>train_losses , train_accuracy = [],[]
validation_losses , validation_accuracy = [],[]
for epoch in range(1,20):
    epoch_loss, epoch_accuracy = fit(epoch,fc,train_features_loader,phase='training')
    validation_epoch_loss , validation_epoch_accuracy = fit(epoch,fc,validation_features_loader,phase='validation')
    train_losses.append(epoch_loss)
    train_accuracy.append(epoch_accuracy)
    validation_losses.append(validation_epoch_loss)
    validation_accuracy.append(validation_epoch_accuracy)<jupyter_output>training loss is 0.8001697063446045 and training accuracy is 601/114952
validation loss is 0.6819716095924377 and validation accuracy is 150/25060
training loss is 0.698416531085968 and training accuracy is 669/114958
validation loss is 0.61382657289505 and validation accuracy is 170/25068
training loss is 0.5987679958343506 and training accuracy is 796/114969
validation loss is 0.5830464959144592 and validation accuracy is 176/25070
training loss is 0.5694668889045715 and training accuracy is 822/114971
validation loss is 0.5522308349609375 and validation accuracy is 187/25074
training loss is 0.5458188056945801 and training accuracy is 866/114975
validation loss is 0.5322710275650024 and validation accuracy is 190/25076
training loss is 0.5245423913002014 and training accuracy is 870/114975
validation loss is 0.5251204371452332 and validation accuracy is 186/25074
training loss is 0.520514726638794 and training accuracy is 867/114975
validation loss is 0.5525538325309753 and validati[...]
{'fraction_non_alphanumeric': 0.10077993576999542, 'fraction_numerical': 0.0683590763113626, 'mean_word_length': 4.860215053763441, 'pattern_counts': {'":': 0, '<': 32, '<?xml version=': 0, '>': 32, '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': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '570368', 'license': 'MIT', 'n_tokens_mistral': 2377, 'n_tokens_neox': 2087, 'n_words': 421, 'path': 'Chapter08/DogsandCatsUsingResnet.ipynb', 'repo_name': 'PacktPublishing/Deep-Learning-with-PyTorch-1.x', 'size': '6539'}