-
Notifications
You must be signed in to change notification settings - Fork 31
Implement MedPerf Model Hello World #42
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
Draft
aristizabal95
wants to merge
15
commits into
mlcommons:master
Choose a base branch
from
aristizabal95:medperf-examples
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 2 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
d12deb3
Implement MedPerf Model Hello World
aristizabal95 0034513
Pass uppercase to hello_world funciton
aristizabal95 8f6f368
Create README
aristizabal95 d7a10d8
Correct README mlcube.yaml instructions
aristizabal95 ac24dbf
Start working on data preparator template
aristizabal95 473b769
Update template to recent conceptual changes
aristizabal95 ce9a204
Implement Data Preparator template
aristizabal95 89497e7
Update outdated model documentation
aristizabal95 f901fff
Write Data Preparator README
aristizabal95 1cb5839
README Small fixes
aristizabal95 9dcf220
added instructions to create & activate venv
sarthakpati 4b5414f
Add docker username to image name
aristizabal95 29060a5
Write metrics template
aristizabal95 a0c7b1a
Add README to metrics template
aristizabal95 c240f45
Merge pull request #1 from sarthakpati/patch-1
aristizabal95 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| name: Hello-World Medperf Model MLCube | ||
| description: MLCommons demonstration MLCube for building models for MedPerf | ||
| authors: | ||
| - {name: "MLCommons Medical Working Group"} | ||
|
|
||
| platform: | ||
| accelerator_count: 0 | ||
|
|
||
| docker: | ||
| # Image name. | ||
| image: medical-hello-world | ||
| # Docker build context relative to $MLCUBE_ROOT. Default is `build`. | ||
| build_context: "../project" | ||
| # Docker file name within docker build context, default is `Dockerfile`. | ||
| build_file: "Dockerfile" | ||
|
|
||
| tasks: | ||
| # Model MLCubes require only a single task: `infer`. | ||
| # This task takes input data, as well as configuration parameters | ||
| # and/or extra artifacts, and generates predictions on the data | ||
| infer: | ||
| parameters: | ||
| inputs: { | ||
| data_path: names.csv, # Required. Where to find the data to run predictions on | ||
| parameters_file: parameters.yaml, # Required. Helper file to provide additional arguments. Value MUST be parameters.yaml | ||
| # If you need any additional files that should | ||
| # not be included inside the mlcube image, | ||
| # add them inside `additional_files` folder | ||
| # E.g. model weights | ||
|
|
||
| # Toy Hello World example | ||
| greetings: additional_files/greetings.csv | ||
| } | ||
| outputs: { | ||
| output_path: {type: file, default: predictions.csv} # Required. Where to store predictions artifact. Value MUST be predictions.csv (This will probably be an issue) | ||
| } |
4 changes: 4 additions & 0 deletions
4
medperf/model/mlcube/workspace/additional_files/greetings.csv
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| Hello | ||
| Howdy | ||
| Greetings | ||
| Bonjour |
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| First name,Last name | ||
| Adam,Smith | ||
| John,Smith | ||
| Michael,Stevens |
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| # Here you can store any key-value arguments that should be easily modifiable | ||
| # by external users. E.g. batch_size | ||
|
|
||
| # example argument for Hello World | ||
| uppercase: false |
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| id,greeting | ||
| 0,"Hello, Adam Smith" | ||
| 1,"Hello, John Smith" | ||
| 2,"Hello, Michael Stevens" | ||
| 3,"Howdy, Adam Smith" | ||
| 4,"Howdy, John Smith" | ||
| 5,"Howdy, Michael Stevens" | ||
| 6,"Greetings, Adam Smith" | ||
| 7,"Greetings, John Smith" | ||
| 8,"Greetings, Michael Stevens" | ||
| 9,"Bonjour, Adam Smith" | ||
| 10,"Bonjour, John Smith" | ||
| 11,"Bonjour, Michael Stevens" |
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| FROM ubuntu:18.04 | ||
| MAINTAINER MLPerf MLBox Working Group | ||
|
|
||
| RUN apt-get update && \ | ||
| apt-get install -y --no-install-recommends \ | ||
| software-properties-common \ | ||
| python3-dev \ | ||
| curl && \ | ||
| rm -rf /var/lib/apt/lists/* | ||
|
|
||
| RUN add-apt-repository ppa:deadsnakes/ppa -y && apt-get update | ||
|
|
||
| RUN apt-get install python3 -y | ||
|
|
||
| RUN apt-get install python3-pip -y | ||
|
|
||
| COPY ./requirements.txt project/requirements.txt | ||
|
|
||
| RUN pip3 install --upgrade pip | ||
|
|
||
| RUN pip3 install --no-cache-dir -r project/requirements.txt | ||
|
|
||
| ENV LANG C.UTF-8 | ||
|
|
||
| COPY . /project | ||
|
|
||
| WORKDIR /project | ||
|
|
||
| ENTRYPOINT ["python3", "mlcube.py"] |
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| # Hello World Script | ||
| # | ||
| # This script is unrelated to the MLCube interface. It could be run | ||
| # independently without issues. It provides the actual implementation | ||
| # of the app. | ||
| import csv | ||
| import argparse | ||
|
|
||
| def hello_world(greetings, names, uppercase=False): | ||
| """Generates a combination of greetings and names | ||
|
|
||
| Args: | ||
| greetings (List[str]): list of greetings | ||
| names (List[str]): list of names | ||
| uppercase (bool): Wether to uppercase the whole greeting | ||
|
|
||
| Returns: | ||
| List[str]: combinations of greetings and names | ||
| """ | ||
| full_greetings = [] | ||
|
|
||
| for greeting in greetings: | ||
| for name, last_name in names: | ||
| full_greeting = f"{greeting}, {name} {last_name}" | ||
| if uppercase: | ||
| full_greeting = full_greeting.upper() | ||
| full_greetings.append(full_greeting) | ||
|
|
||
| return full_greetings | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| parser = argparse.ArgumentParser("MedPerf Model Hello World Example") | ||
| parser.add_argument('--names', dest="names", type=str, help="file containing names. CSV expected") | ||
| parser.add_argument('--uppercase', dest="uppercase", type=bool, help="wether to return uppercase greetings") | ||
| parser.add_argument('--greetings', dest="greetings", type=str, help="file containing greetings. CSV expected") | ||
| parser.add_argument('--out', dest="out", type=str, help="file to store resulting greetings") | ||
|
|
||
| args = parser.parse_args() | ||
|
|
||
| names = [] | ||
| greetings = [] | ||
|
|
||
| with open(args.names, "r") as f: | ||
| reader = csv.reader(f) | ||
| next(reader) # skip header | ||
| for row in reader: | ||
| names.append(row) | ||
|
|
||
| with open(args.greetings, "r") as f: | ||
| reader = csv.reader(f) | ||
| for row in reader: | ||
| greetings.append(row[0]) | ||
|
|
||
| full_greetings = hello_world(greetings, names, args.uppercase) | ||
|
|
||
| with open(args.out, "w") as f: | ||
| writer = csv.writer(f) | ||
| writer.writerow(["id", "greeting"]) | ||
| for idx, full_greeting in enumerate(full_greetings): | ||
| writer.writerow([idx, full_greeting]) |
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| # MLCube Entrypoint | ||
| # | ||
| # This script shows how you can bridge your app with an MLCube interface. | ||
| # MLCubes expect the entrypoint to behave like a CLI, where tasks are | ||
| # commands, and input/output parameters and command-line arguments. | ||
| # You can provide that interface to MLCube in any way you prefer. | ||
| # Here, we show a way that requires minimal intrusion to the original code, | ||
| # By running the application through subprocesses. | ||
|
|
||
| import yaml | ||
| import typer | ||
| import subprocess | ||
|
|
||
| app = typer.Typer() | ||
|
|
||
| def exec_python(cmd: str) -> None: | ||
| """Execute a python script as a subprocess | ||
|
|
||
| Args: | ||
| cmd (str): command to run as would be written inside the terminal | ||
| """ | ||
| splitted_cmd = cmd.split() | ||
| process = subprocess.Popen(splitted_cmd, cwd=".") | ||
| process.wait() | ||
|
|
||
| @app.command("infer") | ||
| def infer( | ||
| data_path: str = typer.Option(..., "--data_path"), | ||
| params_file: str = typer.Option(..., "--parameters_file"), | ||
| greetings: str = typer.Option(..., "--greetings"), | ||
| out_path: str = typer.Option(..., "--output_path") | ||
| ): | ||
| """infer task command. This is what gets executed when we run: | ||
| `mlcube run infer` | ||
|
|
||
| Args: | ||
| data_path (str): Location of the data to run inference with. Required for Medperf Model MLCubes. | ||
| params_file (str): Location of the parameters.yaml file. Required for Medperf Model MLCubes. | ||
| greetings (str): Example of an extra parameter that uses `additional_files`. | ||
| out_path (str): Location to store prediction results. Required for Medperf Model MLCubes. | ||
| """ | ||
| with open(params_file, "r") as f: | ||
| params = yaml.safe_load(f) | ||
|
|
||
| uppercase = params["uppercase"] | ||
| cmd = f"python3 app.py --names={data_path} --uppercase={uppercase} --greetings={greetings} --out={out_path}" | ||
| exec_python(cmd) | ||
|
|
||
| @app.command("hotfix") | ||
| def hotfix(): | ||
| pass | ||
|
|
||
| if __name__ == "__main__": | ||
| app() | ||
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| pyYAML | ||
| typer |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would explain that this is a workaround for the "typer" module (could be with the "click" module that typer uses).