{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.11","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":31041,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# 导入必要库\n!pip install py7zr\nimport os\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\nfrom tensorflow.keras import layers, models, callbacks\nfrom sklearn.model_selection import train_test_split\nfrom tqdm import tqdm\nimport cv2\nimport py7zr  # 用于解压.7z文件","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-06-20T07:05:41.770947Z","iopub.execute_input":"2025-06-20T07:05:41.771691Z","iopub.status.idle":"2025-06-20T07:05:44.732403Z","shell.execute_reply.started":"2025-06-20T07:05:41.771666Z","shell.execute_reply":"2025-06-20T07:05:44.731680Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# 解压数据集函数\ndef extract_7z(file_path, output_dir):\n    if not os.path.exists(output_dir):\n        os.makedirs(output_dir)\n    print(f\"Extracting {file_path} to {output_dir}...\")\n    with py7zr.SevenZipFile(file_path, mode='r') as z:\n        z.extractall(output_dir)\n    print(f\"Extraction complete. Files in {output_dir}: {len(os.listdir(output_dir))}\")\n\n# 解压数据集（在Kaggle环境中路径如下）\nextract_7z('/kaggle/input/cifar-10/train.7z', '/kaggle/working/train')\nextract_7z('/kaggle/input/cifar-10/test.7z', '/kaggle/working/test')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-20T07:05:44.734055Z","iopub.execute_input":"2025-06-20T07:05:44.734370Z","iopub.status.idle":"2025-06-20T07:08:12.456516Z","shell.execute_reply.started":"2025-06-20T07:05:44.734339Z","shell.execute_reply":"2025-06-20T07:08:12.455806Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import glob\n# 加载训练标签\ntrain_labels = pd.read_csv('/kaggle/input/cifar-10/trainLabels.csv')\nprint(f\"Loaded {len(train_labels)} training labels\")\nlabel_dict = dict(zip(train_labels['id'], train_labels['label']))\nclass_names = ['airplane', 'automobile', 'bird', 'cat', 'deer', \n               'dog', 'frog', 'horse', 'ship', 'truck']\nprint(\"Class names:\", class_names)\n\n# 加载图像数据的函数\ndef load_images(folder_path, label_dict=None):\n    # 获取文件夹中所有图像文件\n    image_files = glob.glob(os.path.join(folder_path, '*.png'))\n    print(f\"Found {len(image_files)} PNG files in {folder_path}\")\n    \n    if len(image_files) == 0:\n        raise ValueError(f\"No PNG files found in {folder_path}\")\n    \n    images = []\n    labels = [] if label_dict else None\n    ids = []\n    \n    for img_path in tqdm(image_files, desc=f\"Loading {os.path.basename(folder_path)} images\"):\n        img = cv2.imread(img_path)\n        if img is None:\n            print(f\"Warning: Could not read image {img_path}\")\n            continue\n            \n        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)  # 转换为RGB格式\n        images.append(img)\n        \n        if label_dict:\n            filename = os.path.basename(img_path)\n            img_id = int(os.path.splitext(filename)[0])\n            ids.append(img_id)\n            labels.append(label_dict.get(img_id, None))\n    \n    images = np.array(images)\n    print(f\"Successfully loaded {len(images)} images\")\n    \n    if label_dict:\n        return images, np.array(labels), ids\n    return images, None, [int(os.path.splitext(os.path.basename(f))[0]) for f in image_files]\n\n# 加载训练和测试数据\nprint(\"\\nLoading training images...\")\nX_train, y_train, train_ids = load_images('/kaggle/working/train/train', label_dict)\n\nprint(\"\\nLoading test images...\")\nX_test, _, test_ids = load_images('/kaggle/working/test/test')","metadata":{"execution":{"iopub.status.busy":"2025-06-20T07:12:37.656412Z","iopub.execute_input":"2025-06-20T07:12:37.656896Z","iopub.status.idle":"2025-06-20T07:13:13.913278Z","shell.execute_reply.started":"2025-06-20T07:12:37.656871Z","shell.execute_reply":"2025-06-20T07:13:13.912698Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# 数据预处理\ndef preprocess(images):\n    images = images.astype('float32') / 255.0\n    return images\n\nX_train = preprocess(X_train)\nX_test = preprocess(X_test)\nprint(f\"Training data shape: {X_train.shape}, Test data shape: {X_test.shape}\")\n\n# 标签编码\nlabel_to_index = {name: i for i, name in enumerate(class_names)}\ny_train_encoded = np.array([label_to_index[label] for label in y_train])\ny_train_onehot = tf.keras.utils.to_categorical(y_train_encoded, 10)\nprint(f\"Labels encoded. One-hot shape: {y_train_onehot.shape}\")\n\n# 划分验证集\nX_train, X_val, y_train, y_val = train_test_split(\n    X_train, y_train_onehot, test_size=0.2, random_state=42\n)\nprint(f\"Train set: {X_train.shape}, Validation set: {X_val.shape}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-20T07:13:47.759266Z","iopub.execute_input":"2025-06-20T07:13:47.759839Z","iopub.status.idle":"2025-06-20T07:13:50.001148Z","shell.execute_reply.started":"2025-06-20T07:13:47.759814Z","shell.execute_reply":"2025-06-20T07:13:50.000425Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# 构建增强型CNN模型\ndef create_enhanced_model():\n    model = models.Sequential()\n    \n    # 数据增强层\n    model.add(layers.RandomFlip(\"horizontal\", input_shape=(32, 32, 3)))\n    model.add(layers.RandomRotation(0.1))\n    model.add(layers.RandomZoom(0.1))\n    \n    # 卷积块1\n    model.add(layers.Conv2D(64, (3, 3), activation='relu', padding='same'))\n    model.add(layers.BatchNormalization())\n    model.add(layers.Conv2D(64, (3, 3), activation='relu', padding='same'))\n    model.add(layers.MaxPooling2D((2, 2)))\n    model.add(layers.Dropout(0.25))\n    \n    # 卷积块2\n    model.add(layers.Conv2D(128, (3, 3), activation='relu', padding='same'))\n    model.add(layers.BatchNormalization())\n    model.add(layers.Conv2D(128, (3, 3), activation='relu', padding='same'))\n    model.add(layers.MaxPooling2D((2, 2)))\n    model.add(layers.Dropout(0.35))\n    \n    # 卷积块3\n    model.add(layers.Conv2D(256, (3, 3), activation='relu', padding='same'))\n    model.add(layers.BatchNormalization())\n    model.add(layers.Conv2D(256, (3, 3), activation='relu', padding='same'))\n    model.add(layers.MaxPooling2D((2, 2)))\n    model.add(layers.Dropout(0.45))\n    \n    # 全连接层\n    model.add(layers.Flatten())\n    model.add(layers.Dense(512, activation='relu'))\n    model.add(layers.BatchNormalization())\n    model.add(layers.Dropout(0.5))\n    model.add(layers.Dense(256, activation='relu'))\n    model.add(layers.BatchNormalization())\n    model.add(layers.Dropout(0.5))\n    model.add(layers.Dense(10, activation='softmax'))\n    \n    # 优化器配置\n    optimizer = tf.keras.optimizers.Adam(learning_rate=0.001)\n    \n    model.compile(optimizer=optimizer,\n                 loss='categorical_crossentropy',\n                 metrics=['accuracy'])\n    return model\n\nmodel = create_enhanced_model()\nmodel.summary()\n\n# 设置回调函数\nearly_stopping = callbacks.EarlyStopping(\n    monitor='val_accuracy', patience=15, restore_best_weights=True, verbose=1\n)\nreduce_lr = callbacks.ReduceLROnPlateau(\n    monitor='val_loss', factor=0.5, patience=5, min_lr=1e-6, verbose=1\n)\nmodel_checkpoint = callbacks.ModelCheckpoint(\n    'best_model.h5', save_best_only=True, monitor='val_accuracy', mode='max'\n)\n\n# 训练模型\nprint(\"\\nStarting model training...\")\nhistory = model.fit(\n    X_train, y_train,\n    epochs=20,\n    batch_size=128,\n    validation_data=(X_val, y_val),\n    callbacks=[early_stopping, reduce_lr, model_checkpoint],\n    verbose=1\n)\n\n# 加载最佳模型\nmodel = models.load_model('best_model.h5')\nprint(\"Loaded best model for predictions\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-20T07:14:28.296846Z","iopub.execute_input":"2025-06-20T07:14:28.297494Z","iopub.status.idle":"2025-06-20T07:17:55.157032Z","shell.execute_reply.started":"2025-06-20T07:14:28.297468Z","shell.execute_reply":"2025-06-20T07:17:55.156291Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# 预测测试集\nprint(\"\\nPredicting on test set...\")\nprobabilities = model.predict(X_test, batch_size=128, verbose=1)\npredictions = np.argmax(probabilities, axis=1)\npredicted_labels = [class_names[idx] for idx in predictions]\n\n# 生成提交文件\nsubmission = pd.DataFrame({\n    'id': test_ids,\n    'label': predicted_labels\n})\nsubmission.to_csv('submission.csv', index=False)\nprint(f\"Submission file created with {len(submission)} predictions\")\n\n# 可视化训练过程\nplt.figure(figsize=(15, 5))\n\nplt.subplot(1, 2, 1)\nplt.plot(history.history['accuracy'], label='Train Accuracy')\nplt.plot(history.history['val_accuracy'], label='Validation Accuracy')\nplt.title('Training and Validation Accuracy')\nplt.xlabel('Epochs')\nplt.ylabel('Accuracy')\nplt.legend()\n\nplt.subplot(1, 2, 2)\nplt.plot(history.history['loss'], label='Train Loss')\nplt.plot(history.history['val_loss'], label='Validation Loss')\nplt.title('Training and Validation Loss')\nplt.xlabel('Epochs')\nplt.ylabel('Loss')\nplt.legend()\n\nplt.tight_layout()\nplt.savefig('training_history.png')\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-20T07:18:37.679620Z","iopub.execute_input":"2025-06-20T07:18:37.679911Z","iopub.status.idle":"2025-06-20T07:19:03.258350Z","shell.execute_reply.started":"2025-06-20T07:18:37.679882Z","shell.execute_reply":"2025-06-20T07:19:03.257638Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"左侧图：训练和验证的准确率（Accuracy）\n\n纵轴：准确率（Accuracy）\n横轴：训练轮数（Epochs）\n蓝线：训练准确率，显示模型在训练集上的表现，随着训练轮数增加，准确率逐渐提高，趋于稳定。\n黄线：验证准确率，显示模型在验证集上的表现，也逐步提高并逐渐趋于平稳。\n右侧图：训练和验证的损失（Loss）\n\n纵轴：损失值（Loss）\n横轴：训练轮数（Epochs）\n蓝线：训练损失，随着训练轮数增加，损失减小，说明模型逐渐拟合训练数据。\n黄线：验证损失，也逐渐减小，但可能会出现波动，显示模型在验证集上的表现。\n整体而言，模型在训练和验证集上的准确率逐步提高，损失逐步降低，表明模型训练效果良好，可能没有明显的过拟合或欠拟合。","metadata":{}}]}