garbage_recognition #27
29
NeuralNetwork/NeuralNetwork.py
Normal file
@ -0,0 +1,29 @@
|
||||
import torch.nn as nn
|
||||
import torch.nn.functional as F
|
||||
|
||||
class NeuralNetwork(nn.Module):
|
||||
def __init__(self):
|
||||
super(NeuralNetwork, self).__init__()
|
||||
|
||||
# Warstwy konwolucyjnej sieci neuronowej, filtr 5×5, 3 kanały dla RGB
|
||||
self.convolutional_nn_1 = nn.Conv2d(3, 6, 5)
|
||||
self.convolutional_nn_2 = nn.Conv2d(6, 16, 5)
|
||||
|
||||
# Wyciaganie "najwazniejszej" informacji z obrazu
|
||||
self.pool = nn.MaxPool2d(2, 2)
|
||||
|
||||
self.full_connection_layer_1 = nn.Linear(16 * 71 * 71, 120)
|
||||
self.full_connection_layer_2 = nn.Linear(120, 84)
|
||||
self.full_connection_layer_3 = nn.Linear(84, 4)
|
||||
|
||||
# Forward określa przepływ inputu przez warstwy
|
||||
def forward(self, x):
|
||||
x = self.pool(F.relu(self.convolutional_nn_1(x)))
|
||||
x = self.pool(F.relu(self.convolutional_nn_2(x)))
|
||||
|
||||
# 16 kanałów o rozmiarach 71x71
|
||||
x = x.view(x.size(0), 16 * 71 * 71)
|
||||
x = F.relu(self.full_connection_layer_1(x))
|
||||
x = F.relu(self.full_connection_layer_2(x))
|
||||
x = self.full_connection_layer_3(x)
|
||||
return x
|
20
NeuralNetwork/prediction.py
Normal file
@ -0,0 +1,20 @@
|
||||
import torch
|
||||
import torchvision.transforms as transforms
|
||||
|
||||
from PIL import Image
|
||||
from NeuralNetwork import NeuralNetwork
|
||||
|
||||
def getPrediction(img_path):
|
||||
|
||||
# Inicjacja sieci neuronowej
|
||||
neural_net = NeuralNetwork()
|
||||
PATH = './trained_nn.pth'
|
||||
img = Image.open(img_path)
|
||||
transform_tensor = transforms.ToTensor()(img).unsqueeze_(0)
|
||||
classes = ['glass', 'metal', 'paper', 'plastic']
|
||||
neural_net.load_state_dict(torch.load(PATH))
|
||||
neural_net.eval()
|
||||
outputs = neural_net(transform_tensor)
|
||||
|
||||
# Wyciągnięcie największej wagi co przekłada się na rozpoznanie klasy, w tym przypadku rodzju odpadu
|
||||
return classes[torch.max(outputs, 1)[1]]
|
49
NeuralNetwork/train_nn.py
Normal file
@ -0,0 +1,49 @@
|
||||
import torch
|
||||
import torch.nn as nn
|
||||
import torchvision.transforms as transforms
|
||||
import torch.optim as optim
|
||||
from torch.utils.data import DataLoader
|
||||
from torchvision.datasets import ImageFolder
|
||||
from NeuralNetwork import NeuralNetwork
|
||||
|
||||
# import matplotlib.pyplot as plt
|
||||
# import numpy as np
|
||||
# import cv2
|
||||
|
||||
def trainNeuralNetwork():
|
||||
neural_net = NeuralNetwork()
|
||||
train_set = ImageFolder(root='./resources/trash_dataset/train', transform=transforms.Compose([transforms.ToTensor(),transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))]))
|
||||
trainloader = DataLoader(
|
||||
train_set, batch_size=2, shuffle=True, num_workers=2)
|
||||
|
||||
# potrzebne do wyświetlania loss w każdej iteracji
|
||||
criterion = nn.CrossEntropyLoss()
|
||||
optimizer = optim.SGD(neural_net.parameters(), lr=0.001, momentum=0.9)
|
||||
|
||||
epoch_num = 4 # najlepiej 10, dla lepszej wiarygodności
|
||||
for epoch in range(epoch_num):
|
||||
measure_loss = 0.0
|
||||
for i, data in enumerate(trainloader, 0):
|
||||
inputs, labels = data
|
||||
# czyszczenie gradientu f-cji
|
||||
optimizer.zero_grad()
|
||||
outputs = neural_net(inputs)
|
||||
loss = criterion(outputs, labels)
|
||||
loss.backward()
|
||||
optimizer.step()
|
||||
measure_loss += loss.item()
|
||||
if i:
|
||||
print('[%d, %5d] loss: %.3f' %
|
||||
(epoch + 1, i + 1, measure_loss))
|
||||
measure_loss = 0.0
|
||||
|
||||
print('Finished.')
|
||||
PATH = './trained_nn.pth'
|
||||
torch.save(neural_net.state_dict(), PATH)
|
||||
|
||||
def main():
|
||||
trainNeuralNetwork()
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
@ -16,28 +16,6 @@
|
||||
| | | | | |--- feature_1 <= 2.50
|
||||
| | | | | | |--- feature_0 <= 2.50
|
||||
| | | | | | | |--- feature_1 <= 1.50
|
||||
| | | | | | | | |--- feature_4 <= 2.50
|
||||
| | | | | | | | | |--- class: 1
|
||||
| | | | | | | | |--- feature_4 > 2.50
|
||||
| | | | | | | | | |--- feature_2 <= 2.00
|
||||
| | | | | | | | | | |--- class: 1
|
||||
| | | | | | | | | |--- feature_2 > 2.00
|
||||
| | | | | | | | | | |--- class: 0
|
||||
| | | | | | | |--- feature_1 > 1.50
|
||||
| | | | | | | | |--- class: 0
|
||||
| | | | | | |--- feature_0 > 2.50
|
||||
| | | | | | | |--- feature_2 <= 2.50
|
||||
| | | | | | | | |--- class: 1
|
||||
| | | | | | | |--- feature_2 > 2.50
|
||||
| | | | | | | | |--- feature_4 <= 2.50
|
||||
| | | | | | | | | |--- class: 1
|
||||
| | | | | | | | |--- feature_4 > 2.50
|
||||
| | | | | | | | | |--- class: 0
|
||||
| | | | | |--- feature_1 > 2.50
|
||||
| | | | | | |--- feature_0 <= 3.50
|
||||
| | | | | | | |--- class: 0
|
||||
| | | | | | |--- feature_0 > 3.50
|
||||
| | | | | | | |--- feature_1 <= 3.50
|
||||
| | | | | | | | |--- feature_2 <= 2.50
|
||||
| | | | | | | | | |--- class: 1
|
||||
| | | | | | | | |--- feature_2 > 2.50
|
||||
@ -45,6 +23,28 @@
|
||||
| | | | | | | | | | |--- class: 1
|
||||
| | | | | | | | | |--- feature_4 > 2.00
|
||||
| | | | | | | | | | |--- class: 0
|
||||
| | | | | | | |--- feature_1 > 1.50
|
||||
| | | | | | | | |--- class: 0
|
||||
| | | | | | |--- feature_0 > 2.50
|
||||
| | | | | | | |--- feature_4 <= 2.50
|
||||
| | | | | | | | |--- class: 1
|
||||
| | | | | | | |--- feature_4 > 2.50
|
||||
| | | | | | | | |--- feature_2 <= 2.50
|
||||
| | | | | | | | | |--- class: 1
|
||||
| | | | | | | | |--- feature_2 > 2.50
|
||||
| | | | | | | | | |--- class: 0
|
||||
| | | | | |--- feature_1 > 2.50
|
||||
| | | | | | |--- feature_0 <= 3.50
|
||||
| | | | | | | |--- class: 0
|
||||
| | | | | | |--- feature_0 > 3.50
|
||||
| | | | | | | |--- feature_1 <= 3.50
|
||||
| | | | | | | | |--- feature_4 <= 2.50
|
||||
| | | | | | | | | |--- class: 1
|
||||
| | | | | | | | |--- feature_4 > 2.50
|
||||
| | | | | | | | | |--- feature_2 <= 2.00
|
||||
| | | | | | | | | | |--- class: 1
|
||||
| | | | | | | | | |--- feature_2 > 2.00
|
||||
| | | | | | | | | | |--- class: 0
|
||||
| | | | | | | |--- feature_1 > 3.50
|
||||
| | | | | | | | |--- class: 0
|
||||
| | | | |--- feature_3 > 4.50
|
||||
@ -59,13 +59,13 @@
|
||||
| | | | | | |--- class: 1
|
||||
| | | | |--- feature_3 > 3.50
|
||||
| | | | | |--- feature_1 <= 2.50
|
||||
| | | | | | |--- feature_3 <= 4.50
|
||||
| | | | | | | |--- feature_0 <= 2.50
|
||||
| | | | | | | | |--- class: 0
|
||||
| | | | | | | |--- feature_0 > 2.50
|
||||
| | | | | | | | |--- class: 1
|
||||
| | | | | | |--- feature_3 > 4.50
|
||||
| | | | | | |--- feature_0 <= 2.50
|
||||
| | | | | | | |--- class: 0
|
||||
| | | | | | |--- feature_0 > 2.50
|
||||
| | | | | | | |--- feature_3 <= 4.50
|
||||
| | | | | | | | |--- class: 1
|
||||
| | | | | | | |--- feature_3 > 4.50
|
||||
| | | | | | | | |--- class: 0
|
||||
| | | | | |--- feature_1 > 2.50
|
||||
| | | | | | |--- class: 0
|
||||
| | | |--- feature_4 > 4.50
|
||||
|
2
main.py
@ -118,7 +118,7 @@ class Game():
|
||||
# print('----')
|
||||
|
||||
print('positive actions')
|
||||
print(len(self.positive_actions))
|
||||
print(len(self.positive_decision))
|
||||
for i in self.positive_decision:
|
||||
# print(i.get_coords())
|
||||
trash_x, trash_y = i.get_coords()
|
||||
|
BIN
resources/trained_nn.pth
Normal file
BIN
resources/trash_dataset/test/glass/google-image(0601).jpeg
Normal file
After Width: | Height: | Size: 3.3 KiB |
BIN
resources/trash_dataset/test/glass/google-image(0602).jpeg
Normal file
After Width: | Height: | Size: 12 KiB |
BIN
resources/trash_dataset/test/glass/google-image(0605).jpeg
Normal file
After Width: | Height: | Size: 30 KiB |
BIN
resources/trash_dataset/test/glass/google-image(0606).jpeg
Normal file
After Width: | Height: | Size: 16 KiB |
BIN
resources/trash_dataset/test/glass/google-image(0607).jpeg
Normal file
After Width: | Height: | Size: 8.4 KiB |
BIN
resources/trash_dataset/test/glass/google-image(0608).jpeg
Normal file
After Width: | Height: | Size: 7.1 KiB |
BIN
resources/trash_dataset/test/glass/google-image(0613).jpeg
Normal file
After Width: | Height: | Size: 26 KiB |
BIN
resources/trash_dataset/test/glass/google-image(0615).jpeg
Normal file
After Width: | Height: | Size: 18 KiB |
BIN
resources/trash_dataset/test/glass/google-image(0616).jpeg
Normal file
After Width: | Height: | Size: 11 KiB |
BIN
resources/trash_dataset/test/glass/google-image(0617).jpeg
Normal file
After Width: | Height: | Size: 6.6 KiB |
BIN
resources/trash_dataset/test/glass/google-image(0618).jpeg
Normal file
After Width: | Height: | Size: 61 KiB |
BIN
resources/trash_dataset/test/glass/google-image(0618)_1.jpeg
Normal file
After Width: | Height: | Size: 41 KiB |
BIN
resources/trash_dataset/test/glass/google-image(0618)_2.jpeg
Normal file
After Width: | Height: | Size: 50 KiB |
BIN
resources/trash_dataset/test/glass/google-image(0618)_3.jpeg
Normal file
After Width: | Height: | Size: 54 KiB |
BIN
resources/trash_dataset/test/glass/google-image(0618)_4.jpeg
Normal file
After Width: | Height: | Size: 53 KiB |
BIN
resources/trash_dataset/test/glass/google-image(0618)_5.jpeg
Normal file
After Width: | Height: | Size: 48 KiB |
BIN
resources/trash_dataset/test/glass/google-image(0618)_6.jpeg
Normal file
After Width: | Height: | Size: 61 KiB |
BIN
resources/trash_dataset/test/glass/google-image(0618)_7.jpeg
Normal file
After Width: | Height: | Size: 34 KiB |
BIN
resources/trash_dataset/test/glass/google-image(0619).jpeg
Normal file
After Width: | Height: | Size: 26 KiB |
BIN
resources/trash_dataset/test/metal/google-image(0709).jpeg
Normal file
After Width: | Height: | Size: 37 KiB |
BIN
resources/trash_dataset/test/metal/google-image(0712).jpeg
Normal file
After Width: | Height: | Size: 46 KiB |
BIN
resources/trash_dataset/test/metal/google-image(0713).jpeg
Normal file
After Width: | Height: | Size: 57 KiB |
BIN
resources/trash_dataset/test/metal/google-image(0714).jpeg
Normal file
After Width: | Height: | Size: 27 KiB |
BIN
resources/trash_dataset/test/metal/google-image(0715).jpeg
Normal file
After Width: | Height: | Size: 14 KiB |
BIN
resources/trash_dataset/test/metal/google-image(0716).jpeg
Normal file
After Width: | Height: | Size: 14 KiB |
BIN
resources/trash_dataset/test/metal/google-image(0717).jpeg
Normal file
After Width: | Height: | Size: 35 KiB |
BIN
resources/trash_dataset/test/metal/google-image(0719).jpeg
Normal file
After Width: | Height: | Size: 41 KiB |
BIN
resources/trash_dataset/test/metal/google-image(0720).jpeg
Normal file
After Width: | Height: | Size: 13 KiB |
BIN
resources/trash_dataset/test/metal/google-image(0723).jpeg
Normal file
After Width: | Height: | Size: 27 KiB |
BIN
resources/trash_dataset/test/metal/google-image(0724).jpeg
Normal file
After Width: | Height: | Size: 10 KiB |
BIN
resources/trash_dataset/test/metal/google-image(0725).jpeg
Normal file
After Width: | Height: | Size: 14 KiB |
BIN
resources/trash_dataset/test/metal/google-image(0730).jpeg
Normal file
After Width: | Height: | Size: 40 KiB |
BIN
resources/trash_dataset/test/metal/google-image(0731).jpeg
Normal file
After Width: | Height: | Size: 67 KiB |
BIN
resources/trash_dataset/test/metal/google-image(0732).jpeg
Normal file
After Width: | Height: | Size: 28 KiB |
BIN
resources/trash_dataset/test/metal/google-image(0741).jpeg
Normal file
After Width: | Height: | Size: 30 KiB |
BIN
resources/trash_dataset/test/metal/google-image(0747).jpeg
Normal file
After Width: | Height: | Size: 34 KiB |
BIN
resources/trash_dataset/test/metal/google-image(0752).jpeg
Normal file
After Width: | Height: | Size: 35 KiB |
BIN
resources/trash_dataset/test/metal/google-image(0755).jpeg
Normal file
After Width: | Height: | Size: 39 KiB |
BIN
resources/trash_dataset/test/metal/google-image(0758).jpeg
Normal file
After Width: | Height: | Size: 106 KiB |
BIN
resources/trash_dataset/test/paper/google-image(0632).jpeg
Normal file
After Width: | Height: | Size: 16 KiB |
BIN
resources/trash_dataset/test/paper/google-image(0639).jpeg
Normal file
After Width: | Height: | Size: 27 KiB |
BIN
resources/trash_dataset/test/paper/google-image(0644).jpeg
Normal file
After Width: | Height: | Size: 24 KiB |
BIN
resources/trash_dataset/test/paper/google-image(0653).jpeg
Normal file
After Width: | Height: | Size: 11 KiB |
BIN
resources/trash_dataset/test/paper/google-image(0655).jpeg
Normal file
After Width: | Height: | Size: 91 KiB |
BIN
resources/trash_dataset/test/paper/google-image(0658).jpeg
Normal file
After Width: | Height: | Size: 18 KiB |
BIN
resources/trash_dataset/test/paper/google-image(0660).jpeg
Normal file
After Width: | Height: | Size: 44 KiB |
BIN
resources/trash_dataset/test/paper/google-image(0662).jpeg
Normal file
After Width: | Height: | Size: 34 KiB |
BIN
resources/trash_dataset/test/paper/google-image(0667).jpeg
Normal file
After Width: | Height: | Size: 27 KiB |
BIN
resources/trash_dataset/test/paper/google-image(0679).jpeg
Normal file
After Width: | Height: | Size: 45 KiB |
BIN
resources/trash_dataset/test/paper/google-image(0682).jpeg
Normal file
After Width: | Height: | Size: 33 KiB |
BIN
resources/trash_dataset/test/paper/google-image(0684).jpeg
Normal file
After Width: | Height: | Size: 25 KiB |
BIN
resources/trash_dataset/test/paper/google-image(0690).jpeg
Normal file
After Width: | Height: | Size: 49 KiB |
BIN
resources/trash_dataset/test/paper/google-image(0692).jpeg
Normal file
After Width: | Height: | Size: 51 KiB |
BIN
resources/trash_dataset/test/paper/google-image(0694).jpeg
Normal file
After Width: | Height: | Size: 41 KiB |
BIN
resources/trash_dataset/test/paper/google-image(0701).jpeg
Normal file
After Width: | Height: | Size: 55 KiB |
BIN
resources/trash_dataset/test/paper/google-image(0706).jpeg
Normal file
After Width: | Height: | Size: 16 KiB |
BIN
resources/trash_dataset/test/paper/google-image(0708).jpeg
Normal file
After Width: | Height: | Size: 61 KiB |
BIN
resources/trash_dataset/test/plastic/google-image(0613).jpeg
Normal file
After Width: | Height: | Size: 168 KiB |
BIN
resources/trash_dataset/test/plastic/google-image(0617).jpeg
Normal file
After Width: | Height: | Size: 8.2 KiB |
BIN
resources/trash_dataset/test/plastic/google-image(0618).jpeg
Normal file
After Width: | Height: | Size: 13 KiB |
BIN
resources/trash_dataset/test/plastic/google-image(0627).jpeg
Normal file
After Width: | Height: | Size: 15 KiB |
BIN
resources/trash_dataset/test/plastic/google-image(0634).jpeg
Normal file
After Width: | Height: | Size: 33 KiB |
BIN
resources/trash_dataset/test/plastic/google-image(0634)_1.jpeg
Normal file
After Width: | Height: | Size: 22 KiB |
BIN
resources/trash_dataset/test/plastic/google-image(0636).jpeg
Normal file
After Width: | Height: | Size: 8.7 KiB |
BIN
resources/trash_dataset/test/plastic/google-image(0637).jpeg
Normal file
After Width: | Height: | Size: 16 KiB |
BIN
resources/trash_dataset/test/plastic/google-image(0641).jpeg
Normal file
After Width: | Height: | Size: 31 KiB |
BIN
resources/trash_dataset/test/plastic/google-image(0641)_1.jpeg
Normal file
After Width: | Height: | Size: 22 KiB |
BIN
resources/trash_dataset/test/plastic/google-image(0641)_2.jpeg
Normal file
After Width: | Height: | Size: 27 KiB |
BIN
resources/trash_dataset/test/plastic/google-image(0641)_3.jpeg
Normal file
After Width: | Height: | Size: 19 KiB |
BIN
resources/trash_dataset/test/plastic/google-image(0641)_4.jpeg
Normal file
After Width: | Height: | Size: 19 KiB |
BIN
resources/trash_dataset/test/plastic/google-image(0641)_5.jpeg
Normal file
After Width: | Height: | Size: 31 KiB |
BIN
resources/trash_dataset/test/plastic/google-image(0641)_6.jpeg
Normal file
After Width: | Height: | Size: 31 KiB |
BIN
resources/trash_dataset/test/plastic/google-image(0641)_7.jpeg
Normal file
After Width: | Height: | Size: 9.4 KiB |
BIN
resources/trash_dataset/test/plastic/google-image(0645).jpeg
Normal file
After Width: | Height: | Size: 20 KiB |
BIN
resources/trash_dataset/train/glass/google-image(0001).jpeg
Normal file
After Width: | Height: | Size: 4.6 KiB |
BIN
resources/trash_dataset/train/glass/google-image(0002).jpeg
Normal file
After Width: | Height: | Size: 9.5 KiB |
BIN
resources/trash_dataset/train/glass/google-image(0003).jpeg
Normal file
After Width: | Height: | Size: 4.3 KiB |
BIN
resources/trash_dataset/train/glass/google-image(0004).jpeg
Normal file
After Width: | Height: | Size: 51 KiB |
BIN
resources/trash_dataset/train/glass/google-image(0005).jpeg
Normal file
After Width: | Height: | Size: 22 KiB |
BIN
resources/trash_dataset/train/glass/google-image(0006).jpeg
Normal file
After Width: | Height: | Size: 6.1 KiB |
BIN
resources/trash_dataset/train/glass/google-image(0007).jpeg
Normal file
After Width: | Height: | Size: 7.5 KiB |
BIN
resources/trash_dataset/train/glass/google-image(0008).jpeg
Normal file
After Width: | Height: | Size: 72 KiB |
BIN
resources/trash_dataset/train/glass/google-image(0009).jpeg
Normal file
After Width: | Height: | Size: 6.5 KiB |
BIN
resources/trash_dataset/train/glass/google-image(0010).jpeg
Normal file
After Width: | Height: | Size: 6.8 KiB |
BIN
resources/trash_dataset/train/glass/google-image(0011).jpeg
Normal file
After Width: | Height: | Size: 14 KiB |
BIN
resources/trash_dataset/train/glass/google-image(0012).jpeg
Normal file
After Width: | Height: | Size: 5.5 KiB |
BIN
resources/trash_dataset/train/glass/google-image(0014).jpeg
Normal file
After Width: | Height: | Size: 3.6 KiB |
BIN
resources/trash_dataset/train/glass/google-image(0015).jpeg
Normal file
After Width: | Height: | Size: 27 KiB |
BIN
resources/trash_dataset/train/glass/google-image(0016).jpeg
Normal file
After Width: | Height: | Size: 14 KiB |
BIN
resources/trash_dataset/train/glass/google-image(0017).jpeg
Normal file
After Width: | Height: | Size: 4.1 KiB |
BIN
resources/trash_dataset/train/glass/google-image(0018).jpeg
Normal file
After Width: | Height: | Size: 13 KiB |
BIN
resources/trash_dataset/train/glass/google-image(0019).jpeg
Normal file
After Width: | Height: | Size: 12 KiB |
BIN
resources/trash_dataset/train/glass/google-image(0020).jpeg
Normal file
After Width: | Height: | Size: 29 KiB |