Commit cba18b31 authored by Julius2305's avatar Julius2305

Use GitLab

parent f520d7b0
import torch
from torch import autograd, nn, optim
import torch.nn.functional as F
from tqdm import tqdm
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
# self.conv1 = nn.Conv2d(1, 32, 3)
# self.conv2 = nn.Conv2d(32, 64, 3)
#self.dropout1 = nn.Dropout2d(0.25)
#self.dropout2 = nn.Dropout2d(0.5)
self.fc1 = nn.Linear(100, 512)
self.fc2 = nn.Linear(512, 1028)
self.fc3 = nn.Linear(1028, 512)
self.fc4 = nn.Linear(512, 100)
# x represents our data
def forward(self, x):
# Pass data through conv1
# x = self.conv1(x)
# # Use the rectified-linear activation function over x
# x = F.relu(x)
#
# x = self.conv2(x)
# x = F.relu(x)
#
# # Run max pooling over x
# x = F.max_pool2d(x, 2)
# # Pass data through dropout1
# #x = self.dropout1(x)
# # Flatten x with start_dim=1
# x = x.view(-1, self.num_flat_features(x))
# Pass data through fc1
x = self.fc1(x)
x = F.relu(x)
x = self.fc2(x)
x = F.relu(x)
x = self.fc3(x)
x = F.relu(x)
#x = self.dropout2(x)
x = self.fc4(x)
return x
def num_flat_features(self, x):
size = x.size()[1:] # all dimensions except the batch dimension
num_features = 1
for s in size:
num_features *= s
return num_features
def get_data():
f = open("D:\\Studium\\Bachelorarbeit\\Unity Projekte\\A-Stern Test\\A-Stern Test\\Assets\\Resources\\training_data.txt", "r")
# Using readlines()
Lines = f.readlines()
weightmatrix = []
points_return = []
target_data_return = []
count = 0
# Strips the newline character
for line in Lines:
elements = line.split(";")
#get the weightmatrix
x = elements[0].split(", ")
x.remove("")
for i in range(0, len(x), 1):
x[i] = int(x[i])
weightmatrix.append(x)
#get the start and endpoints
points = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
y = elements[1].split(" ")
for i in range(0, len(y), 1):
y[i] = y[i].strip('[]')
y[i] = y[i].split(",")
for j in range(0, len(y[i]), 1):
y[i][j] = int(y[i][j])
points[y[i][0]][y[i][1]] = 1000
points_return.append(points)
#points.append(torch.tensor(y))
#get the path and transform it into a matrix
z = elements[2].split(", ")
z.remove("")
for i in range(0, len(z), 1):
z[i] = z[i].strip('()')
z[i] = z[i].split(",")
for j in range(0, len(z[i]), 1):
z[i][j] = int(z[i][j])
target_data = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
for i in range(0, len(z), 1):
target_data[z[i][0]][z[i][1]] = 100
target_data[y[1][0]][y[1][1]] = 100
target_data_return.append(target_data)
count += 1
f.close()
return weightmatrix, points_return, target_data_return, count
if __name__ == '__main__':
weightmatrix, points, target_data, batch_size = get_data()
weightmatrix = torch.tensor(weightmatrix, dtype=torch.float32)
weightmatrix = weightmatrix.view(batch_size, 1,10,10)
#weightmatrix = weightmatrix.type(torch.int8)
points = torch.tensor(points, dtype=torch.float32)
points = points.view(batch_size, 1,10,10)
#merge the weightmatrix and the start-/endpoints to a two-channeled input
#weightmatrix = torch.cat((weightmatrix, points), 1)
target_data = torch.tensor(target_data, dtype=torch.float32)
target_data = target_data.view(batch_size, 100)
target_data = target_data + weightmatrix.view(batch_size, 100)
target_data = target_data + points.view(batch_size, 100)
#target_data.type(torch.int16)
learning_rate = 0.1
model_path = 'D:\\Studium\\Bachelorarbeit\\Machine Learning\\ressources\\models'
nn_pathfinder = Net()
opt = optim.SGD(params=nn_pathfinder.parameters(), lr=learning_rate)
result = nn_pathfinder(weightmatrix.view(batch_size, 100))
criterion = nn.MSELoss()
#smoothl1 - loss testen
#print ("Result: ", result)
print("before training")
# print ("Result: ", result.flatten())
# print ("Target Data: ", target_data.flatten())
print("loss: ", criterion(result, target_data))
for epoch in tqdm(range(7500)):
result = nn_pathfinder(weightmatrix.view(batch_size, 100))
#loss = F.nll_loss(result, target_data)
loss = criterion(result, target_data)
#if(epoch % 10 == 0):
print("loss" , loss)
nn_pathfinder.zero_grad()
loss.backward()
opt.step()
print("after")
# print ("Result: ", result.flatten())
# #result = result.type(torch.int8)
# result = torch.round(result)
# result = result.type(torch.int8)
# target_data = target_data.type(torch.int8)
# print ("Result: ", result.view(10,10))
# print ("Target Data: ", target_data.view(10,10))
print("loss: ", loss)
torch.save(nn_pathfinder.state_dict(),
model_path + "\\ansatz2_model_nr_" + "1" + ".pth")
......@@ -42,7 +42,7 @@ class Net(nn.Module):
def get_data():
f = open("D:\\Studium\\Bachelorarbeit\\Unity Projekte\\A-Stern Test\\A-Stern Test\\Assets\\Resources\\training_data.txt", "r")
f = open("D:\\Studium\\Bachelorarbeit\\Unity Projekte\\Unity-Projekte\\Generate Data\\Assets\\Resources\\200_training_data.txt", "r") #Insert the path to the data here
# Using readlines()
Lines = f.readlines()
......
......@@ -6,23 +6,11 @@ from tqdm import tqdm
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.conv1 = nn.Conv2d(1, 32, 3)
self.conv2 = nn.Conv2d(32, 64, 3)
self.fc1 = nn.Linear(576, 128)
self.fc1 = nn.Linear(100, 128)
self.fc2 = nn.Linear(128, 100)
# x represents our data
def forward(self, x):
# Pass data through conv1
x = self.conv1(x)
# Use the rectified-linear activation function over x
x = F.relu(x)
x = self.conv2(x)
x = F.relu(x)
# Run max pooling over x
x = F.max_pool2d(x, 2)
# Flatten x with start_dim=1
x = x.view(-1, self.num_flat_features(x))
# Pass data through fc1
......@@ -43,12 +31,12 @@ class Net(nn.Module):
def get_data():
f = open("D:\\Studium\\Bachelorarbeit\\Unity Projekte\\A-Stern Test\\A-Stern Test\\Assets\\Resources\\data.txt", "r")
f = open("D:\\Studium\\Bachelorarbeit\\Unity Projekte\\Unity-Projekte\\Generate Data\\Assets\\Resources\\200_training_data.txt", "r") #Insert the path to the data here
# Using readlines()
Lines = f.readlines()
weightmatrix = []
points = []
points_return = []
target_data_return = []
count = 0
......@@ -64,14 +52,25 @@ def get_data():
weightmatrix.append(x)
#get the start and endpoints
points = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
y = elements[1].split(" ")
for i in range(0, len(y), 1):
y[i] = y[i].strip('[]')
y[i] = y[i].split(",")
for j in range(0, len(y[i]), 1):
y[i][j] = int(y[i][j])
points.append(torch.tensor(y))
points[y[i][0]][y[i][1]] = 1000
points_return.append(points)
#get the path and transform it into a matrix
z = elements[2].split(", ")
......@@ -101,20 +100,24 @@ def get_data():
count += 1
f.close()
return weightmatrix, points, target_data_return, count
return weightmatrix, points_return, target_data_return, count
weightmatrix, points, target_data, batch_size = get_data()
weightmatrix = torch.tensor(weightmatrix)
weightmatrix = weightmatrix.view(batch_size, 1,10,10)
weightmatrix = weightmatrix.view(batch_size, 1, 1, 100)
learning_rate = 0.1
#mark start and endpoint in the weightmatrix
points = torch.tensor(points, dtype=torch.float32)
points = points.view(batch_size, 1,10,10)
weightmatrix = weightmatrix + points.view(batch_size, 1, 1, 100)
learning_rate = 0.1
target_data = torch.tensor(target_data)
target_data = target_data.view(batch_size, 1,10,10)
target_data = torch.tensor(target_data, dtype=torch.float32)
target_data = target_data.view(batch_size, 1, 1, 100)
my_nn = Net()
......
......@@ -6,6 +6,8 @@ import cv2
import numpy as np
import matplotlib.pyplot as plt
#In this code, the loss of each part of the loss function was traced individually
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
......@@ -141,7 +143,7 @@ class CustomLoss(nn.Module):
def get_data():
f = open("D:\\Studium\\Bachelorarbeit\\Unity Projekte\\A-Stern Test\\A-Stern Test\\Assets\\Resources\\single_example_data.txt", "r")
f = open("D:\\Studium\\Bachelorarbeit\\Unity Projekte\\Unity-Projekte\\Generate Data\\Assets\\Resources\\200_training_data.txt", "r") #Insert the path to the data here
# Using readlines()
Lines = f.readlines()
......
......@@ -6,6 +6,8 @@ import cv2
import numpy as np
import matplotlib.pyplot as plt
#In this code, the loss of each part of the loss function was summed up
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
......@@ -122,7 +124,7 @@ class CustomLoss(nn.Module):
def get_data():
f = open("D:\\Studium\\Bachelorarbeit\\Unity Projekte\\A-Stern Test\\A-Stern Test\\Assets\\Resources\\single_example_data.txt", "r")
f = open("D:\\Studium\\Bachelorarbeit\\Unity Projekte\\Unity-Projekte\\Generate Data\\Assets\\Resources\\200_training_data.txt", "r") #Insert the path to the data here
# Using readlines()
Lines = f.readlines()
......
......@@ -6,6 +6,8 @@ import cv2
import numpy as np
import matplotlib.pyplot as plt
#In this code, the loss of each part of the loss function was summed up. The loss function can work with a variable batch size.
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
......@@ -131,7 +133,7 @@ class CustomLoss(nn.Module):
def get_data():
f = open("D:\\Studium\\Bachelorarbeit\\Unity Projekte\\A-Stern Test\\A-Stern Test\\Assets\\Resources\\200_training_data.txt", "r")
f = open("D:\\Studium\\Bachelorarbeit\\Unity Projekte\\Unity-Projekte\\Generate Data\\Assets\\Resources\\200_training_data.txt", "r") #Insert the path to the data here
# Using readlines()
Lines = f.readlines()
......@@ -198,6 +200,11 @@ weightmatrix, points, target_data, batch_size = get_data()
weightmatrix = torch.tensor(weightmatrix, dtype=torch.float32)
weightmatrix = weightmatrix.view(batch_size, 1,10,10)
#mark start and end in the weightmatrix
for i in range(0, batch_size):
weightmatrix[i, 0, points[i][0][0], points[i][0][1]] += 1000
weightmatrix[i, 0, points[i][1][0], points[i][1][1]] += 1000
random_data = autograd.Variable(torch.rand(1, 1, 10, 10),requires_grad = True)
......
......@@ -128,7 +128,7 @@ class CustomLoss(nn.Module):
def get_data():
f = open("D:\\Studium\\Bachelorarbeit\\Unity Projekte\\A-Stern Test\\A-Stern Test\\Assets\\Resources\\single_example_data2.txt", "r")
f = open("D:\\Studium\\Bachelorarbeit\\Unity Projekte\\Unity-Projekte\\Generate Data\\Assets\\Resources\\single_example_data2.txt", "r")
# Using readlines()
Lines = f.readlines()
......@@ -195,6 +195,10 @@ weightmatrix, points, target_data, batch_size = get_data()
weightmatrix = torch.tensor(weightmatrix, dtype=torch.float32)
weightmatrix = weightmatrix.view(batch_size, 1,10,10)
#mark start and end in the weightmatrix
for i in range(0, batch_size):
weightmatrix[i, 0, points[i][0][0], points[i][0][1]] += 1000
weightmatrix[i, 0, points[i][1][0], points[i][1][1]] += 1000
random_data = autograd.Variable(torch.rand(1, 1, 10, 10),requires_grad = True)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment