Advertisement
Mihai_Preda

Untitled

May 15th, 2021
322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.86 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, models
  8. import matplotlib.pyplot as plt
  9. from collections import OrderedDict
  10. from tqdm import tqdm
  11. import numpy as np
  12.  
  13. # %%
  14.  
  15. train_transformation = transforms.Compose([
  16.     transforms.ToTensor(),
  17.     transforms.Resize(224),
  18.     transforms.RandomRotation(10),
  19. ])
  20.  
  21. train_dataset = datasets.ImageFolder (
  22.     root="C:\\Users\\preda\\Desktop\\Github\\Facultate\\Anul 2\\IA\\train_folder",
  23.     transform=train_transformation,
  24. )
  25.  
  26. train_loader = th.utils.data.DataLoader(
  27.     train_dataset, batch_size=32, shuffle=True
  28. )
  29.  
  30. validation_transformation = transforms.Compose([
  31.     transforms.ToTensor(),
  32.     transforms.Resize(224),
  33. ])
  34.  
  35. validation_dataset = datasets.ImageFolder (
  36.     root="C:\\Users\\preda\\Desktop\\Github\\Facultate\\Anul 2\\IA\\validation_folder",
  37.     transform=validation_transformation,
  38. )
  39.  
  40. validation_loader = th.utils.data.DataLoader(
  41.     validation_dataset, batch_size=32, shuffle=True
  42. )
  43.  
  44. # %%
  45. class Network(nn.Module):
  46.     def __init__(self):
  47.         super().__init__()
  48.  
  49.         self.net = nn.Sequential(
  50.             nn.Conv2d(3, 16, 3, padding=1),
  51.             nn.ReLU(),
  52.             nn.BatchNorm2d(16),
  53.             nn.MaxPool2d(2),
  54.             nn.Conv2d(16, 32, 3, padding=1),
  55.             nn.ReLU(),
  56.             nn.BatchNorm2d(32),
  57.             nn.MaxPool2d(2),
  58.  
  59.             nn.Flatten(),
  60.  
  61.             nn.Linear(32 * 50 * 50, 1000),
  62.             nn.ReLU(),
  63.             nn.Dropout(0.7),
  64.             nn.Linear(1000, 3),
  65.             # nn.ReLU(),
  66.             # nn.Dropout(0.5),
  67.             # nn.Linear(200, 3),
  68.         )
  69.  
  70.     def forward(self, x):
  71.         result = self.net(x)
  72.         return result
  73.          
  74. # %%
  75.  
  76. def train_one_epoch():
  77.     net.train()
  78.  
  79.     hit = 0
  80.     total = 0
  81.     avg_loss = 0.
  82.     for images, labels in tqdm(train_loader):
  83.         images = images.cuda()
  84.         labels = labels.cuda()
  85.        
  86.         optimizer.zero_grad()
  87.         predictions = net.forward(images)
  88.         loss = criterion(predictions, labels)
  89.         avg_loss += loss.item()
  90.  
  91.         loss.backward()
  92.         optimizer.step()
  93.  
  94.         hit += th.sum(labels == predictions.argmax(dim=1))
  95.         total += len(images)
  96.    
  97.     return hit / total, avg_loss / len(train_loader)
  98.  
  99. def test_data(data_loader):
  100.     net.eval()
  101.  
  102.     with th.no_grad():
  103.         hit = 0
  104.         total = 0
  105.         avg_loss = 0.
  106.         for images, labels in data_loader:
  107.             images = images.cuda()
  108.             labels = labels.cuda()
  109.  
  110.             predictions = net.forward(images)
  111.  
  112.             loss = criterion(predictions, labels)
  113.             avg_loss += loss.item()
  114.             hit += th.sum(labels == predictions.argmax(dim=1))
  115.             total += len(images)
  116.  
  117.         return (hit / total, avg_loss / len(data_loader))
  118.  
  119. def print_confussion_matrix(data_loader):
  120.     ans = [[0 for i in range(3)] for j in range(3)]
  121.     with th.no_grad():
  122.         for images, labels in tqdm(data_loader):
  123.             images = images.cuda()
  124.             labels = labels.cuda()
  125.  
  126.             predictions = net(images)
  127.             prediction = predictions.argmax(dim=1)
  128.             for i in range(len(labels)):
  129.                 ans[labels[i].item()][prediction[i].item()] += 1
  130.    
  131.     plt.imshow(ans, cmap='gray')
  132.     print(np.array(ans))
  133.  
  134.  
  135. # %%
  136.  
  137. # definim datele
  138. vgg = models.alexnet(pretrained=True).cuda()
  139. # for param in vgg.parameters():
  140. #     param.requires_grad = False
  141. vgg.classifier = nn.Sequential(
  142.                             nn.Dropout(p=0.5, inplace=False),
  143.                             nn.Linear(in_features=9216, out_features=1700, bias=True),
  144.                             nn.ReLU(inplace=True),
  145.                             # nn.Dropout(p=0.5, inplace=False),
  146.                             # nn.Linear(in_features=4096, out_features=4096, bias=True),
  147.                             # nn.ReLU(inplace=True),
  148.                             nn.Linear(in_features=1700, out_features=3, bias=True)
  149.                           ).cuda()
  150.  
  151. net = vgg
  152. criterion = nn.CrossEntropyLoss().cuda()
  153. loss_hist = []
  154. validation_loss_hist = []
  155.  
  156. # %%
  157.  
  158. optimizer = th.optim.Adam(net.parameters(), lr=1e-5)
  159.  
  160. for epoch in range(100000):
  161.     accuracy, loss = train_one_epoch()
  162.     print(f"Loss: {loss}")
  163.     validation_accuracy, validation_loss = test_data(validation_loader)
  164.     loss_hist.append(loss)
  165.     validation_loss_hist.append(validation_loss)
  166.     epoch = len(loss_hist)
  167.     print(f"Epoch: {epoch}")
  168.     print(f"Train: loss: {loss}, accuracy: {accuracy}")
  169.     print(f"Validation: loss: {validation_loss}, accuracy: {validation_accuracy}")
  170.    
  171.     # min_epoch = 2
  172.     # if(epoch > min_epoch):
  173.     #     plt.plot(loss_hist[min_epoch:], color='blue')
  174.     #     plt.plot(validation_loss_hist[min_epoch:], color='red')
  175.     #     plt.show()
  176.    
  177. # %%
  178.  
  179. test_dataset = datasets.ImageFolder (
  180.     root="C:\\Users\\preda\\Desktop\\Github\\Facultate\\Anul 2\\IA\\test_folder",
  181.     transform=validation_transformation,
  182. )
  183.  
  184. test_loader = th.utils.data.DataLoader(
  185.     test_dataset, batch_size=1, shuffle=False
  186. )
  187.  
  188. # %%
  189.  
  190. def print_test():
  191.     tests = []
  192.     for file in os.listdir("C:\\Users\\preda\\Desktop\\Github\\Facultate\\Anul 2\\IA\\test_folder\\test"):
  193.         tests.append(file)
  194.     lastFile = 0
  195.     net.eval()
  196.  
  197.     f = open("C:\\Users\\preda\\Desktop\\Github\\Facultate\\Anul 2\\IA\\solution.txt", "w")
  198.     f.write("id,label\n")
  199.  
  200.     with th.no_grad():
  201.         for images, labels in tqdm(test_loader):
  202.             images = images.cuda()
  203.             labels = labels.cuda()
  204.  
  205.             predictions = net(images)
  206.             prediction = predictions.argmax(dim=1)
  207.             f.write(tests[lastFile] + "," + str(prediction.item()) + "\n")
  208.  
  209.             lastFile += 1
  210.  
  211.     f.close()
  212.  
  213. # %%
  214.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement