{"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":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport tensorflow as ts\nfrom PIL import Image\nfrom PIL import ImageOps\nimport numpy as np\nimport os\nimport json\n\nImageList = os.listdir('/kaggle/input/benetech-making-graphs-accessible/train/images')\ndir = '/kaggle/input/benetech-making-graphs-accessible/train/images/'\nimages = []\nfor i in ImageList:\n    a = Image.open(dir + i)\n    a = ImageOps.grayscale(a)\n    a = a.resize((36,36))\n    images.append(np.asarray(a))\n    \nimages[0].shape","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"(images[0])","metadata":{"execution":{"iopub.status.busy":"2023-04-21T00:37:46.401788Z","iopub.execute_input":"2023-04-21T00:37:46.402085Z","iopub.status.idle":"2023-04-21T00:37:46.409091Z","shell.execute_reply.started":"2023-04-21T00:37:46.402057Z","shell.execute_reply":"2023-04-21T00:37:46.407835Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load annotations data\ndir = '/kaggle/input/benetech-making-graphs-accessible/train/annotations/'\nannotations = []\nfor i in ImageList:\n    with open(dir + i.split('.')[0] + '.json', 'r') as f:\n        data = json.load(f)\n        chart_type = data['chart-type']\n        annotations.append(chart_type)\n","metadata":{"execution":{"iopub.status.busy":"2023-04-21T00:40:10.097715Z","iopub.execute_input":"2023-04-21T00:40:10.098585Z","iopub.status.idle":"2023-04-21T00:40:10.126568Z","shell.execute_reply.started":"2023-04-21T00:40:10.098547Z","shell.execute_reply":"2023-04-21T00:40:10.124994Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Preprocess data\nfrom sklearn.preprocessing import LabelEncoder\nimages = np.array(images)\nannotations = np.array(annotations)\nle = LabelEncoder()\nlabels = le.fit_transform(annotations)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-04-21T00:38:25.863968Z","iopub.execute_input":"2023-04-21T00:38:25.864324Z","iopub.status.idle":"2023-04-21T00:38:25.937799Z","shell.execute_reply.started":"2023-04-21T00:38:25.864294Z","shell.execute_reply":"2023-04-21T00:38:25.936648Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Split data into training and validation sets\nfrom sklearn.model_selection import train_test_split\n\nX_train, X_val, y_train, y_val = train_test_split(images, labels, test_size=0.2, random_state=42)\nX_train = X_train/255\nX_Val = X_val/255","metadata":{"execution":{"iopub.status.busy":"2023-04-21T00:38:25.939156Z","iopub.execute_input":"2023-04-21T00:38:25.939558Z","iopub.status.idle":"2023-04-21T00:38:25.970218Z","shell.execute_reply.started":"2023-04-21T00:38:25.939472Z","shell.execute_reply":"2023-04-21T00:38:25.969127Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = ts.keras.Sequential([\n    ts.keras.layers.Conv2D(64, 7, activation = 'relu', padding = 'same', input_shape = [36, 36, 1]),\n    ts.keras.layers.MaxPooling2D(2),\n    ts.keras.layers.Conv2D(128, 3, activation='relu', padding='same'),\n    ts.keras.layers.Conv2D(128, 3, activation='relu', padding='same'),\n    ts.keras.layers.MaxPooling2D(2),\n    ts.keras.layers.Conv2D(256, 3, activation='relu', padding='same'),\n    ts.keras.layers.Conv2D(256, 3, activation='relu', padding='same'),\n    ts.keras.layers.MaxPooling2D(2),\n    ts.keras.layers.Flatten(),\n    ts.keras.layers.Dense(128, activation = 'relu'),\n    ts.keras.layers.Dropout(0.5),\n    ts.keras.layers.Dense(64, activation = 'relu'),\n    ts.keras.layers.Dropout(0.5),\n    ts.keras.layers.Dense(5, activation = 'softmax'),\n])\nmodel.compile(optimizer='sgd', loss='sparse_categorical_crossentropy', metrics=['accuracy'])\nhistory = model.fit(X_train, y_train, batch_size = 32, epochs=10)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"_, accuracy = model.evaluate(X_val, y_val)\nprint('Validation accuracy: %.2f' % (accuracy*100))\n","metadata":{"execution":{"iopub.status.busy":"2023-04-21T00:38:25.971601Z","iopub.execute_input":"2023-04-21T00:38:25.972647Z","iopub.status.idle":"2023-04-21T00:38:26.579362Z","shell.execute_reply.started":"2023-04-21T00:38:25.972604Z","shell.execute_reply":"2023-04-21T00:38:26.576779Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Predict the class labels for the validation set\ny_pred = model.predict(X_val)\ny_pred_classes = np.argmax(y_pred, axis=1)\n\n# Find the misclassified images\nmisclassified_indices = np.where(y_pred_classes != y_val)[0]\n\n# Visualize the misclassified images\nimport matplotlib.pyplot as plt\n\nfig, axes = plt.subplots(nrows=5, ncols=5, figsize=(10,10))\n\nfor i, ax in enumerate(axes.flatten()):\n    if i < len(misclassified_indices):\n        img_index = misclassified_indices[i]\n        ax.imshow(X_val[img_index], cmap='gray')\n        ax.set_title(f\"True label: {le.inverse_transform([y_val[img_index]])}\\nPredicted label: {le.inverse_transform([y_pred_classes[img_index]])}\")\n    ax.axis('off')\n    \nplt.tight_layout()\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-04-21T00:38:26.580265Z","iopub.status.idle":"2023-04-21T00:38:26.580636Z","shell.execute_reply.started":"2023-04-21T00:38:26.580449Z","shell.execute_reply":"2023-04-21T00:38:26.580468Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19"},"execution_count":null,"outputs":[]}]}