{"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":"## Introduction\nThis kernel is just a simple sample code that \"classifies\" whether input images are whale or dolphin.","metadata":{}},{"cell_type":"markdown","source":"### Input train.csv","metadata":{}},{"cell_type":"code","source":"import os\nimport pandas as pd\n\ndata_root = '/kaggle/input/happy-whale-and-dolphin'\ndf = pd.read_csv(os.path.join(data_root, 'train.csv'))","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-02-27T16:30:13.804325Z","iopub.execute_input":"2022-02-27T16:30:13.80499Z","iopub.status.idle":"2022-02-27T16:30:13.928363Z","shell.execute_reply.started":"2022-02-27T16:30:13.804877Z","shell.execute_reply":"2022-02-27T16:30:13.92738Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Fix Meta Data","metadata":{}},{"cell_type":"code","source":"df['species'] = df['species'].str.replace('beluga', 'beluga_whale')\ndf['species'] = df['species'].str.replace('globis', 'globis_whale')\ndf['species'] = df['species'].str.replace('bottlenose_dolpin', 'bottlenose_dolphin')\ndf['species'] = df['species'].str.replace('kiler_whale', 'killer_whale')","metadata":{"execution":{"iopub.status.busy":"2022-02-27T16:30:13.930159Z","iopub.execute_input":"2022-02-27T16:30:13.930407Z","iopub.status.idle":"2022-02-27T16:30:14.116962Z","shell.execute_reply.started":"2022-02-27T16:30:13.930376Z","shell.execute_reply":"2022-02-27T16:30:14.116194Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Make Dataset","metadata":{}},{"cell_type":"code","source":"import tensorflow as tf\n\ndef load_and_preprocess_img(path):\n    img = tf.io.read_file(path)\n    img = tf.image.decode_jpeg(img, channels=3)\n    img = tf.image.resize(img, [128, 128])\n    img /= 255.0\n    return img\n\nimg_paths = list(map(lambda x: os.path.join(data_root, 'train_images', x), df['image']))\npath_ds = tf.data.Dataset.from_tensor_slices(img_paths)\nimg_ds = path_ds.map(load_and_preprocess_img)\nlabels = list(map(lambda x: 0 if 'whale' in x else 1, df['species']))\nlabel_ds = tf.data.Dataset.from_tensor_slices(tf.cast(labels, tf.int64))\nds = tf.data.Dataset.zip((img_ds, label_ds))\nds = ds.shuffle(100).repeat(1).batch(32)","metadata":{"execution":{"iopub.status.busy":"2022-02-27T16:30:14.118059Z","iopub.execute_input":"2022-02-27T16:30:14.118284Z","iopub.status.idle":"2022-02-27T16:30:19.707061Z","shell.execute_reply.started":"2022-02-27T16:30:14.118257Z","shell.execute_reply":"2022-02-27T16:30:19.706114Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Model","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras import Model, layers, applications\n\nclass MyResNet50(Model):\n    def __init__(self):\n        super().__init__()\n        self.base_model = applications.ResNet50(include_top=False, pooling='max')\n        self.fc1 = layers.Dense(1024, activation='relu')\n        self.fc2 = layers.Dense(2, activation='softmax')\n\n    def call(self, x):\n        x = self.base_model(x)\n        x = self.fc1(x)\n        x = self.fc2(x)\n        return x","metadata":{"execution":{"iopub.status.busy":"2022-02-27T16:30:19.709047Z","iopub.execute_input":"2022-02-27T16:30:19.710202Z","iopub.status.idle":"2022-02-27T16:30:20.792914Z","shell.execute_reply.started":"2022-02-27T16:30:19.710147Z","shell.execute_reply":"2022-02-27T16:30:20.791998Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Training","metadata":{}},{"cell_type":"code","source":"model = MyResNet50()\nmodel.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])\nmodel.fit(ds, epochs=1)","metadata":{"execution":{"iopub.status.busy":"2022-02-27T16:30:20.794255Z","iopub.execute_input":"2022-02-27T16:30:20.794588Z","iopub.status.idle":"2022-02-27T16:30:41.855682Z","shell.execute_reply.started":"2022-02-27T16:30:20.794545Z","shell.execute_reply":"2022-02-27T16:30:41.854165Z"},"trusted":true},"execution_count":null,"outputs":[]}]}