{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":75176,"databundleVersionId":8252256,"sourceType":"competition"}],"dockerImageVersionId":30762,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os\nimport pandas as pd\nimport shutil\nimport random\n\n# Đọc file train.csv\ntrain_df = pd.read_csv('/kaggle/input/amia-public-challenge-2024/train.csv')\n\n# Tạo các thư mục cần thiết\nos.makedirs('data/train_A', exist_ok=True)\nos.makedirs('data/train_B', exist_ok=True)\nos.makedirs('data/test_A', exist_ok=True)\nos.makedirs('data/test_B', exist_ok=True)\n\n# Lấy danh sách các ảnh \"No finding\"\nno_finding_images = train_df[train_df['class_name'] == 'No finding']['image_id'].unique()\n\n# Lấy danh sách các ảnh thuộc các lớp khác\nother_classes_images = train_df[train_df['class_name'] != 'No finding']['image_id'].unique()","metadata":{"execution":{"iopub.status.busy":"2024-09-17T13:09:36.867211Z","iopub.execute_input":"2024-09-17T13:09:36.867483Z","iopub.status.idle":"2024-09-17T13:09:37.394505Z","shell.execute_reply.started":"2024-09-17T13:09:36.867450Z","shell.execute_reply":"2024-09-17T13:09:37.393710Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f\"Số lượng ảnh 'No finding': {len(no_finding_images)}\")\nprint(f\"Số lượng ảnh lớp mục tiêu: {len(other_classes_images)}\")","metadata":{"execution":{"iopub.status.busy":"2024-09-17T13:10:18.234956Z","iopub.execute_input":"2024-09-17T13:10:18.235731Z","iopub.status.idle":"2024-09-17T13:10:18.241192Z","shell.execute_reply.started":"2024-09-17T13:10:18.235685Z","shell.execute_reply":"2024-09-17T13:10:18.240188Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"no_finding_images = no_finding_images[:1000]\nother_classes_images = other_classes_images[:1000]","metadata":{"execution":{"iopub.status.busy":"2024-09-17T13:13:53.983913Z","iopub.execute_input":"2024-09-17T13:13:53.984794Z","iopub.status.idle":"2024-09-17T13:13:53.988913Z","shell.execute_reply.started":"2024-09-17T13:13:53.984753Z","shell.execute_reply":"2024-09-17T13:13:53.987941Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f\"Số lượng ảnh 'No finding': {len(no_finding_images)}\")\nprint(f\"Số lượng ảnh lớp mục tiêu: {len(other_classes_images)}\")","metadata":{"execution":{"iopub.status.busy":"2024-09-17T13:14:00.164907Z","iopub.execute_input":"2024-09-17T13:14:00.165306Z","iopub.status.idle":"2024-09-17T13:14:00.170558Z","shell.execute_reply.started":"2024-09-17T13:14:00.165269Z","shell.execute_reply":"2024-09-17T13:14:00.169622Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Chia dữ liệu thành tập train và test\ntrain_ratio = 0.8\nno_finding_train = random.sample(list(no_finding_images), int(len(no_finding_images) * train_ratio))\nno_finding_test = list(set(no_finding_images) - set(no_finding_train))\nother_classes_train = random.sample(list(other_classes_images), int(len(other_classes_images) * train_ratio))\nother_classes_test = list(set(other_classes_images) - set(other_classes_train))\n\n# Sao chép ảnh vào các thư mục tương ứng\nfor image_id in no_finding_train:\n    shutil.copy(f'/kaggle/input/amia-public-challenge-2024/train/train/{image_id}.png', 'data/train_A/')\nfor image_id in no_finding_test:\n    shutil.copy(f'/kaggle/input/amia-public-challenge-2024/train/train/{image_id}.png', 'data/test_A/')\nfor image_id in other_classes_train:\n    shutil.copy(f'/kaggle/input/amia-public-challenge-2024/train/train/{image_id}.png', 'data/train_B/')\nfor image_id in other_classes_test:\n    shutil.copy(f'/kaggle/input/amia-public-challenge-2024/train/train/{image_id}.png', 'data/test_B/')\n\n# Tạo file fixed_prompt_a.txt và fixed_prompt_b.txt\nwith open('data/fixed_prompt_a.txt', 'w') as f:\n    f.write('No finding')\nwith open('data/fixed_prompt_b.txt', 'w') as f:\n    f.write('Other classes')","metadata":{"execution":{"iopub.status.busy":"2024-09-17T13:14:04.845530Z","iopub.execute_input":"2024-09-17T13:14:04.845940Z","iopub.status.idle":"2024-09-17T13:14:32.281838Z","shell.execute_reply.started":"2024-09-17T13:14:04.845899Z","shell.execute_reply":"2024-09-17T13:14:32.281007Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!git clone https://github.com/GaParmar/img2img-turbo.git\n","metadata":{"execution":{"iopub.status.busy":"2024-09-17T13:15:28.888689Z","iopub.execute_input":"2024-09-17T13:15:28.889441Z","iopub.status.idle":"2024-09-17T13:15:31.450033Z","shell.execute_reply.started":"2024-09-17T13:15:28.889400Z","shell.execute_reply":"2024-09-17T13:15:31.449131Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cd /kaggle/working/img2img-turbo","metadata":{"execution":{"iopub.status.busy":"2024-09-17T13:22:26.493534Z","iopub.execute_input":"2024-09-17T13:22:26.493933Z","iopub.status.idle":"2024-09-17T13:22:26.501515Z","shell.execute_reply.started":"2024-09-17T13:22:26.493893Z","shell.execute_reply":"2024-09-17T13:22:26.500536Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pwd","metadata":{"execution":{"iopub.status.busy":"2024-09-17T13:22:30.533342Z","iopub.execute_input":"2024-09-17T13:22:30.533731Z","iopub.status.idle":"2024-09-17T13:22:31.507654Z","shell.execute_reply.started":"2024-09-17T13:22:30.533695Z","shell.execute_reply":"2024-09-17T13:22:31.506736Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Cài đặt môi trường\n!pip install -q -r requirements.txt","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip uninstall -q torch torchvision -y\n!pip install -q torch torchvision","metadata":{"execution":{"iopub.status.busy":"2024-09-17T13:46:28.037332Z","iopub.execute_input":"2024-09-17T13:46:28.037811Z","iopub.status.idle":"2024-09-17T13:51:29.249396Z","shell.execute_reply.started":"2024-09-17T13:46:28.037762Z","shell.execute_reply":"2024-09-17T13:51:29.248323Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Huấn luyện mô hình\n!python ./src/train_cyclegan_turbo.py \\\n    --dataroot /kaggle/working/data \\\n    --name lung_disease_cyclegan \\\n    --model cycle_gan_turbo \\\n    --train_img_prep resize_and_crop \\\n    --val_img_prep resize \\\n    --lambda_gan 1 \\\n    --lambda_idt 0.5 \\\n    --lambda_cycle 10","metadata":{"execution":{"iopub.status.busy":"2024-09-17T14:01:46.924824Z","iopub.execute_input":"2024-09-17T14:01:46.925269Z","iopub.status.idle":"2024-09-17T14:01:58.985761Z","shell.execute_reply.started":"2024-09-17T14:01:46.925224Z","shell.execute_reply":"2024-09-17T14:01:58.984449Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Theo dõi quá trình huấn luyện\n%load_ext tensorboard\n%tensorboard --logdir ./logs","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Tạo ảnh giả\n!python test_cyclegan_turbo.py \\\n    --dataroot /kaggle/working/data \\\n    --name lung_disease_cyclegan \\\n    --model cycle_gan_turbo \\\n    --phase test \\\n    --no_dropout \\\n    --num_test 100 \\\n    --results_dir ./results","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Đánh giá mô hình\n!python evaluate_cyclegan_turbo.py \\\n    --dataroot /kaggle/working/data \\\n    --name lung_disease_cyclegan \\\n    --model cycle_gan_turbo \\\n    --phase test \\\n    --num_test 100 \\\n    --eval_fid \\\n    --eval_is \\\n    --eval_kid","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}