Compare commits

...

3 Commits

Author SHA1 Message Date
MarRac
df7c553c59 modified the set of photos and types of plants 2024-05-27 04:32:31 +02:00
MarRac
8ddba0c828 modified the CNN to get better results 2024-05-27 04:27:46 +02:00
MarRac
35d7a6ab6e dodano zastosowanie sieci neuronowej w main 2024-05-27 00:32:20 +02:00
6040 changed files with 52 additions and 37 deletions

BIN
source/CNN_model.pth Normal file

Binary file not shown.

View File

@ -10,19 +10,15 @@ class Conv_Neural_Network_Model(nn.Module):
self.conv1 = nn.Conv2d(in_channels=3, out_channels=32, kernel_size=3, stride=1, padding=1) self.conv1 = nn.Conv2d(in_channels=3, out_channels=32, kernel_size=3, stride=1, padding=1)
self.pool1 = nn.MaxPool2d(kernel_size=2, stride=2, padding=0) self.pool1 = nn.MaxPool2d(kernel_size=2, stride=2, padding=0)
self.conv2 = nn.Conv2d(in_channels=32, out_channels=64, kernel_size=3, stride=1, padding=1) self.conv2 = nn.Conv2d(in_channels=32, out_channels=64, kernel_size=3, stride=1, padding=1)
self.pool2 = nn.MaxPool2d(kernel_size=2, stride=2, padding=0)
self.conv3 = nn.Conv2d(in_channels=64, out_channels=128, kernel_size=3, stride=1, padding=1)
self.pool3 = nn.MaxPool2d(kernel_size=2, stride=2, padding=0)
self.fc1 = nn.Linear(128*12*12,hidden_layer1) self.fc1 = nn.Linear(64*25*25,hidden_layer1)
self.fc2 = nn.Linear(hidden_layer1,hidden_layer2) self.fc2 = nn.Linear(hidden_layer1,hidden_layer2)
self.out = nn.Linear(hidden_layer2,num_classes) self.out = nn.Linear(hidden_layer2,num_classes)
def forward(self, x): def forward(self, x):
x = self.pool1(F.relu(self.conv1(x))) x = self.pool1(F.relu(self.conv1(x)))
x = self.pool2(F.relu(self.conv2(x))) x = self.pool1(F.relu(self.conv2(x)))
x = self.pool3(F.relu(self.conv3(x))) x = x.view(-1, 64*25*25) #<----flattening the image
x = x.view(-1, 128*12*12) #<----flattening the image
x = self.fc1(x) x = self.fc1(x)
x = torch.relu(x) x = torch.relu(x)
x = self.fc2(x) x = self.fc2(x)

View File

@ -29,7 +29,7 @@ test_set = datasets.ImageFolder(root='resources/test', transform=data_transforme
#function for training model #function for training model
def train(model, dataset, iter=100, batch_size=64): def train(model, dataset, iter=100, batch_size=64):
optimizer = torch.optim.SGD(model.parameters(), lr=0.001) optimizer = torch.optim.SGD(model.parameters(), lr=0.01)
criterion = nn.NLLLoss() criterion = nn.NLLLoss()
train_loader = DataLoader(dataset, batch_size=batch_size, shuffle=True) train_loader = DataLoader(dataset, batch_size=batch_size, shuffle=True)
model.train() model.train()
@ -60,13 +60,13 @@ model = Conv_Neural_Network_Model()
model.to(device) model.to(device)
#loading the already saved model: #loading the already saved model:
# model.load_state_dict(torch.load('model.pth')) model.load_state_dict(torch.load('CNN_model.pth'))
# model.eval() model.eval()
#training the model: # #training the model:
# train(model, train_set) # train(model, train_set)
# print(f"Accuracy of the network is: {100*accuracy(model, test_set)}%") # print(f"Accuracy of the network is: {100*accuracy(model, test_set)}%")
# torch.save(model.state_dict(), 'model.pth') # torch.save(model.state_dict(), 'CNN_model.pth')
@ -98,7 +98,7 @@ def guess_image(model, image_tensor):
# print(f"The predicted image is: {prediction}") # print(f"The predicted image is: {prediction}")
#TEST - loading the image and getting results: #TEST - loading the image and getting results:
testImage_path = 'resources/images/plant_photos/pexels-justus-menke-3490295-5213970.jpg' testImage_path = 'resources/images/plant_photos/1c76aa4d-11f4-47d1-8bdd-2cb78deeeccf.jpg'
testImage = Image.open(testImage_path) testImage = Image.open(testImage_path)
testImage = data_transformer(testImage) testImage = data_transformer(testImage)
testImage = testImage.unsqueeze(0) testImage = testImage.unsqueeze(0)

View File

@ -62,15 +62,35 @@ def main():
while run: while run:
for event in pygame.event.get(): for event in pygame.event.get():
if event.type == pygame.QUIT: if event.type == pygame.QUIT:
run = False run = False
#small test of work_on_field method:
time.sleep(1) time.sleep(1)
tile1 = tiles[0]
p1 = Plant('wheat', 'cereal', random.randint(1,100), random.randint(1,100), random.randint(1,100)) # movement based on route-planning (test):
tractor.draw_tractor(WIN)
time.sleep(1)
if moves != False:
do_actions(tractor, WIN, moves)
#guessing the image under the tile:
goalTile = tiles[tile_index]
goalTile.display_photo()
image_path = goalTile.photo
image_tensor = load_image(image_path)
prediction = guess_image(load_model(), image_tensor)
print(f"The predicted image is: {prediction}")
p1 = Plant('wheat', 'cereal', random.randint(1,100), random.randint(1,100), random.randint(1,100))
goalTile.plant = p1
d1 = Dirt(random.randint(1, 100), random.randint(1,100)) d1 = Dirt(random.randint(1, 100), random.randint(1,100))
d1.pests_and_weeds() d1.pests_and_weeds()
tile1.ground=d1 goalTile.ground=d1
#getting the name and type of the recognized plant:
p1.update_name(prediction)
#decission tree test:
if d1.pest: if d1.pest:
pe = 1 pe = 1
else: else:
@ -117,25 +137,12 @@ def main():
model = joblib.load('model.pkl') model = joblib.load('model.pkl')
nowe_dane = pd.read_csv('model_data.csv') nowe_dane = pd.read_csv('model_data.csv')
predykcje = model.predict(nowe_dane) predykcje = model.predict(nowe_dane)
# movement based on route-planning (test):
tractor.draw_tractor(WIN)
time.sleep(1)
if moves != False:
do_actions(tractor, WIN, moves)
print(predykcje) print(predykcje)
if predykcje == 'work':
tractor.work_on_field(tile1, d1, p1)
#guessing the image under the tile: #work on field:
tiles[tile_index].display_photo() if predykcje == 'work':
image_path = tiles[tile_index].photo tractor.work_on_field(goalTile, d1, p1)
image_tensor = load_image(image_path)
prediction = guess_image(load_model(), image_tensor)
print(f"The predicted image is: {prediction}")
time.sleep(30) time.sleep(30)
print("\n") print("\n")

Binary file not shown.

View File

@ -19,7 +19,19 @@ class Plant:
else: else:
print("Unable to grow due to bad condition of the ground") print("Unable to grow due to bad condition of the ground")
# more properties def update_name(self, predicted_class):
if predicted_class == "Apple":
self.name = "Apple"
self.plant_type = "fruit"
elif predicted_class == "Radish":
self.name = "Radish"
self.plant_type = "vegetable"
# add init, getters,setters elif predicted_class == "Cauliflower":
self.name = "Cauliflower"
self.plant_type = "vegetable"
elif predicted_class == "Wheat":
self.name = "Wheat"
self.plant_type = "cereal"

Binary file not shown.

After

Width:  |  Height:  |  Size: 39 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 48 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 190 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 245 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 281 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 234 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 190 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 266 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 542 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 64 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.0 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 99 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 5.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 743 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 93 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 37 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 130 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.7 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.2 MiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 341 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.3 MiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.2 MiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 888 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 197 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.7 MiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 305 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 62 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 17 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 18 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 35 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 17 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 32 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 17 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 18 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 13 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 24 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 29 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 18 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 18 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 36 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 13 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 36 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 17 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 13 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 17 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 17 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 28 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 13 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 13 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 33 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 17 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 24 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 33 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 19 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 33 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 17 KiB

Some files were not shown because too many files have changed in this diff Show More