Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
74 commits
Select commit Hold shift + click to select a range
ddbd9b1
skelaton
OfirShechter Mar 16, 2025
64024e3
gitkeep
OfirShechter Mar 16, 2025
cc328b3
ignore
OfirShechter Mar 16, 2025
02f0108
on the way
OfirShechter Mar 16, 2025
805fc2b
dota
OfirShechter Mar 16, 2025
d4dac87
print debug
OfirShechter Mar 16, 2025
a40e5c9
debug
OfirShechter Mar 16, 2025
fa9b456
try change
OfirShechter Mar 16, 2025
e52d1ee
try
OfirShechter Mar 16, 2025
5b53c88
try changes
OfirShechter Mar 16, 2025
0aaab0b
new print
OfirShechter Mar 16, 2025
69c810c
try
OfirShechter Mar 16, 2025
e28f638
try change
OfirShechter Mar 16, 2025
e25c9a4
try alter
OfirShechter Mar 16, 2025
b9b5991
fix
OfirShechter Mar 16, 2025
f3b1f84
fix
OfirShechter Mar 16, 2025
847aee5
check
OfirShechter Mar 16, 2025
09ca151
box1
OfirShechter Mar 16, 2025
a850bd5
debuging
OfirShechter Mar 16, 2025
0683bfe
continue debug
OfirShechter Mar 16, 2025
6756280
more debug
OfirShechter Mar 16, 2025
6cd0b15
try
OfirShechter Mar 16, 2025
679913e
more try
OfirShechter Mar 16, 2025
a74388a
try
OfirShechter Mar 16, 2025
2501407
angle
OfirShechter Mar 16, 2025
935b6bc
change box
OfirShechter Mar 16, 2025
7b233fe
more fix
OfirShechter Mar 16, 2025
2fa13d9
alter convert
OfirShechter Mar 17, 2025
1931464
helpers
OfirShechter Mar 17, 2025
c13c257
on the way
OfirShechter Mar 17, 2025
a957b04
debug mse
OfirShechter Mar 17, 2025
de00c40
debug print
OfirShechter Mar 17, 2025
b50b3fd
fix
OfirShechter Mar 17, 2025
6d8bd9f
debug placeholder
OfirShechter Mar 17, 2025
a1e304a
fix shape
OfirShechter Mar 17, 2025
d76541e
remove print and tey overfit
OfirShechter Mar 17, 2025
b96bf3d
add ebug print
OfirShechter Mar 17, 2025
d950390
minor fix
OfirShechter Mar 17, 2025
030f85b
update and debug
OfirShechter Mar 17, 2025
8de5cd9
remove prints
OfirShechter Mar 17, 2025
9cd568d
no transform
OfirShechter Mar 17, 2025
abc4124
revert no transform
OfirShechter Mar 17, 2025
561fe82
continue debug
OfirShechter Mar 17, 2025
773c1ad
fix angle
OfirShechter Mar 17, 2025
cf82227
fix dataset
OfirShechter Mar 17, 2025
07a86f5
fix- plot image
OfirShechter Mar 17, 2025
23489fd
notebooks
OfirShechter Mar 17, 2025
d61c456
new loss
OfirShechter Mar 17, 2025
cc2bb8d
code
OfirShechter Mar 17, 2025
4efac5c
than activation to loss and convert
OfirShechter Mar 17, 2025
eb1aad9
try- angle-convert
OfirShechter Mar 17, 2025
deca477
convert to tensor
OfirShechter Mar 17, 2025
4521fe5
try sigmoid as it normelized
OfirShechter Mar 17, 2025
cd8cb72
debug print
OfirShechter Mar 17, 2025
3aeec67
clamp object_loss values
OfirShechter Mar 17, 2025
7ffa4d2
fix ious
OfirShechter Mar 17, 2025
92fc9a0
try update
OfirShechter Mar 17, 2025
3e7a440
change device
OfirShechter Mar 17, 2025
fa47789
update losses
OfirShechter Mar 18, 2025
a69f46b
notebook
OfirShechter Mar 18, 2025
e05a8b3
make sure device
OfirShechter Mar 18, 2025
7ef7305
tensor to device
OfirShechter Mar 18, 2025
d17f9a1
try remove device
OfirShechter Mar 18, 2025
28952af
device map
OfirShechter Mar 18, 2025
7d53450
fix dim and device
OfirShechter Mar 18, 2025
08a1ca0
remove import
OfirShechter Mar 18, 2025
3f1f5c9
specific dtype
OfirShechter Mar 18, 2025
613922b
fix dota
OfirShechter Mar 18, 2025
9344619
specific dtype
OfirShechter Mar 18, 2025
7e976d4
fix mismatch
OfirShechter Mar 18, 2025
95bcd1e
comment print
OfirShechter Mar 18, 2025
acf3103
changing
OfirShechter Mar 18, 2025
31463ee
more fix
OfirShechter Mar 18, 2025
55d8b97
current
OfirShechter Mar 18, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ nn/cocodataset/images/*
*checkpoint.pth.tar
runs/
**/video/*
!**/video/.gitkeep
!**/video/.gitkeep
nn/dotadataset/*
!nn/dotadataset/.gitkeep
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@
" load_checkpoint(model_path_base + f\"e108_b0_vgg16_{checkpoint_file}\", model, optimizer, leanring_rate, device) \n",
"\n",
"# Initialize TensorBoard writer\n",
"writer = SummaryWriter(log_dir='runs/YOLO_VGG16_F_2_classes_v1')\n",
"writer = SummaryWriter(log_dir='runs/DEBUG')\n",
"\n"
]
},
Expand Down
77 changes: 77 additions & 0 deletions nn/YOLO_VGG16_OBB/model/YOLO_VGG16_OBB.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import torch
import torch.nn as nn
import torchvision.models as models

from ...YOLO_VGG16_OBB.model.layers import CNNBlock, ResidualBlock, ScalePrediction

# Class for defining YOLOv3 model


class YOLO_VGG16_OBB(nn.Module):
def __init__(self, in_channels=3, num_classes=20):
super().__init__()
self.num_classes = num_classes
self.in_channels = in_channels

vgg16 = models.vgg16_bn(weights=models.VGG16_BN_Weights.DEFAULT)
backbone = list(vgg16.features.children())

# Layers list for YOLOv3
self.layers = nn.ModuleList([
# layers until end channels = 256
# 23 equivalent to ResidualBlock(256, num_repeats=8),
*backbone[:24],
# layers until end channels = 512
# 33 equivalent to ResidualBlock(512, num_repeats=8),
*backbone[24:34],


CNNBlock(512, 1024, kernel_size=3, stride=2, padding=1),
ResidualBlock(1024, num_repeats=4),

# large objects detection
CNNBlock(1024, 512, kernel_size=1, stride=1, padding=0),
CNNBlock(512, 1024, kernel_size=3, stride=1, padding=1),
ResidualBlock(1024, use_residual=False, num_repeats=1),
CNNBlock(1024, 512, kernel_size=1, stride=1, padding=0),
ScalePrediction(512, num_classes=num_classes),

# medium objects detection
CNNBlock(512, 256, kernel_size=1, stride=1, padding=0),
nn.Upsample(scale_factor=2),
CNNBlock(768, 256, kernel_size=1, stride=1, padding=0),
CNNBlock(256, 512, kernel_size=3, stride=1, padding=1),
ResidualBlock(512, use_residual=False, num_repeats=1),
CNNBlock(512, 256, kernel_size=1, stride=1, padding=0),
ScalePrediction(256, num_classes=num_classes),

# small objects detection
CNNBlock(256, 128, kernel_size=1, stride=1, padding=0),
nn.Upsample(scale_factor=2),
CNNBlock(384, 128, kernel_size=1, stride=1, padding=0),
CNNBlock(128, 256, kernel_size=3, stride=1, padding=1),
ResidualBlock(256, use_residual=False, num_repeats=1),
CNNBlock(256, 128, kernel_size=1, stride=1, padding=0),
ScalePrediction(128, num_classes=num_classes)
])

# Forward pass for YOLOv3 with route connections and scale predictions
def forward(self, x):
outputs = []
route_connections = []

layer_num = 0
for layer in self.layers:
if isinstance(layer, ScalePrediction):
outputs.append(layer(x))
continue
x = layer(x)
if layer_num == 23 or layer_num == 33:
route_connections.append(x)

elif isinstance(layer, nn.Upsample):
x = torch.cat([x, route_connections[-1]], dim=1)
route_connections.pop()
layer_num += 1

return outputs
73 changes: 73 additions & 0 deletions nn/YOLO_VGG16_OBB/model/layers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import torch.nn as nn

# Defining CNN Block
class CNNBlock(nn.Module):
def __init__(self, in_channels, out_channels, use_batch_norm=True, **kwargs):
super().__init__()
self.conv = nn.Conv2d(in_channels, out_channels, bias=not use_batch_norm, **kwargs)
self.bn = nn.BatchNorm2d(out_channels)
self.activation = nn.LeakyReLU(0.1)
self.use_batch_norm = use_batch_norm

def forward(self, x):
# Applying convolution
x = self.conv(x)
# Applying BatchNorm and activation if needed
if self.use_batch_norm:
x = self.bn(x)
return self.activation(x)
else:
return x

# Defining residual block
class ResidualBlock(nn.Module):
def __init__(self, channels, use_residual=True, num_repeats=1):
super().__init__()

# Defining all the layers in a list and adding them based on number of
# repeats mentioned in the design
res_layers = []
for _ in range(num_repeats):
res_layers += [
nn.Sequential(
nn.Conv2d(channels, channels // 2, kernel_size=1),
nn.BatchNorm2d(channels // 2),
nn.LeakyReLU(0.1),
nn.Conv2d(channels // 2, channels, kernel_size=3, padding=1),
nn.BatchNorm2d(channels),
nn.LeakyReLU(0.1)
)
]
self.layers = nn.ModuleList(res_layers)
self.use_residual = use_residual
self.num_repeats = num_repeats

# Defining forward pass
def forward(self, x):
for layer in self.layers:
residual = x
x = layer(x)
if self.use_residual:
x = x + residual
return x

# Defining scale prediction class
class ScalePrediction(nn.Module):
def __init__(self, in_channels, num_classes):
super().__init__()
# Defining the layers in the network
self.pred = nn.Sequential(
nn.Conv2d(in_channels, 2*in_channels, kernel_size=3, padding=1),
nn.BatchNorm2d(2*in_channels),
nn.LeakyReLU(0.1),
nn.Conv2d(2*in_channels, (num_classes + 6) * 3, kernel_size=1),
)
self.num_classes = num_classes

# Defining the forward pass and reshaping the output to the desired output
# format: (batch_size, 3, grid_size, grid_size, num_classes + 6)
def forward(self, x):
output = self.pred(x)
output = output.view(x.size(0), 3, self.num_classes + 6, x.size(2), x.size(3))
output = output.permute(0, 1, 3, 4, 2)
return output
73 changes: 73 additions & 0 deletions nn/YOLO_VGG16_OBB/model/loss.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import torch
import torch.nn as nn

from ..utils.helpers import iou

# Defining YOLO loss class


class YOLOLoss(nn.Module):
def __init__(self):
super().__init__()
self.mse = nn.MSELoss()
self.bce = nn.BCEWithLogitsLoss()
self.cross_entropy = nn.CrossEntropyLoss()
self.sigmoid = nn.Sigmoid()
self.than = nn.Tanh()

def forward(self, pred, target, anchors):
# Identifying which cells in target have objects
# and which have no objects
obj = target[..., 0] == 1
no_obj = target[..., 0] == 0

# Calculating No object loss
no_object_loss = self.bce(
(pred[..., 0:1][no_obj]), (target[..., 0:1][no_obj]),
)

# Reshaping anchors to match predictions
anchors = anchors.reshape(1, 3, 1, 1, 2)
pred_angle = torch.tanh(pred[..., 5])
# Box prediction confidence
box_preds = torch.cat([self.sigmoid(pred[..., 1:3]),
torch.exp(pred[..., 3:5]) *
anchors, pred_angle.unsqueeze(-1)
], dim=-1)
# Calculating intersection over union for prediction and target
ious = iou(box_preds[obj], target[..., 1:6][obj]).detach()
# Calculating Object loss
object_loss = self.mse(self.sigmoid(pred[..., 0:1][obj]),
ious * target[..., 0:1][obj]).clamp(0, 100)

# Predicted box coordinates
pred[..., 1:3] = self.sigmoid(pred[..., 1:3])
# Target box coordinates
target[..., 3:5] = torch.log(1e-6 + target[..., 3:5] / anchors)
# Calculating box coordinate loss

box_loss = self.mse(pred[..., 1:5][obj],
target[..., 1:5][obj])

# Calculate angle loss
angle_loss = self.mse(pred_angle[obj],
target[..., 5][obj])
# Claculating class loss
class_loss = self.cross_entropy((pred[..., 6:][obj]),
target[..., 6][obj].long())

# print('~~~~~~~~~~~~~ FIND UNCAPPED ~~~~~~~~~~~~~')
# print('box_loss:', box_loss)
# print('angle_loss:', angle_loss)
# print('object_loss:', object_loss)
# print('no_object_loss:', no_object_loss)
# print('class_loss:', class_loss)

# Total loss
return (
box_loss
+ angle_loss
+ object_loss
+ no_object_loss
+ class_loss
)
Loading