Skip to content

Instantly share code, notes, and snippets.

View Chris-hughes10's full-sized avatar

Chris Hughes Chris-hughes10

View GitHub Profile
@Chris-hughes10
Chris-hughes10 / dataset_adaptor.py
Last active July 16, 2021 09:16
Effdet-blog-dataset-adaptor
from pathlib import Path
import PIL
import numpy as np
class CarsDatasetAdaptor:
def __init__(self, images_dir_path, annotations_dataframe):
self.images_dir_path = Path(images_dir_path)
self.annotations_df = annotations_dataframe
@Chris-hughes10
Chris-hughes10 / effdet_create_model.py
Created July 16, 2021 09:29
Effdet-blog-create-model
from effdet.config.model_config import efficientdet_model_param_dict
from effdet import get_efficientdet_config, EfficientDet, DetBenchTrain
from effdet.efficientdet import HeadNet
from effdet.config.model_config import efficientdet_model_param_dict
def create_model(num_classes=1, image_size=512, architecture="tf_efficientnetv2_l"):
efficientdet_model_param_dict['tf_efficientnetv2_l'] = dict(
name='tf_efficientnetv2_l',
backbone_name='tf_efficientnetv2_l',
backbone_args=dict(drop_path_rate=0.2),
@Chris-hughes10
Chris-hughes10 / effdet_transformations.py
Created July 16, 2021 09:32
Effdet-blog-transformations
import albumentations as A
from albumentations.pytorch.transforms import ToTensorV2
def get_train_transforms(target_img_size=512):
return A.Compose(
[
A.HorizontalFlip(p=0.5),
A.Resize(height=target_img_size, width=target_img_size, p=1),
A.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
@Chris-hughes10
Chris-hughes10 / effdet_dataset.py
Last active December 28, 2021 09:42
Effdet_blog_dataset
from torch.utils.data import Dataset
class EfficientDetDataset(Dataset):
def __init__(
self, dataset_adaptor, transforms=get_valid_transforms()
):
self.ds = dataset_adaptor
self.transforms = transforms
def __getitem__(self, index):
@Chris-hughes10
Chris-hughes10 / effdet_datamodule.py
Created July 16, 2021 09:36
Effdet_blog_datamodule
from pytorch_lightning import LightningDataModule
from torch.utils.data import DataLoader
class EfficientDetDataModule(LightningDataModule):
def __init__(self,
train_dataset_adaptor,
validation_dataset_adaptor,
train_transforms=get_train_transforms(target_img_size=512),
valid_transforms=get_valid_transforms(target_img_size=512),
@Chris-hughes10
Chris-hughes10 / effdet_model_1.py
Created July 16, 2021 09:40
Effdet_blog_model_1
import torch
from pytorch_lightning import LightningModule
from pytorch_lightning.core.decorators import auto_move_data
class EfficientDetModel(LightningModule):
def __init__(
self,
num_classes=1,
img_size=512,
prediction_confidence_threshold=0.2,
@typedispatch
def predict(self, images: List):
"""
For making predictions from images
Args:
images: a list of PIL images
Returns: a tuple of lists containing bboxes, predicted_class_labels, predicted_class_confidences
"""
@Chris-hughes10
Chris-hughes10 / effdet_run_inference.py
Created July 16, 2021 09:46
Effdet_blog_inference
def _run_inference(self, images_tensor, image_sizes):
dummy_targets = self._create_dummy_inference_targets(
num_images=images_tensor.shape[0]
)
detections = self.model(images_tensor.to(self.device), dummy_targets)[
"detections"
]
(
predicted_bboxes,
@Chris-hughes10
Chris-hughes10 / effdet_aggregate_outputs.py
Created July 16, 2021 09:52
Effdet_blog_aggregate_outputs
from fastcore.basics import patch
@patch
def aggregate_prediction_outputs(self: EfficientDetModel, outputs):
detections = torch.cat(
[output["batch_predictions"]["predictions"] for output in outputs]
)
image_ids = []
from objdetecteval.metrics.coco_metrics import get_coco_stats
@patch
def validation_epoch_end(self: EfficientDetModel, outputs):
"""Compute and log training loss and accuracy at the epoch level."""
validation_loss_mean = torch.stack(
[output["loss"] for output in outputs]
).mean()