{"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_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"> #### wanglijie-19大数据-期末","metadata":{}},{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\n#import numpy as np # linear algebra\n#import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-12-09T14:29:13.799677Z"},"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 1、导入相应包及数据","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport os\nimport sys\nimport tensorflow as tf\nimport time\n\n# 导入用于提交预测结果的包\nINPUT_DIR = '../input/tensorflow-great-barrier-reef'\nsys.path.insert(0, INPUT_DIR)\nimport greatbarrierreef","metadata":{"execution":{"iopub.status.busy":"2021-12-09T14:32:10.820295Z","iopub.execute_input":"2021-12-09T14:32:10.820601Z","iopub.status.idle":"2021-12-09T14:32:10.833328Z","shell.execute_reply.started":"2021-12-09T14:32:10.820557Z","shell.execute_reply":"2021-12-09T14:32:10.832605Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 2、将TensorFlow COTS检测模型加载到内存中，并定义一些用于运行的util函数推论。\n\n","metadata":{}},{"cell_type":"code","source":"MODEL_DIR = '../input/cots-detection-w-tensorflow-object-detection-api/cots_efficientdet_d0'\nstart_time = time.time()\ntf.keras.backend.clear_session()\ndetect_fn_tf_odt = tf.saved_model.load(os.path.join(os.path.join(MODEL_DIR, 'output'), 'saved_model'))\nend_time = time.time()\nelapsed_time = end_time - start_time\nprint('Elapsed time: ' + str(elapsed_time) + 's')","metadata":{"execution":{"iopub.status.busy":"2021-12-09T14:33:30.461984Z","iopub.execute_input":"2021-12-09T14:33:30.462289Z","iopub.status.idle":"2021-12-09T14:33:57.017291Z","shell.execute_reply.started":"2021-12-09T14:33:30.462252Z","shell.execute_reply":"2021-12-09T14:33:57.016471Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_image_into_numpy_array(path):\n    \n    img_data = tf.io.gfile.GFile(path, 'rb').read()\n    image = Image.open(io.BytesIO(img_data))\n    (im_width, im_height) = image.size\n    \n    return np.array(image.getdata()).reshape(\n      (im_height, im_width, 3)).astype(np.uint8)","metadata":{"execution":{"iopub.status.busy":"2021-12-09T14:43:47.798116Z","iopub.execute_input":"2021-12-09T14:43:47.798408Z","iopub.status.idle":"2021-12-09T14:43:47.819778Z","shell.execute_reply.started":"2021-12-09T14:43:47.798377Z","shell.execute_reply":"2021-12-09T14:43:47.818779Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"##### ***加载图片到numpy_array***\n\n*将图像放入Numpy数组以输入TensorFlow图。*\n*注意，按照惯例，我们将其放入有形的numpy数组中，(高度，宽度，通道)，其中通道=3表示RGB。*  \n\n*路径：文件路径(可以是本地的，也可以是colossus架构中的)。*\n\n*返回值：uint8 numpy数组形状（高度，宽度，3）。*\n","metadata":{}},{"cell_type":"code","source":"def detect(image_np):\n\n    input_tensor = np.expand_dims(image_np, 0)\n    start_time = time.time()\n    detections = detect_fn_tf_odt(input_tensor)\n    return detections\n    ","metadata":{"execution":{"iopub.status.busy":"2021-12-09T14:43:52.604682Z","iopub.execute_input":"2021-12-09T14:43:52.605140Z","iopub.status.idle":"2021-12-09T14:43:52.609290Z","shell.execute_reply.started":"2021-12-09T14:43:52.605103Z","shell.execute_reply":"2021-12-09T14:43:52.608594Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"##### *从numpy图像识别COTS*","metadata":{}},{"cell_type":"markdown","source":"### 3、运行结果并构建提交数据","metadata":{}},{"cell_type":"code","source":"env = greatbarrierreef.make_env()   # 初始化环境\niter_test = env.iter_test()    # 循环测试集和样本提交的迭代器","metadata":{"execution":{"iopub.status.busy":"2021-12-09T14:43:58.360237Z","iopub.execute_input":"2021-12-09T14:43:58.361106Z","iopub.status.idle":"2021-12-09T14:43:58.394283Z","shell.execute_reply.started":"2021-12-09T14:43:58.361053Z","shell.execute_reply":"2021-12-09T14:43:58.393408Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"DETECTION_THRESHOLD = 0.19\n\nsubmission_dict = {\n    'id': [],\n    'prediction_string': [],\n}\n\nfor (image_np, sample_prediction_df) in iter_test:\n    height, width, _ = image_np.shape\n    \n    # Run object detection using the TensorFlow model.\n    detections = detect(image_np)\n    \n    # Parse the detection result and generate a prediction string.\n    num_detections = detections['num_detections'][0].numpy().astype(np.int32)\n    predictions = []\n    for index in range(num_detections):\n        score = detections['detection_scores'][0][index].numpy()\n        if score < DETECTION_THRESHOLD:\n            continue\n\n        bbox = detections['detection_boxes'][0][index].numpy()\n        y_min = int(bbox[0] * height)\n        x_min = int(bbox[1] * width)\n        y_max = int(bbox[2] * height)\n        x_max = int(bbox[3] * width)\n        \n        bbox_width = x_max - x_min\n        bbox_height = y_max - y_min\n        \n        predictions.append('{:.2f} {} {} {} {}'.format(score, x_min, y_min, bbox_width, bbox_height))\n    # Generate the submission data.\n    prediction_str = ' '.join(predictions)\n    sample_prediction_df['annotations'] = prediction_str\n    env.predict(sample_prediction_df)\n\n    print('Prediction:', prediction_str)   ","metadata":{"execution":{"iopub.status.busy":"2021-12-09T14:44:03.259733Z","iopub.execute_input":"2021-12-09T14:44:03.260333Z","iopub.status.idle":"2021-12-09T14:44:13.434820Z","shell.execute_reply.started":"2021-12-09T14:44:03.260289Z","shell.execute_reply":"2021-12-09T14:44:13.434065Z"},"trusted":true},"execution_count":null,"outputs":[]}]}