{"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\nimport os\n\nimport tensorflow as tf\n\nfrom tensorflow.keras import datasets, layers, models\n\n\nprint(\"This cell has run\")\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-08-08T16:21:52.337782Z","iopub.execute_input":"2023-08-08T16:21:52.338822Z","iopub.status.idle":"2023-08-08T16:22:00.936046Z","shell.execute_reply.started":"2023-08-08T16:21:52.338663Z","shell.execute_reply":"2023-08-08T16:22:00.934667Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Whole Concept is base on color majority in 4x4 sectors, if white then switch colors.\n#### CORRECTION: there were large digits so the 2nd cleaner counts pixels on border and then switch colors","metadata":{}},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"markdown","source":"### 1st version - counting area","metadata":{}},{"cell_type":"code","source":"\n\n# Define paths to image directories\ntrain_dir = '/kaggle/input/ultra-mnist/train'\ntest_dir = '/kaggle/input/ultra-mnist/test'\n\n# Define the number of images to load for testing\nnum_images_to_load = 50 # Adjust this number as needed\n\n# Load and preprocess train images\ntrain_image_paths = [os.path.join(train_dir, filename) for filename in os.listdir(train_dir)[:num_images_to_load]]\ntrain_images = [tf.keras.preprocessing.image.load_img(path, target_size=(4000, 4000)) for path in train_image_paths]\ntrain_images = [tf.keras.preprocessing.image.img_to_array(img) for img in train_images]\ntrain_images = np.array(train_images) / 255.0\n\n# Load and preprocess test images\ntest_image_paths = [os.path.join(test_dir, filename) for filename in os.listdir(test_dir)[:num_images_to_load]]\ntest_images = [tf.keras.preprocessing.image.load_img(path, target_size=(4000, 4000)) for path in test_image_paths]\ntest_images = [tf.keras.preprocessing.image.img_to_array(img) for img in test_images]\ntest_images = np.array(test_images) / 255.0\n\nprint(\"This cell has run\")\nprint(\"Shape of train_images:\", train_images.shape)","metadata":{"execution":{"iopub.status.busy":"2023-08-08T16:26:15.198015Z","iopub.execute_input":"2023-08-08T16:26:15.198643Z","iopub.status.idle":"2023-08-08T16:27:05.040705Z","shell.execute_reply.started":"2023-08-08T16:26:15.198603Z","shell.execute_reply":"2023-08-08T16:27:05.039136Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\n# Assuming you have loaded and preprocessed the images as 'train_images' and 'test_images'\n\n# Define the number of images to display\nnum_images_to_display = 25\n\n# Create a grid of subplots for displaying images\nplt.figure(figsize=(10, 10))\nfor i in range(num_images_to_display):\n    plt.subplot(5, 5, i + 1)\n    plt.imshow(train_images[i], cmap='gray')  # Assuming images are grayscale\n    plt.axis('off')  # Turn off axes\n    plt.title(f\"Image {i + 1}\")  # Add a title to each subplot\n\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-08-08T16:27:07.946665Z","iopub.execute_input":"2023-08-08T16:27:07.947061Z","iopub.status.idle":"2023-08-08T16:28:02.770843Z","shell.execute_reply.started":"2023-08-08T16:27:07.947016Z","shell.execute_reply":"2023-08-08T16:28:02.769824Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 2nd version - counting borders","metadata":{}},{"cell_type":"code","source":"model = models.Sequential()\nmodel.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)))\nmodel.add(layers.MaxPooling2D((2, 2)))\nmodel.add(layers.Conv2D(64, (3, 3), activation='relu'))\nmodel.add(layers.MaxPooling2D((2, 2)))\nmodel.add(layers.Conv2D(64, (3, 3), activation='relu'))","metadata":{"execution":{"iopub.status.busy":"2023-08-08T16:28:36.542811Z","iopub.execute_input":"2023-08-08T16:28:36.543124Z","iopub.status.idle":"2023-08-08T16:28:37.200128Z","shell.execute_reply.started":"2023-08-08T16:28:36.54309Z","shell.execute_reply":"2023-08-08T16:28:37.19932Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-08-08T16:29:01.726926Z","iopub.execute_input":"2023-08-08T16:29:01.727589Z","iopub.status.idle":"2023-08-08T16:29:01.738131Z","shell.execute_reply.started":"2023-08-08T16:29:01.727534Z","shell.execute_reply":"2023-08-08T16:29:01.73675Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.add(layers.Flatten())\nmodel.add(layers.Dense(64, activation='relu'))\nmodel.add(layers.Dense(10))","metadata":{"execution":{"iopub.status.busy":"2023-08-08T16:32:53.05416Z","iopub.execute_input":"2023-08-08T16:32:53.054591Z","iopub.status.idle":"2023-08-08T16:32:53.094817Z","shell.execute_reply.started":"2023-08-08T16:32:53.054546Z","shell.execute_reply":"2023-08-08T16:32:53.094031Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-08-08T16:32:57.773716Z","iopub.execute_input":"2023-08-08T16:32:57.774005Z","iopub.status.idle":"2023-08-08T16:32:57.785606Z","shell.execute_reply.started":"2023-08-08T16:32:57.773974Z","shell.execute_reply":"2023-08-08T16:32:57.784762Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"resized_train_images = tf.image.resize(train_images, (32, 32))\nresized_test_images = tf.image.resize(test_images, (32, 32))\n\nmodel.compile(optimizer='adam',\n              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),\n              metrics=['accuracy'])\n\nhistory = model.fit(resized_train_images, resized_train_images, epochs=20, \n                    validation_data=(resized_test_images, resized_test_images))","metadata":{"execution":{"iopub.status.busy":"2023-08-08T17:18:59.481823Z","iopub.execute_input":"2023-08-08T17:18:59.48274Z","iopub.status.idle":"2023-08-08T17:19:00.82292Z","shell.execute_reply.started":"2023-08-08T17:18:59.482659Z","shell.execute_reply":"2023-08-08T17:19:00.819888Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}