{"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":"tpu1vmV38","dataSources":[{"sourceId":29876,"databundleVersionId":2468249,"sourceType":"competition"}],"dockerImageVersionId":30664,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"## 安装yolov8环境","metadata":{}},{"cell_type":"code","source":"%pip install ultralytics\nimport ultralytics\nultralytics.checks()","metadata":{"execution":{"iopub.status.busy":"2024-03-20T05:13:50.333190Z","iopub.execute_input":"2024-03-20T05:13:50.333458Z","iopub.status.idle":"2024-03-20T05:13:57.929778Z","shell.execute_reply.started":"2024-03-20T05:13:50.333429Z","shell.execute_reply":"2024-03-20T05:13:57.928077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Run inference on an image with YOLOv8n\n!yolo predict model=yolov8n.pt source='https://ultralytics.com/images/zidane.jpg'","metadata":{"execution":{"iopub.status.busy":"2024-03-20T05:13:57.930468Z","iopub.status.idle":"2024-03-20T05:13:57.930742Z","shell.execute_reply.started":"2024-03-20T05:13:57.930604Z","shell.execute_reply":"2024-03-20T05:13:57.930620Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 数据处理","metadata":{}},{"cell_type":"code","source":"import os\nimport pandas as pd\nimport numpy as np\nimport random\nimport json\nimport shutil","metadata":{"execution":{"iopub.status.busy":"2024-03-20T05:13:57.934057Z","iopub.status.idle":"2024-03-20T05:13:57.934515Z","shell.execute_reply.started":"2024-03-20T05:13:57.934347Z","shell.execute_reply":"2024-03-20T05:13:57.934363Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_info = json.load(open('../input/cowboyoutfits/train.json', 'r'))\ntemp_an = train_info[\"annotations\"]\ntotle_an = set(each[\"image_id\"] for each in temp_an)","metadata":{"execution":{"iopub.status.busy":"2024-03-20T05:13:57.935434Z","iopub.status.idle":"2024-03-20T05:13:57.935696Z","shell.execute_reply.started":"2024-03-20T05:13:57.935565Z","shell.execute_reply":"2024-03-20T05:13:57.935578Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ci = [87, 1034, 131, 318, 588]  # category_id, 分别对应belt,sunglasses,boot,cowboy_hat,jacket\n\ntotle_id = set()\n\nfor each in temp_an:\n    for idx,i in enumerate(ci):\n        if ci[idx] == each[\"category_id\"]:\n            totle_id.add(each[\"image_id\"])\n            break\nprint(len(temp_an))\n\nprint(len(totle_id))","metadata":{"execution":{"iopub.status.busy":"2024-03-20T05:13:57.936374Z","iopub.status.idle":"2024-03-20T05:13:57.936640Z","shell.execute_reply.started":"2024-03-20T05:13:57.936510Z","shell.execute_reply":"2024-03-20T05:13:57.936523Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"category = dict()\nfor idx,i in enumerate(ci):\n    category[i] = set()\n\n\nfor each in temp_an:\n    for idx,i in enumerate(ci):\n        if ci[idx] == each[\"category_id\"]:\n            category[i].add(each[\"image_id\"])\n            break\n\nfor idx,i in enumerate(ci):\n    print(len(category[i]))","metadata":{"execution":{"iopub.status.busy":"2024-03-20T05:13:57.937384Z","iopub.status.idle":"2024-03-20T05:13:57.937651Z","shell.execute_reply.started":"2024-03-20T05:13:57.937515Z","shell.execute_reply":"2024-03-20T05:13:57.937528Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## split train and test dataset","metadata":{}},{"cell_type":"code","source":"vi_c = [2, 31, 10, 13, 25]\nval_id = set()\nfor idx,i in enumerate(ci):\n    random_items = random.sample(category[i], vi_c[idx])\n    randon_set = set(random_items)\n    val_id = val_id | randon_set\n\ntrain_id = totle_id - val_id\n\nlen(val_id),len(train_id)","metadata":{"execution":{"iopub.status.busy":"2024-03-20T05:13:57.938662Z","iopub.status.idle":"2024-03-20T05:13:57.938944Z","shell.execute_reply.started":"2024-03-20T05:13:57.938792Z","shell.execute_reply":"2024-03-20T05:13:57.938805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_an = []\nfor each_an in temp_an:\n    for tid in train_id:\n        if each_an['image_id'] == tid:\n            train_an.append(each_an)\n            break\n            \nval_an = []\nfor each_an in temp_an:\n    for tid in val_id:\n        if each_an['image_id'] == tid:\n            val_an.append(each_an)\n            break\n","metadata":{"execution":{"iopub.status.busy":"2024-03-20T05:13:57.939744Z","iopub.status.idle":"2024-03-20T05:13:57.940054Z","shell.execute_reply.started":"2024-03-20T05:13:57.939895Z","shell.execute_reply":"2024-03-20T05:13:57.939927Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.makedirs('./data/train/images' ,exist_ok=True)\nos.makedirs('./data/val/images' ,exist_ok=True)\n\ntrain_img = []\ncopy_img = []\nfor j in train_info['images']:\n    for i in train_id:\n        if j['id'] == i:\n            shutil.copy('../input/cowboyoutfits/images/'+j['file_name'], './data/train/images')\n            for l in category[87]:\n                if j['id'] == l:\n                    copy_img.append(j)\n                    for i in range(15):\n                        shutil.copy('../input/cowboyoutfits/images/'+j['file_name'], './data/train/images/'+ str(i) + j['file_name'])\n            train_img.append(j)    \n                \nval_img = []\n# Move val images\nfor j in train_info['images']:\n    for i in val_id:\n        if j['id'] == i:\n            shutil.copy('../input/cowboyoutfits/images/'+j['file_name'], './data/val/images')\n            val_img.append(j)\n            ","metadata":{"execution":{"iopub.status.busy":"2024-03-20T05:13:57.940817Z","iopub.status.idle":"2024-03-20T05:13:57.941133Z","shell.execute_reply.started":"2024-03-20T05:13:57.940993Z","shell.execute_reply":"2024-03-20T05:13:57.941007Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.makedirs('./data/train/labels', exist_ok=True)\nos.makedirs('./data/val/labels', exist_ok=True)\n\ntrain_inf = [(each['id'], each['file_name'].split('.')[0], each['width'], each['height']) for each in train_img]\nval_inf = [(each['id'], each['file_name'].split('.')[0], each['width'], each['height']) for each in val_img]\n\ntrans = {f'{each}': f'{idx}' for (idx, each) in enumerate(ci)}  # Mapping the category_ids\n\n# Create *.txt files for training\nfor (imid, fn, w, h) in train_inf:\n    with open('./data/train/labels/' + fn + '.txt', 'w') as t_f:\n        for t_ann in train_an:\n            if t_ann['image_id'] == imid:\n                # convert X_min,Y_min,w,h to X_center/width,Y_center/height,w/width,h/height\n                bbox = [str((t_ann['bbox'][0] + (t_ann['bbox'][2] / 2) - 1) / float(w)) + ' ',\n                        str((t_ann['bbox'][1] + (t_ann['bbox'][3] / 2) - 1) / float(h)) + ' ',\n                        str(t_ann['bbox'][2] / float(w)) + ' ', \n                        str(t_ann['bbox'][3] / float(h))]\n                t_f.write(trans[str(t_ann['category_id'])] + ' ' + str(bbox[0] + bbox[1] + bbox[2] + bbox[3]))\n                t_f.write('\\n')\n\n\n# Create *.txt files for evaluating\nfor (imid, fn, w, h) in val_inf:\n    with open('./data/val/labels/' + fn + '.txt', 'w') as v_f:\n        for v_ann in val_an:\n            if v_ann['image_id'] == imid:\n                # convert X_min,Y_min,w,h to X_center/width,Ycenter/height,w/width,h/height\n                bbox = [str((v_ann['bbox'][0] + (v_ann['bbox'][2] / 2) - 1) / float(w)) + ' ',\n                        str((v_ann['bbox'][1] + (v_ann['bbox'][3] / 2) - 1) / float(h)) + ' ',\n                        str(v_ann['bbox'][2] / float(w)) + ' ',\n                        str(v_ann['bbox'][3] / float(h))]\n                v_f.write(trans[str(v_ann['category_id'])] + ' ' + str(bbox[0] + bbox[1] + bbox[2] + bbox[3]))\n                v_f.write('\\n')\ncopy_inf = [(each['id'], each['file_name'].split('.')[0]) for each in copy_img]               \nfor (imid, fn) in copy_inf:\n    for i in range(15):\n        shutil.copy('./data/train/labels/' + fn + '.txt', './data/train/labels/' + str(i) + fn + '.txt')","metadata":{"execution":{"iopub.status.busy":"2024-03-20T05:13:57.942018Z","iopub.status.idle":"2024-03-20T05:13:57.942282Z","shell.execute_reply.started":"2024-03-20T05:13:57.942148Z","shell.execute_reply":"2024-03-20T05:13:57.942161Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## code for trainning","metadata":{}},{"cell_type":"code","source":"import yaml\nos.makedirs('./data', exist_ok=True)\ndata = dict(\n    train = '/kaggle/working/data/train/images',\n    val = '/kaggle/working/data/val/images',\n    test = '/kaggle/working/data/test/images',\n    nc = 5,\n    names = ['belt','sunglasses', 'boot', 'cowboy_hat', 'jacket']\n)\nwith open('./data.yaml', 'w') as f:\n    yaml.dump(data, f, default_flow_style=False)","metadata":{"execution":{"iopub.status.busy":"2024-03-20T05:13:57.943178Z","iopub.status.idle":"2024-03-20T05:13:57.943485Z","shell.execute_reply.started":"2024-03-20T05:13:57.943331Z","shell.execute_reply":"2024-03-20T05:13:57.943346Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!cat data.yaml","metadata":{"execution":{"iopub.status.busy":"2024-03-20T05:13:57.944341Z","iopub.status.idle":"2024-03-20T05:13:57.944639Z","shell.execute_reply.started":"2024-03-20T05:13:57.944488Z","shell.execute_reply":"2024-03-20T05:13:57.944503Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from ultralytics import YOLO\n\n# Build from YAML and transfer weights\nFinal_model = YOLO('yolov8n.yaml').load('yolov8n.pt')  \n\n# Training The Final Model\nResult_Final_model = Final_model.train(data=\"./data.yaml\",epochs=200, imgsz = 1088 , batch = 16 ,lr0 = 0.001, dropout= 0.10, device = 0)\n","metadata":{"execution":{"iopub.status.busy":"2024-03-20T05:13:57.945691Z","iopub.status.idle":"2024-03-20T05:13:57.945983Z","shell.execute_reply.started":"2024-03-20T05:13:57.945823Z","shell.execute_reply":"2024-03-20T05:13:57.945844Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## val the model","metadata":{}},{"cell_type":"code","source":"Val_model = YOLO('./run/detect/train/weights/best.pt')\n\n# Validate the model\nmetrics = Val_model.val()  # no arguments needed, dataset and settings remembered\nmetrics.box.map    # map50-95\nmetrics.box.map50  # map50\nmetrics.box.map75  # map75\nmetrics.box.maps   # a list contains map50-95 of each category","metadata":{"execution":{"iopub.status.busy":"2024-03-20T05:13:57.946740Z","iopub.status.idle":"2024-03-20T05:13:57.947022Z","shell.execute_reply.started":"2024-03-20T05:13:57.946886Z","shell.execute_reply":"2024-03-20T05:13:57.946900Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## predict","metadata":{}},{"cell_type":"code","source":"os.mkdir('./data/test/images', exist_ok = True)\ntest_info = pd.read_csv(\"/kaggle/input/cowboyoutfits/test.csv\")\nfor each in test_info['file_name']:\n    shutil.copy(f'../input/cowboyoutfits/images/{each}', './data/test/images')\n    \nimage_files = [os.path.join('./data/test/images', file) for file in os.listdir('./data/test/images') if file.endswith('.jpg')]\n\nresults = Val_model([image_files])","metadata":{"execution":{"iopub.status.busy":"2024-03-20T05:13:57.947841Z","iopub.status.idle":"2024-03-20T05:13:57.948132Z","shell.execute_reply.started":"2024-03-20T05:13:57.948000Z","shell.execute_reply":"2024-03-20T05:13:57.948014Z"},"trusted":true},"execution_count":null,"outputs":[]}]}