{"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\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":"2023-05-31T15:58:57.043754Z","iopub.execute_input":"2023-05-31T15:58:57.044203Z","iopub.status.idle":"2023-05-31T15:59:14.247010Z","shell.execute_reply.started":"2023-05-31T15:58:57.044171Z","shell.execute_reply":"2023-05-31T15:59:14.245414Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport os\nimport tensorflow as tf\nfrom sklearn.model_selection import train_test_split","metadata":{"execution":{"iopub.status.busy":"2023-05-31T15:59:49.256865Z","iopub.execute_input":"2023-05-31T15:59:49.257579Z","iopub.status.idle":"2023-05-31T15:59:49.262331Z","shell.execute_reply.started":"2023-05-31T15:59:49.257545Z","shell.execute_reply":"2023-05-31T15:59:49.261506Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load the data\ntrain_data = pd.read_csv('/kaggle/input/stylumiamnist/train.csv')\n\n# Prepare the data\nX = train_data['file_name']  # Image file names\ny = train_data['label']  # Class labels\n","metadata":{"execution":{"iopub.status.busy":"2023-05-31T16:00:37.118769Z","iopub.execute_input":"2023-05-31T16:00:37.119243Z","iopub.status.idle":"2023-05-31T16:00:37.173548Z","shell.execute_reply.started":"2023-05-31T16:00:37.119207Z","shell.execute_reply":"2023-05-31T16:00:37.172584Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Split the data into training and validation sets\nX_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2)\n","metadata":{"execution":{"iopub.status.busy":"2023-05-31T16:00:49.417110Z","iopub.execute_input":"2023-05-31T16:00:49.417560Z","iopub.status.idle":"2023-05-31T16:00:49.428364Z","shell.execute_reply.started":"2023-05-31T16:00:49.417528Z","shell.execute_reply":"2023-05-31T16:00:49.427051Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Define data augmentation and preprocessing\ntrain_datagen = tf.keras.preprocessing.image.ImageDataGenerator(\n    rescale=1./255,\n    rotation_range=0.2,\n    horizontal_flip=True\n)","metadata":{"execution":{"iopub.status.busy":"2023-05-31T16:01:03.370431Z","iopub.execute_input":"2023-05-31T16:01:03.371158Z","iopub.status.idle":"2023-05-31T16:01:03.376959Z","shell.execute_reply.started":"2023-05-31T16:01:03.371112Z","shell.execute_reply":"2023-05-31T16:01:03.375763Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_datagen = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1./255)\n\n","metadata":{"execution":{"iopub.status.busy":"2023-05-31T16:01:33.129609Z","iopub.execute_input":"2023-05-31T16:01:33.130031Z","iopub.status.idle":"2023-05-31T16:01:33.135709Z","shell.execute_reply.started":"2023-05-31T16:01:33.129999Z","shell.execute_reply":"2023-05-31T16:01:33.134362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Convert numeric labels to strings\ntrain_data['label'] = train_data['label'].astype(str)\n\n# Create the train generator\ntrain_generator = train_datagen.flow_from_dataframe(\n    dataframe=train_data,\n    x_col='file_name',\n    y_col='label',\n    directory='/kaggle/input/stylumiamnist/train',\n    shuffle=True,\n    target_size=(28, 28),\n    batch_size=32,\n    class_mode='sparse'\n)\n","metadata":{"execution":{"iopub.status.busy":"2023-05-31T16:03:27.674291Z","iopub.execute_input":"2023-05-31T16:03:27.674825Z","iopub.status.idle":"2023-05-31T16:04:37.848173Z","shell.execute_reply.started":"2023-05-31T16:03:27.674785Z","shell.execute_reply":"2023-05-31T16:04:37.847206Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nval_generator = val_datagen.flow_from_dataframe(\n    dataframe=train_data,\n    x_col=\"file_name\",\n    y_col=\"label\",\n    directory='/kaggle/input/stylumiamnist/train',\n    subset=\"validation\",  # Use the validation subset of the data\n    shuffle=True,\n    target_size=(28, 28),  # Update the target size if needed\n    batch_size=32,\n    class_mode='sparse'\n)","metadata":{"execution":{"iopub.status.busy":"2023-05-31T16:07:12.585634Z","iopub.execute_input":"2023-05-31T16:07:12.586062Z","iopub.status.idle":"2023-05-31T16:07:21.346896Z","shell.execute_reply.started":"2023-05-31T16:07:12.586032Z","shell.execute_reply":"2023-05-31T16:07:21.345843Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n# Define the CNN model architecture\nmodel = tf.keras.Sequential([\n    tf.keras.layers.Conv2D(32, 3, activation='relu', input_shape=(28, 28, 3)),\n    tf.keras.layers.MaxPooling2D(),\n    tf.keras.layers.Conv2D(64, 3, activation='relu'),\n    tf.keras.layers.MaxPooling2D(),\n    tf.keras.layers.Flatten(),\n    tf.keras.layers.Dense(128, activation='relu'),\n    tf.keras.layers.Dense(7, activation='softmax')\n])","metadata":{"execution":{"iopub.status.busy":"2023-05-31T16:07:26.166693Z","iopub.execute_input":"2023-05-31T16:07:26.167077Z","iopub.status.idle":"2023-05-31T16:07:26.240709Z","shell.execute_reply.started":"2023-05-31T16:07:26.167050Z","shell.execute_reply":"2023-05-31T16:07:26.239559Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Compile the model\nmodel.compile(optimizer='adam',\n              loss='sparse_categorical_crossentropy',\n              metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-05-31T16:07:29.935941Z","iopub.execute_input":"2023-05-31T16:07:29.936445Z","iopub.status.idle":"2023-05-31T16:07:29.950756Z","shell.execute_reply.started":"2023-05-31T16:07:29.936408Z","shell.execute_reply":"2023-05-31T16:07:29.949480Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Train the model\nhistory = model.fit(\n    train_generator,\n    validation_data=val_generator,\n    epochs=10\n)","metadata":{"execution":{"iopub.status.busy":"2023-05-31T16:07:30.586800Z","iopub.execute_input":"2023-05-31T16:07:30.587224Z","iopub.status.idle":"2023-05-31T16:31:27.966731Z","shell.execute_reply.started":"2023-05-31T16:07:30.587191Z","shell.execute_reply":"2023-05-31T16:31:27.965503Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}