{"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":5048,"databundleVersionId":868335,"sourceType":"competition"},{"sourceId":7706955,"sourceType":"datasetVersion","datasetId":4499671},{"sourceId":8771495,"sourceType":"datasetVersion","datasetId":5271235},{"sourceId":9413079,"sourceType":"datasetVersion","datasetId":5716419},{"sourceId":9413110,"sourceType":"datasetVersion","datasetId":5716446},{"sourceId":9413641,"sourceType":"datasetVersion","datasetId":5716841},{"sourceId":9434902,"sourceType":"datasetVersion","datasetId":5732581},{"sourceId":9435213,"sourceType":"datasetVersion","datasetId":5732830},{"sourceId":9435228,"sourceType":"datasetVersion","datasetId":5732842},{"sourceId":9436088,"sourceType":"datasetVersion","datasetId":5733503},{"sourceId":9436162,"sourceType":"datasetVersion","datasetId":5733562},{"sourceId":9436193,"sourceType":"datasetVersion","datasetId":5733587}],"dockerImageVersionId":30733,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# นำเข้าไลบรารีต่างๆ\nfrom PIL import Image\nimport torch\nimport os\nimport cv2\nfrom tqdm import tqdm\nimport copy\nimport torch.optim as optim\nimport numpy as np\nimport optuna\nfrom sklearn.metrics import f1_score\nfrom optuna.exceptions import TrialPruned\nimport random\nimport torch.nn as nn\nfrom torchvision import datasets, models, transforms\nfrom torch.utils.data import DataLoader, Dataset\nimport pandas as pd\nfrom skimage import io\nfrom tensorflow.keras.callbacks import ModelCheckpoint, EarlyStopping\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.models import load_model\nimport torchvision.transforms.functional as F\nfrom torchvision.datasets import ImageFolder\nfrom sklearn.model_selection import train_test_split\nprint('import complete')","metadata":{"execution":{"iopub.status.busy":"2024-09-19T14:56:38.014132Z","iopub.execute_input":"2024-09-19T14:56:38.014404Z","iopub.status.idle":"2024-09-19T14:56:38.023011Z","shell.execute_reply.started":"2024-09-19T14:56:38.014377Z","shell.execute_reply":"2024-09-19T14:56:38.02143Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# กำหนดตำแหน่งโฟลเดอร์ที่เก็บข้อมูลรูปภาพและไฟล์ CSV ที่มีการบันทึกข้อมูลเกี่ยวกับการฝึก (training data)\nworking_folder = os.path.abspath(\"\") \nimage_dir = os.path.join(working_folder, \"/kaggle/input/all-images/data_all\")\ntrain_df = '/kaggle/input/trainfull/train_full.csv'\nprint('complete')","metadata":{"execution":{"iopub.status.busy":"2024-09-19T14:56:39.267725Z","iopub.execute_input":"2024-09-19T14:56:39.268473Z","iopub.status.idle":"2024-09-19T14:56:39.273837Z","shell.execute_reply.started":"2024-09-19T14:56:39.268441Z","shell.execute_reply":"2024-09-19T14:56:39.27283Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class CustomImageDataset(Dataset):                                   #สร้างคลาส CustomImageDataset ที่ใช้ในการอ่านข้อมูลภาพและรหัสC0-9จากไฟล์ CSV\n    def __init__(self, csv_file, root_dir, transform):\n        self.annotations = pd.read_csv(csv_file)\n        self.root_dir = root_dir\n        self.transform = transform\n\n    def __len__(self):\n        return len(self.annotations)\n\n    def __getitem__(self, index):\n        img_id = self.annotations.iloc[index, 1]\n        img_name = os.path.join(self.root_dir, f\"{img_id}\")\n        image = Image.open(img_name).convert('RGB')\n        label = int(self.annotations.iloc[index, 0][-1])  \n\n        if self.transform:\n            image = self.transform(image)\n\n        return image, label\n    \nclass FlipT: # flip ภาพในแนวนอนแบบไม่Random\n    def __init__(self):\n        pass\n\n    def __call__(self, x):\n        x = F.hflip(x)  \n        return x\n    \ntransform_train = transforms.Compose([                                     #การย่อขนาด, การสุ่มพลิกแนวนอน, แปลงเป็น tensor, และ normalize ค่าพิกเซล.\n    transforms.Resize((224, 224)),\n    transforms.RandomHorizontalFlip(),\n    transforms.ToTensor(),\n    transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),\n])\ntransform_test1 = transforms.Compose([                                     \n    transforms.Resize((224, 224)),\n    transforms.ToTensor(),\n    transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),\n])\ntransform_test2 = transforms.Compose([                                     \n    transforms.Resize((224, 224)),\n    FlipT(),\n    transforms.ToTensor(),\n    transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),\n])\nprint('dataset complete')","metadata":{"execution":{"iopub.status.busy":"2024-09-19T14:56:41.162633Z","iopub.execute_input":"2024-09-19T14:56:41.163369Z","iopub.status.idle":"2024-09-19T14:56:41.176368Z","shell.execute_reply.started":"2024-09-19T14:56:41.163336Z","shell.execute_reply":"2024-09-19T14:56:41.175241Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class SingleFolderDataset(Dataset):                                   #สร้างคลาส SingleFolderDataset ที่ใช้ในการอ่านภาพจากโฟลเดอร์เดียว\n    def __init__(self, directory, transform):\n        self.directory = directory\n        self.transform = transform\n        self.image_filenames = [f for f in os.listdir(directory) if os.path.isfile(os.path.join(directory, f))]\n\n    def __len__(self):\n        return len(self.image_filenames)\n\n    def __getitem__(self, idx):\n        image_name = self.image_filenames[idx]\n        image_path = os.path.join(self.directory, image_name)\n        image = Image.open(image_path).convert('RGB')\n        if self.transform:\n            image = self.transform(image)\n        return image, image_name\n\ndef make_predictions_sigmoid(data_loader, model, device):\n    model.eval()\n    y_pred = []\n    file_names = []\n    with torch.no_grad():\n        for inputs, paths in data_loader:\n            inputs = inputs.to(device)\n            outputs = model(inputs)\n            probabilities = torch.nn.functional.softmax(outputs, dim=1)\n            y_pred.extend(probabilities.cpu().numpy())\n            file_names.extend([os.path.basename(path) for path in paths])\n    return file_names, y_pred\nprint('complete')","metadata":{"execution":{"iopub.status.busy":"2024-09-19T14:56:45.705989Z","iopub.execute_input":"2024-09-19T14:56:45.706861Z","iopub.status.idle":"2024-09-19T14:56:45.717431Z","shell.execute_reply.started":"2024-09-19T14:56:45.706827Z","shell.execute_reply":"2024-09-19T14:56:45.716345Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class CustomDINONormModel(nn.Module): #สร้างโมเดลที่นำ DINO มาประยุกต์ใช้ \n    def __init__(self, dino_model, fc_units, dropout_rate):\n        super(CustomDINONormModel, self).__init__()\n        self.dino_model = dino_model\n        self.classifier = nn.Sequential(\n            nn.Linear(1024, fc_units),\n            nn.ReLU(),\n            \n            nn.Dropout(dropout_rate),\n            nn.Linear(fc_units, 10),\n        )\n\n    def forward(self, x):\n        x = self.dino_model(x)\n        x = self.classifier(x)\n        return x\nprint('dino complete')","metadata":{"execution":{"iopub.status.busy":"2024-09-19T14:56:47.799287Z","iopub.execute_input":"2024-09-19T14:56:47.80001Z","iopub.status.idle":"2024-09-19T14:56:47.80735Z","shell.execute_reply.started":"2024-09-19T14:56:47.799974Z","shell.execute_reply":"2024-09-19T14:56:47.806321Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trail = 0                                                     \ntrain_dataset = CustomImageDataset(train_df, image_dir, transform=transform_train)\ntrain_loader = DataLoader(train_dataset, batch_size=4, shuffle=True, num_workers=4)\n\ndino_model = torch.hub.load(\"facebookresearch/dinov2\", \"dinov2_vitl14\") # โหลดโมเดล DINO จาก Torch Hub และนำมาใช้ในโมเดล\nmodel = CustomDINONormModel(dino_model, 1216, 0.45)\n\ncriterion = nn.CrossEntropyLoss()\noptimizer = optim.Adam(model.parameters(), lr=1.0504088130751306e-06)\n\ndevice = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\") # กำหนดให้โมเดลทำงานบน GPU หากมี\nmodel.to(device)\n\nprint(\"model load_compelete\")","metadata":{"execution":{"iopub.status.busy":"2024-09-19T14:56:48.823982Z","iopub.execute_input":"2024-09-19T14:56:48.824348Z","iopub.status.idle":"2024-09-19T14:57:01.84242Z","shell.execute_reply.started":"2024-09-19T14:56:48.82432Z","shell.execute_reply":"2024-09-19T14:57:01.841504Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_epochs = 8\ndef train_model(model, criterion, optimizer, num_epochs, trail):\n    for epoch in range(num_epochs):\n        model.train()\n        running_loss = 0.0\n        for inputs, labels in train_loader:\n            inputs, labels = inputs.to(device), labels.to(device)\n            optimizer.zero_grad()\n            outputs = model(inputs)\n            loss = criterion(outputs, labels)\n            loss.backward()\n            optimizer.step()\n            running_loss += loss.item()\n        print(f'Epoch {epoch+1}, Loss: {running_loss/len(train_loader)}') # แสดงค่า Loss ของแต่ละ epoch\n\n        # บันทึกโมเดลในทุกๆ Epoch\n        model_save_path = f\"model_{trail}_{epoch}.pth\"\n        torch.save(model.state_dict(), model_save_path)\n        print(f'Model saved to {model_save_path}')\n\n    print('Finished Training')\ntrain_model(model, criterion, optimizer, num_epochs, trail)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# โหลดโมเดลที่ฝึกแล้วและเตรียมพร้อมสำหรับ predic Model 1\nmodel_save_path = f\"/kaggle/input/modelnormal/model.pth\"  # Path to the saved model\nmodel.load_state_dict(torch.load(model_save_path))\nmodel.eval()","metadata":{"execution":{"iopub.status.busy":"2024-09-19T14:57:01.844036Z","iopub.execute_input":"2024-09-19T14:57:01.844339Z","iopub.status.idle":"2024-09-19T14:57:10.460555Z","shell.execute_reply.started":"2024-09-19T14:57:01.844309Z","shell.execute_reply":"2024-09-19T14:57:10.459584Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# โหลดโมเดลที่ฝึกแล้วและเตรียมพร้อมสำหรับ predic Model 2\nmodel_save_path = f\"/kaggle/input/modelflip/Model flip.pth\"  # Path to the saved model\nmodel.load_state_dict(torch.load(model_save_path))\nmodel.eval()","metadata":{"execution":{"iopub.status.busy":"2024-09-19T15:00:29.214493Z","iopub.execute_input":"2024-09-19T15:00:29.214896Z","iopub.status.idle":"2024-09-19T15:00:38.328175Z","shell.execute_reply.started":"2024-09-19T15:00:29.214862Z","shell.execute_reply":"2024-09-19T15:00:38.327242Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset = SingleFolderDataset(directory='/kaggle/input/imgtesteng300/all test', transform=transform_test1)\ntest_loader = DataLoader(dataset, batch_size=4, shuffle=False, num_workers=4)\nprint('complete')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def make_predictions_sigmoid_sorted(data_loader, model, device): #Test1\n    model.eval()\n    y_pred = []\n    file_names = []\n    with torch.no_grad():\n        for inputs, paths in data_loader:\n            inputs = inputs.to(device)\n            outputs = model(inputs)\n            probabilities = torch.nn.functional.softmax(outputs, dim=1)\n            y_pred.extend(probabilities.cpu().numpy())\n            file_names.extend([os.path.basename(path) for path in paths])\n    return file_names, y_pred\n\n# ทำพยากรณ์และเรียงลำดับ\nfile_names, preds = make_predictions_sigmoid_sorted(test_loader, model, device)\n\n# เรียงลำดับผลพยากรณ์ตามชื่อไฟล์\nsorted_indices = sorted(range(len(file_names)), key=lambda k: file_names[k])\nsorted_file_names = [file_names[i] for i in sorted_indices]\nsorted_preds = [preds[i] for i in sorted_indices]\n\n# หาชื่อ column ที่มีค่ามากสุดในแต่ละแถว\nclass_labels = ['c0 safe driving', 'c1 texting - right', 'c2 talking phone right', 'c3 texting - left', 'c4 talking phone left', 'c5 operating the radio', 'c6 drinking', 'c7 reaching behind', 'c8 hair and makeup', 'c9 talking to passenger']\nmax_pred_labels = [class_labels[np.argmax(pred)] for pred in sorted_preds]\n\n# สร้าง DataFrame สำหรับผลพยากรณ์ที่เรียงลำดับแล้ว\npred_df = pd.DataFrame({\n    'img': sorted_file_names,\n    'prediction': max_pred_labels\n})\n\n# บันทึกผลพยากรณ์ที่เรียงลำดับแล้วเป็นไฟล์ CSV\npred_df.to_csv('submissionTest1.csv', index=False)\nprint(' submissionTest1.csv แล้ว')\npred_df","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset = SingleFolderDataset(directory='/kaggle/input/imgtesteng300/all test', transform=transform_test2)\ntest_loader = DataLoader(dataset, batch_size=4, shuffle=False, num_workers=4)\nprint('complete')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def make_predictions_sigmoid_sorted(data_loader, model, device): #Test2\n    model.eval()\n    y_pred = []\n    file_names = []\n    with torch.no_grad():\n        for inputs, paths in data_loader:\n            inputs = inputs.to(device)\n            outputs = model(inputs)\n            probabilities = torch.nn.functional.softmax(outputs, dim=1)\n            y_pred.extend(probabilities.cpu().numpy())\n            file_names.extend([os.path.basename(path) for path in paths])\n    return file_names, y_pred\n\n# ทำพยากรณ์และเรียงลำดับ\nfile_names, preds = make_predictions_sigmoid_sorted(test_loader, model, device)\n\n# เรียงลำดับผลพยากรณ์ตามชื่อไฟล์\nsorted_indices = sorted(range(len(file_names)), key=lambda k: file_names[k])\nsorted_file_names = [file_names[i] for i in sorted_indices]\nsorted_preds = [preds[i] for i in sorted_indices]\n\n# หาชื่อ column ที่มีค่ามากสุดในแต่ละแถว\nclass_labels = ['c0 safe driving', 'c1 texting - right', 'c2 talking phone right', 'c3 texting - left', 'c4 talking phone left', 'c5 operating the radio', 'c6 drinking', 'c7 reaching behind', 'c8 hair and makeup', 'c9 talking to passenger']\nmax_pred_labels = [class_labels[np.argmax(pred)] for pred in sorted_preds]\n\n# สร้าง DataFrame สำหรับผลพยากรณ์ที่เรียงลำดับแล้ว\npred_df = pd.DataFrame({\n    'img': sorted_file_names,\n    'prediction': max_pred_labels\n})\n\n# บันทึกผลพยากรณ์ที่เรียงลำดับแล้วเป็นไฟล์ CSV\npred_df.to_csv('submissionTest2.csv', index=False)\nprint(' submissionTest2.csv แล้ว')\npred_df","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset = SingleFolderDataset(directory='/kaggle/input/imgtestthai300/ALL', transform=transform_test1)\ntest_loader = DataLoader(dataset, batch_size=4, shuffle=False, num_workers=4)\nprint('complete')","metadata":{"execution":{"iopub.status.busy":"2024-09-19T15:00:44.229205Z","iopub.execute_input":"2024-09-19T15:00:44.230094Z","iopub.status.idle":"2024-09-19T15:00:44.412063Z","shell.execute_reply.started":"2024-09-19T15:00:44.230059Z","shell.execute_reply":"2024-09-19T15:00:44.411127Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def make_predictions_sigmoid_sorted(data_loader, model, device): #Test3\n    model.eval()\n    y_pred = []\n    file_names = []\n    with torch.no_grad():\n        for inputs, paths in data_loader:\n            inputs = inputs.to(device)\n            outputs = model(inputs)\n            probabilities = torch.nn.functional.softmax(outputs, dim=1)\n            y_pred.extend(probabilities.cpu().numpy())\n            file_names.extend([os.path.basename(path) for path in paths])\n    return file_names, y_pred\n\n# ทำพยากรณ์และเรียงลำดับ\nfile_names, preds = make_predictions_sigmoid_sorted(test_loader, model, device)\n\n# เรียงลำดับผลพยากรณ์ตามชื่อไฟล์\nsorted_indices = sorted(range(len(file_names)), key=lambda k: file_names[k])\nsorted_file_names = [file_names[i] for i in sorted_indices]\nsorted_preds = [preds[i] for i in sorted_indices]\n\n# หาชื่อ column ที่มีค่ามากสุดในแต่ละแถว\nclass_labels = ['c0 safe driving', 'c1 texting - right', 'c2 talking phone right', 'c3 texting - left', 'c4 talking phone left', 'c5 operating the radio', 'c6 drinking', 'c7 reaching behind', 'c8 hair and makeup', 'c9 talking to passenger']\nmax_pred_labels = [class_labels[np.argmax(pred)] for pred in sorted_preds]\n\n# สร้าง DataFrame สำหรับผลพยากรณ์ที่เรียงลำดับแล้ว\npred_df = pd.DataFrame({\n    'img': sorted_file_names,\n    'prediction': max_pred_labels\n})\n\n# บันทึกผลพยากรณ์ที่เรียงลำดับแล้วเป็นไฟล์ CSV\npred_df.to_csv('submissionTest3.csv', index=False)\nprint('บันทึกผลพยากรณ์เป็น submissionTest3.csv แล้ว')\npred_df","metadata":{"execution":{"iopub.status.busy":"2024-09-19T15:00:45.711072Z","iopub.execute_input":"2024-09-19T15:00:45.711847Z","iopub.status.idle":"2024-09-19T15:01:06.406153Z","shell.execute_reply.started":"2024-09-19T15:00:45.711815Z","shell.execute_reply":"2024-09-19T15:01:06.405086Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay\nimport matplotlib.pyplot as plt\n\n# อ่านผลการพยากรณ์\npred_df = pd.read_csv('/kaggle/input/testtttt2/submissionTest3 (1).csv')\n\n# อ่านข้อมูลจริง (true labels) จากไฟล์ CSV\ntrue_labels_df = pd.read_csv('/kaggle/input/truelabelss/truelabel.csv')\n\n# ตรวจสอบชื่อคอลัมน์ในไฟล์ true_labels.csv\nprint(true_labels_df.columns)\n\n# สร้าง dictionary สำหรับข้อมูลจริง\ntrue_labels_dict = dict(zip(true_labels_df['img'], true_labels_df['prediction']))\n\n# เพิ่มคอลัมน์สำหรับข้อมูลจริงลงใน DataFrame ของผลการพยากรณ์\npred_df['true_label'] = pred_df['img'].map(true_labels_dict)\n\n# ตรวจสอบข้อมูล\nprint(pred_df.head())","metadata":{"execution":{"iopub.status.busy":"2024-09-19T15:03:13.998902Z","iopub.execute_input":"2024-09-19T15:03:13.999929Z","iopub.status.idle":"2024-09-19T15:03:14.02124Z","shell.execute_reply.started":"2024-09-19T15:03:13.999885Z","shell.execute_reply":"2024-09-19T15:03:14.020278Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# ตรวจสอบให้แน่ใจว่าไม่มีกระทู้ NaN ใน true_label\npred_df = pred_df.dropna(subset=['true_label'])\n\n# สร้างรายการของข้อมูลจริงและผลการพยากรณ์\ny_true = pred_df['true_label'].astype(int)\ny_pred = pred_df['prediction'].astype(int)\n\n# สร้าง Confusion Matrix\ncm = confusion_matrix(y_true, y_pred, labels=list(range(10)))  # ปรับจำนวน labels ตามจำนวนคลาส\ncmd = ConfusionMatrixDisplay(cm, display_labels=[f'c{i}' for i in range(10)])\n\n# แสดงผล Confusion Matrix\ncmd.plot(cmap=plt.cm.Blues)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-09-19T15:03:15.700504Z","iopub.execute_input":"2024-09-19T15:03:15.701446Z","iopub.status.idle":"2024-09-19T15:03:16.199547Z","shell.execute_reply.started":"2024-09-19T15:03:15.701409Z","shell.execute_reply":"2024-09-19T15:03:16.198673Z"},"trusted":true},"execution_count":null,"outputs":[]}]}