Skip to content

Commit

Permalink
Clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
michal-lightly committed Nov 10, 2023
1 parent f78a4cb commit f238fd1
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 84 deletions.
53 changes: 27 additions & 26 deletions example.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,32 @@
# To run this example please download the PascalVOC 2007 dataset first:
#
# wget http://host.robots.ox.ac.uk/pascal/VOC/voc2007/VOCtrainval_06-Nov-2007.tar
# tar -xvf VOCtrainval_06-Nov-2007.tar

from pathlib import Path

from labelformat.formats import YOLOv8ObjectDetectionInput
from labelformat.formats import PascalVOCObjectDetectionInput

from lightly_insights import analyze, present


def main() -> None:
# Analyze an image folder.
image_folder = Path("/Users/michal/datasets/aquarium.v2-release.yolov8/test/images")
image_analysis = analyze.analyze_images(image_folder=image_folder)

# Analyze object detections.
input_file = Path("/Users/michal/datasets/aquarium.v2-release.yolov8/data.yaml")
label_input = YOLOv8ObjectDetectionInput(
input_file=input_file,
input_split="test",
)
od_analysis = analyze.analyze_object_detections(label_input=label_input)

# Create HTML report.
output_folder = Path("/Users/michal/tmp/lightly_insights_output")
present.create_html_report(
output_folder=output_folder,
image_analysis=image_analysis,
od_analysis=od_analysis,
)


if __name__ == "__main__":
main()
# Analyze an image folder.
image_analysis = analyze.analyze_images(
image_folder=Path("./VOCdevkit/VOC2007/JPEGImages")
)

# Analyze object detections.
label_input = PascalVOCObjectDetectionInput(
input_folder=Path("./VOCdevkit/VOC2007/Annotations"),
category_names=(
"person,bird,cat,cow,dog,horse,sheep,aeroplane,bicycle,boat,bus,car,"
"motorbike,train,bottle,chair,diningtable,pottedplant,sofa,tvmonitor"
),
)
od_analysis = analyze.analyze_object_detections(label_input=label_input)

# Create HTML report.
present.create_html_report(
output_folder=Path("./html_report"),
image_analysis=image_analysis,
od_analysis=od_analysis,
)
31 changes: 0 additions & 31 deletions example_2.py

This file was deleted.

5 changes: 1 addition & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ build-backend = "poetry.core.masonry.api"
name = "lightly-insights"
version = "0.1.0"
authors = ["Lightly.ai"]
description = "Easily get basic insights about your ML dataset."
description = "Get quick insights about your ML dataset."
readme = "README.md"
license = "MIT"

Expand All @@ -29,9 +29,6 @@ build = "*"
twine = "*"
types-Pillow = "*"

# [tool.poetry.scripts]
# labelformat = "lightly_insights.cli.cli:main"

[tool.pytest.ini_options]
pythonpath = [
".", "src/"
Expand Down
1 change: 1 addition & 0 deletions src/lightly_insights/analyze.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ def analyze_object_detections(
total_data.heatmap[int(y1) : int(y2), int(x1) : int(x2)] += 1
class_datum.heatmap[int(y1) : int(y2), int(x1) : int(x2)] += 1

# Sample images.
if len(class_datum.sample_filenames) < 4:
class_datum.sample_filenames.append(label.image.filename)

Expand Down
2 changes: 0 additions & 2 deletions src/lightly_insights/main.py

This file was deleted.

13 changes: 1 addition & 12 deletions src/lightly_insights/plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,6 @@ def create_object_plots(
objects_per_image_path = plot_folder / "objects_per_image.png"
heatmap_path = plot_folder / "heatmap.png"

# TODO: Remove.
if plot_folder.name != "plots":
return PlotPaths(
object_sizes_abs=str(object_sizes_abs_path.relative_to(output_folder)),
object_sizes_rel=str(object_sizes_rel_path.relative_to(output_folder)),
side_length_avg=str(side_length_avg_path.relative_to(output_folder)),
rel_area=str(rel_area_path.relative_to(output_folder)),
objects_per_image=str(objects_per_image_path.relative_to(output_folder)),
heatmap=str(heatmap_path.relative_to(output_folder)),
)

# Bucket by multiples of 20px.
size_histogram_abs = Counter(
[
Expand Down Expand Up @@ -131,7 +120,7 @@ def create_object_plots(

def width_heigth_pixels_plot(
output_file: Path,
size_histogram: Counter[Tuple[float, float]],
size_histogram: Union[Counter[Tuple[float, float]], Counter[Tuple[int, int]]],
title: str,
) -> None:
# Image size plot.
Expand Down
11 changes: 7 additions & 4 deletions src/lightly_insights/present.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

logger = logging.getLogger(__name__)
static_folder = Path(__file__).parent / "static"
template_folder = Path(__file__).parent / "templates"


@dataclass(frozen=True)
Expand Down Expand Up @@ -81,7 +82,7 @@ def create_html_report(

# Setup Jinja2 environment
env = Environment(
loader=FileSystemLoader(searchpath="./src/lightly_insights/templates"),
loader=FileSystemLoader(searchpath=template_folder),
undefined=StrictUndefined,
)
template = env.get_template("report.html")
Expand All @@ -90,7 +91,7 @@ def create_html_report(
html_output = template.render(report_data)

# Write the HTML to file
html_output_path = output_folder / "report.html"
html_output_path = output_folder / "index.html"
html_output_path.write_text(html_output)

# Copy static files.
Expand All @@ -99,6 +100,8 @@ def create_html_report(
shutil.rmtree(output_static_folder, ignore_errors=True)
shutil.copytree(src=static_folder, dst=output_static_folder)

logger.info(f"Successfully created HTML report: {html_output_path}")


def _get_image_insights(
output_folder: Path,
Expand All @@ -107,14 +110,14 @@ def _get_image_insights(
# Image size plot.
plots.width_heigth_pixels_plot(
output_file=output_folder / "image_size_plot.png",
size_histogram=image_analysis.image_sizes, # type: ignore[arg-type]
size_histogram=image_analysis.image_sizes,
title="Image Sizes",
)

# Sample images.
sample_folder = output_folder / "sample"
sample_folder.mkdir(parents=True, exist_ok=True)
sample_images = []
# TODO: Do this more efficiently.
rng = random.Random(42)
selection = rng.sample(sorted(list(image_analysis.filename_set)), k=8)
for filename in selection:
Expand Down
7 changes: 2 additions & 5 deletions tests/test_main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
from lightly_insights import main


def test_main() -> None:
main.main()
def test() -> None:
# Testing infrasctructure is set up!
assert True

0 comments on commit f238fd1

Please sign in to comment.