text_plain_output_1.png
from torch.utils.data import Dataset, DataLoader, ConcatDataset

from torchvision.transforms import transforms

import cv2

import cv2

import matplotlib.pyplot as plt

import numpy as np

import numpy as np

import numpy as np # linear algebra

import os

import os

import pandas as pd

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

import torch

import torch.nn as nn

import torch.nn.functional as f

import torch.optim as optim

import torchvision

import numpy as np
import pandas as pd
import os

data_lebel = pd.read_csv('/kaggle/input/soft-computing-even-id-dataset/training-a.csv')

img = cv2.imread('/kaggle/input/soft-computing-even-id-dataset/training-a/a00000.png')

img.shape

IMAGE_SIZE = 40
transform = transforms.Compose([transforms.ToPILImage(), torchvision.transforms.ColorJitter(brightness=0.4, saturation=0.4, contrast=0.4, hue=0.4), transforms.RandomRotation(20, expand=True), transforms.Resize(IMAGE_SIZE), transforms.CenterCrop(IMAGE_SIZE), transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])

class digit_Dataset(Dataset):
    def __init__(self, csv_file, root_dir, transform=True):
        self.annotations = pd.read_csv(csv_file)
        self.root_dir = root_dir
        self.transform = transform
    def __len__(self):
        return len(self.annotations)
    def __getitem__(self, index):
        img_path = os.path.join(self.root_dir, self.annotations.iloc[index, 0])
        image = cv2.imread(img_path)
        y_label = torch.tensor(int(self.annotations.iloc[index, 3]))
        if self.transform:
            image = self.transform(image)
        return (image, y_label)

dataset = digit_Dataset(csv_file='/kaggle/input/soft-computing-even-id-dataset/training-a.csv', root_dir='/kaggle/input/soft-computing-even-id-dataset/training-a', transform=transform)

train_set, test_set = torch.utils.data.random_split(dataset, [15702, 4000])
train_loader = DataLoader(dataset=train_set, batch_size=16, shuffle=True)
test_loader = DataLoader(dataset=test_set, batch_size=16, shuffle=True)

data_iter = iter(train_loader)
images, labels = data_iter.next()
images.shape

dataiter = iter(train_loader)
images, labels = dataiter.next()
for i in range(10):
    img = images[i].detach().numpy().transpose((1, 2, 0))
    mean = np.array([0.5, 0.5, 0.5])
    std = np.array([0.5, 0.5, 0.5])
    img = std * img + mean
    img = np.clip(img, 0, 1)

class Net(nn.Module):
    def __init__(self):
        super().__init__()
        self.fc1 = nn.Linear(3 * 40 * 40, 200)
        self.fc2 = nn.Linear(200, 200)
        self.fc3 = nn.Linear(200, 200)
        self.fc4 = nn.Linear(200, 200)
        self.fc5 = nn.Linear(200, 200)
        self.fc6 = nn.Linear(200, 10)
    def forward(self, x):
        x = f.relu(self.fc1(x))
        x = f.relu(self.fc2(x))
        x = f.relu(self.fc3(x))
        x = f.relu(self.fc4(x))
        x = f.relu(self.fc5(x))
        x = self.fc6(x)
        return x
net = Net()

net.cuda()

optimizer = optim.SGD(net.parameters(), lr=0.01, momentum=0.9)
criterion = torch.nn.CrossEntropyLoss()

device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')

Path = './net_final.pth'
traininglosses = []
trainingaccuracy = []
testinglosses = []
testaccuracy = []
totalsteps = []
epochs = 20
steps = 0
running_loss = 0
print_every = 5000
for epoch in range(epochs):
    accuracy = 0
    for inputs, labels in train_loader:
        net.train()
        steps += 1
        inputs, labels = (inputs.to(device), labels.to(device))
        optimizer.zero_grad()
        logps = net.forward(inputs.view(-1, 3 * 40 * 40))
        loss = criterion(logps, labels)
        loss.backward()
        optimizer.step()
        pred = torch.argmax(logps, dim=1)
        correct = pred.eq(labels)
        running_loss += loss.item()
        accuracy += torch.mean(correct.float())
        if steps % print_every == 0:
            after_train_accuracy = accuracy / print_every
            test_loss = 0
            accuracy = 0
            net.eval()
            with torch.no_grad():
                for inputs, labels in test_loader:
                    inputs, labels = (inputs.to(device), labels.to(device))
                    logps = net.forward(inputs.view(-1, 3 * 40 * 40))
                    batch_loss = criterion(logps, labels)
                    test_loss += batch_loss.item()
                    pred = torch.argmax(logps, dim=1)
                    correct = pred.eq(labels)
                    accuracy += torch.mean(correct.float())
            traininglosses.append(running_loss / print_every)
            trainingaccuracy.append(after_train_accuracy)
            testinglosses.append(test_loss / len(test_loader))
            testaccuracy.append(accuracy / len(test_loader))
            totalsteps.append(steps)
            running_loss = 0
            accuracy = 0
            net.train()
torch.save(net.state_dict(), Path)

plt.figure(figsize=(50, 10))
plt.plot(totalsteps, traininglosses, label='Train Loss')
plt.plot(totalsteps, trainingaccuracy, label='Test Accuracy')
plt.plot(totalsteps, testinglosses, label='Test Loss')
plt.plot(totalsteps, testaccuracy, label='Test Accuracy')
plt.legend()
plt.grid()
plt.show()