Advertisement
Mihai_Preda

Untitled

Apr 29th, 2021
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.39 KB | None | 0 0
  1. # %%
  2. import torch as th
  3. import torch.nn as nn
  4. import pandas as pd
  5. from os import mkdir
  6. from shutil import copyfile, copytree
  7. from torchvision import datasets, transforms
  8. import matplotlib.pyplot as plt
  9. from tqdm import tqdm
  10. import numpy as np
  11.  
  12. # %%
  13.  
  14. train_transformation = transforms.Compose([
  15.     transforms.ToTensor(),
  16.     transforms.RandomCrop(50, 4),
  17. ])
  18.  
  19. train_dataset = datasets.ImageFolder (
  20.     root="C:\\Users\\preda\\Desktop\\Github\\Facultate\\Anul 2\\IA\\train_folder",
  21.     transform=train_transformation,
  22. )
  23.  
  24. train_loader = th.utils.data.DataLoader(
  25.     train_dataset, batch_size=64, shuffle=True
  26. )
  27.  
  28. validation_transformation = transforms.Compose([
  29.     transforms.ToTensor(),
  30. ])
  31.  
  32. validation_dataset = datasets.ImageFolder (
  33.     root="C:\\Users\\preda\\Desktop\\Github\\Facultate\\Anul 2\\IA\\validation_folder",
  34.     transform=validation_transformation,
  35. )
  36.  
  37. validation_loader = th.utils.data.DataLoader(
  38.     validation_dataset, batch_size=64, shuffle=True
  39. )
  40.  
  41. # %%
  42.  
  43. class Network(nn.Module):
  44.     def __init__(self):
  45.         super().__init__()
  46.  
  47.         self.net = nn.Sequential(
  48.             nn.Conv2d(3, 32, 3, padding=1),
  49.             nn.ReLU(),
  50.             nn.BatchNorm2d(32),
  51.             nn.MaxPool2d(2),
  52.             nn.Conv2d(32, 64, 3, padding=1),
  53.             nn.ReLU(),
  54.             nn.BatchNorm2d(64),
  55.             nn.MaxPool2d(2),
  56.  
  57.             nn.Flatten(),
  58.  
  59.             nn.Linear(64 * 12 * 12, 1000),
  60.             nn.ReLU(),
  61.             nn.Dropout(0.3),
  62.             nn.Linear(1000, 200),
  63.             nn.ReLU(),
  64.             nn.Dropout(0.3),
  65.             nn.Linear(200, 3),
  66.         )
  67.  
  68.     def forward(self, x):
  69.         return self.net(x)
  70.          
  71. # %%
  72.  
  73. def train_one_epoch():
  74.     net.train()
  75.  
  76.     avg_loss = 0.
  77.     for images, labels in tqdm(train_loader):
  78.         images = images.cuda()
  79.         labels = labels.cuda()
  80.  
  81.         predictions = net(images)
  82.         loss = criterion(predictions, labels)
  83.         avg_loss += loss.item()
  84.         loss.backward()
  85.         optimizer.step()
  86.     return avg_loss / len(train_loader)
  87.  
  88. def test_data(data_loader):
  89.     net.eval()
  90.  
  91.     with th.no_grad():
  92.         hit = 0
  93.         total = 0
  94.         avg_loss = 0.
  95.         for images, labels in tqdm(data_loader):
  96.             images = images.cuda()
  97.             labels = labels.cuda()
  98.  
  99.             predictions = net(images)
  100.             loss = criterion(predictions, labels)
  101.             avg_loss += loss.item()
  102.             hit += th.sum(labels == predictions.argmax(dim=1))
  103.             total += len(images)
  104.  
  105.         return (hit / total, avg_loss / len(data_loader))
  106.  
  107. def print_confussion_matrix(data_loader):
  108.     ans = [[0 for i in range(3)] for j in range(3)]
  109.     with th.no_grad():
  110.         for images, labels in tqdm(data_loader):
  111.             images = images.cuda()
  112.             labels = labels.cuda()
  113.  
  114.             predictions = net(images)
  115.             prediction = predictions.argmax(dim=1)
  116.             for i in range(len(labels)):
  117.                 ans[labels[i].item()][prediction[i].item()] += 1
  118.    
  119.     plt.imshow(ans, cmap='gray')
  120.     print(np.array(ans))
  121.  
  122.  
  123. # %%
  124.  
  125. # definim datele
  126. net = Network().cuda()
  127. criterion = nn.CrossEntropyLoss().cuda()
  128. loss_hist = []
  129.  
  130. # %%
  131.  
  132. optimizer = th.optim.Adam(net.parameters(), lr=1e-5)
  133.  
  134. for epoch in range(20):
  135.     loss = train_one_epoch()
  136.     print(f"Loss: {loss}")
  137.     print(f"Accuracy: {test_data(validation_loader)}")
  138.     loss_hist.append(loss)
  139.    
  140. # %%
  141.  
  142. test_dataset = datasets.ImageFolder (
  143.     root="C:\\Users\\preda\\Desktop\\Github\\Facultate\\Anul 2\\IA\\test_folder",
  144.     transform=validation_transformation,
  145. )
  146.  
  147. test_loader = th.utils.data.DataLoader(
  148.     test_dataset, batch_size=1, shuffle=False
  149. )
  150.  
  151. # %%
  152.  
  153. def print_test():
  154.     tests = []
  155.     for file in os.listdir("C:\\Users\\preda\\Desktop\\Github\\Facultate\\Anul 2\\IA\\test_folder\\test"):
  156.         tests.append(file)
  157.     lastFile = 0
  158.     net.eval()
  159.  
  160.     f = open("C:\\Users\\preda\\Desktop\\Github\\Facultate\\Anul 2\\IA\\solution.txt", "w")
  161.     f.write("id,label\n")
  162.  
  163.     with th.no_grad():
  164.         for images, labels in tqdm(test_loader):
  165.             images = images.cuda()
  166.             labels = labels.cuda()
  167.  
  168.             predictions = net(images)
  169.             prediction = predictions.argmax(dim=1)
  170.             f.write(tests[lastFile] + "," + str(prediction.item()) + "\n")
  171.  
  172.             lastFile += 1
  173.  
  174.     f.close()
  175.  
  176. # %%
  177.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement