{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":30732,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"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\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\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":"2024-07-13T14:26:23.741605Z","iopub.execute_input":"2024-07-13T14:26:23.741957Z","iopub.status.idle":"2024-07-13T14:26:24.749081Z","shell.execute_reply.started":"2024-07-13T14:26:23.741916Z","shell.execute_reply":"2024-07-13T14:26:24.748159Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nimport matplotlib.pyplot as plt \nfrom sklearn.model_selection import train_test_split ","metadata":{"execution":{"iopub.status.busy":"2024-07-13T14:26:42.924565Z","iopub.execute_input":"2024-07-13T14:26:42.925369Z","iopub.status.idle":"2024-07-13T14:26:42.929994Z","shell.execute_reply.started":"2024-07-13T14:26:42.925331Z","shell.execute_reply":"2024-07-13T14:26:42.928975Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_path = \"/kaggle/input/digit-recognizer/train.csv\"\ntest_path = \"/kaggle/input/digit-recognizer/test.csv\"","metadata":{"execution":{"iopub.status.busy":"2024-07-13T14:26:46.887032Z","iopub.execute_input":"2024-07-13T14:26:46.887471Z","iopub.status.idle":"2024-07-13T14:26:46.891790Z","shell.execute_reply.started":"2024-07-13T14:26:46.887438Z","shell.execute_reply":"2024-07-13T14:26:46.890822Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_train_data(file_path):\n    data = pd.read_csv(file_path)\n    pixel_data = data.values\n    images = []\n    labels = []\n    for pixels in pixel_data:\n        labels.append(pixels[0])\n        images.append(pixels[1:].reshape(28,28,1))\n    return labels, images    ","metadata":{"execution":{"iopub.status.busy":"2024-07-13T14:26:49.861810Z","iopub.execute_input":"2024-07-13T14:26:49.862424Z","iopub.status.idle":"2024-07-13T14:26:49.869073Z","shell.execute_reply.started":"2024-07-13T14:26:49.862389Z","shell.execute_reply":"2024-07-13T14:26:49.868150Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_test_data(file_path):\n    data = pd.read_csv(file_path)\n    pixel_data = data.values\n    images = []\n    for pixels in pixel_data:\n        images.append(pixels[0:].reshape(28,28,1))\n    return labels, images","metadata":{"execution":{"iopub.status.busy":"2024-07-10T04:33:58.294638Z","iopub.execute_input":"2024-07-10T04:33:58.295048Z","iopub.status.idle":"2024-07-10T04:33:58.306906Z","shell.execute_reply.started":"2024-07-10T04:33:58.295015Z","shell.execute_reply":"2024-07-10T04:33:58.305258Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels, train_image = load_train_data(train_path)","metadata":{"execution":{"iopub.status.busy":"2024-07-13T14:26:53.333590Z","iopub.execute_input":"2024-07-13T14:26:53.334381Z","iopub.status.idle":"2024-07-13T14:26:57.319265Z","shell.execute_reply.started":"2024-07-13T14:26:53.334340Z","shell.execute_reply":"2024-07-13T14:26:57.318470Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(train_image)","metadata":{"execution":{"iopub.status.busy":"2024-07-13T14:32:11.235781Z","iopub.execute_input":"2024-07-13T14:32:11.236161Z","iopub.status.idle":"2024-07-13T14:32:11.242417Z","shell.execute_reply.started":"2024-07-13T14:32:11.236127Z","shell.execute_reply":"2024-07-13T14:32:11.241446Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"indices = np.random.randint(1,5000,3)\nindices","metadata":{"execution":{"iopub.status.busy":"2024-07-13T14:32:54.899998Z","iopub.execute_input":"2024-07-13T14:32:54.900658Z","iopub.status.idle":"2024-07-13T14:32:54.907099Z","shell.execute_reply.started":"2024-07-13T14:32:54.900625Z","shell.execute_reply":"2024-07-13T14:32:54.906152Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i,idx in enumerate(indices,start=1):\n    plt.subplot(1,len(indices),i)\n    plt.imshow(train_image[idx])\n    plt.title(labels[idx])\n    plt.axis(\"off\")\n    \nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-07-13T14:32:56.893613Z","iopub.execute_input":"2024-07-13T14:32:56.894294Z","iopub.status.idle":"2024-07-13T14:32:57.082433Z","shell.execute_reply.started":"2024-07-13T14:32:56.894260Z","shell.execute_reply":"2024-07-13T14:32:57.081477Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(labels), len(train_image)","metadata":{"execution":{"iopub.status.busy":"2024-07-10T04:34:03.217550Z","iopub.execute_input":"2024-07-10T04:34:03.217954Z","iopub.status.idle":"2024-07-10T04:34:03.227035Z","shell.execute_reply.started":"2024-07-10T04:34:03.217914Z","shell.execute_reply":"2024-07-10T04:34:03.225709Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_image = np.array(train_image)\nlabels = np.array(labels)\n\ntrain_image = train_image / 255.0    # normalization => value (0 to 1)\n# standardization => mean 1, variance 0\n# BatchNormalization => standardization + normalization\nprint(train_image.shape)","metadata":{"execution":{"iopub.status.busy":"2024-07-13T14:33:43.713489Z","iopub.execute_input":"2024-07-13T14:33:43.714164Z","iopub.status.idle":"2024-07-13T14:33:43.963493Z","shell.execute_reply.started":"2024-07-13T14:33:43.714113Z","shell.execute_reply":"2024-07-13T14:33:43.962577Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_image.dtype","metadata":{"execution":{"iopub.status.busy":"2024-07-13T14:34:42.019389Z","iopub.execute_input":"2024-07-13T14:34:42.019758Z","iopub.status.idle":"2024-07-13T14:34:42.025790Z","shell.execute_reply.started":"2024-07-13T14:34:42.019728Z","shell.execute_reply":"2024-07-13T14:34:42.024717Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train , x_val , y_train , y_val =  train_test_split(train_image, labels, train_size = 0.85 , random_state = 42)","metadata":{"execution":{"iopub.status.busy":"2024-07-13T14:54:31.689962Z","iopub.execute_input":"2024-07-13T14:54:31.690770Z","iopub.status.idle":"2024-07-13T14:54:31.781526Z","shell.execute_reply.started":"2024-07-13T14:54:31.690732Z","shell.execute_reply":"2024-07-13T14:54:31.780712Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(x_train.shape)","metadata":{"execution":{"iopub.status.busy":"2024-07-13T14:54:34.056223Z","iopub.execute_input":"2024-07-13T14:54:34.056949Z","iopub.status.idle":"2024-07-13T14:54:34.061487Z","shell.execute_reply.started":"2024-07-13T14:54:34.056919Z","shell.execute_reply":"2024-07-13T14:54:34.060521Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(x_val.shape)","metadata":{"execution":{"iopub.status.busy":"2024-07-13T14:54:36.106977Z","iopub.execute_input":"2024-07-13T14:54:36.107359Z","iopub.status.idle":"2024-07-13T14:54:36.112206Z","shell.execute_reply.started":"2024-07-13T14:54:36.107326Z","shell.execute_reply":"2024-07-13T14:54:36.111136Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def CNN_Model(input_shape=(28, 28, 1)):\n    inputs = tf.keras.layers.Input(shape=input_shape)\n\n    conv1 = tf.keras.layers.Conv2D(filters=32, kernel_size=(3, 3), padding=\"same\", activation=\"relu\")(inputs)\n    pool1 = tf.keras.layers.MaxPool2D(pool_size=(2, 2), strides=(2, 2))(conv1)\n    \n    conv2 = tf.keras.layers.Conv2D(filters=64, kernel_size=(3, 3), padding=\"same\", activation=\"relu\")(pool1)\n    pool2 = tf.keras.layers.MaxPool2D(pool_size=(2, 2), strides=(2, 2))(conv2)\n    \n    conv3 = tf.keras.layers.Conv2D(filters=128, kernel_size=(3, 3), padding=\"same\", activation=\"relu\")(pool2)\n    pool3 = tf.keras.layers.MaxPool2D(pool_size=(2, 2), strides=(2,2))(conv3)\n \n    flatten = tf.keras.layers.Flatten()(pool3)\n    fc1 = tf.keras.layers.Dense(units=128, activation=\"relu\")(flatten)\n    fc2 = tf.keras.layers.Dense(units=256, activation=\"relu\")(fc1)\n    \n    outputs = tf.keras.layers.Dense(units=10, activation=\"softmax\")(fc2)\n    \n    model = tf.keras.models.Model(inputs, outputs)\n    model.summary()\n    \n    return model\n","metadata":{"execution":{"iopub.status.busy":"2024-07-13T14:54:37.785690Z","iopub.execute_input":"2024-07-13T14:54:37.786336Z","iopub.status.idle":"2024-07-13T14:54:37.795985Z","shell.execute_reply.started":"2024-07-13T14:54:37.786297Z","shell.execute_reply":"2024-07-13T14:54:37.795077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cnn_model = CNN_Model()","metadata":{"execution":{"iopub.status.busy":"2024-07-13T14:54:40.659496Z","iopub.execute_input":"2024-07-13T14:54:40.659856Z","iopub.status.idle":"2024-07-13T14:54:41.428777Z","shell.execute_reply.started":"2024-07-13T14:54:40.659826Z","shell.execute_reply":"2024-07-13T14:54:41.428045Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cnn_model.compile(optimizer=tf.keras.optimizers.Adam(), loss=\"sparse_categorical_crossentropy\", metrics=[\"acc\"])\ncallbacks = tf.keras.callbacks.ReduceLROnPlateau(monitor=\"val_acc\", factor=0.5, patience=5, min_lr=1e-4)","metadata":{"execution":{"iopub.status.busy":"2024-07-13T15:02:36.770106Z","iopub.execute_input":"2024-07-13T15:02:36.770840Z","iopub.status.idle":"2024-07-13T15:02:36.786480Z","shell.execute_reply.started":"2024-07-13T15:02:36.770794Z","shell.execute_reply":"2024-07-13T15:02:36.785615Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = cnn_model.fit(x_train, y_train, validation_data=(x_val,y_val), epochs = 10, callbacks = [callbacks])","metadata":{"execution":{"iopub.status.busy":"2024-07-13T15:02:39.624746Z","iopub.execute_input":"2024-07-13T15:02:39.625459Z","iopub.status.idle":"2024-07-13T15:03:15.941612Z","shell.execute_reply.started":"2024-07-13T15:02:39.625424Z","shell.execute_reply":"2024-07-13T15:03:15.940653Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_test_data(file_path):\n    data = pd.read_csv(file_path)\n    pixel_data = data.values\n    images = []\n    for pixels in pixel_data:\n        images.append(pixels[0:].reshape(28,28,1))\n    return images","metadata":{"execution":{"iopub.status.busy":"2024-07-13T15:04:55.435058Z","iopub.execute_input":"2024-07-13T15:04:55.435918Z","iopub.status.idle":"2024-07-13T15:04:55.440862Z","shell.execute_reply.started":"2024-07-13T15:04:55.435884Z","shell.execute_reply":"2024-07-13T15:04:55.439883Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_image = load_test_data(test_path)","metadata":{"execution":{"iopub.status.busy":"2024-07-13T15:04:59.198188Z","iopub.execute_input":"2024-07-13T15:04:59.199021Z","iopub.status.idle":"2024-07-13T15:05:01.322176Z","shell.execute_reply.started":"2024-07-13T15:04:59.198989Z","shell.execute_reply":"2024-07-13T15:05:01.321399Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(test_image)","metadata":{"execution":{"iopub.status.busy":"2024-07-13T15:05:02.076062Z","iopub.execute_input":"2024-07-13T15:05:02.076822Z","iopub.status.idle":"2024-07-13T15:05:02.084371Z","shell.execute_reply.started":"2024-07-13T15:05:02.076787Z","shell.execute_reply":"2024-07-13T15:05:02.083376Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_image = np.array(test_image)","metadata":{"execution":{"iopub.status.busy":"2024-07-13T15:05:04.425533Z","iopub.execute_input":"2024-07-13T15:05:04.426336Z","iopub.status.idle":"2024-07-13T15:05:04.533541Z","shell.execute_reply.started":"2024-07-13T15:05:04.426292Z","shell.execute_reply":"2024-07-13T15:05:04.532732Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_image = test_image/255.0","metadata":{"execution":{"iopub.status.busy":"2024-07-13T15:05:06.696291Z","iopub.execute_input":"2024-07-13T15:05:06.696951Z","iopub.status.idle":"2024-07-13T15:05:06.766256Z","shell.execute_reply.started":"2024-07-13T15:05:06.696920Z","shell.execute_reply":"2024-07-13T15:05:06.765449Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_image.shape","metadata":{"execution":{"iopub.status.busy":"2024-07-13T15:05:08.830050Z","iopub.execute_input":"2024-07-13T15:05:08.830718Z","iopub.status.idle":"2024-07-13T15:05:08.836533Z","shell.execute_reply.started":"2024-07-13T15:05:08.830686Z","shell.execute_reply":"2024-07-13T15:05:08.835672Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction = cnn_model.predict(test_image)\n\npreds = np.argmax(prediction, axis=1)\n\nprint(preds)","metadata":{"execution":{"iopub.status.busy":"2024-07-13T15:05:10.737719Z","iopub.execute_input":"2024-07-13T15:05:10.738329Z","iopub.status.idle":"2024-07-13T15:05:12.987072Z","shell.execute_reply.started":"2024-07-13T15:05:10.738298Z","shell.execute_reply":"2024-07-13T15:05:12.985861Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = {\n    \"ImageId\" : [i+1 for i in range(test_image.shape[0])],\n    \"Label\" : preds \n}\n\ndf = pd.DataFrame(data)\n\nprint(df)\n\ndf.to_csv(\"submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2024-07-13T15:06:22.553203Z","iopub.execute_input":"2024-07-13T15:06:22.553584Z","iopub.status.idle":"2024-07-13T15:06:22.629481Z","shell.execute_reply.started":"2024-07-13T15:06:22.553554Z","shell.execute_reply":"2024-07-13T15:06:22.628568Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"indices = np.random.randint(700,size=3)","metadata":{"execution":{"iopub.status.busy":"2024-07-13T15:09:12.876386Z","iopub.execute_input":"2024-07-13T15:09:12.876736Z","iopub.status.idle":"2024-07-13T15:09:12.881454Z","shell.execute_reply.started":"2024-07-13T15:09:12.876709Z","shell.execute_reply":"2024-07-13T15:09:12.880397Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"indices","metadata":{"execution":{"iopub.status.busy":"2024-07-13T15:09:15.798969Z","iopub.execute_input":"2024-07-13T15:09:15.799641Z","iopub.status.idle":"2024-07-13T15:09:15.805454Z","shell.execute_reply.started":"2024-07-13T15:09:15.799606Z","shell.execute_reply":"2024-07-13T15:09:15.804458Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i,idx in enumerate(indices,start=1):\n    plt.subplot(1,len(indices),i)\n    plt.imshow(test_image[idx])\n    plt.title(preds[idx])\n    plt.axis('off')\n    \nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-07-13T15:09:17.895148Z","iopub.execute_input":"2024-07-13T15:09:17.895949Z","iopub.status.idle":"2024-07-13T15:09:18.144704Z","shell.execute_reply.started":"2024-07-13T15:09:17.895919Z","shell.execute_reply":"2024-07-13T15:09:18.143465Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from PIL import Image, ImageOps\nimport ipywidgets as widgets\nfrom IPython.display import display\nimport io","metadata":{"execution":{"iopub.status.busy":"2024-07-13T15:09:46.333227Z","iopub.execute_input":"2024-07-13T15:09:46.334075Z","iopub.status.idle":"2024-07-13T15:09:46.422397Z","shell.execute_reply.started":"2024-07-13T15:09:46.334028Z","shell.execute_reply":"2024-07-13T15:09:46.421381Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def preprocess_image(image):\n    image = ImageOps.grayscale(image)  # Convert to grayscale\n    image = ImageOps.invert(image)     # Invert the image colors\n    image = image.resize((28, 28))     # Resize to 28x28\n    image = np.array(image).astype('float32') / 255  # Normalize\n    image = np.expand_dims(image, axis=(0, -1))  # Add batch and channel dimensions\n    return image\n\ndef predict_digit(image):\n    processed_image = preprocess_image(image)\n    prediction = cnn_model.predict(processed_image)\n    predicted_digit = np.argmax(prediction, axis=1)[0]\n    return predicted_digit\n","metadata":{"execution":{"iopub.status.busy":"2024-07-13T15:10:08.194761Z","iopub.execute_input":"2024-07-13T15:10:08.195622Z","iopub.status.idle":"2024-07-13T15:10:08.201771Z","shell.execute_reply.started":"2024-07-13T15:10:08.195590Z","shell.execute_reply":"2024-07-13T15:10:08.200875Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"upload_widget = widgets.FileUpload(accept='image/*', multiple=False)\n\noutput_widget = widgets.Output()\n\ndef on_file_upload(change):\n    output_widget.clear_output()\n    with output_widget:\n        for name, file_info in upload_widget.value.items():\n            image = Image.open(io.BytesIO(file_info['content']))\n\n            display(image)\n\n            predicted_digit = predict_digit(image)\n            print(f'Predicted Digit: {predicted_digit}')\n\nupload_widget.observe(on_file_upload, names='value')\n\ndisplay(widgets.VBox([upload_widget, output_widget]))","metadata":{"execution":{"iopub.status.busy":"2024-07-13T15:10:11.505665Z","iopub.execute_input":"2024-07-13T15:10:11.506029Z","iopub.status.idle":"2024-07-13T15:10:11.522561Z","shell.execute_reply.started":"2024-07-13T15:10:11.505998Z","shell.execute_reply":"2024-07-13T15:10:11.521576Z"},"trusted":true},"execution_count":null,"outputs":[]}]}