{"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":"# Classification with EfficientNetV2\n\n* Original Google Repo: https://github.com/google/automl/tree/master/efficientnetv2\n* Paper published 2021","metadata":{}},{"cell_type":"code","source":"import math, re, os\nimport numpy as np\nimport tensorflow as tf\nimport tensorflow_addons as tfa\nprint(tf.__version__)\nprint(tfa.__version__)\n\nfrom flowerclass_read_tf_ds import get_datasets\nimport tensorflow_hub as hub\nimport pandas as pd\nimport math\nimport plotly_express as px","metadata":{"execution":{"iopub.status.busy":"2022-03-05T02:01:50.669536Z","iopub.execute_input":"2022-03-05T02:01:50.669839Z","iopub.status.idle":"2022-03-05T02:01:59.978566Z","shell.execute_reply.started":"2022-03-05T02:01:50.669761Z","shell.execute_reply":"2022-03-05T02:01:59.977801Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tf.test.gpu_device_name()","metadata":{"execution":{"iopub.status.busy":"2022-03-05T02:01:59.980090Z","iopub.execute_input":"2022-03-05T02:01:59.980328Z","iopub.status.idle":"2022-03-05T02:02:01.896974Z","shell.execute_reply.started":"2022-03-05T02:01:59.980293Z","shell.execute_reply":"2022-03-05T02:02:01.896028Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# I. Data Loading\n\n* Choose 480x480 as model is fixed: https://tfhub.dev/google/imagenet/efficientnet_v2_imagenet21k_l/feature_vector/2","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19"}},{"cell_type":"code","source":"image_size = 224\nbatch_size = 64","metadata":{"execution":{"iopub.status.busy":"2022-03-05T02:02:01.898515Z","iopub.execute_input":"2022-03-05T02:02:01.899297Z","iopub.status.idle":"2022-03-05T02:02:01.905002Z","shell.execute_reply.started":"2022-03-05T02:02:01.899257Z","shell.execute_reply":"2022-03-05T02:02:01.903973Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#%%debug (50, 480)\nds_train, ds_valid, ds_test = get_datasets(BATCH_SIZE=batch_size, IMAGE_SIZE=(image_size, image_size), \n                                           RESIZE=None, tpu=False)","metadata":{"execution":{"iopub.status.busy":"2022-03-05T02:02:01.907447Z","iopub.execute_input":"2022-03-05T02:02:01.908380Z","iopub.status.idle":"2022-03-05T02:02:02.789865Z","shell.execute_reply.started":"2022-03-05T02:02:01.908339Z","shell.execute_reply":"2022-03-05T02:02:02.789035Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# II. Model Setup: EfficientNetV2","metadata":{}},{"cell_type":"code","source":"#effnet2_base = \"https://tfhub.dev/google/imagenet/efficientnet_v2_imagenet21k_l/feature_vector/2\"\n#effnet2_base = \"https://tfhub.dev/google/imagenet/efficientnet_v2_imagenet21k_m/feature_vector/2\"\neffnet2_base = \"https://tfhub.dev/google/imagenet/efficientnet_v2_imagenet21k_s/feature_vector/2\"","metadata":{"execution":{"iopub.status.busy":"2022-03-05T02:02:02.791316Z","iopub.execute_input":"2022-03-05T02:02:02.791605Z","iopub.status.idle":"2022-03-05T02:02:02.798635Z","shell.execute_reply.started":"2022-03-05T02:02:02.791565Z","shell.execute_reply":"2022-03-05T02:02:02.797684Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"hub.KerasLayer","metadata":{"execution":{"iopub.status.busy":"2022-03-05T02:02:02.800122Z","iopub.execute_input":"2022-03-05T02:02:02.800577Z","iopub.status.idle":"2022-03-05T02:02:02.809643Z","shell.execute_reply.started":"2022-03-05T02:02:02.800531Z","shell.execute_reply":"2022-03-05T02:02:02.808540Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"    \neffnet2_tfhub = tf.keras.Sequential([\n    # Explicitly define the input shape so the model can be properly\n    # loaded by the TFLiteConverter\n    tf.keras.layers.InputLayer(input_shape=(image_size, image_size,3)),\n    hub.KerasLayer(effnet2_base, trainable=False),\n    tf.keras.layers.Dropout(rate=0.2),\n    tf.keras.layers.Dense(104, activation='softmax')\n])\neffnet2_tfhub.build((None, image_size, image_size,3,)) #This is to be used for subclassed models, which do not know at instantiation time what their inputs look like.\n\n\neffnet2_tfhub.summary()","metadata":{"execution":{"iopub.status.busy":"2022-03-05T02:02:02.811403Z","iopub.execute_input":"2022-03-05T02:02:02.811619Z","iopub.status.idle":"2022-03-05T02:02:14.779425Z","shell.execute_reply.started":"2022-03-05T02:02:02.811593Z","shell.execute_reply":"2022-03-05T02:02:14.778672Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Notice large amounts of untrainable params as efficientnetv2 layers are frozen","metadata":{}},{"cell_type":"code","source":"effnet2_tfhub.layers","metadata":{"execution":{"iopub.status.busy":"2022-03-05T02:02:14.780562Z","iopub.execute_input":"2022-03-05T02:02:14.780800Z","iopub.status.idle":"2022-03-05T02:02:14.789969Z","shell.execute_reply.started":"2022-03-05T02:02:14.780762Z","shell.execute_reply":"2022-03-05T02:02:14.789157Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"layer = effnet2_tfhub.layers[0]\nprint(\"weights:\", len(layer.weights))\nprint(\"trainable_weights:\", len(layer.trainable_weights))\nprint(\"non_trainable_weights:\", len(layer.non_trainable_weights))","metadata":{"execution":{"iopub.status.busy":"2022-03-05T02:02:14.791557Z","iopub.execute_input":"2022-03-05T02:02:14.792117Z","iopub.status.idle":"2022-03-05T02:02:14.800381Z","shell.execute_reply.started":"2022-03-05T02:02:14.792078Z","shell.execute_reply":"2022-03-05T02:02:14.799496Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"layer.weights[0].shape\n","metadata":{"execution":{"iopub.status.busy":"2022-03-05T02:02:14.804319Z","iopub.execute_input":"2022-03-05T02:02:14.805174Z","iopub.status.idle":"2022-03-05T02:02:14.811288Z","shell.execute_reply.started":"2022-03-05T02:02:14.805136Z","shell.execute_reply":"2022-03-05T02:02:14.810482Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"layer.trainable","metadata":{"execution":{"iopub.status.busy":"2022-03-05T02:02:14.812896Z","iopub.execute_input":"2022-03-05T02:02:14.813470Z","iopub.status.idle":"2022-03-05T02:02:14.822221Z","shell.execute_reply.started":"2022-03-05T02:02:14.813428Z","shell.execute_reply":"2022-03-05T02:02:14.821350Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Why?","metadata":{}},{"cell_type":"markdown","source":"# III. Training","metadata":{}},{"cell_type":"markdown","source":"Keras Transfer Learning: https://keras.io/guides/transfer_learning/","metadata":{}},{"cell_type":"markdown","source":"# IIIa) Phase I: Train Top Layer (frozen layers)","metadata":{}},{"cell_type":"markdown","source":"### Optimize Training for Compute Infrastructure","metadata":{}},{"cell_type":"code","source":"effnet2_tfhub.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.001),\n              loss='categorical_crossentropy',\n              metrics=[tfa.metrics.F1Score(num_classes=104, average='macro'), tf.keras.metrics.CategoricalAccuracy(\n    name='categorical_accuracy', dtype=None)])","metadata":{"execution":{"iopub.status.busy":"2022-03-05T02:02:14.823423Z","iopub.execute_input":"2022-03-05T02:02:14.825122Z","iopub.status.idle":"2022-03-05T02:02:14.849723Z","shell.execute_reply.started":"2022-03-05T02:02:14.825089Z","shell.execute_reply":"2022-03-05T02:02:14.848883Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* batchsize:4 with 512 resized to 480px OOM\n\n* `effnet2L_tfhub.fit(ds_train, epochs=1, validation_data=ds_valid, batch_size=batch_size, steps_per_epoch=1)`\n\n#### EfficientNetV2 Large\n\n* try batchsize 4, 8, 16 and image size  224, 331 (without resizing for now)\n* bs/image size (no resize)\n    * 8/224: pass\n    * 16/224 pass\n    * 32/224 pass\n    * 64/224 pass\n    * 128/224 pass\n* try 331 (second largest size of images available) with efficientetV2 small\n    * 16/331: OOM\n    * 8/331: OOM\n\n* Test with optimal 480x480 input:\n    \n   * 8/448 (resized 480): OOM\n   * 8/224 (resized 480): OOM\n   * 2/224 (resized 480): OOM\n   > Resizing to the optimal 480x480 image size not possible with EfficientNetV2 Large due to OOM\n\n    \n    \n#### EfficientNetV2 Medium\n\n* Test with optimal 480x480 input:\n\n   * 2/224 (resized 480): OOM\n\n#### EfficientNetV2 Small\n\n* Test with optimal 384 x 384: OOM\n\n> All 3 model types, small, medium, large cannot be used with their optimal resolution.\n> \n\n\n","metadata":{}},{"cell_type":"code","source":"compute_steps_per_epoch = lambda x: int(math.ceil(1. * x / batch_size))\nsteps_per_epoch_tr = compute_steps_per_epoch(12753)\nsteps_per_epoch_val = compute_steps_per_epoch(3712)\nsteps_per_epoch_tr, steps_per_epoch_val","metadata":{"execution":{"iopub.status.busy":"2022-03-05T02:02:14.851190Z","iopub.execute_input":"2022-03-05T02:02:14.851674Z","iopub.status.idle":"2022-03-05T02:02:14.858825Z","shell.execute_reply.started":"2022-03-05T02:02:14.851637Z","shell.execute_reply":"2022-03-05T02:02:14.858061Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"callback_stopping = tf.keras.callbacks.EarlyStopping(\n    monitor='val_f1_score', min_delta=0, patience=5, verbose=1,\n    mode='max', baseline=None, restore_best_weights=False\n)\ncallback_model_checkpoint = tf.keras.callbacks.ModelCheckpoint(filepath=\"training/cp-{epoch:04d}.ckpt\",\n                                                 save_weights_only=True,\n                                                               monitor='val_f1_score',\n                                                 verbose=1,  mode='max', save_best_only=True)\n\nhistory = effnet2_tfhub.fit(ds_train, epochs=40, validation_data=ds_valid, \n                            batch_size=batch_size, \n                            steps_per_epoch= steps_per_epoch_tr,\n                            validation_steps=steps_per_epoch_val,\n                           callbacks=[callback_stopping, callback_model_checkpoint], shuffle=True)","metadata":{"execution":{"iopub.status.busy":"2022-03-05T02:02:14.860438Z","iopub.execute_input":"2022-03-05T02:02:14.860728Z","iopub.status.idle":"2022-03-05T02:02:38.236585Z","shell.execute_reply.started":"2022-03-05T02:02:14.860683Z","shell.execute_reply":"2022-03-05T02:02:38.235795Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"effnet2_tfhub.save('saved_model/my_model_phase1')","metadata":{"execution":{"iopub.status.busy":"2022-03-05T02:02:38.238423Z","iopub.execute_input":"2022-03-05T02:02:38.240036Z","iopub.status.idle":"2022-03-05T02:02:59.797214Z","shell.execute_reply.started":"2022-03-05T02:02:38.239987Z","shell.execute_reply":"2022-03-05T02:02:59.796426Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results_tr = pd.DataFrame.from_dict(history.history)\nresults_tr['epochs'] = results_tr.index + 1\nresults_tr.head()\n\nresults_to_plot = results_tr.melt(id_vars=\"epochs\")\nresults_to_plot.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-05T02:02:59.806625Z","iopub.execute_input":"2022-03-05T02:02:59.806871Z","iopub.status.idle":"2022-03-05T02:02:59.841645Z","shell.execute_reply.started":"2022-03-05T02:02:59.806844Z","shell.execute_reply":"2022-03-05T02:02:59.840967Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results_to_plot['variable'].unique()","metadata":{"execution":{"iopub.status.busy":"2022-03-05T02:02:59.843071Z","iopub.execute_input":"2022-03-05T02:02:59.843312Z","iopub.status.idle":"2022-03-05T02:02:59.850063Z","shell.execute_reply.started":"2022-03-05T02:02:59.843280Z","shell.execute_reply":"2022-03-05T02:02:59.849301Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"px.line(data_frame=results_to_plot[results_to_plot.variable.isin(['loss', 'val_loss'])],\n           x='epochs', y='value', color=\"variable\")","metadata":{"execution":{"iopub.status.busy":"2022-03-05T02:02:59.851672Z","iopub.execute_input":"2022-03-05T02:02:59.852374Z","iopub.status.idle":"2022-03-05T02:03:00.872387Z","shell.execute_reply.started":"2022-03-05T02:02:59.852336Z","shell.execute_reply":"2022-03-05T02:03:00.871624Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"px.line(data_frame=results_to_plot[results_to_plot.variable.isin(['f1_score', 'val_f1_score'])],\n           x='epochs', y='value', color=\"variable\")","metadata":{"execution":{"iopub.status.busy":"2022-03-05T02:03:00.873830Z","iopub.execute_input":"2022-03-05T02:03:00.874218Z","iopub.status.idle":"2022-03-05T02:03:01.907574Z","shell.execute_reply.started":"2022-03-05T02:03:00.874174Z","shell.execute_reply":"2022-03-05T02:03:01.906899Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"best_phase1_f1 = results_tr['val_f1_score'].max()\nbest_phase1_epoch = results_tr.loc[results_tr['val_f1_score'] == best_phase1_f1, 'epochs'].values[0]\n","metadata":{"execution":{"iopub.status.busy":"2022-03-05T02:45:47.572266Z","iopub.execute_input":"2022-03-05T02:45:47.572807Z","iopub.status.idle":"2022-03-05T02:45:47.578131Z","shell.execute_reply.started":"2022-03-05T02:45:47.572771Z","shell.execute_reply":"2022-03-05T02:45:47.577305Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"best_phase1_f1, best_phase1_epoch","metadata":{"execution":{"iopub.status.busy":"2022-03-05T02:45:48.236878Z","iopub.execute_input":"2022-03-05T02:45:48.237660Z","iopub.status.idle":"2022-03-05T02:45:48.243949Z","shell.execute_reply.started":"2022-03-05T02:45:48.237605Z","shell.execute_reply":"2022-03-05T02:45:48.243087Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## IIIb) Phase II: Unfreeze and FineTuning\n\nUnfreeze weights, try fine tuning whole network","metadata":{}},{"cell_type":"code","source":"effnet2_tfhub.trainable = True","metadata":{"execution":{"iopub.status.busy":"2022-03-05T02:03:01.908712Z","iopub.execute_input":"2022-03-05T02:03:01.909152Z","iopub.status.idle":"2022-03-05T02:03:01.914068Z","shell.execute_reply.started":"2022-03-05T02:03:01.909114Z","shell.execute_reply":"2022-03-05T02:03:01.913118Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"effnet2_tfhub.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=1e-5),\n              loss='categorical_crossentropy',\n              metrics=[tfa.metrics.F1Score(num_classes=104, average='macro'), tf.keras.metrics.CategoricalAccuracy(\n    name='categorical_accuracy', dtype=None)])","metadata":{"execution":{"iopub.status.busy":"2022-03-05T02:03:01.915563Z","iopub.execute_input":"2022-03-05T02:03:01.915831Z","iopub.status.idle":"2022-03-05T02:03:01.939615Z","shell.execute_reply.started":"2022-03-05T02:03:01.915796Z","shell.execute_reply":"2022-03-05T02:03:01.938905Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"callback_stopping = tf.keras.callbacks.EarlyStopping(\n    monitor='val_f1_score', min_delta=0, patience=5, verbose=1,\n    mode='max', baseline=None, restore_best_weights=False\n)\ncallback_model_checkpoint = tf.keras.callbacks.ModelCheckpoint(filepath=\"training2/cp-{epoch:04d}.ckpt\",\n                                                 save_weights_only=True,\n                                                               monitor='val_f1_score',\n                                                 verbose=1, mode='max', save_best_only=True)\n\nhistory = effnet2_tfhub.fit(ds_train, epochs=10, validation_data=ds_valid, \n                            batch_size=batch_size, \n                            steps_per_epoch=steps_per_epoch_tr,\n                            validation_steps=steps_per_epoch_val,\n                           callbacks=[callback_stopping, callback_model_checkpoint], shuffle=True)","metadata":{"execution":{"iopub.status.busy":"2022-03-05T02:03:01.942150Z","iopub.execute_input":"2022-03-05T02:03:01.942556Z","iopub.status.idle":"2022-03-05T02:03:30.556414Z","shell.execute_reply.started":"2022-03-05T02:03:01.942517Z","shell.execute_reply":"2022-03-05T02:03:30.555636Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"effnet2_tfhub.save('saved_model/my_model_phase2')","metadata":{"execution":{"iopub.status.busy":"2022-03-05T02:03:30.560984Z","iopub.execute_input":"2022-03-05T02:03:30.561390Z","iopub.status.idle":"2022-03-05T02:03:56.757794Z","shell.execute_reply.started":"2022-03-05T02:03:30.561349Z","shell.execute_reply":"2022-03-05T02:03:56.757042Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results_tr = pd.DataFrame.from_dict(history.history)\nresults_tr['epochs'] = results_tr.index + 1\nresults_tr.head()\n\nresults_to_plot = results_tr.melt(id_vars=\"epochs\")\nresults_to_plot.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-05T02:03:56.765338Z","iopub.execute_input":"2022-03-05T02:03:56.765556Z","iopub.status.idle":"2022-03-05T02:03:56.793272Z","shell.execute_reply.started":"2022-03-05T02:03:56.765531Z","shell.execute_reply":"2022-03-05T02:03:56.792573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"px.line(data_frame=results_to_plot[results_to_plot.variable.isin(['loss', 'val_loss'])],\n           x='epochs', y='value', color=\"variable\")","metadata":{"execution":{"iopub.status.busy":"2022-03-05T02:03:56.794784Z","iopub.execute_input":"2022-03-05T02:03:56.795058Z","iopub.status.idle":"2022-03-05T02:03:56.862933Z","shell.execute_reply.started":"2022-03-05T02:03:56.795022Z","shell.execute_reply":"2022-03-05T02:03:56.862283Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"px.line(data_frame=results_to_plot[results_to_plot.variable.isin(['f1_score', 'val_f1_score'])],\n           x='epochs', y='value', color=\"variable\")","metadata":{"execution":{"iopub.status.busy":"2022-03-05T02:03:56.864547Z","iopub.execute_input":"2022-03-05T02:03:56.864842Z","iopub.status.idle":"2022-03-05T02:03:56.932175Z","shell.execute_reply.started":"2022-03-05T02:03:56.864792Z","shell.execute_reply":"2022-03-05T02:03:56.931407Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Load best model, either phase 1 or 2","metadata":{}},{"cell_type":"code","source":"best_phase2_f1 = results_tr['val_f1_score'].max()\n\nif best_phase1_f1 > best_phase2_f1:\n    effnet2_tfhub.load_weights(\"training/\"+\"cp-\"+f\"{best_phase1_epoch}\".rjust(4, '0')+\".ckpt\")\n    print(f\"best phase 1: {best_phase1_f1}\")\nelse:\n    print(f\"best phase 2: {best_phase2_f1}\")\n\n","metadata":{"execution":{"iopub.status.busy":"2022-03-05T02:55:30.818295Z","iopub.execute_input":"2022-03-05T02:55:30.818768Z","iopub.status.idle":"2022-03-05T02:55:30.828058Z","shell.execute_reply.started":"2022-03-05T02:55:30.818731Z","shell.execute_reply":"2022-03-05T02:55:30.827063Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# IV. Submission\n\nid,label\na762df180,0\n24c5cf439,0\n7581e896d,0\neb4b03b29,0\netc.","metadata":{}},{"cell_type":"code","source":"test_pred = effnet2_tfhub.predict(ds_test, batch_size=batch_size)\n","metadata":{"execution":{"iopub.status.busy":"2022-03-05T02:06:46.185414Z","iopub.execute_input":"2022-03-05T02:06:46.185676Z","iopub.status.idle":"2022-03-05T02:07:00.987942Z","shell.execute_reply.started":"2022-03-05T02:06:46.185648Z","shell.execute_reply":"2022-03-05T02:07:00.987183Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_ids = []\nimg_preds = []\nfor imgs, idnum in ds_test:\n    img_preds.append(effnet2_tfhub.predict(imgs, batch_size=batch_size))\n    img_ids.append(idnum)","metadata":{"execution":{"iopub.status.busy":"2022-03-05T02:10:30.816252Z","iopub.execute_input":"2022-03-05T02:10:30.816522Z","iopub.status.idle":"2022-03-05T02:11:09.272562Z","shell.execute_reply.started":"2022-03-05T02:10:30.816495Z","shell.execute_reply":"2022-03-05T02:11:09.271738Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_ids = np.concatenate([img_id.numpy() for img_id in img_ids])\n","metadata":{"execution":{"iopub.status.busy":"2022-03-05T02:13:41.427742Z","iopub.execute_input":"2022-03-05T02:13:41.428007Z","iopub.status.idle":"2022-03-05T02:13:41.434574Z","shell.execute_reply.started":"2022-03-05T02:13:41.427965Z","shell.execute_reply":"2022-03-05T02:13:41.433880Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_preds = np.concatenate([img_pred.argmax(1) for img_pred in img_preds])","metadata":{"execution":{"iopub.status.busy":"2022-03-05T02:14:53.142376Z","iopub.execute_input":"2022-03-05T02:14:53.142828Z","iopub.status.idle":"2022-03-05T02:14:53.148838Z","shell.execute_reply.started":"2022-03-05T02:14:53.142790Z","shell.execute_reply":"2022-03-05T02:14:53.148059Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_ids.shape, img_preds.shape","metadata":{"execution":{"iopub.status.busy":"2022-03-05T02:15:01.566673Z","iopub.execute_input":"2022-03-05T02:15:01.567591Z","iopub.status.idle":"2022-03-05T02:15:01.573787Z","shell.execute_reply.started":"2022-03-05T02:15:01.567551Z","shell.execute_reply":"2022-03-05T02:15:01.572959Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame({\"id\": img_ids, \"label\": img_preds})\nsubmission['id'] = submission['id'].apply(lambda x: x.decode())","metadata":{"execution":{"iopub.status.busy":"2022-03-05T02:16:37.500496Z","iopub.execute_input":"2022-03-05T02:16:37.501396Z","iopub.status.idle":"2022-03-05T02:16:37.509624Z","shell.execute_reply.started":"2022-03-05T02:16:37.501348Z","shell.execute_reply":"2022-03-05T02:16:37.508864Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-05T02:16:47.101686Z","iopub.execute_input":"2022-03-05T02:16:47.101946Z","iopub.status.idle":"2022-03-05T02:16:47.110107Z","shell.execute_reply.started":"2022-03-05T02:16:47.101917Z","shell.execute_reply":"2022-03-05T02:16:47.109310Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.dtypes","metadata":{"execution":{"iopub.status.busy":"2022-03-05T02:16:49.990720Z","iopub.execute_input":"2022-03-05T02:16:49.991268Z","iopub.status.idle":"2022-03-05T02:16:49.997806Z","shell.execute_reply.started":"2022-03-05T02:16:49.991232Z","shell.execute_reply":"2022-03-05T02:16:49.997118Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2022-03-05T02:18:16.828774Z","iopub.execute_input":"2022-03-05T02:18:16.829061Z","iopub.status.idle":"2022-03-05T02:18:16.850219Z","shell.execute_reply.started":"2022-03-05T02:18:16.829027Z","shell.execute_reply":"2022-03-05T02:18:16.849521Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}