{"cells":[{"metadata":{"_uuid":"a403fc4c63bbfc64d1dd7711ebb144b9cc42172e","_cell_guid":"84b517f4-4ee3-4a9a-a996-aa541e478007","trusted":true},"cell_type":"code","source":"%matplotlib inline\nimport os\nimport matplotlib\nimport matplotlib.pyplot as plt\nfrom matplotlib.offsetbox import OffsetImage, AnnotationBbox\nimport cv2\nimport numpy as np\nfrom glob import glob\nimport matplotlib.cm as cm\nfrom sklearn.decomposition import PCA\nfrom sklearn.manifold import TSNE\nfrom sklearn.preprocessing import StandardScaler\nimport pandas as pd\nimport json","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"ec9973d81e0adc3af427e20ba9c41386bb5c821c","_cell_guid":"33af5e35-f402-4a52-882b-14502bbb6a21","trusted":true},"cell_type":"code","source":"BASE_DATA_FOLDER = \"../input/plant-seedlings-classification/\"\nTRAin_DATA_FOLDER = os.path.join(BASE_DATA_FOLDER, \"train\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train = pd.read_csv('../input/cassava-leaf-disease-classification/train.csv')\nimage_path = '../input/cassava-leaf-disease-classification/train_images'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train.head()\nlabel_to_disease = json.load(open('../input/cassava-leaf-disease-classification/label_num_to_disease_map.json'))\ntrain['disease'] = train.label.map(label_to_disease)\nlabel_to_disease","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"deb2e410e83cd59c716669237e8d6c75fee90470","_cell_guid":"379e7165-f94a-44d7-b899-c145c4912d55","trusted":true},"cell_type":"code","source":"def create_mask_for_plant(image):\n    image_hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)\n\n    sensitivity = 35\n    lower_hsv = np.array([60 - sensitivity, 100, 50])\n    upper_hsv = np.array([60 + sensitivity, 255, 255])\n\n    mask = cv2.inRange(image_hsv, lower_hsv, upper_hsv)\n    kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (11,11))\n    mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel)\n    \n    return mask\n\ndef segment_plant(image):\n    mask = create_mask_for_plant(image)\n    output = cv2.bitwise_and(image, image, mask = mask)\n    return output","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"2030bc9a30b290b42fbc4b91c8ef2b6d51cad0a2","_cell_guid":"023dbad4-0687-4b1c-8e87-e97c9c484b7a","trusted":true},"cell_type":"code","source":"def visualize_scatter_with_images(X_2d_data, images, figsize=(45,45), image_zoom=1):\n    fig, ax = plt.subplots(figsize=figsize)\n    artists = []\n    for xy, i in zip(X_2d_data, images):\n        x0, y0 = xy\n        img = OffsetImage(i, zoom=image_zoom)\n        ab = AnnotationBbox(img, (x0, y0), xycoords='data', frameon=False)\n        artists.append(ax.add_artist(ab))\n    ax.update_datalim(X_2d_data)\n    ax.autoscale()\n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"c8a5b9a2c8c8291fe5652b1187f05140fb48d0f5","_cell_guid":"621148a2-3e9b-4586-9c77-6a624de8f7fc","trusted":true},"cell_type":"code","source":"def visualize_scatter(data_2d, label_ids, figsize=(20,20)):\n    plt.figure(figsize=figsize)\n    plt.grid()\n    \n    nb_classes = len(np.unique(label_ids))\n    \n    for label_id in np.unique(label_ids):\n        plt.scatter(data_2d[np.where(label_ids == label_id), 0],\n                    data_2d[np.where(label_ids == label_id), 1],\n                    marker='o',\n                    color= plt.cm.Set1(label_id / float(nb_classes)),\n                    linewidth='1',\n                    alpha=0.8,\n                    label=id_to_label_dict[label_id])\n    plt.legend(loc='best')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"images = []\nfor image in os.listdir(\"../input/cassava-leaf-disease-classification/train_images/\"):\n    print(image)\n#     for image_path in ):\n    image = cv2.imread(os.path.join(image_path, image), cv2.IMREAD_COLOR)\n    print(image)\n    image = cv2.resize(image, (150, 150))\n    image = segment_plant(image)\n    image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)\n    image = cv2.resize(image, (45,45))\n\n    image = image.flatten()\n\n    images.append(image)\nimages = np.array(images)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"images_scaled = StandardScaler().fit_transform(images)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"images_scaled.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.imshow(np.reshape(images[734], (45,45)), cmap=\"gray\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"pca = PCA(n_components=180)\npca_result = pca.fit_transform(images_scaled)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"pca_result.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"tsne = TSNE(n_components=2, perplexity=40.0)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"tsne_result = tsne.fit_transform(pca_result)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"tsne_result_scaled = StandardScaler().fit_transform(tsne_result)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"lab = list(label_to_disease.values())\nlab","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"labels = np.array(lab)\nlabels","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"label_to_id_dict = {v:i for i,v in enumerate(np.unique(labels))}\nid_to_label_dict = {v: k for k, v in label_to_id_dict.items()}","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"label_ids = np.array(list([label_to_id_dict[x] for x in labels]))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"visualize_scatter(tsne_result_scaled, label_ids)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"visualize_scatter_with_images(tsne_result_scaled, images = [np.reshape(i, (45,45)) for i in images], image_zoom=0.7)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"78ac19e5325be5b1da00f80ab4b0c288d75514ea","_cell_guid":"faf7f925-9068-46a7-ba8a-dd2e90511969"},"cell_type":"markdown","source":"## 3D t-SNE with animation"},{"metadata":{"_uuid":"8c8191b165afe03daf229b27a0897fb3e91bfb60","_cell_guid":"29f895ab-39f1-41b3-bb12-523281bd7fce","trusted":true},"cell_type":"code","source":"tsne = TSNE(n_components=3)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# tsne_result = tsne.fit_transform(pca_result)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"tsne_result_scaled = StandardScaler().fit_transform(tsne_result)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"2505c5a74c249c31ccdceebc8a22fd2722a02332","_cell_guid":"0383af96-31ac-43a9-a62e-49a26e8382c4","trusted":true},"cell_type":"code","source":"from mpl_toolkits.mplot3d import Axes3D\nfrom matplotlib import animation","execution_count":null,"outputs":[]},{"metadata":{"scrolled":false,"_uuid":"22ee19dbc4193088f3f680fc6224ce1d8ae0b8bd","_cell_guid":"a0507963-6f74-4dad-9be1-b11ee7eea9c2","trusted":true},"cell_type":"code","source":"fig = plt.figure(figsize=(25,25))\nax = fig.add_subplot(111,projection='3d')\n\nplt.grid()\n    \nnb_classes = len(np.unique(label_ids))\n    \nfor label_id in np.unique(label_ids):\n    ax.scatter(tsne_result_scaled[np.where(label_ids == label_id), 0],\n                tsne_result_scaled[np.where(label_ids == label_id), 1],\n#                 tsne_result_scaled[np.where(label_ids == label_id), 2],\n                alpha=0.8,\n                color= plt.cm.Set1(label_id / float(nb_classes)),\n                marker='o',\n                label=id_to_label_dict[label_id])\nax.legend(loc='best')\nax.view_init(25, 45)\nax.set_xlim(-2.5, 2.5)\nax.set_ylim(-2.5, 2.5)\nax.set_zlim(-2.5, 2.5)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"anim = animation.FuncAnimation(fig, lambda frame_number: ax.view_init(30, 4 * frame_number), interval=75, frames=90)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"6b87688cea7ceeeff3e090ee39710f002acb672a","_cell_guid":"3a705239-a9fb-45b2-85ee-296b208f6313","trusted":true},"cell_type":"code","source":"plot_3d_animation_filename = 'animation.gif'\nanim.save(plot_3d_animation_filename, writer='imagemagick')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"a6cc883597bc50195f4364d39b6e05e51a9a30b3","_cell_guid":"db025d58-2b81-4501-88c3-1b3d3ad75e70","trusted":true},"cell_type":"code","source":"import io\nimport base64\nfrom IPython.display import HTML\n\nvideo = io.open(plot_3d_animation_filename, 'r+b').read()\n# HTML(data='''<img src=\"data:image/gif;base64,{0}\" type=\"gif\" />'''.format(base64.b64encode(video).decode('ascii')))","execution_count":null,"outputs":[]},{"metadata":{"collapsed":true,"_uuid":"b6bd4e6ee3711082e398ca5784ab2bdec1fef222","_cell_guid":"c87d0805-2b61-4a3d-bfad-0d0861dbe773"},"cell_type":"markdown","source":"With this last step we would get a neat little 3D scatter plot animation. I commented it, because the size of the gif can take up to 30MB and it slows down the whole notebook unfortunately. If you would like to try it out, just uncomment it and run the last cell. Or of course you can view it with any program because it is saved as `animation.gif` "}],"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}