{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","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"},{"sourceId":6157,"sourceType":"modelInstanceVersion","isSourceIdPinned":true,"modelInstanceId":4607,"modelId":2797}],"dockerImageVersionId":30762,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport tensorflow as tf\nfrom sklearn.model_selection import train_test_split\n\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-09-09T18:07:16.859867Z","iopub.execute_input":"2024-09-09T18:07:16.860264Z","iopub.status.idle":"2024-09-09T18:07:16.867662Z","shell.execute_reply.started":"2024-09-09T18:07:16.860229Z","shell.execute_reply":"2024-09-09T18:07:16.866875Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ntest_data = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')","metadata":{"execution":{"iopub.status.busy":"2024-09-09T18:07:16.869551Z","iopub.execute_input":"2024-09-09T18:07:16.870005Z","iopub.status.idle":"2024-09-09T18:07:21.288392Z","shell.execute_reply.started":"2024-09-09T18:07:16.869959Z","shell.execute_reply":"2024-09-09T18:07:21.287571Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datagen = ImageDataGenerator(\n    rotation_range=10,\n    width_shift_range=0.1,\n    height_shift_range=0.1,\n    zoom_range=0.1,\n    horizontal_flip=False\n)","metadata":{"execution":{"iopub.status.busy":"2024-09-09T18:07:21.289457Z","iopub.execute_input":"2024-09-09T18:07:21.289768Z","iopub.status.idle":"2024-09-09T18:07:21.294439Z","shell.execute_reply.started":"2024-09-09T18:07:21.289736Z","shell.execute_reply":"2024-09-09T18:07:21.293517Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Prepare the data\nX = train_data.drop('label', axis=1).values / 255.0  # Normalize pixel values\ny = train_data['label'].values\n\n# Reshape data and convert to 3 channels for EfficientNetV2\nX = X.reshape(-1, 28, 28, 1)\nX = np.repeat(X, 3, axis=-1)  # Convert to 3 channels\n\n# Resize to 32x32\nX_resized = np.array([tf.image.resize(image, (32, 32)).numpy() for image in X])\n\n# Split into training and validation sets\nX_train, X_val, y_train, y_val = train_test_split(X_resized, y, test_size=0.2, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2024-09-09T18:07:21.295627Z","iopub.execute_input":"2024-09-09T18:07:21.295997Z","iopub.status.idle":"2024-09-09T18:07:54.476667Z","shell.execute_reply.started":"2024-09-09T18:07:21.295956Z","shell.execute_reply":"2024-09-09T18:07:54.475862Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datagen.fit(X_train)","metadata":{"execution":{"iopub.status.busy":"2024-09-09T18:07:54.478678Z","iopub.execute_input":"2024-09-09T18:07:54.479010Z","iopub.status.idle":"2024-09-09T18:07:54.625834Z","shell.execute_reply.started":"2024-09-09T18:07:54.478977Z","shell.execute_reply":"2024-09-09T18:07:54.624819Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lr_scheduler = tf.keras.callbacks.ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=3)","metadata":{"execution":{"iopub.status.busy":"2024-09-09T18:09:32.251894Z","iopub.execute_input":"2024-09-09T18:09:32.252564Z","iopub.status.idle":"2024-09-09T18:09:32.257285Z","shell.execute_reply.started":"2024-09-09T18:09:32.252518Z","shell.execute_reply":"2024-09-09T18:09:32.256395Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = tf.keras.applications.EfficientNetV2B2(\n    weights=None,  # No pretrained weights\n    input_shape=(32, 32, 3),  # Adjust the input size accordingly\n    classes=10  # Change to 10 classes for digit classification\n)\n\n# Compile the model\nmodel.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.001), loss='sparse_categorical_crossentropy', metrics=['accuracy'])\n\n# Train the model\nmodel.fit(datagen.flow(X_train, y_train, batch_size=64),\n          validation_data=(X_val, y_val),\n          epochs=20,\n          callbacks=[lr_scheduler])","metadata":{"execution":{"iopub.status.busy":"2024-09-09T18:09:50.100254Z","iopub.execute_input":"2024-09-09T18:09:50.100640Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Preprocess test data\ntest_data = test_data.values / 255.0\ntest_data = test_data.reshape(-1, 28, 28, 1)\ntest_data = np.repeat(test_data, 3, axis=-1)  # Convert to 3 channels for EfficientNet\ntest_data = np.array([tf.image.resize(image, (32, 32)).numpy() for image in test_data])  # Resize to 32x32\n\n# Predict on test data\npredictions = model.predict(test_data)\n\n# Convert predictions to labels\npredicted_labels = np.argmax(predictions, axis=1)\n\n# Load the sample submission and update the labels\nsubmission = pd.read_csv('/kaggle/input/digit-recognizer/sample_submission.csv')\nsubmission['Label'] = predicted_labels\n\n# Save the predictions for submission\nsubmission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2024-09-09T18:07:55.423663Z","iopub.status.idle":"2024-09-09T18:07:55.424032Z","shell.execute_reply.started":"2024-09-09T18:07:55.423861Z","shell.execute_reply":"2024-09-09T18:07:55.423878Z"},"trusted":true},"execution_count":null,"outputs":[]}]}