{"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":"import json\nimport os\nimport pandas as pd\nimport numpy as np\nimport cv2","metadata":{"execution":{"iopub.status.busy":"2023-03-26T03:04:42.253954Z","iopub.execute_input":"2023-03-26T03:04:42.254390Z","iopub.status.idle":"2023-03-26T03:04:42.481696Z","shell.execute_reply.started":"2023-03-26T03:04:42.254358Z","shell.execute_reply":"2023-03-26T03:04:42.480642Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"annotation = '/kaggle/input/benetech-making-graphs-accessible/train/annotations/'\nimages_path = \"/kaggle/input/benetech-making-graphs-accessible/train/images/\"\nfilename = []\nfor i in os.listdir(annotation):\n    filename.append(i)","metadata":{"execution":{"iopub.status.busy":"2023-03-25T04:52:09.658520Z","iopub.execute_input":"2023-03-25T04:52:09.659474Z","iopub.status.idle":"2023-03-25T04:52:10.187130Z","shell.execute_reply.started":"2023-03-25T04:52:09.659433Z","shell.execute_reply":"2023-03-25T04:52:10.185990Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"arr = []\nfor i in filename:\n    tmp = []\n    path = annotation + i\n    \n    json_file = open(path, \"r\", encoding=\"utf-8\")\n    sh_json = json.load(json_file)\n    \n    tmp.append(sh_json['chart-type'])\n    tmp.append(images_path + i.split('.')[0]+ '.jpg')\n    \n    arr.append(tmp)\n    json_file.close()","metadata":{"execution":{"iopub.status.busy":"2023-03-25T05:09:17.400441Z","iopub.execute_input":"2023-03-25T05:09:17.401361Z","iopub.status.idle":"2023-03-25T05:16:07.241541Z","shell.execute_reply.started":"2023-03-25T05:09:17.401309Z","shell.execute_reply":"2023-03-25T05:16:07.240446Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.DataFrame(arr, columns=[\"chart-type\", \"fileName\"])","metadata":{"execution":{"iopub.status.busy":"2023-03-25T05:16:07.243973Z","iopub.execute_input":"2023-03-25T05:16:07.244698Z","iopub.status.idle":"2023-03-25T05:16:07.270902Z","shell.execute_reply.started":"2023-03-25T05:16:07.244658Z","shell.execute_reply":"2023-03-25T05:16:07.269823Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2023-03-25T05:25:10.527623Z","iopub.execute_input":"2023-03-25T05:25:10.528368Z","iopub.status.idle":"2023-03-25T05:25:10.550164Z","shell.execute_reply.started":"2023-03-25T05:25:10.528329Z","shell.execute_reply":"2023-03-25T05:25:10.549250Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['chart-type'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-03-25T05:25:10.927458Z","iopub.execute_input":"2023-03-25T05:25:10.927825Z","iopub.status.idle":"2023-03-25T05:25:10.949212Z","shell.execute_reply.started":"2023-03-25T05:25:10.927783Z","shell.execute_reply":"2023-03-25T05:25:10.948033Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# IMG_WIDTH=200\n# IMG_HEIGHT=200\n    \n# def create_dataset(df):\n#     img_data_array=[]\n#     class_name=[]\n    \n#     for ind in df.index:\n#         image_path = df['fileName'][ind]\n#         cls = df['chart-type'][ind]\n#         image = cv2.imread(image_path, cv2.COLOR_BGR2RGB)\n#         image = cv2.resize(image, (IMG_HEIGHT, IMG_WIDTH),interpolation = cv2.INTER_AREA)\n#         image = np.array(image)\n#         image = image.astype('float32')\n#         image /= 255 \n#         img_data_array.append(image)\n#         class_name.append(cls)\n#     return img_data_array, class_name\n","metadata":{"execution":{"iopub.status.busy":"2023-03-25T05:25:11.728851Z","iopub.execute_input":"2023-03-25T05:25:11.729836Z","iopub.status.idle":"2023-03-25T05:25:11.735438Z","shell.execute_reply.started":"2023-03-25T05:25:11.729786Z","shell.execute_reply":"2023-03-25T05:25:11.734278Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# img_data, class_name = create_dataset(df)","metadata":{"execution":{"iopub.status.busy":"2023-03-25T05:25:12.150783Z","iopub.execute_input":"2023-03-25T05:25:12.151706Z","iopub.status.idle":"2023-03-25T05:25:12.157609Z","shell.execute_reply.started":"2023-03-25T05:25:12.151657Z","shell.execute_reply":"2023-03-25T05:25:12.155788Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Work in progreess","metadata":{"execution":{"iopub.status.busy":"2023-03-25T05:25:12.507956Z","iopub.execute_input":"2023-03-25T05:25:12.508669Z","iopub.status.idle":"2023-03-25T05:25:12.513886Z","shell.execute_reply.started":"2023-03-25T05:25:12.508613Z","shell.execute_reply":"2023-03-25T05:25:12.512590Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir train","metadata":{"execution":{"iopub.status.busy":"2023-03-25T05:25:12.921606Z","iopub.execute_input":"2023-03-25T05:25:12.922904Z","iopub.status.idle":"2023-03-25T05:25:13.873901Z","shell.execute_reply.started":"2023-03-25T05:25:12.922850Z","shell.execute_reply":"2023-03-25T05:25:13.872562Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir train/line\n!mkdir train/vertical_bar\n!mkdir train/scatter\n!mkdir train/dot\n!mkdir train/horizontal_bar","metadata":{"execution":{"iopub.status.busy":"2023-03-25T05:25:14.981393Z","iopub.execute_input":"2023-03-25T05:25:14.981809Z","iopub.status.idle":"2023-03-25T05:25:19.718410Z","shell.execute_reply.started":"2023-03-25T05:25:14.981748Z","shell.execute_reply":"2023-03-25T05:25:19.717046Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!ls train","metadata":{"execution":{"iopub.status.busy":"2023-03-25T05:25:19.721187Z","iopub.execute_input":"2023-03-25T05:25:19.721593Z","iopub.status.idle":"2023-03-25T05:25:20.876122Z","shell.execute_reply.started":"2023-03-25T05:25:19.721551Z","shell.execute_reply":"2023-03-25T05:25:20.874935Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import shutil\n    \nfor i in range(0,df.shape[0]):\n    path = f\"train/{df['chart-type'][i]}\"\n    shutil.copy(df['fileName'][i], path)\n    \n\n# df.shape[0]\n# p = \"train\"\n# os.listdir(p)","metadata":{"execution":{"iopub.status.busy":"2023-03-25T05:25:20.879437Z","iopub.execute_input":"2023-03-25T05:25:20.879813Z","iopub.status.idle":"2023-03-25T05:32:30.016224Z","shell.execute_reply.started":"2023-03-25T05:25:20.879775Z","shell.execute_reply":"2023-03-25T05:32:30.014792Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.keras.utils import image_dataset_from_directory\nfrom tensorflow.keras.layers import Rescaling\nfrom tensorflow.keras.models import Sequential","metadata":{"execution":{"iopub.status.busy":"2023-03-25T05:38:46.308139Z","iopub.execute_input":"2023-03-25T05:38:46.309091Z","iopub.status.idle":"2023-03-25T05:38:56.006782Z","shell.execute_reply.started":"2023-03-25T05:38:46.309052Z","shell.execute_reply":"2023-03-25T05:38:56.005262Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_size = 32\nimg_height = 180\nimg_width = 180","metadata":{"execution":{"iopub.status.busy":"2023-03-25T05:38:56.008808Z","iopub.execute_input":"2023-03-25T05:38:56.009542Z","iopub.status.idle":"2023-03-25T05:38:56.014833Z","shell.execute_reply.started":"2023-03-25T05:38:56.009498Z","shell.execute_reply":"2023-03-25T05:38:56.013664Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_dir = \"train/\"\ntrain_ds = image_dataset_from_directory(\n    data_dir,\n    validation_split=0.2,\n    subset=\"training\",\n    seed=123,\n    image_size=(img_height, img_width),\n    batch_size=batch_size\n)","metadata":{"execution":{"iopub.status.busy":"2023-03-25T05:39:16.050377Z","iopub.execute_input":"2023-03-25T05:39:16.050972Z","iopub.status.idle":"2023-03-25T05:39:24.379403Z","shell.execute_reply.started":"2023-03-25T05:39:16.050933Z","shell.execute_reply":"2023-03-25T05:39:24.378289Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_ds = tf.keras.utils.image_dataset_from_directory(\n    data_dir,\n    validation_split=0.2,\n    subset=\"validation\",\n    seed=123,\n    image_size=(img_height, img_width),\n    batch_size=batch_size\n)","metadata":{"execution":{"iopub.status.busy":"2023-03-25T05:47:45.556285Z","iopub.execute_input":"2023-03-25T05:47:45.557403Z","iopub.status.idle":"2023-03-25T05:47:49.770682Z","shell.execute_reply.started":"2023-03-25T05:47:45.557354Z","shell.execute_reply":"2023-03-25T05:47:49.769724Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_names = train_ds.class_names\nprint(class_names)","metadata":{"execution":{"iopub.status.busy":"2023-03-25T05:47:49.772583Z","iopub.execute_input":"2023-03-25T05:47:49.772882Z","iopub.status.idle":"2023-03-25T05:47:49.780570Z","shell.execute_reply.started":"2023-03-25T05:47:49.772855Z","shell.execute_reply":"2023-03-25T05:47:49.779425Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del df\ndel arr","metadata":{"execution":{"iopub.status.busy":"2023-03-25T05:47:51.475026Z","iopub.execute_input":"2023-03-25T05:47:51.475738Z","iopub.status.idle":"2023-03-25T05:47:51.491644Z","shell.execute_reply.started":"2023-03-25T05:47:51.475698Z","shell.execute_reply":"2023-03-25T05:47:51.490491Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\nplt.figure(figsize=(10, 10))\nfor images, labels in train_ds.take(1):\n    for i in range(9):\n        ax = plt.subplot(3, 3, i + 1)\n        plt.imshow(images[i].numpy().astype(\"uint8\"))\n        plt.title(class_names[labels[i]])\n        plt.axis(\"off\")","metadata":{"execution":{"iopub.status.busy":"2023-03-25T05:47:51.662939Z","iopub.execute_input":"2023-03-25T05:47:51.663212Z","iopub.status.idle":"2023-03-25T05:47:52.676275Z","shell.execute_reply.started":"2023-03-25T05:47:51.663186Z","shell.execute_reply":"2023-03-25T05:47:52.674843Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for image_batch, labels_batch in train_ds:\n    print(image_batch.shape)\n    print(labels_batch.shape)\n    break","metadata":{"execution":{"iopub.status.busy":"2023-03-25T05:47:52.827265Z","iopub.execute_input":"2023-03-25T05:47:52.827578Z","iopub.status.idle":"2023-03-25T05:47:53.132594Z","shell.execute_reply.started":"2023-03-25T05:47:52.827548Z","shell.execute_reply":"2023-03-25T05:47:53.131454Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"AUTOTUNE = tf.data.AUTOTUNE\n\ntrain_ds = train_ds.cache().shuffle(1000).prefetch(buffer_size=AUTOTUNE)\nval_ds = val_ds.cache().prefetch(buffer_size=AUTOTUNE)","metadata":{"execution":{"iopub.status.busy":"2023-03-25T05:47:54.425293Z","iopub.execute_input":"2023-03-25T05:47:54.425884Z","iopub.status.idle":"2023-03-25T05:47:54.440282Z","shell.execute_reply.started":"2023-03-25T05:47:54.425846Z","shell.execute_reply":"2023-03-25T05:47:54.439313Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"normalization_layer = Rescaling(1./255)","metadata":{"execution":{"iopub.status.busy":"2023-03-25T05:47:55.476071Z","iopub.execute_input":"2023-03-25T05:47:55.476454Z","iopub.status.idle":"2023-03-25T05:47:55.484781Z","shell.execute_reply.started":"2023-03-25T05:47:55.476422Z","shell.execute_reply":"2023-03-25T05:47:55.483419Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"normalized_ds = train_ds.map(lambda x, y: (normalization_layer(x), y))","metadata":{"execution":{"iopub.status.busy":"2023-03-25T05:47:56.563485Z","iopub.execute_input":"2023-03-25T05:47:56.564201Z","iopub.status.idle":"2023-03-25T05:47:56.595620Z","shell.execute_reply.started":"2023-03-25T05:47:56.564162Z","shell.execute_reply":"2023-03-25T05:47:56.594676Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# image_batch, labels_batch = next(iter(normalized_ds))","metadata":{"execution":{"iopub.status.busy":"2023-03-25T04:50:37.777141Z","iopub.execute_input":"2023-03-25T04:50:37.778576Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# first_image = image_batch[0]\n# # Notice the pixel values are now in `[0,1]`.\n# print(np.min(first_image), np.max(first_image))","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# num_classes = len(class_names)\n\n# model = Sequential([\n#   layers.Rescaling(1./255, input_shape=(img_height, img_width, 3)),\n#   layers.Conv2D(16, 3, padding='same', activation='relu'),\n#   layers.MaxPooling2D(),\n#   layers.Conv2D(32, 3, padding='same', activation='relu'),\n#   layers.MaxPooling2D(),\n#   layers.Conv2D(64, 3, padding='same', activation='relu'),\n#   layers.MaxPooling2D(),\n#   layers.Flatten(),\n#   layers.Dense(128, activation='relu'),\n#   layers.Dense(num_classes)\n# ])","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# model.compile(\n#     optimizer='adam',\n#     loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),\n#     metrics=['accuracy']\n# )","metadata":{},"execution_count":null,"outputs":[]}]}