loss: 3.7
This commit is contained in:
@@ -15,7 +15,7 @@ import torch.nn.functional as F
|
|||||||
from production.yolo_model_production import convert_prediction
|
from production.yolo_model_production import convert_prediction
|
||||||
|
|
||||||
def view_data(dataset):
|
def view_data(dataset):
|
||||||
dataloader = DataLoader(dataset=dataset, batch_size=1, shuffle=False)
|
dataloader = DataLoader(dataset=dataset, batch_size=1, shuffle=True)
|
||||||
for images, labels in iter(dataloader):
|
for images, labels in iter(dataloader):
|
||||||
for batch in range(images.shape[0]):
|
for batch in range(images.shape[0]):
|
||||||
image = images[batch]
|
image = images[batch]
|
||||||
@@ -112,16 +112,16 @@ def train_yolo():
|
|||||||
BATCH_SIZE = 64
|
BATCH_SIZE = 64
|
||||||
|
|
||||||
dataset = YoloDataset(
|
dataset = YoloDataset(
|
||||||
image_dir="data/faces_2/train",
|
image_dir="data/faces_scenery/train",
|
||||||
annotation_path="data/faces_2/train/_annotations.coco.json",
|
annotation_path="data/faces_scenery/train/_annotations.coco.json",
|
||||||
img_size=IMAGE_SIZE,
|
img_size=IMAGE_SIZE,
|
||||||
transform=True,
|
transform=True,
|
||||||
grid=GRID
|
grid=GRID
|
||||||
)
|
)
|
||||||
|
|
||||||
dataset_valid = YoloDataset(
|
dataset_valid = YoloDataset(
|
||||||
image_dir="data/faces_2/test",
|
image_dir="data/faces_scenery/test",
|
||||||
annotation_path="data/faces_2/test/_annotations.coco.json",
|
annotation_path="data/faces_scenery/test/_annotations.coco.json",
|
||||||
img_size=IMAGE_SIZE,
|
img_size=IMAGE_SIZE,
|
||||||
transform=True,
|
transform=True,
|
||||||
grid=GRID
|
grid=GRID
|
||||||
@@ -140,7 +140,7 @@ def train_yolo():
|
|||||||
loss_module = YoloLoss()
|
loss_module = YoloLoss()
|
||||||
|
|
||||||
|
|
||||||
#view_data(dataset)
|
#view_data(dataset_valid)
|
||||||
#exit()
|
#exit()
|
||||||
|
|
||||||
|
|
||||||
@@ -152,8 +152,8 @@ def train_yolo():
|
|||||||
#exit()
|
#exit()
|
||||||
|
|
||||||
|
|
||||||
use_webcam(GRID, IMAGE_SIZE)
|
#use_webcam(GRID, IMAGE_SIZE)
|
||||||
exit()
|
#exit()
|
||||||
|
|
||||||
|
|
||||||
train(model=model, loss_module=loss_module, train_loader=train_loader, val_loader=val_loader,
|
train(model=model, loss_module=loss_module, train_loader=train_loader, val_loader=val_loader,
|
||||||
|
|||||||
@@ -48,7 +48,7 @@ class YoloDataset(Dataset):
|
|||||||
self.image_ids = list(self.coco.imgs.keys())
|
self.image_ids = list(self.coco.imgs.keys())
|
||||||
self.img_size = img_size
|
self.img_size = img_size
|
||||||
self.grid = grid
|
self.grid = grid
|
||||||
self.num_classes = len(self.coco.cats)-1
|
self.num_classes = len(self.coco.cats)
|
||||||
self.toTensor = transforms.ToTensor()
|
self.toTensor = transforms.ToTensor()
|
||||||
|
|
||||||
if transform:
|
if transform:
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ class YoloLoss(nn.Module):
|
|||||||
def __init__(self):
|
def __init__(self):
|
||||||
super(YoloLoss, self).__init__()
|
super(YoloLoss, self).__init__()
|
||||||
|
|
||||||
def forward(self, predictions, targets, lambda_coord=1, lambda_noobj=1):
|
def forward(self, predictions, targets):
|
||||||
pred_boxes = predictions[..., :4]
|
pred_boxes = predictions[..., :4]
|
||||||
pred_conf = predictions[..., 4]
|
pred_conf = predictions[..., 4]
|
||||||
pred_classes = predictions[..., 5:]
|
pred_classes = predictions[..., 5:]
|
||||||
@@ -27,8 +27,6 @@ class YoloLoss(nn.Module):
|
|||||||
else:
|
else:
|
||||||
box_loss = torch.tensor(0.0, device=predictions.device)
|
box_loss = torch.tensor(0.0, device=predictions.device)
|
||||||
|
|
||||||
box_loss = lambda_coord * box_loss
|
|
||||||
|
|
||||||
|
|
||||||
obj_loss = F.mse_loss(
|
obj_loss = F.mse_loss(
|
||||||
pred_conf[obj_mask],
|
pred_conf[obj_mask],
|
||||||
@@ -42,8 +40,6 @@ class YoloLoss(nn.Module):
|
|||||||
reduction="mean"
|
reduction="mean"
|
||||||
) if noobj_mask.any() else torch.tensor(0.0, device=predictions.device)
|
) if noobj_mask.any() else torch.tensor(0.0, device=predictions.device)
|
||||||
|
|
||||||
noobj_loss = lambda_noobj * noobj_loss
|
|
||||||
|
|
||||||
|
|
||||||
class_loss = F.binary_cross_entropy_with_logits(
|
class_loss = F.binary_cross_entropy_with_logits(
|
||||||
pred_classes[obj_mask],
|
pred_classes[obj_mask],
|
||||||
|
|||||||
@@ -80,10 +80,7 @@ class Yolo_model(nn.Module):
|
|||||||
|
|
||||||
def forward(self, x):
|
def forward(self, x):
|
||||||
x = self.model(x).permute(0, 2, 3, 1)
|
x = self.model(x).permute(0, 2, 3, 1)
|
||||||
center = F.sigmoid(x[..., :2])
|
return F.sigmoid(x)
|
||||||
size = torch.exp(x[..., 2:4])
|
|
||||||
conf_class = F.sigmoid(x[..., 4:])
|
|
||||||
return torch.cat([center, size, conf_class], dim=3)
|
|
||||||
|
|
||||||
|
|
||||||
def train(model, loss_module, train_loader, val_loader, optimizer, SAVE_PATH, model_name, saving=True):
|
def train(model, loss_module, train_loader, val_loader, optimizer, SAVE_PATH, model_name, saving=True):
|
||||||
|
|||||||
Reference in New Issue
Block a user