<jupyter_start><jupyter_text>WorkFlow  Classes  Load the data  Test Modelling  Modelling ****  Classes<jupyter_code>NAME = "change the conv2d"
BATCH_SIZE = 32
import os
import cv2
import torch
import numpy as np
def load_data(img_size=112):
    data = []
    index = -1
    labels = {}
    for directory in os.listdir('./data/'):
        index += 1
        labels[f'./data/{directory}/'] = [index,-1]
    print(len(labels))
    for label in labels:
        for file in os.listdir(label):
            filepath = label + file
            img = cv2.imread(filepath,cv2.IMREAD_GRAYSCALE)
            img = cv2.resize(img,(img_size,img_size))
            img = img / 255.0
            data.append([
                np.array(img),
                labels[label][0]
            ])
            labels[label][1] += 1
    for _ in range(12):
        np.random.shuffle(data)
    print(len(data))
    np.save('./data.npy',data)
    return data
import torch
def other_loading_data_proccess(data):
    X = []
    y = []
    print('going through the data..')
    for d in data:
        X.append(d[0])
        y.append(d[1])
    print('splitting the data')
    VAL_SPLIT = 0.25
    VAL_SPLIT = len(X)*VAL_SPLIT
    VAL_SPLIT = int(VAL_SPLIT)
    X_train = X[:-VAL_SPLIT]
    y_train = y[:-VAL_SPLIT]
    X_test = X[-VAL_SPLIT:]
    y_test = y[-VAL_SPLIT:]
    print('turning data to tensors')
    X_train = torch.from_numpy(np.array(X_train))
    y_train = torch.from_numpy(np.array(y_train))
    X_test = torch.from_numpy(np.array(X_test))
    y_test = torch.from_numpy(np.array(y_test))
    return [X_train,X_test,y_train,y_test]<jupyter_output><empty_output><jupyter_text>****  Load the data<jupyter_code>REBUILD_DATA = True
if REBUILD_DATA:
    data = load_data()
    np.random.shuffle(data)
    X_train,X_test,y_train,y_test = other_loading_data_proccess(data)<jupyter_output>36
2515<jupyter_text>Test Modelling<jupyter_code>import torch
import torch.nn as nn
import torch.nn.functional as F
# class Test_Model(nn.Module):
#     def __init__(self):
#         super().__init__()
#         self.conv1 = nn.Conv2d(1, 6, 5)
#         self.pool = nn.MaxPool2d(2, 2)
#         self.conv2 = nn.Conv2d(6, 16, 5)
#         self.fc1 = nn.Linear(16 * 25 * 25, 120)
#         self.fc2 = nn.Linear(120, 84)
#         self.fc3 = nn.Linear(84, 36)

#     def forward(self, x):
#         x = self.pool(F.relu(self.conv1(x)))
#         x = self.pool(F.relu(self.conv2(x)))
#         x = x.view(-1, 16 * 25 * 25)
#         x = F.relu(self.fc1(x))
#         x = F.relu(self.fc2(x))
#         x = self.fc3(x)
#         return x
class Test_Model(nn.Module):
    def __init__(self):
        super().__init__()
        self.pool = nn.MaxPool2d(2, 2)
        self.conv1 = nn.Conv2d(1, 32, 5)
        self.conv3 = nn.Conv2d(32,64,5)
        self.conv2 = nn.Conv2d(64, 128, 5)
        self.fc1 = nn.Linear(128 * 10 * 10, 512)
        self.fc2 = nn.Linear(512, 256)
        self.fc4 = nn.Linear(256,128)
        self.fc3 = nn.Linear(128, 36)

    def forward(self, x,shape=False):
        x = self.pool(F.relu(self.conv1(x)))
        x = self.pool(F.relu(self.conv3(x)))
        x = self.pool(F.relu(self.conv2(x)))
        if shape:
            print(x.shape)
        x = x.view(-1, 128 * 10 * 10)
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = F.relu(self.fc4(x))
        x = self.fc3(x)
        return x
device = torch.device('cuda')
model = Test_Model().to(device)
preds = model(X_test.reshape(-1,1,112,112).float().to(device),True)
preds[0]
optimizer = torch.optim.SGD(model.parameters(),lr=0.1)
criterion = nn.CrossEntropyLoss()
EPOCHS = 5
loss_logs = []
from tqdm import tqdm
PROJECT_NAME = "Sign-Language-Recognition"
def test(net,X,y):
    correct = 0
    total = 0
    net.eval()
    with torch.no_grad():
        for i in range(len(X)):
            real_class = torch.argmax(y[i]).to(device)
            net_out = net(X[i].view(-1,1,112,112).to(device).float())
            net_out = net_out[0]
            predictied_class = torch.argmax(net_out)
            if predictied_class == real_class:
                correct += 1
            total += 1
    return round(correct/total,3)
