Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(gesund): refactor structure #10

Open
wants to merge 25 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
0a33086
refactored core
Xnsam Nov 5, 2024
82ccc10
added converter factory
Xnsam Nov 6, 2024
b6ddcd0
added validation and plot data
Xnsam Nov 6, 2024
ba515f3
refactored utils
Xnsam Nov 7, 2024
7c3d962
removed utils package
Xnsam Nov 7, 2024
717a7e3
refactored(gesund): refactored reference links
Xnsam Nov 8, 2024
c983423
add(_plots.py): added plot single function
Xnsam Nov 8, 2024
b8112d9
refactor(metrics): minor update
Xnsam Nov 9, 2024
f3b4636
small update
ozkanuysal Nov 11, 2024
ec34f54
classification plot
ozkanuysal Nov 11, 2024
9cea3b1
seg&od plots init
ozkanuysal Nov 11, 2024
cb60857
small update
ozkanuysal Nov 12, 2024
b83c4c5
renaming bug solved
ozkanuysal Nov 12, 2024
738f8ef
refactor & naming
ozkanuysal Nov 12, 2024
006f43b
add typing correct format types
ozkanuysal Nov 12, 2024
4aa3c4a
add ref script
ozkanuysal Nov 12, 2024
f1ce731
latest changes for testing
ozkanuysal Nov 13, 2024
61da871
basic test script
ozkanuysal Nov 13, 2024
0ee6472
remove typing mistake
ozkanuysal Nov 13, 2024
df43887
Merge pull request #18 from gesund-ai/refactor/plot_methods
Xnsam Nov 13, 2024
299637c
refactor(core): fixed references and implemented plot driver
Xnsam Nov 13, 2024
990f350
refactor(core): fixed references, test cases
Xnsam Nov 13, 2024
4dcbb5d
refactor(tests): added test case for validation class initialization
Xnsam Nov 13, 2024
f81b283
refactor(core): fixed minor issues found during testing
Xnsam Nov 15, 2024
b8b3c7f
refactored(core): plot metrics independent and validation workflow
Xnsam Nov 15, 2024
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
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,7 @@ cython_debug/
#.idea/
#

gesund/test_validation.py
.pytest_cache

*/*/__pycache__
*/__pycache__
2 changes: 1 addition & 1 deletion gesund/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from . import validation
from .validation._validation import Validation
12 changes: 0 additions & 12 deletions gesund/core.py

This file was deleted.

7 changes: 7 additions & 0 deletions gesund/core/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from ._plot import PlotData
from ._converters import *
from ._metrics import *
from ._data_loaders import DataLoader
from ._exceptions import DataLoadError, InputError, MetricCalculationError, PlotError
from ._plot import PlotData
from ._schema import UserInputData, UserInputParams, ResultData
10 changes: 10 additions & 0 deletions gesund/core/_common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"""
This file contains common functions and class that could be used across the modules and
packages

"""



def load_data():
pass
1 change: 1 addition & 0 deletions gesund/core/_converters/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .converter_factory import ConverterFactory
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
from collections import defaultdict
import pycocotools.mask as mask_utils

from gesund.utils.validation_data_utils import ValidationUtils
from gesund.core._utils import ValidationUtils

class COCOConverter:
class COCOToGesund:
"""
A class to convert annotations and predictions between COCO format and custom format
for various problem types such as classification, object detection, and segmentation.
Expand Down
23 changes: 23 additions & 0 deletions gesund/core/_converters/converter_factory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from typing import Union

from .coco_converter import COCOToGesund
from .yolo_converter import YoloToGesund


class ConverterFactory:

def get_converter(self, json_structure_type: str) -> Union[COCOToGesund, YoloToGesund]:
"""
A function to get the converter to transform the data to gesund format given the current json
structure type

:param json_structure_type: current type of the data
:type json_structure_type: str

