message: Cleaning up label_image.py example.
diff: # See the License for the specific language governing permissions and
  # limitations under the License.
  # ==============================================================================
  -"""label_image for tflite"""
  +"""label_image for tflite."""
  from __future__ import absolute_import
  from __future__ import division
  @@ -23,46 +23,49 @@ import numpy as np
  from PIL import Image
  -from tensorflow.lite.python import interpreter as interpreter_wrapper
  +from tensorflow.lite.python.interpreter import Interpreter
  +
  def load_labels(filename):
  - my_labels = []
  - input_file = open(filename, 'r')
  - for l in input_file:
  - my_labels.append(l.strip())
  - return my_labels
  + with open(filename, 'r') as f:
  + return [line.strip() for line in f.readlines()]
  -if __name__ == "__main__":
  - floating_model = False
  +if __name__ == '__main__':
  parser = argparse.ArgumentParser()
  - parser.add_argument("-i", "--image", default="/tmp/grace_hopper.bmp", \
  - help="image to be classified")
  - parser.add_argument("-m", "--model_file", \
  - default="/tmp/mobilenet_v1_1.0_224_quant.tflite", \
  - help=".tflite model to be executed")
  - parser.add_argument("-l", "--label_file", default="/tmp/labels.txt", \
  - help="name of file containing labels")
  - parser.add_argument("--input_mean", default=127.5, help="input_mean")
  - parser.add_argument("--input_std", default=127.5, \
  - help="input standard deviation")
  + parser.add_argument(
  + '-i',
  + '--image',
  + default='/tmp/grace_hopper.bmp',
  + help='image to be classified')
  + parser.add_argument(
  + '-m',
  + '--model_file',
  + default='/tmp/mobilenet_v1_1.0_224_quant.tflite',
  + help='.tflite model to be executed')
  + parser.add_argument(
  + '-l',
  + '--label_file',
  + default='/tmp/labels.txt',
  + help='name of file containing labels')
  + parser.add_argument('--input_mean', default=127.5, help='input_mean')
  + parser.add_argument(
  + '--input_std', default=127.5, help='input standard deviation')
  args = parser.parse_args()
  - interpreter = interpreter_wrapper.Interpreter(model_path=args.model_file)
  + interpreter = Interpreter(model_path=args.model_file)
  interpreter.allocate_tensors()
  input_details = interpreter.get_input_details()
  output_details = interpreter.get_output_details()
  # check the type of the input tensor
  - if input_details[0]['dtype'] == np.float32:
  - floating_model = True
  + floating_model = input_details[0]['dtype'] == np.float32
  # NxHxWxC, H:1, W:2
  height = input_details[0]['shape'][1]
  width = input_details[0]['shape'][2]
  - img = Image.open(args.image)
  - img = img.resize((width, height))
  + img = Image.open(args.image).resize((width, height))
  # add N dim
  input_data = np.expand_dims(img, axis=0)
  @@ -81,6 +84,6 @@ if __name__ == "__main__":
  labels = load_labels(args.label_file)
  for i in top_k:
  if floating_model:
  - print('{0:08.6f}'.format(float(results[i]))+":", labels[i])
  + print('{:08.6f}: {}'.format(float(results[i]), labels[i]))
  else:
  - print('{0:08.6f}'.format(float(results[i]/255.0))+":", labels[i])
  + print('{:08.6f}: {}'.format(float(results[i] / 255.0), labels[i]))
  