summaryrefslogtreecommitdiff
path: root/tools/tflkit/tflite_convert.sh
blob: 2056797abb5ff634da46a7003646c8883ad587e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/bin/bash

usage()
{
  echo "usage : $0"
  echo "       --info=Information file"
  echo "       --tensorflow_path=TensorFlow path (Use externals/tensorflow by default)"
  echo "       --tensorflow_version=TensorFlow version (Must be entered)"
}

SCRIPT_PATH="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

TF_DIR="${SCRIPT_PATH}/../../externals/tensorflow"

for i in "$@"
do
  case $i in
    --info=*)
      INFO=${i#*=}
      ;;
    --tensorflow_path=*)
      TF_DIR=${i#*=}
      ;;
    --tensorflow_version=*)
      TF_VERSION=${i#*=}
      ;;
    -h|--help)
      usage
      exit 0
      ;;
    *)
      usage
      exit 1
      ;;
  esac
  shift
done

if [ -z "$INFO" ]; then
  echo "INFO is unset or set to the empty string"
  usage
  exit 1
fi
if [ -z "$TF_DIR" ]; then
  echo "tensorflow_path is unset or set to the empty string"
  usage
  exit 1
fi
if [ -z "$TF_VERSION" ]; then
  echo "tensorflow_version is unset or set to the empty string"
  usage
  exit 1
fi

if [ ! -x "$(command -v bazel)" ]; then
  echo "Cannot find bazel. Please install bazel."
  exit 1
fi

source $INFO

if [ -z "$GRAPHDEF_PATH" ]; then
  echo "GRAPHDEF_PATH is unset or set to the empty string"
  echo "Update the $INFO file"
  exit 1
fi
if [ -z "$TFLITE_PATH" ]; then
  echo "TFLITE_PATH is unset or set to the empty string"
  echo "Update the $INFO file"
  exit 1
fi
if [ -z "$INPUT" ]; then
  echo "INPUT is unset or set to the empty string"
  echo "Update the $INFO file"
  exit 1
fi
if [ -z "$INPUT_SHAPE" ]; then
  echo "INPUT_SHAPE is unset or set to the empty string"
  echo "Update the $INFO file"
  exit 1
fi
if [ -z "$OUTPUT" ]; then
  echo "OUTPUT is unset or set to the empty string"
  echo "Update the $INFO file"
  exit 1
fi

CUR_DIR=$(pwd)
{
  echo "Enter $TF_DIR"
  pushd $TF_DIR > /dev/null

  NAME_LIST=()
  INPUT_SHAPE_LIST=()
  if [ -z "$NAME" ]; then
    NAME_LIST+=("$TFLITE_PATH")
    INPUT_SHAPE_LIST+=("$INPUT_SHAPE")
  else
    for name in $NAME; do
      NAME_LIST[${#NAME_LIST[*]}]="${TFLITE_PATH/NAME/$name}"
    done
    for shape in $INPUT_SHAPE; do
      INPUT_SHAPE_LIST[${#INPUT_SHAPE_LIST[*]}]="$shape"
    done
    if (( ${#NAME_LIST[*]} != ${#INPUT_SHAPE_LIST[*]} )); then
      echo "The number of NAME and INPUT_SHAPE are different"
      echo "Update the $INFO file"
      exit 1
    fi
  fi

  for (( i=0; i < ${#NAME_LIST[@]}; ++i )); do
    if [ "${TF_VERSION%%.*}" = "2" ]; then
      bazel run tensorflow/lite/python:tflite_convert -- \
      --output_file="${NAME_LIST[$i]}" \
      --graph_def_file="$GRAPHDEF_PATH" \
      --input_arrays="$INPUT" \
      --input_shapes="${INPUT_SHAPE_LIST[$i]}" \
      --output_arrays="$OUTPUT" \
      --allow_custom_ops=true
    else
      bazel run tensorflow/contrib/lite/python:tflite_convert -- \
      --output_file="${NAME_LIST[$i]}" \
      --graph_def_file="$GRAPHDEF_PATH" \
      --input_arrays="$INPUT" \
      --input_shapes="${INPUT_SHAPE_LIST[$i]}" \
      --output_arrays="$OUTPUT" \
      --allow_custom_ops
    fi
  done
  popd

  for (( i=0; i < ${#NAME_LIST[@]}; ++i )); do
    echo "OUTPUT FILE : ${NAME_LIST[$i]}"
  done
}