Datasets:
File size: 5,867 Bytes
a7859d1 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 | {
"cells": [
{
"cell_type": "markdown",
"id": "7c130e37-a029-4011-b741-14adb0bc15bb",
"metadata": {},
"source": [
"<img src=\"./figs/IOAI-Logo.png\" alt=\"IOAI Logo\" width=\"200\" height=\"auto\">\n",
"\n",
"[IOAI 2025 (Beijing, China), Individual Contest](https://ioai-official.org/china-2025)\n",
"\n",
"[](https://colab.research.google.com/github/IOAI-official/IOAI-2025/blob/main/Individual-Contest/Antique/Solution/Antique_Solution.ipynb)"
]
},
{
"cell_type": "markdown",
"id": "3ae71b15-8e97-4896-90a2-000c9cd6e683",
"metadata": {},
"source": [
"# Antique Painting Authentication: Reference Solution"
]
},
{
"cell_type": "markdown",
"id": "44bf0dce",
"metadata": {},
"source": [
"## Step 1: Train Your Model"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5bd6db06",
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import sys\n",
"\n",
"# 1. Get the current working directory\n",
"current_dir = os.getcwd()\n",
"\n",
"# 2. Check if the path contains \"Individual-Contest/Antique\" and trim it to that point\n",
"if \"Individual-Contest/Antique\" in current_dir:\n",
" root_index = current_dir.index(\"Individual-Contest/Antique\") + len(\"Individual-Contest/Antique\")\n",
" project_root = current_dir[:root_index]\n",
"else:\n",
" raise Exception(\"Project root directory not found. Please check the folder structure.\")\n",
"\n",
"# 3. Change working directory to the project root\n",
"os.chdir(project_root)\n",
"print(\"Working directory set to:\", os.getcwd())\n",
"\n",
"# 4. Add module search path (e.g., where metrics.py is located)\n",
"sys.path.append(os.path.join(project_root, \"Scoring\"))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "03dae883",
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"import numpy as np\n",
"from sklearn.cluster import SpectralClustering\n",
"from collections import Counter\n",
"from sklearn.svm import SVC\n",
"import os\n",
"\n",
"TRAIN_PATH = \"./training_set/\" # The address of trainig set\n",
"\n",
"train = pd.read_csv(TRAIN_PATH + \"training_set.csv\")\n",
"\n",
"X = np.array(train.iloc[:,:5])\n",
"y = np.array(train.iloc[:,5])\n",
"\n",
"labeled_mask = y != 0\n",
"unlabeled_mask = y == 0\n",
"X_labeled = X[labeled_mask]\n",
"y_labeled = y[labeled_mask]\n",
"X_unlabeled = X[unlabeled_mask]\n",
"\n",
"n_clusters = 2\n",
"spectral = SpectralClustering(n_clusters=n_clusters, affinity='rbf', gamma=10, random_state=42)\n",
"cluster_labels = spectral.fit_predict(X) \n",
"\n",
"cluster_to_label = {}\n",
"for cluster in range(n_clusters):\n",
"\n",
" labeled_in_cluster = y_labeled[cluster_labels[labeled_mask] == cluster]\n",
"\n",
" if len(labeled_in_cluster) > 0:\n",
" most_common_label = Counter(labeled_in_cluster).most_common(1)[0][0]\n",
" cluster_to_label[cluster] = most_common_label\n",
"\n",
"pseudo_labels = np.array([cluster_to_label[cluster] for cluster in cluster_labels])\n",
"\n",
"svm = SVC(kernel='rbf', C=1.0, gamma='scale', random_state=42)\n",
"svm.fit(X, pseudo_labels)"
]
},
{
"cell_type": "markdown",
"id": "a2049ba4",
"metadata": {},
"source": [
"## Step 2: Make Predictions on the Validation and Test Set"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c69d9d92",
"metadata": {},
"outputs": [],
"source": [
"VAL_DATA_PATH = \"./Solution/validation_set/\"\n",
"TEST_DATA_PATH = \"./Solution/test_set/\"\n",
"\n",
"testA = np.array(pd.read_csv(VAL_DATA_PATH + \"validation_set.csv\"))\n",
"testB = np.array(pd.read_csv(TEST_DATA_PATH + \"test_set.csv\"))\n",
"\n",
"predA = svm.predict(testA)\n",
"predB = svm.predict(testB)"
]
},
{
"cell_type": "markdown",
"id": "3e2141d8",
"metadata": {},
"source": [
"## Step 3: Generate `submission.zip` for Submission"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "342e6ddb",
"metadata": {},
"outputs": [],
"source": [
"import zipfile\n",
"import os\n",
"\n",
"submissionA = pd.DataFrame(predA)\n",
"submissionA.to_csv(\"./Scoring/submissionA.csv\", index=False, header=False)\n",
"\n",
"submissionB = pd.DataFrame(predB)\n",
"submissionB.to_csv(\"./Scoring/submissionB.csv\", index=False, header=False)\n",
"\n",
"files_to_zip = ['./Scoring/submissionA.csv', './Scoring/submissionB.csv']\n",
"zip_filename = './Scoring/submission.zip'\n",
"\n",
"with zipfile.ZipFile(zip_filename, 'w') as zipf:\n",
" for file in files_to_zip:\n",
" zipf.write(file, os.path.basename(file))\n",
"\n",
"print(f'{zip_filename} is created succefully!')"
]
},
{
"cell_type": "markdown",
"id": "e65766d9",
"metadata": {},
"source": [
"### Evaluate the Model Performance"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "04d9f1be",
"metadata": {},
"outputs": [],
"source": [
"%run Scoring/metrics.py"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.9"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|