{"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":"markdown","source":"# <B><u>Plotly/Matplotlib による Whale&Dolphin データセットのプレビュー</u></B>","metadata":{}},{"cell_type":"markdown","source":"# [1] はじめに","metadata":{}},{"cell_type":"markdown","source":"このノートブックでは、Kaggle Competition の \"Happywhale - Whale and Dolphin Identification\"のTraining(訓練)の方針やData augumentation(データ拡張)のやり方等々についての知見やヒントを得るため、<u>[Plotly](https://plotly.com/python/)</u>と<u>[Matplotlib](https://matplotlib.org/)</u>を用いて、データセットを可視化しています。参考として頂けますと幸いです。","metadata":{}},{"cell_type":"markdown","source":"このノートブックで紹介するデータ／図表の一覧を、以下に示します。","metadata":{}},{"cell_type":"markdown","source":"< Train images(訓練用の画像)に係るデータ／図表 >\n- Train images(訓練用の画像)の数\n- Species(種)の数\n- Species(種)毎の、Train images(訓練用の画像)／Individuals(個体)の数をまとめた図表(Plotlyを使用)\n- Individuals(個体)毎の、Train images(訓練用の画像)の数をまとめた図表(Plotlyを使用)\n- Species(種)／Individuals(個体)毎の、Train images(訓練用の画像)のサンプル(Matplotlibを使用)","metadata":{}},{"cell_type":"markdown","source":"< Test images(検証用の画像)に係るデータ／図表 >\n- Test images(検証用の画像)の数\n- Test images(検証用の画像)のサンプル(Matplotlibを使用)","metadata":{}},{"cell_type":"markdown","source":"注意1：著者は、Kaggle／機械学習／Python の初心者です。したがって、内容は、初心者の方々に向けたものとなっています。また、内容に、バグや間違いが含まれている可能性があります。より良いノートブックとするため、忌憚のないご意見／コメントを頂けますと甚大です。","metadata":{}},{"cell_type":"markdown","source":"注意2：初心者の方々には、Kaggle Competition用のデータの読み込み、Pandasによるデータの処理、Numpy/Pillowによる画像データの取り扱い、Matplotlib/Plotlyによるデータの可視化 等々が参考になるかと思います。","metadata":{}},{"cell_type":"markdown","source":"注意3：このノートブックは、別途作成した<u>[英語のノートブック](https://www.kaggle.com/acchiko/preview-of-whale-dolphin-dataset-with-plotly-matpl?scriptVersionId=88631402)</u>を日本語に変更したものです。","metadata":{}},{"cell_type":"markdown","source":"# [2] データセットの準備","metadata":{}},{"cell_type":"markdown","source":"\"Happywhale - Whale and Dolphin Identification\" のデータセットは、ノートブックのサイドバーを開き、以下のボタンを順にクリックしていくと、(ノートブック内のコードから、)アクセスできるようになります。","metadata":{}},{"cell_type":"markdown","source":"###  \"+ Add data\" -> \"Competition Data\" -> \"Add (Happywhale - Whale and Dolphin Identification)\".","metadata":{}},{"cell_type":"markdown","source":"上記の手順がうまくいくと、以下のパスから、データにアクセスできるようになります。","metadata":{}},{"cell_type":"code","source":"path_to_inputs = \"/kaggle/input/happy-whale-and-dolphin\"\n!ls {path_to_inputs}","metadata":{"execution":{"iopub.status.busy":"2022-02-25T08:37:50.813488Z","iopub.execute_input":"2022-02-25T08:37:50.814596Z","iopub.status.idle":"2022-02-25T08:37:51.120872Z","shell.execute_reply.started":"2022-02-25T08:37:50.814450Z","shell.execute_reply":"2022-02-25T08:37:51.119864Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"上記の、csvファイル(画像に関する情報をまとめたメタデータ)の内容を、以下に示します。","metadata":{}},{"cell_type":"code","source":"!head {path_to_inputs}/sample_submission.csv","metadata":{"execution":{"iopub.status.busy":"2022-02-25T08:37:51.122878Z","iopub.execute_input":"2022-02-25T08:37:51.123282Z","iopub.status.idle":"2022-02-25T08:37:51.421419Z","shell.execute_reply.started":"2022-02-25T08:37:51.123245Z","shell.execute_reply":"2022-02-25T08:37:51.420849Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!head {path_to_inputs}/train.csv","metadata":{"execution":{"iopub.status.busy":"2022-02-25T08:37:51.422632Z","iopub.execute_input":"2022-02-25T08:37:51.422941Z","iopub.status.idle":"2022-02-25T08:37:51.698616Z","shell.execute_reply.started":"2022-02-25T08:37:51.422919Z","shell.execute_reply":"2022-02-25T08:37:51.697673Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"上記の、訓練用／検証用の画像のリストを、以下に示します。","metadata":{}},{"cell_type":"code","source":"!ls {path_to_inputs}/train_images | head","metadata":{"execution":{"iopub.status.busy":"2022-02-25T08:37:51.700749Z","iopub.execute_input":"2022-02-25T08:37:51.700964Z","iopub.status.idle":"2022-02-25T08:37:53.044598Z","shell.execute_reply.started":"2022-02-25T08:37:51.700935Z","shell.execute_reply":"2022-02-25T08:37:53.043582Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!ls {path_to_inputs}/test_images | head","metadata":{"execution":{"iopub.status.busy":"2022-02-25T08:37:53.046838Z","iopub.execute_input":"2022-02-25T08:37:53.047139Z","iopub.status.idle":"2022-02-25T08:37:54.074536Z","shell.execute_reply.started":"2022-02-25T08:37:53.047106Z","shell.execute_reply":"2022-02-25T08:37:54.073755Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"上記の、訓練用／検証用の画像の数を、以下に示します。","metadata":{}},{"cell_type":"code","source":"!echo \"Number of train_images:\"\n!ls {path_to_inputs}/train_images | cat -n | tail -1 | cut -f1\n!echo \"\"\n!echo \"Number of test_images:\"\n!ls {path_to_inputs}/test_images | cat -n | tail -1 | cut -f1","metadata":{"execution":{"iopub.status.busy":"2022-02-25T08:37:54.076033Z","iopub.execute_input":"2022-02-25T08:37:54.076228Z","iopub.status.idle":"2022-02-25T08:37:55.687143Z","shell.execute_reply.started":"2022-02-25T08:37:54.076197Z","shell.execute_reply":"2022-02-25T08:37:55.686116Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"訓練用の画像については、csvファイル(画像に関する情報をまとめたメタデータ)が用意されていますが、検証の画像については、該当のファイルが用意されていないため、新たに作成します。","metadata":{}},{"cell_type":"code","source":"# Generates metadata for test images. \npath_to_test_metadata = \"/kaggle/working/test.csv\"\n\n!echo \"image,species,individual_id\" > {path_to_test_metadata}\n!ls {path_to_inputs}/test_images | sed \"s/.jpg/.jpg,unknown,unknown/g\" >> {path_to_test_metadata}\n\n# Shows contents of generated metadata.\n!head {path_to_test_metadata}","metadata":{"execution":{"iopub.status.busy":"2022-02-25T08:37:55.688595Z","iopub.execute_input":"2022-02-25T08:37:55.689418Z","iopub.status.idle":"2022-02-25T08:37:56.589646Z","shell.execute_reply.started":"2022-02-25T08:37:55.689390Z","shell.execute_reply":"2022-02-25T08:37:56.589116Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# [3] Train images(訓練用の画像)の確認","metadata":{}},{"cell_type":"markdown","source":"Train images(訓練用の画像)と以下の統計データを示します。\n- Species(種)の数\n- Species(種)毎の、Train images(訓練用の画像)／Individuals(個体)の数をまとめた図表(Plotlyを使用)\n- Individuals(個体)毎の、Train images(訓練用の画像)の数をまとめた図表(Plotlyを使用)","metadata":{}},{"cell_type":"markdown","source":"## [3-1] Train images(訓練用の画像)に係るデータセットの読み込み","metadata":{}},{"cell_type":"code","source":"# Installs required libraries.\n!pip install numpy\n!pip install pandas\n!pip install matplotlib\n!pip install Pillow\n!pip install plotly","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-02-25T08:37:56.590762Z","iopub.execute_input":"2022-02-25T08:37:56.591564Z","iopub.status.idle":"2022-02-25T08:38:34.823186Z","shell.execute_reply.started":"2022-02-25T08:37:56.591539Z","shell.execute_reply":"2022-02-25T08:38:34.822618Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Import required libraries.\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nfrom PIL import Image, ImageDraw\nimport plotly.graph_objects as go\nfrom plotly.subplots import make_subplots\n#import IPython","metadata":{"execution":{"iopub.status.busy":"2022-02-25T08:38:34.824454Z","iopub.execute_input":"2022-02-25T08:38:34.824706Z","iopub.status.idle":"2022-02-25T08:38:34.848326Z","shell.execute_reply.started":"2022-02-25T08:38:34.824651Z","shell.execute_reply":"2022-02-25T08:38:34.847747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Defines the class to load metadata and images and to process those.\nclass WhaleAndDolphin():\n    def __init__(self, path_to_metadata, path_to_dir_images):\n        self._path_to_metadata = path_to_metadata\n        self._path_to_dir_images = path_to_dir_images\n        self._metadata = pd.read_csv(path_to_metadata)\n        \n    def getAllSpecies(self):\n        return self._metadata[\"species\"].unique()\n    \n    def sliceMetadata(self, query):\n        return self._metadata.query(query).reset_index(drop=True)\n    \n    def getAllIndividualIDs(self, metadata):\n        return metadata[\"individual_id\"].unique()\n    \n    def showImagesTile(self, metadata, num_cols=4):\n        num_rows = len(metadata) // num_cols + 1\n        fig = plt.figure(figsize=(6.4 * num_cols, 4.8 * num_rows))\n        \n        for row in metadata.itertuples():\n            ax = fig.add_subplot(num_rows, num_cols, row.Index + 1)\n            \n            title = self.getTitle(row)\n            ax.set_title(title)\n            \n            image = self.getImage(row)\n            plt.imshow(image)\n            \n        plt.show()\n        plt.clf()\n        plt.close()\n        \n    def getTitle(self, metadata_row):\n        #return self._title(metadata_row.individual_id, metadata_row.species)\n        return metadata_row.image\n    \n    def getImage(self, metadata_row):\n        path_to_image = self._pathToImage(self._path_to_dir_images, \\\n                                          metadata_row.image)\n        return Image.open(path_to_image)\n    \n    def _title(self, individual_id, species):\n        return \"%s (%s)\" % (individual_id, species)\n    \n    def _pathToImage(self, path_to_dir_images, file_name):\n        return \"%s/%s\" % (path_to_dir_images, file_name)\n    \n    def getImageArray(self, metadata_row):\n        image_pil = self.getImage(metadata_row)\n        return np.array(image_pil)\n    \n    def showIndividualImagesTile(self, metadata, num_cols=3, \\\n                                 max_num_individual_images=3, \\\n                                 max_num_individuals=10):\n        individual_ids = self.getAllIndividualIDs(metadata)\n        for individual_id in individual_ids[:max_num_individuals]:\n            print()\n            print(\"Individual ID : %s\" % individual_id)\n            metadata_individual = \\\n                metadata.query(\"individual_id == @individual_id\").reset_index(drop=True)\n            self.showImagesTile(\n                metadata=metadata_individual[:max_num_individual_images], \\\n                num_cols=num_cols \\\n            )","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-02-25T08:38:34.851490Z","iopub.execute_input":"2022-02-25T08:38:34.851755Z","iopub.status.idle":"2022-02-25T08:38:34.868555Z","shell.execute_reply.started":"2022-02-25T08:38:34.851726Z","shell.execute_reply":"2022-02-25T08:38:34.867378Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"訓練用の画像に係るデータセットを読み込みます。検証用の画像に係るデータセットは、後段で、同じクラスを使って読み込みます。","metadata":{}},{"cell_type":"code","source":"# Loads metadata for train images.\npath_to_metadata = \"%s/train.csv\" % path_to_inputs\npath_to_dir_images = \"%s/train_images\" % path_to_inputs\n\nwhale_and_dolphin = WhaleAndDolphin(\n    path_to_metadata=path_to_metadata,\n    path_to_dir_images=path_to_dir_images\n)","metadata":{"execution":{"iopub.status.busy":"2022-02-25T08:38:34.869867Z","iopub.execute_input":"2022-02-25T08:38:34.870059Z","iopub.status.idle":"2022-02-25T08:38:35.006647Z","shell.execute_reply.started":"2022-02-25T08:38:34.870038Z","shell.execute_reply":"2022-02-25T08:38:35.006128Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## [3-2] Train images(訓練用の画像)に係る統計データ","metadata":{}},{"cell_type":"markdown","source":"### [3-2-1] Species(種)の数","metadata":{}},{"cell_type":"markdown","source":"Species(種)の数を示します。","metadata":{}},{"cell_type":"code","source":"# Shows number of species and its names.\nall_species = whale_and_dolphin.getAllSpecies()\n\nprint(\"Number of species:\")\nprint(len(all_species))\nprint()\n\nprint(\"Name of species:\")\nprint(all_species)\nprint()","metadata":{"execution":{"iopub.status.busy":"2022-02-25T08:38:35.007785Z","iopub.execute_input":"2022-02-25T08:38:35.008136Z","iopub.status.idle":"2022-02-25T08:38:35.030349Z","shell.execute_reply.started":"2022-02-25T08:38:35.008104Z","shell.execute_reply":"2022-02-25T08:38:35.029765Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### [3-2-2] Species(種)毎の、Train images(訓練用の画像)／Individuals(個体)の数","metadata":{}},{"cell_type":"markdown","source":"Plotlyを用いて、Species(種)毎の、Train images(訓練用の画像)／Individuals(個体)の数をまとめた図表を示します。","metadata":{}},{"cell_type":"code","source":"# Calculates numbers of images/individuals for each species are calculated.\nmetadata = {}\nstats_species = pd.DataFrame(columns=[\"num_of_images\", \"num_of_individuals\"], \\\n                             index=all_species)\n\nfor species in all_species:\n    # Calculates number of images for each species.\n    metadata[species] = whale_and_dolphin.sliceMetadata(query=\"species == @species\")\n    num_images = len(metadata[species])\n    \n    # Calculates number of individuals for each species. \n    individual_ids = whale_and_dolphin.getAllIndividualIDs(metadata[species])\n    num_individuals = len(individual_ids)\n    \n    # Appends the result into summary table.\n    stats_species.loc[species] = [num_images, num_individuals]\n\n# Calculates total number of images/individuals and appends the result into summary table.\nstats_species.loc[\"total\"] = [stats_species[\"num_of_images\"].sum(), stats_species[\"num_of_individuals\"].sum()]","metadata":{"execution":{"iopub.status.busy":"2022-02-25T08:38:35.031398Z","iopub.execute_input":"2022-02-25T08:38:35.031814Z","iopub.status.idle":"2022-02-25T08:38:35.217618Z","shell.execute_reply.started":"2022-02-25T08:38:35.031790Z","shell.execute_reply":"2022-02-25T08:38:35.215603Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Shows the graph of numbers of images/individuals for each species with Plotly.\nfig = go.Figure()\nfor column_name, items in stats_species[:len(stats_species)-1].iteritems():\n    trace = go.Bar(x=items.index.tolist(), y=items.tolist(), name=column_name)\n    fig.add_trace(trace)\nfig.update_layout(yaxis_title=\"Number of images/individuals for each species\")\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-02-25T08:38:35.218974Z","iopub.execute_input":"2022-02-25T08:38:35.219255Z","iopub.status.idle":"2022-02-25T08:38:35.490419Z","shell.execute_reply.started":"2022-02-25T08:38:35.219218Z","shell.execute_reply":"2022-02-25T08:38:35.489873Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Shows the table of numbers of images/individuals for each species.\nprint(\"Number of images/individuals for each species:\")\nstats_species","metadata":{"execution":{"iopub.status.busy":"2022-02-25T08:38:35.491394Z","iopub.execute_input":"2022-02-25T08:38:35.492204Z","iopub.status.idle":"2022-02-25T08:38:35.511708Z","shell.execute_reply.started":"2022-02-25T08:38:35.492166Z","shell.execute_reply":"2022-02-25T08:38:35.510408Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### [3-2-3] Individuals(個体)毎の、Train images(訓練用の画像)の数をまとめた図表","metadata":{}},{"cell_type":"markdown","source":"Plotlyを用いて、Individuals(個体)毎の、Train images(訓練用の画像)の数をまとめた図表を示します。画像の数が多く、全てを表示するのは難しいため、ここでは、例として、\"false_killer_whale\"のデータを示しています。必要に応じ、変数\"species\"の値を変えると、他の種のデータも表示することができます。","metadata":{}},{"cell_type":"code","source":"species = \"false_killer_whale\" # It can be changed to \"melon_headed_whale\", \"humpback_whale\", etc.\nindividual_ids = whale_and_dolphin.getAllIndividualIDs(metadata[species])\n\nstats_individuals = pd.DataFrame(columns=[\"num_of_images\"], index=individual_ids)\n\nfor individual_id in individual_ids:\n    # Calculates number of images for each individual and appends the result into summary table.\n    metadata_individual = metadata[species].query(\"individual_id == @individual_id\").reset_index(drop=True)\n    num_images = len(metadata_individual)\n    \n    stats_individuals.loc[individual_id] = [num_images]\n\n# Calculates total number of images/individuals and appends the result into summary table.\nstats_individuals.loc[\"total\"] = [stats_individuals[\"num_of_images\"].sum()]","metadata":{"execution":{"iopub.status.busy":"2022-02-25T08:38:35.513883Z","iopub.execute_input":"2022-02-25T08:38:35.514209Z","iopub.status.idle":"2022-02-25T08:38:35.921490Z","shell.execute_reply.started":"2022-02-25T08:38:35.514172Z","shell.execute_reply":"2022-02-25T08:38:35.920718Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Shows the graph of numbers of images for each individuals with Plotly.\nfig = go.Figure()\nfor column_name, items in stats_individuals[:len(stats_individuals)-1].iteritems():\n    trace = go.Bar(x=items.index.tolist(), y=items.tolist(), name=column_name)\n    fig.add_trace(trace)\nfig.update_layout(title_text=\"Species : %s\" % species, yaxis_title=\"Number of images for each individuals\")\nfig.update_layout(xaxis_rangeslider_visible=True)\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-02-25T08:38:35.922996Z","iopub.execute_input":"2022-02-25T08:38:35.923238Z","iopub.status.idle":"2022-02-25T08:38:35.963189Z","shell.execute_reply.started":"2022-02-25T08:38:35.923206Z","shell.execute_reply":"2022-02-25T08:38:35.962133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Unsets limitation of display of rows.\npd.set_option(\"display.max_rows\", None)\n\n# Shows the table of number of images for each individuals.\nprint(\"Number of images for each individual of %s:\" % species)\nprint(\"(Total number of individuals for %s: %d)\" % (species, len(stats_individuals)-1))\nstats_individuals","metadata":{"execution":{"iopub.status.busy":"2022-02-25T08:38:35.964170Z","iopub.execute_input":"2022-02-25T08:38:35.964361Z","iopub.status.idle":"2022-02-25T08:38:35.984289Z","shell.execute_reply.started":"2022-02-25T08:38:35.964336Z","shell.execute_reply":"2022-02-25T08:38:35.983204Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## [3-3] Train images(訓練用の画像)のサンプル","metadata":{}},{"cell_type":"markdown","source":"Plotlyを用いて、全てのSpecies(種)についての、最初の10の個体の、Train images(訓練用の画像)を示します。同じ個体の画像は、同じ行にまとめるようにしています。表示する画像の数は、num_cols等の変数の値を変えることで、調整することができます。","metadata":{}},{"cell_type":"code","source":"# Shows train images for the first 10 individuals for each species.\nnum_cols = 4\nmax_num_individual_images = 4\nmax_num_individuals = 10\n\nfor species in all_species:\n    print()\n    print(\"--------------------------------------------------\")\n    print()\n    print(\"   Images for %s\" % species)\n    print()\n    print(\"--------------------------------------------------\")\n    whale_and_dolphin.showIndividualImagesTile( \\\n        metadata=metadata[species], \\\n        num_cols=num_cols, \\\n        max_num_individual_images=max_num_individual_images, \\\n        max_num_individuals=max_num_individuals \\\n    )\n    print()\n    print()","metadata":{"execution":{"iopub.status.busy":"2022-02-25T08:38:35.985543Z","iopub.execute_input":"2022-02-25T08:38:35.985755Z","iopub.status.idle":"2022-02-25T08:47:50.420617Z","shell.execute_reply.started":"2022-02-25T08:38:35.985725Z","shell.execute_reply":"2022-02-25T08:47:50.419526Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# [4] Test images(検証用の画像)の確認","metadata":{}},{"cell_type":"markdown","source":"Test images(検証用の画像)と以下の統計データを示します。\n- Test images(検証用の画像)の数","metadata":{}},{"cell_type":"markdown","source":"## [4-1] Test images(検証用の画像)に係るデータセットの読み込み","metadata":{}},{"cell_type":"markdown","source":" Test images(検証用の画像)は、Train images(訓練用の画像)の読み込みに用いたクラスで読み込むことができます。","metadata":{}},{"cell_type":"code","source":"# Loads metadata for test images.\npath_to_metadata = \"%s\" % path_to_test_metadata\npath_to_dir_images = \"%s/test_images\" % path_to_inputs\n\nwhale_and_dolphin = WhaleAndDolphin(\n    path_to_metadata=path_to_metadata,\n    path_to_dir_images=path_to_dir_images\n)","metadata":{"execution":{"iopub.status.busy":"2022-02-25T08:47:50.422346Z","iopub.execute_input":"2022-02-25T08:47:50.422643Z","iopub.status.idle":"2022-02-25T08:47:50.455436Z","shell.execute_reply.started":"2022-02-25T08:47:50.422604Z","shell.execute_reply":"2022-02-25T08:47:50.454814Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## [4-2] Test images(検証用の画像)に係る統計データ","metadata":{}},{"cell_type":"markdown","source":"Species(種)の数を示します。","metadata":{}},{"cell_type":"code","source":"# Shows number of images for each species.\nall_species = whale_and_dolphin.getAllSpecies()\n\nprint(\"Number of species:\")\nprint(len(all_species))\nprint()\n\nprint(\"All species:\")\nprint(all_species)\nprint()","metadata":{"execution":{"iopub.status.busy":"2022-02-25T08:47:50.456361Z","iopub.execute_input":"2022-02-25T08:47:50.457018Z","iopub.status.idle":"2022-02-25T08:47:50.465739Z","shell.execute_reply.started":"2022-02-25T08:47:50.456989Z","shell.execute_reply":"2022-02-25T08:47:50.464509Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Test images(検証用の画像)の数を示します。","metadata":{}},{"cell_type":"code","source":"print(\"Number of images:\")\nprint()\nprint(\"species, num_of_images\")\n\nmetadata = {}\nfor species in all_species:\n    metadata[species] = whale_and_dolphin.sliceMetadata(query=\"species == @species\")\n    num_images = len(metadata[species])\n    \n    print(\"%s, %d\" % (species, num_images))","metadata":{"execution":{"iopub.status.busy":"2022-02-25T08:47:50.467644Z","iopub.execute_input":"2022-02-25T08:47:50.467930Z","iopub.status.idle":"2022-02-25T08:47:50.502976Z","shell.execute_reply.started":"2022-02-25T08:47:50.467905Z","shell.execute_reply":"2022-02-25T08:47:50.501530Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## [4-3] Test images(検証用の画像)のサンプル","metadata":{}},{"cell_type":"markdown","source":"Matplotlibを用いて、最初の100の、Test images(検証用の画像)を示します。表示する画像の数は、num_images等の変数の値を変えることで、調整することができます。","metadata":{}},{"cell_type":"code","source":"# Shows first 100 test images.\nnum_images = 100\ni_first = 0\ni_end = i_first + num_images\nnum_cols = 4\n\nfor species in all_species:\n    print()\n    print(\"--------------------------------------------------\")\n    print()\n    print(\"   Images for %s\" % species)\n    print()\n    print(\"--------------------------------------------------\")\n    print()\n    whale_and_dolphin.showImagesTile( \\\n        metadata=metadata[species][i_first:i_end], \\\n        num_cols=num_cols \\\n    )\n    print()\n    print()","metadata":{"execution":{"iopub.status.busy":"2022-02-25T08:47:50.504701Z","iopub.execute_input":"2022-02-25T08:47:50.505032Z","iopub.status.idle":"2022-02-25T08:48:48.327221Z","shell.execute_reply.started":"2022-02-25T08:47:50.504991Z","shell.execute_reply":"2022-02-25T08:48:48.326064Z"},"trusted":true},"execution_count":null,"outputs":[]}]}