{"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":"gpu","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)\nimport seaborn as sns\nimport tensorflow as tf\nimport matplotlib.pyplot as plt\n%matplotlib inline\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-16T10:39:39.214127Z","iopub.execute_input":"2024-05-16T10:39:39.214477Z","iopub.status.idle":"2024-05-16T10:39:51.955131Z","shell.execute_reply.started":"2024-05-16T10:39:39.214445Z","shell.execute_reply":"2024-05-16T10:39:51.954218Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Classify the fashion items!**  \n\nThere are 10 categories.\n\n0 T-shirt/top\n\n1 Trouser\n\n2 Pullover\n\n3 Dress\n\n4 Coat\n\n5 Sandal\n\n6 Shirt\n\n7 Sneaker\n\n8 Bag\n\n9 Ankle boot","metadata":{}},{"cell_type":"code","source":"train_filepath = '/kaggle/input/2024-smarcle-ks-3-fashion-mnist2/train.csv'\ntrain_df = pd.read_csv(train_filepath)\n\ntest_filepath = '/kaggle/input/2024-smarcle-ks-3-fashion-mnist2/test.csv'\ntest_df = pd.read_csv(test_filepath)\n\nprint(train_df)","metadata":{"execution":{"iopub.status.busy":"2024-05-16T10:40:00.859211Z","iopub.execute_input":"2024-05-16T10:40:00.860063Z","iopub.status.idle":"2024-05-16T10:40:07.827611Z","shell.execute_reply.started":"2024-05-16T10:40:00.860030Z","shell.execute_reply":"2024-05-16T10:40:07.826635Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.shape","metadata":{"execution":{"iopub.status.busy":"2024-05-16T10:40:13.554024Z","iopub.execute_input":"2024-05-16T10:40:13.554370Z","iopub.status.idle":"2024-05-16T10:40:13.561189Z","shell.execute_reply.started":"2024-05-16T10:40:13.554342Z","shell.execute_reply":"2024-05-16T10:40:13.560241Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.shape","metadata":{"execution":{"iopub.status.busy":"2024-05-16T10:40:14.544523Z","iopub.execute_input":"2024-05-16T10:40:14.545213Z","iopub.status.idle":"2024-05-16T10:40:14.550834Z","shell.execute_reply.started":"2024-05-16T10:40:14.545180Z","shell.execute_reply":"2024-05-16T10:40:14.549883Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"https://www.tensorflow.org/api_docs/python/tf/keras/datasets/fashion_mnist/load_data?_gl=1*p3lql0*_up*MQ..*_ga*Nzc5NTE1NDgxLjE3MTUxODU2NzA.*_ga_W0YLR4190T*MTcxNTE4NTY3MC4xLjAuMTcxNTE4NTY3MC4wLjAuMA..\n\nTuple of NumPy arrays: (x_train, y_train), (x_test, y_test).\n\nx_train: uint8 NumPy array of grayscale image data with shapes (60000, 28, 28), containing the training data.\n\ny_train: uint8 NumPy array of labels (integers in range 0-9) with shape (60000,) for the training data.\n\nx_test: uint8 NumPy array of grayscale image data with shapes (10000, 28, 28), containing the test data.\n\ny_test: uint8 NumPy array of labels (integers in range 0-9) with shape (10000,) for the test data.","metadata":{}},{"cell_type":"markdown","source":"다른 자료로 형태 확인","metadata":{}},{"cell_type":"code","source":"(x_train, y_train), (x_test, y_test) = tf.keras.datasets.fashion_mnist.load_data()","metadata":{"execution":{"iopub.status.busy":"2024-05-16T10:40:16.356535Z","iopub.execute_input":"2024-05-16T10:40:16.357554Z","iopub.status.idle":"2024-05-16T10:40:17.112287Z","shell.execute_reply.started":"2024-05-16T10:40:16.357520Z","shell.execute_reply":"2024-05-16T10:40:17.111304Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def Check_MNIST_data():\n    (x_train, y_train), (x_test, y_test) = tf.keras.datasets.fashion_mnist.load_data()\n \n    print(x_train, '\\n','--'*2,'\\n' ,y_train)\n    print(x_train[0])  # 이미지데이터\n    print(y_train[0])  # 이미지의 숫자\n    print('x_train shape = ',x_train.shape)\n    print('y_train shape = ',y_train.shape)\n    print('x_test shape = ',x_test.shape)\n    print('y_test shape = ',y_test.shape)\n    plt.imshow(x_train[0])\n\nCheck_MNIST_data()","metadata":{"execution":{"iopub.status.busy":"2024-05-16T10:40:18.220135Z","iopub.execute_input":"2024-05-16T10:40:18.221016Z","iopub.status.idle":"2024-05-16T10:40:18.784410Z","shell.execute_reply.started":"2024-05-16T10:40:18.220982Z","shell.execute_reply":"2024-05-16T10:40:18.783516Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"train, test 데이터 변환","metadata":{}},{"cell_type":"code","source":"train_x = train_df.drop(['label'], axis=1).values\ntrain_y = train_df['label'].values\ntest_x = test_df.values\n\nprint(train_x.shape, test_x.shape)","metadata":{"execution":{"iopub.status.busy":"2024-05-16T10:40:20.454426Z","iopub.execute_input":"2024-05-16T10:40:20.455019Z","iopub.status.idle":"2024-05-16T10:40:20.571116Z","shell.execute_reply.started":"2024-05-16T10:40:20.454987Z","shell.execute_reply":"2024-05-16T10:40:20.570111Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_x = train_x.reshape(-1, 28, 28)\ntest_x = test_x.reshape(-1, 28, 28)\nprint(train_x.shape, test_x.shape)","metadata":{"execution":{"iopub.status.busy":"2024-05-16T10:40:21.385350Z","iopub.execute_input":"2024-05-16T10:40:21.385720Z","iopub.status.idle":"2024-05-16T10:40:21.391266Z","shell.execute_reply.started":"2024-05-16T10:40:21.385692Z","shell.execute_reply":"2024-05-16T10:40:21.390348Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label = {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'}\n\n# 픽셀값 범위 확인\nmin_train = np.min(train_x)\nmax_train = np.max(train_x)\n\nmin_test = np.min(test_x)\nmax_test = np.max(test_x)\n\nprint(min_train, max_train)\nprint(min_test, max_test)","metadata":{"execution":{"iopub.status.busy":"2024-05-16T10:40:22.115954Z","iopub.execute_input":"2024-05-16T10:40:22.117258Z","iopub.status.idle":"2024-05-16T10:40:22.200757Z","shell.execute_reply.started":"2024-05-16T10:40:22.117208Z","shell.execute_reply":"2024-05-16T10:40:22.199785Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_x = train_x.astype('float64')\ntest_x = test_x.astype('float64') \ntrain_x = train_x / 255\ntest_x = test_x / 255","metadata":{"execution":{"iopub.status.busy":"2024-05-16T10:40:22.864144Z","iopub.execute_input":"2024-05-16T10:40:22.864466Z","iopub.status.idle":"2024-05-16T10:40:23.131774Z","shell.execute_reply.started":"2024-05-16T10:40:22.864441Z","shell.execute_reply":"2024-05-16T10:40:23.130981Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train_x[0])","metadata":{"execution":{"iopub.status.busy":"2024-05-16T10:40:23.894488Z","iopub.execute_input":"2024-05-16T10:40:23.895326Z","iopub.status.idle":"2024-05-16T10:40:23.907302Z","shell.execute_reply.started":"2024-05-16T10:40:23.895295Z","shell.execute_reply":"2024-05-16T10:40:23.906382Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 라벨 확인\nplt.figure(figsize=(10,10))\n\nfor i in range(20):\n    plt.subplot(4,5,i+1)\n    plt.xlabel(label[train_y[i]])\n    plt.imshow(train_x[i])","metadata":{"execution":{"iopub.status.busy":"2024-05-16T10:40:24.908533Z","iopub.execute_input":"2024-05-16T10:40:24.909237Z","iopub.status.idle":"2024-05-16T10:40:27.391222Z","shell.execute_reply.started":"2024-05-16T10:40:24.909201Z","shell.execute_reply":"2024-05-16T10:40:27.390306Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"cnn","metadata":{}},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.keras import datasets, layers, models","metadata":{"execution":{"iopub.status.busy":"2024-05-16T10:40:29.609706Z","iopub.execute_input":"2024-05-16T10:40:29.610521Z","iopub.status.idle":"2024-05-16T10:40:29.615456Z","shell.execute_reply.started":"2024-05-16T10:40:29.610489Z","shell.execute_reply":"2024-05-16T10:40:29.614534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_x = train_x.reshape((60000, 28, 28, 1)).astype('float32') / 255\ntest_x = test_x.reshape((10000, 28, 28, 1)).astype('float32') / 255","metadata":{"execution":{"iopub.status.busy":"2024-05-16T10:40:30.079066Z","iopub.execute_input":"2024-05-16T10:40:30.079960Z","iopub.status.idle":"2024-05-16T10:40:30.187481Z","shell.execute_reply.started":"2024-05-16T10:40:30.079928Z","shell.execute_reply":"2024-05-16T10:40:30.186468Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = models.Sequential()\nmodel.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)))\nmodel.add(layers.MaxPooling2D((2, 2)))\nmodel.add(layers.Conv2D(64, (3, 3), activation='relu'))\nmodel.add(layers.MaxPooling2D((2, 2)))\nmodel.add(layers.Conv2D(64, (3, 3), activation='relu'))","metadata":{"execution":{"iopub.status.busy":"2024-05-16T10:40:31.043156Z","iopub.execute_input":"2024-05-16T10:40:31.044133Z","iopub.status.idle":"2024-05-16T10:40:31.719527Z","shell.execute_reply.started":"2024-05-16T10:40:31.044098Z","shell.execute_reply":"2024-05-16T10:40:31.718741Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 완전 연결 계층 추가\nmodel.add(layers.Flatten())\nmodel.add(layers.Dense(64, activation='relu'))\nmodel.add(layers.Dense(10))\n\n# 모델 컴파일\nmodel.compile(optimizer='adam',\n              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),\n              metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2024-05-16T10:40:32.989215Z","iopub.execute_input":"2024-05-16T10:40:32.990041Z","iopub.status.idle":"2024-05-16T10:40:33.039328Z","shell.execute_reply.started":"2024-05-16T10:40:32.990006Z","shell.execute_reply":"2024-05-16T10:40:33.038545Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 모델 요약 출력\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2024-05-16T10:40:33.973406Z","iopub.execute_input":"2024-05-16T10:40:33.974157Z","iopub.status.idle":"2024-05-16T10:40:33.998238Z","shell.execute_reply.started":"2024-05-16T10:40:33.974116Z","shell.execute_reply":"2024-05-16T10:40:33.997349Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"epochs: 입력 데이터 학습 횟수   \nbatch_size: 학습할 때 사용되는 데이터 수   \nvalidation_data: 검증 데이터로 하나의 epoch가 끝날 때마다 모델을 평가해줌   \nverbose: 학습 중 출력되는 로그 수준 (0, 1, 2)\n\nvalidation_data를 따로 만들거나, 학습횟수를 늘리면 정확도가 늘어날 것으로 예상","metadata":{}},{"cell_type":"markdown","source":"validation_data를 만들었으나, 정확도가 떨어져서 다른 코드 참고하여 다시 작성\n","metadata":{}},{"cell_type":"code","source":"'''\nfrom sklearn.model_selection import train_test_split\n\ny = train_y\nX = train_x\n\ntrain_X1, test_X1, train_y1, test_y1 = train_test_split(X, y,test_size=0.3 ,random_state = 100)\n'''","metadata":{"execution":{"iopub.status.busy":"2024-05-16T10:40:36.264910Z","iopub.execute_input":"2024-05-16T10:40:36.265521Z","iopub.status.idle":"2024-05-16T10:40:36.271788Z","shell.execute_reply.started":"2024-05-16T10:40:36.265488Z","shell.execute_reply":"2024-05-16T10:40:36.270798Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(x=train_x,\n                    y=train_y,\n                    validation_split=0.15,\n                    epochs=20,\n                   )","metadata":{"execution":{"iopub.status.busy":"2024-05-16T10:40:39.215630Z","iopub.execute_input":"2024-05-16T10:40:39.216508Z","iopub.status.idle":"2024-05-16T10:42:04.913477Z","shell.execute_reply.started":"2024-05-16T10:40:39.216478Z","shell.execute_reply":"2024-05-16T10:42:04.912620Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 모델 학습\npredictions_cnn = model.predict(test_x)\n\nprint(predictions_cnn)","metadata":{"execution":{"iopub.status.busy":"2024-05-16T10:42:20.385872Z","iopub.execute_input":"2024-05-16T10:42:20.386706Z","iopub.status.idle":"2024-05-16T10:42:22.057743Z","shell.execute_reply.started":"2024-05-16T10:42:20.386674Z","shell.execute_reply":"2024-05-16T10:42:22.056647Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 가장 높은 확률의 예측값\npred_cnn = np.argmax(predictions_cnn, axis=1)\n\nprint(pred_cnn)","metadata":{"execution":{"iopub.status.busy":"2024-05-16T10:42:22.059488Z","iopub.execute_input":"2024-05-16T10:42:22.059816Z","iopub.status.idle":"2024-05-16T10:42:22.065497Z","shell.execute_reply.started":"2024-05-16T10:42:22.059789Z","shell.execute_reply":"2024-05-16T10:42:22.064596Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 라벨 확인\nplt.figure(figsize=(10,10))\n\nfor i in range(20):\n    plt.subplot(4,5,i+1)\n    plt.xlabel(label[pred_cnn[i]])\n    plt.imshow(test_x[i])","metadata":{"execution":{"iopub.status.busy":"2024-05-16T10:42:22.935257Z","iopub.execute_input":"2024-05-16T10:42:22.936029Z","iopub.status.idle":"2024-05-16T10:42:25.270552Z","shell.execute_reply.started":"2024-05-16T10:42:22.935999Z","shell.execute_reply":"2024-05-16T10:42:25.269640Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub = pd.read_csv('/kaggle/input/2024-smarcle-ks-3-fashion-mnist2/sample_submission.csv')\nsub","metadata":{"execution":{"iopub.status.busy":"2024-05-16T10:42:25.272332Z","iopub.execute_input":"2024-05-16T10:42:25.272806Z","iopub.status.idle":"2024-05-16T10:42:25.294880Z","shell.execute_reply.started":"2024-05-16T10:42:25.272770Z","shell.execute_reply":"2024-05-16T10:42:25.294028Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submit = pd.DataFrame({'ImageId' : sub['ImageId'], 'label' : pred_cnn})","metadata":{"execution":{"iopub.status.busy":"2024-05-16T10:42:25.309327Z","iopub.execute_input":"2024-05-16T10:42:25.309613Z","iopub.status.idle":"2024-05-16T10:42:25.314012Z","shell.execute_reply.started":"2024-05-16T10:42:25.309581Z","shell.execute_reply":"2024-05-16T10:42:25.313171Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submit.to_csv(\"submission_cnn.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2024-05-16T10:42:26.827598Z","iopub.execute_input":"2024-05-16T10:42:26.827945Z","iopub.status.idle":"2024-05-16T10:42:26.846677Z","shell.execute_reply.started":"2024-05-16T10:42:26.827921Z","shell.execute_reply":"2024-05-16T10:42:26.845745Z"},"trusted":true},"execution_count":null,"outputs":[]}]}