{"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":"code","source":"#Ship detection data visualization and analysis\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport cv2\nfrom skimage.data import imread\nfrom pathlib import Path\nfrom skimage.measure import label, regionprops\nfrom skimage.morphology import label","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-08-19T07:39:46.14984Z","iopub.execute_input":"2022-08-19T07:39:46.150294Z","iopub.status.idle":"2022-08-19T07:39:47.885014Z","shell.execute_reply.started":"2022-08-19T07:39:46.150248Z","shell.execute_reply":"2022-08-19T07:39:47.883994Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('../input/airbus-ship-detection/train_ship_segmentations_v2.csv')\ntrain.head()\n","metadata":{"_uuid":"00d746de6b1488cd953a08bf1ce68bebb7c68749","execution":{"iopub.status.busy":"2022-08-19T07:39:55.737352Z","iopub.execute_input":"2022-08-19T07:39:55.737709Z","iopub.status.idle":"2022-08-19T07:39:57.00789Z","shell.execute_reply.started":"2022-08-19T07:39:55.737663Z","shell.execute_reply":"2022-08-19T07:39:57.006834Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from skimage.feature import hog\nimport random","metadata":{"execution":{"iopub.status.busy":"2022-08-19T07:40:01.226669Z","iopub.execute_input":"2022-08-19T07:40:01.227304Z","iopub.status.idle":"2022-08-19T07:40:01.313132Z","shell.execute_reply.started":"2022-08-19T07:40:01.22725Z","shell.execute_reply":"2022-08-19T07:40:01.312018Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def rle_decode(mask_rle, shape=(768, 768)):\n    s = mask_rle.split()\n    starts, lengths = [np.asarray(x, dtype=int) for x in (s[0:][::2], s[1:][::2])]\n    #s[::2] means every two elements\n    starts -= 1\n    ends = starts + lengths\n    img = np.zeros(shape[0]*shape[1], dtype=np.uint8)\n    for start, end in zip(starts, ends):\n        img[start:end] = 1\n    return img.reshape(shape).T  # Needed to align to RLE direction\n\ndef reshapeLabel(label) :\n    res = np.zeros((48, 48))\n    for i in range(48) :\n        for j in range(48) :\n            sub_label = int(label[i*16:(i+1)*16][j*16:(j+1)*16].sum() > 50)\n            res[i][j] = sub_label\n    return res\n\nX = []\ny = []\nmaxIm = 2000\nimport tqdm\nfor ind, row in tqdm.tqdm(train.dropna().iterrows()) :\n    image = row['ImageId']\n    img = imread(f'../input/airbus-ship-detection/train_v2/{image}')\n    im = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)    \n    hogres = hog(im, orientations=16, pixels_per_cell=(32, 32),\n                        cells_per_block=(1, 1))\n    hogres = np.reshape(hogres, (24, 24, 16))\n    labels = row['EncodedPixels']\n    if not type(labels) == float:\n        label = rle_decode(labels, shape=(768, 768))\n        finlabel = reshapeLabel(label)\n        for i in range(12) :\n            for j in range(12) :\n                if finlabel[i][j] == 0 :\n                    if random.random() > 0.97 :\n                        X.append(hogres[i][j])\n                        y.append(finlabel[i][j])\n                else : \n                    X.append(hogres[i][j])\n                    y.append(finlabel[i][j])       \n        if ind > maxIm :\n            break","metadata":{"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(np.asarray(y).sum())\nprint(len(y))","metadata":{"execution":{"iopub.status.busy":"2022-08-19T09:47:04.942052Z","iopub.execute_input":"2022-08-19T09:47:04.942387Z","iopub.status.idle":"2022-08-19T09:47:04.948905Z","shell.execute_reply.started":"2022-08-19T09:47:04.942328Z","shell.execute_reply":"2022-08-19T09:47:04.948053Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.1, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2022-08-19T09:47:05.563552Z","iopub.execute_input":"2022-08-19T09:47:05.563865Z","iopub.status.idle":"2022-08-19T09:47:05.570829Z","shell.execute_reply.started":"2022-08-19T09:47:05.563824Z","shell.execute_reply":"2022-08-19T09:47:05.57006Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nfrom sklearn.pipeline import make_pipeline\nfrom sklearn.preprocessing import StandardScaler\nfrom sklearn.svm import SVC\nfrom sklearn.ensemble import GradientBoostingClassifier\n\nclf = GradientBoostingClassifier(n_estimators=100, learning_rate=1.0, max_depth=16, random_state=0)\n# clf = SVC(gamma='auto')\n\nclf.fit(X_train, y_train)\n\npreds = clf.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2022-08-19T09:47:09.752209Z","iopub.execute_input":"2022-08-19T09:47:09.752501Z","iopub.status.idle":"2022-08-19T09:47:12.11131Z","shell.execute_reply.started":"2022-08-19T09:47:09.752461Z","shell.execute_reply":"2022-08-19T09:47:12.110311Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import classification_report\nprint(classification_report(y_test, preds))","metadata":{"execution":{"iopub.status.busy":"2022-08-19T09:47:29.60378Z","iopub.execute_input":"2022-08-19T09:47:29.604091Z","iopub.status.idle":"2022-08-19T09:47:29.610356Z","shell.execute_reply.started":"2022-08-19T09:47:29.604041Z","shell.execute_reply":"2022-08-19T09:47:29.609548Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.listdir(\"../input/anoopharborimages/\")","metadata":{"execution":{"iopub.status.busy":"2022-08-19T09:47:32.728072Z","iopub.execute_input":"2022-08-19T09:47:32.728644Z","iopub.status.idle":"2022-08-19T09:47:32.736925Z","shell.execute_reply.started":"2022-08-19T09:47:32.728593Z","shell.execute_reply":"2022-08-19T09:47:32.736155Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image = 'singaporeapril_2021.png'\n\nfrom PIL import Image\nim = Image.open(f'../input/anoopharborimages/{image}').resize((768, 768))\nim = cv2.cvtColor(np.array(im), cv2.COLOR_BGR2GRAY)\nhogres = hog(im, orientations=16, pixels_per_cell=(32, 32),\n                    cells_per_block=(1, 1))\nhogres = np.reshape(hogres, (24, 24, 16))\nx_inference = []\nfor i in range(24) :\n    for j in range(24) :\n        x_inference.append(hogres[i][j])\nres = clf.predict(x_inference)\nprint(res.sum())\n\nimport cv2\nimport numpy as np\nfrom matplotlib.pyplot import imshow\n\n# read image\nim = Image.open(f'../input/anoopharborimages/{image}').resize((768, 768))\nim = np.array(im)\nind = 0\nw = 32\nh = 32\nfor i in range(24) :\n    for j in range(24):\n        if res[ind] == 1 :\n#             print(\"adding\")\n            x = i*32\n            y = j*32\n            im = cv2.rectangle(im,(x,y),(x+w,y+h),(150,255,150),2)\n        ind +=1\nfig, ax = plt.subplots(figsize=(10, 10))     \nax.imshow(im)","metadata":{"execution":{"iopub.status.busy":"2022-08-19T09:47:33.4432Z","iopub.execute_input":"2022-08-19T09:47:33.443691Z","iopub.status.idle":"2022-08-19T09:47:34.733624Z","shell.execute_reply.started":"2022-08-19T09:47:33.443643Z","shell.execute_reply":"2022-08-19T09:47:34.73275Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image = train.iloc[270]['ImageId']\n\nfrom PIL import Image\nim = Image.open(f'../input/airbus-ship-detection/train_v2/{image}')\nim = cv2.cvtColor(np.array(im), cv2.COLOR_BGR2GRAY)\nhogres = hog(im, orientations=16, pixels_per_cell=(32, 32),\n                    cells_per_block=(1, 1))\nhogres = np.reshape(hogres, (24, 24, 16))\nx_inference = []\nfor i in range(24) :\n    for j in range(24) :\n        x_inference.append(hogres[i][j])\nres = clf.predict(x_inference)\nprint(res.sum())\n\nimport cv2\nimport numpy as np\nfrom matplotlib.pyplot import imshow\n\n# read image\nim = Image.open(f'../input/airbus-ship-detection/train_v2/{image}')\nim = np.array(im)\nind = 0\nw = 32\nh = 32\nfor i in range(24) :\n    for j in range(24):\n        if res[ind] == 1 :\n#             print(\"adding\")\n            x = i*32\n            y = j*32\n            im = cv2.rectangle(im,(x,y),(x+w,y+h),(150,255,150),2)\n        ind +=1\nfig, ax = plt.subplots(figsize=(10, 10))     \nax.imshow(im)","metadata":{"execution":{"iopub.status.busy":"2022-08-19T09:48:10.734088Z","iopub.execute_input":"2022-08-19T09:48:10.734417Z","iopub.status.idle":"2022-08-19T09:48:11.789895Z","shell.execute_reply.started":"2022-08-19T09:48:10.734364Z","shell.execute_reply":"2022-08-19T09:48:11.788426Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}