{"cells":[{"metadata":{},"cell_type":"markdown","source":"This notebook visualizes the efficientnet features of the Cassava Leaf Disease Classification competition data in 2d using the t-distributed Stochastic Neighbor Embedding algorithm, aka TSNE. The efficient features are extracted separately in [this](https://www.kaggle.com/tolgadincer/cldc-efficientnet-features) notebook as the inference time with the efficientnet is relatively long.\n\nThe 5 classes given in the data are not well separated from each other based on the TSNE analysis. However, bare in mind that this result is model-dependent."},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import os\nimport json\nimport numpy as np\nimport pandas as pd\n\nimport cv2\nfrom tqdm.notebook import tqdm\nimport imageio\n\nimport tensorflow as tf\nif tf.test.is_gpu_available():\n    from cuml.manifold import TSNE\n\nimport matplotlib.pyplot as plt\nplt.rcParams['figure.dpi'] = 200","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"BASE_DIR = '../input/cassava-leaf-disease-classification'\nTRAIN_IMAGES_FOLDER = os.path.join(BASE_DIR, 'train_images')\nTEST_IMAGES_FOLDER = os.path.join(BASE_DIR, 'test_images')\n\ndf = pd.read_csv(os.path.join(BASE_DIR, 'train.csv'))\ndf.image_id = BASE_DIR + '/train_images/' + df.image_id\n\nwith open(os.path.join(BASE_DIR, \"label_num_to_disease_map.json\")) as file:\n    map_classes = json.loads(file.read())\n    map_classes = {int(k) : v for k, v in map_classes.items()}\nfor keys in map_classes.keys():\n    map_classes[keys] = map_classes[keys].replace('Cassava ', '')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"features = np.load('../input/cldc-efficientnet-features/features.npy')\n\ntsne = TSNE(n_components=2, perplexity=30, n_neighbors=120, n_iter=5000, random_state=42, learning_rate=50)\nfeatures_hat = tsne.fit_transform(features)\nnp.save('features_hat.npy', features_hat)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"fig, ax = plt.subplots(1,5, figsize=(14, 3), sharex=True, sharey=True)\nfor i in range(5):\n    ax[i].scatter(features_hat[:, 0][df.label==i],\n                  features_hat[:, 1][df.label==i],\n                  c=df.label[df.label==i], s=0.8)\n    ax[i].set_xlabel('F0')\n    if i ==0:\n        ax[i].set_ylabel('F1')\n    ax[i].set_title('Label-%d\\n%s' % (i, map_classes[i]))\n\nplt.tight_layout()\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"fig, ax = plt.subplots(1,1, figsize=(4, 4), sharex=True, sharey=True)\nax.scatter(features_hat[:, 0],#[df.label!=3],\n           features_hat[:, 1],#[df.label!=3],\n           c=df.label, s=0.05) #[df.label!=3]\nax.set_xlabel('F0')\nax.set_ylabel('F1')\n\nplt.tight_layout()\nplt.set_cmap('Set1')\nplt.show()","execution_count":null,"outputs":[]}],"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}