{"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":"none","dataSources":[{"sourceId":19989,"databundleVersionId":1160143,"sourceType":"competition"}],"dockerImageVersionId":30804,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport cv2\nimport os\nimport re\nfrom PIL import Image\nimport torch\nimport torchvision\nimport json\nfrom tqdm import tqdm\nfrom pathlib import Path\nfrom tqdm.auto import tqdm\nimport glob\nfrom sklearn.model_selection import GroupKFold\ntrain_df = pd.read_csv('/kaggle/input/global-wheat-detection/train.csv')\nn_splits = 5\n# 初始化 GroupKFold\ngkf = GroupKFold(n_splits=n_splits)\nfold_ = 0\nfor train_idx, val_idx in gkf.split(X=train_df.index, groups=train_df['image_id']):\n    train_df.loc[val_idx, 'fold'] = fold_\n    fold_ += 1\nIMG_DIR = \"/kaggle/input/global-wheat-detection/train\"\nFOLDS = [0, 1, 2, 3, 4]\nOD_INPUT_SIZE = 1042\ntrain_df['x'] = -1\ntrain_df['y'] = -1\ntrain_df['w'] = -1\ntrain_df['h'] = -1\n\n\ndef expand_bbox(x):\n    r = np.array(re.findall(\"([0-9]+[.]?[0-9]*)\", x))\n    if len(r) == 0:\n        r = [-1, -1, -1, -1]\n    return r\ntrain_df[['x', 'y', 'w', 'h']] = np.stack(train_df['bbox'].apply(lambda x: expand_bbox(x)))\ntrain_df['category_id']=0\ndef gen_coco_format(ann_df, phase='train'):\n    # COCO format template\n    coco_data = {\n        \"images\": [],\n        \"annotations\": [],\n        \"categories\": []\n    }\n    categories = {\n        0: \"wheat\",\n    }\n    for cat_id, cat_name in categories.items():\n        coco_data[\"categories\"].append({\"id\": cat_id, \"name\": cat_name})\n    image_id_counter = 0\n    annotation_id_counter = 0\n    for name, group in tqdm(ann_df.groupby(['image_id'])):\n        image_id = name[0]\n        path = f'{IMG_DIR}/{image_id}.jpg'\n        image = cv2.imread(path, cv2.IMREAD_COLOR)\n        H, W = image.shape[:2]\n        img_dir = os.path.join(OUT_DIR,f'{phase}2017')\n        os.makedirs(img_dir, exist_ok=True)\n        img_path = os.path.join(img_dir, f'{image_id}.jpg')\n        cv2.imwrite(img_path, image)\n        image_info = {\n            \"id\": image_id_counter,\n            \"file_name\": os.path.basename(img_path),\n            \"width\": W,\n            \"height\": H\n        }\n        coco_data[\"images\"].append(image_info)\n        for i, row in group.iterrows():\n            x_min, y_min, width, height = float(row['x']),float(row['y']),float(row['w']),float(row['h'])\n            x_max = x_min + width\n            y_max = y_min + height\n            annotation = {\n                \"id\": annotation_id_counter,\n                \"image_id\":image_id_counter,\n                \"category_id\": row['category_id'],\n                \"bbox\": [x_min, y_min, width, height],\n                \"area\": width * height,\n                \"iscrowd\": 0\n            }\n            coco_data[\"annotations\"].append(annotation)\n            annotation_id_counter += 1\n        image_id_counter += 1\n    ann_path = os.path.join(OUT_DIR, f'instances_{phase}2017.json')\n    with open(ann_path, 'w') as f:\n        json.dump(coco_data, f)\n\n\nfor FOLD in FOLDS:\n    print('Gen data fold', FOLD)\n    OUT_DIR = f'/kaggle/working'\n    os.makedirs(OUT_DIR, exist_ok=True)\n    train_df_ = train_df[train_df.fold != FOLD]\n    val_df = train_df[train_df.fold == FOLD]\n    gen_coco_format(train_df_, phase='train')\n    gen_coco_format(val_df, phase='valid')\n    break","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T01:26:12.677728Z","iopub.execute_input":"2024-12-08T01:26:12.678136Z","iopub.status.idle":"2024-12-08T01:27:41.855369Z","shell.execute_reply.started":"2024-12-08T01:26:12.678102Z","shell.execute_reply":"2024-12-08T01:27:41.854290Z"}},"outputs":[],"execution_count":null}]}