{"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 numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nfrom matplotlib import gridspec\nimport seaborn as sns\n\nimport os\nimport os, warnings\nimport PIL\nimport PIL.Image\n\nimport tensorflow as tf\nfrom tensorflow.keras.preprocessing import image_dataset_from_directory\nimport tensorflow_datasets as tfds\nfrom keras.models import Sequential\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom sklearn import preprocessing\n\nimport sys\n\nif not sys.warnoptions:\n    import warnings\n    warnings.simplefilter(\"ignore\")","metadata":{"execution":{"iopub.status.busy":"2022-04-24T20:37:03.817188Z","iopub.execute_input":"2022-04-24T20:37:03.817641Z","iopub.status.idle":"2022-04-24T20:37:06.308263Z","shell.execute_reply.started":"2022-04-24T20:37:03.817553Z","shell.execute_reply":"2022-04-24T20:37:06.307515Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## HappyWhale - Whale and Dolphin Classification","metadata":{}},{"cell_type":"code","source":"df_train = pd.read_csv('../input/ml7641-train/train_subset.csv') #('../input/happy-whale-and-dolphin/train.csv')\ndf_train.head()","metadata":{"execution":{"iopub.status.busy":"2022-04-24T20:37:08.737274Z","iopub.execute_input":"2022-04-24T20:37:08.737635Z","iopub.status.idle":"2022-04-24T20:37:08.981254Z","shell.execute_reply.started":"2022-04-24T20:37:08.737594Z","shell.execute_reply":"2022-04-24T20:37:08.980535Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"raw","source":"# Using LabelEncoder\nle = preprocessing.LabelEncoder()\nle.fit(df_train[\"class\"])\ndf_train['label']=le.transform(df_train[\"class\"])\ndf_train.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-10T13:43:04.865142Z","iopub.execute_input":"2022-03-10T13:43:04.865389Z","iopub.status.idle":"2022-03-10T13:43:04.893465Z","shell.execute_reply.started":"2022-03-10T13:43:04.865352Z","shell.execute_reply":"2022-03-10T13:43:04.892789Z"}}},{"cell_type":"code","source":"!ls ../input/ml7641-train/","metadata":{"execution":{"iopub.status.busy":"2022-04-24T20:37:10.256153Z","iopub.execute_input":"2022-04-24T20:37:10.256426Z","iopub.status.idle":"2022-04-24T20:37:10.924810Z","shell.execute_reply.started":"2022-04-24T20:37:10.256395Z","shell.execute_reply":"2022-04-24T20:37:10.924007Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Class Distribution","metadata":{}},{"cell_type":"code","source":"df_train_random = df_train.sample(frac=1)\ndf_train_random","metadata":{"execution":{"iopub.status.busy":"2022-04-24T20:37:11.515117Z","iopub.execute_input":"2022-04-24T20:37:11.515957Z","iopub.status.idle":"2022-04-24T20:37:11.555729Z","shell.execute_reply.started":"2022-04-24T20:37:11.515912Z","shell.execute_reply":"2022-04-24T20:37:11.555020Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train_random.to_csv('df_train_random.csv')","metadata":{"execution":{"iopub.status.busy":"2022-04-24T20:37:12.100019Z","iopub.execute_input":"2022-04-24T20:37:12.100875Z","iopub.status.idle":"2022-04-24T20:37:12.526735Z","shell.execute_reply.started":"2022-04-24T20:37:12.100829Z","shell.execute_reply":"2022-04-24T20:37:12.525880Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train[df_train[\"image\"]==\"000be9acf46619.jpg\"]","metadata":{"execution":{"iopub.status.busy":"2022-04-24T20:37:12.611994Z","iopub.execute_input":"2022-04-24T20:37:12.612229Z","iopub.status.idle":"2022-04-24T20:37:12.638868Z","shell.execute_reply.started":"2022-04-24T20:37:12.612200Z","shell.execute_reply":"2022-04-24T20:37:12.638147Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_view = pd.DataFrame(df_train['species'].value_counts())\nclass_view\n\nplt.figure(figsize=(14,7))\nlabel=[class_view['species']]\nsns.set_theme(style=\"whitegrid\")\nax=sns.histplot(df_train, x=\"species\",color='#0B606F', kde = False)\n\nfor rect in ax.patches:\n    height = rect.get_height()\n    ax.annotate(f'{int(height)}', xy=(rect.get_x()+rect.get_width()/2, height), \n                xytext=(0, 5), textcoords='offset points', ha='center', va='bottom') \n\nplt.xticks(rotation=90)\nax.set_title('Class count', x=0.54, y=1.1, fontsize=30)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-04-24T20:37:13.015470Z","iopub.execute_input":"2022-04-24T20:37:13.015927Z","iopub.status.idle":"2022-04-24T20:37:13.343965Z","shell.execute_reply.started":"2022-04-24T20:37:13.015889Z","shell.execute_reply":"2022-04-24T20:37:13.343288Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train[\"species\"].unique()","metadata":{"execution":{"iopub.status.busy":"2022-04-24T20:37:13.683495Z","iopub.execute_input":"2022-04-24T20:37:13.683989Z","iopub.status.idle":"2022-04-24T20:37:13.693037Z","shell.execute_reply.started":"2022-04-24T20:37:13.683955Z","shell.execute_reply":"2022-04-24T20:37:13.692223Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classe_names=df_train[\"species\"].unique() #=['whale', 'dolphin']","metadata":{"execution":{"iopub.status.busy":"2022-04-24T20:37:14.060744Z","iopub.execute_input":"2022-04-24T20:37:14.061224Z","iopub.status.idle":"2022-04-24T20:37:14.067235Z","shell.execute_reply.started":"2022-04-24T20:37:14.061183Z","shell.execute_reply":"2022-04-24T20:37:14.066303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## VGG16 preprocessing","metadata":{"execution":{"iopub.status.busy":"2022-03-09T14:49:08.189357Z","iopub.execute_input":"2022-03-09T14:49:08.189658Z","iopub.status.idle":"2022-03-09T14:49:08.19382Z","shell.execute_reply.started":"2022-03-09T14:49:08.189629Z","shell.execute_reply":"2022-03-09T14:49:08.192824Z"}}},{"cell_type":"code","source":"!rm -r testing_data\n!rm -r training_data\n\n!mkdir testing_data\n!mkdir training_data","metadata":{"execution":{"iopub.status.busy":"2022-04-24T20:37:15.280102Z","iopub.execute_input":"2022-04-24T20:37:15.280901Z","iopub.status.idle":"2022-04-24T20:37:18.914110Z","shell.execute_reply.started":"2022-04-24T20:37:15.280857Z","shell.execute_reply":"2022-04-24T20:37:18.913133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for index, row in df_train_random.iterrows():\n    print(row[\"image\"])\n    break","metadata":{"execution":{"iopub.status.busy":"2022-04-24T20:37:18.917346Z","iopub.execute_input":"2022-04-24T20:37:18.917856Z","iopub.status.idle":"2022-04-24T20:37:18.946211Z","shell.execute_reply.started":"2022-04-24T20:37:18.917807Z","shell.execute_reply":"2022-04-24T20:37:18.945486Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(df_train_random) * 0.8","metadata":{"execution":{"iopub.status.busy":"2022-04-24T20:37:18.947748Z","iopub.execute_input":"2022-04-24T20:37:18.949039Z","iopub.status.idle":"2022-04-24T20:37:18.955936Z","shell.execute_reply.started":"2022-04-24T20:37:18.949000Z","shell.execute_reply":"2022-04-24T20:37:18.955068Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport shutil\nimport random\nfrom tqdm import tqdm\n\ndirectory = \"../input/ml7641-train/train_images/train_images\"\n\n# for filename in tqdm(os.listdir(directory)):\n    \n#     f = os.path.join(directory, filename)\n    \n#     if(random.random()<0.2):\n#         fileName = os.path.join(\".\",\"testing_data\",filename)\n#         shutil.copy(f, fileName)\n#     else:\n#         fileName = os.path.join(\".\",\"training_data\",filename)\n#         shutil.copy(f, fileName)\n\nfor index, row in tqdm(df_train_random.iterrows()):\n    \n    filename = row[\"image\"]\n    f = os.path.join(directory, filename)\n    \n    if(index > len(df_train_random)*0.8):\n        fileName = os.path.join(\".\",\"testing_data\",filename)\n        shutil.copy(f, fileName)\n    else:\n        fileName = os.path.join(\".\",\"training_data\",filename)\n        shutil.copy(f, fileName)","metadata":{"execution":{"iopub.status.busy":"2022-04-24T20:37:18.958445Z","iopub.execute_input":"2022-04-24T20:37:18.959201Z","iopub.status.idle":"2022-04-24T20:37:51.038722Z","shell.execute_reply.started":"2022-04-24T20:37:18.959162Z","shell.execute_reply":"2022-04-24T20:37:51.037834Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(os.listdir('./training_data')), len(os.listdir('./testing_data'))","metadata":{"execution":{"iopub.status.busy":"2022-04-24T20:37:51.040743Z","iopub.execute_input":"2022-04-24T20:37:51.041714Z","iopub.status.idle":"2022-04-24T20:37:51.071667Z","shell.execute_reply.started":"2022-04-24T20:37:51.041672Z","shell.execute_reply":"2022-04-24T20:37:51.070841Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datagen = ImageDataGenerator(preprocessing_function=lambda x: x,validation_split=0.10)\n#tf.keras.applications.vgg16.preprocess_input","metadata":{"execution":{"iopub.status.busy":"2022-04-24T20:37:51.073125Z","iopub.execute_input":"2022-04-24T20:37:51.073491Z","iopub.status.idle":"2022-04-24T20:37:51.079479Z","shell.execute_reply.started":"2022-04-24T20:37:51.073438Z","shell.execute_reply":"2022-04-24T20:37:51.077512Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classe_names = classe_names.tolist()","metadata":{"execution":{"iopub.status.busy":"2022-04-24T20:37:51.082257Z","iopub.execute_input":"2022-04-24T20:37:51.082633Z","iopub.status.idle":"2022-04-24T20:37:51.087637Z","shell.execute_reply.started":"2022-04-24T20:37:51.082595Z","shell.execute_reply":"2022-04-24T20:37:51.086761Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_generator=datagen.flow_from_dataframe(\n    df_train,\n    directory='./training_data/',\n    x_col='image',\n    y_col='species',\n    subset=\"training\",\n    target_size=(224,224), \n    batch_size=32,\n    rescale=1.0/255,\n    seed=7641,\n    shuffle=True,\n    classes=classe_names,\n    class_mode=\"categorical\",)","metadata":{"execution":{"iopub.status.busy":"2022-04-24T20:37:51.089558Z","iopub.execute_input":"2022-04-24T20:37:51.089833Z","iopub.status.idle":"2022-04-24T20:37:51.395098Z","shell.execute_reply.started":"2022-04-24T20:37:51.089796Z","shell.execute_reply":"2022-04-24T20:37:51.391328Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"valid_generator=datagen.flow_from_dataframe(\n    df_train,\n    directory='./training_data/',\n    x_col='image',\n    y_col='species',\n    subset=\"validation\",\n    target_size=(224,224), \n    batch_size=32,\n    rescale=1.0/255,\n    seed=7641,\n    shuffle=True,\n    classes=classe_names,\n    class_mode=\"categorical\",)","metadata":{"execution":{"iopub.status.busy":"2022-04-24T20:37:51.396833Z","iopub.execute_input":"2022-04-24T20:37:51.397056Z","iopub.status.idle":"2022-04-24T20:37:51.636963Z","shell.execute_reply.started":"2022-04-24T20:37:51.397027Z","shell.execute_reply":"2022-04-24T20:37:51.636195Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_submission=pd.read_csv('../input/happy-whale-and-dolphin/sample_submission.csv')\ndf_submission.head()","metadata":{"execution":{"iopub.status.busy":"2022-04-24T20:37:51.638085Z","iopub.execute_input":"2022-04-24T20:37:51.638524Z","iopub.status.idle":"2022-04-24T20:37:51.682303Z","shell.execute_reply.started":"2022-04-24T20:37:51.638476Z","shell.execute_reply":"2022-04-24T20:37:51.681492Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_datagen = ImageDataGenerator(preprocessing_function=lambda x: x,validation_split=0.0)\n\ntest_generator=datagen.flow_from_dataframe(\n    df_train,\n    directory='./testing_data/',\n    x_col='image',\n    y_col='species',\n    subset=\"training\",\n    target_size=(224,224), \n    batch_size=1,\n    rescale=1.0/255,\n    seed=7641,\n    shuffle=True,\n    classes=classe_names,\n    class_mode=\"categorical\",)","metadata":{"execution":{"iopub.status.busy":"2022-04-24T20:37:51.683666Z","iopub.execute_input":"2022-04-24T20:37:51.683992Z","iopub.status.idle":"2022-04-24T20:37:51.938487Z","shell.execute_reply.started":"2022-04-24T20:37:51.683951Z","shell.execute_reply":"2022-04-24T20:37:51.937654Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(nrows=1, ncols=4, figsize=(15,15))\n\nfor i in range(4):\n    image = next(test_generator)[0].astype('uint8')[0]\n    image = np.squeeze(image)\n    ax[i].imshow(image)\n    ax[i].axis('off')","metadata":{"execution":{"iopub.status.busy":"2022-04-24T20:37:51.939889Z","iopub.execute_input":"2022-04-24T20:37:51.940339Z","iopub.status.idle":"2022-04-24T20:37:52.440875Z","shell.execute_reply.started":"2022-04-24T20:37:51.940295Z","shell.execute_reply":"2022-04-24T20:37:52.440228Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(nrows=1, ncols=4, figsize=(15,15))\n\nfor i in range(4):\n    image = next(train_generator)[0].astype('uint8')[0]\n    image = np.squeeze(image)\n    ax[i].imshow(image)\n    ax[i].axis('off')","metadata":{"execution":{"iopub.status.busy":"2022-04-24T20:37:52.443561Z","iopub.execute_input":"2022-04-24T20:37:52.443794Z","iopub.status.idle":"2022-04-24T20:37:53.357033Z","shell.execute_reply.started":"2022-04-24T20:37:52.443765Z","shell.execute_reply":"2022-04-24T20:37:53.356322Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(nrows=1, ncols=4, figsize=(15,15))\n\nfor i in range(4):\n    image = next(valid_generator)[0].astype('uint8')[0]\n    image = np.squeeze(image)\n    ax[i].imshow(image)\n    ax[i].axis('off')","metadata":{"execution":{"iopub.status.busy":"2022-04-24T20:37:53.358579Z","iopub.execute_input":"2022-04-24T20:37:53.359107Z","iopub.status.idle":"2022-04-24T20:37:53.978099Z","shell.execute_reply.started":"2022-04-24T20:37:53.359070Z","shell.execute_reply":"2022-04-24T20:37:53.977363Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow import keras\nfrom tensorflow.keras import layers\nfrom keras.layers import Dense, Activation, Flatten, Dropout, BatchNormalization\nfrom keras.layers import Conv2D, MaxPooling2D\nfrom keras import regularizers\nfrom tensorflow.keras import optimizers\nfrom keras.layers import Dense, Conv2D , MaxPool2D , Flatten , Dropout\nfrom tensorflow.keras.optimizers import Adam\n","metadata":{"execution":{"iopub.status.busy":"2022-04-24T20:37:53.979662Z","iopub.execute_input":"2022-04-24T20:37:53.980549Z","iopub.status.idle":"2022-04-24T20:37:53.987170Z","shell.execute_reply.started":"2022-04-24T20:37:53.980507Z","shell.execute_reply":"2022-04-24T20:37:53.986586Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vgg_model = tf.keras.applications.vgg16.VGG16(\n    include_top=False,\n    weights='imagenet',\n    input_tensor=None,\n    input_shape=(224, 224, 3),\n    pooling=None,\n    classes=1000,\n    classifier_activation='softmax'\n)\n\nvgg_model.trainable = False\n\nvgg_model.summary()","metadata":{"execution":{"iopub.status.busy":"2022-04-24T20:37:53.988665Z","iopub.execute_input":"2022-04-24T20:37:53.989221Z","iopub.status.idle":"2022-04-24T20:37:55.296983Z","shell.execute_reply.started":"2022-04-24T20:37:53.989181Z","shell.execute_reply":"2022-04-24T20:37:55.296151Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\n# model.add(Conv2D(32, (3, 3), activation='relu', padding='same', name='conv_1', \n#                  input_shape=(224, 224, 3)))\n# model.add(MaxPooling2D((2, 2), name='maxpool_1'))\n# model.add(Conv2D(64, (3, 3), activation='relu', padding='same', name='conv_2'))\n# model.add(MaxPooling2D((2, 2), name='maxpool_2'))\n# model.add(Conv2D(128, (3, 3), activation='relu', padding='same', name='conv_3'))\n# model.add(MaxPooling2D((2, 2), name='maxpool_3'))\n# model.add(Conv2D(128, (3, 3), activation='relu', padding='same', name='conv_4'))\n# model.add(MaxPooling2D((2, 2), name='maxpool_4'))\nmodel.add(vgg_model)\nmodel.add(Flatten())\nmodel.add(Dropout(0.5))\nmodel.add(Dense(512, activation='relu', name='dense_1'))\nmodel.add(Dense(256, activation='relu', name='dense_2'))\nmodel.add(Dense(128, activation='relu', name='dense_3'))\nmodel.add(Dense(len(classe_names), activation='sigmoid', name='output'))\n\nmodel.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-04-24T20:37:55.299373Z","iopub.execute_input":"2022-04-24T20:37:55.299874Z","iopub.status.idle":"2022-04-24T20:37:55.407918Z","shell.execute_reply.started":"2022-04-24T20:37:55.299831Z","shell.execute_reply":"2022-04-24T20:37:55.407166Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"callback = tf.keras.callbacks.EarlyStopping(\n    monitor=\"val_accuracy\",\n    min_delta=0,\n    patience=0,\n    verbose=0,\n    mode=\"auto\",\n    baseline=None,\n    restore_best_weights=True,\n)\n\nhistory = model.fit(train_generator, epochs=24, validation_data=valid_generator, verbose=1, callbacks=[callback])","metadata":{"execution":{"iopub.status.busy":"2022-04-24T20:37:55.409012Z","iopub.execute_input":"2022-04-24T20:37:55.409357Z","iopub.status.idle":"2022-04-24T20:38:58.056914Z","shell.execute_reply.started":"2022-04-24T20:37:55.409312Z","shell.execute_reply":"2022-04-24T20:38:58.056105Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy', 'Precision', 'Recall'])\nmodel.evaluate_generator(test_generator, 5431)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def extract_layers(main_model, starting_layer_ix, ending_layer_ix):\n    # create an empty model\n    new_model = Sequential()\n    for ix in range(starting_layer_ix, ending_layer_ix + 1):\n        curr_layer = main_model.get_layer(index=ix)\n        # copy this layer over to the new model\n        new_model.add(curr_layer)\n    return new_model","metadata":{"execution":{"iopub.status.busy":"2022-04-24T20:42:20.931443Z","iopub.execute_input":"2022-04-24T20:42:20.932166Z","iopub.status.idle":"2022-04-24T20:42:20.937821Z","shell.execute_reply.started":"2022-04-24T20:42:20.932119Z","shell.execute_reply":"2022-04-24T20:42:20.936915Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_part = extract_layers(model, 0, 6) #.summary()\nmodel_part.summary()","metadata":{"execution":{"iopub.status.busy":"2022-04-24T20:43:38.597179Z","iopub.execute_input":"2022-04-24T20:43:38.597521Z","iopub.status.idle":"2022-04-24T20:43:38.680233Z","shell.execute_reply.started":"2022-04-24T20:43:38.597479Z","shell.execute_reply":"2022-04-24T20:43:38.679530Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.preprocessing import image\n\nfolder_path = \"./testing_data\"\nimg_width, img_height = 224, 224\n\npredictions=[]\nfor img in tqdm(os.listdir(folder_path)):\n    img = os.path.join(folder_path, img)\n    img = image.load_img(img, target_size=(img_width, img_height))\n    img = image.img_to_array(img)\n    img = np.expand_dims(img, axis=0)\n    \n    prediction = model_part.predict(img, batch_size=10)\n    predictions.append(prediction)","metadata":{"execution":{"iopub.status.busy":"2022-04-24T21:08:02.799395Z","iopub.execute_input":"2022-04-24T21:08:02.800292Z","iopub.status.idle":"2022-04-24T21:12:42.041428Z","shell.execute_reply.started":"2022-04-24T21:08:02.800243Z","shell.execute_reply":"2022-04-24T21:12:42.040710Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"a = np.array(predictions)\n# pd.DataFrame(a).to_csv('testing_data.csv')","metadata":{"execution":{"iopub.status.busy":"2022-04-24T21:12:42.043158Z","iopub.execute_input":"2022-04-24T21:12:42.043601Z","iopub.status.idle":"2022-04-24T21:12:42.055775Z","shell.execute_reply.started":"2022-04-24T21:12:42.043558Z","shell.execute_reply":"2022-04-24T21:12:42.054896Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pickle","metadata":{"execution":{"iopub.status.busy":"2022-04-24T21:12:42.057635Z","iopub.execute_input":"2022-04-24T21:12:42.058155Z","iopub.status.idle":"2022-04-24T21:12:42.062127Z","shell.execute_reply.started":"2022-04-24T21:12:42.058118Z","shell.execute_reply":"2022-04-24T21:12:42.061401Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with open('testing_emb_vgg.pickle', 'wb') as handle:\n    pickle.dump(a, handle, protocol=pickle.HIGHEST_PROTOCOL)","metadata":{"execution":{"iopub.status.busy":"2022-04-24T21:12:42.064255Z","iopub.execute_input":"2022-04-24T21:12:42.064953Z","iopub.status.idle":"2022-04-24T21:12:42.072476Z","shell.execute_reply.started":"2022-04-24T21:12:42.064852Z","shell.execute_reply":"2022-04-24T21:12:42.071612Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_part.predict(images[0])","metadata":{"execution":{"iopub.status.busy":"2022-04-24T20:44:47.895921Z","iopub.execute_input":"2022-04-24T20:44:47.896203Z","iopub.status.idle":"2022-04-24T20:44:48.356041Z","shell.execute_reply.started":"2022-04-24T20:44:47.896170Z","shell.execute_reply":"2022-04-24T20:44:48.355320Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Resnet preprocessing (None)","metadata":{"execution":{"iopub.status.busy":"2022-03-09T14:49:08.189357Z","iopub.execute_input":"2022-03-09T14:49:08.189658Z","iopub.status.idle":"2022-03-09T14:49:08.19382Z","shell.execute_reply.started":"2022-03-09T14:49:08.189629Z","shell.execute_reply":"2022-03-09T14:49:08.192824Z"}}},{"cell_type":"code","source":"datagen = ImageDataGenerator(preprocessing_function=lambda x: x,validation_split=0.10)\n","metadata":{"execution":{"iopub.status.busy":"2022-04-24T21:14:30.181215Z","iopub.execute_input":"2022-04-24T21:14:30.181871Z","iopub.status.idle":"2022-04-24T21:14:30.186167Z","shell.execute_reply.started":"2022-04-24T21:14:30.181826Z","shell.execute_reply":"2022-04-24T21:14:30.185172Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classe_names","metadata":{"execution":{"iopub.status.busy":"2022-04-24T21:14:33.507208Z","iopub.execute_input":"2022-04-24T21:14:33.507798Z","iopub.status.idle":"2022-04-24T21:14:33.513986Z","shell.execute_reply.started":"2022-04-24T21:14:33.507755Z","shell.execute_reply":"2022-04-24T21:14:33.512977Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_generator=datagen.flow_from_dataframe(\n    df_train,\n    directory='./training_data/',\n    x_col='image',\n    y_col='species',\n    subset=\"training\",\n    target_size=(224,224), \n    batch_size=32,\n    rescale=1.0/255,\n    seed=7641,\n    shuffle=True,\n    classes=classe_names,\n    class_mode=\"categorical\",)","metadata":{"execution":{"iopub.status.busy":"2022-04-24T21:14:40.875432Z","iopub.execute_input":"2022-04-24T21:14:40.876063Z","iopub.status.idle":"2022-04-24T21:14:41.147342Z","shell.execute_reply.started":"2022-04-24T21:14:40.876023Z","shell.execute_reply":"2022-04-24T21:14:41.146546Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"valid_generator=datagen.flow_from_dataframe(\n    df_train,\n    directory='./training_data/',\n    x_col='image',\n    y_col='species',\n    subset=\"validation\",\n    target_size=(224,224), \n    batch_size=32,\n    rescale=1.0/255,\n    seed=7641,\n    shuffle=True,\n    classes=classe_names,\n    class_mode=\"categorical\",)","metadata":{"execution":{"iopub.status.busy":"2022-04-24T21:14:41.791004Z","iopub.execute_input":"2022-04-24T21:14:41.791877Z","iopub.status.idle":"2022-04-24T21:14:42.025676Z","shell.execute_reply.started":"2022-04-24T21:14:41.791828Z","shell.execute_reply":"2022-04-24T21:14:42.024759Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_submission=pd.read_csv('../input/happy-whale-and-dolphin/sample_submission.csv')\ndf_submission.head()","metadata":{"execution":{"iopub.status.busy":"2022-04-24T21:16:26.288273Z","iopub.execute_input":"2022-04-24T21:16:26.289197Z","iopub.status.idle":"2022-04-24T21:16:26.330647Z","shell.execute_reply.started":"2022-04-24T21:16:26.289151Z","shell.execute_reply":"2022-04-24T21:16:26.329807Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_datagen = ImageDataGenerator(preprocessing_function=lambda x: x,validation_split=0.0)\n\ntest_generator=datagen.flow_from_dataframe(\n    df_train,\n    directory='./testing_data/',\n    x_col='image',\n    y_col='species',\n    subset=\"training\",\n    target_size=(224,224), \n    batch_size=1,\n    rescale=1.0/255,\n    seed=7641,\n    shuffle=True,\n    classes=classe_names,\n    class_mode=\"categorical\",)","metadata":{"execution":{"iopub.status.busy":"2022-04-24T21:16:29.136108Z","iopub.execute_input":"2022-04-24T21:16:29.136380Z","iopub.status.idle":"2022-04-24T21:16:29.353801Z","shell.execute_reply.started":"2022-04-24T21:16:29.136348Z","shell.execute_reply":"2022-04-24T21:16:29.353038Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(nrows=1, ncols=4, figsize=(15,15))\n\nfor i in range(4):\n    image = next(test_generator)[0].astype('uint8')[0]\n    image = np.squeeze(image)\n    ax[i].imshow(image)\n    ax[i].axis('off')","metadata":{"execution":{"iopub.status.busy":"2022-04-24T21:16:31.198837Z","iopub.execute_input":"2022-04-24T21:16:31.199120Z","iopub.status.idle":"2022-04-24T21:16:31.713042Z","shell.execute_reply.started":"2022-04-24T21:16:31.199088Z","shell.execute_reply":"2022-04-24T21:16:31.712354Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(nrows=1, ncols=4, figsize=(15,15))\n\nfor i in range(4):\n    image = next(train_generator)[0].astype('uint8')[0]\n    image = np.squeeze(image)\n    ax[i].imshow(image)\n    ax[i].axis('off')","metadata":{"execution":{"iopub.status.busy":"2022-04-24T21:16:31.943163Z","iopub.execute_input":"2022-04-24T21:16:31.943889Z","iopub.status.idle":"2022-04-24T21:16:32.568964Z","shell.execute_reply.started":"2022-04-24T21:16:31.943848Z","shell.execute_reply":"2022-04-24T21:16:32.568315Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(nrows=1, ncols=4, figsize=(15,15))\n\nfor i in range(4):\n    image = next(valid_generator)[0].astype('uint8')[0]\n    image = np.squeeze(image)\n    ax[i].imshow(image)\n    ax[i].axis('off')","metadata":{"execution":{"iopub.status.busy":"2022-04-24T21:16:33.280388Z","iopub.execute_input":"2022-04-24T21:16:33.281210Z","iopub.status.idle":"2022-04-24T21:16:33.887771Z","shell.execute_reply.started":"2022-04-24T21:16:33.281167Z","shell.execute_reply":"2022-04-24T21:16:33.887033Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"resnet_model = tf.keras.applications.resnet50.ResNet50(\n    include_top=False,\n    weights='imagenet',\n    input_tensor=None,\n    input_shape=(224, 224, 3),\n    pooling=None,\n    classes=1000\n)\n\nresnet_model.trainable = False","metadata":{"execution":{"iopub.status.busy":"2022-04-24T21:16:35.757901Z","iopub.execute_input":"2022-04-24T21:16:35.760930Z","iopub.status.idle":"2022-04-24T21:16:37.856596Z","shell.execute_reply.started":"2022-04-24T21:16:35.760878Z","shell.execute_reply":"2022-04-24T21:16:37.855828Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\n# model.add(Conv2D(32, (3, 3), activation='relu', padding='same', name='conv_1', \n#                  input_shape=(224, 224, 3)))\n# model.add(MaxPooling2D((2, 2), name='maxpool_1'))\n# model.add(Conv2D(64, (3, 3), activation='relu', padding='same', name='conv_2'))\n# model.add(MaxPooling2D((2, 2), name='maxpool_2'))\n# model.add(Conv2D(128, (3, 3), activation='relu', padding='same', name='conv_3'))\n# model.add(MaxPooling2D((2, 2), name='maxpool_3'))\n# model.add(Conv2D(128, (3, 3), activation='relu', padding='same', name='conv_4'))\n# model.add(MaxPooling2D((2, 2), name='maxpool_4'))\nmodel.add(resnet_model)\nmodel.add(Flatten())\nmodel.add(Dropout(0.5))\nmodel.add(Dense(512, activation='relu', name='dense_1'))\nmodel.add(Dense(256, activation='relu', name='dense_2'))\nmodel.add(Dense(len(classe_names), activation='sigmoid', name='output'))\n\nmodel.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-04-24T21:16:46.323317Z","iopub.execute_input":"2022-04-24T21:16:46.323955Z","iopub.status.idle":"2022-04-24T21:16:46.744901Z","shell.execute_reply.started":"2022-04-24T21:16:46.323911Z","shell.execute_reply":"2022-04-24T21:16:46.744147Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#without VGG preprocessing\ncallback = tf.keras.callbacks.EarlyStopping(\n    monitor=\"val_accuracy\",\n    min_delta=0,\n    patience=10,\n    verbose=0,\n    mode=\"auto\",\n    baseline=None,\n    restore_best_weights=True,\n)\nhistory = model.fit(train_generator, epochs=24, validation_data=valid_generator, verbose=1, callbacks=[callback])","metadata":{"execution":{"iopub.status.busy":"2022-04-24T21:16:49.477971Z","iopub.execute_input":"2022-04-24T21:16:49.478747Z","iopub.status.idle":"2022-04-24T21:21:37.381471Z","shell.execute_reply.started":"2022-04-24T21:16:49.478705Z","shell.execute_reply":"2022-04-24T21:21:37.378881Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy', 'Precision', 'Recall'])\nmodel.evaluate_generator(test_generator, 5431)","metadata":{"execution":{"iopub.status.busy":"2022-04-24T21:21:50.112363Z","iopub.execute_input":"2022-04-24T21:21:50.113183Z","iopub.status.idle":"2022-04-24T21:23:02.017041Z","shell.execute_reply.started":"2022-04-24T21:21:50.113138Z","shell.execute_reply":"2022-04-24T21:23:02.016321Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"modelPredictions = model.predict(test_generator)\nmodelPredictions1 = [np.argmax(x) for x in modelPredictions]\ntestLabels = test_generator.labels\nplt.imshow(tf.math.confusion_matrix(testLabels, modelPredictions1))","metadata":{"execution":{"iopub.status.busy":"2022-04-24T21:23:15.415947Z","iopub.execute_input":"2022-04-24T21:23:15.416589Z","iopub.status.idle":"2022-04-24T21:24:10.464059Z","shell.execute_reply.started":"2022-04-24T21:23:15.416534Z","shell.execute_reply":"2022-04-24T21:24:10.463320Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.preprocessing import image\n\nfolder_path = \"./training_data\"\nimg_width, img_height = 224, 224\n\npredictions=[]\nfor img in tqdm(os.listdir(folder_path)):\n    img = os.path.join(folder_path, img)\n    img = image.load_img(img, target_size=(img_width, img_height))\n    img = image.img_to_array(img)\n    img = np.expand_dims(img, axis=0)\n    \n    prediction = model_part.predict(img, batch_size=10)\n    predictions.append(prediction)","metadata":{"execution":{"iopub.status.busy":"2022-04-24T21:31:15.549279Z","iopub.execute_input":"2022-04-24T21:31:15.549598Z","iopub.status.idle":"2022-04-24T21:49:53.231057Z","shell.execute_reply.started":"2022-04-24T21:31:15.549564Z","shell.execute_reply":"2022-04-24T21:49:53.230269Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"a = np.array(predictions)\n# pd.DataFrame(a).to_csv('testing_data.csv')","metadata":{"execution":{"iopub.status.busy":"2022-04-24T21:50:18.528329Z","iopub.execute_input":"2022-04-24T21:50:18.529121Z","iopub.status.idle":"2022-04-24T21:50:18.558204Z","shell.execute_reply.started":"2022-04-24T21:50:18.529079Z","shell.execute_reply":"2022-04-24T21:50:18.557500Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pickle","metadata":{"execution":{"iopub.status.busy":"2022-04-24T21:50:19.784371Z","iopub.execute_input":"2022-04-24T21:50:19.784908Z","iopub.status.idle":"2022-04-24T21:50:19.788819Z","shell.execute_reply.started":"2022-04-24T21:50:19.784870Z","shell.execute_reply":"2022-04-24T21:50:19.788103Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with open('training_emb_resnet.pickle', 'wb') as handle:\n    pickle.dump(a, handle, protocol=pickle.HIGHEST_PROTOCOL)","metadata":{"execution":{"iopub.status.busy":"2022-04-24T21:50:25.156086Z","iopub.execute_input":"2022-04-24T21:50:25.156530Z","iopub.status.idle":"2022-04-24T21:50:25.165594Z","shell.execute_reply.started":"2022-04-24T21:50:25.156427Z","shell.execute_reply":"2022-04-24T21:50:25.164560Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_part.predict(images[0])","metadata":{"execution":{"iopub.status.busy":"2022-04-24T20:44:47.895921Z","iopub.execute_input":"2022-04-24T20:44:47.896203Z","iopub.status.idle":"2022-04-24T20:44:48.356041Z","shell.execute_reply.started":"2022-04-24T20:44:47.896170Z","shell.execute_reply":"2022-04-24T20:44:48.355320Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Xception","metadata":{}},{"cell_type":"code","source":"xception_model = tf.keras.applications.xception.Xception(\n    include_top=False,\n    weights='imagenet',\n    input_tensor=None,\n    input_shape=(224, 224, 3),\n    pooling=None,\n    classes=1000,\n    classifier_activation='softmax'\n)\n\nxception_model.trainable = False","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\n# model.add(Conv2D(32, (3, 3), activation='relu', padding='same', name='conv_1', \n#                  input_shape=(224, 224, 3)))\n# model.add(MaxPooling2D((2, 2), name='maxpool_1'))\n# model.add(Conv2D(64, (3, 3), activation='relu', padding='same', name='conv_2'))\n# model.add(MaxPooling2D((2, 2), name='maxpool_2'))\n# model.add(Conv2D(128, (3, 3), activation='relu', padding='same', name='conv_3'))\n# model.add(MaxPooling2D((2, 2), name='maxpool_3'))\n# model.add(Conv2D(128, (3, 3), activation='relu', padding='same', name='conv_4'))\n# model.add(MaxPooling2D((2, 2), name='maxpool_4'))\nmodel.add(xception_model)\nmodel.add(Flatten())\nmodel.add(Dropout(0.5))\nmodel.add(Dense(512, activation='relu', name='dense_1'))\nmodel.add(Dense(256, activation='relu', name='dense_2'))\nmodel.add(Dense(128, activation='relu', name='dense_3'))\nmodel.add(Dense(len(classe_names), activation='sigmoid', name='output'))\n\nmodel.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])\nmodel.summary()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"callback = tf.keras.callbacks.EarlyStopping(\n    monitor=\"val_accuracy\",\n    min_delta=0,\n    patience=10,\n    verbose=0,\n    mode=\"auto\",\n    baseline=None,\n    restore_best_weights=True,\n)\nhistory = model.fit(train_generator, epochs=24, validation_data=valid_generator, verbose=1, callbacks=[callback])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy', 'Precision', 'Recall'])\nmodel.evaluate_generator(test_generator, 5431)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"modelPredictions = model.predict(test_generator)\nmodelPredictions1 = [np.argmax(x) for x in modelPredictions]\ntestLabels = test_generator.labels\nplt.imshow(tf.math.confusion_matrix(testLabels, modelPredictions1))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Save model","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.models import load_model\nmodel.save('model.h5')\nnew_model=load_model('model.h5')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"probability_model = tf.keras.Sequential([new_model, \n                                         tf.keras.layers.Softmax()])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = probability_model.predict(test_generator[0])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions[0]","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.argmax(predictions[0])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_generator.class_indices","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.image as mpimg\nimg = mpimg.imread('../input/happy-whale-and-dolphin/test_images/000110707af0ba.jpg')\nimgplot = plt.imshow(img)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}