{"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":"markdown","source":"# My learnig pathway\n\nThis is the first image detection model using YOLO v7.  \nDataset is RSNA chest X-ray images.  \nI think this code is too timid to be seen by professionals...  \nIf you have any advices, let me know!!!","metadata":{}},{"cell_type":"markdown","source":"## References\nYOLO v7 official document: https://github.com/WongKinYiu/yolov7  \nA little old, but very insightful code: https://www.kaggle.com/code/seohyeondeok/yolov3-rsna-starting-notebook  \nWritten in Japanese, but very useful!: https://farml1.com/yolov7/  ","metadata":{}},{"cell_type":"code","source":"!git clone https://github.com/WongKinYiu/yolov7.git","metadata":{"execution":{"iopub.status.busy":"2022-10-20T09:39:34.500576Z","iopub.execute_input":"2022-10-20T09:39:34.501002Z","iopub.status.idle":"2022-10-20T09:39:43.321641Z","shell.execute_reply.started":"2022-10-20T09:39:34.500919Z","shell.execute_reply":"2022-10-20T09:39:43.320505Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%pip install -r ./yolov7/requirements.txt","metadata":{"execution":{"iopub.status.busy":"2022-10-20T09:39:43.324486Z","iopub.execute_input":"2022-10-20T09:39:43.324874Z","iopub.status.idle":"2022-10-20T09:39:55.255363Z","shell.execute_reply.started":"2022-10-20T09:39:43.324824Z","shell.execute_reply":"2022-10-20T09:39:55.254096Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\n\ntrain_img_dir = '../input/rsna-pneumonia-dcm-to-jpg-v2/train'\ntest_img_dir = '../input/rsna-pneumonia-dcm-to-jpg-v2/test'\n\nrsna_yolov7_dataset = os.path.join(os.getcwd(), \"./dataset\")\n\nos.mkdir(rsna_yolov7_dataset)","metadata":{"execution":{"iopub.status.busy":"2022-10-20T09:39:55.257119Z","iopub.execute_input":"2022-10-20T09:39:55.257434Z","iopub.status.idle":"2022-10-20T09:39:55.264268Z","shell.execute_reply.started":"2022-10-20T09:39:55.257405Z","shell.execute_reply":"2022-10-20T09:39:55.263344Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset_img = os.path.join(rsna_yolov7_dataset + '/images')\ndataset_label = os.path.join(rsna_yolov7_dataset + '/labels')\n\nfor directory in [dataset_img, dataset_label]:\n    os.mkdir(directory)","metadata":{"execution":{"iopub.status.busy":"2022-10-20T09:39:55.267217Z","iopub.execute_input":"2022-10-20T09:39:55.267971Z","iopub.status.idle":"2022-10-20T09:39:55.273937Z","shell.execute_reply.started":"2022-10-20T09:39:55.267935Z","shell.execute_reply":"2022-10-20T09:39:55.272910Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_train = os.path.join(dataset_img + '/train')\nimage_val = os.path.join(dataset_img + '/val')\nimage_test = os.path.join(dataset_img + '/test')\nlabels_train = os.path.join(dataset_label + '/train')\nlabels_val = os.path.join(dataset_label + '/val')\nlabels_test = os.path.join(dataset_label + '/test')\n\nfor directory in [image_train, image_val, image_test, labels_train, labels_val, labels_test]:\n    os.mkdir(directory)","metadata":{"execution":{"iopub.status.busy":"2022-10-20T09:39:55.275607Z","iopub.execute_input":"2022-10-20T09:39:55.276358Z","iopub.status.idle":"2022-10-20T09:39:55.284812Z","shell.execute_reply.started":"2022-10-20T09:39:55.276283Z","shell.execute_reply":"2022-10-20T09:39:55.283756Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\n\nannots = pd.read_csv('../input/rsna-pneumonia-detection-challenge/stage_2_train_labels.csv')\nannots = annots[annots.Target == 1]\nannots.head()","metadata":{"execution":{"iopub.status.busy":"2022-10-20T09:39:55.286470Z","iopub.execute_input":"2022-10-20T09:39:55.287270Z","iopub.status.idle":"2022-10-20T09:39:55.396203Z","shell.execute_reply.started":"2022-10-20T09:39:55.287235Z","shell.execute_reply":"2022-10-20T09:39:55.395251Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\npatient_id_series = annots.patientId.drop_duplicates()\n\ntr_series, val_series = train_test_split(patient_id_series, test_size=0.1, random_state=42)\nprint(\"The # of train set: {}, The # of validation set: {}\".format(tr_series.shape[0], val_series.shape[0]))","metadata":{"execution":{"iopub.status.busy":"2022-10-20T09:39:55.397709Z","iopub.execute_input":"2022-10-20T09:39:55.398059Z","iopub.status.idle":"2022-10-20T09:39:55.930546Z","shell.execute_reply.started":"2022-10-20T09:39:55.398025Z","shell.execute_reply":"2022-10-20T09:39:55.929526Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import shutil\n\nfor id in tr_series:\n    shutil.copy(train_img_dir+'/{}.jpg'.format(id), image_train)\n    \nfor id in val_series:\n    shutil.copy(train_img_dir+'/{}.jpg'.format(id), image_val)","metadata":{"execution":{"iopub.status.busy":"2022-10-20T09:40:14.648051Z","iopub.execute_input":"2022-10-20T09:40:14.648405Z","iopub.status.idle":"2022-10-20T09:40:29.477137Z","shell.execute_reply.started":"2022-10-20T09:40:14.648376Z","shell.execute_reply":"2022-10-20T09:40:29.476132Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import glob\n\ntest_imgs = glob.glob(test_img_dir+'/*.jpg')\nfor img in test_imgs:\n    shutil.copy(img, image_test)","metadata":{"execution":{"iopub.status.busy":"2022-10-20T09:40:29.479109Z","iopub.execute_input":"2022-10-20T09:40:29.479509Z","iopub.status.idle":"2022-10-20T09:40:39.758541Z","shell.execute_reply.started":"2022-10-20T09:40:29.479473Z","shell.execute_reply":"2022-10-20T09:40:39.757400Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def save_label(label_dir, patient_id, row=None):\n    # rsna defualt image size\n    img_size = 1024\n    label_fp = os.path.join(label_dir, \"{}.txt\".format(patient_id))\n    \n    f = open(label_fp, \"a\")\n    if row is None:\n        f.close()\n        return\n    \n    top_left_x = row[1]\n    top_left_y = row[2]\n    w = row[3]\n    h = row[4]\n\n    # 'r' means relative. 'c' means center.\n    rx = top_left_x/img_size\n    ry = top_left_y/img_size\n    rw = w/img_size\n    rh = h/img_size\n    rcx = rx+rw/2\n    rcy = ry+rh/2\n\n    line = \"{} {} {} {} {}\\n\".format(0, rcx, rcy, rw, rh)\n\n    f.write(line)\n    f.close()","metadata":{"execution":{"iopub.status.busy":"2022-10-20T09:40:39.761161Z","iopub.execute_input":"2022-10-20T09:40:39.761438Z","iopub.status.idle":"2022-10-20T09:40:39.768984Z","shell.execute_reply.started":"2022-10-20T09:40:39.761413Z","shell.execute_reply":"2022-10-20T09:40:39.767961Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_dir = os.path.join(os.getcwd(), \"labels\")\nos.mkdir(labels_dir)","metadata":{"execution":{"iopub.status.busy":"2022-10-20T09:40:39.771892Z","iopub.execute_input":"2022-10-20T09:40:39.772936Z","iopub.status.idle":"2022-10-20T09:40:39.782146Z","shell.execute_reply.started":"2022-10-20T09:40:39.772909Z","shell.execute_reply":"2022-10-20T09:40:39.781216Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for x in annots.values:\n    id = x[0]\n    save_label(labels_dir, id, x)","metadata":{"execution":{"iopub.status.busy":"2022-10-20T09:40:39.783867Z","iopub.execute_input":"2022-10-20T09:40:39.784524Z","iopub.status.idle":"2022-10-20T09:40:40.187257Z","shell.execute_reply.started":"2022-10-20T09:40:39.784489Z","shell.execute_reply":"2022-10-20T09:40:40.186072Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for id in tr_series:\n    shutil.copy('./labels/{}.txt'.format(id), labels_train)\n    \nfor id in val_series:\n    shutil.copy('./labels/{}.txt'.format(id), labels_val)","metadata":{"execution":{"iopub.status.busy":"2022-10-20T09:40:40.189003Z","iopub.execute_input":"2022-10-20T09:40:40.189391Z","iopub.status.idle":"2022-10-20T09:40:40.700795Z","shell.execute_reply.started":"2022-10-20T09:40:40.189352Z","shell.execute_reply":"2022-10-20T09:40:40.699834Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_extention_file_path = os.path.join('./yolov7/data', 'dataset.yaml')\nwith open(data_extention_file_path, 'w') as f:\n    contents =\"\"\"\ntrain: ../dataset/images/train \nval: ../dataset/images/val \ntest: ../dataset/images/test\n\nis_coco: False\n\n# Classes\nnc: 1  \nnames: ['pneumonia']  \n    \"\"\"\n    f.write(contents)","metadata":{"execution":{"iopub.status.busy":"2022-10-20T09:40:40.702035Z","iopub.execute_input":"2022-10-20T09:40:40.702375Z","iopub.status.idle":"2022-10-20T09:40:40.710278Z","shell.execute_reply.started":"2022-10-20T09:40:40.702333Z","shell.execute_reply":"2022-10-20T09:40:40.708408Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!cat ./yolov7/data/dataset.yaml","metadata":{"execution":{"iopub.status.busy":"2022-10-20T09:40:40.712070Z","iopub.execute_input":"2022-10-20T09:40:40.712583Z","iopub.status.idle":"2022-10-20T09:40:41.698706Z","shell.execute_reply.started":"2022-10-20T09:40:40.712538Z","shell.execute_reply":"2022-10-20T09:40:41.697587Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.chdir(\"./yolov7\")","metadata":{"execution":{"iopub.status.busy":"2022-10-20T09:40:56.085405Z","iopub.execute_input":"2022-10-20T09:40:56.086420Z","iopub.status.idle":"2022-10-20T09:40:56.090897Z","shell.execute_reply.started":"2022-10-20T09:40:56.086381Z","shell.execute_reply":"2022-10-20T09:40:56.089955Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!wget -q https://github.com/WongKinYiu/yolov7/releases/download/v0.1/yolov7_training.pt","metadata":{"execution":{"iopub.status.busy":"2022-10-20T09:41:05.307428Z","iopub.execute_input":"2022-10-20T09:41:05.308523Z","iopub.status.idle":"2022-10-20T09:41:17.100102Z","shell.execute_reply.started":"2022-10-20T09:41:05.308467Z","shell.execute_reply":"2022-10-20T09:41:17.098809Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.mkdir('weights')","metadata":{"execution":{"iopub.status.busy":"2022-10-20T09:41:17.103764Z","iopub.execute_input":"2022-10-20T09:41:17.104494Z","iopub.status.idle":"2022-10-20T09:41:17.110554Z","shell.execute_reply.started":"2022-10-20T09:41:17.104431Z","shell.execute_reply":"2022-10-20T09:41:17.109376Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"shutil.move('./yolov7_training.pt', 'weights')","metadata":{"execution":{"iopub.status.busy":"2022-10-20T09:41:17.112320Z","iopub.execute_input":"2022-10-20T09:41:17.113494Z","iopub.status.idle":"2022-10-20T09:41:17.122685Z","shell.execute_reply.started":"2022-10-20T09:41:17.113440Z","shell.execute_reply":"2022-10-20T09:41:17.121370Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import wandb","metadata":{"execution":{"iopub.status.busy":"2022-10-20T09:41:24.006526Z","iopub.execute_input":"2022-10-20T09:41:24.006900Z","iopub.status.idle":"2022-10-20T09:41:24.602995Z","shell.execute_reply.started":"2022-10-20T09:41:24.006867Z","shell.execute_reply":"2022-10-20T09:41:24.601852Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"wandb.login(key='your wandb API key')","metadata":{"execution":{"iopub.status.busy":"2022-10-20T09:41:25.379745Z","iopub.execute_input":"2022-10-20T09:41:25.383370Z","iopub.status.idle":"2022-10-20T09:41:29.898876Z","shell.execute_reply.started":"2022-10-20T09:41:25.383312Z","shell.execute_reply":"2022-10-20T09:41:29.897735Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Below code is training.  \nIt took too much time to be completed...","metadata":{}},{"cell_type":"code","source":"!python train.py --workers 8 --adam  --batch-size 8  --data data/dataset.yaml --img 1024 1024 --cfg cfg/training/yolov7.yaml --weights './weights/yolov7_training.pt' --name yolov7_rsna_pneumonia","metadata":{"execution":{"iopub.status.busy":"2022-10-20T09:41:32.439057Z","iopub.execute_input":"2022-10-20T09:41:32.439421Z","iopub.status.idle":"2022-10-20T09:43:38.685788Z","shell.execute_reply.started":"2022-10-20T09:41:32.439386Z","shell.execute_reply":"2022-10-20T09:43:38.684430Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Evolve hyperparamaters\n!python train.py --workers 8\n                --adam\n                --batch-size 8\n                --data data/dataset.yaml\n                --img 1024 1024\n                --cfg cfg/training/yolov7.yaml\n                --weights './weights/yolov7_training.pt'\n                --name yolov7_evolve__rsna_pneumonia\n                --evolve","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Infer the test datas using trained model\n!python detect.py --source ./data/inference/ #inference image data\n                 --weights ./runs/train/exp/weights/last.pt #trained model's weight\n                 --conf 0.3 #threshold to evaluate positive\n                 --line-thickness 6 ","metadata":{},"execution_count":null,"outputs":[]}]}