{"cells":[{"metadata":{"trusted":true,"_kg_hide-output":true},"cell_type":"code","source":"import os\nfrom pprint import pprint\nfrom six import BytesIO\n\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport pandas as pd\nimport tensorflow.compat.v1 as tf\ntf.disable_v2_behavior()\nimport tensorflow_hub as hub\nfrom PIL import Image, ImageColor, ImageDraw, ImageFont, ImageOps\nfrom tqdm import tqdm","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Utility Functions\n\nIn this section, we define a few functions that will be used for processing images and formatting the output prediction. You can safely skip this section and use the following functions as is:\n* `format_prediction_string(image_id, result)`: `image_id` is the ID of the test image you are trying to label. `result` is the dictionary created from running a `tf.Session`. The output is a formatted output row (i.e. `{Label Confidence XMin YMin XMax YMax},{...}`), so we need to modify the order from Tensorflow, which is by default `YMin XMin YMax XMax` (Thanks to [Nicolas for discovering this](https://www.kaggle.com/nhlr21/tf-hub-bounding-boxes-coordinates-corrected/notebook)).\n* `draw_boxes(image, boxes, class_names, scores, max_boxes=10, min_score=0.1)`: `image` is a numpy array representing an image, `boxes`, `class_names`, and `scores` are directly retrieved from the model predictions.\n* `display_image(image)`: Display a numpy array representing an `image`."},{"metadata":{"trusted":true},"cell_type":"code","source":"def format_prediction_string(image_id, result):\n    prediction_strings = []\n    \n    class_names = []\n    for i in range(len(result['detection_scores'])):\n        \n        class_name = result['detection_class_entities'][i].decode('ascii')\n        #class_name = result['detection_class_names'][i]\n        YMin,XMin,YMax,XMax = result['detection_boxes'][i]\n        score = result['detection_scores'][i]\n        class_names.append(class_name)\n        prediction_strings.append(\n            f\"{class_name} {score} {XMin} {YMin} {XMax} {YMax}\"\n        )\n        \n    prediction_string = \" \".join(prediction_strings)\n\n    return {\n        \"ImageID\": image_id,\n        \"PredictionString\": prediction_string,\n        'detection_class_names': class_names,\n        'detection_boxes':result['detection_boxes'],\n        'detection_scores':result['detection_scores']\n        \n        \n        \n    }","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def display_image(image):\n    fig = plt.figure(figsize=(20, 15))\n    plt.grid(False)\n    plt.axis('off')\n    plt.imshow(image)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"def draw_bounding_box_on_image(image,\n                               ymin,\n                               xmin,\n                               ymax,\n                               xmax,\n                               color,\n                               font,\n                               thickness=4,\n                               display_str_list=()):\n    \"\"\"Adds a bounding box to an image.\"\"\"\n    draw = ImageDraw.Draw(image)\n    im_width, im_height = image.size\n    (left, right, top, bottom) = (xmin * im_width, xmax * im_width,\n                                  ymin * im_height, ymax * im_height)\n    draw.line([(left, top), (left, bottom), (right, bottom), (right, top),\n               (left, top)],\n              width=thickness,\n              fill=color)\n\n    # If the total height of the display strings added to the top of the bounding\n    # box exceeds the top of the image, stack the strings below the bounding box\n    # instead of above.\n    display_str_heights = [font.getsize(ds)[1] for ds in display_str_list]\n    # Each display_str has a top and bottom margin of 0.05x.\n    total_display_str_height = (1 + 2 * 0.05) * sum(display_str_heights)\n\n    if top > total_display_str_height:\n        text_bottom = top\n    else:\n        text_bottom = bottom + total_display_str_height\n    # Reverse list and print from bottom to top.\n    for display_str in display_str_list[::-1]:\n        text_width, text_height = font.getsize(display_str)\n        margin = np.ceil(0.05 * text_height)\n        draw.rectangle([(left, text_bottom - text_height - 2 * margin),\n                        (left + text_width, text_bottom)],\n                       fill=color)\n        draw.text((left + margin, text_bottom - text_height - margin),\n                  display_str,\n                  fill=\"black\",\n                  font=font)\n        text_bottom -= text_height - 2 * margin","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def draw_boxes(image, boxes, class_names, scores, max_boxes=10, min_score=0.1):\n    \"\"\"Overlay labeled boxes on an image with formatted scores and label names.\"\"\"\n    colors = list(ImageColor.colormap.values())\n\n    try:\n        font = ImageFont.truetype(\n            \"/usr/share/fonts/truetype/liberation/LiberationSansNarrow-Regular.ttf\",\n            25)\n    except IOError:\n        print(\"Font not found, using default font.\")\n        font = ImageFont.load_default()\n\n    for i in range(min(boxes.shape[0], max_boxes)):\n        if scores[i] >= min_score:\n            ymin, xmin, ymax, xmax = tuple(boxes[i].tolist())\n            display_str = \"{}: {}%\".format(class_names[i].decode(\"ascii\"),\n                                           int(100 * scores[i]))\n            color = colors[hash(class_names[i]) % len(colors)]\n            image_pil = Image.fromarray(np.uint8(image)).convert(\"RGB\")\n            draw_bounding_box_on_image(\n                image_pil,\n                ymin,\n                xmin,\n                ymax,\n                xmax,\n                color,\n                font,\n                display_str_list=[display_str])\n            np.copyto(image, np.array(image_pil))\n    return image","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Running the model on a Sample Image\n\nLet's start by running the model on a single image. We will go through each step of the process afterwards."},{"metadata":{"trusted":true,"_kg_hide-output":true},"cell_type":"code","source":"#sample_image_path = \"../input/a-figure/A_figure/ATT126_454FC17B-F6AF-48AC-80FA-7C89B7C0658E.jpg\"\nsample_image_path = \"../input/a-figure/A_figure/ATT121_Close Up.jpg\"\n\nwith tf.Graph().as_default():\n    # Create our inference graph\n    image_string_placeholder = tf.placeholder(tf.string)\n    decoded_image = tf.image.decode_jpeg(image_string_placeholder)\n    decoded_image_float = tf.image.convert_image_dtype(\n        image=decoded_image, dtype=tf.float32\n    )\n    # Expanding image from (height, width, 3) to (1, height, width, 3)\n    image_tensor = tf.expand_dims(decoded_image_float, 0)\n\n    # Load the model from tfhub.dev, and create a detector_output tensor\n    model_url = \"https://tfhub.dev/google/openimages_v4/ssd/mobilenet_v2/1\"\n    detector = hub.Module(model_url)\n    detector_output = detector(image_tensor, as_dict=True)\n    \n    # Initialize the Session\n    init_ops = [tf.global_variables_initializer(), tf.tables_initializer()]\n    sess = tf.Session()\n    sess.run(init_ops)\n\n    # Load our sample image into a binary string\n    with tf.gfile.Open(sample_image_path, \"rb\") as binfile:\n        image_string = binfile.read()\n\n    # Run the graph we just created\n    result_out, image_out = sess.run(\n        [detector_output, decoded_image],\n        feed_dict={image_string_placeholder: image_string}\n    )\n    \nimage_with_boxes = draw_boxes(\n    np.array(image_out), result_out[\"detection_boxes\"],\n    result_out[\"detection_class_entities\"], result_out[\"detection_scores\"]\n)\ndisplay_image(image_with_boxes)\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Let's see what it looks like:"},{"metadata":{"trusted":true},"cell_type":"code","source":"model_url = \"https://tfhub.dev/google/openimages_v4/ssd/mobilenet_v2/1\"\ndetector = hub.Module(model_url)\ndetector_output = detector(image_tensor, as_dict=True)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# a组全训练"},{"metadata":{"trusted":true},"cell_type":"code","source":"init_ops = [tf.global_variables_initializer(), tf.tables_initializer()]\nsess = tf.Session()\nsess.run(init_ops)\n\ng = tf.Graph()\nwith g.as_default():\n    # Define operations and tensors in `g`.\n    c = tf.constant(30.0)\n    assert c.graph is g\n\n# sample_submission_df = pd.read_csv('../input/2021mcm-c-images-global-id/2021MCM_ProblemC_ Images_by_GlobalID.CSV')\n# FileNames = sample_submission_df['FileName']\npredictions = []\nimport os\nfilePath = '../input/a-figure/A_figure'\nFileNames=os.listdir(filePath)\n\nfor FileNames in tqdm(FileNames):\n    # Load the image string\n    image_path = f'../input/a-figure/A_figure/{FileNames}'\n    with tf.gfile.Open(image_path, \"rb\") as binfile:\n        image_string = binfile.read()\n    \n    # Run our session\n    result_out = sess.run(\n        detector_output,\n        feed_dict={image_string_placeholder: image_string}\n    )\n    predictions.append(format_prediction_string(FileNames, result_out))\n\nsess.close()\n\npred_df = pd.DataFrame(predictions)\npred_df.head(10)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"看标签数排序"},{"metadata":{"trusted":true},"cell_type":"code","source":"categs = {}\nfor class_n in pred_df['detection_class_names']:\n    \n    for vol in class_n:\n        if vol not in categs:\n            categs[vol] = 1\n        else:\n            categs[vol] += 1\n                \n        \nprint(sorted(categs.items(), key = lambda kv:(kv[1], kv[0]), reverse=True))  ","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Single（只训练一张，用来实验节约时间）"},{"metadata":{"trusted":true},"cell_type":"code","source":"init_ops = [tf.global_variables_initializer(), tf.tables_initializer()]\nsess = tf.Session()\nsess.run(init_ops)\n\ng = tf.Graph()\nwith g.as_default():\n    # Define operations and tensors in `g`.\n    c = tf.constant(30.0)\n    assert c.graph is g\n    \npredictions1 = []\nimport os\nfilePath = '../input/a-figure/A_figure'\nFileNames=os.listdir(filePath)[3:8]\n\nfor FileNames in tqdm(FileNames):\n    # Load the image string\n    image_path = f'../input/a-figure/A_figure/{FileNames}'\n    with tf.gfile.Open(image_path, \"rb\") as binfile:\n        image_string = binfile.read()\n    \n    # Run our session\n    result_out = sess.run(\n        detector_output,\n        feed_dict={image_string_placeholder: image_string}\n    )\n    predictions1.append(format_prediction_string(FileNames, result_out))\n\nsess.close()","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat":4,"nbformat_minor":4}