<jupyter_start><jupyter_text>grabbing a batch of images<jupyter_code>for images,labels in trainloader:
    break
images.shape

im= make_grid(images,nrow=5)

#inverse normalize to display original images
inv_normalize = transforms.Normalize(
    mean=[-0.485/0.229, -0.456/0.224, -0.406/0.225],
    std=[1/0.229, 1/0.224, 1/0.225]
)
im_inv=inv_normalize(im)

# Print the images
plt.figure(figsize=(12,4))
plt.imshow(np.transpose(im_inv.numpy(), (1, 2, 0)));<jupyter_output><empty_output><jupyter_text>defining model<jupyter_code>class ConvNet(nn.Module):
    def __init__(self):
        super().__init__()
        self.conv1=nn.Conv2d(3,6,3,1)
        self.conv2=nn.Conv2d(6,16,3,1)
        self.fc1=nn.Linear(54*54*16,120)  #(((224-2)/2)-2)/2= 54.5 -->54*54 
        self.fc2= nn.Linear(120,84)
        self.fc3= nn.Linear(84,2)
    def forward(self,X):
        X=F.relu(self.conv1(X))
        X=F.max_pool2d(X,2,2)
        X=F.relu(self.conv2(X))
        X=F.max_pool2d(X,2,2)
        X=X.view(-1,54*54*16)
        X=F.relu(self.fc1(X))
        X=F.relu(self.fc2(X))
        X=self.fc3(X)
        
        return F.log_softmax(X,dim=1)
torch.manual_seed(101)
cnnmodel=ConvNet()
criterion= nn.CrossEntropyLoss()
optimizer= torch.optim.Adam(cnnmodel.parameters(),lr=0.001)
cnnmodel
for param in cnnmodel.parameters():
    print(param.numel())
import time
starttime=time.time()

epochs=3
#limits on num of batches
maxtrnbatch=800 #batch has 10 imgs-->8000imgs
maxtstbatch=300 #3000 imgs

for i in range(epochs):
    for b,(X_train,y_train) in enumerate(trainloader):
        #optional limit no of batches
        if b==maxtrnbatch:
            break
        b+=1
        y_pred=cnnmodel(X_train)
        loss=criterion(y_pred,y_train)
        
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
        
        if b%200==0:
            print(f'epoch {i} loss {loss.item()}')
    #testset
    with torch.no_grad():
        for b,(X_test,y_test) in enumerate(testloader):
            
            if b==maxtstbatch:
                break
            y_val=cnnmodel(X_test)
            
        loss=criterion(y_val,y_test)
        


totaltime=time.time()-starttime
print(f'Totaltime{totaltime/60} mins')
torch.save(cnnmodel.state_dict(),'customcnnmodel.pt')<jupyter_output><empty_output><jupyter_text>using a pretrained modelusing alexnethere images have to be normalized the same way as how alexnet was trained bfore<jupyter_code>Alexnet=models.alexnet(pretrained=True)
Alexnet
#freezing the pretrained weights n biases so we dont propagate thru those weights and biases 
for param in Alexnet.parameters():
    param.requires_grad=False
#modifying the classifier since the alexnet worked on 1000 categories which we dont want
#chnge the classifier unit alone in the above structure

torch.manual_seed(42)
Alexnet.classifier= nn.Sequential(nn.Linear(9216,1024),nn.ReLU(),nn.Dropout(0.5),nn.Linear(1024,2),nn.LogSoftmax(dim=1))
Alexnet
for param in Alexnet.parameters():
    print(param.numel())
criterion=nn.CrossEntropyLoss()
optimizer= torch.optim.Adam(Alexnet.classifier.parameters(),lr=0.001)  #only classifier params are optimized since thats only changed

import time
start_time = time.time()

epochs = 1

max_trn_batch = 800
max_tst_batch = 300

train_losses = []
test_losses = []
train_correct = []
test_correct = []

for i in range(epochs):
    trn_corr = 0
    tst_corr = 0
    
    # Run the training batches
    for b, (X_train, y_train) in enumerate(trainloader):
        if b == max_trn_batch:
            break
        b+=1
        
        # Apply the model
        y_pred = Alexnet(X_train)
        loss = criterion(y_pred, y_train)
 
        # Tally the number of correct predictions
        predicted = torch.max(y_pred.data, 1)[1]
        batch_corr = (predicted == y_train).sum()
        trn_corr += batch_corr
        
        # Update parameters
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

        # Print interim results
        if b%200 == 0:
            print(f'epoch: {i:2}  batch: {b:4} [{10*b:6}/8000]  loss: {loss.item():10.8f}  \
accuracy: {trn_corr.item()*100/(10*b):7.3f}%')

    train_losses.append(loss)
    train_correct.append(trn_corr)

    # Run the testing batches
    with torch.no_grad():
        for b, (X_test, y_test) in enumerate(testloader):
            if b == max_tst_batch:
                break

            # Apply the model
            y_val = Alexnet(X_test)

            # Tally the number of correct predictions
            predicted = torch.max(y_val.data, 1)[1] 
            tst_corr += (predicted == y_test).sum()

    loss = criterion(y_val, y_test)
    test_losses.append(loss)
    test_correct.append(tst_corr)

print(f'\nDuration: {time.time() - start_time:.0f} seconds') # print the time elapsed<jupyter_output>epoch:  0  batch:  200 [  2000/8000]  loss: 3.17440605  accuracy:  88.400%
epoch:  0  batch:  400 [  4000/8000]  loss: 0.05652046  accuracy:  90.775%
epoch:  0  batch:  600 [  6000/8000]  loss: 0.14692737  accuracy:  91.633%
epoch:  0  batch:  800 [  8000/8000]  loss: 0.09021058  accuracy:  91.950%

Duration: 439 seconds<jupyter_text>using a single image to compare models<jupyter_code>imageindex=2019
im=inv_normalize(testdata[imageindex][0])
plt.imshow(np.transpose(im.numpy(),(1,2,0)))
cnnmodel.eval()
with torch.no_grad():
    newpred=cnnmodel(testdata[imageindex][0].view(1,3,224,224)).argmax()
classnames[newpred.item()]
Alexnet.eval()
with torch.no_grad():
    newpred=Alexnet(testdata[imageindex][0].view(1,3,224,224)).argmax()
classnames[newpred.item()]<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup