Merge pull request 'cnn' (#35) from cnn into master

Reviewed-on: #35
This commit is contained in:
Juliusz Sadowski 2022-06-09 14:49:55 +02:00
commit bab75274dc
26 changed files with 83 additions and 69 deletions

3
.gitignore vendored
View File

@ -149,4 +149,5 @@ cython_debug/
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
.idea/
.idea/
/algorithms/neural_network/data/

Binary file not shown.

Before

Width:  |  Height:  |  Size: 814 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 820 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 789 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.0 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 760 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.2 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 725 B

View File

@ -10,23 +10,39 @@ from common.constants import DEVICE, BATCH_SIZE, NUM_EPOCHS, LEARNING_RATE, SETU
class NeuralNetwork(pl.LightningModule):
def __init__(self, numChannels=3, batch_size=BATCH_SIZE, learning_rate=LEARNING_RATE, num_classes=4):
super().__init__()
self.layer = nn.Sequential(
nn.Linear(36*36*3, 300),
nn.ReLU(),
nn.Linear(300, 4),
nn.LogSoftmax(dim=-1)
)
super(NeuralNetwork, self).__init__()
self.conv1 = nn.Conv2d(numChannels, 24, (3, 3), padding=1)
self.relu1 = nn.ReLU()
self.maxpool1 = nn.MaxPool2d((2, 2), stride=2)
self.conv2 = nn.Conv2d(24, 48, (3, 3), padding=1)
self.relu2 = nn.ReLU()
self.fc1 = nn.Linear(48*18*18, 800)
self.relu3 = nn.ReLU()
self.fc2 = nn.Linear(800, 400)
self.relu4 = nn.ReLU()
self.fc3 = nn.Linear(400, 4)
self.logSoftmax = nn.LogSoftmax(dim=1)
self.batch_size = batch_size
self.learning_rate = learning_rate
def forward(self, x):
x = self.conv1(x)
x = self.relu1(x)
x = self.maxpool1(x)
x = self.conv2(x)
x = self.relu2(x)
x = x.reshape(x.shape[0], -1)
x = self.layer(x)
x = self.fc1(x)
x = self.relu3(x)
x = self.fc2(x)
x = self.relu4(x)
x = self.fc3(x)
x = self.logSoftmax(x)
return x
def configure_optimizers(self):
optimizer = SGD(self.parameters(), lr=self.learning_rate)
optimizer = Adam(self.parameters(), lr=self.learning_rate)
return optimizer
def training_step(self, batch, batch_idx):

View File

@ -10,44 +10,8 @@ from torch.optim import Adam
import matplotlib.pyplot as plt
import pytorch_lightning as pl
from pytorch_lightning.callbacks import EarlyStopping
def train(model):
model = model.to(DEVICE)
model.train()
trainset = WaterSandTreeGrass('./data/train_csv_file.csv', transform=SETUP_PHOTOS)
testset = WaterSandTreeGrass('./data/test_csv_file.csv', transform=SETUP_PHOTOS)
train_loader = DataLoader(trainset, batch_size=BATCH_SIZE, shuffle=True)
test_loader = DataLoader(testset, batch_size=BATCH_SIZE, shuffle=True)
criterion = nn.CrossEntropyLoss()
optimizer = Adam(model.parameters(), lr=LEARNING_RATE)
for epoch in range(NUM_EPOCHS):
for batch_idx, (data, targets) in enumerate(train_loader):
data = data.to(device=DEVICE)
targets = targets.to(device=DEVICE)
scores = model(data)
loss = criterion(scores, targets)
optimizer.zero_grad()
loss.backward()
optimizer.step()
if batch_idx % 4 == 0:
print("epoch: %d loss: %.4f" % (epoch, loss.item()))
print("FINISHED TRAINING!")
torch.save(model.state_dict(), "./learnednetwork.pth")
print("Checking accuracy for the train set.")
check_accuracy(train_loader)
print("Checking accuracy for the test set.")
check_accuracy(test_loader)
print("Checking accuracy for the tiles.")
check_accuracy_tiles()
import torchvision.transforms.functional as F
from PIL import Image
def check_accuracy_tiles():
@ -95,12 +59,13 @@ def check_accuracy_tiles():
def what_is_it(img_path, show_img=False):
image = read_image(img_path, mode=ImageReadMode.RGB)
image = Image.open(img_path).convert('RGB')
if show_img:
plt.imshow(plt.imread(img_path))
plt.imshow(image)
plt.show()
image = SETUP_PHOTOS(image).unsqueeze(0)
model = NeuralNetwork.load_from_checkpoint('./lightning_logs/version_3/checkpoints/epoch=8-step=810.ckpt')
model = NeuralNetwork.load_from_checkpoint('./lightning_logs/version_20/checkpoints/epoch=3-step=324.ckpt')
with torch.no_grad():
model.eval()
@ -108,18 +73,53 @@ def what_is_it(img_path, show_img=False):
return ID_TO_CLASS[idx]
CNN = NeuralNetwork()
def check_accuracy(tset):
model = NeuralNetwork.load_from_checkpoint('./lightning_logs/version_23/checkpoints/epoch=3-step=324.ckpt')
num_correct = 0
num_samples = 0
model = model.to(DEVICE)
model.eval()
with torch.no_grad():
for photo, label in tset:
photo = photo.to(DEVICE)
label = label.to(DEVICE)
scores = model(photo)
predictions = scores.argmax(dim=1)
num_correct += (predictions == label).sum()
num_samples += predictions.size(0)
print(f'Got {num_correct} / {num_samples} with accuracy {float(num_correct)/float(num_samples)*100:.2f}%')
trainer = pl.Trainer(accelerator='gpu', devices=1, auto_scale_batch_size=True, callbacks=[EarlyStopping('val_loss')], max_epochs=NUM_EPOCHS)
def check_accuracy_data():
trainset = WaterSandTreeGrass('./data/train_csv_file.csv', transform=SETUP_PHOTOS)
testset = WaterSandTreeGrass('./data/test_csv_file.csv', transform=SETUP_PHOTOS)
train_loader = DataLoader(trainset, batch_size=BATCH_SIZE, shuffle=True)
test_loader = DataLoader(testset, batch_size=BATCH_SIZE)
print("Accuracy of train_set:")
check_accuracy(train_loader)
print("Accuracy of test_set:")
check_accuracy(test_loader)
#CNN = NeuralNetwork()
#common.helpers.createCSV()
#trainer = pl.Trainer(accelerator='gpu', callbacks=EarlyStopping('val_loss'), devices=1, max_epochs=NUM_EPOCHS)
#trainer = pl.Trainer(accelerator='gpu', devices=1, auto_lr_find=True, max_epochs=NUM_EPOCHS)
trainset = WaterSandTreeGrass('./data/train_csv_file.csv', transform=SETUP_PHOTOS)
testset = WaterSandTreeGrass('./data/test_csv_file.csv', transform=SETUP_PHOTOS)
train_loader = DataLoader(trainset, batch_size=BATCH_SIZE, shuffle=True)
test_loader = DataLoader(testset, batch_size=BATCH_SIZE)
#trainset = WaterSandTreeGrass('./data/train_csv_file.csv', transform=SETUP_PHOTOS)
#testset = WaterSandTreeGrass('./data/test_csv_file.csv', transform=SETUP_PHOTOS)
#train_loader = DataLoader(trainset, batch_size=BATCH_SIZE, shuffle=True)
#test_loader = DataLoader(testset, batch_size=BATCH_SIZE)
#trainer.fit(CNN, train_loader, test_loader)
#trainer.tune(CNN, train_loader, test_loader)
check_accuracy_tiles()
print(what_is_it('../../resources/textures/sand.png', True))
#print(what_is_it('../../resources/textures/grass2.png', True))
#check_accuracy_data()
#check_accuracy_tiles()

View File

@ -3,6 +3,7 @@ from torch.utils.data import Dataset
import pandas as pd
from torchvision.io import read_image, ImageReadMode
from common.helpers import createCSV
from PIL import Image
class WaterSandTreeGrass(Dataset):
@ -15,7 +16,8 @@ class WaterSandTreeGrass(Dataset):
return len(self.img_labels)
def __getitem__(self, idx):
image = read_image(self.img_labels.iloc[idx, 0], mode=ImageReadMode.RGB)
image = Image.open(self.img_labels.iloc[idx, 0]).convert('RGB')
label = torch.tensor(int(self.img_labels.iloc[idx, 1]))
if self.transform:

View File

@ -77,19 +77,17 @@ BAR_HEIGHT_MULTIPLIER = 0.1
#NEURAL_NETWORK
LEARNING_RATE = 0.13182567385564073
LEARNING_RATE = 0.000630957344480193
BATCH_SIZE = 64
NUM_EPOCHS = 50
NUM_EPOCHS = 9
DEVICE = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')
print("Using ", DEVICE)
CLASSES = ['grass', 'sand', 'tree', 'water']
SETUP_PHOTOS = transforms.Compose([
transforms.Resize(36),
transforms.CenterCrop(36),
transforms.ToPILImage(),
transforms.ToTensor(),
transforms.Resize((36, 36)),
transforms.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5])
])