{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np \nimport pandas as pd\nimport os\nfrom PIL import Image\nimport cv2\nimport matplotlib.pyplot as plt\nfrom tqdm import tqdm\nfrom glob import glob\nfrom sklearn.model_selection import train_test_split\nimport seaborn as sns\nimport time\nfrom imagehash import phash\nfrom math import sqrt\n\n\nfrom subprocess import check_output\nfrom glob import glob\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D\nfrom tensorflow.keras.layers import Dense, Dropout, Flatten, Activation\nfrom tensorflow.keras.metrics import categorical_accuracy, top_k_categorical_accuracy, categorical_crossentropy\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.callbacks import EarlyStopping, ReduceLROnPlateau, ModelCheckpoint\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.applications import ResNet50,MobileNetV2\nfrom tensorflow.keras.applications.mobilenet import preprocess_input\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\n\nprint(os.listdir(\"../input\"))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9afcb91d49e5e961197dc292bde249dac7020668"},"cell_type":"code","source":"def top_5_accuracy(x,y): \n    t5 = top_k_categorical_accuracy(x,y,5)\n    return t5","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1003fed82ca9103439f7b5bd7b2e3cbde71dc07a"},"cell_type":"code","source":"train_imgs = \"../input/train\"\ntest_imgs = \"../input/test\"\n\nresize = 224\nbatch_size = 64","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"36f37ac697a7e0582380b5a38738a695a4b24aa8"},"cell_type":"code","source":"train = pd.read_csv(\"../input/train.csv\")\ntrain = train.loc[train['Id'] != 'new_whale']\nnum_classes = len(train['Id'].unique())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2e8cf39c76b0ca2c1536a52a4e0effef3b2ba96f","scrolled":true},"cell_type":"code","source":"d = {cat: k for k,cat in enumerate(train.Id.unique())}","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8bd4a18a46164bc12947c08ec1c65f047d6eb836"},"cell_type":"markdown","source":"### Distribution of Labels\nMajority of images only appear in training dataset once; this makes this situation a great candiate for a one shot learning simese network. "},{"metadata":{"trusted":true,"_uuid":"430a07eca777b5c4870971ff332b09d92d840458","scrolled":true},"cell_type":"code","source":"plt.title('Distribution of classes excluding new_whale');\ntrain.Id.value_counts()[1:].plot(kind='hist');","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"df6515c0113a805b8fb28f8f8bca9df24ec13f1d"},"cell_type":"markdown","source":"### PreProccessing \n\nTraining is performed on images subjected to the following operations:\n\n* Transform to black and white;\n* Normalized to 0 mean and unit variance"},{"metadata":{"trusted":true,"_uuid":"7838a37a2eba1cf4ad0d8930797f38c2a02031e4","scrolled":false},"cell_type":"code","source":"im_arrays = []\nlabels = []\nfs = {} ##dictionary with original size of each photo \nfor index, row in tqdm(train.iterrows()):  \n    im = cv2.imread(os.path.join(train_imgs,row['Image']),0)\n    norm_image = cv2.normalize(im, None, alpha=0, beta=1, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_32F)\n    new_image = cv2.resize(norm_image,(resize,resize))\n    new_image = np.reshape(new_image,[resize,resize,1])\n    im_arrays.append(new_image)\n    labels.append(d[row['Id']])\n    fs[row['Image']] = norm_image.shape\ntrain_ims = np.array(im_arrays)\ntrain_labels = np.array(labels)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ba658df60ddb3579bd067db819601d7a811e4c14","scrolled":false},"cell_type":"code","source":"train_labels = keras.utils.to_categorical(train_labels)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"13eef0eca9b325234eef5844f059e598a4997f62"},"cell_type":"code","source":"x_train,x_val, y_train, y_val = train_test_split(train_ims,\n                                                   train_labels,\n                                                   test_size=0.10, \n                                                   random_state=42\n                                                  )","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a177622c593c6d092064848d3b32aa16fcf27e80"},"cell_type":"code","source":"print(x_train.shape)\nprint(x_val.shape)\nprint(y_train.shape)\nprint(y_val.shape)\n##print(test_imgs.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6ff5304eeca3746fb96112f52d390fb59301676b"},"cell_type":"code","source":"gen =ImageDataGenerator(zoom_range = 0.2,\n                            horizontal_flip = True\n                       )","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"cab0b4be7f0f9201ee4a0038ac653a40b3b7d82f"},"cell_type":"code","source":"reduceLROnPlat = ReduceLROnPlateau(monitor='val_top_5_accuracy',\n                                      factor = 0.50,\n                                      patience = 3,\n                                      verbose = 1, \n                                      mode = 'max', \n                                      min_delta = .001,\n                                      min_lr = 1e-5\n                                  )\n\nearlystop = EarlyStopping(monitor='val_top_5_accuracy',\n                            mode= 'max',\n                            patience= 5 )\n\ncallbacks = [earlystop, reduceLROnPlat]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4ba5cf0ba6338d1dc0e38a54f4458ee4d426cee8"},"cell_type":"code","source":"model = ResNet50(input_shape=(resize, resize, 1),\n                      weights=None, \n                      classes=num_classes)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"89c20c9129d8a07c68a37603fe1da9f34924235d"},"cell_type":"code","source":"model.compile(optimizer=Adam(lr = .005), loss='categorical_crossentropy',\n              metrics=[categorical_crossentropy, categorical_accuracy, top_5_accuracy])\nprint(model.summary())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4215e21c0b7027fd81d832dfe6d8c75059ef61e6"},"cell_type":"code","source":"batches = gen.flow(x_train, y_train, batch_size=batch_size)\nval_batches=gen.flow(x_val, y_val, batch_size=batch_size)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"567cc9d5588f7d138f4b7b8a032506e03eecccc5"},"cell_type":"code","source":"batches.n//batch_size","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"59fe42fb02ea558b3e75d24327ea6bd6b70577cd"},"cell_type":"code","source":"epochs = 50\nhistory=model.fit_generator(generator=batches, \n                            steps_per_epoch=batches.n//batch_size, \n                            epochs=epochs, \n                            validation_data=val_batches, \n                            validation_steps=val_batches.n//batch_size,\n                            callbacks = callbacks)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"85dc961599142538532b61f34a22f8edb5ac6155"},"cell_type":"code","source":"# Plot the loss and accuracy curves for training and validation \nfig, ax = plt.subplots(2,1)\nax[0].plot(history.history['categorical_crossentropy'], color='b', label=\"Training loss\")\nax[0].plot(history.history['val_categorical_crossentropy'], color='r', label=\"Validation loss\",axes =ax[0])\nlegend = ax[0].legend(loc='best', shadow=True)\n\nax[1].plot(history.history['top_5_accuracy'], color='b', label=\"Training Top 5 Accuracy\")\nax[1].plot(history.history['val_top_5_accuracy'], color='r',label=\"Validation Top 5 accuracy\")\nlegend = ax[1].legend(loc='best', shadow=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8555b1347442ae95cb969f9ae52a366aac604cf6"},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}