:return: object of the converter
:rtype: Union[CocoToGesund, YoloToGesund]
"""
if json_structure_type == "coco":
return COCOToGesund()
elif json_structure_type == "yolo":
return YoloToGesund()
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Union, Dict, List
from typing import Union, Dict, List, Optional
import numpy as np


Expand Down Expand Up @@ -94,18 +94,27 @@ def _convert_annotations(self) -> dict:
}
return custom_json



class ObjectDetectionConverter:
def __init__(self, annotations: list, predictions: list, image_width: int = 512, image_height: int = 512):
def __init__(
self,
annotations: Union[list, dict],
predictions: Union[list, dict],
image_width: int,
image_height: int
):
"""
The initialization function of the class


:param annotations: list: a list of annotation in the yolo format to convert to gesund format
:param predictions: list: a list of predictions in the yolo format to convert into the gesund format
:parma image_width: int: The width of the image
:param image_height: int: The height of the image
:param annotations: a list of annotation in the yolo format to convert to gesund format
:type annotations: list
:param predictions: a list of predictions in the yolo format to convert into the gesund format
:type predictions: list
:param image_width: The width of the image
:type image_width: int
:param image_height: The height of the image
:type image_height: int

:return: None
"""
self.annotation = annotations
Expand All @@ -118,6 +127,7 @@ def _convert_predictions(self) -> dict:
A function to convert the yolo predictions to gesund predictions format

:return: a dictionary of predictions in the gesund predictions format
:rtype: dict
"""
custom_json = {}

Expand Down Expand Up @@ -165,6 +175,7 @@ def _convert_annotations(self) -> dict:
A function to convert the yolo predictions to gesund predictions format

:return: a dictionary of predictions in the gesund predictions format
:rtype: dict
"""
custom_json = {}

Expand Down Expand Up @@ -203,14 +214,23 @@ def _convert_annotations(self) -> dict:
return custom_json

class SemanticSegmentationConverter:
def __init__(self, annotations: list, predictions: list, image_width: int = 512, image_height: int = 512):
def __init__(
self,
annotations: Union[list, dict],
predictions: Union[list, dict],
image_width: int,
image_height: int):
"""
the initialization function of the class

:param annotations: list: a list of annotation in the yolo format to convert to gesund format
:param predictions: list: a list of predictions in the yolo format to convert into the gesund format
:param image_width: int: The width of the image
:param image_height: int: The height of the image
:param annotations: a list of annotation in the yolo format to convert to gesund format
:type annotations: Union[list, dict]
:param predictions: a list of predictions in the yolo format to convert into the gesund format
:type predictions: Union[list, dict]
:param image_width: The width of the image
:type image_width: int
:param image_height: The height of the image
:type image_height: int

:return: None
"""
Expand All @@ -224,6 +244,7 @@ def _convert_predictions(self) -> dict:
A function to convert the yolo predictions to gesund predictions format

:return: a list of objects in the gesund predictions format
:rtype: dict
"""
custom_json = {}

Expand Down Expand Up @@ -271,6 +292,7 @@ def _convert_annotations(self) -> dict:
A function to convert the yolo annotations to gesund predictions format

:return: a list of objects in the gesund predictions format
:rtype: dict
"""
custom_json = {}

Expand Down Expand Up @@ -324,15 +346,24 @@ def _convert_annotations(self) -> dict:
return custom_json

class InstanceSegmentationConverter:
def __init__(self, annotations: list, predictions: list, image_width: int = 512, image_height: int = 512):
def __init__(
self,
annotations: Union[list, dict],
predictions: Union[list, dict],
image_width: int,
image_height: int):
"""
The initialization function of the class


:param annotations: list: a list of annotation in the yolo format to convert to gesund format
:param predictions: list: a list of predictions in the yolo format to convert into the gesund format
:param image_width: int: The width of the image
:param image_height: int: The height of the image
:param annotations: a list of annotation in the yolo format to convert to gesund format
:type annotations: Union[list, dict]
:param predictions: a list of predictions in the yolo format to convert into the gesund format
:type predictions: Union[list, dict]
:param image_width: The width of the image
:type image_width: int
:param image_height: The height of the image
:type image_height: int

