{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# !pip install -qU wandb kaggle现在依旧默认安装了wandb\n!pip install -qU bbox-utility # check https://github.com/awsaf49/bbox for source code","metadata":{"execution":{"iopub.status.busy":"2023-05-24T11:54:14.490830Z","iopub.execute_input":"2023-05-24T11:54:14.491340Z","iopub.status.idle":"2023-05-24T11:54:28.032259Z","shell.execute_reply.started":"2023-05-24T11:54:14.491284Z","shell.execute_reply":"2023-05-24T11:54:28.030346Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nfrom tqdm.notebook import tqdm\ntqdm.pandas() # 可以显示pandas操作的进度条\nimport pandas as pd\nimport os\nimport cv2\nimport matplotlib.pyplot as plt\nimport glob\n\nimport shutil\nimport sys\nsys.path.append('../input/tensorflow-great-barrier-reef')\n\nfrom joblib import Parallel, delayed\nfrom IPython.display import display\n","metadata":{"execution":{"iopub.status.busy":"2023-05-24T11:54:28.035236Z","iopub.execute_input":"2023-05-24T11:54:28.035656Z","iopub.status.idle":"2023-05-24T11:54:28.044716Z","shell.execute_reply.started":"2023-05-24T11:54:28.035617Z","shell.execute_reply":"2023-05-24T11:54:28.043426Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"FOLD      = 1 # which fold to train\nREMOVE_NOBBOX = True # remove images with no bbox\nROOT_DIR  = '../input/tensorflow-great-barrier-reef/' # 原始数据集的路径\nIMAGE_DIR = './kaggle/images' # 新的训练集图片路径\nLABEL_DIR = './kaggle/labels' # 新的训练集图片标签路径","metadata":{"execution":{"iopub.status.busy":"2023-05-24T11:54:28.046698Z","iopub.execute_input":"2023-05-24T11:54:28.047062Z","iopub.status.idle":"2023-05-24T11:54:28.061604Z","shell.execute_reply.started":"2023-05-24T11:54:28.047029Z","shell.execute_reply":"2023-05-24T11:54:28.060056Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir -p {IMAGE_DIR}\n!mkdir -p {LABEL_DIR}","metadata":{"execution":{"iopub.status.busy":"2023-05-24T11:54:28.065143Z","iopub.execute_input":"2023-05-24T11:54:28.065575Z","iopub.status.idle":"2023-05-24T11:54:30.246193Z","shell.execute_reply.started":"2023-05-24T11:54:28.065530Z","shell.execute_reply":"2023-05-24T11:54:30.244429Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Train Data\ndf = pd.read_csv(f'{ROOT_DIR}/train.csv')\ndf['old_image_path'] = f'{ROOT_DIR}/train_images/video_'+df.video_id.astype(str)+'/'+df.video_frame.astype(str)+'.jpg'\n# image_id 已经包含了video_id和video_frame，直接以此作为图片样本编号\ndf['image_path']  = f'{IMAGE_DIR}/'+df.image_id+'.jpg'\ndf['label_path']  = f'{LABEL_DIR}/'+df.image_id+'.txt'\ndf['annotations'] = df['annotations'].progress_apply(eval) # progress_apply可以监视运行进度\ndisplay(df.head(2))","metadata":{"execution":{"iopub.status.busy":"2023-05-24T11:54:30.248199Z","iopub.execute_input":"2023-05-24T11:54:30.248641Z","iopub.status.idle":"2023-05-24T11:54:30.815380Z","shell.execute_reply.started":"2023-05-24T11:54:30.248602Z","shell.execute_reply":"2023-05-24T11:54:30.814218Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['num_bbox'] = df['annotations'].progress_apply(lambda x: len(x))\ndata = (df.num_bbox>0).value_counts(normalize=True)*100\nprint(f\"No BBox: {data[0]:0.2f}% | With BBox: {data[1]:0.2f}%\")","metadata":{"execution":{"iopub.status.busy":"2023-05-24T11:54:30.817548Z","iopub.execute_input":"2023-05-24T11:54:30.817926Z","iopub.status.idle":"2023-05-24T11:54:30.923770Z","shell.execute_reply.started":"2023-05-24T11:54:30.817890Z","shell.execute_reply":"2023-05-24T11:54:30.922610Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = df.query(\"num_bbox>0\")","metadata":{"execution":{"iopub.status.busy":"2023-05-24T11:54:30.925555Z","iopub.execute_input":"2023-05-24T11:54:30.925893Z","iopub.status.idle":"2023-05-24T11:54:30.945248Z","shell.execute_reply.started":"2023-05-24T11:54:30.925863Z","shell.execute_reply":"2023-05-24T11:54:30.944124Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def make_copy(row):\n    shutil.copyfile(row.old_image_path, row.image_path)\n    return","metadata":{"execution":{"iopub.status.busy":"2023-05-24T11:54:30.947103Z","iopub.execute_input":"2023-05-24T11:54:30.947584Z","iopub.status.idle":"2023-05-24T11:54:30.953448Z","shell.execute_reply.started":"2023-05-24T11:54:30.947539Z","shell.execute_reply":"2023-05-24T11:54:30.952400Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_paths = df.old_image_path.tolist()\n_ = Parallel(n_jobs=-1, backend='threading')(delayed(make_copy)(row) for _, row in tqdm(df.iterrows(), total=len(df)))","metadata":{"execution":{"iopub.status.busy":"2023-05-24T11:54:30.955524Z","iopub.execute_input":"2023-05-24T11:54:30.955938Z","iopub.status.idle":"2023-05-24T11:54:41.932663Z","shell.execute_reply.started":"2023-05-24T11:54:30.955897Z","shell.execute_reply":"2023-05-24T11:54:41.931407Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from bbox.utils import coco2yolo, coco2voc, voc2yolo\nfrom bbox.utils import draw_bboxes, load_image\nfrom bbox.utils import clip_bbox, str2annot, annot2str\n\ndef get_bbox(annots):\n    bboxes = [list(annot.values()) for annot in annots]\n    return bboxes\n\ndef get_imgsize(row):\n    row['width'], row['height'] = imagesize.get(row['image_path'])\n    return row\n\nnp.random.seed(32)\ncolors = [(np.random.randint(255), np.random.randint(255), np.random.randint(255))\\\n          for idx in range(1)]\n\ndf['bboxes'] = df.annotations.progress_apply(get_bbox) # 直接得到标注框的[xmin，ymin，w，h]信息\ncolors","metadata":{"execution":{"iopub.status.busy":"2023-05-24T11:54:41.936951Z","iopub.execute_input":"2023-05-24T11:54:41.937390Z","iopub.status.idle":"2023-05-24T11:54:42.002716Z","shell.execute_reply.started":"2023-05-24T11:54:41.937351Z","shell.execute_reply":"2023-05-24T11:54:42.001374Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['width']  = 1280\ndf['height'] = 720\ndf","metadata":{"execution":{"iopub.status.busy":"2023-05-24T11:54:42.004345Z","iopub.execute_input":"2023-05-24T11:54:42.004743Z","iopub.status.idle":"2023-05-24T11:54:42.670317Z","shell.execute_reply.started":"2023-05-24T11:54:42.004708Z","shell.execute_reply":"2023-05-24T11:54:42.668766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cnt = 0\nall_bboxes = []\nbboxes_info = []\nfor row_idx in tqdm(range(df.shape[0])):\n    row = df.iloc[row_idx] # 读取df的一行数据\n    image_height = row.height\n    image_width  = row.width\n    # 读取coco格式的标注框信息，比如array([[559., 213.,  50.,  32.]], dtype=float32)\n    bboxes_coco  = np.array(row.bboxes).astype(np.float32).copy() \n    num_bbox     = len(bboxes_coco) # 标注框个数\n    names        = ['cots']*num_bbox # 每个标注框名字都是cots（刺海星冠）\n    labels       = np.array([0]*num_bbox)[..., None].astype(str) #  array([['0'],['0']],[..., None]表示增加一个维度\n    ## Create Annotation(YOLO)\n    with open(row.label_path, 'w') as f:\n    \t# 这一步表示如果读取的行没有标注框，标注信息就填''，且表示'Missing:'的cnt+1\n        if num_bbox<1:\n            annot = ''\n            f.write(annot)\n            cnt+=1\n            continue\n        # 将coco格式标注框[xmin,ymin,w,h]转为voc格式标注框[xmin,ymin,xmax,ymax]比如array([[559., 213., 609., 245.]])\n        bboxes_voc  = coco2voc(bboxes_coco, image_height, image_width)\n        bboxes_voc  = clip_bbox(bboxes_voc, image_height, image_width)\n        # 将voc格式标注框[xmin,ymin,w,h]转为yolo格式标注框[xcenter,ycenter,w,h]\n        # 比如array([['0.5407407', '0.31805557', '0.0462963', '0.04444447']])，标准格式\n        bboxes_yolo = voc2yolo(bboxes_voc, image_height, image_width).astype(str)\n        \n        all_bboxes.extend(bboxes_yolo.astype(float))\n        bboxes_info.extend([[row.image_id, row.video_id, row.sequence]]*len(bboxes_yolo))\n        # 将标签信息连上标注框信息\n        annots = np.concatenate([labels, bboxes_yolo], axis=1)\n        string = annot2str(annots) # 转为字符格式，比如'0 0.5407407 0.31805557 0.0462963 0.04444447'\n        f.write(string) # 将转换完的标签和yolo格式标注框信息写入label文件夹下对应名字的txt文件，每行为一个目标样本\nprint('Missing:',cnt)","metadata":{"execution":{"iopub.status.busy":"2023-05-24T11:54:42.672399Z","iopub.execute_input":"2023-05-24T11:54:42.672806Z","iopub.status.idle":"2023-05-24T11:54:45.778490Z","shell.execute_reply.started":"2023-05-24T11:54:42.672759Z","shell.execute_reply":"2023-05-24T11:54:45.777013Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import GroupKFold\nkf = GroupKFold(n_splits = 3)\ndf = df.reset_index(drop=True) # 重设索引，之前索引是原始train.csv中的索引，不连续\ndf['fold'] = -1\n# 根据video_id给df每一行添加fold字段，其值等于video_id\nfor fold, (train_idx, val_idx) in enumerate(kf.split(df, groups=df.video_id.tolist())):\n    df.loc[val_idx, 'fold'] = fold\ndisplay(df.fold.value_counts()) # # 三段视频分别有2143、2099和677张有标注的图片","metadata":{"execution":{"iopub.status.busy":"2023-05-24T11:54:45.780701Z","iopub.execute_input":"2023-05-24T11:54:45.781279Z","iopub.status.idle":"2023-05-24T11:54:45.802881Z","shell.execute_reply.started":"2023-05-24T11:54:45.781218Z","shell.execute_reply":"2023-05-24T11:54:45.801242Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 前面设定了FOLD = 1，query引用外部变量，前面要加@\ntrain_files = []\nval_files   = []\ntrain_df = df.query(\"fold!=@FOLD\")\nvalid_df = df.query(\"fold==@FOLD\")\ntrain_files += list(train_df.image_path.unique())\nval_files += list(valid_df.image_path.unique())\nlen(train_files), len(val_files)","metadata":{"execution":{"iopub.status.busy":"2023-05-24T11:54:45.804491Z","iopub.execute_input":"2023-05-24T11:54:45.804868Z","iopub.status.idle":"2023-05-24T11:54:45.827354Z","shell.execute_reply.started":"2023-05-24T11:54:45.804834Z","shell.execute_reply":"2023-05-24T11:54:45.826088Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bbox_df = pd.DataFrame(np.concatenate([bboxes_info, all_bboxes], axis=1),\n             columns=['image_id','video_id','sequence',\n                     'xmid','ymid','w','h'])\nbbox_df[['xmid','ymid','w','h']] = bbox_df[['xmid','ymid','w','h']].astype(float)\nbbox_df['area'] = bbox_df.w * bbox_df.h * 1280 * 720\nbbox_df = bbox_df.merge(df[['image_id','fold']], on='image_id', how='left')\nbbox_df.head(2)","metadata":{"execution":{"iopub.status.busy":"2023-05-24T11:54:45.829048Z","iopub.execute_input":"2023-05-24T11:54:45.829819Z","iopub.status.idle":"2023-05-24T11:54:46.043880Z","shell.execute_reply.started":"2023-05-24T11:54:45.829756Z","shell.execute_reply":"2023-05-24T11:54:46.042221Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib as mpl\nimport seaborn as sns\n\nf, ax = plt.subplots(figsize=(12, 6))\nsns.despine(f)\n\nsns.histplot(\n    bbox_df,\n    x=\"area\", hue=\"fold\",\n    multiple=\"stack\",\n    palette=\"viridis\",\n    edgecolor=\".3\",\n    linewidth=.5,\n    log_scale=True,\n)\nax.xaxis.set_major_formatter(mpl.ticker.ScalarFormatter())\nax.set_xticks([500, 1000, 2000, 5000, 10000]);","metadata":{"execution":{"iopub.status.busy":"2023-05-24T11:54:46.045520Z","iopub.execute_input":"2023-05-24T11:54:46.046606Z","iopub.status.idle":"2023-05-24T11:54:47.761357Z","shell.execute_reply.started":"2023-05-24T11:54:46.046554Z","shell.execute_reply":"2023-05-24T11:54:47.760162Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_val = all_bboxes[...,2]\ny_val = all_bboxes[...,3]\n\n# Calculate the point density\nxy = np.vstack([x_val,y_val])\nz = gaussian_kde(xy)(xy)\n\nfig, ax = plt.subplots(figsize = (10, 10))\n# ax.axis('off')\nax.scatter(x_val, y_val, c=z, s=100, cmap='viridis')\n# ax.set_xlabel('bbox_width')\n# ax.set_ylabel('bbox_height')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-05-24T11:54:47.762912Z","iopub.execute_input":"2023-05-24T11:54:47.763272Z","iopub.status.idle":"2023-05-24T11:54:47.800560Z","shell.execute_reply.started":"2023-05-24T11:54:47.763239Z","shell.execute_reply":"2023-05-24T11:54:47.799018Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df2 = df[(df.num_bbox>0)].sample(100) # takes samples with bbox\ny = 3; x = 2\nplt.figure(figsize=(12.8*x, 7.2*y))\nfor idx in range(x*y):\n    row = df2.iloc[idx]\n    img           = load_image(row.image_path)\n    image_height  = row.height\n    image_width   = row.width\n    with open(row.label_path) as f:\n        annot = str2annot(f.read())\n    bboxes_yolo = annot[...,1:]\n    labels      = annot[..., 0].astype(int).tolist()\n    names         = ['cots']*len(bboxes_yolo)\n    plt.subplot(y, x, idx+1)\n    plt.imshow(draw_bboxes(img = img,\n                           bboxes = bboxes_yolo, \n                           classes = names,\n                           class_ids = labels,\n                           class_name = True, \n                           colors = colors, \n                           bbox_format = 'yolo',\n                           line_thickness = 2))\n    plt.axis('OFF')\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-05-24T11:54:47.801776Z","iopub.status.idle":"2023-05-24T11:54:47.802816Z","shell.execute_reply.started":"2023-05-24T11:54:47.802519Z","shell.execute_reply":"2023-05-24T11:54:47.802548Z"},"trusted":true},"execution_count":null,"outputs":[]}]}