Files
cloud_removal/format_lại_đataset.ipynb
Victor Phan fb8a107e77 init
2026-01-24 15:48:52 +00:00

242 lines
11 KiB
Plaintext
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "415c3125-320a-4164-a9ad-4007ac750ff9",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"📂 Đang thực hiện sắp xếp tại: /home/jovyan/cloud_train/dataset\n",
" Đã tạo folder mẹ: ROIs1158_spring\n",
" ➡ Di chuyển: ROIs1158_spring_s1 -> ROIs1158_spring/s1\n",
" ➡ Di chuyển: ROIs1158_spring_s2 -> ROIs1158_spring/s2\n",
" ➡ Di chuyển: ROIs1158_spring_s2_cloudy -> ROIs1158_spring/s2_cloudy\n",
" Đã tạo folder mẹ: ROIs1868_summer\n",
" ➡ Di chuyển: ROIs1868_summer_s1 -> ROIs1868_summer/s1\n",
" ➡ Di chuyển: ROIs1868_summer_s2 -> ROIs1868_summer/s2\n",
" ➡ Di chuyển: ROIs1868_summer_s2_cloudy -> ROIs1868_summer/s2_cloudy\n",
" Đã tạo folder mẹ: ROIs1970_fall\n",
" ➡ Di chuyển: ROIs1970_fall_s1 -> ROIs1970_fall/s1\n",
" ➡ Di chuyển: ROIs1970_fall_s2 -> ROIs1970_fall/s2\n",
" ➡ Di chuyển: ROIs1970_fall_s2_cloudy -> ROIs1970_fall/s2_cloudy\n",
" Đã tạo folder mẹ: ROIs2017_winter\n",
" ➡ Di chuyển: ROIs2017_winter_s1 -> ROIs2017_winter/s1\n",
" ➡ Di chuyển: ROIs2017_winter_s2 -> ROIs2017_winter/s2\n",
" ➡ Di chuyển: ROIs2017_winter_s2_cloudy -> ROIs2017_winter/s2_cloudy\n",
"\n",
"✅ ĐÃ XONG! Đã di chuyển 12 thư mục.\n",
"👉 Bây giờ hãy chạy lại Cell load dữ liệu (Train code), nó sẽ hoạt động.\n"
]
}
],
"source": [
"import os\n",
"import shutil\n",
"\n",
"# ==========================================\n",
"# SCRIPT SẮP XẾP LẠI THƯ MỤC (CHẠY 1 LẦN)\n",
"# ==========================================\n",
"\n",
"# 1. Xác định đường dẫn gốc\n",
"user_home = os.path.expanduser(\"~\")\n",
"base_dir = os.path.join(user_home, \"cloud_train\", \"dataset\")\n",
"print(f\"📂 Đang thực hiện sắp xếp tại: {base_dir}\")\n",
"\n",
"if not os.path.exists(base_dir):\n",
" print(\"❌ Lỗi: Không tìm thấy thư mục dataset!\")\n",
"else:\n",
" # 2. Định nghĩa cấu trúc chuẩn cần gom\n",
" # Key = Tên thư mục mẹ (Mới)\n",
" # Value = Danh sách các thư mục con (Cũ - đang nằm lẻ)\n",
" seasons_map = {\n",
" \"ROIs1158_spring\": [\"ROIs1158_spring_s1\", \"ROIs1158_spring_s2\", \"ROIs1158_spring_s2_cloudy\"],\n",
" \"ROIs1868_summer\": [\"ROIs1868_summer_s1\", \"ROIs1868_summer_s2\", \"ROIs1868_summer_s2_cloudy\"],\n",
" \"ROIs1970_fall\": [\"ROIs1970_fall_s1\", \"ROIs1970_fall_s2\", \"ROIs1970_fall_s2_cloudy\"],\n",
" \"ROIs2017_winter\": [\"ROIs2017_winter_s1\", \"ROIs2017_winter_s2\", \"ROIs2017_winter_s2_cloudy\"]\n",
" }\n",
"\n",
" count_moved = 0\n",
" \n",
" for target_season, sub_folders in seasons_map.items():\n",
" # Tạo thư mục mẹ (ví dụ: ROIs1158_spring)\n",
" target_path = os.path.join(base_dir, target_season)\n",
" if not os.path.exists(target_path):\n",
" os.makedirs(target_path)\n",
" print(f\" Đã tạo folder mẹ: {target_season}\")\n",
" \n",
" for sub in sub_folders:\n",
" source_path = os.path.join(base_dir, sub) # Đường dẫn thư mục lẻ hiện tại\n",
" \n",
" # Nếu thư mục lẻ tồn tại, di chuyển nội dung của nó vào thư mục mẹ\n",
" if os.path.exists(source_path):\n",
" # Đổi tên thư mục lẻ cho đúng chuẩn (bỏ prefix mùa đi)\n",
" # Ví dụ: ROIs1158_spring_s1 -> s1\n",
" \n",
" new_folder_name = \"\"\n",
" if \"_s1\" in sub: new_folder_name = \"s1\"\n",
" elif \"_s2_cloudy\" in sub: new_folder_name = \"s2_cloudy\"\n",
" elif \"_s2\" in sub: new_folder_name = \"s2\"\n",
" \n",
" final_dest = os.path.join(target_path, new_folder_name)\n",
" \n",
" print(f\" ➡ Di chuyển: {sub} -> {target_season}/{new_folder_name}\")\n",
" \n",
" try:\n",
" # Nếu đích chưa có thì move thẳng folder sang và đổi tên\n",
" if not os.path.exists(final_dest):\n",
" shutil.move(source_path, final_dest)\n",
" count_moved += 1\n",
" else:\n",
" print(f\" ⚠️ Thư mục {new_folder_name} đã tồn tại trong {target_season}, bỏ qua.\")\n",
" except Exception as e:\n",
" print(f\" ❌ Lỗi: {e}\")\n",
" else:\n",
" # Kiểm tra xem nó đã nằm đúng chỗ chưa\n",
" check_path = os.path.join(target_path, sub.split(\"_\")[-1].replace(\"cloudy\", \"s2_cloudy\") if \"cloudy\" in sub else sub.split(\"_\")[-1])\n",
" # Logic check đơn giản\n",
" pass\n",
"\n",
" if count_moved > 0:\n",
" print(f\"\\n✅ ĐÃ XONG! Đã di chuyển {count_moved} thư mục.\")\n",
" print(\"👉 Bây giờ hãy chạy lại Cell load dữ liệu (Train code), nó sẽ hoạt động.\")\n",
" else:\n",
" print(\"\\n Không có gì thay đổi. Có thể cấu trúc đã đúng hoặc tên file không khớp.\")\n",
" print(\"Hãy kiểm tra thủ công bằng lệnh: !ls -F /home/jovyan/cloud_train/dataset/\")"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "0b006b3f-54f9-46d2-87a6-c4ee00f7b1e4",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"📂 Đang xử lý tại: /home/jovyan/cloud_train/dataset\n",
"🔍 Đang quét mùa: ROIs1158_spring\n",
" found nested folder: s1 -> Đang di chuyển nội dung ra ngoài...\n",
" ✅ Đã xóa folder rỗng: s1\n",
" found nested folder: s2 -> Đang di chuyển nội dung ra ngoài...\n",
" ✅ Đã xóa folder rỗng: s2\n",
" found nested folder: s2_cloudy -> Đang di chuyển nội dung ra ngoài...\n",
" ✅ Đã xóa folder rỗng: s2_cloudy\n",
"🔍 Đang quét mùa: ROIs1868_summer\n",
" found nested folder: s1 -> Đang di chuyển nội dung ra ngoài...\n",
" ✅ Đã xóa folder rỗng: s1\n",
" found nested folder: s2 -> Đang di chuyển nội dung ra ngoài...\n",
" ✅ Đã xóa folder rỗng: s2\n",
" found nested folder: s2_cloudy -> Đang di chuyển nội dung ra ngoài...\n",
" ✅ Đã xóa folder rỗng: s2_cloudy\n",
"🔍 Đang quét mùa: ROIs1970_fall\n",
" found nested folder: s1 -> Đang di chuyển nội dung ra ngoài...\n",
" ✅ Đã xóa folder rỗng: s1\n",
" found nested folder: s2 -> Đang di chuyển nội dung ra ngoài...\n",
" ✅ Đã xóa folder rỗng: s2\n",
" found nested folder: s2_cloudy -> Đang di chuyển nội dung ra ngoài...\n",
" ✅ Đã xóa folder rỗng: s2_cloudy\n",
"🔍 Đang quét mùa: ROIs2017_winter\n",
" found nested folder: s1 -> Đang di chuyển nội dung ra ngoài...\n",
" ✅ Đã xóa folder rỗng: s1\n",
" found nested folder: s2 -> Đang di chuyển nội dung ra ngoài...\n",
" ✅ Đã xóa folder rỗng: s2\n",
" found nested folder: s2_cloudy -> Đang di chuyển nội dung ra ngoài...\n",
" ✅ Đã xóa folder rỗng: s2_cloudy\n",
"\n",
"✅ ĐÃ SỬA XONG CẤU TRÚC!\n"
]
}
],
"source": [
"import os\n",
"import shutil\n",
"\n",
"# ==========================================\n",
"# FIX CẤU TRÚC PHASE 2: ĐƯA THƯ MỤC CON RA NGOÀI\n",
"# ==========================================\n",
"\n",
"user_home = os.path.expanduser(\"~\")\n",
"base_dir = os.path.join(user_home, \"cloud_train\", \"dataset\")\n",
"print(f\"📂 Đang xử lý tại: {base_dir}\")\n",
"\n",
"seasons = [\n",
" \"ROIs1158_spring\", \n",
" \"ROIs1868_summer\", \n",
" \"ROIs1970_fall\", \n",
" \"ROIs2017_winter\"\n",
"]\n",
"\n",
"# Các folder trung gian cần loại bỏ\n",
"sub_types = [\"s1\", \"s2\", \"s2_cloudy\"]\n",
"\n",
"for season in seasons:\n",
" season_path = os.path.join(base_dir, season)\n",
" if not os.path.exists(season_path):\n",
" continue\n",
" \n",
" print(f\"🔍 Đang quét mùa: {season}\")\n",
" \n",
" for sub in sub_types:\n",
" # Đường dẫn tới folder trung gian (ví dụ: ROIs1158_spring/s1)\n",
" nested_path = os.path.join(season_path, sub)\n",
" \n",
" if os.path.exists(nested_path):\n",
" print(f\" found nested folder: {sub} -> Đang di chuyển nội dung ra ngoài...\")\n",
" \n",
" # Lấy danh sách các scene bên trong (s1_1, s1_2...)\n",
" scenes = os.listdir(nested_path)\n",
" for scene in scenes:\n",
" src = os.path.join(nested_path, scene)\n",
" dst = os.path.join(season_path, scene)\n",
" \n",
" # Di chuyển ra folder mẹ\n",
" if not os.path.exists(dst):\n",
" shutil.move(src, dst)\n",
" \n",
" # Sau khi chuyển hết thì xóa folder rỗng đi\n",
" try:\n",
" os.rmdir(nested_path)\n",
" print(f\" ✅ Đã xóa folder rỗng: {sub}\")\n",
" except OSError:\n",
" print(f\" ⚠️ Không xóa được {sub} (có thể còn file rác)\")\n",
"\n",
"print(\"\\n✅ ĐÃ SỬA XONG CẤU TRÚC!\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e6151206-f103-451c-a572-029a4da25816",
"metadata": {},
"outputs": [],
"source": []
}
],
"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.3"
}
},
"nbformat": 4,
"nbformat_minor": 5
}