-
Notifications
You must be signed in to change notification settings - Fork 0
/
Deep Learning Crash Course Tensorflow Inception Z (2) copy.json
1 lines (1 loc) · 12.6 KB
/
Deep Learning Crash Course Tensorflow Inception Z (2) copy.json
1
{"paragraphs":[{"text":"%spark.pyspark\n\n# ==============================================================================\n# Copyright 2015 The TensorFlow Authors. All Rights Reserved.\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n# http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n# ==============================================================================\n\n\n# Simple image classification with Inception.\n#Run image classification with Inception trained on ImageNet 2012 Challenge data\n#set.\n#This program creates a graph from a saved GraphDef protocol buffer,\n#and runs inference on an input JPEG image. It outputs human readable\n#strings of the top 5 predictions along with their probabilities.\n#Change the --image_file argument to any jpg image to compute a\n#classification of that image.\n#Please see the tutorial and website for a detailed description of how\n#to use this script to perform image recognition.\n#https://tensorflow.org/tutorials/image_recognition/\n\nfrom __future__ import absolute_import\nfrom __future__ import division\nfrom __future__ import print_function\n\nimport errno as _errno\nimport sys as _sys\n\n# import tensorflow libraries for parsing flags\nfrom tensorflow.python.platform import flags\nfrom tensorflow.python.util.all_util import remove_undocumented\nfrom tensorflow.python.util.tf_export import tf_export\n\n# import argument parsing, operating system, date and math libraries\nimport argparse\nimport os.path\nimport re\nimport sys\nimport tarfile\nimport os\nimport datetime\nimport math\n\n\n# numpy is for numeric processing\nimport numpy as np\n\n# to load files over network\nfrom six.moves import urllib\n\n# the tensorflow python interface\nimport tensorflow as tf\n\n# set logging levels\ntf.logging.set_verbosity(tf.logging.ERROR)\n\nFLAGS = None\n\n# pylint: disable=line-too-long\nDATA_URL = 'http://download.tensorflow.org/models/image/imagenet/inception-2015-12-05.tgz'\n# pylint: enable=line-too-long\n\n\n# ----- Node Lookup Object\n# ----- \nclass NodeLookup(object):\n \"\"\"Converts integer node ID's to human readable labels.\"\"\"\n\n # initialize method\n def __init__(self,\n label_lookup_path=None,\n uid_lookup_path=None):\n if not label_lookup_path:\n label_lookup_path = os.path.join(\n FLAGS.model_dir, 'imagenet_2012_challenge_label_map_proto.pbtxt')\n if not uid_lookup_path:\n uid_lookup_path = os.path.join(\n FLAGS.model_dir, 'imagenet_synset_to_human_label_map.txt')\n self.node_lookup = self.load(label_lookup_path, uid_lookup_path)\n\n # load the text descriptions\n def load(self, label_lookup_path, uid_lookup_path):\n \"\"\"Loads a human readable English name for each softmax node.\n Args:\n label_lookup_path: string UID to integer node ID.\n uid_lookup_path: string UID to human-readable string.\n Returns:\n dict from integer node ID to human-readable string.\n \"\"\"\n if not tf.gfile.Exists(uid_lookup_path):\n tf.logging.fatal('File does not exist %s', uid_lookup_path)\n if not tf.gfile.Exists(label_lookup_path):\n tf.logging.fatal('File does not exist %s', label_lookup_path)\n\n\n # Loads mapping from string UID to human-readable string\n proto_as_ascii_lines = tf.gfile.GFile(uid_lookup_path).readlines()\n uid_to_human = {}\n p = re.compile(r'[n\\d]*[ \\S,]*')\n for line in proto_as_ascii_lines:\n parsed_items = p.findall(line)\n uid = parsed_items[0]\n human_string = parsed_items[2]\n uid_to_human[uid] = human_string\n\n\n # Loads mapping from string UID to integer node ID.\n node_id_to_uid = {}\n proto_as_ascii = tf.gfile.GFile(label_lookup_path).readlines()\n for line in proto_as_ascii:\n if line.startswith(' target_class:'):\n target_class = int(line.split(': ')[1])\n if line.startswith(' target_class_string:'):\n target_class_string = line.split(': ')[1]\n node_id_to_uid[target_class] = target_class_string[1:-2]\n\n # Loads the final mapping of integer node ID to human-readable string\n node_id_to_name = {}\n for key, val in node_id_to_uid.items():\n if val not in uid_to_human:\n tf.logging.fatal('Failed to locate: %s', val)\n name = uid_to_human[val]\n node_id_to_name[key] = name\n\n return node_id_to_name\n\n # convert the node id to a string\n def id_to_string(self, node_id):\n if node_id not in self.node_lookup:\n return ''\n return self.node_lookup[node_id]\n\n\n# create the graph from the pre-trained stored binary model\ndef create_graph():\n \"\"\"Creates a graph from saved GraphDef file and returns a saver.\"\"\"\n # Creates graph from saved graph_def.pb.\n with tf.gfile.FastGFile(os.path.join(\n FLAGS.model_dir, 'classify_image_graph_def.pb'), 'rb') as f:\n graph_def = tf.GraphDef()\n graph_def.ParseFromString(f.read())\n _ = tf.import_graph_def(graph_def, name='')\n\n\n# we are running the pretrained model\ndef run_inference_on_image(image):\n \"\"\"Runs inference on an image.\n Args:\n image: Image file name.\n Returns:\n Nothing\n \"\"\"\n #if not tf.gfile.Exists(image):\n # tf.logging.fatal('File does not exist %s', image)\n #image_data = tf.gfile.FastGFile(image, 'rb').read()\n image_url = \"https://raw.githubusercontent.com/tspannhw/DWS-DeepLearning-CrashCourse/master/photo1.jpg\"\n req = urllib.request.Request(image_url)\n response = urllib.request.urlopen(req)\n image_data = response.read()\n\n # Creates graph from saved GraphDef.\n create_graph()\n\n # tensorflow session processing\n with tf.Session() as sess:\n # Some useful tensors:\n # 'softmax:0': A tensor containing the normalized prediction across\n # 1000 labels.\n # 'pool_3:0': A tensor containing the next-to-last layer containing 2048\n # float description of the image.\n # 'DecodeJpeg/contents:0': A tensor containing a string providing JPEG\n # encoding of the image.\n # Runs the softmax tensor by feeding the image_data as input to the graph.\n softmax_tensor = sess.graph.get_tensor_by_name('softmax:0')\n predictions = sess.run(softmax_tensor,\n {'DecodeJpeg/contents:0': image_data})\n predictions = np.squeeze(predictions)\n\n\n # Creates node ID --> English string lookup.\n node_lookup = NodeLookup()\n \n # print a zeppelin table\n print('%table Node ID\\tDescription\\tScore\\n')\n\n # sort the predictions in order of score\n top_k = predictions.argsort()[-FLAGS.num_top_predictions:][::-1]\n\n # output all of our human string and scores\n for node_id in top_k:\n # get the printable string for that id\n human_string = node_lookup.id_to_string(node_id)\n # get the prediction score for that id\n score = predictions[node_id]\n # let's print it as a nice table in zeppelin\n print('{0}\\t{1}\\t{2}%\\n'.format(str(node_id), str(human_string), float(score) * 100.0))\n\n# download and extract model if not done already\ndef maybe_download_and_extract():\n \"\"\"Download and extract model tar file.\"\"\"\n dest_directory = FLAGS.model_dir\n if not os.path.exists(dest_directory):\n os.makedirs(dest_directory)\n filename = DATA_URL.split('/')[-1]\n filepath = os.path.join(dest_directory, filename)\n if not os.path.exists(filepath):\n def _progress(count, block_size, total_size):\n pass\n filepath, _ = urllib.request.urlretrieve(DATA_URL, filepath, _progress)\n statinfo = os.stat(filepath)\n tarfile.open(filepath, 'r:gz').extractall(dest_directory)\n\n# let's run it all\ndef main(_):\n # download pretrained model if necessary\n maybe_download_and_extract()\n # name of our file on the local filesystem\n img_name = 'https://raw.githubusercontent.com/tspannhw/DWS-DeepLearning-CrashCourse/master/photo1.jpg'\n # run on that image\n run_inference_on_image(img_name)\n\n\n# create arguments\nparser = argparse.ArgumentParser()\n\n# classify_image_graph_def.pb:\n# Binary representation of the GraphDef protocol buffer.\n# imagenet_synset_to_human_label_map.txt:\n# Map from synset ID to a human readable string.\n# imagenet_2012_challenge_label_map_proto.pbtxt:\n# Text representation of a protocol buffer mapping a label to synset ID.\nparser.add_argument(\n '--model_dir',\n type=str,\n default='/tmp',\n help=\"\"\"\\\n Path to classify_image_graph_def.pb,\n imagenet_synset_to_human_label_map.txt, and\n imagenet_2012_challenge_label_map_proto.pbtxt.\\\n \"\"\")\nparser.add_argument(\n '--image_file',\n type=str,\n default='',\n help='Absolute path to image file.' )\nparser.add_argument(\n '--num_top_predictions',\n type=int,\n default=5,\n help='Display this many predictions.')\nFLAGS, unparsed = parser.parse_known_args()\n#tf.app.run(main=main)\nmain(FLAGS)","dateUpdated":"2018-06-08T14:01:15+0000","config":{"colWidth":12,"editorMode":"ace/mode/python","results":{},"enabled":true,"editorSetting":{"editOnDblClick":false,"language":"python"}},"settings":{"params":{},"forms":{}},"results":{"code":"SUCCESS","msg":[{"type":"TEXT","data":"2018-06-08 14:01:21.259409: I tensorflow/core/platform/cpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.1 SSE4.2 AVX\n2018-06-08 14:01:24.175253: W tensorflow/core/framework/op_def_util.cc:334] Op BatchNormWithGlobalNormalization is deprecated. It will cease to work in GraphDef version 9. Use tf.nn.batch_normalization().\n"},{"type":"TABLE","data":"Node ID\tDescription\tScore\n273\tracer, race car, racing car\t37.4601334333%\n274\tsports car, sport car\t25.3520905972%\n267\tcab, hack, taxi, taxicab\t11.1182719469%\n268\tconvertible\t9.85429733992%\n271\tminivan\t3.2295178622%\n"}]},"apps":[],"jobName":"paragraph_1528466245103_2115928267","id":"20180605-134529_508893614","dateCreated":"2018-06-08T13:57:25+0000","status":"FINISHED","progressUpdateIntervalMs":500,"focus":true,"$$hashKey":"object:90","user":"robert","dateFinished":"2018-06-08T14:01:29+0000","dateStarted":"2018-06-08T14:01:15+0000"},{"text":"%sh\n\necho \"%html <img src='https://raw.githubusercontent.com/tspannhw/DWS-DeepLearning-CrashCourse/master/photo1.jpg'>\"\n","dateUpdated":"2018-06-08T13:58:25+0000","config":{"colWidth":12,"editorMode":"ace/mode/sh","results":{},"enabled":true,"editorSetting":{"editOnDblClick":false,"language":"sh"}},"settings":{"params":{},"forms":{}},"results":{"code":"SUCCESS","msg":[{"type":"HTML","data":"<img src='https://raw.githubusercontent.com/tspannhw/DWS-DeepLearning-CrashCourse/master/photo1.jpg'>\n"}]},"apps":[],"jobName":"paragraph_1528466245235_1781966222","id":"20180605-140121_476774146","dateCreated":"2018-06-08T13:57:25+0000","status":"FINISHED","progressUpdateIntervalMs":500,"$$hashKey":"object:91","user":"robert","dateFinished":"2018-06-08T13:59:11+0000","dateStarted":"2018-06-08T13:58:25+0000"},{"text":"%python\n\nprint('hi')\n","dateUpdated":"2018-06-08T13:57:25+0000","config":{"colWidth":12,"editorMode":"ace/mode/python","results":{},"enabled":true,"editorSetting":{"editOnDblClick":false,"language":"python"}},"settings":{"params":{},"forms":{}},"results":{"code":"SUCCESS","msg":[{"type":"TEXT","data":"hi\n"}]},"apps":[],"jobName":"paragraph_1528466245238_1780811975","id":"20180605-140211_1342852688","dateCreated":"2018-06-08T13:57:25+0000","status":"READY","errorMessage":"","progressUpdateIntervalMs":500,"$$hashKey":"object:92"},{"text":"%python\n","dateUpdated":"2018-06-08T13:57:25+0000","config":{"colWidth":12,"editorMode":"ace/mode/python","results":{},"enabled":true,"editorSetting":{"editOnDblClick":false,"language":"python"}},"settings":{"params":{},"forms":{}},"apps":[],"jobName":"paragraph_1528466245238_1780811975","id":"20180605-145550_1945530349","dateCreated":"2018-06-08T13:57:25+0000","status":"READY","errorMessage":"","progressUpdateIntervalMs":500,"$$hashKey":"object:93"}],"name":"Deep Learning Crash Course Tensorflow Inception Z","id":"2DHMK5GVV","angularObjects":{"2CYSFF4KX:shared_process":[],"2CZ7M5Z1U:shared_process":[],"2CZN4E536:shared_process":[],"2CXMXB6B9:shared_process":[],"2CX1UN2AM:shared_process":[],"2CVYP86A3:shared_process":[],"2CZPMT4AR:shared_process":[]},"config":{"looknfeel":"default","personalizedMode":"false"},"info":{}}