{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import 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 5GB 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","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"train_path = '/kaggle/input/global-wheat-detection/train.csv'\npath = '/kaggle/input/global-wheat-detection/train/'\ntest_path = '/kaggle/input/global-wheat-detection/test/'\ntemp_path = '/kaggle/working/'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nfrom sklearn.model_selection import train_test_split\nimport urllib\nimport cv2\nfrom matplotlib import pyplot as plt\n\n%matplotlib inline\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data_table = pd.read_csv(train_path)\ndata_table.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"annotations = pd.DataFrame(columns=['id_path', 'x1','y1','w','h','class'])\nannotations.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"a = 0\nfor id in data_table['image_id']:\n  jpg_path = os.path.join(path, id + '.jpg')\n  data_table.iat[a,0]=jpg_path\n  a+=1\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"annotations['id_path']=data_table['image_id']\nannotations.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"annotations['id_path'][0]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"a = 0\nfor i in data_table['bbox']:\n  item = i.split(',')\n  annotations.iat[a,1] = item[0]\n  annotations.iat[a,2] = item[1]\n  annotations.iat[a,3] = item[2]\n  annotations.iat[a,4] = item[3]\n  a+=1\nannotations.head() ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"annotations['x1'].replace({'\\[':''}, regex=True, inplace=True)\nannotations['h'].replace({']':''}, regex=True, inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"annotations['class']='wheat'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"annotations['x1']=pd.to_numeric(annotations['x1'])\nannotations['y1']=pd.to_numeric(annotations['y1'])\nannotations['w']=pd.to_numeric(annotations['w'])\nannotations['h']=pd.to_numeric(annotations['h'])\nannotations['x1']=annotations['x1'].astype(int)\nannotations['y1']=annotations['y1'].astype(int)\nannotations['w']=annotations['w'].astype(int)\nannotations['h']=annotations['h'].astype(int)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for i in range(len(annotations['w'])):\n    annotations['w'][i]=annotations['x1'][i]+annotations['w'][i]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for i in range(len(annotations['h'])):\n    annotations['h'][i]=annotations['y1'][i]+annotations['h'][i]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"annotations.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"annotations.info()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"annotations.to_csv(temp_path + 'annot.csv', header=False, index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"class_df = pd.DataFrame(columns=['class','class_id'],index=[0])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"class_df['class']='wheat'\nclass_df['class_id']=0\nclass_df","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"class_df.to_csv(temp_path + 'class.csv', header=False, index=False)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**Retina Net**","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"im_path = annotations['id_path'][0]\nim_path","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"x1 = annotations['x1'][1]\nw = annotations['w'][1]\ny1 = annotations['y1'][1]\nh = annotations['h'][1]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"im= cv2.imread(im_path)\nim = cv2.rectangle(im, (x1, y1), (w, h), (255,0,0), 2)\nplt.imshow(im)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!git clone https://github.com/fizyr/keras-retinanet.git","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"%cd keras-retinanet/\n!pip install .","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!python setup.py build_ext --inplace","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"PRETRAINED_MODEL = './snapshots/_pretrained_model.h5'\nPRETRAINED_MODEL1 = '/kaggle/working/keras-retinanet/snapshots/resnet50_csv_06.h5'\n\nURL_MODEL = 'https://github.com/fizyr/keras-retinanet/releases/download/0.5.1/resnet50_coco_best_v2.1.0.h5'\nurllib.request.urlretrieve(URL_MODEL, PRETRAINED_MODEL)\n\nprint('Downloaded pretrained model to ' + PRETRAINED_MODEL)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"EPOCHS = 6\nBATCH_SIZE=8\nSTEPS = 100 \nLR=0.0001","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!keras_retinanet/bin/train.py --random-transform --weights {PRETRAINED_MODEL1} --lr {LR} --batch-size {BATCH_SIZE} --steps {STEPS} --epochs {EPOCHS} --no-resize csv /kaggle/working/annot.csv /kaggle/working/class.csv","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_images = os.listdir(test_path)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import tensorflow as tf\nfrom keras_retinanet import models\nfrom keras_retinanet.utils.image import read_image_bgr, preprocess_image, resize_image\nfrom keras_retinanet.utils.visualization import draw_box, draw_caption\nfrom keras_retinanet.utils.colors import label_color","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!ls snapshots","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"removing_path = '/kaggle/working/keras-retinanet/snapshots/'\nos.remove(removing_path+'resnet50_csv_01.h5')\nos.remove(removing_path+'resnet50_csv_06.h5')\nos.remove(removing_path+'resnet50_csv_03.h5')\nos.remove(removing_path+'resnet50_csv_04.h5')\nos.remove(removing_path+'resnet50_csv_05.h5')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model_path = os.path.join('snapshots', 'resnet50_csv_02.h5')\n\nmodel = models.load_model(model_path, backbone_name='resnet50')\nmodel = models.convert_model(model)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def prediction(image):\n    image = preprocess_image(image.copy())\n    boxes, scores, labels = model.predict_on_batch(np.expand_dims(image, axis=0))\n    return boxes, scores, labels","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"thres = 0.5","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def draw_detections(image, boxes, scores, labels):\n    for box, score, label in zip(boxes[0], scores[0], labels[0]):\n        if score < thres:\n            break\n\n        color = label_color(label)\n\n        b = box.astype(int)\n        draw_box(image, b, color=color)\n\n        caption = \"{:.3f}\".format(score)\n        draw_caption(image, b, caption)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def show_detected_objects(image_name):\n    img_path = test_path+'/'+image_name\n  \n    image = read_image_bgr(img_path)\n\n    boxes, scores, labels = prediction(image)\n    print(boxes[0,0].shape)\n    draw = image.copy()\n    draw = cv2.cvtColor(draw, cv2.COLOR_BGR2RGB)\n\n    draw_detections(draw, boxes, scores, labels)\n    plt.figure(figsize=(15,10))\n    plt.axis('off')\n    plt.imshow(draw)\n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for img in test_images:\n  \n    show_detected_objects(img)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"preds=[]\nimgid=[]\nfor img in test_images:\n    img_path = test_path+'/'+img\n    image = read_image_bgr(img_path)\n    boxes, scores, labels = prediction(image)\n    boxes=boxes[0]\n    scores=scores[0]\n    for idx in range(boxes.shape[0]):\n        if scores[idx]>thres:\n            box,score=boxes[idx],scores[idx]\n            imgid.append(img.split(\".\")[0])\n            preds.append(\"{} {} {} {} {}\".format(score, int(box[0]), int(box[1]), int(box[2]-box[0]), int(box[3]-box[1])))\n    ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"preds[0]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sub={\"image_id\":imgid, \"PredictionString\":preds}\nsub=pd.DataFrame(sub)\nsub.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sub_=sub.groupby([\"image_id\"])['PredictionString'].apply(lambda x: ' '.join(x)).reset_index()\nsub_","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submiss=pd.read_csv(\"/kaggle/input/global-wheat-detection/sample_submission.csv\")\nsubmiss.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for idx,imgid in enumerate(submiss['image_id']):\n    submiss.iloc[idx,1]=sub_[sub_['image_id']==imgid].values[0,1]\n    \nsubmiss.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submiss.to_csv('/kaggle/working/submission.csv',index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"annot = '/kaggle/working/annot.csv'\nclass_cs = '/kaggle/working/class.csv'\nretina = '/kaggle/working/keras-retinanet'\n\nos.remove(annot)\nos.remove(class_cs)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import shutil\nshutil.rmtree(retina)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","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}