:return: None
"""
Expand All @@ -345,81 +376,76 @@ def _convert_predictions(self) -> dict:
"""
A function to convert the yolo predictions to gesund predictions format

:return: a list of objects in the gesund predictions format
:return: a dict of objects in the gesund predictions format
:rtype: dict
"""
pass

def _convert_annotations(self) -> dict:
"""
A function to convert the yolo predictions to gesund predictions format

:return: a list of objects in the gesund predictions format
:return: a dict of objects in the gesund predictions format
:rtype: dict
"""
pass


class YoloProblemTypeFactory:

class YoloToGesund:
def __init__(self, annotations: list, predictions: list, image_width: int = 512, image_height: int = 512):
def get_yolo_converter(self, problem_type: str):
"""
The initialization function of the class
A factory method to get the yolo converter


:param annotations: list: a list of annotation in the yolo format to convert to gesund format
:param predictions: list: a list of predictions in the yolo format to convert into the gesund format
:param image_width: int: The width of the image
:param image_height: int: The height of the image

:return: None
:param problem_type: type of the problem
:type problem_type: str

:return: class of the converter
:rtype: class
"""
self.annotation = annotations
self.predictions = predictions
self.image_width = image_width
self.image_height = image_height
self.classification_converter = ClassificationConverter(
annotations=annotations,
predictions=predictions,
image_width=image_width,
image_height=image_height
)
self.obj_converter = ObjectDetectionConverter(
annotations=annotations,
predictions=predictions,
image_width=image_width,
image_height=image_height
)
self.semantic_segmentation_converter = SemanticSegmentationConverter(
annotations=annotations,
predictions=predictions,
image_width=image_width,
image_height=image_height
)
self.instance_segmentation_converter = InstanceSegmentationConverter(
annotations=annotations,
predictions=predictions,
image_width=image_width,
image_height=image_height
)
if problem_type == "object_detection":
return ObjectDetectionConverter
elif problem_type == "semantic_segmentation":
return SemanticSegmentationConverter
elif problem_type == "classification":
return ClassificationConverter
else:
raise NotImplementedError

class YoloToGesund:

def run(self, problem_type: str= "object_detection", input_type: str="prediction") -> Dict:
def convert(
self,
annotation: Union[List[Dict], Dict],
prediction: Union[List[Dict], Dict],
problem_type: str,
image_width: Optional[int] = 512,
image_height: Optional[int] = 512
) -> Dict:
"""
A run method to execute the pipeline and convert the jsons
A run method to execute the pipeline and convert the given format to gesund format

:param annotation: the annotations data
:type annotation: Union[List[Dict], Dict]
:param prediction: the prediction data
:type prediction: Union[List[Dict], Dict]
:param problem_type: problem type could be classification | object detection | segmentation
:type problem_type: str
:param image_width: width of the image
:type image_width: int
:param image_height: height of the image
:type image_height: int


:param problem_type: str: object detection
:param input_type: str: to indicate if the input is prediction or annotation

:return: dictionary of converted format
:rtype: dict
"""
_class_problems = {
"classification": self.classification_converter,
"object_detection": self.obj_converter,
"semantic_segmentation": self.semantic_segmentation_converter,
"instance_segmentation": self.instance_segmentation_converter
}
_func = {
"prediction": _class_problems[problem_type]._convert_predictions,
"annotation": _class_problems[problem_type]._convert_annotations
}
return _func[input_type]()
_converter_cls = YoloProblemTypeFactory().get_yolo_converter(
problem_type=problem_type)
_converter = _converter_cls(
annotations=annotation,
predictions=prediction,
image_width=image_width,
image_height=image_height
)
return _converter._convert_annotations(), _converter._convert_predictions()

Loading