{"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 os\nimport pandas as pd\nimport numpy as np\nimport cv2\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\nfrom sklearn.model_selection import train_test_split\n\nfrom tensorflow.keras.utils import to_categorical, Sequence\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, Dropout, Flatten\nfrom tensorflow.keras.optimizers import RMSprop,Adam\nfrom tensorflow.keras.applications import ResNet50","metadata":{"execution":{"iopub.status.busy":"2022-02-24T07:25:54.715329Z","iopub.execute_input":"2022-02-24T07:25:54.716040Z","iopub.status.idle":"2022-02-24T07:26:00.063195Z","shell.execute_reply.started":"2022-02-24T07:25:54.715923Z","shell.execute_reply":"2022-02-24T07:26:00.062408Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = '/kaggle/input/happy-whale-and-dolphin/'\nos.listdir(path)","metadata":{"execution":{"iopub.status.busy":"2022-02-24T07:26:00.064772Z","iopub.execute_input":"2022-02-24T07:26:00.065158Z","iopub.status.idle":"2022-02-24T07:26:00.074874Z","shell.execute_reply.started":"2022-02-24T07:26:00.065115Z","shell.execute_reply":"2022-02-24T07:26:00.073939Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = pd.read_csv(path+'train.csv')\nsamp_subm = pd.read_csv(path+'sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2022-02-24T07:26:00.076418Z","iopub.execute_input":"2022-02-24T07:26:00.076708Z","iopub.status.idle":"2022-02-24T07:26:00.238433Z","shell.execute_reply.started":"2022-02-24T07:26:00.076670Z","shell.execute_reply":"2022-02-24T07:26:00.237677Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.individual_id.unique","metadata":{"execution":{"iopub.status.busy":"2022-02-24T07:26:00.240959Z","iopub.execute_input":"2022-02-24T07:26:00.241231Z","iopub.status.idle":"2022-02-24T07:26:00.254382Z","shell.execute_reply.started":"2022-02-24T07:26:00.241193Z","shell.execute_reply":"2022-02-24T07:26:00.253618Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data","metadata":{"execution":{"iopub.status.busy":"2022-02-24T07:26:00.256195Z","iopub.execute_input":"2022-02-24T07:26:00.256896Z","iopub.status.idle":"2022-02-24T07:26:00.272108Z","shell.execute_reply.started":"2022-02-24T07:26:00.256831Z","shell.execute_reply":"2022-02-24T07:26:00.271351Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"samp_subm.loc[0, 'predictions']","metadata":{"execution":{"iopub.status.busy":"2022-02-24T07:26:00.273409Z","iopub.execute_input":"2022-02-24T07:26:00.273661Z","iopub.status.idle":"2022-02-24T07:26:00.283227Z","shell.execute_reply.started":"2022-02-24T07:26:00.273628Z","shell.execute_reply":"2022-02-24T07:26:00.282531Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Number train samples:', len(train_data))\nprint('Number train images:', len(os.listdir(path+'train_images/')))\nprint('Number test images:', len(os.listdir(path+'test_images/')))","metadata":{"execution":{"iopub.status.busy":"2022-02-24T07:26:00.284263Z","iopub.execute_input":"2022-02-24T07:26:00.284460Z","iopub.status.idle":"2022-02-24T07:26:01.625040Z","shell.execute_reply.started":"2022-02-24T07:26:00.284437Z","shell.execute_reply":"2022-02-24T07:26:01.624166Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-24T07:26:01.626606Z","iopub.execute_input":"2022-02-24T07:26:01.627039Z","iopub.status.idle":"2022-02-24T07:26:01.638643Z","shell.execute_reply.started":"2022-02-24T07:26:01.626996Z","shell.execute_reply":"2022-02-24T07:26:01.637056Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data['species'].value_counts()\n","metadata":{"execution":{"iopub.status.busy":"2022-02-24T07:26:01.640130Z","iopub.execute_input":"2022-02-24T07:26:01.640660Z","iopub.status.idle":"2022-02-24T07:26:01.654850Z","shell.execute_reply.started":"2022-02-24T07:26:01.640609Z","shell.execute_reply":"2022-02-24T07:26:01.654154Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data['individual_id'].value_counts()\n","metadata":{"execution":{"iopub.status.busy":"2022-02-24T07:26:01.658156Z","iopub.execute_input":"2022-02-24T07:26:01.658342Z","iopub.status.idle":"2022-02-24T07:26:01.678180Z","shell.execute_reply.started":"2022-02-24T07:26:01.658318Z","shell.execute_reply":"2022-02-24T07:26:01.677348Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"row = 0\nfile = train_data.loc[row, 'image']\nspecies = train_data.loc[row, 'species']\n\nimg = cv2.imread(path+'train_images/'+file)\nprint('Shape:', img.shape)","metadata":{"execution":{"iopub.status.busy":"2022-02-24T07:26:01.679613Z","iopub.execute_input":"2022-02-24T07:26:01.679930Z","iopub.status.idle":"2022-02-24T07:26:01.725929Z","shell.execute_reply.started":"2022-02-24T07:26:01.679886Z","shell.execute_reply":"2022-02-24T07:26:01.725043Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img[1]","metadata":{"execution":{"iopub.status.busy":"2022-02-24T07:26:01.727389Z","iopub.execute_input":"2022-02-24T07:26:01.727666Z","iopub.status.idle":"2022-02-24T07:26:01.737107Z","shell.execute_reply.started":"2022-02-24T07:26:01.727628Z","shell.execute_reply":"2022-02-24T07:26:01.733999Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def image_preprocessing(image, image_size):\n    \"\"\" Image Preprocessing \"\"\"\n    \n    # Crop Image\n    mid_row = int(image.shape[0]/2)\n    mid_col = int(image.shape[1]/2)\n    if image.shape[0]>image.shape[1]:\n        image_cropped = image[mid_row-mid_col:mid_row+mid_col,\n                                   0:image.shape[1]]\n    else:\n        image_cropped = image[0:image.shape[0],\n                                   mid_col-mid_row:mid_col+mid_row]\n    \n    # Rescale Image\n    image_rescale = cv2.resize(image_cropped,\n                               dsize=(image_size, image_size))\n    return image_rescale\n\n\ndef plot_befor_after(image):\n    \"\"\" Compare original and prepared image \"\"\"\n    \n    fig, axs = plt.subplots(1, 2, figsize=(15, 10))\n    fig.subplots_adjust(hspace = .1, wspace=.1)\n    axs = axs.ravel()\n    # Plot Original Image\n    axs[0].imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))\n    axs[0].set_title('original shape: '+str(image.shape))\n    # Image Preprocessing\n    image_rescale = image_preprocessing(image, image_size)\n    # Plot Prepared Image\n    axs[1].imshow(cv2.cvtColor(image_rescale, cv2.COLOR_BGR2RGB))\n    axs[1].set_title('rescaled shape: '+str(image_rescale.shape))\n    for i in range(2):\n        axs[i].set_xticklabels([])\n        axs[i].set_yticklabels([])\n    plt.show()\nimage_size = 128\nrow = 2022\nfile = train_data.loc[row, 'image']\nspecies = train_data.loc[row, 'species']\nimage = cv2.imread(path+'train_images/'+file)\nprint('Shape:', image.shape)","metadata":{"execution":{"iopub.status.busy":"2022-02-24T07:26:01.738703Z","iopub.execute_input":"2022-02-24T07:26:01.738965Z","iopub.status.idle":"2022-02-24T07:26:01.801025Z","shell.execute_reply.started":"2022-02-24T07:26:01.738931Z","shell.execute_reply":"2022-02-24T07:26:01.800185Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.individual_id.unique()","metadata":{"execution":{"iopub.status.busy":"2022-02-24T07:26:01.802104Z","iopub.execute_input":"2022-02-24T07:26:01.802347Z","iopub.status.idle":"2022-02-24T07:26:01.815929Z","shell.execute_reply.started":"2022-02-24T07:26:01.802312Z","shell.execute_reply":"2022-02-24T07:26:01.814962Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.preprocessing import image\nfrom tensorflow.keras.applications.imagenet_utils import preprocess_input\n\nfrom tqdm.autonotebook import tqdm\ndef Loading_Images(data, m, dataset):\n    print(\"Loading images\")\n    X_train = np.zeros((m, 32, 32, 3))\n    count = 0\n    for fig in tqdm(data['image']):\n        if count>5:\n            img = image.load_img(\"../input/happy-whale-and-dolphin/\"+dataset+\"/\"+fig, target_size=(32, 32, 3))\n            x = image.img_to_array(img)\n            x = preprocess_input(x)\n            X_train[count] = x\n            count += 1\n\n    return X_train","metadata":{"execution":{"iopub.status.busy":"2022-02-24T07:26:01.817243Z","iopub.execute_input":"2022-02-24T07:26:01.817580Z","iopub.status.idle":"2022-02-24T07:26:01.881393Z","shell.execute_reply.started":"2022-02-24T07:26:01.817541Z","shell.execute_reply":"2022-02-24T07:26:01.880703Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loaded = Loading_Images(train_data, train_data.shape[0], \"train_images\")\n#X /= 255","metadata":{"execution":{"iopub.status.busy":"2022-02-24T07:26:01.882712Z","iopub.execute_input":"2022-02-24T07:26:01.883142Z","iopub.status.idle":"2022-02-24T07:26:01.957086Z","shell.execute_reply.started":"2022-02-24T07:26:01.883105Z","shell.execute_reply":"2022-02-24T07:26:01.956277Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loaded.shape","metadata":{"execution":{"iopub.status.busy":"2022-02-24T07:26:01.958436Z","iopub.execute_input":"2022-02-24T07:26:01.958699Z","iopub.status.idle":"2022-02-24T07:26:01.965024Z","shell.execute_reply.started":"2022-02-24T07:26:01.958664Z","shell.execute_reply":"2022-02-24T07:26:01.964186Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.shape","metadata":{"execution":{"iopub.status.busy":"2022-02-24T07:26:01.966368Z","iopub.execute_input":"2022-02-24T07:26:01.966986Z","iopub.status.idle":"2022-02-24T07:26:01.974219Z","shell.execute_reply.started":"2022-02-24T07:26:01.966947Z","shell.execute_reply":"2022-02-24T07:26:01.973350Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder\nfrom sklearn.preprocessing import OneHotEncoder\ndef prepare_labels(y):\n    values = np.array(y)\n    label_encoder = LabelEncoder()\n    integer_encoded = label_encoder.fit_transform(values)\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    y = onehot_encoded\n    return y, label_encoder","metadata":{"execution":{"iopub.status.busy":"2022-02-24T07:26:01.975813Z","iopub.execute_input":"2022-02-24T07:26:01.976322Z","iopub.status.idle":"2022-02-24T07:26:01.983688Z","shell.execute_reply.started":"2022-02-24T07:26:01.976283Z","shell.execute_reply":"2022-02-24T07:26:01.982863Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y, label_encoder = prepare_labels(train_data['individual_id'])\ny.shape","metadata":{"execution":{"iopub.status.busy":"2022-02-24T07:26:01.985544Z","iopub.execute_input":"2022-02-24T07:26:01.986153Z","iopub.status.idle":"2022-02-24T07:26:02.270528Z","shell.execute_reply.started":"2022-02-24T07:26:01.986116Z","shell.execute_reply":"2022-02-24T07:26:02.269509Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_befor_after(img[1])","metadata":{"execution":{"iopub.status.busy":"2022-02-24T07:26:02.271997Z","iopub.execute_input":"2022-02-24T07:26:02.272344Z","iopub.status.idle":"2022-02-24T07:26:02.565368Z","shell.execute_reply.started":"2022-02-24T07:26:02.272303Z","shell.execute_reply":"2022-02-24T07:26:02.564684Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nX =loaded/ 255","metadata":{"execution":{"iopub.status.busy":"2022-02-24T07:26:02.566623Z","iopub.execute_input":"2022-02-24T07:26:02.566883Z","iopub.status.idle":"2022-02-24T07:26:03.194669Z","shell.execute_reply.started":"2022-02-24T07:26:02.566837Z","shell.execute_reply":"2022-02-24T07:26:03.193849Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.save(\"X.npy\", X)\n\nnp.save(\"y.npy\", y)\n","metadata":{"execution":{"iopub.status.busy":"2022-02-24T07:33:14.344400Z","iopub.execute_input":"2022-02-24T07:33:14.345015Z","iopub.status.idle":"2022-02-24T07:33:38.283805Z","shell.execute_reply.started":"2022-02-24T07:33:14.344971Z","shell.execute_reply":"2022-02-24T07:33:38.281587Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train,X_test,y_train,y_test = train_test_split(X,y,test_size = 0.5,random_state = 1)\n# Normalize pixel values to be between 0 and 1\n","metadata":{"execution":{"iopub.status.busy":"2022-02-24T07:26:03.196107Z","iopub.execute_input":"2022-02-24T07:26:03.196351Z","iopub.status.idle":"2022-02-24T07:26:07.152919Z","shell.execute_reply.started":"2022-02-24T07:26:03.196316Z","shell.execute_reply":"2022-02-24T07:26:07.152056Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del X\ndel train_data\n#del yo","metadata":{"execution":{"iopub.status.busy":"2022-02-24T07:26:07.154444Z","iopub.execute_input":"2022-02-24T07:26:07.154725Z","iopub.status.idle":"2022-02-24T07:26:07.162539Z","shell.execute_reply.started":"2022-02-24T07:26:07.154687Z","shell.execute_reply":"2022-02-24T07:26:07.160568Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\n\nfrom tensorflow.keras import datasets, layers, models\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2022-02-24T07:26:07.164300Z","iopub.status.idle":"2022-02-24T07:26:07.165036Z","shell.execute_reply.started":"2022-02-24T07:26:07.164764Z","shell.execute_reply":"2022-02-24T07:26:07.164794Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"model = models.Sequential()\nmodel.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)))\nmodel.add(layers.MaxPooling2D((2, 2)))\nmodel.add(layers.Conv2D(64, (3, 3), activation='relu'))\nmodel.add(layers.MaxPooling2D((2, 2)))\nmodel.add(layers.Conv2D(64, (3, 3), activation='relu'))\"\"\"","metadata":{"execution":{"iopub.status.busy":"2022-02-24T07:26:07.167014Z","iopub.status.idle":"2022-02-24T07:26:07.167455Z","shell.execute_reply.started":"2022-02-24T07:26:07.167211Z","shell.execute_reply":"2022-02-24T07:26:07.167235Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"model.summary()\"\"\"","metadata":{"execution":{"iopub.status.busy":"2022-02-24T07:26:07.169297Z","iopub.status.idle":"2022-02-24T07:26:07.169872Z","shell.execute_reply.started":"2022-02-24T07:26:07.169619Z","shell.execute_reply":"2022-02-24T07:26:07.169646Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"model.add(layers.Flatten())\nmodel.add(layers.Dense(64, activation='relu'))\nmodel.add(layers.Dense(10))\"\"\"","metadata":{"execution":{"iopub.status.busy":"2022-02-24T07:26:07.171895Z","iopub.status.idle":"2022-02-24T07:26:07.172495Z","shell.execute_reply.started":"2022-02-24T07:26:07.172095Z","shell.execute_reply":"2022-02-24T07:26:07.172120Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"model.add(layers.Flatten())\nmodel.add(layers.Dense(64, activation='relu'))\nmodel.add(layers.Dense(10))\"\"\"","metadata":{"execution":{"iopub.status.busy":"2022-02-24T07:26:07.174066Z","iopub.status.idle":"2022-02-24T07:26:07.174837Z","shell.execute_reply.started":"2022-02-24T07:26:07.174324Z","shell.execute_reply":"2022-02-24T07:26:07.174349Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"model.compile(optimizer='adam',\n              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),\n              metrics=['accuracy'])\n\nhistory = model.fit(X_train, y_train, epochs=10, \n                    validation_data=(X_test, y_test))\"\"\"","metadata":{"execution":{"iopub.status.busy":"2022-02-24T07:26:07.176460Z","iopub.status.idle":"2022-02-24T07:26:07.177172Z","shell.execute_reply.started":"2022-02-24T07:26:07.176856Z","shell.execute_reply":"2022-02-24T07:26:07.176883Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import keras.backend as K\nfrom keras.models import Sequential\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 tensorflow.keras.callbacks import ModelCheckpoint, EarlyStopping, ReduceLROnPlateau\nfrom keras.models import Model\n\nmodel = Sequential()\n\nmodel.add(Conv2D(32, (6, 6), strides = (1, 1), input_shape = (32, 32, 3)))\nmodel.add(BatchNormalization(axis = 3))\nmodel.add(Activation('relu'))\n\nmodel.add(MaxPooling2D((2, 2)))\nmodel.add(Conv2D(64, (3, 3), strides = (1,1)))\nmodel.add(Activation('relu'))\nmodel.add(AveragePooling2D((3, 3)))\n\nmodel.add(Flatten())\nmodel.add(Dense(512, activation=\"relu\"))\nmodel.add(Dropout(0.85))\nmodel.add(Dense(712, activation=\"relu\"))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(y.shape[1], activation='sigmoid'))\n\nmodel.compile(loss='categorical_crossentropy', optimizer=\"adam\", metrics=['accuracy'])\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-02-24T07:26:07.178965Z","iopub.status.idle":"2022-02-24T07:26:07.179392Z","shell.execute_reply.started":"2022-02-24T07:26:07.179161Z","shell.execute_reply":"2022-02-24T07:26:07.179184Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(X_train, y_train, epochs=1, batch_size=128, verbose=1)\n","metadata":{"execution":{"iopub.status.busy":"2022-02-24T07:26:07.181345Z","iopub.status.idle":"2022-02-24T07:26:07.181885Z","shell.execute_reply.started":"2022-02-24T07:26:07.181595Z","shell.execute_reply":"2022-02-24T07:26:07.181620Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}