{"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":77476,"databundleVersionId":8462666,"sourceType":"competition"}],"dockerImageVersionId":30698,"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-05-15T21:54:59.606922Z","iopub.execute_input":"2024-05-15T21:54:59.607628Z","iopub.status.idle":"2024-05-15T21:54:59.957298Z","shell.execute_reply.started":"2024-05-15T21:54:59.607595Z","shell.execute_reply":"2024-05-15T21:54:59.956396Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Import the necessary libraries","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2024-05-15T21:56:41.200264Z","iopub.execute_input":"2024-05-15T21:56:41.200632Z","iopub.status.idle":"2024-05-15T21:56:41.614513Z","shell.execute_reply.started":"2024-05-15T21:56:41.200600Z","shell.execute_reply":"2024-05-15T21:56:41.613414Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Load dataframe","metadata":{}},{"cell_type":"code","source":"path = \"/kaggle/input/2024-smarcle-ks-3-fashion-mnist2/\"\n\nsample_df = pd.read_csv(path + \"sample_submission.csv\")\ntest_df = pd.read_csv(path + \"test.csv\")\ntrain_df = pd.read_csv(path + \"train.csv\")\n\nprint(test_df.shape, train_df.shape)","metadata":{"execution":{"iopub.status.busy":"2024-05-15T21:56:43.369243Z","iopub.execute_input":"2024-05-15T21:56:43.369755Z","iopub.status.idle":"2024-05-15T21:56:51.041633Z","shell.execute_reply.started":"2024-05-15T21:56:43.369725Z","shell.execute_reply":"2024-05-15T21:56:51.040634Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Explore data","metadata":{}},{"cell_type":"code","source":"# Extract pixel values and labels\npixels = train_df.drop('label', axis=1).values\nlabels = train_df['label'].values\n\n# Dictionary to store representative images for each label\nrepresentatives = {}\n\n# Find representative image for each label\nfor label in range(10):  # For labels 0 to 9\n    label_indices = np.where(labels == label)[0]  # Find indices corresponding to the label\n    representative_index = label_indices[0]  # Select the first index\n    representative_image = pixels[representative_index].reshape(28, 28)  # Convert to image\n    representatives[label] = representative_image  # Store representative image\n\n# Visualize representative images in a 3x3 grid\nfig, axes = plt.subplots(3, 3, figsize=(8, 8))\n\nfor i, ax in enumerate(axes.flat):\n    label = i  # Labels will be from 0 to 8 sequentially\n    image = representatives[label]\n    ax.imshow(image, cmap='gray')\n    ax.set_title(f\"Label: {label}\")\n    ax.axis('off')  # Hide axes\n\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-15T21:56:54.614662Z","iopub.execute_input":"2024-05-15T21:56:54.615337Z","iopub.status.idle":"2024-05-15T21:56:55.741766Z","shell.execute_reply.started":"2024-05-15T21:56:54.615303Z","shell.execute_reply":"2024-05-15T21:56:55.740734Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"|Label|category|\n|:---:|:--:|\n|0|T-shirt/top|\n|1|Trouser|\n|2|Pullover|\n|3|Dress|\n|4|Coat|\n|5|Sandal|\n|6|Shirt|\n|7|Sneaker|\n|8|Bag|\n|9|Ankle boot|","metadata":{}},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2024-05-15T21:56:59.629207Z","iopub.execute_input":"2024-05-15T21:56:59.629575Z","iopub.status.idle":"2024-05-15T21:56:59.655933Z","shell.execute_reply.started":"2024-05-15T21:56:59.629548Z","shell.execute_reply":"2024-05-15T21:56:59.654637Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\n# Calculate the frequency of each label\nlabel_counts = train_df['label'].value_counts()\n\n# Visualize the distribution of labels using a pie chart\nplt.figure(figsize=(8, 6))\nplt.pie(label_counts, labels=label_counts.index, autopct='%1.1f%%', startangle=140)\nplt.axis('equal')  # Make the pie chart appear round\nplt.title('Distribution of Labels in train_df')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-15T21:57:05.731172Z","iopub.execute_input":"2024-05-15T21:57:05.731556Z","iopub.status.idle":"2024-05-15T21:57:05.945323Z","shell.execute_reply.started":"2024-05-15T21:57:05.731524Z","shell.execute_reply":"2024-05-15T21:57:05.944109Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def normalize_dataframe(dataframe):\n    # Check if the label column exists\n    if 'label' in dataframe.columns:\n        # Extract pixel values and label column\n        pixels = dataframe.iloc[:, 1:].values\n        labels = dataframe['label'].values\n\n        # Normalize pixel values to range [0, 1]\n        normalized_pixels = pixels / 255.0\n\n        # Replace pixel values in dataframe using DataFrame.loc\n        dataframe.loc[:, dataframe.columns[1:]] = normalized_pixels.astype(float)\n\n        # Assign the label column back to the dataframe\n        dataframe['label'] = labels\n    else:\n        # Normalize all columns if the label column doesn't exist\n        dataframe = (dataframe - dataframe.min()) / (dataframe.max() - dataframe.min())\n\n    return dataframe\n\n# Normalize train_df, test_df, and sample_df\ntrain_df_normalized = normalize_dataframe(train_df)\ntest_df_normalized = normalize_dataframe(test_df)\n\ntrain_df_normalized.head()","metadata":{"_kg_hide-input":true,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2024-05-15T21:57:22.802458Z","iopub.execute_input":"2024-05-15T21:57:22.803376Z","iopub.status.idle":"2024-05-15T21:57:25.007614Z","shell.execute_reply.started":"2024-05-15T21:57:22.803341Z","shell.execute_reply":"2024-05-15T21:57:25.006507Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Modeling","metadata":{}},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras import layers\nfrom sklearn.model_selection import train_test_split\nimport kerastuner as kt\nfrom skimage.transform import resize","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2024-05-15T21:57:36.208143Z","iopub.execute_input":"2024-05-15T21:57:36.208563Z","iopub.status.idle":"2024-05-15T21:57:49.991410Z","shell.execute_reply.started":"2024-05-15T21:57:36.208530Z","shell.execute_reply":"2024-05-15T21:57:49.990256Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train_images와 test_images를 준비합니다.\ntrain_images = train_df_normalized.drop(columns=['label']).values.reshape(-1, 28, 28)  # 이미지 데이터를 28x28로 재구성\ntest_images = test_df_normalized.values.reshape(-1, 28, 28)    # 이미지 데이터를 28x28로 재구성\n\n# train_labels와 test_labels를 준비합니다.\ntrain_labels = train_df_normalized['label'].values\n\n# 데이터의 형태를 확인합니다.\nprint(\"Train Images shape:\", train_images.shape)\nprint(\"Test Images shape:\", test_images.shape)\nprint(\"Train Labels shape:\", train_labels.shape)","metadata":{"execution":{"iopub.status.busy":"2024-05-15T21:58:07.115516Z","iopub.execute_input":"2024-05-15T21:58:07.115943Z","iopub.status.idle":"2024-05-15T21:58:07.677313Z","shell.execute_reply.started":"2024-05-15T21:58:07.115909Z","shell.execute_reply":"2024-05-15T21:58:07.676229Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def preprocess_images(images):\n    resized_images = np.array([resize(image.reshape(28, 28), (32, 32)) for image in images])\n    final_images = np.repeat(resized_images[..., np.newaxis], 3, -1)\n    return final_images\n\n# 이미지 전처리\ntrain_images_final = preprocess_images(train_images)\ntest_images_final = preprocess_images(test_images)","metadata":{"execution":{"iopub.status.busy":"2024-05-15T21:58:09.439394Z","iopub.execute_input":"2024-05-15T21:58:09.440297Z","iopub.status.idle":"2024-05-15T21:58:22.225780Z","shell.execute_reply.started":"2024-05-15T21:58:09.440262Z","shell.execute_reply":"2024-05-15T21:58:22.224644Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def build_model(hp):\n    model = keras.Sequential()\n\n    # 입력 레이어\n    model.add(layers.Input(shape=(32, 32, 3)))\n\n    # CNN 레이어들\n    for i in range(hp.Int('num_layers', 1, 5)):\n        model.add(layers.Conv2D(\n            filters=hp.Int(f'conv_{i}_filters', 32, 256, step=32),\n            kernel_size=hp.Choice(f'conv_{i}_kernel', [3, 5]),\n            activation='relu',\n            padding='same'  # 패딩 추가\n        ))\n        model.add(layers.MaxPooling2D(pool_size=(2, 2)))\n        model.add(layers.Dropout(rate=hp.Float(f'conv_{i}_dropout', 0.0, 0.5, step=0.1)))\n\n    # 완전 연결 레이어\n    model.add(layers.Flatten())\n    model.add(layers.Dense(units=hp.Int('dense_units', 32, 512, step=32), activation='relu'))\n    model.add(layers.Dropout(rate=hp.Float('dense_dropout', 0.0, 0.5, step=0.1)))\n\n    # 출력 레이어\n    model.add(layers.Dense(10, activation='softmax'))\n\n    # 모델 컴파일\n    model.compile(optimizer=keras.optimizers.Adam(hp.Choice('learning_rate', [1e-2, 1e-3, 1e-4])),\n                  loss='sparse_categorical_crossentropy',\n                  metrics=['accuracy'])\n\n    return model\n\n# 하이퍼파라미터 튜닝을 위한 RandomSearch 튜너 생성\ntuner = kt.RandomSearch(\n    build_model,\n    objective='val_accuracy',\n    max_trials=10,\n    directory='my_dir',\n    project_name='cnn_hyperparameter_tuning'\n)\n\n# 하이퍼파라미터 튜닝 시작\ntuner.search(train_images_final, train_labels, epochs=10, validation_split=0.2)\n\n# 최적의 하이퍼파라미터 출력\nbest_hps = tuner.get_best_hyperparameters(num_trials=1)[0]\nprint(f\"\"\"\n최적의 CNN 레이어 개수: {best_hps.get('num_layers')}\n\"\"\")\n","metadata":{"execution":{"iopub.status.busy":"2024-05-15T21:58:36.905872Z","iopub.execute_input":"2024-05-15T21:58:36.906769Z","iopub.status.idle":"2024-05-15T22:16:01.127629Z","shell.execute_reply.started":"2024-05-15T21:58:36.906725Z","shell.execute_reply":"2024-05-15T22:16:01.126778Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\n# train 데이터를 train과 validation으로 나눔\ntrain_images_final, val_images_final, train_labels, val_labels = train_test_split(train_images_final, train_labels, test_size=0.2, random_state=42)\n\n# 최적의 모델 빌드\nmodel = tuner.hypermodel.build(best_hps)\n\n# 모델 훈련에는 train 데이터를 사용\nhistory = model.fit(train_images_final, train_labels, epochs=40, validation_data=(val_images_final, val_labels))\n\n# 테스트 데이터에 대한 예측 수행\npredictions = model.predict(test_images_final)","metadata":{"execution":{"iopub.status.busy":"2024-05-15T22:16:31.498795Z","iopub.execute_input":"2024-05-15T22:16:31.499144Z","iopub.status.idle":"2024-05-15T22:23:31.921169Z","shell.execute_reply.started":"2024-05-15T22:16:31.499120Z","shell.execute_reply":"2024-05-15T22:23:31.920213Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Prediction:\", predictions)","metadata":{"execution":{"iopub.status.busy":"2024-05-15T22:24:39.905762Z","iopub.execute_input":"2024-05-15T22:24:39.906358Z","iopub.status.idle":"2024-05-15T22:24:39.912136Z","shell.execute_reply.started":"2024-05-15T22:24:39.906321Z","shell.execute_reply":"2024-05-15T22:24:39.911084Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predicted_class = np.argmax(predictions, axis=1)\n\nprint(\"Predicted class:\", predicted_class)","metadata":{"execution":{"iopub.status.busy":"2024-05-15T22:24:44.179583Z","iopub.execute_input":"2024-05-15T22:24:44.180370Z","iopub.status.idle":"2024-05-15T22:24:44.185826Z","shell.execute_reply.started":"2024-05-15T22:24:44.180325Z","shell.execute_reply":"2024-05-15T22:24:44.185041Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_df['label'] = predicted_class\n\nsample_df.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2024-05-15T22:24:46.531554Z","iopub.execute_input":"2024-05-15T22:24:46.532145Z","iopub.status.idle":"2024-05-15T22:24:46.551061Z","shell.execute_reply.started":"2024-05-15T22:24:46.532114Z","shell.execute_reply":"2024-05-15T22:24:46.550351Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_df.head(10)","metadata":{"execution":{"iopub.status.busy":"2024-05-15T22:24:48.412187Z","iopub.execute_input":"2024-05-15T22:24:48.413035Z","iopub.status.idle":"2024-05-15T22:24:48.422209Z","shell.execute_reply.started":"2024-05-15T22:24:48.413003Z","shell.execute_reply":"2024-05-15T22:24:48.421093Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}