import wandb
len(os.listdir('./data/'))
import random
# index = random.randint(0,29)
# print(index)
# wandb.init(project=PROJECT_NAME,name=NAME)
# for _ in tqdm(range(EPOCHS)):
#     for i in range(0,len(X_train),BATCH_SIZE):
#         X_batch = X_train[i:i+BATCH_SIZE].view(-1,1,112,112).to(device)
#         y_batch = y_train[i:i+BATCH_SIZE].to(device)
#         model.to(device)
#         preds = model(X_batch.float())
#         loss = criterion(preds,torch.tensor(y_batch,dtype=torch.long))
#         optimizer.zero_grad()
#         loss.backward()
#         optimizer.step()
#         wandb.log({'loss':loss.item(),'accuracy':test(model,X_train,y_train)*100,'val_accuracy':test(model,X_test,y_test)*100,'pred':torch.argmax(preds[index]),'real':torch.argmax(y_batch[index])})
# wandb.finish()
import matplotlib.pyplot as plt
import pandas as pd
df = pd.Series(loss_logs)
df.plot.line(figsize=(12,6))
test(model,X_test,y_test)
test(model,X_train,y_train)
preds
X_testing = X_train
y_testing = y_train
correct = 0
total = 0
model.eval()
with torch.no_grad():
    for i in range(len(X_testing)):
        real_class = torch.argmax(y_testing[i]).to(device)
        net_out = model(X_testing[i].view(-1,1,112,112).to(device).float())
        net_out = net_out[0]
        predictied_class = torch.argmax(net_out)
#         print(predictied_class)
        if str(predictied_class) == str(real_class):
            correct += 1
        total += 1
print(round(correct/total,3))
# for real,pred in zip(y_batch,preds):
#     print(real)
#     print(torch.argmax(pred))
#     print('\n')<jupyter_output><empty_output><jupyter_text>Modelling<jupyter_code># conv2d_output
# conv2d_1_ouput
# conv2d_2_ouput
# output_fc1
# output_fc2
# output_fc4
# max_pool2d_keranl
# max_pool2d
# num_of_linear

# activation
# best num of epochs
# best optimizer
# best loss
## best lr
class Test_Model(nn.Module):
    def __init__(self,conv2d_output=128,conv2d_1_ouput=32,conv2d_2_ouput=64,output_fc1=512,output_fc2=256,output_fc4=128,output=36,activation=F.relu,max_pool2d_keranl=2):
        super().__init__()
        print(conv2d_output)
        print(conv2d_1_ouput)
        print(conv2d_2_ouput)
        print(output_fc1)
        print(output_fc2)
        print(output_fc4)
        print(activation)
        self.conv2d_output = conv2d_output
        self.pool = nn.MaxPool2d(max_pool2d_keranl)
        self.conv1 = nn.Conv2d(1, conv2d_1_ouput, 5)
        self.conv3 = nn.Conv2d(conv2d_1_ouput,conv2d_2_ouput,5)
        self.conv2 = nn.Conv2d(conv2d_2_ouput, conv2d_output, 5)
        self.fc1 = nn.Linear(conv2d_output * 10 * 10, output_fc1)
        self.fc2 = nn.Linear(output_fc1, output_fc2)
        self.fc4 = nn.Linear(output_fc2,output_fc4)
        self.fc3 = nn.Linear(output_fc4, output)
        self.activation = activation

    def forward(self, x,shape=False):
        x = self.pool(self.activation(self.conv1(x)))
        x = self.pool(self.activation(self.conv3(x)))
        x = self.pool(self.activation(self.conv2(x)))
        if shape:
            print(x.shape)
        x = x.view(-1, self.conv2d_output * 10 * 10)
        x = self.activation(self.fc1(x))
        x = self.activation(self.fc2(x))
        x = self.activation(self.fc4(x))
        x = self.fc3(x)
        return x
# conv2d_output
# conv2d_1_ouput
# conv2d_2_ouput
# output_fc1
# output_fc2
# output_fc4
# max_pool2d_keranl
# max_pool2d
# num_of_linear
# best num of epochs
# best loss
## best lr
# batch size
EPOCHS = 3
BATCH_SIZE = 32
# conv2d_output
# conv2d_1_ouput
# conv2d_2_ouput
# output_fc1
# output_fc2
# output_fc4
# max_pool2d_keranl
# max_pool2d
# num_of_linear

