This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]), |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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 | |
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
OlderNewer