{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"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 in \n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n\nimport os\nprint(os.listdir(\"../input\"))\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Download the latest release 2.0.2 of ImageAI\n!pip install https://github.com/OlafenwaMoses/ImageAI/releases/download/2.0.2/imageai-2.0.2-py3-none-any.whl","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Download YOLO v3 model\n!wget \"https://github.com/OlafenwaMoses/ImageAI/releases/download/1.0/yolo.h5\"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#check for downloaded model\nprint(os.listdir(os.getcwd()))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#download the classes data from official competetion page\ndf_classes = pd.read_csv(\"https://storage.googleapis.com/openimages/challenge_2018/challenge-2018-class-descriptions-500.csv\", header=None)\ndf_classes.columns = ['imageClassId', 'imageClass']\ndf_classes.shape #check if all the classes are fetched\ndict_classes = dict(zip(df_classes.imageClassId, df_classes.imageClass))  #convert the df into dictionary \nlen(dict_classes)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#converting all classnames to lowercase\nimage_classes = {}\nfor k,v in dict_classes.items():\n    image_classes[v.lower()] = k","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from imageai.Detection import ObjectDetection\nimport os\n\nmodel = ObjectDetection()\nexecution_path = os.getcwd()\nmodel.setModelTypeAsYOLOv3()\nmodel.setModelPath('/kaggle/working/yolo.h5') \nmodel.loadModel()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from keras.preprocessing.image import ImageDataGenerator\nfrom keras.applications.inception_v3 import preprocess_input\nfrom keras.utils.data_utils import GeneratorEnqueuer\nimport matplotlib.pyplot as plt\nimport pandas as pd \nimport numpy as np \nimport math, os\nfrom PIL import  Image\nfrom progressbar import progressbar as pbar\nimport glob","execution_count":51,"outputs":[{"output_type":"stream","text":"Using TensorFlow backend.\n","name":"stderr"}]},{"metadata":{"trusted":true},"cell_type":"code","source":"image_path = \"../input/google-ai-open-images-object-detection-track/test/challenge2018_test/\"\ntest_images = os.listdir(image_path)\n#Complete processing and object detection with coordinates of each image takes 0.4 secs, \n#so, divide the 99,999 test images into batches if you want\ntest_images_batch = test_images[:2] #I am only taking 2 images in the batch here to reduce compile time, as i have already predicted all images earlier by dividing them into bacthes\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**NOTE :** The object classes detected from ImageAI wont necessarily match the given labelled training dataset, as the ImageAI model we loaded is not trained on given dataset, We are only using the model for prediction.\n\nOur output prediction strings can be any of these:\n\n1. Complete Prediction string with **object class ids** and **coordinates** where the object class generated by ImageAI matches given classes in this use case.\n2. Prediction String with **object class names** and **coordinates** where the object class generated by ImageAI doesnt match given classes.\n3. Empty Prediction string when ImageAI is not able to detect any objects in the test image."},{"metadata":{"trusted":true},"cell_type":"code","source":"df_output = pd.DataFrame(columns=['ImageId', 'PredictionString']) #Create a dataframe and add results to it\n#dummy = 1\nfor image in pbar(test_images_batch):\n    #print('Image working on is: ', image,' and dummy variable value is: ',dummy)    \n    detections = model.detectObjectsFromImage(input_image=image_path+image, output_image_path=\"image_with_box.png\", minimum_percentage_probability = 65)  \n    #print(detections)\n    ImageId = str(image).split('.')[0]\n    im = Image.open(image_path+image)\n    image_width, image_height = im.size\n    pred_str = \"\"\n    labels = \"\"\n    for eachObject in detections:                   \n        x1,y1,x2,y2 = eachObject[\"box_points\"]\n        box_pts = str(round(x1/image_width,2))+\" \"+str(round(y1/image_height,2))+\" \"+str(round(x2/image_width,2))+\" \"+str(round(y2/image_height,2))\n        if eachObject[\"name\"] in image_classes:\n            pred_str += image_classes[eachObject[\"name\"]] + \" \" + str(round(float(eachObject[\"percentage_probability\"])/100,2)) +\" \"+ box_pts + \" \"\n        else:\n            #if the detected class is not present in classes given, there wont be any classid , then istead of classid, put the classname detected\n            pred_str += eachObject[\"name\"] + \" \" + str(round(float(eachObject[\"percentage_probability\"])/100,2)) +\" \"+ box_pts + \" \"\n        labels += eachObject['name'] + \", \" + str(round(float(eachObject['percentage_probability'])/100, 1)) \n        labels += \" | \"    \n            \n    df_output = df_output.append({'ImageId': ImageId, 'PredictionString': pred_str}, ignore_index=True)\n\nprint(\"Completed predictions of all test images\")\ndf_output.set_index('ImageId', inplace=True)\n#df_output.to_csv('predictions.csv') #uncomment this before running to get predcition results for the batch in predictions.csv","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"I have predicted the images in batches (predictions_1.csv, predictions_2.csv etc.. from above function) and then uploaded them back manually and generating a combined prediction results file here."},{"metadata":{"trusted":true},"cell_type":"code","source":"os.chdir(\"../input/resultsgooglechallengetoupload/googleopenimageschallengeresults/GoogleOpenImagesChallengeResults/\")\nresults = pd.DataFrame([])\n \nfor counter, file in enumerate(glob.glob(\"*\")):\n    namedf = pd.read_csv(file)\n    results = results.append(namedf)\n    \nresults.set_index('ImageId', inplace=True)\nos.chdir('/kaggle/working/') \nresults.to_csv('predictions_combinedfile.csv')","execution_count":52,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.4","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}