{"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":"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)\nimport matplotlib.pyplot as plt\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-16T00:40:23.123341Z","iopub.execute_input":"2024-05-16T00:40:23.123704Z","iopub.status.idle":"2024-05-16T00:40:24.204403Z","shell.execute_reply.started":"2024-05-16T00:40:23.123667Z","shell.execute_reply":"2024-05-16T00:40:24.203255Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow import keras\nfashion_mnist=keras.datasets.fashion_mnist\n\nfrom keras.layers import Conv2D,MaxPool2D,Flatten,Dense\nfrom keras.models import Sequential\nfrom keras.utils import to_categorical","metadata":{"execution":{"iopub.status.busy":"2024-05-16T00:40:24.206329Z","iopub.execute_input":"2024-05-16T00:40:24.206921Z","iopub.status.idle":"2024-05-16T00:40:37.811552Z","shell.execute_reply.started":"2024-05-16T00:40:24.206873Z","shell.execute_reply":"2024-05-16T00:40:37.810400Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"28*28 픽셀로 이루어진 흑백 이미지 60,000장.  \n각 이미지는 10개의 class로 분류됨  \n0: T-shirt/top  \n1: Trouser  \n2: Pullover  \n3: Dress  \n4: Coat  \n5: Sandal  \n6: Shirt  \n7: Sneaker  \n8: Bag  \n9: Ankle boot  ","metadata":{}},{"cell_type":"code","source":"train_data = pd.read_csv(\"/kaggle/input/2024-smarcle-ks-3-fashion-mnist2/train.csv\")\ntest_data = pd.read_csv(\"/kaggle/input/2024-smarcle-ks-3-fashion-mnist2/test.csv\")\nprint(test_data.shape, train_data.shape)","metadata":{"execution":{"iopub.status.busy":"2024-05-16T00:40:37.812762Z","iopub.execute_input":"2024-05-16T00:40:37.813345Z","iopub.status.idle":"2024-05-16T00:40:44.179573Z","shell.execute_reply.started":"2024-05-16T00:40:37.813316Z","shell.execute_reply":"2024-05-16T00:40:44.178240Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"(train_image,train_label),(test_image,test_label)=fashion_mnist.load_data()","metadata":{"execution":{"iopub.status.busy":"2024-05-16T00:40:44.181899Z","iopub.execute_input":"2024-05-16T00:40:44.182252Z","iopub.status.idle":"2024-05-16T00:40:45.269997Z","shell.execute_reply.started":"2024-05-16T00:40:44.182225Z","shell.execute_reply":"2024-05-16T00:40:45.268841Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#클래스 네임 지정\nclass_names=[\n    'T-shirt/top',\n    'Trouser',\n    'Pullover',\n    'Dress',\n    'Coat',\n    'Sandal',\n    'Shirt',\n    'Sneaker',\n    'Bag',\n    'Ankle boot'\n]","metadata":{"execution":{"iopub.status.busy":"2024-05-16T00:40:45.271399Z","iopub.execute_input":"2024-05-16T00:40:45.273327Z","iopub.status.idle":"2024-05-16T00:40:45.277788Z","shell.execute_reply.started":"2024-05-16T00:40:45.273296Z","shell.execute_reply":"2024-05-16T00:40:45.277062Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure()\nplt.imshow(train_image[717])\nplt.colorbar()\nplt.grid(False)\nplt.show()\nprint(class_names[train_label[717]])","metadata":{"execution":{"iopub.status.busy":"2024-05-16T00:40:45.279061Z","iopub.execute_input":"2024-05-16T00:40:45.279825Z","iopub.status.idle":"2024-05-16T00:40:45.597790Z","shell.execute_reply.started":"2024-05-16T00:40:45.279798Z","shell.execute_reply":"2024-05-16T00:40:45.597017Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure()\nplt.imshow(train_image[1])\nplt.colorbar()\nplt.grid(False)\nplt.show()\nprint(class_names[train_label[1]])","metadata":{"execution":{"iopub.status.busy":"2024-05-16T00:40:45.598916Z","iopub.execute_input":"2024-05-16T00:40:45.599885Z","iopub.status.idle":"2024-05-16T00:40:45.854329Z","shell.execute_reply.started":"2024-05-16T00:40:45.599855Z","shell.execute_reply":"2024-05-16T00:40:45.853120Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.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    plt.imshow(train_image[i])\n    plt.xlabel(class_names[train_label[i]])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-16T00:40:45.856196Z","iopub.execute_input":"2024-05-16T00:40:45.856627Z","iopub.status.idle":"2024-05-16T00:40:47.140279Z","shell.execute_reply.started":"2024-05-16T00:40:45.856591Z","shell.execute_reply":"2024-05-16T00:40:47.139148Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"이미지와 라벨이 각 인덱스에 맞게 잘 저장이 되어있음을 확인.\n\n데이터가 이미지의 픽셀값(0~255)로 저장되어 있으니까 0과1 사이 값으로 정규화","metadata":{}},{"cell_type":"code","source":"train_image=train_image/255.0\ntest_image=test_image/255.0","metadata":{"execution":{"iopub.status.busy":"2024-05-16T00:40:47.141437Z","iopub.execute_input":"2024-05-16T00:40:47.141736Z","iopub.status.idle":"2024-05-16T00:40:47.338419Z","shell.execute_reply.started":"2024-05-16T00:40:47.141712Z","shell.execute_reply":"2024-05-16T00:40:47.337312Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_image=train_image.reshape(train_image.shape[0],28,28).astype('float32')\ntest_image=test_image.reshape(test_image.shape[0],28,28).astype('float')\ntrain_label=to_categorical(train_label)\ntest_label=to_categorical(test_label)","metadata":{"execution":{"iopub.status.busy":"2024-05-16T00:40:47.342166Z","iopub.execute_input":"2024-05-16T00:40:47.343151Z","iopub.status.idle":"2024-05-16T00:40:47.472884Z","shell.execute_reply.started":"2024-05-16T00:40:47.343117Z","shell.execute_reply":"2024-05-16T00:40:47.471797Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model=keras.Sequential([\n    Conv2D(32,(3,3),padding='same',strides=(1,1),activation='relu',input_shape=(28,28,1)),\n    MaxPool2D(pool_size=(2,2)),\n    Conv2D(64,(3,3),padding='same',strides=(1,1),activation='relu'),\n    MaxPool2D(pool_size=(2,2)),\n    Flatten(),\n    Dense(10,activation='softmax')\n])\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2024-05-16T00:40:47.474522Z","iopub.execute_input":"2024-05-16T00:40:47.474873Z","iopub.status.idle":"2024-05-16T00:40:47.581650Z","shell.execute_reply.started":"2024-05-16T00:40:47.474847Z","shell.execute_reply":"2024-05-16T00:40:47.580413Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='adam',loss='categorical_crossentropy',metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2024-05-16T00:40:47.585044Z","iopub.execute_input":"2024-05-16T00:40:47.585368Z","iopub.status.idle":"2024-05-16T00:40:47.603517Z","shell.execute_reply.started":"2024-05-16T00:40:47.585343Z","shell.execute_reply":"2024-05-16T00:40:47.602509Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(train_image,train_label,epochs=15)","metadata":{"execution":{"iopub.status.busy":"2024-05-16T00:40:47.605146Z","iopub.execute_input":"2024-05-16T00:40:47.605832Z","iopub.status.idle":"2024-05-16T00:49:54.794847Z","shell.execute_reply.started":"2024-05-16T00:40:47.605795Z","shell.execute_reply":"2024-05-16T00:49:54.794015Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction=model.predict(test_image)\n\nsubmission = pd.read_csv('/kaggle/input/2024-smarcle-ks-3-fashion-mnist2/sample_submission.csv')\n\nsubmission.to_csv(\"submission.csv\", index=False)\nprint(submission.head())","metadata":{"execution":{"iopub.status.busy":"2024-05-16T00:54:33.613306Z","iopub.execute_input":"2024-05-16T00:54:33.613715Z","iopub.status.idle":"2024-05-16T00:54:36.274586Z","shell.execute_reply.started":"2024-05-16T00:54:33.613683Z","shell.execute_reply":"2024-05-16T00:54:36.273398Z"},"trusted":true},"execution_count":null,"outputs":[]}]}