{"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\nimport matplotlib.pyplot as plt\nimport matplotlib.image as mplimg\nfrom matplotlib.pyplot import imshow\n\nfrom keras import optimizers\nfrom sklearn.preprocessing import LabelEncoder\nfrom sklearn.preprocessing import OneHotEncoder\n\nfrom keras import layers\nfrom keras.preprocessing import image\nfrom keras.applications.imagenet_utils import preprocess_input\nfrom keras.layers import Input, Dense, Activation, BatchNormalization, Flatten, Conv2D\nfrom keras.layers import AveragePooling2D, MaxPooling2D, Dropout\nfrom keras.models import Model\n\nimport keras.backend as K\nfrom keras.models import Sequential\n\nimport warnings\nwarnings.simplefilter(\"ignore\", category=DeprecationWarning)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d4dd084c8a528cc3cd1886f49c4738478e107ac1"},"cell_type":"code","source":"train_df = pd.read_csv(\"../input/humpback-whale-identification/train.csv\")\ntrain_df.head()","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-output":false,"trusted":true,"_uuid":"40beb834cf7d87210b7a97e9d60b20089417d00e"},"cell_type":"code","source":"\nplt.figure(figsize=(25,25))\nax1=plt.subplot(5, 2, 1)\nimg = image.load_img(\"../input/humpback-whale-identification/train/\"+train_df['Image'][0])\nprint(train_df['Id'][0])\nimshow(img)\n\nax1=plt.subplot(5, 2, 2)\nimg = image.load_img(\"../input/humpback-whale-identification/train/\"+train_df['Image'][1])\nprint(train_df['Id'][1])\nimshow(img)\n\n\nax1=plt.subplot(5, 2, 3)\nimg = image.load_img(\"../input/humpback-whale-identification/train/\"+train_df['Image'][2])\nprint(train_df['Id'][2])\nimshow(img)\n\n\nax1=plt.subplot(5, 2, 4)\nimg = image.load_img(\"../input/humpback-whale-identification/train/\"+train_df['Image'][3])\nprint(train_df['Id'][3])\nimshow(img)\n\nax1=plt.subplot(5, 2, 5)\nimg = image.load_img(\"../input/humpback-whale-identification/train/\"+train_df['Image'][4])\nprint(train_df['Id'][4])\nimshow(img)\n\n\nax1=plt.subplot(5, 2, 6)\nimg = image.load_img(\"../input/humpback-whale-identification/train/\"+train_df['Image'][5])\nprint(train_df['Id'][5])\nimshow(img)\n\n\nax1=plt.subplot(5, 2, 7)\nimg = image.load_img(\"../input/humpback-whale-identification/train/\"+train_df['Image'][6])\nprint(train_df['Id'][6])\nimshow(img)\n\n\n\nax1=plt.subplot(5, 2, 8)\nimg = image.load_img(\"../input/humpback-whale-identification/train/\"+train_df['Image'][7])\nprint(train_df['Id'][7])\nimshow(img)\n\n\nax1=plt.subplot(5, 2, 9)\nimg = image.load_img(\"../input/humpback-whale-identification/train/\"+train_df['Image'][8])\nprint(train_df['Id'][8])\nimshow(img)\n\n\nax1=plt.subplot(5, 2, 10)\nimg = image.load_img(\"../input/humpback-whale-identification/train/\"+train_df['Image'][9])\nprint(train_df['Id'][9])\nimshow(img)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"dd7250c6ccc9e0d6ff467d399e8e86d4be6a3326"},"cell_type":"code","source":"from progressbar import ProgressBar\n\n\ndef prepareImages(data, m, dataset):\n    pbar = ProgressBar()\n    X_train = np.zeros((m, 100, 100, 3))\n    count = 0\n    \n    for fig in pbar(data['Image']):\n        #load images into images of size 100x100x3\n        img = image.load_img(\"../input/humpback-whale-identification/\"+dataset+\"/\"+fig, target_size=(100, 100, 3))\n        x = image.img_to_array(img)\n        x = preprocess_input(x)\n\n        X_train[count] = x\n        count += 1\n    \n    return X_train\n\ndef prepare_labels(y):\n    values = np.array(y)\n    label_encoder = LabelEncoder()\n    integer_encoded = label_encoder.fit_transform(values)\n    # print(integer_encoded)\n\n    onehot_encoder = OneHotEncoder(sparse=False)\n    integer_encoded = integer_encoded.reshape(len(integer_encoded), 1)\n    onehot_encoded = onehot_encoder.fit_transform(integer_encoded)\n    # print(onehot_encoded)\n\n    y = onehot_encoded\n    # print(y.shape)\n    return y, label_encoder","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"X = prepareImages(train_df, train_df.shape[0], \"train\")\nX /= 255","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"863301c8b3a82318230f29248c4aa84d359354f1"},"cell_type":"code","source":"y_train, label_encoder = prepare_labels(train_df['Id'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"90927fa53ac35b00ebb76c7789df2e286926eb2a"},"cell_type":"code","source":"y_train.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"cf84e1b72c3a473ca5bbe78a367998965d12bbe4"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"ac25c6ed32fecf15b18d92fd7269a9a344b5f0ea"},"cell_type":"markdown","source":"## This means that there are 5005 classes"},{"metadata":{"trusted":true,"_uuid":"701d9a389d533e4308557bd4138a731c67360717"},"cell_type":"code","source":"from pathlib import Path\n\nfrom tqdm import tqdm\nfrom keras import applications\n\nweights = Path('../input/xception-weight/xception_weights_tf_dim_ordering_tf_kernels_notop (1).h5')\n# base_model = applications.inception_resnet_v2.InceptionResNetV2(include_top=False, weights=weights, input_shape=(100, 100, 3) )#input_tensor=None, input_shape=None, pooling=None, classes=1000)\nbase_model =applications.xception.Xception(input_shape=(100, 100, 3), include_top=False, weights=weights)## set model architechture\nx = base_model.output\nx = Flatten()(x)\nx = Dense(256, activation='relu')(x)\nx = Dense(256, activation='relu')(x)\npredictions = Dense(y_train.shape[1], activation='softmax')(x) \nmodel = Model(input=base_model.input, output=predictions)\n\nmodel.compile(loss='categorical_crossentropy', optimizer=optimizers.Adam(lr=1e-4),\n              metrics=['accuracy'])\n\nmodel.summary()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"2cf9c175d589efaf19ec13aa74d2424657185e22"},"cell_type":"markdown","source":"Let us perform data Agumentation to increase the number of training points"},{"metadata":{"trusted":true,"_uuid":"10173765999a3f5222aae41fc92a168aa57413c0"},"cell_type":"code","source":"from keras.preprocessing.image import ImageDataGenerator\n\ntrain_datagen = ImageDataGenerator(\n        rotation_range=0,\n        width_shift_range=0,\n        height_shift_range=0,\n        shear_range=0,\n        zoom_range=0,\n        vertical_flip=True,\n        horizontal_flip=True)\n\ntrain_datagen.fit(X)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"31aef091886a6c2a0c71f9a984512fca7560c0d9"},"cell_type":"code","source":"history = model.fit_generator(\n    train_datagen.flow(X, y_train, batch_size=100),\n    steps_per_epoch=10,\n    epochs=1000\n)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"669cd5a8bfbe668e897db16ac2e7ea7fe782af52"},"cell_type":"code","source":"plt.plot(history.history['loss'])\nplt.title('Model Loss')\nplt.ylabel('Loss')\nplt.xlabel('Epoch')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"228088d230256f8a41282a446e499482daee5d92"},"cell_type":"code","source":"plt.plot(history.history['acc'])\nplt.title('Model categorical accuracy')\nplt.ylabel('categorical accuracy')\nplt.xlabel('Epoch')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"bff35f164b8b50883287857886d5c39f304e32f1"},"cell_type":"code","source":"test = os.listdir(\"../input/humpback-whale-identification/test/\")\nprint(len(test))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4ee2b95d31f0e3e08e6e5cde9a651267c31cd7b0"},"cell_type":"code","source":"col = ['Image']\ntest_df = pd.DataFrame(test, columns=col)\ntest_df['Id'] = ''","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"84bb315f31ee6356e8f2516078b696257575795f"},"cell_type":"code","source":"X = prepareImages(test_df, test_df.shape[0], \"test\")\nX /= 255","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"083c8cefe09808b8e1b5ec50d5ee7b0e08f748ce"},"cell_type":"code","source":"predictions = model.predict(np.array(X), verbose=1)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"c24cccafc9b11835dc7a8efd9135e7d850457a0a"},"cell_type":"markdown","source":"Now we will simply add the predicted labels into the test dataframe"},{"metadata":{"trusted":true,"_uuid":"7850c88fb2444992afbec9fff0c54fb6f92b0df9"},"cell_type":"code","source":"for i, pred in enumerate(predictions):\n    test_df.loc[i, 'Id'] = ' '.join(label_encoder.inverse_transform(pred.argsort()[-5:][::-1]))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2fec7776e02f5fab389141b324b2feaaba2681e7"},"cell_type":"code","source":"test_df.head(10)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a6573a9dfc3d526632a69c255e1643c8afa2d6e7"},"cell_type":"code","source":"test_df.to_csv('ans.csv', index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"af4a5d398c4740d9e282d6f34bdd4b9381166a13"},"cell_type":"code","source":"\nplt.figure(figsize=(25,25))\nax1=plt.subplot(5, 2, 1)\nimg = image.load_img(\"../input/humpback-whale-identification/test/\"+test_df['Image'][0])\nprint(test_df['Id'][0])\nimshow(img)\n\nax1=plt.subplot(5, 2, 2)\nimg = image.load_img(\"../input/humpback-whale-identification/test/\"+test_df['Image'][1])\nprint(test_df['Id'][1])\nimshow(img)\n\n\nax1=plt.subplot(5, 2, 3)\nimg = image.load_img(\"../input/humpback-whale-identification/test/\"+test_df['Image'][2])\nprint(test_df['Id'][2])\nimshow(img)\n\n\nax1=plt.subplot(5, 2, 4)\nimg = image.load_img(\"../input/humpback-whale-identification/test/\"+test_df['Image'][3])\nprint(test_df['Id'][3])\nimshow(img)\n\nax1=plt.subplot(5, 2, 5)\nimg = image.load_img(\"../input/humpback-whale-identification/test/\"+test_df['Image'][4])\nprint(test_df['Id'][4])\nimshow(img)\n\n\nax1=plt.subplot(5, 2, 6)\nimg = image.load_img(\"../input/humpback-whale-identification/test/\"+test_df['Image'][5])\nprint(test_df['Id'][5])\nimshow(img)\n\n\nax1=plt.subplot(5, 2, 7)\nimg = image.load_img(\"../input/humpback-whale-identification/test/\"+test_df['Image'][6])\nprint(test_df['Id'][6])\nimshow(img)\n\n\n\nax1=plt.subplot(5, 2, 8)\nimg = image.load_img(\"../input/humpback-whale-identification/test/\"+test_df['Image'][7])\nprint(test_df['Id'][7])\nimshow(img)\n\n\nax1=plt.subplot(5, 2, 9)\nimg = image.load_img(\"../input/humpback-whale-identification/test/\"+test_df['Image'][8])\nprint(test_df['Id'][8])\nimshow(img)\n\n\nax1=plt.subplot(5, 2, 10)\nimg = image.load_img(\"../input/humpback-whale-identification/test/\"+test_df['Image'][9])\nprint(test_df['Id'][9])\nimshow(img)","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}