{"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":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport os\nfrom sklearn.metrics import classification_report\nfrom tensorflow import keras\nimport tensorflow as tf\nfrom keras import layers, models,regularizers\nfrom keras.preprocessing.image import ImageDataGenerator","metadata":{"execution":{"iopub.status.busy":"2022-11-09T20:23:05.518127Z","iopub.execute_input":"2022-11-09T20:23:05.518522Z","iopub.status.idle":"2022-11-09T20:23:05.525413Z","shell.execute_reply.started":"2022-11-09T20:23:05.518491Z","shell.execute_reply":"2022-11-09T20:23:05.523974Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = pd.read_csv('/kaggle/input/happy-whale-and-dolphin/train.csv')","metadata":{"execution":{"iopub.status.busy":"2022-11-09T16:46:36.659922Z","iopub.execute_input":"2022-11-09T16:46:36.660686Z","iopub.status.idle":"2022-11-09T16:46:36.781241Z","shell.execute_reply.started":"2022-11-09T16:46:36.660639Z","shell.execute_reply":"2022-11-09T16:46:36.780202Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(set(data.species))","metadata":{"execution":{"iopub.status.busy":"2022-11-09T16:46:43.690804Z","iopub.execute_input":"2022-11-09T16:46:43.691269Z","iopub.status.idle":"2022-11-09T16:46:43.720020Z","shell.execute_reply.started":"2022-11-09T16:46:43.691227Z","shell.execute_reply":"2022-11-09T16:46:43.719105Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15,5))\nsns.histplot(data.species)\nplt.xticks(rotation=90)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-11-09T14:20:58.194206Z","iopub.execute_input":"2022-11-09T14:20:58.194698Z","iopub.status.idle":"2022-11-09T14:20:58.726743Z","shell.execute_reply.started":"2022-11-09T14:20:58.194653Z","shell.execute_reply":"2022-11-09T14:20:58.725394Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.info()","metadata":{"execution":{"iopub.status.busy":"2022-11-09T14:21:01.953306Z","iopub.execute_input":"2022-11-09T14:21:01.953805Z","iopub.status.idle":"2022-11-09T14:21:01.979538Z","shell.execute_reply.started":"2022-11-09T14:21:01.953762Z","shell.execute_reply":"2022-11-09T14:21:01.977833Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"set(data.species)","metadata":{"execution":{"iopub.status.busy":"2022-11-09T14:21:04.493967Z","iopub.execute_input":"2022-11-09T14:21:04.494394Z","iopub.status.idle":"2022-11-09T14:21:04.507974Z","shell.execute_reply.started":"2022-11-09T14:21:04.494357Z","shell.execute_reply":"2022-11-09T14:21:04.506755Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#  we see we have bottlenose_dolphin and bottlenose_dolpin which are the same;\n# and kiler_whale and killer_whale too; beluga is a whale and globis is pilot_whale.\n\ndata.loc[data['species'] == 'beluga', 'species'] = 'beluga_whale'\ndata.loc[data['species'] == 'globis', 'species'] = 'pilot_whale' \ndata.loc[data['species'] == 'bottlenose_dolpin', 'species'] = 'bottlenose_dolphin' \ndata.loc[data['species'] == 'kiler_whale', 'species'] = 'killer_whale' ","metadata":{"execution":{"iopub.status.busy":"2022-11-09T16:46:47.367324Z","iopub.execute_input":"2022-11-09T16:46:47.367708Z","iopub.status.idle":"2022-11-09T16:46:47.390215Z","shell.execute_reply.started":"2022-11-09T16:46:47.367674Z","shell.execute_reply":"2022-11-09T16:46:47.389375Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(set(data.species))","metadata":{"execution":{"iopub.status.busy":"2022-11-09T16:46:49.545629Z","iopub.execute_input":"2022-11-09T16:46:49.545989Z","iopub.status.idle":"2022-11-09T16:46:49.558349Z","shell.execute_reply.started":"2022-11-09T16:46:49.545959Z","shell.execute_reply":"2022-11-09T16:46:49.557116Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15,5))\nsns.barplot(x=data.species.value_counts().index, y=data.species.value_counts().values)\nplt.xticks(rotation=90)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-11-09T14:21:14.283245Z","iopub.execute_input":"2022-11-09T14:21:14.283649Z","iopub.status.idle":"2022-11-09T14:21:14.716706Z","shell.execute_reply.started":"2022-11-09T14:21:14.283616Z","shell.execute_reply":"2022-11-09T14:21:14.715341Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data['label'] = ['whale' if 'whale' in i else 'dolphin' for i in data['species']]","metadata":{"execution":{"iopub.status.busy":"2022-11-09T16:46:52.987755Z","iopub.execute_input":"2022-11-09T16:46:52.988205Z","iopub.status.idle":"2022-11-09T16:46:53.015391Z","shell.execute_reply.started":"2022-11-09T16:46:52.988162Z","shell.execute_reply":"2022-11-09T16:46:53.014468Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.sample(3)","metadata":{"execution":{"iopub.status.busy":"2022-11-09T14:21:21.294590Z","iopub.execute_input":"2022-11-09T14:21:21.295215Z","iopub.status.idle":"2022-11-09T14:21:21.323597Z","shell.execute_reply.started":"2022-11-09T14:21:21.295158Z","shell.execute_reply":"2022-11-09T14:21:21.322699Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(6,6))\nsns.histplot(data['label']);","metadata":{"execution":{"iopub.status.busy":"2022-11-09T14:21:26.314809Z","iopub.execute_input":"2022-11-09T14:21:26.315320Z","iopub.status.idle":"2022-11-09T14:21:26.599423Z","shell.execute_reply.started":"2022-11-09T14:21:26.315275Z","shell.execute_reply":"2022-11-09T14:21:26.598218Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.layers import Input, Lambda, Dense\nfrom keras.models import Model\nfrom keras.preprocessing import image\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.models import Sequential\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport pandas as pd\nfrom pathlib import Path\nimport os.path\nimport seaborn as sns\nfrom sklearn.model_selection import train_test_split\nimport tensorflow as tf\nfrom sklearn.metrics import confusion_matrix, classification_report","metadata":{"execution":{"iopub.status.busy":"2022-11-09T20:11:11.311211Z","iopub.execute_input":"2022-11-09T20:11:11.311670Z","iopub.status.idle":"2022-11-09T20:11:17.584859Z","shell.execute_reply.started":"2022-11-09T20:11:11.311577Z","shell.execute_reply":"2022-11-09T20:11:17.583603Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_dir = Path('../input/happywhaleimagessortedbyspecies/train_species_list')","metadata":{"execution":{"iopub.status.busy":"2022-11-09T20:12:13.109566Z","iopub.execute_input":"2022-11-09T20:12:13.110439Z","iopub.status.idle":"2022-11-09T20:12:13.116549Z","shell.execute_reply.started":"2022-11-09T20:12:13.110394Z","shell.execute_reply":"2022-11-09T20:12:13.115392Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filepaths = list(image_dir.glob(r'**/*.jpg'))\nlabels = list(map(lambda x: os.path.split(os.path.split(x)[0])[1], filepaths))\n\nfilepaths = pd.Series(filepaths, name='Filepath').astype(str)\nlabels = pd.Series(labels, name='Label')\n\nimage_df = pd.concat([filepaths, labels], axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-11-09T20:12:29.558255Z","iopub.execute_input":"2022-11-09T20:12:29.558731Z","iopub.status.idle":"2022-11-09T20:13:38.394358Z","shell.execute_reply.started":"2022-11-09T20:12:29.558686Z","shell.execute_reply":"2022-11-09T20:13:38.393324Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_df","metadata":{"execution":{"iopub.status.busy":"2022-11-09T20:13:45.974590Z","iopub.execute_input":"2022-11-09T20:13:45.974979Z","iopub.status.idle":"2022-11-09T20:13:45.998923Z","shell.execute_reply.started":"2022-11-09T20:13:45.974946Z","shell.execute_reply":"2022-11-09T20:13:45.997833Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train_path = '../input/happywhaleimagessortedbyspecies/train_species_list'\n# nr_files = 0\n# for root, dirc, files in os.walk(train_path):\n#       nr_files += len(files)\n# print('#files: ', nr_files)","metadata":{"execution":{"iopub.status.busy":"2022-11-09T17:04:02.754415Z","iopub.execute_input":"2022-11-09T17:04:02.755173Z","iopub.status.idle":"2022-11-09T17:04:13.588512Z","shell.execute_reply.started":"2022-11-09T17:04:02.755129Z","shell.execute_reply":"2022-11-09T17:04:13.587303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# model = tf.keras.applications.MobileNetV2(weights='imagenet',\n#               include_top=False,\n#               input_shape=(224, 224, 3))\n# model.summary()","metadata":{"execution":{"iopub.status.busy":"2022-11-09T20:33:23.610787Z","iopub.execute_input":"2022-11-09T20:33:23.611303Z","iopub.status.idle":"2022-11-09T20:33:23.616843Z","shell.execute_reply.started":"2022-11-09T20:33:23.611260Z","shell.execute_reply":"2022-11-09T20:33:23.615497Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train, test = train_test_split(image_df, test_size=0.2, shuffle=True, random_state=1)","metadata":{"execution":{"iopub.status.busy":"2022-11-09T20:26:33.358494Z","iopub.execute_input":"2022-11-09T20:26:33.358858Z","iopub.status.idle":"2022-11-09T20:26:33.375943Z","shell.execute_reply.started":"2022-11-09T20:26:33.358827Z","shell.execute_reply":"2022-11-09T20:26:33.374946Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_generator = ImageDataGenerator(preprocessing_function=\n                                   tf.keras.applications.mobilenet_v2.preprocess_input)","metadata":{"execution":{"iopub.status.busy":"2022-11-09T20:26:38.537418Z","iopub.execute_input":"2022-11-09T20:26:38.537780Z","iopub.status.idle":"2022-11-09T20:26:38.543006Z","shell.execute_reply.started":"2022-11-09T20:26:38.537751Z","shell.execute_reply":"2022-11-09T20:26:38.541802Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_images = train_generator.flow_from_dataframe(dataframe=train,x_col='Filepath',y_col='Label',\n    target_size=(224, 224),\n    color_mode='rgb',\n    class_mode='categorical',\n    batch_size=32,\n    subset='training')","metadata":{"execution":{"iopub.status.busy":"2022-11-09T20:27:11.192268Z","iopub.execute_input":"2022-11-09T20:27:11.192978Z","iopub.status.idle":"2022-11-09T20:27:27.949107Z","shell.execute_reply.started":"2022-11-09T20:27:11.192926Z","shell.execute_reply":"2022-11-09T20:27:27.948042Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_generator = ImageDataGenerator(preprocessing_function=\n                                   tf.keras.applications.mobilenet_v2.preprocess_input)","metadata":{"execution":{"iopub.status.busy":"2022-11-09T20:27:27.951326Z","iopub.execute_input":"2022-11-09T20:27:27.951709Z","iopub.status.idle":"2022-11-09T20:27:27.957531Z","shell.execute_reply.started":"2022-11-09T20:27:27.951673Z","shell.execute_reply":"2022-11-09T20:27:27.956360Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_images = test_generator.flow_from_dataframe(dataframe=test,x_col='Filepath',y_col='Label',\n    target_size=(224, 224),\n    color_mode='rgb',\n    class_mode='categorical',\n    batch_size=32)","metadata":{"execution":{"iopub.status.busy":"2022-11-09T20:27:27.959260Z","iopub.execute_input":"2022-11-09T20:27:27.959658Z","iopub.status.idle":"2022-11-09T20:27:32.202767Z","shell.execute_reply.started":"2022-11-09T20:27:27.959622Z","shell.execute_reply":"2022-11-09T20:27:32.201719Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# def feature_extractor(sample_size, batch_size, dir):\n#     features = np.zeros((sample_size, 7, 7, 1280))\n#     labels = np.zeros(sample_size)\n#     datagen = datagenerator.flow_from_directory(dir,\n#                                               target_size=(224, 224),\n#                                               batch_size=batch_size,\n#                                               class_mode='categorical')\n#     cnt = 0 \n#     for input_pxl, lbl in datagen:\n#         features_batch = model.predict(input_pxl)\n#         features[cnt * batch_size : (cnt+1) * batch_size] = features_batch\n#         labels[cnt * batch_size : (cnt+1) * batch_size] = np.argmax(lbl)\n#         cnt += 1\n#         if cnt * batch_size > sample_size:\n#             break\n#     return features, labels, datagen.class_indices","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train_features, train_labels, train_dict = feature_extractor(nr_files, 32, train_path)","metadata":{"execution":{"iopub.status.busy":"2022-11-09T16:59:28.589246Z","iopub.execute_input":"2022-11-09T16:59:28.589804Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train_features = train_features.reshape(-1, 7*7*1280)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pretrained_model = tf.keras.applications.MobileNetV2(\n    input_shape=(224, 224, 3),\n    include_top=False,\n    weights='imagenet',\n    pooling='avg')\n\npretrained_model.trainable = False","metadata":{"execution":{"iopub.status.busy":"2022-11-09T20:34:57.110584Z","iopub.execute_input":"2022-11-09T20:34:57.110957Z","iopub.status.idle":"2022-11-09T20:34:58.159760Z","shell.execute_reply.started":"2022-11-09T20:34:57.110922Z","shell.execute_reply":"2022-11-09T20:34:58.158668Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# n_features = train_features.shape[1]\n\n# input = layers.Input(shape=n_features)\n\ninputs = pretrained_model.input\n\nlayer1 = layers.Dense(128, activation='relu')(pretrained_model.output)\n# layer2 = layers.BatchNormalization()(layer1)\nlayer2 = layers.Dense(128, activation='relu')(layer1)\n# layer3 = layers.Dropout(0.55)(layer2)\n\noutputs = layers.Dense(30, activation='softmax')(layer2)\n\nmodel = models.Model(inputs, outputs)\nmodel.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2022-11-09T20:40:48.846400Z","iopub.execute_input":"2022-11-09T20:40:48.846778Z","iopub.status.idle":"2022-11-09T20:40:48.898150Z","shell.execute_reply.started":"2022-11-09T20:40:48.846746Z","shell.execute_reply":"2022-11-09T20:40:48.897120Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(train_images,epochs=7 ,batch_size=64,\n                    validation_data=test_images)","metadata":{"execution":{"iopub.status.busy":"2022-11-09T20:41:21.706714Z","iopub.execute_input":"2022-11-09T20:41:21.707107Z","iopub.status.idle":"2022-11-09T21:03:27.478289Z","shell.execute_reply.started":"2022-11-09T20:41:21.707075Z","shell.execute_reply":"2022-11-09T21:03:27.476851Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nplt.plot(history.history['accuracy'], label='train-acc')\nplt.plot(history.history['loss'],label='train-loss')\nplt.legend();","metadata":{"execution":{"iopub.status.busy":"2022-11-09T21:03:46.553988Z","iopub.execute_input":"2022-11-09T21:03:46.554747Z","iopub.status.idle":"2022-11-09T21:03:46.832760Z","shell.execute_reply.started":"2022-11-09T21:03:46.554710Z","shell.execute_reply":"2022-11-09T21:03:46.831806Z"},"trusted":true},"execution_count":null,"outputs":[]}]}