{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceType":"competition","sourceId":5916,"databundleVersionId":45048},{"sourceType":"datasetVersion","sourceId":8707973,"datasetId":5223384,"databundleVersionId":8861017},{"sourceType":"datasetVersion","sourceId":8707982,"datasetId":5223375,"databundleVersionId":8861026}],"dockerImageVersionId":30732,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"just make the script below into notebook. no original point.\n\nhttps://www.kaggle.com/code/amanbh/visualize-polygons-and-image-data","metadata":{}},{"cell_type":"code","source":"\"\"\"\nAuthor : amanbh\n\n- Set up some basic functions to load/manipulate image data\n- Visualize/Summarize cType counts, training data, and true classes\n- Plot Polygons with holes correctly by using descartes package\n\nBased on Kernel by\n    Author : Oleg Medvedev\n    Link   : https://www.kaggle.com/torrinos/dstl-satellite-imagery-feature-detection/exploration-and-plotting/run/553107\n\"\"\"\n\nimport pandas as pd\nimport numpy as np\n\nfrom shapely.wkt import loads as wkt_loads\nfrom matplotlib.patches import Polygon, Patch\n\n# decartes package makes plotting with holes much easier\nfrom descartes.patch import PolygonPatch\n\nimport matplotlib.pyplot as plt\nimport tifffile as tiff\n\nimport pylab\n# turn interactive mode on so that plots immediately\n# See: http://stackoverflow.com/questions/2130913/no-plot-window-in-matplotlib\n# pylab.ion()","metadata":{"execution":{"iopub.status.busy":"2024-06-17T08:22:31.853979Z","iopub.execute_input":"2024-06-17T08:22:31.854589Z","iopub.status.idle":"2024-06-17T08:22:32.749645Z","shell.execute_reply.started":"2024-06-17T08:22:31.854545Z","shell.execute_reply":"2024-06-17T08:22:32.748091Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"- CSVファイルだけならサイズが50MBなのでworkingにunpackしても問題ない。\n    - 画像データのunpackは不可能 (workingの20GB制限を軽く超える) なので一度ローカルにダウンロードして自分でデータセットを作るかGCPに課金。\n    - あるいは真面目に技術的に頑張る。参考文献: https://qiita.com/Hanjin_Liu/items/7a01c1c481161fe20a3e\n        - 多分これ真面目に頑張ってKaggle Notebookで実装しきれば役には立つんだろうな...\n- inputディレクトリは書き込み不可。","metadata":{}},{"cell_type":"code","source":"import shutil\nshutil.unpack_archive(\"/kaggle/input/dstl-satellite-imagery-feature-detection/train_wkt_v4.csv.zip\", \"/kaggle/working\")\nshutil.unpack_archive(\"/kaggle/input/dstl-satellite-imagery-feature-detection/grid_sizes.csv.zip\", \"/kaggle/working\")","metadata":{"execution":{"iopub.status.busy":"2024-06-17T08:22:32.752176Z","iopub.execute_input":"2024-06-17T08:22:32.752949Z","iopub.status.idle":"2024-06-17T08:22:33.242667Z","shell.execute_reply.started":"2024-06-17T08:22:32.752912Z","shell.execute_reply":"2024-06-17T08:22:33.241431Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Give short names, sensible colors and zorders to object types\nCLASSES = {\n        1 : 'Bldg',\n        2 : 'Struct',\n        3 : 'Road',\n        4 : 'Track',\n        5 : 'Trees',\n        6 : 'Crops',\n        7 : 'Fast H20',\n        8 : 'Slow H20',\n        9 : 'Truck',\n        10 : 'Car',\n        }\nCOLORS = {\n        1 : '0.7',\n        2 : '0.4',\n        3 : '#b35806',\n        4 : '#dfc27d',\n        5 : '#1b7837',\n        6 : '#a6dba0',\n        7 : '#74add1',\n        8 : '#4575b4',\n        9 : '#f46d43',\n        10: '#d73027',\n        }\nZORDER = {\n        1 : 5,\n        2 : 5,\n        3 : 4,\n        4 : 1,\n        5 : 3,\n        6 : 2,\n        7 : 7,\n        8 : 8,\n        9 : 9,\n        10: 10,\n        }","metadata":{"execution":{"iopub.status.busy":"2024-06-17T08:22:33.245266Z","iopub.execute_input":"2024-06-17T08:22:33.245938Z","iopub.status.idle":"2024-06-17T08:22:33.257734Z","shell.execute_reply.started":"2024-06-17T08:22:33.245874Z","shell.execute_reply":"2024-06-17T08:22:33.255824Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# read the training data from train_wkt_v4.csv\ninDir = \"../working\"\n\ndf = pd.read_csv(inDir + '/train_wkt_v4.csv')\nprint(df.head())\n\n# grid size will also be needed later..\ngs = pd.read_csv(inDir + '/grid_sizes.csv', names=['ImageId', 'Xmax', 'Ymin'], skiprows=1)\nprint(gs.head())\n\n# imageIds in a DataFrame\nallImageIds = gs.ImageId.unique()\ntrainImageIds = df.ImageId.unique()","metadata":{"execution":{"iopub.status.busy":"2024-06-17T08:22:54.638824Z","iopub.execute_input":"2024-06-17T08:22:54.639317Z","iopub.status.idle":"2024-06-17T08:22:55.164608Z","shell.execute_reply.started":"2024-06-17T08:22:54.639280Z","shell.execute_reply":"2024-06-17T08:22:55.163201Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trainImageIds","metadata":{"execution":{"iopub.status.busy":"2024-06-17T08:22:57.958597Z","iopub.execute_input":"2024-06-17T08:22:57.959081Z","iopub.status.idle":"2024-06-17T08:22:57.969869Z","shell.execute_reply.started":"2024-06-17T08:22:57.959044Z","shell.execute_reply":"2024-06-17T08:22:57.968200Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_image_names(imageId):\n    '''\n    Get the names of the tiff files\n    '''\n    #  作成したデータセットの名前などで変わりうるので適宜変更が必要\n    _inDir = \"../input\"\n    d = {'3': '{}/dstl-three-band-id-0/{}.tif'.format(_inDir, imageId),\n         'A': '{}/dstl-16-band-id-0/{}_A.tif'.format(_inDir, imageId),\n         'M': '{}/dstl-16-band-id-0/{}_M.tif'.format(_inDir, imageId),\n         'P': '{}/dstl-16-band-id-0/{}_P.tif'.format(_inDir, imageId),\n         }\n    return d\n\n\ndef get_images(imageId, img_key = None):\n    '''\n    Load images correspoding to imageId\n\n    Parameters\n    ----------\n    imageId : str\n        imageId as used in grid_size.csv\n    img_key : {None, '3', 'A', 'M', 'P'}, optional\n        Specify this to load single image\n        None loads all images and returns in a dict\n        '3' loads image from three_band/\n        'A' loads '_A' image from sixteen_band/\n        'M' loads '_M' image from sixteen_band/\n        'P' loads '_P' image from sixteen_band/\n\n    Returns\n    -------\n    images : dict\n        A dict of image data from TIFF files as numpy array\n    '''\n\n    img_names = get_image_names(imageId)\n    images = dict()\n\n    if img_key is None:\n        # tiff形式の画像もライブラリがあればすぐ読める。\n        for k in img_names.keys():\n            images[k] = tiff.imread(img_names[k])\n    else:\n        images[img_key] = tiff.imread(img_names[img_key])\n\n    return images\n\n\ndef get_size(imageId):\n    \"\"\"\n    Get the grid size of the image\n\n    Parameters\n    ----------\n    imageId : str\n        imageId as used in grid_size.csv\n    \"\"\"\n\n    xmax, ymin = gs[gs.ImageId == imageId].iloc[0,1:].astype(float)\n    W, H = get_images(imageId, '3')['3'].shape[1:]\n    return (xmax, ymin, W, H)\n\n\ndef is_training_image(imageId):\n    '''\n    Returns\n    -------\n    is_training_image : bool\n        True if imageId belongs to training data\n    '''\n    return any(trainImageIds == imageId)\n\n\ndef plot_polygons(fig, ax, polygonsList):\n    '''\n    Plot descrates.PolygonPatch from list of polygons objs for each CLASS\n    '''\n    \n    legend_patches = []\n    \n    for cType in polygonsList:\n        # CLASSESに含まれるポリゴンが各々何個あるかを示す凡例 (ledgend) を作成\n        print('{} : {} \\t count = {}'.format(cType, CLASSES[cType], len(polygonsList[cType])))\n        legend_patches.append(Patch(color=COLORS[cType],\n                                    label='{} ({})'.format(CLASSES[cType], len(polygonsList[cType]))))\n\n        for polygon in polygonsList[cType]:\n            mpl_poly = PolygonPatch(polygon,\n                                    color=COLORS[cType],\n                                    lw=0,\n                                    alpha=0.7,\n                                    zorder=ZORDER[cType])\n            ax.add_patch(mpl_poly)\n    # ax.relim()\n    ax.autoscale_view()\n    ax.set_title('Objects')\n    ax.set_xticks([])\n    ax.set_yticks([])\n    return legend_patches\n\n\ndef plot_image(fig, ax, imageId, img_key, selected_channels=None):\n    '''\n    Plot get_images(imageId)[image_key] on axis/fig\n    Optional: select which channels of the image are used (used for sixteen_band/ images)\n    Parameters\n    ----------\n    img_key : str, {'3', 'P', 'N', 'A'}\n        See get_images for description.\n    '''\n    images = get_images(imageId, img_key)\n    img = images[img_key]\n    title_suffix = ''\n    if selected_channels is not None:\n        # 16バンドの画像を読み込みたいときだけ使うので説明は省略\n        # reprはstrの仲間: https://gammasoft.jp/blog/use-diffence-str-and-repr-python/\n        img = img[selected_channels]\n        title_suffix = ' (' + ','.join([ repr(i) for i in selected_channels ]) + ')'\n    if len(img.shape) == 2:\n        new_img = np.zeros((3, img.shape[0], img.shape[1]))\n        new_img[0] = img\n        new_img[1] = img\n        new_img[2] = img\n        img = new_img\n    \n    tiff.imshow(img, figure=fig, subplot=ax)\n    ax.set_title(imageId + ' - ' + img_key + title_suffix)\n    ax.set_xlabel(img.shape[-2])\n    ax.set_ylabel(img.shape[-1])\n    ax.set_xticks([])\n    ax.set_yticks([])\n\n\n\ndef visualize_image(imageId, plot_all=True):\n    '''         \n    Plot all images and object-polygons\n    \n    Parameters\n    ----------\n    imageId : str\n        imageId as used in grid_size.csv\n    plot_all : bool, True by default\n        If True, plots all images (from three_band/ and sixteen_band/) as subplots.\n        Otherwise, only plots Polygons.\n    '''         \n    df_image = df[df.ImageId == imageId]\n    xmax, ymin, W, H = get_size(imageId)\n    \n    if plot_all:\n        fig, axArr = plt.subplots(figsize=(10, 10), nrows=3, ncols=3)\n        ax = axArr[0][0]\n    else:\n        fig, axArr = plt.subplots(figsize=(10, 10))\n        ax = axArr\n\n    if is_training_image(imageId):\n        print('ImageId : {}'.format(imageId))\n        polygonsList = {}\n        for cType in CLASSES.keys():\n            # 各クラス番号をすべてiterateしてWKT形式のポリゴンを取ってくる\n            polygonsList[cType] = wkt_loads(df_image[df_image.ClassType == cType].MultipolygonWKT.values[0])\n            # print(wkt_loads(df_image[df_image.ClassType == cType].MultipolygonWKT.values[0]))\n        legend_patches = plot_polygons(fig, ax, polygonsList)\n        ax.set_xlim(0, xmax)\n        ax.set_ylim(ymin, 0)\n        ax.set_xlabel(xmax)\n        ax.set_ylabel(ymin)\n    if plot_all:\n        plot_image(fig, axArr[0][1], imageId, '3')\n        plot_image(fig, axArr[0][2], imageId, 'P')\n        plot_image(fig, axArr[1][0], imageId, 'A', [0, 3, 6])\n        plot_image(fig, axArr[1][1], imageId, 'A', [1, 4, 7])\n        plot_image(fig, axArr[1][2], imageId, 'A', [2, 5, 0])\n        plot_image(fig, axArr[2][0], imageId, 'M', [0, 3, 6])\n        plot_image(fig, axArr[2][1], imageId, 'M', [1, 4, 7])\n        plot_image(fig, axArr[2][2], imageId, 'M', [2, 5, 0])\n\n    if is_training_image(imageId):\n        ax.legend(handles=legend_patches,\n                   # loc='upper center',\n                   bbox_to_anchor=(0.9, 1),\n                   bbox_transform=plt.gcf().transFigure,\n                   ncol=5,\n                   fontsize='x-small',\n                   title='Objects-' + imageId,\n                   # mode=\"expand\",\n                   framealpha=0.3)\n    return (fig, axArr, ax)","metadata":{"execution":{"iopub.status.busy":"2024-06-17T08:22:59.693807Z","iopub.execute_input":"2024-06-17T08:22:59.694230Z","iopub.status.idle":"2024-06-17T08:22:59.731302Z","shell.execute_reply.started":"2024-06-17T08:22:59.694195Z","shell.execute_reply":"2024-06-17T08:22:59.729531Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Loop over few training images and save to files\nfor imageId in trainImageIds:\n    try:\n        fig, axArr, ax = visualize_image(imageId, plot_all=False)\n        plt.savefig('Objects-' + imageId + '.png')\n        plt.clf()\n    except Exception as e:\n        print(e)\n\n\n# Optionally, view images immediately:\npylab.show()\n# Uncomment to show plot when interactive mode is off \n# (this function blocks till fig is closed)","metadata":{"execution":{"iopub.status.busy":"2024-06-17T08:23:05.347661Z","iopub.execute_input":"2024-06-17T08:23:05.348128Z","iopub.status.idle":"2024-06-17T08:23:21.010647Z","shell.execute_reply.started":"2024-06-17T08:23:05.348090Z","shell.execute_reply":"2024-06-17T08:23:21.008433Z"},"trusted":true},"execution_count":null,"outputs":[]}]}