{"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":"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":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-04-12T14:34:50.491599Z","iopub.execute_input":"2022-04-12T14:34:50.492300Z","iopub.status.idle":"2022-04-12T14:34:50.499072Z","shell.execute_reply.started":"2022-04-12T14:34:50.492262Z","shell.execute_reply":"2022-04-12T14:34:50.498088Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('../input/happy-whale-and-dolphin/train.csv')\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2022-04-12T14:34:50.500851Z","iopub.execute_input":"2022-04-12T14:34:50.501197Z","iopub.status.idle":"2022-04-12T14:34:50.570348Z","shell.execute_reply.started":"2022-04-12T14:34:50.501073Z","shell.execute_reply":"2022-04-12T14:34:50.569562Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.shape","metadata":{"execution":{"iopub.status.busy":"2022-04-12T14:34:50.572102Z","iopub.execute_input":"2022-04-12T14:34:50.572355Z","iopub.status.idle":"2022-04-12T14:34:50.578423Z","shell.execute_reply.started":"2022-04-12T14:34:50.572321Z","shell.execute_reply":"2022-04-12T14:34:50.577462Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.species.nunique()","metadata":{"execution":{"iopub.status.busy":"2022-04-12T14:34:50.580230Z","iopub.execute_input":"2022-04-12T14:34:50.580509Z","iopub.status.idle":"2022-04-12T14:34:50.593436Z","shell.execute_reply.started":"2022-04-12T14:34:50.580463Z","shell.execute_reply":"2022-04-12T14:34:50.592538Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.species.value_counts().sort_index()","metadata":{"execution":{"iopub.status.busy":"2022-04-12T14:34:50.595785Z","iopub.execute_input":"2022-04-12T14:34:50.596202Z","iopub.status.idle":"2022-04-12T14:34:50.610822Z","shell.execute_reply.started":"2022-04-12T14:34:50.596168Z","shell.execute_reply":"2022-04-12T14:34:50.610177Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(20, 10))\ndf['species'].value_counts().sort_values(ascending=True).plot(kind='barh');","metadata":{"execution":{"iopub.status.busy":"2022-04-12T14:34:50.611898Z","iopub.execute_input":"2022-04-12T14:34:50.612201Z","iopub.status.idle":"2022-04-12T14:34:51.063419Z","shell.execute_reply.started":"2022-04-12T14:34:50.612167Z","shell.execute_reply":"2022-04-12T14:34:51.062665Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_dir = Path('../input/happywhaleimagessortedbyspecies/train_species_list')","metadata":{"execution":{"iopub.status.busy":"2022-04-12T14:34:51.064645Z","iopub.execute_input":"2022-04-12T14:34:51.065779Z","iopub.status.idle":"2022-04-12T14:34:51.070076Z","shell.execute_reply.started":"2022-04-12T14:34:51.065736Z","shell.execute_reply":"2022-04-12T14:34:51.069332Z"},"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-04-12T14:34:51.071248Z","iopub.execute_input":"2022-04-12T14:34:51.072354Z","iopub.status.idle":"2022-04-12T14:35:04.672889Z","shell.execute_reply.started":"2022-04-12T14:34:51.072308Z","shell.execute_reply":"2022-04-12T14:35:04.672152Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_df","metadata":{"execution":{"iopub.status.busy":"2022-04-12T14:35:04.674279Z","iopub.execute_input":"2022-04-12T14:35:04.674549Z","iopub.status.idle":"2022-04-12T14:35:04.686263Z","shell.execute_reply.started":"2022-04-12T14:35:04.674506Z","shell.execute_reply":"2022-04-12T14:35:04.685294Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_df['Label'].value_counts()\n","metadata":{"execution":{"iopub.status.busy":"2022-04-12T14:35:04.688077Z","iopub.execute_input":"2022-04-12T14:35:04.688345Z","iopub.status.idle":"2022-04-12T14:35:04.706012Z","shell.execute_reply.started":"2022-04-12T14:35:04.688311Z","shell.execute_reply":"2022-04-12T14:35:04.705160Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df, test_df = train_test_split(image_df, train_size=0.8, shuffle=True, random_state=1)\n","metadata":{"execution":{"iopub.status.busy":"2022-04-12T14:35:04.709226Z","iopub.execute_input":"2022-04-12T14:35:04.709672Z","iopub.status.idle":"2022-04-12T14:35:04.722551Z","shell.execute_reply.started":"2022-04-12T14:35:04.709639Z","shell.execute_reply":"2022-04-12T14:35:04.721912Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_generator = tf.keras.preprocessing.image.ImageDataGenerator(\n    preprocessing_function=tf.keras.applications.mobilenet_v2.preprocess_input,\n)\n\ntest_generator = tf.keras.preprocessing.image.ImageDataGenerator(\n    preprocessing_function=tf.keras.applications.mobilenet_v2.preprocess_input\n)","metadata":{"execution":{"iopub.status.busy":"2022-04-12T14:35:04.724544Z","iopub.execute_input":"2022-04-12T14:35:04.724729Z","iopub.status.idle":"2022-04-12T14:35:04.729590Z","shell.execute_reply.started":"2022-04-12T14:35:04.724707Z","shell.execute_reply":"2022-04-12T14:35:04.728716Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_images = train_generator.flow_from_dataframe(\n    dataframe=train_df,\n    x_col='Filepath',\n    y_col='Label',\n    target_size=(224, 224),\n    color_mode='rgb',\n    class_mode='categorical',\n    batch_size=32,\n    shuffle=True,\n    seed=42,\n    subset='training'\n)\n\ntest_images = test_generator.flow_from_dataframe(\n    dataframe=test_df,\n    x_col='Filepath',\n    y_col='Label',\n    target_size=(224, 224),\n    color_mode='rgb',\n    class_mode='categorical',\n    batch_size=32,\n    shuffle=False\n)","metadata":{"execution":{"iopub.status.busy":"2022-04-12T14:35:04.730870Z","iopub.execute_input":"2022-04-12T14:35:04.731279Z","iopub.status.idle":"2022-04-12T14:35:11.819125Z","shell.execute_reply.started":"2022-04-12T14:35:04.731239Z","shell.execute_reply":"2022-04-12T14:35:11.818224Z"},"trusted":true},"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)\n\npretrained_model.trainable = False","metadata":{"execution":{"iopub.status.busy":"2022-04-12T14:35:11.820238Z","iopub.execute_input":"2022-04-12T14:35:11.820510Z","iopub.status.idle":"2022-04-12T14:35:12.747207Z","shell.execute_reply.started":"2022-04-12T14:35:11.820447Z","shell.execute_reply":"2022-04-12T14:35:12.746484Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"inputs = pretrained_model.input\n\nx = tf.keras.layers.Dense(128, activation='relu')(pretrained_model.output)\nx = tf.keras.layers.Dense(128, activation='relu')(x)\n\noutputs = tf.keras.layers.Dense(30, activation='softmax')(x)\n\nmodel = tf.keras.Model(inputs, outputs)\n\n\nprint(model.summary())","metadata":{"execution":{"iopub.status.busy":"2022-04-12T14:35:12.748450Z","iopub.execute_input":"2022-04-12T14:35:12.748717Z","iopub.status.idle":"2022-04-12T14:35:12.854179Z","shell.execute_reply.started":"2022-04-12T14:35:12.748683Z","shell.execute_reply":"2022-04-12T14:35:12.853350Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(\n    optimizer='adam',\n    loss='categorical_crossentropy',\n    metrics=['accuracy']\n)\n","metadata":{"execution":{"iopub.status.busy":"2022-04-12T14:35:12.855737Z","iopub.execute_input":"2022-04-12T14:35:12.856005Z","iopub.status.idle":"2022-04-12T14:35:12.870405Z","shell.execute_reply.started":"2022-04-12T14:35:12.855968Z","shell.execute_reply":"2022-04-12T14:35:12.869454Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"r = model.fit(\n    train_images,\n    validation_data=test_images,\n    epochs=5,\n    )","metadata":{"execution":{"iopub.status.busy":"2022-04-12T14:35:12.871696Z","iopub.execute_input":"2022-04-12T14:35:12.872045Z","iopub.status.idle":"2022-04-12T14:47:43.381555Z","shell.execute_reply.started":"2022-04-12T14:35:12.872006Z","shell.execute_reply":"2022-04-12T14:47:43.380815Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(r.history['loss'], label='train loss')\nplt.plot(r.history['val_loss'], label='val loss')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-04-12T14:47:43.383227Z","iopub.execute_input":"2022-04-12T14:47:43.383774Z","iopub.status.idle":"2022-04-12T14:47:43.571732Z","shell.execute_reply.started":"2022-04-12T14:47:43.383736Z","shell.execute_reply":"2022-04-12T14:47:43.571017Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(r.history['accuracy'], label='train acc')\nplt.plot(r.history['val_accuracy'], label='val acc')\nplt.legend()\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2022-04-12T14:47:43.572791Z","iopub.execute_input":"2022-04-12T14:47:43.573689Z","iopub.status.idle":"2022-04-12T14:47:43.766356Z","shell.execute_reply.started":"2022-04-12T14:47:43.573649Z","shell.execute_reply":"2022-04-12T14:47:43.765687Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results = model.evaluate(test_images, verbose=0)\nprint(\"Test Accuracy: {:.2f}%\".format(results[1] * 100))","metadata":{"execution":{"iopub.status.busy":"2022-04-12T14:47:43.767441Z","iopub.execute_input":"2022-04-12T14:47:43.768195Z","iopub.status.idle":"2022-04-12T14:48:12.574050Z","shell.execute_reply.started":"2022-04-12T14:47:43.768155Z","shell.execute_reply":"2022-04-12T14:48:12.573167Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = np.argmax(model.predict(test_images), axis=1)\n\ncm = confusion_matrix(test_images.labels, predictions)\nclr = classification_report(test_images.labels, predictions, target_names=test_images.class_indices, zero_division=0)\n","metadata":{"execution":{"iopub.status.busy":"2022-04-12T14:48:12.575683Z","iopub.execute_input":"2022-04-12T14:48:12.576219Z","iopub.status.idle":"2022-04-12T14:48:39.755976Z","shell.execute_reply.started":"2022-04-12T14:48:12.576180Z","shell.execute_reply":"2022-04-12T14:48:39.755065Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15, 15))\nsns.heatmap(cm, annot=True, fmt='g', vmin=0, cmap='Blues', cbar=False)\nplt.xticks(ticks=np.arange(30) + 0.5, labels=test_images.class_indices, rotation=90)\nplt.yticks(ticks=np.arange(30) + 0.5, labels=test_images.class_indices, rotation=0)\nplt.xlabel(\"Predicted\")\nplt.ylabel(\"Actual\")\nplt.title(\"Confusion Matrix\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-04-12T14:48:39.757405Z","iopub.execute_input":"2022-04-12T14:48:39.757687Z","iopub.status.idle":"2022-04-12T14:48:42.483019Z","shell.execute_reply.started":"2022-04-12T14:48:39.757651Z","shell.execute_reply":"2022-04-12T14:48:42.482337Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Classification Report:\\n----------------------\\n\", clr)","metadata":{"execution":{"iopub.status.busy":"2022-04-12T14:48:42.484263Z","iopub.execute_input":"2022-04-12T14:48:42.484657Z","iopub.status.idle":"2022-04-12T14:48:42.489750Z","shell.execute_reply.started":"2022-04-12T14:48:42.484621Z","shell.execute_reply":"2022-04-12T14:48:42.488915Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save('WCvF.h5')","metadata":{"execution":{"iopub.status.busy":"2022-04-12T14:48:42.491013Z","iopub.execute_input":"2022-04-12T14:48:42.491268Z","iopub.status.idle":"2022-04-12T14:48:42.799622Z","shell.execute_reply.started":"2022-04-12T14:48:42.491232Z","shell.execute_reply":"2022-04-12T14:48:42.798558Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.models import load_model\nfrom keras.preprocessing.image import load_img,img_to_array\nmodel1 = load_model('./WCvF.h5',compile=False)","metadata":{"execution":{"iopub.status.busy":"2022-04-12T14:48:42.801451Z","iopub.execute_input":"2022-04-12T14:48:42.801859Z","iopub.status.idle":"2022-04-12T14:48:44.372147Z","shell.execute_reply.started":"2022-04-12T14:48:42.801822Z","shell.execute_reply":"2022-04-12T14:48:44.371430Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lab = train_images.class_indices\nlab={k:v for v,k in lab.items()}","metadata":{"execution":{"iopub.status.busy":"2022-04-12T14:48:44.373896Z","iopub.execute_input":"2022-04-12T14:48:44.374160Z","iopub.status.idle":"2022-04-12T14:48:44.378212Z","shell.execute_reply.started":"2022-04-12T14:48:44.374124Z","shell.execute_reply":"2022-04-12T14:48:44.377552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def output(location):\n    img=load_img(location,target_size=(224,224,3))\n    img=img_to_array(img)\n    img=img/255\n    img=np.expand_dims(img,[0])\n    answer=model1.predict(img)\n    y_class = answer.argmax(axis=-1)\n    y = \" \".join(str(x) for x in y_class)\n    y = int(y)\n    res = lab[y]\n    return res","metadata":{"execution":{"iopub.status.busy":"2022-04-12T14:48:44.381886Z","iopub.execute_input":"2022-04-12T14:48:44.382353Z","iopub.status.idle":"2022-04-12T14:48:44.388911Z","shell.execute_reply.started":"2022-04-12T14:48:44.382315Z","shell.execute_reply":"2022-04-12T14:48:44.388208Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img='../input/happywhaleimagessortedbyspecies/train_species_list/southern_right_whale/01e1590083b363.jpg'\npic=load_img('../input/happywhaleimagessortedbyspecies/train_species_list/southern_right_whale/01e1590083b363.jpg',target_size=(224,224,3))\nplt.imshow(pic)\noutput(img)","metadata":{"execution":{"iopub.status.busy":"2022-04-12T14:48:44.390318Z","iopub.execute_input":"2022-04-12T14:48:44.390603Z","iopub.status.idle":"2022-04-12T14:48:45.293020Z","shell.execute_reply.started":"2022-04-12T14:48:44.390568Z","shell.execute_reply":"2022-04-12T14:48:45.292351Z"},"trusted":true},"execution_count":null,"outputs":[]}]}