{"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":"markdown","source":"# Pretrained model with Keras ","metadata":{"papermill":{"duration":0.034962,"end_time":"2022-02-05T23:31:40.553165","exception":false,"start_time":"2022-02-05T23:31:40.518203","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"\n### Please if this kernel is useful, <font color='red'>please upvote !!</font>","metadata":{"papermill":{"duration":0.030674,"end_time":"2022-02-05T23:31:40.612874","exception":false,"start_time":"2022-02-05T23:31:40.5822","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"This kernel is based on: [cnn-with-keras-stater](https://www.kaggle.com/ammarnassanalhajali/cnn-with-keras-stater)\n\n","metadata":{}},{"cell_type":"markdown","source":"### Importing Libraries","metadata":{"papermill":{"duration":0.029876,"end_time":"2022-02-05T23:31:40.67392","exception":false,"start_time":"2022-02-05T23:31:40.644044","status":"completed"},"tags":[]}},{"cell_type":"code","source":"import numpy as np \nimport pandas as pd \nimport os\nimport gc\nimport sys\nimport matplotlib.pyplot as plt\nimport matplotlib.image as mplimg\nfrom matplotlib.pyplot import imshow\nfrom tqdm.autonotebook import tqdm\n\nfrom sklearn.preprocessing import LabelEncoder\nfrom sklearn.preprocessing import OneHotEncoder\n\nimport 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\nfrom keras.models import load_model\nimport tensorflow as tf\n\nimport warnings\nwarnings.filterwarnings(\"ignore\", category=DeprecationWarning)\n","metadata":{"_uuid":"0d9c73ad23e6c2eae3028255ee00c3254fe66401","papermill":{"duration":7.153222,"end_time":"2022-02-05T23:31:47.856874","exception":false,"start_time":"2022-02-05T23:31:40.703652","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-02-12T08:06:50.195008Z","iopub.execute_input":"2022-02-12T08:06:50.195788Z","iopub.status.idle":"2022-02-12T08:06:50.208192Z","shell.execute_reply.started":"2022-02-12T08:06:50.195729Z","shell.execute_reply":"2022-02-12T08:06:50.207051Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gpus = tf.config.experimental.list_physical_devices('GPU')\nfor gpu in gpus:\n    print(\"Name:\", gpu.name, \"  Type:\", gpu.device_type)\nfrom tensorflow.python.client import device_lib\n\ndevice_lib.list_local_devices()\n\nprint(tf.test.is_gpu_available())","metadata":{"execution":{"iopub.status.busy":"2022-02-12T08:06:52.427549Z","iopub.execute_input":"2022-02-12T08:06:52.427861Z","iopub.status.idle":"2022-02-12T08:06:52.450616Z","shell.execute_reply.started":"2022-02-12T08:06:52.427807Z","shell.execute_reply":"2022-02-12T08:06:52.449445Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv(\"../input/happy-whale-and-dolphin/train.csv\")\n#train_df=train_df.drop_duplicates(subset=['individual_id'],keep='last')\ntrain_df.head()\ntrain_df_small = train_df[:50]\nprint(train_df_small.image)","metadata":{"_uuid":"46a8839e13a14eb8d16ea6823de9927ea63d5001","papermill":{"duration":0.180775,"end_time":"2022-02-05T23:31:48.055908","exception":false,"start_time":"2022-02-05T23:31:47.875133","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-02-12T08:21:11.337785Z","iopub.execute_input":"2022-02-12T08:21:11.338279Z","iopub.status.idle":"2022-02-12T08:21:11.438516Z","shell.execute_reply.started":"2022-02-12T08:21:11.338241Z","shell.execute_reply":"2022-02-12T08:21:11.437439Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train_df.shape)\nprint(train_df_small.shape)","metadata":{"papermill":{"duration":0.043973,"end_time":"2022-02-05T23:31:48.130304","exception":false,"start_time":"2022-02-05T23:31:48.086331","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-02-12T02:50:58.633088Z","iopub.execute_input":"2022-02-12T02:50:58.633353Z","iopub.status.idle":"2022-02-12T02:50:58.640095Z","shell.execute_reply.started":"2022-02-12T02:50:58.633318Z","shell.execute_reply":"2022-02-12T02:50:58.639277Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = image.load_img('../input/happy-whale-and-dolphin/train_images/002618d6f63ebc.jpg')\nimg","metadata":{"execution":{"iopub.status.busy":"2022-02-12T02:50:58.642763Z","iopub.execute_input":"2022-02-12T02:50:58.643121Z","iopub.status.idle":"2022-02-12T02:50:59.340413Z","shell.execute_reply.started":"2022-02-12T02:50:58.643082Z","shell.execute_reply":"2022-02-12T02:50:59.339597Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = plt.imread('../input/happy-whale-and-dolphin/train_images/002618d6f63ebc.jpg')\nimg.shape","metadata":{"execution":{"iopub.status.busy":"2022-02-12T02:50:59.341624Z","iopub.execute_input":"2022-02-12T02:50:59.342027Z","iopub.status.idle":"2022-02-12T02:50:59.385346Z","shell.execute_reply.started":"2022-02-12T02:50:59.341991Z","shell.execute_reply":"2022-02-12T02:50:59.384732Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_images_list = os.listdir('../input/happy-whale-and-dolphin/train_images')\ntrain_images_list","metadata":{"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''\nif not os.path.exists('./kaggle/working/small'):\n    os.makedirs('./kaggle/working/small')\nimport shutil\nfor i in range(50):\n    shutil.copyfile('../input/happy-whale-and-dolphin/train_images/'+train_df_small.image[i],'./kaggle/working/small/'+train_df_small.image[i])\n'''","metadata":{"execution":{"iopub.status.busy":"2022-02-12T02:50:59.439852Z","iopub.execute_input":"2022-02-12T02:50:59.440396Z","iopub.status.idle":"2022-02-12T02:50:59.446128Z","shell.execute_reply.started":"2022-02-12T02:50:59.440349Z","shell.execute_reply":"2022-02-12T02:50:59.445401Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''\ntrain_images_list_small = os.listdir('./kaggle/working/small')\ntrain_images_list_small\n'''","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#for i in train_images_list_small:\n    #os.remove('./kaggle/working/small/'+i)","metadata":{"execution":{"iopub.status.busy":"2022-02-12T02:50:59.4601Z","iopub.execute_input":"2022-02-12T02:50:59.460795Z","iopub.status.idle":"2022-02-12T02:50:59.467714Z","shell.execute_reply.started":"2022-02-12T02:50:59.46076Z","shell.execute_reply":"2022-02-12T02:50:59.466764Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''\nmin_shape = 5000\nmax_shape = 5000\nlist0 = train_images_list[:10]\nfor i in list0:\n    img = plt.imread('../input/happy-whale-and-dolphin/train_images/'+i)\n    if img.shape[0]<min_shape: \n        min_shape = img.shape[0]\n    if img.shape[0]>max_shape: \n        max_shape = img.shape[0]\nprint(min_shape) # 59(from 0-5000)\nprint(max_shape) # 5277\n'''","metadata":{"execution":{"iopub.status.busy":"2022-02-12T02:50:59.469162Z","iopub.execute_input":"2022-02-12T02:50:59.469547Z","iopub.status.idle":"2022-02-12T02:50:59.478493Z","shell.execute_reply.started":"2022-02-12T02:50:59.469509Z","shell.execute_reply":"2022-02-12T02:50:59.477722Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Functions","metadata":{}},{"cell_type":"code","source":"def 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        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    return X_train\n\ndef prepare_labels(y):  # 先转成int编码，再转成one-hot\n    values = np.array(y)\n    label_encoder = LabelEncoder() # #获取一个LabelEncoder\n    integer_encoded = label_encoder.fit_transform(values)  #训练LabelEncoder,使用训练好的LabelEncoder对原数据进行编码\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":{"papermill":{"duration":0.050739,"end_time":"2022-02-05T23:31:48.21012","exception":false,"start_time":"2022-02-05T23:31:48.159381","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-02-12T08:21:19.967040Z","iopub.execute_input":"2022-02-12T08:21:19.967336Z","iopub.status.idle":"2022-02-12T08:21:19.976725Z","shell.execute_reply.started":"2022-02-12T08:21:19.967302Z","shell.execute_reply":"2022-02-12T08:21:19.975683Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = Loading_Images(train_df, train_df.shape[0], \"train_images\")\nX /= 255","metadata":{"_uuid":"4afe4128a0cd6859848c8a80686208082d647c39","papermill":{"duration":5274.644854,"end_time":"2022-02-06T00:59:42.885337","exception":false,"start_time":"2022-02-05T23:31:48.240483","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-02-12T08:21:24.486306Z","iopub.execute_input":"2022-02-12T08:21:24.486626Z","iopub.status.idle":"2022-02-12T08:21:30.588261Z","shell.execute_reply.started":"2022-02-12T08:21:24.486594Z","shell.execute_reply":"2022-02-12T08:21:30.586668Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y, label_encoder = prepare_labels(train_df['individual_id'])","metadata":{"_uuid":"675924f8863aef27cf90dc668e0a68cd609dfc1c","papermill":{"duration":0.353086,"end_time":"2022-02-06T00:59:43.257414","exception":false,"start_time":"2022-02-06T00:59:42.904328","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-02-12T03:53:46.478874Z","iopub.execute_input":"2022-02-12T03:53:46.479148Z","iopub.status.idle":"2022-02-12T03:53:46.77049Z","shell.execute_reply.started":"2022-02-12T03:53:46.479115Z","shell.execute_reply":"2022-02-12T03:53:46.769726Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(X.shape)\nprint(y.shape)\ngc.collect()","metadata":{"_uuid":"14d243b19023e830b636bea16679e13bc40deae6","papermill":{"duration":0.20682,"end_time":"2022-02-06T00:59:43.482592","exception":false,"start_time":"2022-02-06T00:59:43.275772","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-02-12T03:53:46.77198Z","iopub.execute_input":"2022-02-12T03:53:46.772221Z","iopub.status.idle":"2022-02-12T03:53:46.999256Z","shell.execute_reply.started":"2022-02-12T03:53:46.772188Z","shell.execute_reply":"2022-02-12T03:53:46.998548Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#导入\nfrom tensorflow.keras.applications import EfficientNetB0\n#使用\nbase_model = EfficientNetB0(\n               input_shape=(32,32,3), \n               weights=None,\n               include_top=False)\n\nlayer = base_model.output\nlayer = Dense(1024, activation='relu')(layer)\nlayer = Flatten()(layer)\npredictions = Dense(y.shape[1], activation='softmax')(layer)\n# 得到新的模型\nmodel = Model(inputs=base_model.input, outputs=predictions)\n\nmodel.compile(loss='categorical_crossentropy', optimizer=\"adam\", metrics=['accuracy'])\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-02-12T03:53:47.000405Z","iopub.execute_input":"2022-02-12T03:53:47.002028Z","iopub.status.idle":"2022-02-12T03:53:48.276228Z","shell.execute_reply.started":"2022-02-12T03:53:47.001988Z","shell.execute_reply":"2022-02-12T03:53:48.275552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(X, y, epochs=150, batch_size=128, verbose=1)\nmodel.save('./effb0_0.h5')","metadata":{"_kg_hide-output":true,"_uuid":"169f45e150c3a584e0f655a8eda523e0675da63a","papermill":{"duration":936.68661,"end_time":"2022-02-06T01:15:23.381149","exception":false,"start_time":"2022-02-06T00:59:46.694539","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-02-12T03:53:48.277243Z","iopub.execute_input":"2022-02-12T03:53:48.278003Z","iopub.status.idle":"2022-02-12T04:52:31.994424Z","shell.execute_reply.started":"2022-02-12T03:53:48.277964Z","shell.execute_reply":"2022-02-12T04:52:31.993225Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del X\ndel y\ngc.collect()","metadata":{"papermill":{"duration":6.478547,"end_time":"2022-02-06T01:15:35.684424","exception":false,"start_time":"2022-02-06T01:15:29.205877","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-02-12T04:52:32.002665Z","iopub.execute_input":"2022-02-12T04:52:32.005371Z","iopub.status.idle":"2022-02-12T04:52:32.86616Z","shell.execute_reply.started":"2022-02-12T04:52:32.005314Z","shell.execute_reply":"2022-02-12T04:52:32.865345Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Evaluation","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(15,5))\nplt.plot(history.history['accuracy'])\nplt.title('Model accuracy')\nplt.ylabel('Accuracy')\nplt.xlabel('Epoch')\nplt.show()","metadata":{"_uuid":"7bca48a1d0963cbf70685b75431435cef9499895","papermill":{"duration":5.5474,"end_time":"2022-02-06T01:15:46.890467","exception":false,"start_time":"2022-02-06T01:15:41.343067","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-02-12T04:52:32.869761Z","iopub.execute_input":"2022-02-12T04:52:32.870456Z","iopub.status.idle":"2022-02-12T04:52:33.107505Z","shell.execute_reply.started":"2022-02-12T04:52:32.870425Z","shell.execute_reply":"2022-02-12T04:52:33.106742Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15,5))\nplt.plot(history.history['loss'])\nplt.title('Model loss')\nplt.ylabel('loss')\nplt.xlabel('Epoch')\nplt.show()","metadata":{"papermill":{"duration":5.642791,"end_time":"2022-02-06T01:15:58.272765","exception":false,"start_time":"2022-02-06T01:15:52.629974","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-02-12T04:52:33.109136Z","iopub.execute_input":"2022-02-12T04:52:33.109652Z","iopub.status.idle":"2022-02-12T04:52:33.346627Z","shell.execute_reply.started":"2022-02-12T04:52:33.109609Z","shell.execute_reply":"2022-02-12T04:52:33.345862Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### inference","metadata":{}},{"cell_type":"code","source":"test = os.listdir(\"../input/happy-whale-and-dolphin/test_images\")\nprint(len(test))","metadata":{"_uuid":"debe961c93b72bef151d9aad3ca2cb500ee00aaa","papermill":{"duration":5.883234,"end_time":"2022-02-06T01:16:10.232707","exception":false,"start_time":"2022-02-06T01:16:04.349473","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-02-12T08:03:58.812577Z","iopub.execute_input":"2022-02-12T08:03:58.812925Z","iopub.status.idle":"2022-02-12T08:03:59.258162Z","shell.execute_reply.started":"2022-02-12T08:03:58.812891Z","shell.execute_reply":"2022-02-12T08:03:59.257079Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"col = ['image']\ntest_df = pd.DataFrame(test, columns=col)\ntest_df['predictions'] = ''\n#test_df=test_df.head(n=250)","metadata":{"_uuid":"72ed8198f519f7b1ae3efbc688933c78d8cdd0e4","papermill":{"duration":5.475832,"end_time":"2022-02-06T01:16:21.392852","exception":false,"start_time":"2022-02-06T01:16:15.91702","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-02-12T08:04:02.863139Z","iopub.execute_input":"2022-02-12T08:04:02.863635Z","iopub.status.idle":"2022-02-12T08:04:02.883333Z","shell.execute_reply.started":"2022-02-12T08:04:02.863596Z","shell.execute_reply":"2022-02-12T08:04:02.882298Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = load_model(r'../input/happywhaleanddolphin1/effb0_0.h5')","metadata":{"execution":{"iopub.status.busy":"2022-02-12T08:07:19.005000Z","iopub.execute_input":"2022-02-12T08:07:19.005302Z","iopub.status.idle":"2022-02-12T08:07:26.269367Z","shell.execute_reply.started":"2022-02-12T08:07:19.005262Z","shell.execute_reply":"2022-02-12T08:07:26.268329Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_size=5000\nbatch_start = 0\nbatch_end = batch_size\nL = len(test_df)\n\nwhile batch_start < L:\n    limit = min(batch_end, L)\n    test_df_batch = test_df.iloc[batch_start:limit]\n    print(type(test_df_batch))\n    X = Loading_Images(test_df_batch, test_df_batch.shape[0], \"test_images\")\n    X /= 255\n    predictions = model.predict(np.array(X), verbose=1)\n    for i, pred in enumerate(predictions):\n        p=pred.argsort()[-5:][::-1]\n        idx=-1\n        s=''\n        s1=''\n        s2=''\n        for x in p:\n            idx=idx+1\n            if pred[x]>0.5:\n                s1 = s1 + ' ' +  label_encoder.inverse_transform(p)[idx]\n            else:\n                s2 = s2 + ' ' + label_encoder.inverse_transform(p)[idx]\n        s= s1 + ' new_individual' + s2\n        s = s.strip(' ')\n        test_df.loc[ batch_start + i, 'predictions'] = s\n    batch_start += batch_size   \n    batch_end += batch_size\n    del X\n    del test_df_batch\n    del predictions\n    gc.collect()\n    ","metadata":{"papermill":{"duration":2924.164775,"end_time":"2022-02-06T02:05:11.208924","exception":false,"start_time":"2022-02-06T01:16:27.044149","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-02-12T08:07:46.654213Z","iopub.execute_input":"2022-02-12T08:07:46.655120Z","iopub.status.idle":"2022-02-12T08:16:13.144222Z","shell.execute_reply.started":"2022-02-12T08:07:46.655068Z","shell.execute_reply":"2022-02-12T08:16:13.142871Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.to_csv('submission.csv',index=False)\ntest_df.head()","metadata":{"papermill":{"duration":5.911723,"end_time":"2022-02-06T02:05:22.337319","exception":false,"start_time":"2022-02-06T02:05:16.425596","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-02-12T04:52:34.052634Z","iopub.status.idle":"2022-02-12T04:52:34.053357Z","shell.execute_reply.started":"2022-02-12T04:52:34.053069Z","shell.execute_reply":"2022-02-12T04:52:34.053105Z"},"trusted":true},"execution_count":null,"outputs":[]}]}