Skip to content

Commit

Permalink
Add model and ground_truth tarball examples
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisvanrun committed Jan 6, 2025
1 parent 0652ba8 commit a6afa39
Show file tree
Hide file tree
Showing 16 changed files with 88 additions and 14 deletions.
6 changes: 6 additions & 0 deletions grand_challenge_forge/forge.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ def generate_example_algorithm(
output_path=algorithm_path,
context={
"image_tag": f"example-algorithm-{context['phase']['slug']}",
"tarball_dirname": "model",
"tarball_extraction_dir": "/opt/ml/model/",
"_no_gpus": context.get("_no_gpus", False),
},
)
Expand Down Expand Up @@ -196,6 +198,8 @@ def generate_example_evaluation(
output_path=evaluation_path,
context={
"image_tag": f"example-evaluation-{context['phase']['slug']}",
"tarball_dirname": "ground_truth",
"tarball_extraction_dir": "/opt/ml/input/data/ground_truth/",
"_no_gpus": context.get("_no_gpus", False),
},
)
Expand Down Expand Up @@ -293,6 +297,8 @@ def generate_algorithm_template(
output_path=template_path,
context={
"image_tag": algorithm_slug,
"tarball_dirname": "model",
"tarball_extraction_dir": "/opt/ml/model/",
"_no_gpus": context.get("_no_gpus", False),
},
)
Expand Down
12 changes: 10 additions & 2 deletions grand_challenge_forge/partials/algorithm-template/inference.py.j2
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import numpy

INPUT_PATH = Path("/input")
OUTPUT_PATH = Path("/output")
RESOURCE_PATH = Path("resources")

{% if "__should_fail" in algorithm -%} 1/0 {%- endif %}

Expand All @@ -59,7 +58,16 @@ def run():
# Process the inputs: any way you'd like
_show_torch_cuda_info()

with open(RESOURCE_PATH / "some_resource.txt", "r") as f:
# Some additional resources might be required, place these in one of two locations.

# First location: part of the Docker-container image: resources/
resource_dir = Path("/opt/app/resources")
with open(resource_dir / "some_resource.txt", "r") as f:
print(f.read())

# Second location: part of the model tarball
model_dir = Path("/opt/ml/model")
with open(model_dir / "a_tarball_subdirectory" / "some_tarball_resource.txt", "r") as f:
print(f.read())

# TODO: add your custom inference here
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
You can upload your method's model separately from the container image as a tarball (.tar.gz). Alternatively, you can include it in the container-image build by adding it to the `resources/`.

A tarball is easier to update than the entire container image.

