{"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":"none","dataSources":[{"sourceId":77476,"databundleVersionId":8462666,"sourceType":"competition"}],"dockerImageVersionId":30698,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# **0.setting**","metadata":{}},{"cell_type":"markdown","source":"해야할일: 패션 아이템을 분류하여야 함  \n카테고리:\n- 티셔츠/상의\n- 바지\n- 풀오버(머리부터 뒤집어써 입는 스웨터 등)\n- 드레스\n- 코트\n- 샌들\n- 셔츠\n- 운동화\n- 가방\n- 발목부츠","metadata":{}},{"cell_type":"code","source":"#라이브러리\nimport os\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\nfrom tensorflow.keras import Sequential\nfrom tensorflow.keras.layers import Flatten, Dense, Conv2D, MaxPooling2D, Dropout\nfrom tensorflow.keras.losses import SparseCategoricalCrossentropy\nfrom tensorflow.keras.utils import plot_model\n\nfrom sklearn.model_selection import train_test_split\n\nimport matplotlib.pyplot as plt\n%matplotlib inline","metadata":{"execution":{"iopub.status.busy":"2024-05-15T15:18:03.760216Z","iopub.execute_input":"2024-05-15T15:18:03.761064Z","iopub.status.idle":"2024-05-15T15:18:17.328228Z","shell.execute_reply.started":"2024-05-15T15:18:03.761031Z","shell.execute_reply":"2024-05-15T15:18:17.327144Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#시각화를 위한 함수 만들어주기\ndef predict_n_validate_individual_samples(index):\n    plt.figure()\n    plt.imshow(test_x[index].reshape(28, 28))\n    plt.xticks([])\n    plt.yticks([])\n    plt.grid(False)\n    plt.show()\n    print(\"Actual Value: \", class_names[test_y[index]])\n    print(\"Model Prediction: \", class_names[np.argmax(model.predict(test_x[index].reshape(1, 28, 28, 1)))])\n    \ndef plot_sample(df, index):\n    plt.figure()\n    plt.imshow(df[index].reshape(28, 28))\n    plt.colorbar()\n    plt.grid(False)\n    plt.show()\n","metadata":{"execution":{"iopub.status.busy":"2024-05-15T15:18:52.555575Z","iopub.execute_input":"2024-05-15T15:18:52.556419Z","iopub.status.idle":"2024-05-15T15:18:52.563437Z","shell.execute_reply.started":"2024-05-15T15:18:52.556385Z","shell.execute_reply":"2024-05-15T15:18:52.562139Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#데이터 불러오기\ntrain_images = pd.read_csv('/kaggle/input/2024-smarcle-ks-3-fashion-mnist2/train.csv')\ntest_images = pd.read_csv('/kaggle/input/2024-smarcle-ks-3-fashion-mnist2/test.csv')","metadata":{"execution":{"iopub.status.busy":"2024-05-15T15:19:51.758331Z","iopub.execute_input":"2024-05-15T15:19:51.758739Z","iopub.status.idle":"2024-05-15T15:19:56.475175Z","shell.execute_reply.started":"2024-05-15T15:19:51.758709Z","shell.execute_reply":"2024-05-15T15:19:56.474203Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#데이터 확인\ntrain_images.head()","metadata":{"execution":{"iopub.status.busy":"2024-05-15T15:20:12.166001Z","iopub.execute_input":"2024-05-15T15:20:12.166418Z","iopub.status.idle":"2024-05-15T15:20:12.184312Z","shell.execute_reply.started":"2024-05-15T15:20:12.166389Z","shell.execute_reply":"2024-05-15T15:20:12.183295Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_images.head()","metadata":{"execution":{"iopub.status.busy":"2024-05-15T15:20:13.823439Z","iopub.execute_input":"2024-05-15T15:20:13.823807Z","iopub.status.idle":"2024-05-15T15:20:13.842842Z","shell.execute_reply.started":"2024-05-15T15:20:13.823783Z","shell.execute_reply":"2024-05-15T15:20:13.841719Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train_images.shape)\nprint(test_images.shape)","metadata":{"execution":{"iopub.status.busy":"2024-05-15T15:20:15.404254Z","iopub.execute_input":"2024-05-15T15:20:15.404651Z","iopub.status.idle":"2024-05-15T15:20:15.410076Z","shell.execute_reply.started":"2024-05-15T15:20:15.404622Z","shell.execute_reply":"2024-05-15T15:20:15.409077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# ***1.EDA***","metadata":{}},{"cell_type":"code","source":"#bar로 확인하기\ntrain_images.label.value_counts().plot(kind='bar')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-15T15:20:58.163604Z","iopub.execute_input":"2024-05-15T15:20:58.163990Z","iopub.status.idle":"2024-05-15T15:20:58.428626Z","shell.execute_reply.started":"2024-05-15T15:20:58.163961Z","shell.execute_reply":"2024-05-15T15:20:58.427635Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#라벨 분리해주기\ntrain_x = train_images.iloc[:, 1:].values\ntrain_y = train_images.label.values\n\ntest_x = test_images.iloc[:, 1:].values","metadata":{"execution":{"iopub.status.busy":"2024-05-15T15:21:31.415442Z","iopub.execute_input":"2024-05-15T15:21:31.415805Z","iopub.status.idle":"2024-05-15T15:21:31.421519Z","shell.execute_reply.started":"2024-05-15T15:21:31.415781Z","shell.execute_reply":"2024-05-15T15:21:31.420226Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#각 클래스마다 명칭 할당해주기\nclass_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat',\n               'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']","metadata":{"execution":{"iopub.status.busy":"2024-05-15T15:24:28.656829Z","iopub.execute_input":"2024-05-15T15:24:28.657554Z","iopub.status.idle":"2024-05-15T15:24:28.662131Z","shell.execute_reply.started":"2024-05-15T15:24:28.657521Z","shell.execute_reply":"2024-05-15T15:24:28.661118Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#시각화\nplt.figure(figsize=(10,10))\nfor i in range(25): \n    plt.subplot(5,5,i+1) \n    plt.xticks([]) \n    plt.yticks([]) \n    plt.grid(False)\n    \n    plt.imshow(train_x[i].reshape(28, 28)) \n    plt.xlabel(class_names[train_y[i]]) \nplt.show() ","metadata":{"execution":{"iopub.status.busy":"2024-05-15T15:26:41.327848Z","iopub.execute_input":"2024-05-15T15:26:41.328243Z","iopub.status.idle":"2024-05-15T15:26:42.496088Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_sample(train_x, 0)","metadata":{"execution":{"iopub.status.busy":"2024-05-15T15:27:11.487089Z","iopub.execute_input":"2024-05-15T15:27:11.487496Z","iopub.status.idle":"2024-05-15T15:27:11.804069Z","shell.execute_reply.started":"2024-05-15T15:27:11.487466Z","shell.execute_reply":"2024-05-15T15:27:11.803117Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 더 보기 편하게 만들어주기\ntrain_x = train_x / 255.0\ntest_x = test_x / 255.0\n\nprint(\"Sample image after Scaling\")\nplot_sample(train_x, 0)","metadata":{"execution":{"iopub.status.busy":"2024-05-15T15:27:18.642071Z","iopub.execute_input":"2024-05-15T15:27:18.642463Z","iopub.status.idle":"2024-05-15T15:27:19.098411Z","shell.execute_reply.started":"2024-05-15T15:27:18.642435Z","shell.execute_reply":"2024-05-15T15:27:19.097146Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# ***2.Modeling***","metadata":{}},{"cell_type":"code","source":"#함수에 적용하기 위해서 split해주기\ntrain_x, val_x, train_y, val_y = train_test_split(train_x, train_y, test_size=0.2)","metadata":{"execution":{"iopub.status.busy":"2024-05-15T15:27:44.486520Z","iopub.execute_input":"2024-05-15T15:27:44.487340Z","iopub.status.idle":"2024-05-15T15:27:45.268914Z","shell.execute_reply.started":"2024-05-15T15:27:44.487305Z","shell.execute_reply":"2024-05-15T15:27:45.267676Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#keras Sequential 사용\nmodel = Sequential([\n    Flatten(),\n    Dense(128, activation='relu'),\n    Dense(10, activation='softmax') # 10 units as we have 10 output classes, softmax activation as we need the probability output\n])","metadata":{"execution":{"iopub.status.busy":"2024-05-15T15:30:13.252681Z","iopub.execute_input":"2024-05-15T15:30:13.253065Z","iopub.status.idle":"2024-05-15T15:30:13.264079Z","shell.execute_reply.started":"2024-05-15T15:30:13.253037Z","shell.execute_reply":"2024-05-15T15:30:13.263043Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='adam',\n              loss=SparseCategoricalCrossentropy(from_logits=True),\n              metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2024-05-15T15:30:20.788369Z","iopub.execute_input":"2024-05-15T15:30:20.788730Z","iopub.status.idle":"2024-05-15T15:30:20.831635Z","shell.execute_reply.started":"2024-05-15T15:30:20.788706Z","shell.execute_reply":"2024-05-15T15:30:20.830471Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#훈련해주기\nmodel1_history = model.fit(train_x.reshape(train_x.shape[0], 28, 28, 1), \n                    train_y, \n                    epochs=10,\n                    validation_data=(val_x.reshape(val_x.shape[0], 28, 28, 1), val_y),\n                   )","metadata":{"execution":{"iopub.status.busy":"2024-05-15T15:30:38.437147Z","iopub.execute_input":"2024-05-15T15:30:38.437557Z","iopub.status.idle":"2024-05-15T15:31:14.941450Z","shell.execute_reply.started":"2024-05-15T15:30:38.437531Z","shell.execute_reply":"2024-05-15T15:31:14.940523Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#모델링한 결과 확인해보기\nacc = model1_history.history['accuracy']\nval_acc = model1_history.history['val_accuracy']\nloss = model1_history.history['loss']\nval_loss = model1_history.history['val_loss']\n\nepochs = range(len(acc))\n\nplt.plot(epochs, acc, 'r', label='Training accuracy')\nplt.plot(epochs, val_acc, 'b', label='Validation accuracy')\nplt.title('Training and validation accuracy')\n\nplt.figure()\n\nplt.plot(epochs, loss, 'r', label='Training Loss')\nplt.plot(epochs, val_loss, 'b', label='Validation Loss')\nplt.title('Training and validation loss')\nplt.legend()\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-15T15:32:06.465061Z","iopub.execute_input":"2024-05-15T15:32:06.465953Z","iopub.status.idle":"2024-05-15T15:32:06.946692Z","shell.execute_reply.started":"2024-05-15T15:32:06.465917Z","shell.execute_reply":"2024-05-15T15:32:06.945512Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 모델 평가\nloss, accuracy = model.evaluate(val_x.reshape(val_x.shape[0], 28, 28, 1), val_y)\nprint(f'모델의 정확도는 {accuracy * 100:.2f}% 입니다.')","metadata":{"execution":{"iopub.status.busy":"2024-05-15T15:32:17.352146Z","iopub.execute_input":"2024-05-15T15:32:17.352559Z","iopub.status.idle":"2024-05-15T15:32:17.905686Z","shell.execute_reply.started":"2024-05-15T15:32:17.352531Z","shell.execute_reply":"2024-05-15T15:32:17.904578Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#데이터 형태확인\nprint(test_x.shape)","metadata":{"execution":{"iopub.status.busy":"2024-05-15T15:32:54.120062Z","iopub.execute_input":"2024-05-15T15:32:54.120491Z","iopub.status.idle":"2024-05-15T15:32:54.125914Z","shell.execute_reply.started":"2024-05-15T15:32:54.120460Z","shell.execute_reply":"2024-05-15T15:32:54.124830Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 테스트 데이터셋을 모델에 입력할 수 있는 형태로 변환\ntest_x_reshaped = np.pad(test_x, ((0, 0), (0, 1)), mode='constant', constant_values=0)\ntest_x_reshaped = test_x_reshaped.reshape(-1, 28, 28, 1)\n\n# 테스트 데이터셋에서 예측 수행\npredictions = model.predict(test_x_reshaped)\n\n# 예측 결과를 클래스 레이블로 변환\npredicted_labels = [np.argmax(prediction) for prediction in predictions]\n\n# 예측 결과를 DataFrame에 저장\nsubmission_df = pd.DataFrame({'ImageId': range(1, len(predicted_labels) + 1), 'Label': predicted_labels})\n\n# CSV 파일로 저장\nsubmission_df.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2024-05-15T15:49:42.639778Z","iopub.execute_input":"2024-05-15T15:49:42.640720Z","iopub.status.idle":"2024-05-15T15:49:43.294642Z","shell.execute_reply.started":"2024-05-15T15:49:42.640677Z","shell.execute_reply":"2024-05-15T15:49:43.293612Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"참고: https://www.kaggle.com/code/syammohan2103/fashion-mnist-a-beginner-s-guide/notebook","metadata":{}}]}