Skip to content

Commit

Permalink
Add '3d-unet' to release branch
Browse files Browse the repository at this point in the history
  • Loading branch information
ashahba committed Feb 5, 2021
1 parent eda85b9 commit 98196ef
Show file tree
Hide file tree
Showing 41 changed files with 2,555 additions and 1 deletion.
1 change: 1 addition & 0 deletions benchmarks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ dependencies to be installed:
| Image Recognition | TensorFlow | [ResNet 50](https://arxiv.org/pdf/1512.03385.pdf) | Inference | Model Containers: [Int8](https://software.intel.com/content/www/us/en/develop/articles/containers/resnet50-int8-inference-tensorflow-container.html) [FP32](https://software.intel.com/content/www/us/en/develop/articles/containers/resnet50-fp32-inference-tensorflow-container.html) <br> Model Packages: [Int8](https://software.intel.com/content/www/us/en/develop/articles/containers/resnet50-int8-inference-tensorflow-model.html) [FP32](https://software.intel.com/content/www/us/en/develop/articles/containers/resnet50-fp32-inference-tensorflow-model.html) | [Int8](image_recognition/tensorflow/resnet50/README.md#int8-inference-instructions) [FP32](image_recognition/tensorflow/resnet50/README.md#fp32-inference-instructions) |
| Image Recognition | TensorFlow | [ResNet 50v1.5](https://github.com/tensorflow/models/tree/master/official/resnet) | Inference | Model Containers: [Int8](https://software.intel.com/content/www/us/en/develop/articles/containers/resnet50v1-5-int8-inference-tensorflow-container.html) [FP32](https://software.intel.com/content/www/us/en/develop/articles/containers/resnet50v1-5-fp32-inference-container.html) [BFloat16**](https://software.intel.com/content/www/us/en/develop/articles/containers/resnet50v1-5-bfloat16-inference-tensorflow-container.html) <br> Model Packages: [Int8](https://software.intel.com/content/www/us/en/develop/articles/containers/resnet50v1-5-int8-inference-tensorflow-model.html) [FP32](https://software.intel.com/content/www/us/en/develop/articles/containers/resnet50v1-5-fp32-inference-model-package.html) [BFloat16**](https://software.intel.com/content/www/us/en/develop/articles/containers/resnet50v1-5-bfloat16-inference-tensorflow-model.html) | [Int8](image_recognition/tensorflow/resnet50v1_5/README.md#int8-inference-instructions) [FP32](image_recognition/tensorflow/resnet50v1_5/README.md#fp32-inference-instructions) [BFloat16**](image_recognition/tensorflow/resnet50v1_5/README.md#bfloat16-inference-instructions) |
| Image Recognition | TensorFlow | [ResNet 50v1.5](https://github.com/tensorflow/models/tree/master/official/resnet) | Training | Model Containers: [FP32](https://software.intel.com/content/www/us/en/develop/articles/containers/resnet50v1-5-fp32-training-tensorflow-container.html) [BFloat16**](https://software.intel.com/content/www/us/en/develop/articles/containers/resnet50v1-5-bfloat16-training-tensorflow-container.html) <br> Model Packages: [FP32](https://software.intel.com/content/www/us/en/develop/articles/containers/resnet50v1-5-fp32-training-tensorflow-model.html) [BFloat16**](https://software.intel.com/content/www/us/en/develop/articles/containers/resnet50v1-5-bfloat16-training-tensorflow-model.html) | [FP32](image_recognition/tensorflow/resnet50v1_5/README.md#fp32-training-instructions) [BFloat16**](image_recognition/tensorflow/resnet50v1_5/README.md#bfloat16-training-instructions) |
| Image Segmentation | TensorFlow | [3D U-Net](https://arxiv.org/pdf/1606.06650.pdf) | Inference | | [FP32](image_segmentation/tensorflow/3d_unet/README.md) |
| Image Segmentation | TensorFlow | [UNet](https://arxiv.org/pdf/1606.06650.pdf) | Inference | Model Containers: [FP32](https://software.intel.com/content/www/us/en/develop/articles/containers/unet-fp32-inference-tensorflow-container.html) <br> Model Packages: [FP32](https://software.intel.com/content/www/us/en/develop/articles/containers/unet-fp32-inference-tensorflow-model.html) | [FP32](image_segmentation/tensorflow/unet/README.md#fp32-inference-instructions) |
| Image Segmentation | TensorFlow | [MaskRCNN](https://arxiv.org/abs/1703.06870) | Inference | Model Containers: [FP32](https://software.intel.com/content/www/us/en/develop/articles/containers/mask-rcnn-fp32-inference-tensorflow-container.html) <br> Model Packages: [FP32](https://software.intel.com/content/www/us/en/develop/articles/containers/mask-rcnn-fp32-inference-tensorflow-model.html) | [FP32](image_segmentation/tensorflow/maskrcnn/README.md#fp32-training-instructions) |
| Language Modeling | TensorFlow | [BERT](https://arxiv.org/pdf/1810.04805.pdf) | Inference | Model Containers: [FP32](https://software.intel.com/content/www/us/en/develop/articles/containers/bert-large-fp32-inference-tensorflow-container.html) <br> Model Packages: [FP32](https://software.intel.com/content/www/us/en/develop/articles/containers/bert-large-fp32-inference-tensorflow-model.html) | [FP32](language_modeling/tensorflow/bert_large/README.md#fp32-inference-instructions) [BFloat16**](language_modeling/tensorflow/bert_large/README.md#bfloat16-inference-instructions) |
Expand Down
18 changes: 17 additions & 1 deletion benchmarks/common/tensorflow/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,20 @@ function add_calibration_arg() {
echo "${calibration_arg}"
}

# 3D UNet model
function 3d_unet() {
if [[ ${PRECISION} == "fp32" ]] && [[ ${MODE} == "inference" ]]; then
if [[ ${NOINSTALL} != "True" ]]; then
pip install -r "${MOUNT_BENCHMARK}/${USE_CASE}/${FRAMEWORK}/${MODEL_NAME}/requirements.txt"
fi
export PYTHONPATH=${PYTHONPATH}:${MOUNT_INTELAI_MODELS_SOURCE}/inference/fp32
PYTHONPATH=${PYTHONPATH} CMD=${CMD} run_model
else
echo "${PRECISION} ${MODE} is not supported for ${MODEL_NAME}"
exit 1
fi
}

#BERT model
function bert() {
if [ ${PRECISION} == "fp32" ]; then
Expand Down Expand Up @@ -1334,7 +1348,9 @@ function wide_deep_large_ds() {
LOGFILE=${OUTPUT_DIR}/${LOG_FILENAME}

MODEL_NAME=$(echo ${MODEL_NAME} | tr 'A-Z' 'a-z')
if [ ${MODEL_NAME} == "bert" ]; then
if [ ${MODEL_NAME} == "3d_unet" ]; then
3d_unet
elif [ ${MODEL_NAME} == "bert" ]; then
bert
elif [ ${MODEL_NAME} == "dcgan" ]; then
dcgan
Expand Down
65 changes: 65 additions & 0 deletions benchmarks/image_segmentation/tensorflow/3d_unet/README.md
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
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"optimization_parameters": {
"KMP_AFFINITY": "granularity=fine, compact"
}
}
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)
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

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"]
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.
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()
Loading

0 comments on commit 98196ef

Please sign in to comment.