If provided, the tarball will be extracted to `/opt/ml/model/` at runtime.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Inside a_tarball_subdirectory/some_tarball_resource.txt
This might be some resource you would require to do a forward pass.
For instance: some weights for your model.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
With the instructions in the Dockerfile, the contents of this `resources/` directory will be included in the Docker image when build.
21 changes: 18 additions & 3 deletions grand_challenge_forge/partials/docker-bash-scripts/do_save.sh.j2
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,23 @@ formatted_build_info=$(echo $build_timestamp | sed -E 's/(.*)T(.*)\..*Z/\1_\2/'
# Set the output filename with timestamp and build information
output_filename="${SCRIPT_DIR}/${DOCKER_IMAGE_TAG}_${formatted_build_info}.tar.gz"

# Save the Docker container and gzip it
echo "Saving the container as ${output_filename}. This can take a while."
# Save the Docker-container image and gzip it
echo "==+=="
echo "Saving the container image as ${output_filename}. This can take a while."

echo ""

docker save "$DOCKER_IMAGE_TAG" | gzip -c > "$output_filename"
echo "Container image saved as ${output_filename}"
echo "==+=="

# Create the tarbal
echo "==+=="
output_tarball_name="${SCRIPT_DIR}/{{ tarball_dirname }}.tar.gz"
echo "Creating the optional tarball as ${output_tarball_name}. This can take a while."

echo ""

echo "Container saved as ${output_filename}"
tar -czf $output_tarball_name -C "${SCRIPT_DIR}/{{ tarball_dirname }}" .
echo "(Optional) Uploadable tarball was created as ${output_tarball_name}"
echo "==+=="
11 changes: 10 additions & 1 deletion grand_challenge_forge/partials/example-algorithm/inference.py.j2
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,16 @@ def run():
# Process the inputs: any way you'd like
_show_torch_cuda_info()

with open(RESOURCE_PATH / "some_resource.txt", "r") as f:
# Some additional resources might be required, place these in one of two locations.

# First location: part of the Docker-container image: resources/
resource_dir = Path("/opt/app/resources")
with open(resource_dir / "some_resource.txt", "r") as f:
print(f.read())

# Second location: part of the model tarball
model_dir = Path("/opt/ml/model")
with open(model_dir / "a_tarball_subdirectory" / "some_tarball_resource.txt", "r") as f:
print(f.read())

# For now, let us make bogus predictions
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
You can upload your method's model separately from the container image as a tarball (.tar.gz). Alternatively, you can include it in the container-image build by adding it to the `resources/`.

A tarball is easier to update than the entire container image.

If provided, the tarball will be extracted to `/opt/ml/model/` at runtime.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Inside a_tarball_subdirectory/some_tarball_resource.txt
This might be some resource you would require to do a forward pass.
For instance: some weights for your model.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
With the instructions in the Dockerfile, the contents of this `resources/` directory will be included in the Docker image when build.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ USER user
WORKDIR /opt/app

COPY --chown=user:user requirements.txt /opt/app/
COPY --chown=user:user ground_truth /opt/app/ground_truth
COPY --chown=user:user resources /opt/app/resources

# You can add any Python dependencies to requirements.txt
RUN python -m pip install \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@ from helpers import run_prediction_processing, tree

INPUT_DIRECTORY = Path("/input")
OUTPUT_DIRECTORY = Path("/output")
GROUND_TRUTH_DIRECTORY = Path("ground_truth")



{% if "__should_fail" in phase -%} 1/0 {%- endif %}

Expand Down Expand Up @@ -104,9 +101,21 @@ def process(job):
{%- endfor %}

# Fourthly, load your ground truth
# Include it in your evaluation container by placing it in ground_truth/
with open(GROUND_TRUTH_DIRECTORY / "some_resource.txt", "r") as f:
report += f.read()
# Include it in your evaluation-method container by placing it one of two locations:

# First location: part of the Docker-container image: resources/
resource_dir = Path("/opt/app/resources")
with open(resource_dir / "some_resource.txt", "r") as f:
truth = f.read()
report += truth


# Second location: part of the ground-truth tarball
ground_truth_dir = Path("/opt/ml/input/data/ground_truth")
with open(ground_truth_dir / "a_tarball_subdirectory" / "some_tarball_resource.txt", "r") as f:
truth = f.read()
report += truth

print(report)

# TODO: compare the results to your ground truth and compute some metrics
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
You can upload your method's ground truth separately from the container image as a tarball (.tar.gz). Alternatively, you can include it in the container-image build by adding it to the `resources/`.

A tarball is easier to update than the entire container image.

If provided, the tarball will be extracted to `/opt/ml/input/data/ground_truth/` at runtime.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Inside a_tarball_subdirectory/some_tarball_resource.txt
This might be some resource you would require to evaluate a submission.
For instance: some ground truth for each image for your model.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
With the instructions in the Dockerfile, the contents of this `resources/` directory will be included in the Docker image when build.
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# Inside ground_truth/some_resource.txt
# Inside resources/some_resource.txt
This might be some resource you would require to evaluate a submission.
For instance: some ground truth for each image for your model.

0 comments on commit a6afa39

Please sign in to comment.