<jupyter_start><jupyter_text><jupyter_code>import os
import random
import shutil
import cv2
import zipfile
import torch.optim as optim
import torch
import torchvision.transforms as transforms
zipf = zipfile.ZipFile('/content/Def_class.zip','r')
zipf.extractall('/content/data')
zipf.close()
os.listdir('/content/data/Def_class')  #size of the crops in the dataset = 32x32
ttr = transforms.Compose([
    transforms.RandomVerticalFlip(p=1.0),
    transforms.RandomHorizontalFlip(p=1.0),
    transforms.RandomRotation(90),
    transforms.ToTensor(),
    transforms.Normalize(mean=[0.5,0.5,0.5], std=[0.5,0.5,0.5])
])
tte = transforms.Compose([
    transforms.ToTensor(),
    transforms.Normalize(mean=[0.5,0.5,0.5], std=[0.5,0.5,0.5])
])
import torch
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
print(device)
import torch.nn as nn
import torch.nn.functional as F

class cnn(nn.Module):
    def __init__(self):
        super(cnn, self).__init__()
        self.dep1 = nn.Conv2d(3,3,3,groups = 3)
        self.point1 = nn.Conv2d(3,16,1)
        self.dep2 = nn.Conv2d(16,16,3,groups = 16)
        self.point2 = nn.Conv2d(16,32,1)
        self.dep3 = nn.Conv2d(32,32,3,groups = 32)
        self.point3 = nn.Conv2d(32,64,1)
        #self.con3 = nn.Conv2d(64,32,3)
        self.pool = nn.MaxPool2d(2,2)
        self.lin1 = nn.Linear(256,256)
        self.lin2 = nn.Linear(256,2)
    def forward(self,x):
        x = self.point1(self.dep1(x))
        x = self.pool(F.relu(x))
        x = self.point2(self.dep2(x))
        x = self.pool(F.relu(x))
        x = self.point3(self.dep3(x))
        x = self.pool(F.relu(x))
        #x = self.pool(F.relu(self.con3(x)))
        x = x.view(-1,256)
        x = F.relu(self.lin1(x))
        x = self.lin2(x)
        return F.softmax(x,dim=1)
mod = cnn().to(device)   
import numpy as np
total_params = 0
for parameter in mod.parameters():
  if parameter.requires_grad:
    total_params += np.prod(parameter.size())
print('total_params:', total_params)
import torchvision.datasets as datasets
from sklearn.model_selection import KFold
data = datasets.ImageFolder('/content/data/Def_class',transform = ttr)
kf = KFold(n_splits=5, shuffle=True)
t=[]
v=[]
for j, (train_index, test_index) in enumerate(kf.split(data)):
    train = torch.utils.data.Subset(data, train_index)
    test = torch.utils.data.Subset(data, test_index)    
    #print(len(train))
    train_loader = torch.utils.data.DataLoader(train, batch_size=32, shuffle=True)
    val_loader = torch.utils.data.DataLoader(test, batch_size=32, shuffle=True)
    t.append(train_loader)
    v.append(val_loader)
train_acc=[]

for j,train_loader in enumerate(t):   
    tot_loss = 0
    criterion = nn.CrossEntropyLoss()
    optimizer = optim.Adam(mod.parameters(), lr=0.001)
    
    ## Training 
    print(j,'th split-----------')
    for epochs in range(3):

        for i,data in enumerate(train_loader):
            inp,label = data
            inp,label = inp.to(device),label.to(device)
            optimizer.zero_grad()
            out = mod(inp)
            loss = criterion(out,label)
            loss.backward()
            optimizer.step()
            tot_loss += loss.item()
            if i%288 == 287 :
                print('epochs : {},batch : {} = loss : {}'.format(epochs,i+1,tot_loss/288))
                tot_loss = 0
        
    correct = 0
    tot = 0
    print('Train accuracy calculating..............')
    with torch.no_grad():
        for data in train_loader:
            inp,lab = data
            inp,lab = inp.to(device),lab.to(device)
            out = mod(inp)
            #print(out)
            #print(lab)
            for i in range(out.shape[0]):
                tot += 1
                if out[i].argmax() == lab[i]:
                    #print(out[i].argmax(),lab[i])
                    correct+=1
    print('Split - ',j)
    print('correct : ',correct)        
    print('total : ',tot)
    acc = correct/tot * 100
    train_acc.append(acc)
    print('train_accuracy : ',acc)
    
test_acc = []
correct = 0
tot = 0
print('Test accuracy calculating..............')
for j,val_loader in enumerate(v):    
    with torch.no_grad():
        for data in val_loader:
            inp,lab = data
            inp,lab = inp.to(device),lab.to(device)
            out = mod(inp)
            #print(out)
            #print(lab)
            for i in range(out.shape[0]):
                tot += 1
                if out[i].argmax() == lab[i]:
                    #print(out[i].argmax(),lab[i])
                    correct+=1
    print('Split - ',j)
    print('correct : ',correct)        
    print('total : ',tot)
    acc = correct/tot * 100
    test_acc.append(acc)
    print('test_accuracy : ',acc)
import statistics
print('final_train_acc = ',statistics.mean(train_acc))
print('final_test_acc = ',statistics.mean(test_acc))
torch.save(mod.state_dict(), '/content/weights_32_mod.pt')<jupyter_output><empty_output><jupyter_text>**Obj.localization**<jupyter_code>img = cv2.imread('/content/img.JPG')
#print(img)
img.shape
cen=[]
for i in range(8):
    for j in range(8):
        l=[]
        l.append(16+32*i)
        l.append(16+32*j)
        cen.append(l)
anchor = [[25,25]]
import torchvision.transforms as transforms
trans = transforms.Compose([
    transforms.ToPILImage(),
    transforms.Resize((32,32)),
    transforms.ToTensor(),
    transforms.Normalize(mean=[0.5,0.5,0.5], std=[0.5,0.5,0.5])
])
import matplotlib.pyplot as plt
defe = []
i=0
with torch.no_grad():
    for an in anchor:
        for c in cen:
            i = i+1
            stx = c[0]-an[0]//2
            edx = c[0]+an[0]//2
            sty = c[1]-an[1]//2
            edy = c[1]+an[1]//2
            tar = img[stx:edx,sty:edy]
            #print(tar.shape)
            tar = trans(tar)
            out = mod(tar.view(1,3,32,32).to(device))
            out
            if(out[0][0].item() >= 0.85):
                cv2.rectangle(img,(stx,sty),(edx,edy),(255,0,0),1)
                defe.append([an,c])
           
plt.imshow(img)
cv2.imwrite(os.path.join('/content','img1.jpg'),img)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16149575440888309, 'fraction_numerical': 0.03625081645983018, 'mean_word_length': 2.387721238938053, 'pattern_counts': {'":': 0, '<': 9, '<?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': '45366', 'license': 'MIT', 'n_tokens_mistral': 2301, 'n_tokens_neox': 2188, 'n_words': 388, 'path': 'Models/Classifier.ipynb', 'repo_name': 'vkk1710/SOS---Leaf-Localization', 'size': '6124'}