Skip to content

Commit

Permalink
ref: Update and change several files
Browse files Browse the repository at this point in the history
  • Loading branch information
yakhyo committed Nov 8, 2024
1 parent f85d2b3 commit 926744f
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 105 deletions.
4 changes: 2 additions & 2 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
'variance': [0.1, 0.2],
'clip': False,
'loc_weight': 2.0,
'milestones': [190, 220],
'epochs': 250,
'milestones': [100, 200],
'epochs': 300,
'rgb_mean': (104, 117, 123) # bgr order
}
15 changes: 3 additions & 12 deletions train.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@
import torch
import torch.nn as nn
from torch.utils.data import DataLoader
from torch.optim.lr_scheduler import PolynomialLR, StepLR

from config import cfg
from models import FaceBoxes
from layers import PriorBox, MultiBoxLoss
from utils import Augmentation, VOCDetection, WiderFaceDetection
from utils import Augmentation, WiderFaceDetection


def parse_args():
Expand All @@ -22,7 +21,7 @@ def parse_args():
# Dataset and data handling arguments
parser.add_argument(
'--train-data',
default='./data/widerface/train',
default='./data/WIDER_FACE/',
type=str,
help='Path to the training dataset directory.'
)
Expand Down Expand Up @@ -170,16 +169,8 @@ def main(params):
)

# Learning rate scheduler
# lr_scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=100, gamma=0.1)
lr_scheduler = torch.optim.lr_scheduler.MultiStepLR(optimizer, milestones=cfg['milestones'], gamma=params.gamma)

# iters_per_epoch = len(data_loader)
# lr_scheduler = PolynomialLR(
# optimizer,
# total_iters=iters_per_epoch * (params.epochs - params.lr_warmup_epochs),
# power=params.power
# )

start_epoch = 0
if params.resume:
try:
Expand Down Expand Up @@ -218,7 +209,7 @@ def main(params):

# save final model
state = model.state_dict()
torch.save(state, f'{params.save_dir}/final.pth')
torch.save(state, f'{params.save_dir}/faceboxes.pth')


if __name__ == '__main__':
Expand Down
2 changes: 1 addition & 1 deletion utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
from utils.transform import Augmentation
from utils.dataset import VOCDetection, WiderFaceDetection
from utils.dataset import WiderFaceDetection
90 changes: 2 additions & 88 deletions utils/dataset.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import torch.utils.data as data
import os
import cv2
import numpy as np
Expand Down Expand Up @@ -40,7 +41,7 @@ def __call__(self, target):
return bboxes_array


class VOCDetection(Dataset):
class WiderFaceDetection(Dataset):

def __init__(self, root: str, transform=None):
self.root = root
Expand Down Expand Up @@ -95,90 +96,3 @@ def collate_fn(batch):
targets.append(torch.from_numpy(target).float())

return torch.stack(images, 0), targets



import os
import cv2
import numpy as np

import torch
import torch.utils.data as data


class WiderFaceDetection(data.Dataset):
def __init__(self, root: str, transform=None):
self.root = root
self.transform = transform
self.image_paths = []
self.words = []
self._parse_labels(self.root)

def _parse_labels(self, root):
with open(os.path.join(root, 'label.txt'), 'r') as f:
lines = f.read().splitlines()

labels = []
for line in lines:
if line.startswith('#'):
if labels:
self.words.append(labels.copy())
labels.clear()
image_path = os.path.join(root, 'images', line[2:])
self.image_paths.append(image_path)
else:
labels.append([float(x) for x in line.split(' ')])
self.words.append(labels)

def __len__(self):
return len(self.image_paths)

def __getitem__(self, index):
image = cv2.imread(self.image_paths[index])
height, width, _ = image.shape

labels = self.words[index]
annotations = np.zeros((0, 5))
if not labels:
return torch.from_numpy(image), annotations

for label in labels:
annotation = np.zeros((1, 5))
# bbox
annotation[0, 0] = label[0] # x1
annotation[0, 1] = label[1] # y1
annotation[0, 2] = label[0] + label[2] # x2
annotation[0, 3] = label[1] + label[3] # y2
annotation[0, 4] = 1 # class idx

# landmarks
# annotation[0, 4] = label[4] # l0_x
# annotation[0, 5] = label[5] # l0_y
# annotation[0, 6] = label[7] # l1_x
# annotation[0, 7] = label[8] # l1_y
# annotation[0, 8] = label[10] # l2_x
# annotation[0, 9] = label[11] # l2_y
# annotation[0, 10] = label[13] # l3_x
# annotation[0, 11] = label[14] # l3_y
# annotation[0, 12] = label[16] # l4_x
# annotation[0, 13] = label[17] # l4_y
# annotation[0, 14] = 1 if label[4] >= 0 else -1

annotations = np.append(annotations, annotation, axis=0)

target = np.array(annotations)
if self.transform is not None:
image, target = self.transform(image, target)

return torch.from_numpy(image), target

@staticmethod
def collate_fn(batch):
images = []
targets = []
# Iterate over each data sample in the batch
for image, target in batch:
images.append(image)
targets.append(torch.from_numpy(target).float())

return torch.stack(images, 0), targets
4 changes: 2 additions & 2 deletions webcam_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ def parse_arguments():
parser.add_argument(
'-w', '--weights',
type=str,
default='./weights/Resnet34_Final.pth',
default='./weights/faceboxes.pth',
help='Path to the trained model weights'
)

# Detection settings
parser.add_argument(
'--conf-threshold',
type=float,
default=0.4,
default=0.7,
help='Confidence threshold for filtering detections'
)
parser.add_argument(
Expand Down

0 comments on commit 926744f

Please sign in to comment.