{"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\nfrom sklearn.decomposition import PCA\nfrom sklearn.preprocessing import StandardScaler\nfrom tensorflow.keras.datasets import fashion_mnist\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-16T01:58:49.473121Z","iopub.execute_input":"2024-05-16T01:58:49.473640Z","iopub.status.idle":"2024-05-16T01:58:49.485349Z","shell.execute_reply.started":"2024-05-16T01:58:49.473601Z","shell.execute_reply":"2024-05-16T01:58:49.484068Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as n\nimport pandas as p\nimport seaborn as s\nimport matplotlib as m\nimport matplotlib.pyplot as pp\n\nsample=p.read_csv('/kaggle/input/2024-smarcle-ks-3-fashion-mnist2/sample_submission.csv')\ntest=p.read_csv(\"/kaggle/input/2024-smarcle-ks-3-fashion-mnist2/test.csv\")\ntrain=p.read_csv(\"/kaggle/input/2024-smarcle-ks-3-fashion-mnist2/train.csv\")\n\npp.style.use(\"ggplot\")\n\nprint(\"Setup finish\")","metadata":{"execution":{"iopub.status.busy":"2024-05-16T01:58:49.488613Z","iopub.execute_input":"2024-05-16T01:58:49.489172Z","iopub.status.idle":"2024-05-16T01:58:55.302453Z","shell.execute_reply.started":"2024-05-16T01:58:49.489130Z","shell.execute_reply":"2024-05-16T01:58:55.301340Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2024-05-16T01:58:55.304129Z","iopub.execute_input":"2024-05-16T01:58:55.304712Z","iopub.status.idle":"2024-05-16T01:58:55.322716Z","shell.execute_reply.started":"2024-05-16T01:58:55.304681Z","shell.execute_reply":"2024-05-16T01:58:55.321580Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.info()","metadata":{"execution":{"iopub.status.busy":"2024-05-16T01:58:55.325347Z","iopub.execute_input":"2024-05-16T01:58:55.325699Z","iopub.status.idle":"2024-05-16T01:58:55.379773Z","shell.execute_reply.started":"2024-05-16T01:58:55.325671Z","shell.execute_reply":"2024-05-16T01:58:55.378585Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.shape","metadata":{"execution":{"iopub.status.busy":"2024-05-16T01:58:55.381249Z","iopub.execute_input":"2024-05-16T01:58:55.381586Z","iopub.status.idle":"2024-05-16T01:58:55.387585Z","shell.execute_reply.started":"2024-05-16T01:58:55.381558Z","shell.execute_reply":"2024-05-16T01:58:55.386672Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.describe()","metadata":{"execution":{"iopub.status.busy":"2024-05-16T01:58:55.389622Z","iopub.execute_input":"2024-05-16T01:58:55.390031Z","iopub.status.idle":"2024-05-16T01:58:58.015494Z","shell.execute_reply.started":"2024-05-16T01:58:55.389984Z","shell.execute_reply":"2024-05-16T01:58:58.014339Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_img=train.drop(['label'], axis=1).values\ntrain_lbl=train['label'].values\ntrain_lb=train['label']\n\ntest_img=test.values\n\nprint(\"divided\")","metadata":{"execution":{"iopub.status.busy":"2024-05-16T01:58:58.017041Z","iopub.execute_input":"2024-05-16T01:58:58.017469Z","iopub.status.idle":"2024-05-16T01:58:58.135710Z","shell.execute_reply.started":"2024-05-16T01:58:58.017431Z","shell.execute_reply":"2024-05-16T01:58:58.134600Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_img.shape","metadata":{"execution":{"iopub.status.busy":"2024-05-16T01:58:58.137321Z","iopub.execute_input":"2024-05-16T01:58:58.137753Z","iopub.status.idle":"2024-05-16T01:58:58.144789Z","shell.execute_reply.started":"2024-05-16T01:58:58.137716Z","shell.execute_reply":"2024-05-16T01:58:58.143939Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_lb.shape\n","metadata":{"execution":{"iopub.status.busy":"2024-05-16T01:58:58.146104Z","iopub.execute_input":"2024-05-16T01:58:58.146412Z","iopub.status.idle":"2024-05-16T01:58:58.156852Z","shell.execute_reply.started":"2024-05-16T01:58:58.146365Z","shell.execute_reply":"2024-05-16T01:58:58.155825Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_img=train_img.reshape(-1, 28, 28)\ntest_img=test_img.reshape(-1, 28, 28)\nprint(train_img.shape, test_img.shape)","metadata":{"execution":{"iopub.status.busy":"2024-05-16T01:58:58.161129Z","iopub.execute_input":"2024-05-16T01:58:58.161466Z","iopub.status.idle":"2024-05-16T01:58:58.168259Z","shell.execute_reply.started":"2024-05-16T01:58:58.161439Z","shell.execute_reply":"2024-05-16T01:58:58.166971Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pp.imshow(train_img[0], cmap='gray')","metadata":{"execution":{"iopub.status.busy":"2024-05-16T01:59:56.528826Z","iopub.execute_input":"2024-05-16T01:59:56.530727Z","iopub.status.idle":"2024-05-16T01:59:56.788699Z","shell.execute_reply.started":"2024-05-16T01:59:56.530670Z","shell.execute_reply":"2024-05-16T01:59:56.787468Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pick=[0, 500, 1000, 1500, 2000, 2500, 3000, 3500, 4000, 4500, 5000, 5500, 6000, 6500, 7000, 7500, 8000, 8500, 9000, 9500]\nk=0\nfor i in pick:\n    pp.subplot(4,5,k+1)\n    pp.imshow(train_img[i], cmap='gray')\n    pp.title(train_lbl[i])\n    k+=1\n    \npp.tight_layout()\npp.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-16T02:00:03.056243Z","iopub.execute_input":"2024-05-16T02:00:03.056771Z","iopub.status.idle":"2024-05-16T02:00:06.307477Z","shell.execute_reply.started":"2024-05-16T02:00:03.056734Z","shell.execute_reply":"2024-05-16T02:00:06.306318Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_img2=train_img.reshape(60000, -1)\ntest_img2=test_img.reshape(10000, -1)\nprint(train_img2.shape, test_img2.shape)\n\nprint(train_img2[0])","metadata":{"execution":{"iopub.status.busy":"2024-05-16T02:00:10.152266Z","iopub.execute_input":"2024-05-16T02:00:10.153473Z","iopub.status.idle":"2024-05-16T02:00:10.166715Z","shell.execute_reply.started":"2024-05-16T02:00:10.153385Z","shell.execute_reply":"2024-05-16T02:00:10.165094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_img2=train_img2.astype('float64')\ntest_img2=test_img2.astype('float64')\ntrain_img2/=255\ntest_img2/=255\n\nprint(train_img2[0])","metadata":{"execution":{"iopub.status.busy":"2024-05-16T02:00:17.240526Z","iopub.execute_input":"2024-05-16T02:00:17.241004Z","iopub.status.idle":"2024-05-16T02:00:17.557868Z","shell.execute_reply.started":"2024-05-16T02:00:17.240969Z","shell.execute_reply":"2024-05-16T02:00:17.556225Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.decomposition import PCA\n\npca=PCA(n_components=0.95)\n\ntrain_img3=pca.fit_transform(train_img2)\n\nprint(train_img3.shape)","metadata":{"execution":{"iopub.status.busy":"2024-05-16T02:00:28.192031Z","iopub.execute_input":"2024-05-16T02:00:28.192518Z","iopub.status.idle":"2024-05-16T02:00:35.638371Z","shell.execute_reply.started":"2024-05-16T02:00:28.192479Z","shell.execute_reply":"2024-05-16T02:00:35.637258Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lbls=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n\n\npp.figure(figsize=(8, 6))\nscat=pp.scatter(train_img3[:, 0], train_img3[:, 1], c=train_lbl, cmap='inferno')\npp.legend(handles=scat.legend_elements()[0], labels=[lbls[i] for i in n.unique(train_lbl)])#범례 추가\npp.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-16T02:00:35.640279Z","iopub.execute_input":"2024-05-16T02:00:35.640716Z","iopub.status.idle":"2024-05-16T02:00:40.085284Z","shell.execute_reply.started":"2024-05-16T02:00:35.640686Z","shell.execute_reply":"2024-05-16T02:00:40.083610Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\ntrain_img2=train_img2.reshape(60000, 28, 28, 1)\ntest_img2=test_img2.reshape(10000, 28, 28, 1)\nprint(train_img2.shape)\n\ntrainx, train0x, trainy, train0y = train_test_split(train_img2, train_lb, test_size = 0.2, random_state = 1234)\nprint(trainx.shape, train0x.shape, trainy.shape, train0y.shape)","metadata":{"execution":{"iopub.status.busy":"2024-05-16T02:00:40.087619Z","iopub.execute_input":"2024-05-16T02:00:40.088375Z","iopub.status.idle":"2024-05-16T02:00:40.683724Z","shell.execute_reply.started":"2024-05-16T02:00:40.088330Z","shell.execute_reply":"2024-05-16T02:00:40.682557Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as t\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout\nfrom tensorflow.keras.utils import to_categorical\n\ntrainy = to_categorical(trainy)\ntrain0y = to_categorical(train0y)\n\n# CNN 모델 구성\nmodel = Sequential([\n    Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)),\n    MaxPooling2D((2, 2)),\n    Conv2D(64, (3, 3), activation='relu'),\n    MaxPooling2D((2, 2)),\n    Flatten(),\n    Dense(128, activation='relu'),\n    Dropout(0.5),  # 과적합 방지\n    Dense(10, activation='softmax')\n])\n\n# 모델 컴파일\nmodel.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])\n\n# 모델 학습\nhistory = model.fit(trainx, trainy, epochs=12, validation_data=(train0x, train0y))\n\n# 모델 평가\ntest_loss, test_acc = model.evaluate(train0x, train0y)\nprint('Test accuracy:', test_acc)\n\n# 학습 결과 시각화 (정확도)\npp.plot(history.history['accuracy'], label='accuracy')\npp.plot(history.history['val_accuracy'], label='val_accuracy')\npp.xlabel('Epoch')\npp.ylabel('Accuracy')\npp.legend(loc='lower right')\npp.show()\n\n# 학습 결과 시각화 (손실)\npp.plot(history.history['loss'], label='loss')\npp.plot(history.history['val_loss'], label='val_loss')\npp.xlabel('Epoch')\npp.ylabel('Loss')\npp.legend(loc='upper right')\npp.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-16T02:01:06.160187Z","iopub.execute_input":"2024-05-16T02:01:06.160686Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"---------------------------------------------------------------------","metadata":{}},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow import keras\nimport numpy as np\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2024-05-16T01:58:58.169734Z","iopub.execute_input":"2024-05-16T01:58:58.170053Z","iopub.status.idle":"2024-05-16T01:58:58.178211Z","shell.execute_reply.started":"2024-05-16T01:58:58.170026Z","shell.execute_reply":"2024-05-16T01:58:58.177447Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fashion_mnist = keras.datasets.fashion_mnist","metadata":{"execution":{"iopub.status.busy":"2024-05-16T01:58:58.179299Z","iopub.execute_input":"2024-05-16T01:58:58.179749Z","iopub.status.idle":"2024-05-16T01:58:58.188902Z","shell.execute_reply.started":"2024-05-16T01:58:58.179711Z","shell.execute_reply":"2024-05-16T01:58:58.187576Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"(train_image, train_label), (test_image, test_label) = fashion_mnist.load_data()\nprint(f'train Image Shape : {train_image.shape}')\nprint(f'test Image Shape : {test_image.shape}')\nprint(f'train Iabel Shape : {train_label.shape}')\nprint(f'test Iabel Shape : {test_label.shape}')","metadata":{"execution":{"iopub.status.busy":"2024-05-16T01:58:58.190168Z","iopub.execute_input":"2024-05-16T01:58:58.190507Z","iopub.status.idle":"2024-05-16T01:58:58.587439Z","shell.execute_reply.started":"2024-05-16T01:58:58.190478Z","shell.execute_reply":"2024-05-16T01:58:58.586150Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_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-16T01:58:58.589159Z","iopub.execute_input":"2024-05-16T01:58:58.589635Z","iopub.status.idle":"2024-05-16T01:58:58.596388Z","shell.execute_reply.started":"2024-05-16T01:58:58.589595Z","shell.execute_reply":"2024-05-16T01:58:58.595035Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure()\nplt.imshow(train_image[22330])\nplt.colorbar()\nplt.grid(False)\nplt.show()\nprint(class_names[train_label[22330]])","metadata":{"execution":{"iopub.status.busy":"2024-05-16T01:58:58.597771Z","iopub.execute_input":"2024-05-16T01:58:58.598201Z","iopub.status.idle":"2024-05-16T01:58:58.930783Z","shell.execute_reply.started":"2024-05-16T01:58:58.598162Z","shell.execute_reply":"2024-05-16T01:58:58.929972Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"더 많은 데이터를 확인하기 위해 훈련 세트에서 처음 25개의 사진을 확인해봅니다","metadata":{}},{"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], cmap=plt.cm.binary)\n  plt.xlabel(class_names[train_label[i]])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-16T01:58:58.931893Z","iopub.execute_input":"2024-05-16T01:58:58.932778Z","iopub.status.idle":"2024-05-16T01:59:00.448875Z","shell.execute_reply.started":"2024-05-16T01:58:58.932723Z","shell.execute_reply":"2024-05-16T01:59:00.448057Z"},"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-16T01:59:00.449993Z","iopub.execute_input":"2024-05-16T01:59:00.450879Z","iopub.status.idle":"2024-05-16T01:59:00.592259Z","shell.execute_reply.started":"2024-05-16T01:59:00.450846Z","shell.execute_reply":"2024-05-16T01:59:00.591245Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"이제 모델을 설계해봅시다.\n\n이번 시간에는 간단한 모델을 사용하고, 다음 글에서는 CNN을 사용하여 이미지를 분류해보겠습니다.","metadata":{}},{"cell_type":"code","source":"model = keras.Sequential([\n    keras.layers.Flatten(input_shape=(28,28)),\n    keras.layers.Dense(128, activation='relu'),\n    keras.layers.Dense(10, activation='softmax')\n])\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2024-05-16T01:59:00.593454Z","iopub.execute_input":"2024-05-16T01:59:00.593745Z","iopub.status.idle":"2024-05-16T01:59:00.638906Z","shell.execute_reply.started":"2024-05-16T01:59:00.593719Z","shell.execute_reply":"2024-05-16T01:59:00.637735Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"첫 번째로, Flatten 모델을 사용하여 28 x 28 의 데이터를 1차원으로 받아옵니다.\n\n다음으로 Dense 모델에 relu 함수를 사용하여 오차가 급격하게 줄어드는 기울기 소멸 문제를 완화합니다.\n\n마지막으로 총 10개의 class_names가 있으므로 출력층의 노드는 10개로 정해주며 각 노드의 결과값의 총 합이\n\n1이 되도록 하는 softmax를 사용합니다.\n\n \n\n다음으로 원핫인코딩을 사용하지 않아도 되며 정수값으로 분류가 가능한 sparse_categorical_entropy를 사용하여 model을 컴파일 해줍니다.","metadata":{}},{"cell_type":"code","source":"model.compile(optimizer = 'adam', loss = 'sparse_categorical_crossentropy', metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2024-05-16T01:59:00.640458Z","iopub.execute_input":"2024-05-16T01:59:00.640882Z","iopub.status.idle":"2024-05-16T01:59:00.651503Z","shell.execute_reply.started":"2024-05-16T01:59:00.640833Z","shell.execute_reply":"2024-05-16T01:59:00.650377Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"이제 모델 학습의 마지막 결과인 fit 를 사용하여 모델을 학습합니다.","metadata":{}},{"cell_type":"code","source":"model.fit(train_image, train_label, epochs=5)","metadata":{"execution":{"iopub.status.busy":"2024-05-16T01:59:00.652503Z","iopub.execute_input":"2024-05-16T01:59:00.652861Z","iopub.status.idle":"2024-05-16T01:59:25.462864Z","shell.execute_reply.started":"2024-05-16T01:59:00.652833Z","shell.execute_reply":"2024-05-16T01:59:25.461720Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"학습 결과를 확인해보겠습니다.","metadata":{}},{"cell_type":"code","source":"test_loss, test_acc = model.evaluate(test_image, test_label, verbose=2)","metadata":{"execution":{"iopub.status.busy":"2024-05-16T01:59:25.464126Z","iopub.execute_input":"2024-05-16T01:59:25.464470Z","iopub.status.idle":"2024-05-16T01:59:26.078422Z","shell.execute_reply.started":"2024-05-16T01:59:25.464434Z","shell.execute_reply":"2024-05-16T01:59:26.077280Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"epoch를 낮은 수치인 5로 설정하였기 때문에 정확도가 안좋지만, 적당한 시간으로 학습을 하면 성능이 많이 올라갈 수 있습니다.\n\n \n\n이제 마지막으로 예측을 해볼까요?","metadata":{}},{"cell_type":"code","source":"prediction = model.predict(test_image)\nplt.imshow(test_image[0])\nnp.argmax(prediction[0])\nprint(class_names[test_label[0]])","metadata":{"execution":{"iopub.status.busy":"2024-05-16T01:59:26.079923Z","iopub.execute_input":"2024-05-16T01:59:26.080248Z","iopub.status.idle":"2024-05-16T01:59:27.044365Z","shell.execute_reply.started":"2024-05-16T01:59:26.080220Z","shell.execute_reply":"2024-05-16T01:59:27.043195Z"},"trusted":true},"execution_count":null,"outputs":[]}]}