{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"markdown","source":"## Whale Identification - CNN\nIdentify a whale by the whale tail  \n"},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true,"_kg_hide-output":false},"cell_type":"code","source":"import os\nprint(os.listdir(\"../input\"))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2e5529de3bd3acdbe0f016f41895f64fd2fc6e34"},"cell_type":"code","source":"import tensorflow as tf\nimport random\nimport time\nimport cv2\n\nfrom skimage import io\nfrom pylab import rcParams\n\nfrom PIL import Image\nfrom PIL import ImageDraw\nfrom PIL import ImageFont\nfrom skimage.color import rgb2gray\nfrom skimage.transform import resize\nfrom skimage import data, color\n\nimport matplotlib.pyplot as plt\nfrom matplotlib.patches import Rectangle\n\nimport keras\nfrom keras.models import Sequential\nfrom keras.layers import Activation, Dropout, Flatten, Dense, Conv2D, MaxPooling2D,BatchNormalization,AveragePooling2D\nfrom keras.layers import Conv2D, MaxPooling2D\n\nfrom keras.preprocessing.image import (\n    random_rotation, random_shift, random_shear, random_zoom,\n    random_channel_shift,img_to_array, ImageDataGenerator)\n\nimport numpy as np\nimport pandas as pd\n\nimport warnings\nfrom glob import glob\n\nprint('TensorFlow version:', tf.__version__)\nprint('Keras version:', keras.__version__)\n\n%matplotlib inline","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"78cd867f785b8ae911ddbe52e1aa0c09b8b51d5f"},"cell_type":"code","source":"TRAIN_IMAGE_PATH = \"../input/train/\"\nTEST_IMAGE_PATH = \"../input/test/\"\nTRAINING_DATA='../input/train.csv'\nIMG_SIZE = 64","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6fcd52fc94ae13f011050fe19c7e7a29b1a44350"},"cell_type":"code","source":"df_train = pd.read_csv(TRAINING_DATA)\n#df_train.index.name = 'index'\n#df_train = df_train.query('index < 10000')\n\n#string to unique int\n#set unique int value for each unique classes sring.. string to int\nunique_calsses_value = np.unique(df_train[['Id']].values)\nprint(unique_calsses_value)\nunique_classes_id_dict = {}\nunique_id_classes_dict = {}\nfor i in range(len(unique_calsses_value)):\n    unique_classes_id_dict[unique_calsses_value[i]] = i\n    unique_id_classes_dict[i] = unique_calsses_value[i]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b6a3c88cae1c6bb53326558eae5fd9f94627e183"},"cell_type":"code","source":"#add new class_id col in df_train df\ndf_train['classes_id'] = df_train.apply (lambda row: unique_classes_id_dict.get(row['Id']),axis=1)\ndf_train.head(15)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e30962384a1e0e8c7cf479669e6e62aba64d02d7"},"cell_type":"code","source":"def show_image(image):\n    plt.imshow(image)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b7dbb7f9cc910d54f1a0888a2f7672c7a89c4785"},"cell_type":"code","source":"def plot_images(images):\n    rcParams['figure.figsize'] = 14, 8\n    plt.gray()\n    fig = plt.figure()\n    for i in range(min(9, images.shape[0])):\n        fig.add_subplot(3, 3, i+1)\n        show_image(images[i])\n    plt.show()   ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5bcc0161b76a9e043c324a83bae3e36cf168b151"},"cell_type":"code","source":"#resize the image\ndef LoadImage(img_path):\n    image = color.rgb2gray(io.imread(img_path))\n    image_resized = resize(image,(IMG_SIZE,IMG_SIZE))\n    return image_resized[:,:] / 255.\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ee6509dca39f19e9fd8fe685fcef72e95d22f97a"},"cell_type":"code","source":"#load  images data and classes id\ndef LoadImageData(path):\n    xs = []\n    ys = []\n    #for ex_paths in paths:\n    for index, row in df_train.iterrows():        \n        img_path = path + row['Image']\n        igm = LoadImage(img_path)\n        xs.append(igm)\n        ys.append(row['classes_id'])\n    return np.array(xs),np.array(ys)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"03e9bbd70cbbc1e83e6fc3c67ebc9b01e655e030"},"cell_type":"code","source":"X_train,Y_train = LoadImageData(TRAIN_IMAGE_PATH)\nprint(\"Loaded\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"37a54e9b3991e31f2faff4916bc94fdef743e694"},"cell_type":"code","source":"print(\"X_train \",X_train.shape)\nprint(\"Y_train \",Y_train.shape)\nprint(\"X_train \",len(df_train))\nprint(\"y_train \",Y_train)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ec6c6c032b0ad503830c87374897f7162e104005"},"cell_type":"code","source":"#plot randam images\nxs = [random.randint(0, X_train.shape[0]-1) for _ in range(9)]   \nprint(\"XS \",xs)\nplot_images(X_train[xs])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d0496a4e356070669086788c2937d1f17f38bbae"},"cell_type":"code","source":"X_train = X_train.reshape(-1, IMG_SIZE, IMG_SIZE, 1)\n#change the classes id to 0 1 format\nY_train = keras.utils.to_categorical(Y_train,num_classes=len(unique_classes_id_dict))\n\nprint(np.shape(X_train))\nprint(np.shape(Y_train))\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c788a88980403fd908870f2d0b1a52babf2cae2a"},"cell_type":"code","source":"#CNN model\ndef cnn():\n    model = Sequential()\n    model.add(Conv2D(32, (3, 3), strides = (1, 1), input_shape = (IMG_SIZE, IMG_SIZE, 1)))\n    model.add(BatchNormalization(axis = 3))\n    model.add(Activation('relu'))\n    model.add(MaxPooling2D((2, 2)))\n    model.add(Conv2D(64, (3, 3), strides = (1,1)))\n    model.add(Activation('relu'))\n    model.add(AveragePooling2D((3, 3)))\n    model.add(Flatten())\n    model.add(Dense(500, activation=\"relu\"))\n    model.add(Dropout(0.6))\n    model.add(Dense(5005, activation='softmax'))\n    model.compile(loss='categorical_crossentropy', optimizer=\"adam\", metrics=['accuracy'])\n    model.summary()\n    return model","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"fac76efddf20dc84b1188a9771ccf6318bd30707"},"cell_type":"code","source":"model = cnn()\nhistory = model.fit(X_train, Y_train, epochs=100, batch_size=100, verbose=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e052f4a13d80abe818f482c787b37bd37d99fea2"},"cell_type":"code","source":"plt.plot(history.history['acc'], color='green', linewidth = 2, \n         marker='o', markerfacecolor='blue', markersize=4) \nplt.title('Whale Identification CNN Model accuracy')\nplt.ylabel('Accuracy')\nplt.xlabel('Epoch')\nplt.grid(True)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c3e823ce81de403c098eede3d4d3edd715edcd67"},"cell_type":"code","source":"SAMPLE_SUBMISSION_FILE=\"sample_submission.csv\"\n\ndef getLabel(classes):\n    result = []\n    for i in range(0, len(classes)):\n        _class = unique_id_classes_dict.get(classes[i])\n        result.append(_class)\n    return result\n\nwith open(SAMPLE_SUBMISSION_FILE,\"w\") as f:\n    test_imgs = glob(\"../input/test/*jpg\")\n    f.write(\"Image,Id\\n\")\n    for image in test_imgs:\n        #print(image)\n        igm = LoadImage(image)\n        X_test = np.array(igm)\n        X_test = X_test.reshape(-1, IMG_SIZE, IMG_SIZE, 1)\n        Y_test = model.predict_proba(X_test,batch_size=1)\n        best_predict_5 = np.argsort(Y_test)[0][::-1][:5]\n        pre = getLabel(best_predict_5)\n        #print(image, \" \".join( pre))\n        f.write(\"%s,%s\\n\" %(os.path.basename(image), \" \".join( pre)))\nprint(\"csv created\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"455ae784e1064e5be65683730a78703540753ffb"},"cell_type":"code","source":"df_sample = pd.read_csv(SAMPLE_SUBMISSION_FILE)\ndf_sample.head(10)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"71a819aa4fd1f662f87b708b3d37b6a43f40d7e5"},"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}