{"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 os\nimport json\nimport cv2\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-04-27T23:25:16.703342Z","iopub.execute_input":"2023-04-27T23:25:16.704015Z","iopub.status.idle":"2023-04-27T23:25:24.519079Z","shell.execute_reply.started":"2023-04-27T23:25:16.703978Z","shell.execute_reply":"2023-04-27T23:25:24.517946Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load annotations\nannotations_path = \"/kaggle/input/benetech-making-graphs-accessible/train/annotations\"\nannotations = []\nfor file in os.listdir(annotations_path):\n    with open(os.path.join(annotations_path, file)) as f:\n        annotations.append(json.load(f))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load images\nimages_path = \"/kaggle/input/benetech-making-graphs-accessible/train/images\"\nimages = []\nfor file in os.listdir(images_path):\n    img = cv2.imread(os.path.join(images_path, file), cv2.IMREAD_GRAYSCALE)\n    images.append(img)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Preprocess data\nX = np.array(images).reshape(-1, 128, 128, 1) / 255.0\ny = [annotation['data-series'] for annotation in annotations]\ny = np.array(y)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Train-test split\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential([\n    Conv2D(32, (3, 3), activation='relu', input_shape=(128, 128, 1)),\n    MaxPooling2D((2, 2)),\n    Conv2D(64, (3, 3), activation='relu'),\n    MaxPooling2D((2, 2)),\n    Flatten(),\n    Dense(128, activation='relu'),\n    Dropout(0.5),\n    Dense(2, activation='linear')  # Predict X and Y coordinates\n])","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='adam', loss='mse')","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(X_train, y_train, epochs=20, batch_size=32, validation_data=(X_test, y_test))","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_images_path = \"test/images/\"\ntest_images = []\nfor file in os.listdir(test_images_path):\n    img = cv2.imread(os.path.join(test_images_path, file), cv2.IMREAD_GRAYSCALE)\n    test_images.append(img)\n\nX_test = np.array(test_images).reshape(-1, 128, 128, 1) / 255.0\npredictions = model.predict(X_test)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.read_csv('sample_submission.csv')\nsubmission['data-series'] = predictions.tolist()\nsubmission.to_csv('submission.csv', index=False)","metadata":{},"execution_count":null,"outputs":[]}]}