{"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)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\n\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-04-28T14:32:50.353959Z","iopub.execute_input":"2023-04-28T14:32:50.354367Z","iopub.status.idle":"2023-04-28T14:32:50.360512Z","shell.execute_reply.started":"2023-04-28T14:32:50.354332Z","shell.execute_reply":"2023-04-28T14:32:50.359073Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#1. Read image in (Pillow \"PIL\")\n\n#2. Convert into a black and white photo (image.convert('mode'))\n\n#3. Resize the image so all the same size. LOOP THROUGH\n\n#4. Save to numpy array. Takes about 5 min to run through images.\n\n\n# Import the necessary libraries\nfrom PIL import Image, ImageOps\nimport numpy as np\nimport os\nfrom numpy import asarray\n\ntrain_x = []\n\ndir = '/kaggle/input/benetech-making-graphs-accessible/train/images/'\n\nimage_names = os.listdir('/kaggle/input/benetech-making-graphs-accessible/train/images/')\nimage_names.sort() #at the recommendation of COL Cummiskey\n\n\n#Michael Lenart Assisted through tutoring sesssion with this code.\nfor i in image_names:\n    img = Image.open(dir + i)\n    img2 = ImageOps.grayscale(img)\n    new_image = img2.resize((28, 28))\n    numpydata = np.asarray(new_image)\n    #print(numpydata)\n    train_x.append(numpydata)\n\n\nprint(np.array(train_x).shape)\n\ntrain_x = np.array(train_x)\nprint('train_z shape:', np.array(train_x).shape)\n\n","metadata":{"execution":{"iopub.status.busy":"2023-04-28T14:32:54.191300Z","iopub.execute_input":"2023-04-28T14:32:54.191714Z","iopub.status.idle":"2023-04-28T14:41:07.802593Z","shell.execute_reply.started":"2023-04-28T14:32:54.191678Z","shell.execute_reply":"2023-04-28T14:41:07.801067Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(type(train_x))","metadata":{"execution":{"iopub.status.busy":"2023-04-28T14:42:52.504204Z","iopub.execute_input":"2023-04-28T14:42:52.504630Z","iopub.status.idle":"2023-04-28T14:42:52.510966Z","shell.execute_reply.started":"2023-04-28T14:42:52.504596Z","shell.execute_reply":"2023-04-28T14:42:52.509501Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nimport json\ntrain_y = []\n\nmy = os.listdir('/kaggle/input/benetech-making-graphs-accessible/train/annotations/')\n\nmy.sort()\n\nfor image in my:\n    images = open(\"/kaggle/input/benetech-making-graphs-accessible/train/annotations/\" + image)\n    loaded = json.load(images)\n    type_chart = loaded['chart-type']\n    train_y.append(type_chart)\n#this is how to get the chart type from the data\n","metadata":{"execution":{"iopub.status.busy":"2023-04-28T14:42:55.231830Z","iopub.execute_input":"2023-04-28T14:42:55.232423Z","iopub.status.idle":"2023-04-28T14:49:18.230258Z","shell.execute_reply.started":"2023-04-28T14:42:55.232381Z","shell.execute_reply":"2023-04-28T14:49:18.229104Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(train_y))","metadata":{"execution":{"iopub.status.busy":"2023-04-28T14:50:56.044069Z","iopub.execute_input":"2023-04-28T14:50:56.044485Z","iopub.status.idle":"2023-04-28T14:50:56.050603Z","shell.execute_reply.started":"2023-04-28T14:50:56.044449Z","shell.execute_reply":"2023-04-28T14:50:56.049355Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"my_labels = list(train_y)\nencoded_list = []\n\nfor i in my_labels:\n    if i == 'line':\n        encoded = 1\n    elif i == 'scatter':\n        encoded = 2\n    elif i == 'dot':\n        encoded = 3\n    elif i == 'vertical_bar':\n        encoded = 4\n    encoded_list.append(encoded)\n\nencoded_list\ntrain_y = np.array(encoded_list)\n\nprint(len(train_y))","metadata":{"execution":{"iopub.status.busy":"2023-04-28T14:50:59.509504Z","iopub.execute_input":"2023-04-28T14:50:59.510459Z","iopub.status.idle":"2023-04-28T14:50:59.545313Z","shell.execute_reply.started":"2023-04-28T14:50:59.510410Z","shell.execute_reply":"2023-04-28T14:50:59.543939Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(np.unique(train_y))","metadata":{"execution":{"iopub.status.busy":"2023-04-28T14:51:04.143337Z","iopub.execute_input":"2023-04-28T14:51:04.143892Z","iopub.status.idle":"2023-04-28T14:51:04.152977Z","shell.execute_reply.started":"2023-04-28T14:51:04.143846Z","shell.execute_reply":"2023-04-28T14:51:04.151835Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nimport numpy as np\n\ntrain_x =  train_x/255\nencoded_list_tup = tuple(encoded_list)\n\n\n#loss function a number\n#if the loss function NA, problem! Haven't scaled variables. Take the\n#loss and improve the weights. Improved by increasing the number of neurons int\n#the layers\n\n#80%\nsplit = 43616\nx_test = train_x[split:]\ny_test = train_y[split:]\n\nx_train = train_x[:split]\ny_train = train_y[:split]\n","metadata":{"execution":{"iopub.status.busy":"2023-04-28T14:51:07.490384Z","iopub.execute_input":"2023-04-28T14:51:07.490769Z","iopub.status.idle":"2023-04-28T14:51:18.048705Z","shell.execute_reply.started":"2023-04-28T14:51:07.490736Z","shell.execute_reply":"2023-04-28T14:51:18.046772Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Sigmoid Model\nimport tensorflow as tf\nmodel = tf.keras.Sequential([\n        tf.keras.layers.Flatten(input_shape = [28, 28]),\n        tf.keras.layers.Dense(300, activation = 'sigmoid'),\n        tf.keras.layers.Dense(100, activation = 'sigmoid'),\n        tf.keras.layers.Dense(5, activation = 'softmax')\n    \n    ])","metadata":{"execution":{"iopub.status.busy":"2023-04-28T14:51:22.581482Z","iopub.execute_input":"2023-04-28T14:51:22.582320Z","iopub.status.idle":"2023-04-28T14:51:22.815260Z","shell.execute_reply.started":"2023-04-28T14:51:22.582256Z","shell.execute_reply":"2023-04-28T14:51:22.813766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-04-28T14:51:28.988306Z","iopub.execute_input":"2023-04-28T14:51:28.988731Z","iopub.status.idle":"2023-04-28T14:51:29.014584Z","shell.execute_reply.started":"2023-04-28T14:51:28.988691Z","shell.execute_reply":"2023-04-28T14:51:29.013300Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss = 'sparse_categorical_crossentropy',\n             optimizer = 'sgd',\n             metrics = ['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-04-28T14:53:41.818459Z","iopub.execute_input":"2023-04-28T14:53:41.818876Z","iopub.status.idle":"2023-04-28T14:53:41.844035Z","shell.execute_reply.started":"2023-04-28T14:53:41.818837Z","shell.execute_reply":"2023-04-28T14:53:41.842477Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history2 = model.fit(x_train,y_train, epochs = 30, validation_data = (x_test,y_test))","metadata":{"execution":{"iopub.status.busy":"2023-04-28T14:53:49.589399Z","iopub.execute_input":"2023-04-28T14:53:49.589821Z","iopub.status.idle":"2023-04-28T14:56:57.421110Z","shell.execute_reply.started":"2023-04-28T14:53:49.589782Z","shell.execute_reply":"2023-04-28T14:56:57.419851Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport pandas as pd\n\npd.DataFrame(history2.history).plot(ylim = [0,1])","metadata":{"execution":{"iopub.status.busy":"2023-04-28T14:57:06.198408Z","iopub.execute_input":"2023-04-28T14:57:06.198800Z","iopub.status.idle":"2023-04-28T14:57:06.569662Z","shell.execute_reply.started":"2023-04-28T14:57:06.198765Z","shell.execute_reply":"2023-04-28T14:57:06.568291Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Relu Model\nimport tensorflow as tf\nmodel2 = tf.keras.Sequential([\n        tf.keras.layers.Flatten(input_shape = [28, 28]),\n        tf.keras.layers.Dense(300, activation = 'relu'),\n        tf.keras.layers.Dense(100, activation = 'relu'),\n        tf.keras.layers.Dense(100, activation = 'relu'),\n        tf.keras.layers.Dense(100, activation = 'relu'),\n        tf.keras.layers.Dense(5, activation = 'softmax')\n    \n    ])","metadata":{"execution":{"iopub.status.busy":"2023-04-28T14:57:19.833396Z","iopub.execute_input":"2023-04-28T14:57:19.834590Z","iopub.status.idle":"2023-04-28T14:57:19.910846Z","shell.execute_reply.started":"2023-04-28T14:57:19.834529Z","shell.execute_reply":"2023-04-28T14:57:19.909589Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model2.summary()","metadata":{"execution":{"iopub.status.busy":"2023-04-28T14:57:26.649782Z","iopub.execute_input":"2023-04-28T14:57:26.650247Z","iopub.status.idle":"2023-04-28T14:57:26.682233Z","shell.execute_reply.started":"2023-04-28T14:57:26.650160Z","shell.execute_reply":"2023-04-28T14:57:26.681079Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"model2.compile(loss = 'sparse_categorical_crossentropy',\n             optimizer = 'sgd',\n             metrics = ['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-04-28T14:57:37.807614Z","iopub.execute_input":"2023-04-28T14:57:37.808049Z","iopub.status.idle":"2023-04-28T14:57:37.822739Z","shell.execute_reply.started":"2023-04-28T14:57:37.808010Z","shell.execute_reply":"2023-04-28T14:57:37.821616Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history2 = model2.fit(x_train,y_train, epochs = 30, validation_data = (x_test,y_test))","metadata":{"execution":{"iopub.status.busy":"2023-04-28T14:57:43.311452Z","iopub.execute_input":"2023-04-28T14:57:43.311827Z","iopub.status.idle":"2023-04-28T15:01:07.973300Z","shell.execute_reply.started":"2023-04-28T14:57:43.311793Z","shell.execute_reply":"2023-04-28T15:01:07.971934Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport pandas as pd\n\npd.DataFrame(history2.history).plot(ylim = [0,1])","metadata":{"execution":{"iopub.status.busy":"2023-04-28T15:01:20.815196Z","iopub.execute_input":"2023-04-28T15:01:20.816363Z","iopub.status.idle":"2023-04-28T15:01:21.111234Z","shell.execute_reply.started":"2023-04-28T15:01:20.816307Z","shell.execute_reply":"2023-04-28T15:01:21.109736Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Convolutional Neural Network\nfrom functools import partial\n\nDefaultConv2D = partial(tf.keras.layers.Conv2D,\n                       kernel_size = 3,\n                       padding = 'same',\n                       activation = 'relu',\n                       kernel_initializer = 'he_normal')\n\nmodel3 = tf.keras.Sequential([\n    DefaultConv2D(filters = 64, kernel_size = 7, input_shape = [28, 28, 1]),\n    tf.keras.layers.MaxPool2D(),\n    DefaultConv2D(filters = 128),\n    DefaultConv2D(filters = 128),\n    tf.keras.layers.MaxPool2D(),\n    DefaultConv2D(filters = 256),\n    DefaultConv2D(filters = 256),\n    tf.keras.layers.MaxPool2D(),\n    tf.keras.layers.Flatten(),\n    tf.keras.layers.Dense(units = 128, activation = 'relu', kernel_initializer = 'he_normal'),\n    tf.keras.layers.Dropout(0.5),\n    tf.keras.layers.Dense(units = 64,  activation = 'relu', kernel_initializer = 'he_normal'),\n    tf.keras.layers.Dropout(0.5),\n    tf.keras.layers.Dense(10, activation = 'softmax')\n])\n","metadata":{"execution":{"iopub.status.busy":"2023-04-28T15:01:28.435436Z","iopub.execute_input":"2023-04-28T15:01:28.435864Z","iopub.status.idle":"2023-04-28T15:01:28.634127Z","shell.execute_reply.started":"2023-04-28T15:01:28.435823Z","shell.execute_reply":"2023-04-28T15:01:28.632820Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model3.compile(loss = 'sparse_categorical_crossentropy',\n             optimizer = 'sgd',\n             metrics = ['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-04-28T15:01:32.275910Z","iopub.execute_input":"2023-04-28T15:01:32.276350Z","iopub.status.idle":"2023-04-28T15:01:32.292937Z","shell.execute_reply.started":"2023-04-28T15:01:32.276309Z","shell.execute_reply":"2023-04-28T15:01:32.291481Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model3.summary()","metadata":{"execution":{"iopub.status.busy":"2023-04-28T15:01:35.004187Z","iopub.execute_input":"2023-04-28T15:01:35.004631Z","iopub.status.idle":"2023-04-28T15:01:35.052715Z","shell.execute_reply.started":"2023-04-28T15:01:35.004593Z","shell.execute_reply":"2023-04-28T15:01:35.051343Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history2 = model3.fit(x_train, y_train, epochs = 10, validation_data = (x_test, y_test))","metadata":{"execution":{"iopub.status.busy":"2023-04-28T15:01:45.931492Z","iopub.execute_input":"2023-04-28T15:01:45.931907Z","iopub.status.idle":"2023-04-28T16:02:09.071643Z","shell.execute_reply.started":"2023-04-28T15:01:45.931866Z","shell.execute_reply":"2023-04-28T16:02:09.070094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport pandas as pd\n\npd.DataFrame(history2.history).plot(ylim = [0,1])","metadata":{"execution":{"iopub.status.busy":"2023-04-28T16:02:09.955808Z","iopub.execute_input":"2023-04-28T16:02:09.956156Z","iopub.status.idle":"2023-04-28T16:02:10.242671Z","shell.execute_reply.started":"2023-04-28T16:02:09.956118Z","shell.execute_reply":"2023-04-28T16:02:10.241435Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Convolutional Neural Network\nfrom functools import partial\n\nDefaultConv2D = partial(tf.keras.layers.Conv2D,\n                       kernel_size = 3,\n                       padding = 'same',\n                       activation = 'relu',\n                       kernel_initializer = 'he_normal')\n\nmodel3a = tf.keras.Sequential([\n    DefaultConv2D(filters = 64, kernel_size = 7, input_shape = [28, 28, 1]),\n    tf.keras.layers.MaxPool2D(),\n    DefaultConv2D(filters = 128),\n    DefaultConv2D(filters = 128),\n    tf.keras.layers.MaxPool2D(),\n    DefaultConv2D(filters = 256),\n    DefaultConv2D(filters = 256),\n    tf.keras.layers.MaxPool2D(),\n    tf.keras.layers.Flatten(),\n    tf.keras.layers.Dense(units = 128, activation = 'relu', kernel_initializer = 'he_normal'),\n    tf.keras.layers.Dropout(0.7),\n    tf.keras.layers.Dense(units = 64,  activation = 'relu', kernel_initializer = 'he_normal'),\n    tf.keras.layers.Dropout(0.7),\n    tf.keras.layers.Dense(10, activation = 'softmax')\n])","metadata":{"execution":{"iopub.status.busy":"2023-04-28T16:02:28.700322Z","iopub.execute_input":"2023-04-28T16:02:28.700757Z","iopub.status.idle":"2023-04-28T16:02:28.856142Z","shell.execute_reply.started":"2023-04-28T16:02:28.700715Z","shell.execute_reply":"2023-04-28T16:02:28.854964Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model3a.compile(loss = 'sparse_categorical_crossentropy',\n             optimizer = 'sgd',\n             metrics = ['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-04-28T16:02:32.907773Z","iopub.execute_input":"2023-04-28T16:02:32.908221Z","iopub.status.idle":"2023-04-28T16:02:32.923010Z","shell.execute_reply.started":"2023-04-28T16:02:32.908179Z","shell.execute_reply":"2023-04-28T16:02:32.921291Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model3a.summary()","metadata":{"execution":{"iopub.status.busy":"2023-04-28T16:02:43.764863Z","iopub.execute_input":"2023-04-28T16:02:43.765351Z","iopub.status.idle":"2023-04-28T16:02:43.815233Z","shell.execute_reply.started":"2023-04-28T16:02:43.765308Z","shell.execute_reply":"2023-04-28T16:02:43.813975Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history2 = model3a.fit(x_train, y_train, epochs = 10, validation_data = (x_test, y_test))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport pandas as pd\n\npd.DataFrame(history2.history).plot(ylim = [0,1])","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Convolutional Neural Network\nfrom functools import partial\n\nDefaultConv2D = partial(tf.keras.layers.Conv2D,\n                       kernel_size = 3,\n                       padding = 'same',\n                       activation = 'relu',\n                       kernel_initializer = 'he_normal')\n\nmodel3b = tf.keras.Sequential([\n    DefaultConv2D(filters = 64, kernel_size = 7, input_shape = [28, 28, 1]),\n    tf.keras.layers.MaxPool2D(),\n    DefaultConv2D(filters = 128),\n    DefaultConv2D(filters = 128),\n    tf.keras.layers.MaxPool2D(),\n    DefaultConv2D(filters = 256),\n    DefaultConv2D(filters = 256),\n    tf.keras.layers.MaxPool2D(),\n    tf.keras.layers.Flatten(),\n    tf.keras.layers.Dense(units = 128, activation = 'relu', kernel_initializer = 'he_normal'),\n    tf.keras.layers.Dropout(0.3),\n    tf.keras.layers.Dense(units = 64,  activation = 'relu', kernel_initializer = 'he_normal'),\n    tf.keras.layers.Dropout(0.3),\n    tf.keras.layers.Dense(10, activation = 'softmax')\n])","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model3b.compile(loss = 'sparse_categorical_crossentropy',\n             optimizer = 'sgd',\n             metrics = ['accuracy'])","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model3b.summary()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history2 = model3b.fit(x_train, y_train, epochs = 10, validation_data = (x_test, y_test))","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport pandas as pd\n\npd.DataFrame(history2.history).plot(ylim = [0,1])","metadata":{},"execution_count":null,"outputs":[]}]}