Skip to content

Commit

Permalink
add image_classification benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
manyiw99 committed Aug 9, 2023
1 parent 57909a9 commit 36d07ca
Show file tree
Hide file tree
Showing 15 changed files with 50,118 additions and 0 deletions.
Empty file.
Empty file.
52 changes: 52 additions & 0 deletions benchmarks/image_classification/docker/Dockerfile.cpu
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
FROM ubuntu:16.04

ENV PYTHON_VERSION=3.7
ENV LANG C.UTF-8
ENV LC_ALL C.UTF-8
ENV PATH /opt/anaconda3/bin:$PATH

WORKDIR /root
ENV HOME /root

RUN apt-get update

RUN apt-get install -y --no-install-recommends \
git \
build-essential \
software-properties-common \
ca-certificates \
wget \
curl \
htop \
zip \
unzip

RUN cd /opt && \
wget --quiet https://repo.anaconda.com/miniconda/Miniconda3-4.6.14-Linux-x86_64.sh -O miniconda.sh && \
/bin/bash ./miniconda.sh -b -p /opt/anaconda3 && \
rm miniconda.sh && \
/opt/anaconda3/bin/conda clean -tipsy && \
ln -s /opt/anaconda3/etc/profile.d/conda.sh /etc/profile.d/conda.sh && \
echo ". /opt/anaconda3/etc/profile.d/conda.sh" >> ~/.bashrc && \
echo "conda activate base" >> ~/.bashrc && \
conda config --set always_yes yes --set changeps1 no

RUN conda install pytorch-cpu torchvision-cpu -c pytorch
RUN pip install --upgrade pip
RUN pip install cmake
RUN pip install future pillow onnx opencv-python-headless tensorflow onnxruntime
RUN pip install Cython && pip install pycocotools
RUN pip install protobuf==3.20.*
RUN cd /tmp && \
git clone --recursive https://github.com/manyiw99/mlperf_inference && \
cd mlperf_inference/vision/classification_and_detection/data_imagenet && \
wget --quiet https://image-net.org/data/ILSVRC/2012/ILSVRC2012_img_val.tar && \
tar xf ILSVRC2012_img_val.tar && \
cd ../models && \
wget --quiet https://zenodo.org/record/2535873/files/resnet50_v1.pb && \
cd ../../../loadgen && \
pip install pybind11 && \
CFLAGS="-std=c++14" python setup.py install && \
rm -rf mlperf

ENTRYPOINT ["/bin/bash", "/tmp/mlperf_inference/vision/classification_and_detection/run_helper.sh"]
Empty file.
66 changes: 66 additions & 0 deletions benchmarks/image_classification/python/backend_tf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
"""
tensorflow backend (https://github.com/tensorflow/tensorflow)
"""

# pylint: disable=unused-argument,missing-docstring,useless-super-delegation

import tensorflow as tf
from tensorflow import dtypes
from tensorflow.python.tools.optimize_for_inference_lib import optimize_for_inference

import os


class BackendTensorflow():
def __init__(self):
# super(BackendTensorflow, self).__init__()
self.inputs = []
self.outputs = []

def version(self):
return tf.__version__ + "/" + tf.__git_version__

def name(self):
return "tensorflow"

def image_format(self):
# By default tensorflow uses NHWC (and the cpu implementation only does NHWC)
return "NHWC"

def load(self, model_path, inputs=None, outputs=None):
# there is no input/output meta data i the graph so it need to come from config.

if not inputs:
raise ValueError("BackendTensorflow needs inputs")
if not outputs:
raise ValueError("BackendTensorflow needs outputs")
self.outputs = outputs
self.inputs = inputs

infer_config = tf.compat.v1.ConfigProto()
infer_config.intra_op_parallelism_threads = int(os.environ['TF_INTRA_OP_PARALLELISM_THREADS']) \
if 'TF_INTRA_OP_PARALLELISM_THREADS' in os.environ else os.cpu_count()
infer_config.inter_op_parallelism_threads = int(os.environ['TF_INTER_OP_PARALLELISM_THREADS']) \
if 'TF_INTER_OP_PARALLELISM_THREADS' in os.environ else os.cpu_count()
infer_config.use_per_session_threads = 1

# TODO: support checkpoint and saved_model formats?
graph_def = tf.compat.v1.GraphDef()
with tf.compat.v1.gfile.FastGFile(model_path, "rb") as f:
graph_def.ParseFromString(f.read())
try:
optimized_graph_def = optimize_for_inference(graph_def, [item.split(':')[0] for item in inputs],
[item.split(':')[0] for item in outputs], dtypes.float32.as_datatype_enum, False)
g = tf.compat.v1.import_graph_def(optimized_graph_def, name='')
except ValueError:
try:
optimized_graph_def = optimize_for_inference(graph_def, [item.split(':')[0] for item in inputs],
[item.split(':')[0] for item in outputs], dtypes.uint8.as_datatype_enum, False)
g = tf.compat.v1.import_graph_def(optimized_graph_def, name='')
except ValueError:
g = tf.compat.v1.import_graph_def(graph_def, name='')
self.sess = tf.compat.v1.Session(graph=g, config=infer_config)
return self

def predict(self, feed):
return self.sess.run(self.outputs, feed_dict=feed)
Empty file.
Empty file.
Loading

0 comments on commit 36d07ca

Please sign in to comment.