{"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":"# Simple Keras Training, only using the thumbnail images (Inference)\n\nPlease consider upvoting this notebook\n\nAnd the training notebook can be found here:  https://www.kaggle.com/code/pjmathematician/ubco-keras-cnn-baseline-thumbnails-training","metadata":{}},{"cell_type":"code","source":"import os\n\nimport pandas as pd\nimport numpy as np\nfrom tqdm.auto import tqdm\nimport matplotlib.pyplot as plt\n\n\nfrom skimage import io\nfrom skimage.color import rgb2gray\nfrom skimage.transform import rescale, resize, downscale_local_mean\n\n\nfrom sklearn.preprocessing import LabelEncoder, OneHotEncoder\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import accuracy_score\n\nimport matplotlib.pyplot as plt\n\n\nfrom tensorflow.keras.models import Sequential, load_model\nfrom tensorflow.keras.layers import Dense, Conv2D, Flatten, Dropout\nfrom keras.utils import to_categorical","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-10-07T11:18:55.981898Z","iopub.execute_input":"2023-10-07T11:18:55.982278Z","iopub.status.idle":"2023-10-07T11:19:06.052031Z","shell.execute_reply.started":"2023-10-07T11:18:55.982252Z","shell.execute_reply":"2023-10-07T11:19:06.051069Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class CFG:\n    TRAIN_THUMBNAILS_PATH = \"/kaggle/input/UBC-OCEAN/train_thumbnails\"\n    TRAIN_FULL_PATH = \"/kaggle/input/UBC-OCEAN/train_images\"\n    TEST_THUMBNAILS_PATH = \"/kaggle/input/UBC-OCEAN/test_thumbnails\"\n    TEST_FULL_PATH = \"/kaggle/input/UBC-OCEAN/test_images\"","metadata":{"execution":{"iopub.status.busy":"2023-10-07T11:19:06.053935Z","iopub.execute_input":"2023-10-07T11:19:06.054616Z","iopub.status.idle":"2023-10-07T11:19:06.059134Z","shell.execute_reply.started":"2023-10-07T11:19:06.054584Z","shell.execute_reply":"2023-10-07T11:19:06.058246Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/UBC-OCEAN/train.csv')\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2023-10-07T11:19:07.751871Z","iopub.execute_input":"2023-10-07T11:19:07.752186Z","iopub.status.idle":"2023-10-07T11:19:07.766402Z","shell.execute_reply.started":"2023-10-07T11:19:07.752158Z","shell.execute_reply":"2023-10-07T11:19:07.76518Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classes = train['label'].unique().tolist()\nclasses.append(\"Other\")\nclasses","metadata":{"execution":{"iopub.status.busy":"2023-10-07T11:19:09.321374Z","iopub.execute_input":"2023-10-07T11:19:09.32203Z","iopub.status.idle":"2023-10-07T11:19:09.332416Z","shell.execute_reply.started":"2023-10-07T11:19:09.321999Z","shell.execute_reply":"2023-10-07T11:19:09.331451Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"proba = [0.4,0.2,0.1,0.075,0.075,0.15]","metadata":{"execution":{"iopub.status.busy":"2023-10-07T11:19:10.873453Z","iopub.execute_input":"2023-10-07T11:19:10.87383Z","iopub.status.idle":"2023-10-07T11:19:10.879117Z","shell.execute_reply.started":"2023-10-07T11:19:10.873795Z","shell.execute_reply":"2023-10-07T11:19:10.87785Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"encoder = OneHotEncoder(sparse_output = False)\nencoder.fit(train[train['is_tma']!=True]['label'].values.reshape(-1,1), )","metadata":{"execution":{"iopub.status.busy":"2023-10-07T11:20:41.667789Z","iopub.execute_input":"2023-10-07T11:20:41.668117Z","iopub.status.idle":"2023-10-07T11:20:41.682132Z","shell.execute_reply.started":"2023-10-07T11:20:41.66809Z","shell.execute_reply":"2023-10-07T11:20:41.68118Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = load_model(\"/kaggle/input/keras-cnn-baseline-thumbnails/model.h5\")","metadata":{"execution":{"iopub.status.busy":"2023-10-07T11:21:03.428134Z","iopub.execute_input":"2023-10-07T11:21:03.428454Z","iopub.status.idle":"2023-10-07T11:21:11.999337Z","shell.execute_reply.started":"2023-10-07T11:21:03.42843Z","shell.execute_reply":"2023-10-07T11:21:11.998379Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = pd.read_csv(\"/kaggle/input/UBC-OCEAN/test.csv\")\ntest.head()","metadata":{"execution":{"iopub.status.busy":"2023-10-07T11:21:14.876421Z","iopub.execute_input":"2023-10-07T11:21:14.877084Z","iopub.status.idle":"2023-10-07T11:21:14.890107Z","shell.execute_reply.started":"2023-10-07T11:21:14.877055Z","shell.execute_reply":"2023-10-07T11:21:14.889065Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.random.seed(69)\nthumbnails = os.listdir(CFG.TEST_THUMBNAILS_PATH)\npreds = []\nfor (i, row) in tqdm(test.iterrows()):\n    if str(row['image_id'])+\"_thumbnail.png\" not in thumbnails:\n        print(row['id'])\n        preds.append(np.random.choice(classes, p = proba))\n    img = io.imread(os.path.join(CFG.TEST_THUMBNAILS_PATH, str(row['image_id'])+\"_thumbnail.png\"))\n    img = rgb2gray(img)\n    img = resize(img, (128,128), anti_aliasing=False)\n    img = img.reshape(128,128,1)\n    prediction = encoder.inverse_transform(model.predict(np.array([img])))[0][0]\n    preds.append(prediction)","metadata":{"execution":{"iopub.status.busy":"2023-10-07T11:21:50.337952Z","iopub.execute_input":"2023-10-07T11:21:50.33828Z","iopub.status.idle":"2023-10-07T11:21:50.664433Z","shell.execute_reply.started":"2023-10-07T11:21:50.338252Z","shell.execute_reply":"2023-10-07T11:21:50.663592Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission = pd.read_csv(\"/kaggle/input/UBC-OCEAN/sample_submission.csv\")\nsample_submission['label'] = preds\nsample_submission","metadata":{"execution":{"iopub.status.busy":"2023-10-07T11:21:53.970176Z","iopub.execute_input":"2023-10-07T11:21:53.970543Z","iopub.status.idle":"2023-10-07T11:21:53.982521Z","shell.execute_reply.started":"2023-10-07T11:21:53.97049Z","shell.execute_reply":"2023-10-07T11:21:53.981556Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission.to_csv('submission.csv', index = False)","metadata":{"execution":{"iopub.status.busy":"2023-10-07T11:21:56.016164Z","iopub.execute_input":"2023-10-07T11:21:56.016478Z","iopub.status.idle":"2023-10-07T11:21:56.022269Z","shell.execute_reply.started":"2023-10-07T11:21:56.016453Z","shell.execute_reply":"2023-10-07T11:21:56.021388Z"},"trusted":true},"execution_count":null,"outputs":[]}]}