{"metadata":{"kernelspec":{"name":"python3","display_name":"Python 3","language":"python"},"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":3649,"databundleVersionId":46718,"sourceType":"competition"}],"dockerImageVersionId":30699,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# CIFAR-10图像分类","metadata":{}},{"cell_type":"code","source":"import tensorflow as tf\nimport pandas as pd\nimport numpy as np","metadata":{"ExecuteTime":{"end_time":"2024-02-02T11:46:00.602933600Z","start_time":"2024-02-02T11:45:59.919204100Z"},"execution":{"iopub.status.busy":"2024-05-24T03:56:27.678182Z","iopub.execute_input":"2024-05-24T03:56:27.679192Z","iopub.status.idle":"2024-05-24T03:56:31.393535Z","shell.execute_reply.started":"2024-05-24T03:56:27.679149Z","shell.execute_reply":"2024-05-24T03:56:31.392541Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.random.seed(10)","metadata":{"execution":{"iopub.status.busy":"2024-05-24T03:56:31.395318Z","iopub.execute_input":"2024-05-24T03:56:31.395984Z","iopub.status.idle":"2024-05-24T03:56:31.400878Z","shell.execute_reply.started":"2024-05-24T03:56:31.395947Z","shell.execute_reply":"2024-05-24T03:56:31.399886Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\n\n# check out the dataset\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"execution":{"iopub.status.busy":"2024-05-24T03:56:31.402759Z","iopub.execute_input":"2024-05-24T03:56:31.403362Z","iopub.status.idle":"2024-05-24T03:56:31.418367Z","shell.execute_reply.started":"2024-05-24T03:56:31.403337Z","shell.execute_reply":"2024-05-24T03:56:31.417323Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 导入数据集","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.datasets import cifar10\n(x_img_train, y_label_train), (x_label_test, y_label_test) = cifar10.load_data()","metadata":{"execution":{"iopub.status.busy":"2024-05-24T03:56:31.420717Z","iopub.execute_input":"2024-05-24T03:56:31.421015Z","iopub.status.idle":"2024-05-24T03:56:32.242588Z","shell.execute_reply.started":"2024-05-24T03:56:31.420991Z","shell.execute_reply":"2024-05-24T03:56:32.241552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"train_data:\", \"images:\", x_img_train.shape, \" labels:\", y_label_train.shape)","metadata":{"execution":{"iopub.status.busy":"2024-05-24T03:56:32.243810Z","iopub.execute_input":"2024-05-24T03:56:32.244144Z","iopub.status.idle":"2024-05-24T03:56:32.249222Z","shell.execute_reply.started":"2024-05-24T03:56:32.244119Z","shell.execute_reply":"2024-05-24T03:56:32.248255Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"test_data:\", \"images:\", x_label_test.shape, \" labels:\", y_label_test.shape)","metadata":{"execution":{"iopub.status.busy":"2024-05-24T03:56:32.250493Z","iopub.execute_input":"2024-05-24T03:56:32.251348Z","iopub.status.idle":"2024-05-24T03:56:32.260245Z","shell.execute_reply.started":"2024-05-24T03:56:32.251317Z","shell.execute_reply":"2024-05-24T03:56:32.259305Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 观察数据类型","metadata":{}},{"cell_type":"code","source":"label_dict={0:\"airplane\",1:\"automobile\",2:\"bird\",3:\"cat\",4:\"deer\",\n            5:\"dog\",6:\"frog\",7:\"horse\",8:\"ship\",9:\"truck\"}","metadata":{"execution":{"iopub.status.busy":"2024-05-24T03:56:32.261380Z","iopub.execute_input":"2024-05-24T03:56:32.261646Z","iopub.status.idle":"2024-05-24T03:56:32.271028Z","shell.execute_reply.started":"2024-05-24T03:56:32.261625Z","shell.execute_reply":"2024-05-24T03:56:32.270135Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\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    plt.imshow(x_img_train[i], cmap=plt.cm.binary)\n    plt.xlabel(label_dict[y_label_train[i][0]])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-24T03:56:32.272221Z","iopub.execute_input":"2024-05-24T03:56:32.272789Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 对数据标准化","metadata":{}},{"cell_type":"code","source":"from tensorflow import keras\nfrom tensorflow.keras.utils import to_categorical","metadata":{"execution":{"iopub.status.busy":"2024-05-24T03:56:33.246870Z","iopub.execute_input":"2024-05-24T03:56:33.247239Z","iopub.status.idle":"2024-05-24T03:56:33.253049Z","shell.execute_reply.started":"2024-05-24T03:56:33.247207Z","shell.execute_reply":"2024-05-24T03:56:33.252107Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_img_train_normalize = x_img_train.astype('float32') / 255.0\nx_img_test_normalize = x_label_test.astype('float32') / 255.0","metadata":{"execution":{"iopub.status.busy":"2024-05-24T03:56:33.256183Z","iopub.execute_input":"2024-05-24T03:56:33.256516Z","iopub.status.idle":"2024-05-24T03:56:33.512979Z","shell.execute_reply.started":"2024-05-24T03:56:33.256492Z","shell.execute_reply":"2024-05-24T03:56:33.511965Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_label_train_OneHot = to_categorical(y_label_train)\ny_label_test_OneHot = to_categorical(y_label_test)","metadata":{"execution":{"iopub.status.busy":"2024-05-24T03:56:33.514338Z","iopub.execute_input":"2024-05-24T03:56:33.515214Z","iopub.status.idle":"2024-05-24T03:56:33.520719Z","shell.execute_reply.started":"2024-05-24T03:56:33.515175Z","shell.execute_reply":"2024-05-24T03:56:33.519937Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_label_test_OneHot.shape","metadata":{"execution":{"iopub.status.busy":"2024-05-24T03:56:33.521863Z","iopub.execute_input":"2024-05-24T03:56:33.522156Z","iopub.status.idle":"2024-05-24T03:56:33.530629Z","shell.execute_reply.started":"2024-05-24T03:56:33.522133Z","shell.execute_reply":"2024-05-24T03:56:33.529783Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 构建模型","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.models import Sequential\nfrom tensorflow.keras import layers\nfrom tensorflow.keras import preprocessing","metadata":{"execution":{"iopub.status.busy":"2024-05-24T03:56:33.531660Z","iopub.execute_input":"2024-05-24T03:56:33.531947Z","iopub.status.idle":"2024-05-24T03:56:33.539993Z","shell.execute_reply.started":"2024-05-24T03:56:33.531914Z","shell.execute_reply":"2024-05-24T03:56:33.539140Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential([\n    layers.Input(shape=[32, 32, 3], dtype=tf.float32, name=\"input_layer\"),\n    # data augmentation\n#     layers.RandomFlip('horizontal_and_vertical'),\n#     layers.RandomContrast(0.5),\n#     layers.RandomRotation(factor=0.2),\n    layers.Conv2D(filters=64, kernel_size=(3,3), \n                  input_shape=(32, 32, 3),\n                  activation='relu',\n                  padding='same'),\n    layers.BatchNormalization(),\n    layers.Conv2D(64, (3, 3), input_shape=(32, 32, 3),activation='relu'),\n    layers.BatchNormalization(),\n    layers.MaxPooling2D(pool_size=(2, 2)),\n    layers.Dropout(0.25),\n    \n    layers.Conv2D(filters=128, kernel_size=(3, 3), \n                  activation='relu',\n                  padding='same'),\n    layers.BatchNormalization(),\n    layers.Conv2D(filters=128, kernel_size=(3, 3), \n                  activation='relu',\n                  padding='same'),\n    layers.BatchNormalization(),\n    layers.MaxPooling2D(pool_size=(2, 2)),\n    layers.Dropout(0.25),\n    \n    layers.Flatten(),\n    layers.Dense(128, activation='relu'),\n    layers.Dropout(0.25),\n    layers.Dense(10, activation='softmax')\n])","metadata":{"execution":{"iopub.status.busy":"2024-05-24T03:56:33.541142Z","iopub.execute_input":"2024-05-24T03:56:33.541448Z","iopub.status.idle":"2024-05-24T03:56:34.072036Z","shell.execute_reply.started":"2024-05-24T03:56:33.541425Z","shell.execute_reply":"2024-05-24T03:56:34.071272Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2024-05-24T03:56:34.073345Z","iopub.execute_input":"2024-05-24T03:56:34.074135Z","iopub.status.idle":"2024-05-24T03:56:34.106668Z","shell.execute_reply.started":"2024-05-24T03:56:34.074097Z","shell.execute_reply":"2024-05-24T03:56:34.105796Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.optimizers import SGD\n\nmodel.compile(\n    loss='categorical_crossentropy',\n    optimizer=SGD(learning_rate=0.01, momentum=0.9),\n    metrics=['accuracy']\n)","metadata":{"execution":{"iopub.status.busy":"2024-05-24T03:56:34.107816Z","iopub.execute_input":"2024-05-24T03:56:34.108109Z","iopub.status.idle":"2024-05-24T03:56:34.123860Z","shell.execute_reply.started":"2024-05-24T03:56:34.108086Z","shell.execute_reply":"2024-05-24T03:56:34.122817Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.callbacks import EarlyStopping, ReduceLROnPlateau\n\nhistory=model.fit(\n    x_img_train_normalize, y_label_train_OneHot,\n    validation_split=0.2,\n    epochs=50,\n    batch_size=32,\n    callbacks=[\n#         ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=10),\n          EarlyStopping(\n              monitor='val_loss', patience=3,\n              min_delta=0.0005,\n              mode='min',\n              verbose=1\n          ),\n      ],\n      verbose=1\n)","metadata":{"execution":{"iopub.status.busy":"2024-05-24T03:56:34.124899Z","iopub.execute_input":"2024-05-24T03:56:34.125184Z","iopub.status.idle":"2024-05-24T03:59:13.462992Z","shell.execute_reply.started":"2024-05-24T03:56:34.125161Z","shell.execute_reply":"2024-05-24T03:59:13.462194Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history_frame = pd.DataFrame(history.history)","metadata":{"execution":{"iopub.status.busy":"2024-05-24T03:59:13.464351Z","iopub.execute_input":"2024-05-24T03:59:13.464636Z","iopub.status.idle":"2024-05-24T03:59:13.471599Z","shell.execute_reply.started":"2024-05-24T03:59:13.464610Z","shell.execute_reply":"2024-05-24T03:59:13.470834Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history_frame.loc[:, ['loss', 'val_loss']].plot()\nhistory_frame.loc[:, ['accuracy', 'val_accuracy']].plot();","metadata":{"execution":{"iopub.status.busy":"2024-05-24T03:59:13.472716Z","iopub.execute_input":"2024-05-24T03:59:13.473064Z","iopub.status.idle":"2024-05-24T03:59:13.911177Z","shell.execute_reply.started":"2024-05-24T03:59:13.473033Z","shell.execute_reply":"2024-05-24T03:59:13.910292Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_accuracy = model.evaluate(x_img_test_normalize,y_label_test_OneHot)\nprint(f\"Test Accuracy: {test_accuracy[1] * 100:.2f}%\")  ","metadata":{"execution":{"iopub.status.busy":"2024-05-24T03:59:13.912474Z","iopub.execute_input":"2024-05-24T03:59:13.913136Z","iopub.status.idle":"2024-05-24T03:59:15.004797Z","shell.execute_reply.started":"2024-05-24T03:59:13.913100Z","shell.execute_reply":"2024-05-24T03:59:15.003857Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub = pd.read_csv(\"../input/cifar-10/sampleSubmission.csv\")\n\ny_pred = np.argmax(model.predict(x_img_test_normalize),axis = 1) \n#convert to string label\nlabels = ['Airplane', 'Automobile', 'Bird', 'Cat', 'Deer', 'Dog', 'Frog', 'Horse', 'Ship', 'Truck']\nstr_pred = [labels[i] for i in y_pred]\nsub[\"label\"] = pd.DataFrame(str_pred)\n\nsub.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2024-05-24T03:59:15.006227Z","iopub.execute_input":"2024-05-24T03:59:15.007054Z","iopub.status.idle":"2024-05-24T03:59:17.450650Z","shell.execute_reply.started":"2024-05-24T03:59:15.007014Z","shell.execute_reply":"2024-05-24T03:59:17.449596Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}