text_plain_output_5.png text_plain_output_15.png text_plain_output_9.png text_plain_output_20.png text_plain_output_4.png text_plain_output_13.png text_plain_output_14.png text_plain_output_10.png text_plain_output_6.png text_plain_output_18.png text_plain_output_3.png text_plain_output_7.png text_plain_output_16.png text_plain_output_8.png text_plain_output_2.png text_plain_output_1.png text_plain_output_19.png text_plain_output_17.png text_plain_output_11.png text_plain_output_12.png
from PIL import Image

from torch.utils.data import DataLoader, Dataset, random_split

from tqdm import tqdm_notebook

import matplotlib.pyplot as plt

import matplotlib.pyplot as plt

import numpy as np

import numpy as np # linear algebra

import os

import os

import torch

import torch

import torch.nn as nn

import torch.nn as nn

import torch.optim as optim

import torch.optim as optim

import torchvision.models as models

import torchvision.transforms as transforms

import torchvision.transforms as transforms

import numpy as np
import pandas as pd
import os

import os
from PIL import Image
import matplotlib.pyplot as plt
import torch
import torchvision
from torch.utils.data import DataLoader, Dataset, random_split
import torchvision.transforms as transforms
class VowelConsonantDataset(Dataset):
    def __init__(self, file_path, train=True, transform=None):
        self.transform = transform
        self.file_path = file_path
        self.train = train
        self.file_names = [file for _, _, files in os.walk(self.file_path) for file in files]
        self.len = len(self.file_names)
        if self.train:
            self.classes_mapping = self.get_classes()
    def __len__(self):
        return len(self.file_names)
    def __getitem__(self, index):
        file_name = self.file_names[index]
        image_data = self.pil_loader(self.file_path + '/' + file_name)
        if self.transform:
            image_data = self.transform(image_data)
        if self.train:
            file_name_splitted = file_name.split('_')
            Y1 = self.classes_mapping[file_name_splitted[0]]
            Y2 = self.classes_mapping[file_name_splitted[1]]
            z1, z2 = (torch.zeros(10), torch.zeros(10))
            z1[Y1 - 10], z2[Y2] = (1, 1)
            label = torch.stack([z1, z2])
            return (image_data, label)
        else:
            return (image_data, file_name)
    def pil_loader(self, path):
        with open(path, 'rb') as f:
            img = Image.open(f)
            return img.convert('RGB')
    def get_classes(self):
        classes = []
        for name in self.file_names:
            name_splitted = name.split('_')
            classes.extend([name_splitted[0], name_splitted[1]])
        classes = list(set(classes))
        classes_mapping = {}
        for i, cl in enumerate(sorted(classes)):
            classes_mapping[cl] = i
        return classes_mapping

import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import torchvision
import matplotlib.pyplot as plt
from torchvision import datasets
import torchvision.transforms as transforms
import numpy as np
import pandas as pd
train_on_gpu = torch.cuda.is_available()

transform = transforms.Compose([transforms.ToTensor()])

full_data = VowelConsonantDataset('../input/train/train', train=True, transform=transform)
train_size = int(0.9 * len(full_data))
test_size = len(full_data) - train_size
train_data, validation_data = random_split(full_data, [train_size, test_size])
train_loader = torch.utils.data.DataLoader(train_data, batch_size=64, shuffle=True)
validation_loader = torch.utils.data.DataLoader(validation_data, batch_size=60, shuffle=True)

test_data = VowelConsonantDataset('../input/test/test', train=False, transform=transform)
test_loader = torch.utils.data.DataLoader(test_data, batch_size=60, shuffle=False)

for data in train_loader:
    img, lab = data
    im = np.transpose(img[0].numpy(), (1, 2, 0))
    im = np.squeeze(im)
    break

model_v = models.resnet18()
model_c = models.resnet18()

model_c.fc = nn.Linear(512, 10, bias=True)
model_v.fc = nn.Linear(512, 10, bias=True)

opt_v = optim.Adam(model_v.parameters())
opt_c = optim.Adam(model_c.parameters())
loss_fn_v = nn.CrossEntropyLoss()
loss_fn_c = nn.CrossEntropyLoss()

max_epochs = 20
loss_arr = []
st = 'cuda:0'
model_v.to(st)
model_c.to(st)
for i in tqdm_notebook(range(max_epochs), total=max_epochs, unit='epochs'):
    for data in tqdm_notebook(train_loader, total=len(train_loader), unit='batch'):
        img, lab = data
        img, lab = (img.to(st), lab.to(st))
        out_v = model_v(img)
        out_c = model_c(img)
        opt_v.zero_grad()
        opt_c.zero_grad()
        val, ind = torch.max(lab[:, 0, :], 1)
        val, ind1 = torch.max(lab[:, 1, :], 1)
        lab_v = ind
        lab_c = ind1
        loss = loss_fn_v(out_v, lab_v) + loss_fn_c(out_c, lab_c)
        loss.backward()
        opt_v.step()
        opt_c.step()
        del img, lab
    loss_arr.append(loss)

torch.max(lab[:, 0, :]).int()

def evaluation(dataloader, model):
    total = 0
    v = 0
    c = 0
    for data in tqdm_notebook(dataloader, total=len(dataloader), unit='batch'):
        img, lab = data
        _, out_v = torch.max(model.VowForward(img), 1)
        _, out_c = torch.max(model.ConForward(img), 1)
        _, lab1 = torch.max(lab[:, 0, :], 1)
        _, lab2 = torch.max(lab[:, 1, :], 1)
        total += 64
        v += (out_v == lab1).sum().item()
        c += (out_c == lab2).sum().item()
    print(total, v, c)
    print(v / total, c / total)
evaluation(train_loader, model)