{"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":"nvidiaTeslaT4","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"},{"sourceId":3107,"sourceType":"modelInstanceVersion","isSourceIdPinned":true,"modelInstanceId":2320,"modelId":271}],"dockerImageVersionId":30699,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!pip install silence_tensorflow","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-06-14T19:11:13.446630Z","iopub.execute_input":"2024-06-14T19:11:13.447097Z","iopub.status.idle":"2024-06-14T19:11:31.977373Z","shell.execute_reply.started":"2024-06-14T19:11:13.447070Z","shell.execute_reply":"2024-06-14T19:11:31.976412Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport matplotlib.pyplot as plt\nfrom silence_tensorflow import silence_tensorflow\nsilence_tensorflow()\nimport tensorflow as tf","metadata":{"execution":{"iopub.status.busy":"2024-06-14T19:12:14.402190Z","iopub.execute_input":"2024-06-14T19:12:14.402530Z","iopub.status.idle":"2024-06-14T19:12:26.395368Z","shell.execute_reply.started":"2024-06-14T19:12:14.402501Z","shell.execute_reply":"2024-06-14T19:12:26.394537Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ntest = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\n\n# explore train data\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2024-06-14T19:12:37.006801Z","iopub.execute_input":"2024-06-14T19:12:37.007747Z","iopub.status.idle":"2024-06-14T19:12:42.339636Z","shell.execute_reply.started":"2024-06-14T19:12:37.007711Z","shell.execute_reply":"2024-06-14T19:12:42.338708Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# get labels\ny_train = train['label']\nx_train = train.drop(labels = ['label'], axis = 1)\n\n# Normalize the data\nx_train = x_train.values.reshape(-1, 28, 28, 1) / 255.0\nx_test = test.values.reshape(-1, 28, 28, 1) / 255.0\n\n# Show first 10 images\nplt.figure(figsize=(10, 10))\nfor i in range(10):\n    plt.subplot(5, 5, i+1)\n    plt.xticks([])\n    plt.yticks([])\n    plt.grid(False)\n    plt.imshow(x_train[i].reshape(28, 28), cmap=plt.cm.binary)\n    plt.xlabel(y_train[i])","metadata":{"execution":{"iopub.status.busy":"2024-06-14T19:12:46.747301Z","iopub.execute_input":"2024-06-14T19:12:46.747659Z","iopub.status.idle":"2024-06-14T19:12:47.736510Z","shell.execute_reply.started":"2024-06-14T19:12:46.747631Z","shell.execute_reply":"2024-06-14T19:12:47.735311Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## Transfer learning model with InceptionV3\n\nfrom tensorflow.keras.applications import InceptionV3\n\nbase_model = InceptionV3(input_shape=(75,75,3), include_top=False, weights='imagenet')\n\nprint(len(base_model.layers))","metadata":{"execution":{"iopub.status.busy":"2024-06-14T19:40:28.050241Z","iopub.execute_input":"2024-06-14T19:40:28.051228Z","iopub.status.idle":"2024-06-14T19:40:29.825900Z","shell.execute_reply.started":"2024-06-14T19:40:28.051188Z","shell.execute_reply":"2024-06-14T19:40:29.824960Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for layer in base_model.layers:\n    layer.trainable = False\n    \nlast_layer = base_model.get_layer('mixed7')\n    \nlast_output = last_layer.output\n\nx = tf.keras.layers.Flatten()(last_output)\nx = tf.keras.layers.Dense(512, activation='relu')(x)\nx = tf.keras.layers.Dropout(0.3)(x)\nx = tf.keras.layers.Dense(256, activation='relu')(x)\nx = tf.keras.layers.Dropout(0.5)(x)\nx = tf.keras.layers.Dense(10, activation='softmax')(x)\n\nmodel_inception = tf.keras.Model(base_model.input, x)    \n# Compile Model\n\nmodel_inception.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.0001), loss='sparse_categorical_crossentropy', metrics=['accuracy'])\n\n# Train Model\ninception_x_train = tf.image.resize(x_train, [75, 75])\ninception_x_train = tf.repeat(inception_x_train, 3, -1)\n","metadata":{"execution":{"iopub.status.busy":"2024-06-14T19:42:19.025763Z","iopub.execute_input":"2024-06-14T19:42:19.026662Z","iopub.status.idle":"2024-06-14T19:42:19.570785Z","shell.execute_reply.started":"2024-06-14T19:42:19.026628Z","shell.execute_reply":"2024-06-14T19:42:19.569815Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datagen = tf.keras.preprocessing.image.ImageDataGenerator(\n    rotation_range=10,\n    width_shift_range=0.1,\n    height_shift_range=0.1,\n    zoom_range=0.1\n)","metadata":{"execution":{"iopub.status.busy":"2024-06-14T19:42:22.403714Z","iopub.execute_input":"2024-06-14T19:42:22.404438Z","iopub.status.idle":"2024-06-14T19:42:22.408851Z","shell.execute_reply.started":"2024-06-14T19:42:22.404393Z","shell.execute_reply":"2024-06-14T19:42:22.407823Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nearly_stopping = tf.keras.callbacks.EarlyStopping(monitor='loss', patience=5, restore_best_weights=True)\nhistory = model_inception.fit( datagen.flow(inception_x_train, y_train, batch_size=32), epochs=20,  callbacks=[early_stopping])","metadata":{"execution":{"iopub.status.busy":"2024-06-14T19:42:29.048774Z","iopub.execute_input":"2024-06-14T19:42:29.049604Z","iopub.status.idle":"2024-06-14T20:05:05.630281Z","shell.execute_reply.started":"2024-06-14T19:42:29.049572Z","shell.execute_reply":"2024-06-14T20:05:05.629422Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Prepare test data\n\ninception_x_test = tf.image.resize(x_test, [75, 75])\ninception_x_test = tf.repeat(inception_x_test, 3, -1)\n\npredictions = model_inception.predict(inception_x_test[:10])\n\n# Get labels\n\nlabels = [tf.argmax(prediction).numpy() for prediction in predictions]\n\n# Show first 10 images\n\nplt.figure(figsize=(10, 10))\nfor i in range(10):\n    plt.subplot(5, 5, i+1)\n    plt.xticks([])\n    plt.yticks([])\n    plt.grid(False)\n    plt.imshow(x_test[i].reshape(28, 28), cmap=plt.cm.binary)\n    plt.xlabel(labels[i])\n","metadata":{"execution":{"iopub.status.busy":"2024-06-14T20:05:09.443416Z","iopub.execute_input":"2024-06-14T20:05:09.443789Z","iopub.status.idle":"2024-06-14T20:05:18.000920Z","shell.execute_reply.started":"2024-06-14T20:05:09.443757Z","shell.execute_reply":"2024-06-14T20:05:17.999990Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create submission file\npredictions = model_inception.predict(inception_x_test)\nlabels = [tf.argmax(prediction).numpy() for prediction in predictions]\nsubmission = pd.DataFrame({'ImageId': range(1, len(inception_x_test) + 1), 'Label': labels})\nsubmission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2024-06-14T20:05:24.955541Z","iopub.execute_input":"2024-06-14T20:05:24.955908Z","iopub.status.idle":"2024-06-14T20:05:44.574001Z","shell.execute_reply.started":"2024-06-14T20:05:24.955878Z","shell.execute_reply":"2024-06-14T20:05:44.573206Z"},"trusted":true},"execution_count":null,"outputs":[]}]}