{"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":"## Import Libraries","metadata":{"id":"MW6l9tA8GPh4"}},{"cell_type":"markdown","source":"***Here, I tackle the problem as a Counting problem. I have also solved them as classification problem - refere Version 3 & earlier for that***","metadata":{}},{"cell_type":"code","source":"!pip -q install attrdict","metadata":{"execution":{"iopub.status.busy":"2021-08-27T08:44:48.405251Z","iopub.execute_input":"2021-08-27T08:44:48.406193Z","iopub.status.idle":"2021-08-27T08:44:57.754435Z","shell.execute_reply.started":"2021-08-27T08:44:48.406041Z","shell.execute_reply":"2021-08-27T08:44:57.753124Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nfrom glob import glob\nfrom itertools import chain\nimport cv2\nimport random\nfrom tqdm.notebook import tqdm\nimport time\n\nfrom attrdict import AttrDict\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom sklearn.preprocessing import label_binarize\nfrom sklearn.metrics import accuracy_score, mean_absolute_error, mean_squared_error, roc_auc_score\nfrom sklearn.model_selection import train_test_split\n\nimport tensorflow as tf\nimport tensorflow.keras.backend as K\nfrom tensorflow.keras.layers import Activation, Conv2D, Dense\nfrom tensorflow.keras.layers import Flatten, Input, Layer, MaxPooling2D\nfrom tensorflow.keras.models import Model","metadata":{"id":"iiSvkJJQGRDe","execution":{"iopub.status.busy":"2021-08-27T08:44:57.764924Z","iopub.execute_input":"2021-08-27T08:44:57.765656Z","iopub.status.idle":"2021-08-27T08:45:03.479620Z","shell.execute_reply.started":"2021-08-27T08:44:57.765618Z","shell.execute_reply":"2021-08-27T08:45:03.478489Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Constants","metadata":{"id":"Vklpx80PFn2x"}},{"cell_type":"code","source":"config = dict(SEED = 42,\n              IMG_HEIGHT = 250,\n              IMG_WIDTH = 500,\n              EPOCHS = 3,\n              BATCH_SIZE = 16,\n              MAX_HOURS = 80,\n              LR = 0.0001)\nconfig = AttrDict(config)","metadata":{"id":"ec1i3e4WFmW8","execution":{"iopub.status.busy":"2021-08-27T08:45:03.483820Z","iopub.execute_input":"2021-08-27T08:45:03.484154Z","iopub.status.idle":"2021-08-27T08:45:03.493102Z","shell.execute_reply.started":"2021-08-27T08:45:03.484123Z","shell.execute_reply":"2021-08-27T08:45:03.491821Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Utility Functions","metadata":{"id":"3YbNYza0FsaJ"}},{"cell_type":"code","source":"def seed_everything(seed):\n    np.random.seed(seed)\n    random.seed(seed)\n    tf.random.set_seed(seed)\n    os.environ[\"PYTHONHASHSEED\"] = str(seed)","metadata":{"id":"Ryk0IPFkFphy","execution":{"iopub.status.busy":"2021-08-27T08:45:03.497387Z","iopub.execute_input":"2021-08-27T08:45:03.497769Z","iopub.status.idle":"2021-08-27T08:45:03.506128Z","shell.execute_reply.started":"2021-08-27T08:45:03.497711Z","shell.execute_reply":"2021-08-27T08:45:03.504851Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_hist(hist):\n    fig, ax = plt.subplots(1, 2, figsize=(12, 6))\n    ax[0].plot(hist.history[\"loss\"])\n    ax[0].plot(hist.history[\"val_loss\"])\n    ax[0].set_title(\"Loss Plot\")\n    ax[0].set_ylabel(\"Loss\")\n    ax[0].set_xlabel(\"Epoch\")\n    ax[0].legend([\"Train\", \"Validation\"], loc=\"upper left\")\n\n    ax[1].plot(hist.history[\"accuracy\"])\n    ax[1].plot(hist.history[\"val_accuracy\"])\n    ax[1].set_title(\"Accuracy Plot\")\n    ax[1].set_ylabel(\"Accuracy\")\n    ax[1].set_xlabel(\"Epoch\")\n    ax[1].legend([\"Train\", \"Validation\"], loc=\"upper left\")    \n    plt.show()","metadata":{"id":"M3iF7dBvFpkt","execution":{"iopub.status.busy":"2021-08-27T08:45:03.509939Z","iopub.execute_input":"2021-08-27T08:45:03.510380Z","iopub.status.idle":"2021-08-27T08:45:03.522524Z","shell.execute_reply.started":"2021-08-27T08:45:03.510337Z","shell.execute_reply":"2021-08-27T08:45:03.521167Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_grid(dataset, h=3, w=3, title=\"\"):\n    f, ax = plt.subplots(h, w, figsize=(30, 30))\n    for images, labels in dataset.shuffle(100).take(1):\n        for i in range(h*w):\n            img = (images[i] * 255).numpy().astype(\"uint8\")\n            ax[i // h, i % w].imshow(img[:, :, 0])\n            ax[i // h, i % w].axis(\"off\")\n            ax[i // h, i % w].set_title(labels[i].numpy(), fontdict={\"fontsize\": 20})\n    plt.tight_layout()\n    f.suptitle(title, fontsize=\"large\", fontweight=\"extra bold\")\n    plt.show()","metadata":{"id":"isKRHpxlFmhQ","execution":{"iopub.status.busy":"2021-08-27T08:45:03.526791Z","iopub.execute_input":"2021-08-27T08:45:03.527253Z","iopub.status.idle":"2021-08-27T08:45:03.539437Z","shell.execute_reply.started":"2021-08-27T08:45:03.527221Z","shell.execute_reply":"2021-08-27T08:45:03.537993Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_roc_auc_score(y_true, y_pred):\n    classes = np.unique(np.array(y_true))\n    y_true = label_binarize(y_true, classes=classes)\n    y_pred = label_binarize(y_pred, classes=classes)\n    print(y_true.shape ,y_pred.shape)\n    return roc_auc_score(y_true, y_pred, average=\"macro\", multi_class=\"ovr\")","metadata":{"execution":{"iopub.status.busy":"2021-08-27T08:45:12.405946Z","iopub.execute_input":"2021-08-27T08:45:12.406780Z","iopub.status.idle":"2021-08-27T08:45:12.414230Z","shell.execute_reply.started":"2021-08-27T08:45:12.406732Z","shell.execute_reply":"2021-08-27T08:45:12.412846Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def print_metrics(stage, y_true, y_pred):\n    auc = get_roc_auc_score(y_true, y_pred)\n    acc = accuracy_score(y_true, y_pred)\n    mae = mean_absolute_error(y_true, y_pred)\n    mse = mean_squared_error(y_true, y_pred)\n    print(f\"{stage} ROC-AUC Score: {auc:.2f}\")\n    print(f\"{stage} Accuracy: {acc:.2f}\")\n    print(f\"{stage} Mean Absolute Error: {mae:.2f}\")\n    print(f\"{stage} Mean Squared Error: {mse:.2f}\")","metadata":{"execution":{"iopub.status.busy":"2021-08-27T08:45:12.416138Z","iopub.execute_input":"2021-08-27T08:45:12.416830Z","iopub.status.idle":"2021-08-27T08:45:12.425715Z","shell.execute_reply.started":"2021-08-27T08:45:12.416783Z","shell.execute_reply":"2021-08-27T08:45:12.424162Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Load Data","metadata":{"id":"N-RW4XhDWw0u"}},{"cell_type":"code","source":"train_files = sorted(glob(\"../input/count-the-blue-boxes/train/train/*/*\"))\ntest_files = sorted(glob(\"../input/count-the-blue-boxes/test/test/*\"))","metadata":{"id":"fahKxr2AGHDt","execution":{"iopub.status.busy":"2021-08-27T08:45:12.427934Z","iopub.execute_input":"2021-08-27T08:45:12.428553Z","iopub.status.idle":"2021-08-27T08:45:17.734743Z","shell.execute_reply.started":"2021-08-27T08:45:12.428494Z","shell.execute_reply":"2021-08-27T08:45:17.733605Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f\"# Train Images: {len(train_files)}\")\nprint(f\"# Test Images: {len(test_files)}\")","metadata":{"id":"WyOkWMTYGVDs","outputId":"99ea5c23-5a58-4593-c465-5a367da5e1bc","execution":{"iopub.status.busy":"2021-08-27T08:45:17.736426Z","iopub.execute_input":"2021-08-27T08:45:17.736895Z","iopub.status.idle":"2021-08-27T08:45:17.743923Z","shell.execute_reply.started":"2021-08-27T08:45:17.736852Z","shell.execute_reply":"2021-08-27T08:45:17.742675Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv(\"../input/count-the-blue-boxes/train.csv\")\ntrain_df[\"image_path\"] = train_df[\"count\"].astype(str) + \"_\" + train_df[\"image_id\"]\ntrain_df[\"image_path\"] = train_df[\"image_path\"].apply(lambda x: f\"../input/count-the-blue-boxes/train/train/{x.split('_')[0]}/{x}.png\")","metadata":{"id":"P3LgIReRGHGO","execution":{"iopub.status.busy":"2021-08-27T08:45:17.745825Z","iopub.execute_input":"2021-08-27T08:45:17.746668Z","iopub.status.idle":"2021-08-27T08:45:18.667685Z","shell.execute_reply.started":"2021-08-27T08:45:17.746620Z","shell.execute_reply":"2021-08-27T08:45:18.666589Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = train_df.loc[train_df[\"count\"] < config.MAX_HOURS]","metadata":{"id":"GcsrWIUXw-SV","execution":{"iopub.status.busy":"2021-08-27T08:45:18.669099Z","iopub.execute_input":"2021-08-27T08:45:18.669545Z","iopub.status.idle":"2021-08-27T08:45:18.716652Z","shell.execute_reply.started":"2021-08-27T08:45:18.669463Z","shell.execute_reply":"2021-08-27T08:45:18.715408Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.shape","metadata":{"id":"zo2fZG_WxFpL","outputId":"80c64f1c-dacf-4336-eafe-211c1e9322c2","execution":{"iopub.status.busy":"2021-08-27T08:45:18.718359Z","iopub.execute_input":"2021-08-27T08:45:18.719102Z","iopub.status.idle":"2021-08-27T08:45:18.730911Z","shell.execute_reply.started":"2021-08-27T08:45:18.718997Z","shell.execute_reply":"2021-08-27T08:45:18.729310Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"id":"88tlNTZJW_fw","outputId":"79ba6cc7-232b-4484-8ac6-04d7a4c1e766","execution":{"iopub.status.busy":"2021-08-27T08:45:18.733187Z","iopub.execute_input":"2021-08-27T08:45:18.733856Z","iopub.status.idle":"2021-08-27T08:45:18.755354Z","shell.execute_reply.started":"2021-08-27T08:45:18.733811Z","shell.execute_reply":"2021-08-27T08:45:18.754210Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df[\"count\"].value_counts().head(20)","metadata":{"id":"qyd7JtB6Wpdb","outputId":"6df79947-0b9c-45ae-bca9-b9606cf03509","execution":{"iopub.status.busy":"2021-08-27T08:45:18.756938Z","iopub.execute_input":"2021-08-27T08:45:18.757450Z","iopub.status.idle":"2021-08-27T08:45:18.770798Z","shell.execute_reply.started":"2021-08-27T08:45:18.757405Z","shell.execute_reply":"2021-08-27T08:45:18.769221Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df[\"count\"].value_counts().tail(20)","metadata":{"id":"Sj101gzLX3c0","outputId":"ddb28b38-832f-4206-ffc0-8ae62de8c918","execution":{"iopub.status.busy":"2021-08-27T08:45:18.773919Z","iopub.execute_input":"2021-08-27T08:45:18.774409Z","iopub.status.idle":"2021-08-27T08:45:18.787115Z","shell.execute_reply.started":"2021-08-27T08:45:18.774374Z","shell.execute_reply":"2021-08-27T08:45:18.785694Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Train - Validation Split","metadata":{"id":"MkFaKTOOYFdx"}},{"cell_type":"code","source":"X_train, X_valid, y_train, y_valid = train_test_split(train_df[\"image_path\"].values, train_df[\"count\"].values, \n                                                      stratify=train_df[\"count\"],\n                                                      random_state=config.SEED,\n                                                      test_size=0.2)","metadata":{"id":"c-coTjqEYE9X","execution":{"iopub.status.busy":"2021-08-27T08:45:18.937627Z","iopub.execute_input":"2021-08-27T08:45:18.938062Z","iopub.status.idle":"2021-08-27T08:45:19.099807Z","shell.execute_reply.started":"2021-08-27T08:45:18.938019Z","shell.execute_reply":"2021-08-27T08:45:19.098637Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f\"Training: {X_train.shape}, {y_train.shape}\")\nprint(f\"Validation: {X_valid.shape}, {y_valid.shape}\")","metadata":{"id":"vivCsM2WYE_0","outputId":"4c01ba43-59fa-466f-cefd-1f225d540dec","execution":{"iopub.status.busy":"2021-08-27T08:45:19.101637Z","iopub.execute_input":"2021-08-27T08:45:19.102108Z","iopub.status.idle":"2021-08-27T08:45:19.109654Z","shell.execute_reply.started":"2021-08-27T08:45:19.102040Z","shell.execute_reply":"2021-08-27T08:45:19.108134Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Create Tensorflow Dataset","metadata":{"id":"LAF482x2YdGx"}},{"cell_type":"code","source":"def encode_single_sample(img_path, label):\n    # 1. Read image\n    img = tf.io.read_file(img_path)\n    # 2. Decode and convert to grayscale\n    img = tf.io.decode_png(img, channels=3)\n    # 3. Convert to float32 in [0, 1] range\n    img = tf.image.convert_image_dtype(img, tf.float32)\n    # 4. Resize to the desired size\n    img = tf.image.resize(img, [config.IMG_HEIGHT, config.IMG_WIDTH])\n    return (img, label)","metadata":{"id":"0K000ALkYFEw","execution":{"iopub.status.busy":"2021-08-27T08:45:19.117155Z","iopub.execute_input":"2021-08-27T08:45:19.117747Z","iopub.status.idle":"2021-08-27T08:45:19.124421Z","shell.execute_reply.started":"2021-08-27T08:45:19.117700Z","shell.execute_reply":"2021-08-27T08:45:19.123127Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset = tf.data.Dataset.from_tensor_slices((X_train, y_train))\ntrain_dataset = (\n    train_dataset.map(\n        encode_single_sample, num_parallel_calls=tf.data.experimental.AUTOTUNE\n    )\n    .batch(config.BATCH_SIZE) # pads to the smallest per-batch size that fits all elements\n    .prefetch(buffer_size=tf.data.experimental.AUTOTUNE)\n)","metadata":{"id":"9ujGsWlbYFIk","execution":{"iopub.status.busy":"2021-08-27T08:45:19.126954Z","iopub.execute_input":"2021-08-27T08:45:19.127813Z","iopub.status.idle":"2021-08-27T08:45:20.159406Z","shell.execute_reply.started":"2021-08-27T08:45:19.127763Z","shell.execute_reply":"2021-08-27T08:45:20.158241Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_grid(train_dataset, title=\"Training Images\")","metadata":{"id":"o1720QYBX3e9","outputId":"6ffc4ff9-800e-4d49-b202-a1e971f36f4d","execution":{"iopub.status.busy":"2021-08-27T08:45:20.161041Z","iopub.execute_input":"2021-08-27T08:45:20.161471Z","iopub.status.idle":"2021-08-27T08:45:34.667424Z","shell.execute_reply.started":"2021-08-27T08:45:20.161429Z","shell.execute_reply":"2021-08-27T08:45:34.666232Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"validation_dataset = tf.data.Dataset.from_tensor_slices((X_valid, y_valid))\nvalidation_dataset = (\n    validation_dataset.map(\n        encode_single_sample, num_parallel_calls=tf.data.experimental.AUTOTUNE\n    )\n    .batch(config.BATCH_SIZE)\n    .prefetch(buffer_size=tf.data.experimental.AUTOTUNE)\n)","metadata":{"id":"6Xx_nVLMWp-s","execution":{"iopub.status.busy":"2021-08-27T08:45:34.668992Z","iopub.execute_input":"2021-08-27T08:45:34.669443Z","iopub.status.idle":"2021-08-27T08:45:34.704311Z","shell.execute_reply.started":"2021-08-27T08:45:34.669393Z","shell.execute_reply":"2021-08-27T08:45:34.703185Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_grid(validation_dataset, title=\"Validation Images\")","metadata":{"id":"cPty9jbPZJkF","outputId":"95b3cd87-6e42-4a73-d2e6-6e47c5b070ca","execution":{"iopub.status.busy":"2021-08-27T08:45:34.706015Z","iopub.execute_input":"2021-08-27T08:45:34.706652Z","iopub.status.idle":"2021-08-27T08:45:46.891551Z","shell.execute_reply.started":"2021-08-27T08:45:34.706603Z","shell.execute_reply":"2021-08-27T08:45:46.890342Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Simple CNN Model","metadata":{"id":"v3SmFjULZr4E"}},{"cell_type":"code","source":"def build_model():\n    # Inputs to the model\n    input_img = Input(\n        shape=(config.IMG_HEIGHT, config.IMG_WIDTH, 3), name=\"image\", dtype=\"float32\"\n    )\n\n    x = Conv2D(16, (5, 5), activation=\"relu\", padding=\"same\", name=\"conv_1\")(input_img)\n    x = MaxPooling2D((2, 2), name=\"pool_1\")(x)\n    x = Conv2D(32, (5, 5), activation=\"relu\", padding=\"same\", name=\"conv_2\")(x)\n    x = MaxPooling2D((2, 2), name=\"pool_2\")(x)\n    x = Conv2D(64, (5, 5), activation=\"relu\", padding=\"same\", name=\"conv_3\")(x)\n    x = MaxPooling2D((2, 2), name=\"pool_3\")(x)\n    x = Conv2D(128, (5, 5), activation=\"relu\", padding=\"same\", name=\"conv_4\")(x)\n    x = Flatten()(x)\n    x = Dense(512, activation=\"relu\", name=\"dense_1\")(x)\n    x = Dense(256, activation=\"relu\", name=\"dense_2\")(x)\n    output = Dense(len(set(y_train)), activation=\"softmax\", name=\"output\")(x)\n\n    model = Model(inputs=[input_img], outputs=output, name=\"count_model\")\n\n    opt = tf.keras.optimizers.Adam(learning_rate=config.LR)\n\n    model.compile(optimizer=opt, \n                  loss=tf.keras.losses.Poisson(), \n                  metrics=[\"accuracy\", tf.keras.metrics.RootMeanSquaredError(), \n                           tf.keras.metrics.MeanAbsoluteError(), \n                           tf.keras.metrics.MeanSquaredError()])\n    return model","metadata":{"id":"nH2dLJjjZqcv","execution":{"iopub.status.busy":"2021-08-27T08:45:46.895441Z","iopub.execute_input":"2021-08-27T08:45:46.895783Z","iopub.status.idle":"2021-08-27T08:45:46.910143Z","shell.execute_reply.started":"2021-08-27T08:45:46.895750Z","shell.execute_reply":"2021-08-27T08:45:46.908635Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Training","metadata":{"id":"rZ2Tf4Navd_b"}},{"cell_type":"code","source":"model = build_model()\nmodel.summary()","metadata":{"id":"akA0kNu3vccS","outputId":"d02a7b29-b01b-48f3-8b86-75dca4905cfb","execution":{"iopub.status.busy":"2021-08-27T08:45:46.912203Z","iopub.execute_input":"2021-08-27T08:45:46.912682Z","iopub.status.idle":"2021-08-27T08:45:47.113425Z","shell.execute_reply.started":"2021-08-27T08:45:46.912634Z","shell.execute_reply":"2021-08-27T08:45:47.112307Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tf.keras.utils.plot_model(model)","metadata":{"id":"6CLwgZCT5lcv","outputId":"380b833d-4f8c-42fa-86c8-5f32704db22b","execution":{"iopub.status.busy":"2021-08-27T08:45:47.116048Z","iopub.execute_input":"2021-08-27T08:45:47.116603Z","iopub.status.idle":"2021-08-27T08:45:47.352322Z","shell.execute_reply.started":"2021-08-27T08:45:47.116557Z","shell.execute_reply":"2021-08-27T08:45:47.351124Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"early_stopping_patience = 3\nearly_stopping = tf.keras.callbacks.EarlyStopping(\n    monitor=\"val_loss\", patience=early_stopping_patience, \n    restore_best_weights=True\n)\n\ncheckpoint_filepath = \"model_checkpoint\"\nmodel_checkpoint_callback = tf.keras.callbacks.ModelCheckpoint(\n    filepath=checkpoint_filepath,\n    save_weights_only=False,\n    monitor=\"val_loss\",\n    mode=\"min\",\n    save_best_only=True)","metadata":{"id":"zMw-LAJ6vcrP","execution":{"iopub.status.busy":"2021-08-27T08:45:47.354464Z","iopub.execute_input":"2021-08-27T08:45:47.354933Z","iopub.status.idle":"2021-08-27T08:45:47.362484Z","shell.execute_reply.started":"2021-08-27T08:45:47.354877Z","shell.execute_reply":"2021-08-27T08:45:47.360693Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(\n    train_dataset,\n    validation_data=validation_dataset,\n    epochs=config.EPOCHS,\n    callbacks=[early_stopping, model_checkpoint_callback]\n)","metadata":{"id":"Fi3L4qeKvczc","outputId":"886e9387-e4e9-49f9-e29f-2e3e41ab0f52","execution":{"iopub.status.busy":"2021-08-27T08:45:47.364600Z","iopub.execute_input":"2021-08-27T08:45:47.365183Z","iopub.status.idle":"2021-08-27T09:53:59.538525Z","shell.execute_reply.started":"2021-08-27T08:45:47.365093Z","shell.execute_reply":"2021-08-27T09:53:59.537420Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_hist(history)","metadata":{"id":"kLhwrCSFvc8r","execution":{"iopub.status.busy":"2021-08-27T09:53:59.542433Z","iopub.execute_input":"2021-08-27T09:53:59.542879Z","iopub.status.idle":"2021-08-27T09:53:59.935699Z","shell.execute_reply.started":"2021-08-27T09:53:59.542834Z","shell.execute_reply":"2021-08-27T09:53:59.934386Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Inference\n\nAlways load the best model","metadata":{"id":"-C5Y0AJdvyuS"}},{"cell_type":"code","source":"best_model = tf.keras.models.load_model(checkpoint_filepath)","metadata":{"execution":{"iopub.status.busy":"2021-08-27T09:53:59.937673Z","iopub.execute_input":"2021-08-27T09:53:59.938186Z","iopub.status.idle":"2021-08-27T09:54:04.763714Z","shell.execute_reply.started":"2021-08-27T09:53:59.938142Z","shell.execute_reply":"2021-08-27T09:54:04.762569Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Validation Data Evaluation**","metadata":{}},{"cell_type":"code","source":"valid_preds = best_model.predict(validation_dataset)","metadata":{"execution":{"iopub.status.busy":"2021-08-27T09:54:04.768458Z","iopub.execute_input":"2021-08-27T09:54:04.768766Z","iopub.status.idle":"2021-08-27T09:57:39.640342Z","shell.execute_reply.started":"2021-08-27T09:54:04.768736Z","shell.execute_reply":"2021-08-27T09:57:39.639119Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = np.argmax(valid_preds, axis=1)","metadata":{"execution":{"iopub.status.busy":"2021-08-27T09:57:39.642718Z","iopub.execute_input":"2021-08-27T09:57:39.643146Z","iopub.status.idle":"2021-08-27T09:57:39.653739Z","shell.execute_reply.started":"2021-08-27T09:57:39.643099Z","shell.execute_reply":"2021-08-27T09:57:39.652525Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print_metrics(\"Validation\", y_valid, y_pred)","metadata":{"execution":{"iopub.status.busy":"2021-08-27T09:57:39.655631Z","iopub.execute_input":"2021-08-27T09:57:39.656146Z","iopub.status.idle":"2021-08-27T09:57:40.091913Z","shell.execute_reply.started":"2021-08-27T09:57:39.656060Z","shell.execute_reply":"2021-08-27T09:57:40.090711Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Training Data Evaluation**","metadata":{}},{"cell_type":"code","source":"train_preds = best_model.predict(train_dataset)","metadata":{"execution":{"iopub.status.busy":"2021-08-27T09:57:40.093577Z","iopub.execute_input":"2021-08-27T09:57:40.094296Z","iopub.status.idle":"2021-08-27T10:13:01.492607Z","shell.execute_reply.started":"2021-08-27T09:57:40.094232Z","shell.execute_reply":"2021-08-27T10:13:01.491488Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = np.argmax(train_preds, axis=1)","metadata":{"execution":{"iopub.status.busy":"2021-08-27T10:13:01.494388Z","iopub.execute_input":"2021-08-27T10:13:01.494834Z","iopub.status.idle":"2021-08-27T10:13:01.518094Z","shell.execute_reply.started":"2021-08-27T10:13:01.494791Z","shell.execute_reply":"2021-08-27T10:13:01.516990Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print_metrics(\"Training\", y_train, y_pred)","metadata":{"execution":{"iopub.status.busy":"2021-08-27T10:13:01.519630Z","iopub.execute_input":"2021-08-27T10:13:01.520333Z","iopub.status.idle":"2021-08-27T10:13:03.388711Z","shell.execute_reply.started":"2021-08-27T10:13:01.520254Z","shell.execute_reply":"2021-08-27T10:13:03.387274Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Submission","metadata":{}},{"cell_type":"code","source":"submission = pd.read_csv(\"../input/count-the-blue-boxes/sample_submission.csv\")\nsubmission[\"image_path\"] = submission[\"image_id\"].apply(lambda x: f\"../input/count-the-blue-boxes/test/test/{x}.png\")","metadata":{"execution":{"iopub.status.busy":"2021-08-27T10:17:09.368518Z","iopub.execute_input":"2021-08-27T10:17:09.368922Z","iopub.status.idle":"2021-08-27T10:17:09.662355Z","shell.execute_reply.started":"2021-08-27T10:17:09.368890Z","shell.execute_reply":"2021-08-27T10:17:09.661146Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def encode_single_sample(img_path):\n    img = tf.io.read_file(img_path)\n    img = tf.io.decode_png(img, channels=3)\n    img = tf.image.convert_image_dtype(img, tf.float32)\n    img = tf.image.resize(img, [config.IMG_HEIGHT, config.IMG_WIDTH])\n    return img","metadata":{"execution":{"iopub.status.busy":"2021-08-27T10:17:09.752126Z","iopub.execute_input":"2021-08-27T10:17:09.752471Z","iopub.status.idle":"2021-08-27T10:17:09.759659Z","shell.execute_reply.started":"2021-08-27T10:17:09.752441Z","shell.execute_reply":"2021-08-27T10:17:09.758058Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dataset = tf.data.Dataset.from_tensor_slices(submission[\"image_path\"].tolist())\ntest_dataset = (\n    test_dataset.map(\n        encode_single_sample, num_parallel_calls=tf.data.experimental.AUTOTUNE\n    )\n    .batch(config.BATCH_SIZE)\n    .prefetch(buffer_size=tf.data.experimental.AUTOTUNE)\n)","metadata":{"execution":{"iopub.status.busy":"2021-08-27T10:17:09.993608Z","iopub.execute_input":"2021-08-27T10:17:09.993966Z","iopub.status.idle":"2021-08-27T10:17:10.536142Z","shell.execute_reply.started":"2021-08-27T10:17:09.993934Z","shell.execute_reply":"2021-08-27T10:17:10.534994Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_all_preds(dataset):\n    all_preds = []\n    for batch_images in tqdm(dataset):\n        preds = tf.math.argmax(best_model.predict(batch_images), axis=1)\n        all_preds.append(preds)\n    # flatten 2D list\n    all_preds = list(chain.from_iterable(all_preds))\n    return all_preds","metadata":{"execution":{"iopub.status.busy":"2021-08-27T10:17:10.986421Z","iopub.execute_input":"2021-08-27T10:17:10.986801Z","iopub.status.idle":"2021-08-27T10:17:10.994394Z","shell.execute_reply.started":"2021-08-27T10:17:10.986769Z","shell.execute_reply":"2021-08-27T10:17:10.992597Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_preds = get_all_preds(test_dataset)","metadata":{"execution":{"iopub.status.busy":"2021-08-27T10:17:11.568774Z","iopub.execute_input":"2021-08-27T10:17:11.569186Z","iopub.status.idle":"2021-08-27T10:38:33.214991Z","shell.execute_reply.started":"2021-08-27T10:17:11.569155Z","shell.execute_reply":"2021-08-27T10:38:33.213448Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission[\"count\"] = [x.numpy() for x in test_preds]\nsubmission = submission[[\"image_id\", \"count\"]]\nprint(submission.shape)\nsubmission.head()","metadata":{"execution":{"iopub.status.busy":"2021-08-27T10:38:33.219472Z","iopub.execute_input":"2021-08-27T10:38:33.220009Z","iopub.status.idle":"2021-08-27T10:38:44.125557Z","shell.execute_reply.started":"2021-08-27T10:38:33.219922Z","shell.execute_reply":"2021-08-27T10:38:44.124481Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission[\"count\"].describe()","metadata":{"execution":{"iopub.status.busy":"2021-08-27T10:38:44.127882Z","iopub.execute_input":"2021-08-27T10:38:44.128313Z","iopub.status.idle":"2021-08-27T10:38:44.149091Z","shell.execute_reply.started":"2021-08-27T10:38:44.128272Z","shell.execute_reply":"2021-08-27T10:38:44.147562Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2021-08-27T10:38:44.151583Z","iopub.execute_input":"2021-08-27T10:38:44.152070Z","iopub.status.idle":"2021-08-27T10:38:44.499700Z","shell.execute_reply.started":"2021-08-27T10:38:44.152028Z","shell.execute_reply":"2021-08-27T10:38:44.498532Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Random Plots","metadata":{}},{"cell_type":"code","source":"def plot_random(paths, labels, h=4, w=4, title=\"\"):\n    f, ax = plt.subplots(h, w, figsize=(30, 30))\n    for i in range(h*w):\n        img = cv2.imread(paths[i])\n        ax[i // h, i % w].imshow(img)\n        ax[i // h, i % w].axis(\"off\")\n        ax[i // h, i % w].set_title(labels[i], fontdict={\"fontsize\": 20})\n    plt.tight_layout()\n    f.suptitle(title, fontsize=\"large\", fontweight=\"extra bold\")\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2021-08-27T10:38:44.501601Z","iopub.execute_input":"2021-08-27T10:38:44.502110Z","iopub.status.idle":"2021-08-27T10:38:44.513168Z","shell.execute_reply.started":"2021-08-27T10:38:44.502036Z","shell.execute_reply":"2021-08-27T10:38:44.511803Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission[\"image_path\"] = submission[\"image_id\"].apply(lambda x: f\"../input/count-the-blue-boxes/test/test/{x}.png\")","metadata":{"execution":{"iopub.status.busy":"2021-08-27T10:38:44.514656Z","iopub.execute_input":"2021-08-27T10:38:44.515349Z","iopub.status.idle":"2021-08-27T10:38:44.582611Z","shell.execute_reply.started":"2021-08-27T10:38:44.515300Z","shell.execute_reply":"2021-08-27T10:38:44.581488Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample = submission.sample(30)\nplot_random(sample[\"image_path\"].tolist(), sample[\"count\"].tolist())","metadata":{"execution":{"iopub.status.busy":"2021-08-27T10:38:44.584952Z","iopub.execute_input":"2021-08-27T10:38:44.585811Z","iopub.status.idle":"2021-08-27T10:38:47.047327Z","shell.execute_reply.started":"2021-08-27T10:38:44.585753Z","shell.execute_reply":"2021-08-27T10:38:47.046249Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample = submission.sample(30)\nplot_random(sample[\"image_path\"].tolist(), sample[\"count\"].tolist())","metadata":{"execution":{"iopub.status.busy":"2021-08-27T10:38:47.050480Z","iopub.execute_input":"2021-08-27T10:38:47.050974Z","iopub.status.idle":"2021-08-27T10:38:49.223127Z","shell.execute_reply.started":"2021-08-27T10:38:47.050926Z","shell.execute_reply":"2021-08-27T10:38:49.221849Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample = submission.sample(30)\nplot_random(sample[\"image_path\"].tolist(), sample[\"count\"].tolist())","metadata":{"execution":{"iopub.status.busy":"2021-08-27T10:38:49.225146Z","iopub.execute_input":"2021-08-27T10:38:49.225828Z","iopub.status.idle":"2021-08-27T10:38:51.696377Z","shell.execute_reply.started":"2021-08-27T10:38:49.225775Z","shell.execute_reply":"2021-08-27T10:38:51.695199Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample = submission.sample(30)\nplot_random(sample[\"image_path\"].tolist(), sample[\"count\"].tolist())","metadata":{"execution":{"iopub.status.busy":"2021-08-27T10:38:51.698096Z","iopub.execute_input":"2021-08-27T10:38:51.698716Z","iopub.status.idle":"2021-08-27T10:38:54.190349Z","shell.execute_reply.started":"2021-08-27T10:38:51.698669Z","shell.execute_reply":"2021-08-27T10:38:54.189202Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample = submission.sample(30)\nplot_random(sample[\"image_path\"].tolist(), sample[\"count\"].tolist())","metadata":{"execution":{"iopub.status.busy":"2021-08-27T10:38:54.192031Z","iopub.execute_input":"2021-08-27T10:38:54.192706Z","iopub.status.idle":"2021-08-27T10:38:56.362800Z","shell.execute_reply.started":"2021-08-27T10:38:54.192646Z","shell.execute_reply":"2021-08-27T10:38:56.361661Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}