Hello! Now im trying to understand how I can use pictures in pytorch. Im quite new to everything here. Now my problem is, i am expecting a output with a list with two numbers. But the output has the format [1, 18, 12, 2].
How do I get the format i desire?
There is a different thing i dont get, the output of the last convolution layer has 18 channels, but why does it output something with 12 layers?
Any advice is appreciated!
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
from os import listdir
from torch.autograd import Variable
from torchvision import transforms
from PIL import Image
class Netz(nn.Module): # Das Netz
def __init__(self):
super(Netz,self).__init__()
self.cn1 = nn.Conv2d(3,5, kernel_size=5)
self.cn2 = nn.Conv2d(5,7, kernel_size=5)
self.cn3 = nn.Conv2d(7,12, kernel_size=5)
self.cn4 = nn.Conv2d(12,18, kernel_size=5)
self.ln1 = nn.Linear(12,200)
self.ln2 = nn.Linear(200,20)
self.ln3 = nn.Linear(20,10)
self.ln4 = nn.Linear(10,2)
def forward(self, x):
x = self.cn1(x)
x = F.max_pool2d(x,2)
x = F.relu(x)
x = self.cn2(x)
x = F.max_pool2d(x,2)
x = F.relu(x)
x = self.cn3(x)
x = F.max_pool2d(x,2)
x = F.relu(x)
x = self.cn4(x)
x = F.max_pool2d(x,2)
x = F.relu(x)
x = F.relu(self.ln1(x))
x = F.relu(self.ln2(x))
x = F.relu(self.ln3(x))
return F.relu(self.ln4(x))
model = Netz()
train_data_list = []
target_list = []
train_data = []
normalize = transforms.Normalize(
mean = [0.485, 0.456, 0.406],
std = [0.229, 0.224, 0.225]
)
transform = transforms.Compose([transforms.Resize(256),
transforms.CenterCrop(256),
transforms.ToTensor(),
normalize]); #Liste von "Befehlen" die ausgeführt werden bei Aufruf
for f in listdir("E:/DataAI"):
img = Image.open("E:/DataAI/" + f)
img_tensor = transform(img)
img_tensor.unsqueeze_(0)
train_data_list.append(img_tensor)
splitFN = f.split("-")
x = splitFN[1].replace('x', '')
y = splitFN[2].replace('y','')
#print("x: " + x + " y: " + y)
target = [int(x),int(y)]
target_list.append(target)
train_data.append((img_tensor, target))
if len(train_data_list) >= 5:
train_data.append((torch.stack(train_data_list), target_list))
train_data_list = []
target_list = []
optimizer = optim.Adam(model.parameters(), lr=0.01)
def train(gen):
size = len(train_data)
count = 0
model.train()
for data, target in train_data:
data = Variable(data)
target = torch.Tensor(target)
target = Variable(target)
optimizer.zero_grad()
out = model(data)
criterion = F.binary_cross_entropy
loss = criterion(out, target)
loss.backward()
optimizer.step()
count+=1
print("Gen: {gene} Count: {counte} Percantage: {per}".format(gene=gen, counte=count, perc = (count/size)*100))
train(1)