forked from intel/ai-reference-models
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
41 changed files
with
2,555 additions
and
1 deletion.
There are no files selected for viewing
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
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
65 changes: 65 additions & 0 deletions
65
benchmarks/image_segmentation/tensorflow/3d_unet/README.md
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
# 3D U-Net | ||
|
||
This document has instructions for how to run 3D U-Net for the following | ||
modes/precisions: | ||
* [FP32 inference](#fp32-inference-instructions) | ||
|
||
## FP32 Inference Instructions | ||
|
||
1. Follow the instructions at the [3DUNet repository](https://github.com/ellisdg/3DUnetCNN) | ||
for [downloading and preprocessing the BRATS dataset](https://github.com/ellisdg/3DUnetCNN/blob/ff5953b3a407ded73a00647f5c2029e9100e23b1/README.md#tutorial-using-brats-data-and-python-3). | ||
The directory that contains the preprocessed dataset files will be | ||
passed to the launch script when running the benchmarking script. | ||
|
||
2. Clone this [intelai/models](https://github.com/IntelAI/models) | ||
repository: | ||
``` | ||
$ git clone https://github.com/IntelAI/models.git | ||
``` | ||
This repository contains the scripts that we will use for running | ||
benchmarks as well as the Intel-Optimized 3D U-Net model code. | ||
|
||
3. Download the pre-trained model from the | ||
[3DUnetCNN](https://github.com/ellisdg/3DUnetCNN/blob/ff5953b3a407ded73a00647f5c2029e9100e23b1/README.md#pre-trained-models) | ||
repository. In this example, we are using the "Original U-Net" model, | ||
trained using the BRATS 2017 data. | ||
|
||
4. Navigate to the `benchmarks` directory in your clone of the | ||
[intelai/models](https://github.com/IntelAI/models) repo from step 2. | ||
This directory contains the `launch_benchmarks.py` script, which we | ||
will use to run 3D U-Net inference. The script takes parameters that | ||
specify which model to run, as well as a path to the preprocessed BraTS | ||
data from step 1 as the `--data-location` and the path to the | ||
pretrained 3D U-Net model that was downloaded in step 3 as the | ||
`--in-graph`. | ||
|
||
To run benchmarking for throughput and latency, use the following | ||
command (replace in your `--data-location` and `--in-graph`): | ||
|
||
``` | ||
$ cd /home/<user>/intelai/models/benchmarks | ||
$ python launch_benchmark.py \ | ||
--precision fp32 \ | ||
--model-name 3d_unet \ | ||
--mode inference \ | ||
--framework tensorflow \ | ||
--docker-image intel/intel-optimized-tensorflow:1.15.2 \ | ||
--in-graph /home/<user>/tumor_segmentation_model.h5 \ | ||
--data-location /home/<user>/3dunet_data/BraTS \ | ||
--batch-size 1 \ | ||
--socket-id 0 | ||
``` | ||
|
||
Note that the `--verbose` or `--output-dir` flag can be added to the above command | ||
to get additional debug output or change the default output location. | ||
|
||
5. Below is an example tail of the log file: | ||
|
||
``` | ||
Loading pre-trained model | ||
Time spent per BATCH: ... ms | ||
Total samples/sec: ... samples/s | ||
Ran inference with batch size 1 | ||
Log location outside container: {--output-dir value}/benchmark_3d_unet_inference_fp32_20190116_234659.log | ||
``` |
5 changes: 5 additions & 0 deletions
5
benchmarks/image_segmentation/tensorflow/3d_unet/inference/fp32/config.json
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"optimization_parameters": { | ||
"KMP_AFFINITY": "granularity=fine, compact" | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
benchmarks/image_segmentation/tensorflow/3d_unet/inference/fp32/model_init.py
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Copyright (c) 2019 Intel Corporation | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
# SPDX-License-Identifier: EPL-2.0 | ||
# | ||
|
||
|
||
import os | ||
|
||
from common.base_model_init import BaseModelInitializer | ||
from common.base_model_init import set_env_var | ||
|
||
|
||
class ModelInitializer(BaseModelInitializer): | ||
""" Model initializer for 3D UNet""" | ||
|
||
def __init__(self, args, custom_args=[], platform_util=None): | ||
super(ModelInitializer, self).__init__(args, custom_args, platform_util) | ||
|
||
self.set_num_inter_intra_threads() | ||
set_env_var("OMP_NUM_THREADS", self.args.num_intra_threads) | ||
|
||
# Set KMP env vars, if they haven't already been set | ||
config_file_path = os.path.join( | ||
os.path.dirname(os.path.realpath(__file__)), "config.json") | ||
self.set_kmp_vars(config_file_path) | ||
set_env_var("KMP_HW_SUBSET", | ||
"{}c,1T".format(self.args.num_intra_threads)) | ||
script_path = os.path.join(self.args.intelai_models, self.args.mode, | ||
self.args.precision, "brats", "predict.py") | ||
|
||
# add numactl prefix to the command | ||
self.command_prefix = self.get_command_prefix(self.args.socket_id) + \ | ||
"python " + script_path | ||
|
||
# add additional args to the command | ||
self.command_prefix += \ | ||
" --inter {} --intra {} --nw 1 --nb 5 --bs {}".\ | ||
format(self.args.num_inter_threads, self.args.num_intra_threads, | ||
self.args.batch_size) | ||
|
||
def run(self): | ||
self.run_command(self.command_prefix) |
7 changes: 7 additions & 0 deletions
7
benchmarks/image_segmentation/tensorflow/3d_unet/requirements.txt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
Keras==2.2.4 | ||
numpy==1.16.1 | ||
nilearn>=0.3.0 | ||
tables==3.4.4 | ||
nibabel==2.3.3 | ||
SimpleITK===1.2.0 | ||
|
88 changes: 88 additions & 0 deletions
88
dockerfiles/model_containers/intel-tf-image-segmentation-3d-unet-fp32-inference.Dockerfile
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
# Copyright (c) 2020 Intel Corporation | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# ============================================================================ | ||
# | ||
# THIS IS A GENERATED DOCKERFILE. | ||
# | ||
# This file was assembled from multiple pieces, whose use is documented | ||
# throughout. Please refer to the TensorFlow dockerfiles documentation | ||
# for more information. | ||
|
||
ARG TENSORFLOW_IMAGE="intel/intel-optimized-tensorflow" | ||
|
||
ARG TENSORFLOW_TAG | ||
|
||
FROM ${TENSORFLOW_IMAGE}:${TENSORFLOW_TAG} | ||
|
||
ENV DEBIAN_FRONTEND=noninteractive | ||
|
||
RUN apt-get update && \ | ||
apt-get install --no-install-recommends --fix-missing -y \ | ||
libsm6 \ | ||
libxext6 \ | ||
python-tk && \ | ||
pip install requests | ||
|
||
ARG PACKAGE_DIR=model_packages | ||
|
||
ARG PACKAGE_NAME | ||
|
||
ARG MODEL_WORKSPACE | ||
|
||
# ${MODEL_WORKSPACE} and below needs to be owned by root:root rather than the current UID:GID | ||
# this allows the default user (root) to work in k8s single-node, multi-node | ||
RUN umask 002 && mkdir -p ${MODEL_WORKSPACE} && chgrp root ${MODEL_WORKSPACE} && chmod g+s+w,o+s+r ${MODEL_WORKSPACE} | ||
|
||
ADD --chown=0:0 ${PACKAGE_DIR}/${PACKAGE_NAME}.tar.gz ${MODEL_WORKSPACE} | ||
|
||
RUN chown -R root ${MODEL_WORKSPACE}/${PACKAGE_NAME} && chgrp -R root ${MODEL_WORKSPACE}/${PACKAGE_NAME} && chmod -R g+s+w ${MODEL_WORKSPACE}/${PACKAGE_NAME} && find ${MODEL_WORKSPACE}/${PACKAGE_NAME} -type d | xargs chmod o+r+x | ||
|
||
WORKDIR ${MODEL_WORKSPACE}/${PACKAGE_NAME} | ||
|
||
RUN pip install \ | ||
Keras==2.2.4 \ | ||
SimpleITK==1.2.0 \ | ||
nibabel==2.3.3 \ | ||
nilearn==0.6.2 \ | ||
numpy==1.16.1 \ | ||
tables==3.4.4 | ||
|
||
ENV USER_ID=0 | ||
|
||
ENV USER_NAME=root | ||
|
||
ENV GROUP_ID=0 | ||
|
||
ENV GROUP_NAME=root | ||
|
||
RUN apt-get update && \ | ||
apt-get install --no-install-recommends --fix-missing -y gosu | ||
|
||
RUN echo '#!/bin/bash\n\ | ||
USER_ID=$USER_ID\n\ | ||
USER_NAME=$USER_NAME\n\ | ||
GROUP_ID=$GROUP_ID\n\ | ||
GROUP_NAME=$GROUP_NAME\n\ | ||
if [[ $GROUP_NAME != root ]]; then\n\ | ||
groupadd -r -g $GROUP_ID $GROUP_NAME\n\ | ||
fi\n\ | ||
if [[ $USER_NAME != root ]]; then\n\ | ||
useradd --no-log-init -r -u $USER_ID -g $GROUP_NAME -s /bin/bash -M $USER_NAME\n\ | ||
fi\n\ | ||
exec /usr/sbin/gosu $USER_NAME:$GROUP_NAME "$@"\n '\ | ||
>> /tmp/entrypoint.sh | ||
|
||
RUN chmod u+x,g+x /tmp/entrypoint.sh | ||
|
||
ENTRYPOINT ["/tmp/entrypoint.sh"] |
21 changes: 21 additions & 0 deletions
21
models/image_segmentation/tensorflow/3d_unet/inference/fp32/LICENSE.txt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2017 David G Ellis | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
Empty file.
57 changes: 57 additions & 0 deletions
57
models/image_segmentation/tensorflow/3d_unet/inference/fp32/brats/predict.py
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import os | ||
|
||
from train import config | ||
from unet3d.prediction import run_validation_cases | ||
from unet3d.prediction import run_large_batch_validation_cases | ||
|
||
import argparse | ||
|
||
parser = argparse.ArgumentParser(description='train opts:') | ||
parser.add_argument('--bs', type=int, default=1) | ||
parser.add_argument('--intra', '--num_intra_threads', type=int, default=56) | ||
parser.add_argument('--inter', '--num_inter_threads', type=int, default=1) | ||
parser.add_argument('--warmup', '--nw', type=int, default=10) | ||
parser.add_argument('--report_interval', type=int, default=1) | ||
parser.add_argument('--nb', type=int, default=10) | ||
|
||
args = parser.parse_args() | ||
|
||
|
||
import tensorflow as tf | ||
from keras import backend as K | ||
tf_config = tf.ConfigProto(intra_op_parallelism_threads=args.intra, inter_op_parallelism_threads=args.inter) | ||
sess = tf.Session(graph=tf.get_default_graph(), config=tf_config) | ||
K.set_session(sess) | ||
|
||
def main(): | ||
prediction_dir = os.path.abspath("prediction") | ||
|
||
# with tf.contrib.tfprof.ProfileContext('./profile_dir') as pctx: | ||
if args.bs == 1: | ||
run_validation_cases(validation_keys_file=config["validation_file"], | ||
model_file=config["model_file"], | ||
training_modalities=config["training_modalities"], | ||
labels=config["labels"], | ||
hdf5_file=config["data_file"], | ||
output_label_map=True, | ||
output_dir=prediction_dir, | ||
warmup=args.warmup, | ||
report_interval=args.report_interval, | ||
batch_size=args.bs, | ||
n_batch=args.nb) | ||
else: | ||
run_large_batch_validation_cases(validation_keys_file=config["validation_file"], | ||
model_file=config["model_file"], | ||
training_modalities=config["training_modalities"], | ||
labels=config["labels"], | ||
hdf5_file=config["data_file"], | ||
output_label_map=True, | ||
output_dir=prediction_dir, | ||
batch_size=args.bs, | ||
report_interval=args.report_interval, | ||
warmup=args.warmup, | ||
n_batch=args.nb) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
Oops, something went wrong.