Hi.
I have a Network which should do the following task.
It gets an Images as Input. Now it should give me 2 Outputs.The first output is the cords where the mouse should move, the second output ist if it should click or not. First i thougth it could be overfitting. But through the fact that there is far more “click” (1) as “no click” (0) values I am wondering why it still always puts out 0.
My code:
import torch
import torchvision
import torchvision.transforms as transforms
import os
from PIL import Image
from CustomDataset import CustomMouseDataset,Rescale
def load_data():
# transform = transforms.Compose([transforms.ToTensor(),transforms.Normalize((0.5,0.5,0.5),(0.5,0.5,0.5))])
#
# #Load Recoreded Data
# with h5py.File('data/video_data_22_7_2018_17_46','r') as data:
# video = data['video'][()]
# mouse = data['mouse'][()]
# video = video[:50]
# mouse = mouse[:50]
transform = transforms.Compose([transforms.ToTensor()])
train_data = CustomMouseDataset('data/video_data_22_7_2018_17_46',transform)
train_loader = torch.utils.data.DataLoader(train_data,batch_size=10,shuffle=True)
return train_loader
import torch.nn as nn
import torch.nn.functional as F
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.conv1 = nn.Conv2d(1, 6, 3, 1, 1)
self.pool1 = nn.MaxPool2d(2)
self.fc1 = nn.Linear(6*16*16, 20)
self.fc2a = nn.Linear(20, 2) # Regression
self.fc2b = nn.Linear(20, 1) # Classification
def forward(self, x):
x = F.relu(self.conv1(x))
x = self.pool1(x)
x = x.view(x.size(0), -1)
x = F.relu(self.fc1(x))
x1 = self.fc2a(x)
x2 = F.log_softmax(self.fc2b(x), dim=1)
return x1, x2
def main():
pass
if __name__ == "__main__":
net = Net()
train_data = load_data()
import torch.optim as optim
criterion = nn.MSELoss()
criterion2 = nn.NLLLoss()
optimizer = optim.SGD(net.parameters(), lr = 0.001, momentum = 0.9)
#Train the Network
for epoch in range(10):
running_loss = 0.0
for i,data in enumerate(train_data,0):
inputs = data['frame']
labels = data['mouse']
target_1 = labels[:,:2]
target_2 = labels[:,2].unsqueeze(1)
# print(type(target_2))
# print(target_2.shape)
#Zero gradients Parameter
optimizer.zero_grad()
#forward + backward +optimize
output1,output2 = net(inputs)
loss1 = criterion(output1,target_1)/2000
loss2 = criterion(output2,target_2)
loss = loss1 + loss2
loss.backward()
optimizer.step()
running_loss = loss
if i % 300 == 0: # print every 2000 mini-batches
print('[%d, %5d] loss: %.3f' %
(epoch + 1, i + 1, running_loss / 5))
print(output1,output2)
running_loss = 0.0
try:
torch.save(net.state_dict(),'Model/model_save')
except:
os.mkdir("Model")
torch.save(net.state_dict(),'Model/model_save')
print('Finished Training')
Example Output:
tensor([[ 500.9624, 829.2684],
[ 442.0272, 770.7896],
[ 534.6215, 858.7812],
[ 465.2378, 793.0269],
[ 516.5006, 844.0679],
[ 512.5015, 837.9688],
[ 469.4029, 797.4636],
[ 462.6033, 787.3254],
[ 453.6012, 784.0760],
[ 503.6086, 833.6633]]) tensor([[ 0.],
[ 0.],
[ 0.],
[ 0.],
[ 0.],
[ 0.],
[ 0.],
[ 0.],
[ 0.],
[ 0.]])