{"cells":[{"metadata":{"id":"kLoS4g59JGgK","outputId":"23d47214-405c-4e05-979a-59e715cea5b7","executionInfo":{"status":"ok","timestamp":1589012503532,"user_tz":-330,"elapsed":1026,"user":{"displayName":"Aviral Pamecha","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GgiQYcZ0y7v_0iK6NLDTKBzzfseWHyM90DJwNAR=s64","userId":"09103175250081373018"}},"trusted":true},"cell_type":"code","source":"import numpy as np \nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns","execution_count":null,"outputs":[]},{"metadata":{"id":"rtB8ovKsJGgV","trusted":true},"cell_type":"code","source":"import os\nimport re\nimport cv2","execution_count":null,"outputs":[]},{"metadata":{"id":"sB7W8omJljFe","trusted":true},"cell_type":"code","source":"import torch\nimport torchvision","execution_count":null,"outputs":[]},{"metadata":{"id":"kMc4mLFHg6c-","trusted":true},"cell_type":"code","source":"import albumentations as A\nfrom albumentations.pytorch import ToTensor ","execution_count":null,"outputs":[]},{"metadata":{"id":"SVhR3dOFJGgd","trusted":true},"cell_type":"code","source":"from matplotlib.image import imread","execution_count":null,"outputs":[]},{"metadata":{"id":"tt8LvvI1JpB-","outputId":"706deef0-f0ad-477a-e83f-2a0493f44bca","executionInfo":{"status":"ok","timestamp":1589012553535,"user_tz":-330,"elapsed":35736,"user":{"displayName":"Aviral Pamecha","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GgiQYcZ0y7v_0iK6NLDTKBzzfseWHyM90DJwNAR=s64","userId":"09103175250081373018"}},"trusted":false},"cell_type":"code","source":"from google.colab import drive\ndrive.mount('/content/drive')","execution_count":null,"outputs":[]},{"metadata":{"id":"6DpfxX1FJ_GG","outputId":"ded2d8b0-ac93-4c08-f9ab-91b8661428d8","executionInfo":{"status":"ok","timestamp":1589019089130,"user_tz":-330,"elapsed":1129,"user":{"displayName":"Aviral Pamecha","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GgiQYcZ0y7v_0iK6NLDTKBzzfseWHyM90DJwNAR=s64","userId":"09103175250081373018"}},"trusted":true},"cell_type":"code","source":"pwd","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"ls","execution_count":null,"outputs":[]},{"metadata":{"id":"bf_aFqDI7VRg","outputId":"669cd5ce-b356-4235-c80b-fae1ee2ef8b7","executionInfo":{"status":"ok","timestamp":1589012560760,"user_tz":-330,"elapsed":1568,"user":{"displayName":"Aviral Pamecha","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GgiQYcZ0y7v_0iK6NLDTKBzzfseWHyM90DJwNAR=s64","userId":"09103175250081373018"}},"trusted":true},"cell_type":"code","source":"cd ..","execution_count":null,"outputs":[]},{"metadata":{"id":"NwCVOlZwLWW9","outputId":"a6d06a7a-5eb0-46fe-993a-f220d591d771","executionInfo":{"status":"ok","timestamp":1589012560765,"user_tz":-330,"elapsed":1550,"user":{"displayName":"Aviral Pamecha","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GgiQYcZ0y7v_0iK6NLDTKBzzfseWHyM90DJwNAR=s64","userId":"09103175250081373018"}},"trusted":true},"cell_type":"code","source":"cd input/global-wheat-detection","execution_count":null,"outputs":[]},{"metadata":{"id":"ZJ8cQ0-cJGgl","trusted":true},"cell_type":"code","source":"train = pd.read_csv('train.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test = pd.read_csv('sample_submission.csv')","execution_count":null,"outputs":[]},{"metadata":{"id":"s9FjdeHaJGgt","outputId":"8f1b0562-78ef-4280-a3f6-cf2542e41de2","executionInfo":{"status":"ok","timestamp":1589012564045,"user_tz":-330,"elapsed":998,"user":{"displayName":"Aviral Pamecha","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GgiQYcZ0y7v_0iK6NLDTKBzzfseWHyM90DJwNAR=s64","userId":"09103175250081373018"}},"trusted":true},"cell_type":"code","source":"train.head()","execution_count":null,"outputs":[]},{"metadata":{"id":"l599GpbSJGg0","trusted":true},"cell_type":"code","source":"def append_ext(fn):\n    return fn+\".jpg\"\ntrain[\"image_id\"]=train[\"image_id\"].apply(append_ext)\n","execution_count":null,"outputs":[]},{"metadata":{"id":"NxhVovIBJGg4","outputId":"005195b9-c7e6-4dfc-8cc8-e534abe0b058","executionInfo":{"status":"ok","timestamp":1589012566840,"user_tz":-330,"elapsed":1791,"user":{"displayName":"Aviral Pamecha","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GgiQYcZ0y7v_0iK6NLDTKBzzfseWHyM90DJwNAR=s64","userId":"09103175250081373018"}},"trusted":true},"cell_type":"code","source":"train['bbox'].count()","execution_count":null,"outputs":[]},{"metadata":{"id":"5Fgifuy8zlf1","trusted":true},"cell_type":"code","source":"train['xmin'] = None\ntrain['ymin'] = None\ntrain['Width'] = None\ntrain['Height'] = None","execution_count":null,"outputs":[]},{"metadata":{"id":"ntzwj9wLze8B","trusted":true},"cell_type":"code","source":"def expand_bbox(x):\n    r = np.array(re.findall(\"([0-9]+[.]?[0-9]*)\", x))\n    if len(r) == 0:\n        r = [-1, -1, -1, -1]\n    return r\n\ntrain[['xmin', 'ymin', 'Width', 'Height']] = np.stack(train['bbox'].apply(lambda x: expand_bbox(x)))\ntrain.drop(columns=['bbox'], inplace=True)\ntrain['xmin'] = train['xmin'].astype(np.float)\ntrain['ymin'] = train['ymin'].astype(np.float)\ntrain['Width'] = train['Width'].astype(np.float)\ntrain['Height'] = train['Height'].astype(np.float)","execution_count":null,"outputs":[]},{"metadata":{"id":"t-tWrHI2YGUi","outputId":"93f996b2-c67d-44ee-ffa1-3fadb1544c6d","executionInfo":{"status":"ok","timestamp":1589012568463,"user_tz":-330,"elapsed":1834,"user":{"displayName":"Aviral Pamecha","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GgiQYcZ0y7v_0iK6NLDTKBzzfseWHyM90DJwNAR=s64","userId":"09103175250081373018"}},"trusted":true},"cell_type":"code","source":"train.head()","execution_count":null,"outputs":[]},{"metadata":{"id":"YEm_GB3SJGg_","outputId":"7ec3cf73-e0d3-4aff-853f-c94dcffa9b7d","executionInfo":{"status":"ok","timestamp":1589012569296,"user_tz":-330,"elapsed":2089,"user":{"displayName":"Aviral Pamecha","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GgiQYcZ0y7v_0iK6NLDTKBzzfseWHyM90DJwNAR=s64","userId":"09103175250081373018"}},"trusted":true},"cell_type":"code","source":"train['source'].unique()","execution_count":null,"outputs":[]},{"metadata":{"id":"v3PJMFxxJGhC","outputId":"38e6d474-4155-4f43-d156-849643cb0ac2","executionInfo":{"status":"ok","timestamp":1589012570175,"user_tz":-330,"elapsed":1815,"user":{"displayName":"Aviral Pamecha","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GgiQYcZ0y7v_0iK6NLDTKBzzfseWHyM90DJwNAR=s64","userId":"09103175250081373018"}},"trusted":true},"cell_type":"code","source":"sns.countplot(x = 'source', data=train)\n\n# THIS SHOWS THE IMBALANCE IN DATASET","execution_count":null,"outputs":[]},{"metadata":{"id":"VhapWn2sJGhG","outputId":"f20e9725-a9dc-4f25-caf0-12dfb5949b59","executionInfo":{"status":"ok","timestamp":1589012573150,"user_tz":-330,"elapsed":3068,"user":{"displayName":"Aviral Pamecha","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GgiQYcZ0y7v_0iK6NLDTKBzzfseWHyM90DJwNAR=s64","userId":"09103175250081373018"}},"trusted":true},"cell_type":"code","source":"sns.heatmap(train.isnull(), yticklabels=False, cbar=False, cmap='viridis')\n\n# NO NULL VALUES","execution_count":null,"outputs":[]},{"metadata":{"id":"SAAhehHNJGhN","trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"id":"5L7wwLVRJGhV","outputId":"02a8a464-4b0d-4038-83be-6b8dd9e730a0","executionInfo":{"status":"ok","timestamp":1589012573169,"user_tz":-330,"elapsed":1353,"user":{"displayName":"Aviral Pamecha","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GgiQYcZ0y7v_0iK6NLDTKBzzfseWHyM90DJwNAR=s64","userId":"09103175250081373018"}},"trusted":true},"cell_type":"code","source":"train.head()","execution_count":null,"outputs":[]},{"metadata":{"id":"l3PYFxiOJGjA","outputId":"cc779865-161f-45cf-8454-c4606a17ca48","executionInfo":{"status":"ok","timestamp":1589012575777,"user_tz":-330,"elapsed":1886,"user":{"displayName":"Aviral Pamecha","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GgiQYcZ0y7v_0iK6NLDTKBzzfseWHyM90DJwNAR=s64","userId":"09103175250081373018"}},"trusted":true},"cell_type":"code","source":"train.head()","execution_count":null,"outputs":[]},{"metadata":{"id":"vuXkLDics9U9","trusted":true},"cell_type":"code","source":"train.drop('width', axis=1, inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"id":"rMX38X79s9dE","trusted":true},"cell_type":"code","source":"train.drop('height', axis=1, inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"pwd","execution_count":null,"outputs":[]},{"metadata":{"id":"iaK0dDALJGk9","trusted":true},"cell_type":"code","source":"data_dir = '/kaggle/input/global-wheat-detection'","execution_count":null,"outputs":[]},{"metadata":{"id":"W6NbevUYJGlM","outputId":"1d8c4324-6e6c-4476-ffbf-1b4f095f6003","executionInfo":{"status":"ok","timestamp":1589012583578,"user_tz":-330,"elapsed":744,"user":{"displayName":"Aviral Pamecha","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GgiQYcZ0y7v_0iK6NLDTKBzzfseWHyM90DJwNAR=s64","userId":"09103175250081373018"}},"trusted":true},"cell_type":"code","source":"os.listdir(data_dir)","execution_count":null,"outputs":[]},{"metadata":{"id":"0tAj9bTgJGlT","trusted":true},"cell_type":"code","source":"train_path = data_dir + '/train/'\ntest_path = data_dir + '/test/'","execution_count":null,"outputs":[]},{"metadata":{"id":"b8rbSUzMOTIg","trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"id":"CHeQXSobJGlY","trusted":true},"cell_type":"code","source":"a = os.listdir(test_path)[0]","execution_count":null,"outputs":[]},{"metadata":{"id":"pMSV-k9GAKEq","outputId":"0439ce80-73b8-4409-c459-c39bc761e6e0","executionInfo":{"status":"ok","timestamp":1589012590522,"user_tz":-330,"elapsed":789,"user":{"displayName":"Aviral Pamecha","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GgiQYcZ0y7v_0iK6NLDTKBzzfseWHyM90DJwNAR=s64","userId":"09103175250081373018"}},"trusted":true},"cell_type":"code","source":"a","execution_count":null,"outputs":[]},{"metadata":{"id":"0AcwY0Y0_a5F","trusted":true},"cell_type":"code","source":"sam_test = test_path + '348a992bb.jpg'","execution_count":null,"outputs":[]},{"metadata":{"id":"jjwdFK2tApzn","trusted":true},"cell_type":"code","source":"sam_tensor = imread(sam_test)","execution_count":null,"outputs":[]},{"metadata":{"id":"7AuIDcMlJGld","trusted":true},"cell_type":"code","source":"test_img = test_path+ '51b3e36ab.jpg'","execution_count":null,"outputs":[]},{"metadata":{"id":"HjtiqgKwJGlg","trusted":true},"cell_type":"code","source":"test_img = imread(test_img)","execution_count":null,"outputs":[]},{"metadata":{"id":"zmOGLKSvJGlj","outputId":"4c74c166-fa80-424b-f6f4-dcae3673e3b3","executionInfo":{"status":"ok","timestamp":1589012599987,"user_tz":-330,"elapsed":1580,"user":{"displayName":"Aviral Pamecha","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GgiQYcZ0y7v_0iK6NLDTKBzzfseWHyM90DJwNAR=s64","userId":"09103175250081373018"}},"trusted":true},"cell_type":"code","source":"test_img.shape","execution_count":null,"outputs":[]},{"metadata":{"id":"0UAPcpZJJGlm","outputId":"80213cea-4299-4a6d-90a4-762cb898b491","executionInfo":{"status":"ok","timestamp":1589012603628,"user_tz":-330,"elapsed":2190,"user":{"displayName":"Aviral Pamecha","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GgiQYcZ0y7v_0iK6NLDTKBzzfseWHyM90DJwNAR=s64","userId":"09103175250081373018"}},"trusted":true},"cell_type":"code","source":"plt.imshow(test_img)","execution_count":null,"outputs":[]},{"metadata":{"id":"BjKo-dplJGlo","trusted":true},"cell_type":"code","source":"image_shape = (224,224,3)","execution_count":null,"outputs":[]},{"metadata":{"id":"pE7PtoQ8JGlt","trusted":true},"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator","execution_count":null,"outputs":[]},{"metadata":{"id":"MqUvCah7JGlz","trusted":false},"cell_type":"markdown","source":"image_gen = ImageDataGenerator(\n    rescale=1./255,\n    featurewise_center=True,\n    featurewise_std_normalization=True,\n    rotation_range=90,\n    width_shift_range=0.15,\n    height_shift_range=0.15,\n    horizontal_flip=True,\n    zoom_range=[0.9, 1.25],\n    brightness_range=[0.5, 1.5]\n)"},{"metadata":{"id":"GKDM7fMSJGl3","trusted":false},"cell_type":"code","source":"batch_size = 64","execution_count":0,"outputs":[]},{"metadata":{"id":"jqEuVT52JGl7","trusted":true},"cell_type":"code","source":"test_image_generator = ImageDataGenerator(\n    rescale=1./255)","execution_count":null,"outputs":[]},{"metadata":{"id":"3MW7nhJ7n0wB","trusted":true},"cell_type":"code","source":"def append_ext(fn):\n    return fn+\".jpg\"\ntest[\"image_id\"]=test[\"image_id\"].apply(append_ext)","execution_count":null,"outputs":[]},{"metadata":{"id":"WBKXJq2RJGmA","outputId":"8bfd43d7-0486-4626-a6b4-f9e033fe42b2","executionInfo":{"status":"ok","timestamp":1588943486057,"user_tz":-330,"elapsed":15693,"user":{"displayName":"Aviral Pamecha","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GgiQYcZ0y7v_0iK6NLDTKBzzfseWHyM90DJwNAR=s64","userId":"09103175250081373018"}},"trusted":false},"cell_type":"markdown","source":"train_gen = image_gen.flow_from_dataframe(\n    dataframe=train,\n    directory=train_path,\n    x_col='image_id',\n    y_col='source',\n    target_size=image_shape[:2],\n    batch_size=batch_size,\n    color_mode='rgb',\n    shuffle=True,\n    class_mode='categorical'\n)"},{"metadata":{"id":"j1I2HMcXTjtQ","trusted":false},"cell_type":"code","source":"total_samples = 146704","execution_count":0,"outputs":[]},{"metadata":{"id":"fsNOxHdfJGmF","outputId":"06b9f3bd-d2d0-43f9-ad09-e24cfcb2bbba","executionInfo":{"status":"ok","timestamp":1589014218559,"user_tz":-330,"elapsed":1031,"user":{"displayName":"Aviral Pamecha","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GgiQYcZ0y7v_0iK6NLDTKBzzfseWHyM90DJwNAR=s64","userId":"09103175250081373018"}},"trusted":true},"cell_type":"code","source":"test_generator = test_image_generator.flow_from_dataframe(\n    dataframe = test,\n    directory=test_path,\n     x_col='image_id',\n    target_size=(1024,1024),\n    shuffle=False,\n    batch_size=8,\n    class_mode=None\n)","execution_count":null,"outputs":[]},{"metadata":{"id":"S0GQ8uPB-y4P","outputId":"7f21a24c-fe15-4cbd-9662-62650da4cdb8","executionInfo":{"status":"ok","timestamp":1589012616158,"user_tz":-330,"elapsed":3172,"user":{"displayName":"Aviral Pamecha","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GgiQYcZ0y7v_0iK6NLDTKBzzfseWHyM90DJwNAR=s64","userId":"09103175250081373018"}},"trusted":true},"cell_type":"code","source":"from keras.preprocessing.image import ImageDataGenerator\nfrom keras.models import Sequential\nfrom keras.layers import Conv2D, MaxPooling2D, Dense, Dropout, Flatten\nfrom keras.optimizers import Adam\nfrom keras.losses import binary_crossentropy\nfrom keras.callbacks import LearningRateScheduler\nfrom keras.metrics import *\n# v4\n\nACCURACY_LIST = []\nfrom keras.applications.resnet50 import ResNet50\nfrom keras.layers import GlobalMaxPooling2D\nfrom keras.models import Model","execution_count":null,"outputs":[]},{"metadata":{"id":"rzbAd5nzAT8X","trusted":true},"cell_type":"code","source":"METRICS = [\n     \n      BinaryAccuracy(name='accuracy'),\n      Precision(name='precision'),\n      Recall(name='recall'),\n      AUC(name='auc'),\n]\n\ndef output_custom_model(prebuilt_model):\n    print(f\"Processing {prebuilt_model}\")\n    prebuilt = prebuilt_model(include_top=False,\n                            input_shape=(1024,1024,3),\n                            weights='imagenet')\n    output = prebuilt.output\n    output = GlobalMaxPooling2D()(output)\n    output = Dense(128, activation='relu')(output)\n    output = Dropout(0.4)(output)\n    output = Dense(7, activation='softmax')(output)\n\n    model = Model(inputs=prebuilt.input, outputs=output)\n    model.compile(optimizer='adam', loss=categorical_crossentropy,\n              metrics=METRICS)\n    return model","execution_count":null,"outputs":[]},{"metadata":{"id":"4KWB1JUlSfCt","trusted":true},"cell_type":"code","source":"def scheduler(epoch):\n    if epoch < 5:\n        return 0.0001\n    else:\n        print(f\"Learning rate reduced to {0.0001 * np.exp(0.5 * (5 - epoch))}\")\n        return 0.0001 * np.exp(0.5 * (5 - epoch))\n    \ncustom_callback = LearningRateScheduler(scheduler)\n","execution_count":null,"outputs":[]},{"metadata":{"id":"QlmqdTyFAZ2b","outputId":"840438ca-7cd1-4b30-d597-32aa3a7b6453","executionInfo":{"status":"ok","timestamp":1589012626152,"user_tz":-330,"elapsed":6324,"user":{"displayName":"Aviral Pamecha","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GgiQYcZ0y7v_0iK6NLDTKBzzfseWHyM90DJwNAR=s64","userId":"09103175250081373018"}},"trusted":true},"cell_type":"code","source":"resnet_custom_model = output_custom_model(ResNet50)\n","execution_count":null,"outputs":[]},{"metadata":{"id":"ZOrrrBP2AUCI","outputId":"c4602ab1-9011-48a4-e30c-91452aa64236","executionInfo":{"status":"ok","timestamp":1588956866862,"user_tz":-330,"elapsed":4244431,"user":{"displayName":"Aviral Pamecha","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GgiQYcZ0y7v_0iK6NLDTKBzzfseWHyM90DJwNAR=s64","userId":"09103175250081373018"}},"trusted":false},"cell_type":"markdown","source":"resnet_custom_model = output_custom_model(ResNet50)\nresnet_history = resnet_custom_model.fit_generator(train_gen,\n                                 epochs=1,callbacks=[custom_callback]\n                                 \n                              )"},{"metadata":{"id":"kEiCPIVvAUHU","outputId":"193e0496-51b2-4e93-a895-3121fd2dda82","executionInfo":{"status":"ok","timestamp":1588960962352,"user_tz":-330,"elapsed":2821,"user":{"displayName":"Aviral Pamecha","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GgiQYcZ0y7v_0iK6NLDTKBzzfseWHyM90DJwNAR=s64","userId":"09103175250081373018"}},"trusted":false},"cell_type":"markdown","source":"  resnet_history.history"},{"metadata":{"id":"wt_wBPPdXA2r","trusted":false},"cell_type":"markdown","source":"resnet_custom_model.save_weights('wheat_detection.h5')"},{"metadata":{"id":"TcpTCHCEapJz","outputId":"211ba5c1-b0ad-4ce3-c96b-24c229fcb7c9","executionInfo":{"status":"ok","timestamp":1589012637660,"user_tz":-330,"elapsed":1141,"user":{"displayName":"Aviral Pamecha","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GgiQYcZ0y7v_0iK6NLDTKBzzfseWHyM90DJwNAR=s64","userId":"09103175250081373018"}},"trusted":false},"cell_type":"code","source":"pwd","execution_count":null,"outputs":[]},{"metadata":{"id":"ACyMz6DlP_5s","trusted":false},"cell_type":"code","source":"test = pd.read_csv('sample_submission.csv')","execution_count":0,"outputs":[]},{"metadata":{"id":"lmOIorKqRLUi","outputId":"1c5eef23-6f24-4bd2-ff71-81cec05eefe2","executionInfo":{"status":"ok","timestamp":1588957914514,"user_tz":-330,"elapsed":2408,"user":{"displayName":"Aviral Pamecha","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GgiQYcZ0y7v_0iK6NLDTKBzzfseWHyM90DJwNAR=s64","userId":"09103175250081373018"}},"trusted":true},"cell_type":"code","source":"test.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"cd ../weights","execution_count":null,"outputs":[]},{"metadata":{"id":"XdjKBfHQ5tK3","trusted":true},"cell_type":"code","source":"resnet_custom_model.load_weights('wheat_detection.h5')","execution_count":null,"outputs":[]},{"metadata":{"id":"XvZWsT2NMDvk","outputId":"31b08d6c-7261-455b-cb97-a837b84c6c75","executionInfo":{"status":"ok","timestamp":1589012778888,"user_tz":-330,"elapsed":1173,"user":{"displayName":"Aviral Pamecha","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GgiQYcZ0y7v_0iK6NLDTKBzzfseWHyM90DJwNAR=s64","userId":"09103175250081373018"}},"trusted":true},"cell_type":"code","source":"sam_tensor.shape","execution_count":null,"outputs":[]},{"metadata":{"id":"M62E7sTzhjEv","trusted":true},"cell_type":"code","source":"sam_tensor = np.expand_dims(sam_tensor,axis=0)","execution_count":null,"outputs":[]},{"metadata":{"id":"zIlKgmv99zAG","trusted":true},"cell_type":"code","source":"preds = resnet_custom_model.predict(test_generator)","execution_count":null,"outputs":[]},{"metadata":{"id":"jBy_fEhBiyvk","outputId":"362cc622-3f97-487d-d9b7-b6b125b33059","executionInfo":{"status":"ok","timestamp":1589026508773,"user_tz":-330,"elapsed":981,"user":{"displayName":"Aviral Pamecha","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GgiQYcZ0y7v_0iK6NLDTKBzzfseWHyM90DJwNAR=s64","userId":"09103175250081373018"}},"trusted":true},"cell_type":"code","source":"preds.min()","execution_count":null,"outputs":[]},{"metadata":{"id":"Wwoayr0ckIPn","trusted":false},"cell_type":"code","source":"","execution_count":0,"outputs":[]},{"metadata":{"id":"FrqfXcAbGqvw","trusted":true},"cell_type":"code","source":"from itertools import chain\nfrom skimage.io import imread, imshow, imread_collection, concatenate_images\nfrom skimage.transform import resize\nfrom skimage.measure import label, regionprops\nfrom PIL import Image, ImageDraw\nfrom ast import literal_eval\nfrom tqdm.notebook import tqdm\n","execution_count":null,"outputs":[]},{"metadata":{"id":"I87vC6TrG8pg","trusted":true},"cell_type":"code","source":"IMG_WIDTH = 512\nIMG_HEIGHT = 512","execution_count":null,"outputs":[]},{"metadata":{"id":"kJm3aRvGCMv1","trusted":true},"cell_type":"code","source":"SC_FACTOR = int(1024 / IMG_WIDTH)\n","execution_count":null,"outputs":[]},{"metadata":{"id":"WzhSw-oDHAKF","trusted":true},"cell_type":"code","source":"from tqdm.notebook import tqdm","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"cd ../global-wheat-detection","execution_count":null,"outputs":[]},{"metadata":{"id":"oEjHUL-PHCYG","trusted":true},"cell_type":"code","source":"train1 = pd.read_csv('train.csv')","execution_count":null,"outputs":[]},{"metadata":{"id":"On4xVRLvHEx5","outputId":"3ab92400-bb1b-4ff3-a883-30848b00d8f1","executionInfo":{"status":"ok","timestamp":1589021188004,"user_tz":-330,"elapsed":44321,"user":{"displayName":"Aviral Pamecha","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GgiQYcZ0y7v_0iK6NLDTKBzzfseWHyM90DJwNAR=s64","userId":"09103175250081373018"}},"trusted":true},"cell_type":"code","source":"def make_polygon(coords):\n    xm, ym, w, h = coords\n    xm, ym, w, h = xm / SC_FACTOR, ym / SC_FACTOR, w / SC_FACTOR, h / SC_FACTOR   # scale values if image was downsized\n    return [(xm, ym), (xm, ym + h), (xm + w, ym + h), (xm + w, ym)]\n\nmasks = dict() # dictionnary containing all masks\n\nfor img_id, gp in tqdm(train1.groupby(\"image_id\")):\n    gp['polygons'] = gp['bbox'].apply(eval).apply(lambda x: make_polygon(x))\n\n    img = Image.new('L', (IMG_WIDTH, IMG_HEIGHT), 0)\n    for pol in gp['polygons'].values:\n        ImageDraw.Draw(img).polygon(pol, outline=1, fill=1)\n\n    mask = np.array(img, dtype=np.uint8)\n    masks[img_id] = mask","execution_count":null,"outputs":[]},{"metadata":{"id":"94rZpT8hBs2L","outputId":"fb0c6268-3326-430b-8d55-f3448735305e","executionInfo":{"status":"ok","timestamp":1589021203326,"user_tz":-330,"elapsed":949,"user":{"displayName":"Aviral Pamecha","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GgiQYcZ0y7v_0iK6NLDTKBzzfseWHyM90DJwNAR=s64","userId":"09103175250081373018"}},"trusted":true},"cell_type":"code","source":"im = Image.fromarray(masks[list(masks.keys())[7]])\nplt.imshow(im)","execution_count":null,"outputs":[]},{"metadata":{"id":"asKaChcYC1dp","outputId":"abf72558-db72-49d7-8749-ead74a29918c","executionInfo":{"status":"ok","timestamp":1589021792338,"user_tz":-330,"elapsed":6280,"user":{"displayName":"Aviral Pamecha","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GgiQYcZ0y7v_0iK6NLDTKBzzfseWHyM90DJwNAR=s64","userId":"09103175250081373018"}},"trusted":true},"cell_type":"code","source":"def show_images(images, num=2):\n    \n    images_to_show = np.random.choice(images, num)\n\n    for image_id in images_to_show:\n\n        image_path = os.path.join(train_path, image_id + \".jpg\")\n        image = Image.open(image_path)\n  \n        # get all bboxes for given image in [xmin, ymin, width, height]\n        bboxes = [literal_eval(box) for box in train1[train1['image_id'] == image_id]['bbox']]\n\n        # visualize them\n        draw = ImageDraw.Draw(image)\n        for bbox in bboxes:    \n            draw.rectangle([bbox[0], bbox[1], bbox[0] + bbox[2], bbox[1] + bbox[3]], width=3)\n\n        plt.figure(figsize = (15,15))\n        plt.imshow(image)\n        plt.show()\n\n\nunique_images = train1['image_id'].unique()\nshow_images(unique_images)","execution_count":null,"outputs":[]},{"metadata":{"id":"XzwUrQinQNG9","trusted":true},"cell_type":"code","source":"THRESH = 0.3\nmasked_preds = preds > THRESH","execution_count":null,"outputs":[]},{"metadata":{"id":"h0nqP2yTE9HU","outputId":"5cc704e7-f8e0-4282-95fd-9b238f47f4f7","executionInfo":{"status":"error","timestamp":1589026536250,"user_tz":-330,"elapsed":2588,"user":{"displayName":"Aviral Pamecha","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GgiQYcZ0y7v_0iK6NLDTKBzzfseWHyM90DJwNAR=s64","userId":"09103175250081373018"}},"trusted":true},"cell_type":"markdown","source":"n_rows = 3\n\nf, ax = plt.subplots(n_rows, 3, figsize=(14, 10))\n\nfor j, idx in enumerate([4,5,6]):\n    for k, kind in enumerate(['original', 'pred', 'masked_pred']):\n        if kind == 'original':\n            img = test[idx]\n        elif kind == 'pred':\n            img = preds[idx]\n        elif kind == 'masked_pred':\n            masked_pred = preds[idx] > .75\n            img = masked_pred\n        ax[j, k].imshow(img)\n\nplt.tight_layout()"},{"metadata":{"id":"_kzAmfLDRRXQ","trusted":false},"cell_type":"code","source":"","execution_count":0,"outputs":[]},{"metadata":{"id":"iWJUFyz_Megw","trusted":true},"cell_type":"code","source":"def get_params_from_bbox(coords, scaling_factor=1):\n    xmin, ymin = coords[1] * scaling_factor, coords[0] * scaling_factor\n    w = (coords[3] - coords[1]) * scaling_factor\n    h = (coords[2] - coords[0]) * scaling_factor\n    \n    return xmin, ymin, w, h","execution_count":null,"outputs":[]},{"metadata":{"id":"NlMF8EfGRZR6","trusted":true},"cell_type":"code","source":"bboxes = list()\n\nfor j in range(masked_preds.shape[0]):\n    label_j = label(masked_preds) \n    props = regionprops(label_j)\n    bboxes.append(props)","execution_count":null,"outputs":[]},{"metadata":{"id":"v9X0xJw3UvMU","trusted":true},"cell_type":"code","source":"sample_sub = pd.read_csv('sample_submission.csv')","execution_count":null,"outputs":[]},{"metadata":{"id":"vZa2dFUeRfMW","trusted":true},"cell_type":"code","source":"output = dict()\nfor i in range(masked_preds.shape[0]):\n    bboxes_processed = [get_params_from_bbox(bb.bbox, scaling_factor=SC_FACTOR) for bb in bboxes[i]]\n    formated_boxes = ['1.0 ' + ' '.join(map(str, bb_m)) for bb_m in bboxes_processed]\n    #if formated_boxes:\n    #    formated_boxes = formated_boxes[0] \n    \n    output[sample_sub[\"image_id\"][i]] = \" \".join(formated_boxes)\n    #output[sample_sub[\"image_id\"][i]] = formated_boxes","execution_count":null,"outputs":[]},{"metadata":{"id":"QlwnlPGdRspm","trusted":true},"cell_type":"code","source":"sample_sub[\"PredictionString\"] = output.values()\n","execution_count":null,"outputs":[]},{"metadata":{"id":"i0zoBrB_U-da","outputId":"36bf0f7b-4966-447a-e513-a873b8ec5a67","executionInfo":{"status":"ok","timestamp":1589026583479,"user_tz":-330,"elapsed":606,"user":{"displayName":"Aviral Pamecha","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GgiQYcZ0y7v_0iK6NLDTKBzzfseWHyM90DJwNAR=s64","userId":"09103175250081373018"}},"trusted":true},"cell_type":"code","source":"sample_sub\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"cd ..","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"cd ../working","execution_count":null,"outputs":[]},{"metadata":{"id":"WxVhohVzVArC","trusted":true},"cell_type":"code","source":"sample_sub.to_csv('submission.csv', index=False)\n","execution_count":null,"outputs":[]},{"metadata":{"id":"BiLWslwzVOnn","outputId":"7174f768-a4bb-489f-ee80-9cae6086a27c","executionInfo":{"status":"ok","timestamp":1589026302056,"user_tz":-330,"elapsed":1233,"user":{"displayName":"Aviral Pamecha","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GgiQYcZ0y7v_0iK6NLDTKBzzfseWHyM90DJwNAR=s64","userId":"09103175250081373018"}},"trusted":false},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"id":"pvt9gJunWQ6O","trusted":false},"cell_type":"code","source":"","execution_count":0,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.7.3"},"colab":{"name":"main.ipynb","provenance":[],"collapsed_sections":[],"machine_shape":"hm"},"accelerator":"TPU","widgets":{"application/vnd.jupyter.widget-state+json":{"0b51710f57b74d75ababe68e3c5c9b34":{"model_module":"@jupyter-widgets/controls","model_name":"HBoxModel","state":{"_view_name":"HBoxView","_dom_classes":[],"_model_name":"HBoxModel","_view_module":"@jupyter-widgets/controls","_model_module_version":"1.5.0","_view_count":null,"_view_module_version":"1.5.0","box_style":"","layout":"IPY_MODEL_d570d4440ad34c608532ba6f8ece239d","_model_module":"@jupyter-widgets/controls","children":["IPY_MODEL_5d81c29544eb42b89f74672c2fec07fe","IPY_MODEL_f4df26fca212494d851f587b248524da"]}},"d570d4440ad34c608532ba6f8ece239d":{"model_module":"@jupyter-widgets/base","model_name":"LayoutModel","state":{"_view_name":"LayoutView","grid_template_rows":null,"right":null,"justify_content":null,"_view_module":"@jupyter-widgets/base","overflow":null,"_model_module_version":"1.2.0","_view_count":null,"flex_flow":null,"width":null,"min_width":null,"border":null,"align_items":null,"bottom":null,"_model_module":"@jupyter-widgets/base","top":null,"grid_column":null,"overflow_y":null,"overflow_x":null,"grid_auto_flow":null,"grid_area":null,"grid_template_columns":null,"flex":null,"_model_name":"LayoutModel","justify_items":null,"grid_row":null,"max_height":null,"align_content":null,"visibility":null,"align_self":null,"height":null,"min_height":null,"padding":null,"grid_auto_rows":null,"grid_gap":null,"max_width":null,"order":null,"_view_module_version":"1.2.0","grid_template_areas":null,"object_position":null,"object_fit":null,"grid_auto_columns":null,"margin":null,"display":null,"left":null}},"5d81c29544eb42b89f74672c2fec07fe":{"model_module":"@jupyter-widgets/controls","model_name":"FloatProgressModel","state":{"_view_name":"ProgressView","style":"IPY_MODEL_bcd38fa63983458f9e82693ad9112f59","_dom_classes":[],"description":"100%","_model_name":"FloatProgressModel","bar_style":"success","max":3373,"_view_module":"@jupyter-widgets/controls","_model_module_version":"1.5.0","value":3373,"_view_count":null,"_view_module_version":"1.5.0","orientation":"horizontal","min":0,"description_tooltip":null,"_model_module":"@jupyter-widgets/controls","layout":"IPY_MODEL_7a02f31a41c341e8bb2f5d095c747a0d"}},"f4df26fca212494d851f587b248524da":{"model_module":"@jupyter-widgets/controls","model_name":"HTMLModel","state":{"_view_name":"HTMLView","style":"IPY_MODEL_6f1a3334c0d54353a1f0b2663cdb5040","_dom_classes":[],"description":"","_model_name":"HTMLModel","placeholder":"​","_view_module":"@jupyter-widgets/controls","_model_module_version":"1.5.0","value":" 3373/3373 [10:14&lt;00:00,  5.49it/s]","_view_count":null,"_view_module_version":"1.5.0","description_tooltip":null,"_model_module":"@jupyter-widgets/controls","layout":"IPY_MODEL_4545cda980eb4eefb705ee3e3f4f19d2"}},"bcd38fa63983458f9e82693ad9112f59":{"model_module":"@jupyter-widgets/controls","model_name":"ProgressStyleModel","state":{"_view_name":"StyleView","_model_name":"ProgressStyleModel","description_width":"initial","_view_module":"@jupyter-widgets/base","_model_module_version":"1.5.0","_view_count":null,"_view_module_version":"1.2.0","bar_color":null,"_model_module":"@jupyter-widgets/controls"}},"7a02f31a41c341e8bb2f5d095c747a0d":{"model_module":"@jupyter-widgets/base","model_name":"LayoutModel","state":{"_view_name":"LayoutView","grid_template_rows":null,"right":null,"justify_content":null,"_view_module":"@jupyter-widgets/base","overflow":null,"_model_module_version":"1.2.0","_view_count":null,"flex_flow":null,"width":null,"min_width":null,"border":null,"align_items":null,"bottom":null,"_model_module":"@jupyter-widgets/base","top":null,"grid_column":null,"overflow_y":null,"overflow_x":null,"grid_auto_flow":null,"grid_area":null,"grid_template_columns":null,"flex":null,"_model_name":"LayoutModel","justify_items":null,"grid_row":null,"max_height":null,"align_content":null,"visibility":null,"align_self":null,"height":null,"min_height":null,"padding":null,"grid_auto_rows":null,"grid_gap":null,"max_width":null,"order":null,"_view_module_version":"1.2.0","grid_template_areas":null,"object_position":null,"object_fit":null,"grid_auto_columns":null,"margin":null,"display":null,"left":null}},"6f1a3334c0d54353a1f0b2663cdb5040":{"model_module":"@jupyter-widgets/controls","model_name":"DescriptionStyleModel","state":{"_view_name":"StyleView","_model_name":"DescriptionStyleModel","description_width":"","_view_module":"@jupyter-widgets/base","_model_module_version":"1.5.0","_view_count":null,"_view_module_version":"1.2.0","_model_module":"@jupyter-widgets/controls"}},"4545cda980eb4eefb705ee3e3f4f19d2":{"model_module":"@jupyter-widgets/base","model_name":"LayoutModel","state":{"_view_name":"LayoutView","grid_template_rows":null,"right":null,"justify_content":null,"_view_module":"@jupyter-widgets/base","overflow":null,"_model_module_version":"1.2.0","_view_count":null,"flex_flow":null,"width":null,"min_width":null,"border":null,"align_items":null,"bottom":null,"_model_module":"@jupyter-widgets/base","top":null,"grid_column":null,"overflow_y":null,"overflow_x":null,"grid_auto_flow":null,"grid_area":null,"grid_template_columns":null,"flex":null,"_model_name":"LayoutModel","justify_items":null,"grid_row":null,"max_height":null,"align_content":null,"visibility":null,"align_self":null,"height":null,"min_height":null,"padding":null,"grid_auto_rows":null,"grid_gap":null,"max_width":null,"order":null,"_view_module_version":"1.2.0","grid_template_areas":null,"object_position":null,"object_fit":null,"grid_auto_columns":null,"margin":null,"display":null,"left":null}}}}},"nbformat":4,"nbformat_minor":4}