{"cells":[{"metadata":{},"cell_type":"markdown","source":"As mentioned by [discussion](https://www.kaggle.com/c/cassava-disease/discussion/94114), pseudo-labels can be used as an option to help improve model performance. Here I generated a pseudo-label dataframe using the model trained in another [notebook](https://www.kaggle.com/electro/keras-baseline-0-88-quickstart-i-training-saving). The quality of pseudo-labels depends on the model used. My model's validation accuracy is 0.88. So feel free to replace it with your own model. And thanks to @anonamename for sharing [unzipped extra images](https://www.kaggle.com/anonamename/cassava-2019-compe-data) from [2019 Cassava Competition](https://www.kaggle.com/c/cassava-disease)."},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport os\nimport tensorflow as tf\nimport math\nfrom tqdm import tqdm\nimport matplotlib.pyplot as plt\nimport cv2\n\ndir_extra = \"../input/cassava-2019-compe-data/kaggle_upload/extraimages\"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true},"cell_type":"code","source":"from tensorflow import keras\nmodel = keras.models.load_model('../input/cassava-baseline-weights/best_weights.h5')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_images = os.listdir(dir_extra)\n\nN = len(test_images)\nBATCH_SIZE = 16\n\npseudo_df = pd.DataFrame(columns=['image_id','label','prob'])","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Batch prediction and appending dataframe."},{"metadata":{"trusted":true},"cell_type":"code","source":"def predict_on_batch(test_list, dir_extra=dir_extra, target_size=(380,380)):\n    input_batch=[]\n    for IMAGE_ID in test_list:\n        image = tf.keras.preprocessing.image.load_img(os.path.join(dir_extra,IMAGE_ID), \n                                                      grayscale=False, \n                                                      color_mode=\"rgb\", \n                                                      target_size=target_size, \n                                                      interpolation=\"nearest\")\n        input_arr = keras.preprocessing.image.img_to_array(image)\n        input_batch.append(input_arr)\n    return np.array(input_batch)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for batch_index in tqdm(range(math.ceil(N/BATCH_SIZE))):\n    if batch_index*BATCH_SIZE+BATCH_SIZE < N:\n        test_X = predict_on_batch(test_images[batch_index*BATCH_SIZE:batch_index*BATCH_SIZE+BATCH_SIZE])\n        prob = model.predict(test_X)\n        predictions = prob.argmax(axis = 1)\n        pseudo_batch = pd.DataFrame({'image_id':test_images[batch_index*BATCH_SIZE:batch_index*BATCH_SIZE+BATCH_SIZE],\n                    'label':list(predictions), 'prob':list(prob)})\n    else:\n        test_X = predict_on_batch(test_images[batch_index*BATCH_SIZE:])\n        prob = model.predict(test_X)\n        predictions = prob.argmax(axis = 1)\n        pseudo_batch = pd.DataFrame({'image_id':test_images[batch_index*BATCH_SIZE:],\n                    'label':list(predictions), 'prob':list(prob)})\n        \n    pseudo_df = pseudo_df.append(pseudo_batch, ignore_index=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"pseudo_df['label'] = pseudo_df['label'].astype('int64')\npseudo_df","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Choosing a threshold to select images you want to use."},{"metadata":{"trusted":true},"cell_type":"code","source":"def selected(row, threshold=0.95):\n    a = row['prob']\n    if np.amax(a) >= threshold:\n        row['selected'] = 1\n    else:\n        row['selected'] = 0\n    return row\n\npseudo_df = pseudo_df.apply(selected, axis='columns')\nselected_df = pseudo_df[(pseudo_df['selected']==1)]\nselected_df","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Check out the quality of pseudo-labels."},{"metadata":{"trusted":true},"cell_type":"code","source":"def show_image(image_ids, labels):\n    plt.figure(figsize=(16, 12))\n    \n    for ind, (image_id, label) in enumerate(zip(image_ids, labels)):\n        plt.subplot(3, 3, ind + 1)\n        image = cv2.imread(os.path.join(dir_extra,image_id))\n        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n\n        plt.imshow(image)\n        plt.title(f\"Class: {label}\", fontsize=12)\n        plt.axis(\"off\")\n    \n    plt.show()\n    \nimage_ids = selected_df[:9][\"image_id\"].values\nlabels = selected_df[:9][\"label\"].values\n\nshow_image(image_ids, labels)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"selected_df.to_csv('pseudo_label_95.csv', index = False)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Thanks for your time."}],"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":4,"nbformat_minor":4}