# activation = 
# best num of epochs
# best optimizer = 
# best loss
## best lr
def get_loss(criterion,y,model,X):
    preds = model(X.view(-1,1,112,112).to(device).float())
    preds.to(device)
    loss = criterion(preds,torch.tensor(y,dtype=torch.long).to(device))
    loss.backward()
    return loss.item()
optimizers = [torch.optim.SGD,torch.optim.Adadelta,torch.optim.Adagrad,torch.optim.Adam,torch.optim.AdamW,torch.optim.SparseAdam,torch.optim.Adamax]
for optimizer in optimizers:
    model = Test_Model(activation=nn.ReLU())
    criterion = optimizer(model.parameters(),lr=0.1)
    wandb.init(project=PROJECT_NAME,name=f'optimizer-{optimizer}')
    for _ in tqdm(range(EPOCHS)):
        for i in range(0,len(X_train),BATCH_SIZE):
            X_batch = X_train[i:i+BATCH_SIZE]
            y_batch = y_train[i:i+BATCH_SIZE]
            model.to(device)
            preds = model(X_batch.float())
            loss = criterion(preds,torch.tensor(y_batch,dtype=torch.long))
            optimizer.zero_grad()
            loss.backward()
            optimizer.step()
            wandb.log({'loss':loss.item(),'accuracy':test(model,X_train,y_train)*100,'val_accuracy':test(model,X_test,y_test)*100,'pred':torch.argmax(preds[index]),'real':torch.argmax(y_batch[index]),'val_loss':get_loss(criterion,y_test,model,X_test)})
        print(f'{torch.argmax(preds[index])} \n {y_batch[index]}')
        print(f'{torch.argmax(preds[1])} \n {y_batch[1]}')
        print(f'{torch.argmax(preds[2])} \n {y_batch[2]}')
        print(f'{torch.argmax(preds[3])} \n {y_batch[3]}')
        print(f'{torch.argmax(preds[4])} \n {y_batch[4]}')
    wandb.finish()
# activations = [nn.ELU(),nn.LeakyReLU(),nn.PReLU(),nn.ReLU(),nn.ReLU6(),nn.RReLU(),nn.SELU(),nn.CELU(),nn.GELU(),nn.SiLU(),nn.Tanh()]
# for activation in activations:
#     model = Test_Model(activation=activation)
#     optimizer = torch.optim.SGD(model.parameters(),lr=0.1)
#     criterion = nn.CrossEntropyLoss()
#     index = random.randint(0,29)
#     print(index)
#     wandb.init(project=PROJECT_NAME,name=f'activation-{activation}')
#     for _ in tqdm(range(EPOCHS)):
#         for i in range(0,len(X_train),BATCH_SIZE):
#             X_batch = X_train[i:i+BATCH_SIZE].view(-1,1,112,112).to(device)
#             y_batch = y_train[i:i+BATCH_SIZE].to(device)
#             model.to(device)
#             preds = model(X_batch.float())
#             loss = criterion(preds,torch.tensor(y_batch,dtype=torch.long))
#             optimizer.zero_grad()
#             loss.backward()
#             optimizer.step()
#             wandb.log({'loss':loss.item(),'accuracy':test(model,X_train,y_train)*100,'val_accuracy':test(model,X_test,y_test)*100,'pred':torch.argmax(preds[index]),'real':torch.argmax(y_batch[index]),'val_loss':get_loss(criterion,y_test,model,X_test)})
#         print(f'{torch.argmax(preds[index])} \n {y_batch[index]}')
#         print(f'{torch.argmax(preds[1])} \n {y_batch[1]}')
#         print(f'{torch.argmax(preds[2])} \n {y_batch[2]}')
#         print(f'{torch.argmax(preds[3])} \n {y_batch[3]}')
#         print(f'{torch.argmax(preds[4])} \n {y_batch[4]}')
#     wandb.finish()
for real,pred in zip(y_batch,preds):
    print(real)
    print(torch.argmax(pred))
    print('\n')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15654289834174479, 'fraction_numerical': 0.03469718817591925, 'mean_word_length': 3.11, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 16, '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': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '3283', 'license': 'Apache-2.0', 'n_tokens_mistral': 4526, 'n_tokens_neox': 4359, 'n_words': 624, 'path': 'wandb/run-20210518_105803-3lpt85em/tmp/code/main.ipynb', 'repo_name': 'Programmer-RD-AI/Sign-Language-Recognition', 'size': '11096'}