{"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":"# 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\n#for 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","execution":{"iopub.status.busy":"2022-08-29T12:01:31.386415Z","iopub.execute_input":"2022-08-29T12:01:31.387231Z","iopub.status.idle":"2022-08-29T12:01:31.417010Z","shell.execute_reply.started":"2022-08-29T12:01:31.387130Z","shell.execute_reply":"2022-08-29T12:01:31.416093Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd \nimport numpy as np \nimport seaborn as sns\nimport matplotlib.pyplot as plt \nimport cv2 as cv \nimport os \nimport torch \nfrom  torch import nn\nimport albumentations as A\nfrom albumentations.pytorch import ToTensorV2\nimport torch.nn.functional as F\nfrom torch.utils.data import Dataset,DataLoader\nfrom torch import optim\nfrom torchvision import transforms\nfrom torch.utils.data import random_split\nfrom tqdm import tqdm\nfrom albumentations.pytorch import ToTensorV2\nfrom matplotlib import patches\nimport os\nfrom bs4 import BeautifulSoup\nimport json\nimport re\nfrom torchmetrics.detection.mean_ap import MeanAveragePrecision","metadata":{"execution":{"iopub.status.busy":"2022-09-01T10:32:27.075570Z","iopub.execute_input":"2022-09-01T10:32:27.075976Z","iopub.status.idle":"2022-09-01T10:32:31.723015Z","shell.execute_reply.started":"2022-09-01T10:32:27.075895Z","shell.execute_reply":"2022-09-01T10:32:31.722057Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = '../input/global-wheat-detection'\ndata=pd.read_csv('../input/global-wheat-detection/train.csv')\ndata.head(5)","metadata":{"execution":{"iopub.status.busy":"2022-09-01T10:32:31.724787Z","iopub.execute_input":"2022-09-01T10:32:31.725435Z","iopub.status.idle":"2022-09-01T10:32:31.999618Z","shell.execute_reply.started":"2022-09-01T10:32:31.725397Z","shell.execute_reply":"2022-09-01T10:32:31.998728Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data1 = pd.read_csv('../input/global-wheat-detection/train.csv')\ndata2  = data1[data1['image_id'] == 'b6ab77fd7'] \nfor i in range(0,1):\n    a = json.loads(data2.iloc[i]['bbox'])\n    image = cv.imread('../input/global-wheat-detection/train/b6ab77fd7.jpg')[::,::,::-1]\n    fig, ax = plt.subplots(figsize=(10, 10))\n    ax.imshow(image)\n    for i in data2['bbox']:\n        a = json.loads(i)\n        xmin,ymin,width,height = a\n        rect = patches.Rectangle((xmin,ymin),width,height, linewidth=1, edgecolor='r', facecolor='none')\n        ax.add_patch(rect)\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-09-01T10:32:32.001577Z","iopub.execute_input":"2022-09-01T10:32:32.001929Z","iopub.status.idle":"2022-09-01T10:32:32.806410Z","shell.execute_reply.started":"2022-09-01T10:32:32.001893Z","shell.execute_reply":"2022-09-01T10:32:32.805453Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_list = data.image_id.unique()[[100,200,300,400,500,600,900,1110,1220,1890]]\ntrain_df = data.copy()\ntest_df = pd.DataFrame()\nfor i in test_list:\n    df2 = train_df[train_df['image_id'] == i]\n    train_df = train_df.drop(train_df[train_df['image_id'] == i].index,axis = 0)\n    test_df = pd.concat([test_df,df2])\ntrain_df = train_df.reset_index(drop = True)\ntest_df = test_df.reset_index(drop = True)\nprint(test_df.image_id.nunique())\nprint(train_df.image_id.nunique())","metadata":{"execution":{"iopub.status.busy":"2022-09-01T10:33:12.053239Z","iopub.execute_input":"2022-09-01T10:33:12.053828Z","iopub.status.idle":"2022-09-01T10:33:12.727357Z","shell.execute_reply.started":"2022-09-01T10:33:12.053782Z","shell.execute_reply":"2022-09-01T10:33:12.726214Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df[['x','y','w','h']] = 0\ntrain_df[['x','y','w','h']]= np.stack(train_df['bbox'].apply(lambda x:np.fromstring(x[1:-1], sep=','))).astype(np.float)\ntrain_df.drop(columns=['bbox','source'], inplace=True)\ntrain_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-09-01T10:33:17.319745Z","iopub.execute_input":"2022-09-01T10:33:17.320112Z","iopub.status.idle":"2022-09-01T10:33:17.749443Z","shell.execute_reply.started":"2022-09-01T10:33:17.320062Z","shell.execute_reply":"2022-09-01T10:33:17.748306Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df[['x','y','w','h']] = 0\ntest_df[['x','y','w','h']]= np.stack(test_df['bbox'].apply(lambda x:np.fromstring(x[1:-1], sep=','))).astype(np.float)\ntest_df.drop(columns=['bbox','source'], inplace=True)\ntest_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-09-01T10:33:26.944680Z","iopub.execute_input":"2022-09-01T10:33:26.945093Z","iopub.status.idle":"2022-09-01T10:33:26.969257Z","shell.execute_reply.started":"2022-09-01T10:33:26.945042Z","shell.execute_reply":"2022-09-01T10:33:26.968111Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class WheatDataset(Dataset):\n    def __init__(self, dataframe, image_dir, transforms=None):\n        super().__init__()\n\n        self.image_ids = dataframe['image_id'].unique()\n        self.df = dataframe\n        self.image_dir = image_dir\n        self.transforms = transforms\n        \n        \n    def __getitem__(self, index: int):\n        image_id = self.image_ids[index]\n        records = self.df[self.df['image_id'] == image_id]\n        image = cv.imread(f'{self.image_dir}/{image_id}.jpg', cv.IMREAD_COLOR)\n        image = cv.cvtColor(image, cv.COLOR_BGR2RGB)\n        boxes = records[['x', 'y', 'w', 'h']].values\n        boxes[:, 2] = boxes[:, 0] + boxes[:, 2]\n        boxes[:, 3] = boxes[:, 1] + boxes[:, 3]\n        labels = torch.ones((records.shape[0],), dtype=torch.int64)\n        target = {}\n        target['boxes'] = boxes\n        target['labels'] = labels\n        if self.transforms:\n            sample = {'image': image,'bboxes': target['boxes'],'labels': labels}\n            sample = self.transforms(**sample)\n            image = sample['image']\n            target['boxes'] = torch.stack(tuple(map(torch.tensor, zip(*sample['bboxes'])))).permute(1,0)\n            target['boxes'][:,[0,1,2,3]] = target['boxes'][:,[1,0,3,2]]\n        return image, target, image_id\n\n    def __len__(self) -> int:\n        return self.image_ids.shape[0]","metadata":{"execution":{"iopub.status.busy":"2022-09-01T10:33:34.256021Z","iopub.execute_input":"2022-09-01T10:33:34.256720Z","iopub.status.idle":"2022-09-01T10:33:34.267948Z","shell.execute_reply.started":"2022-09-01T10:33:34.256684Z","shell.execute_reply":"2022-09-01T10:33:34.266849Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IMAGE_SIZE = 512\ntrain_aug = A.Compose([A.Resize(IMAGE_SIZE,IMAGE_SIZE ,p = 1.0),\n                       A.ToGray(p=0.1),\n                       A.HorizontalFlip(p=0.5),\n                       A.VerticalFlip(p=0.5),ToTensorV2()],\n                      p=1.0,bbox_params=A.BboxParams(format=\"pascal_voc\", label_fields=[\"labels\"]))\n\ntest_aug = A.Compose([A.Resize(IMAGE_SIZE,IMAGE_SIZE ,p = 1.0),ToTensorV2()],\n                      p=1.0,bbox_params=A.BboxParams(format=\"pascal_voc\", label_fields=[\"labels\"]))\n","metadata":{"execution":{"iopub.status.busy":"2022-09-01T10:33:36.172145Z","iopub.execute_input":"2022-09-01T10:33:36.173209Z","iopub.status.idle":"2022-09-01T10:33:36.179922Z","shell.execute_reply.started":"2022-09-01T10:33:36.173165Z","shell.execute_reply":"2022-09-01T10:33:36.178992Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data= WheatDataset(train_df ,'../input/global-wheat-detection/train',train_aug)\nvalid_data= WheatDataset(test_df ,'../input/global-wheat-detection/train',test_aug)","metadata":{"execution":{"iopub.status.busy":"2022-09-01T10:33:39.000884Z","iopub.execute_input":"2022-09-01T10:33:39.001270Z","iopub.status.idle":"2022-09-01T10:33:39.018406Z","shell.execute_reply.started":"2022-09-01T10:33:39.001238Z","shell.execute_reply":"2022-09-01T10:33:39.017501Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in train_data:\n    print(i[0].shape)\n    plt.figure(figsize = (16,10))\n    plt.imshow(np.transpose(np.array(i[0]),[1,2,0]))\n    break","metadata":{"execution":{"iopub.status.busy":"2022-09-01T10:33:46.052385Z","iopub.execute_input":"2022-09-01T10:33:46.052748Z","iopub.status.idle":"2022-09-01T10:33:46.465570Z","shell.execute_reply.started":"2022-09-01T10:33:46.052717Z","shell.execute_reply":"2022-09-01T10:33:46.464752Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in train_data:\n    image = np.transpose(np.array(i[0]),[1,2,0])\n    boxes = i[1]['boxes'].numpy().astype(np.int32)\n    fig, ax = plt.subplots(1, 1, figsize=(16, 8))\n    for box in boxes:\n        cv.rectangle(image,(box[1], box[0]), (box[3],  box[2]), (225, 1, 60), 2)\n    ax.set_axis_off()\n    ax.imshow(image)\n    break","metadata":{"execution":{"iopub.status.busy":"2022-09-01T10:34:04.115700Z","iopub.execute_input":"2022-09-01T10:34:04.116096Z","iopub.status.idle":"2022-09-01T10:34:04.443825Z","shell.execute_reply.started":"2022-09-01T10:34:04.116049Z","shell.execute_reply":"2022-09-01T10:34:04.442980Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in valid_data:\n    image = np.transpose(np.array(i[0]),[1,2,0])\n    boxes = i[1]['boxes'].numpy().astype(np.int32)\n    fig, ax = plt.subplots(1, 1, figsize=(16, 8))\n    for box in boxes:\n        cv.rectangle(image,(box[1], box[0]), (box[3],  box[2]), (225, 1, 60), 2)\n    ax.set_axis_off()\n    ax.imshow(image)\n    break","metadata":{"execution":{"iopub.status.busy":"2022-09-01T10:34:32.030786Z","iopub.execute_input":"2022-09-01T10:34:32.031525Z","iopub.status.idle":"2022-09-01T10:34:32.341877Z","shell.execute_reply.started":"2022-09-01T10:34:32.031488Z","shell.execute_reply":"2022-09-01T10:34:32.341019Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def collate_fn(batch):\n    return tuple(zip(*batch))\n\ntrain_loader =DataLoader(train_data,batch_size=16,shuffle=True,collate_fn=collate_fn)\nvalid_loader =DataLoader(valid_data,batch_size=10,collate_fn=collate_fn)","metadata":{"execution":{"iopub.status.busy":"2022-09-01T10:34:45.877195Z","iopub.execute_input":"2022-09-01T10:34:45.878154Z","iopub.status.idle":"2022-09-01T10:34:45.884004Z","shell.execute_reply.started":"2022-09-01T10:34:45.878105Z","shell.execute_reply":"2022-09-01T10:34:45.882929Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for batch in train_loader:\n    plt.imshow(np.transpose(np.array(batch[0][1]),[1,2,0])) \n    break","metadata":{"execution":{"iopub.status.busy":"2022-09-01T10:34:47.755864Z","iopub.execute_input":"2022-09-01T10:34:47.756286Z","iopub.status.idle":"2022-09-01T10:34:48.734867Z","shell.execute_reply.started":"2022-09-01T10:34:47.756253Z","shell.execute_reply":"2022-09-01T10:34:48.733928Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for batch in valid_loader:\n    plt.imshow(np.transpose(np.array(batch[0][1]),[1,2,0])) \n    break","metadata":{"execution":{"iopub.status.busy":"2022-09-01T10:35:03.514403Z","iopub.execute_input":"2022-09-01T10:35:03.514781Z","iopub.status.idle":"2022-09-01T10:35:04.100030Z","shell.execute_reply.started":"2022-09-01T10:35:03.514750Z","shell.execute_reply":"2022-09-01T10:35:04.099011Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"! pip install effdet","metadata":{"execution":{"iopub.status.busy":"2022-09-01T10:35:13.251908Z","iopub.execute_input":"2022-09-01T10:35:13.252284Z","iopub.status.idle":"2022-09-01T10:35:53.320993Z","shell.execute_reply.started":"2022-09-01T10:35:13.252253Z","shell.execute_reply":"2022-09-01T10:35:53.319835Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from effdet import create_model","metadata":{"execution":{"iopub.status.busy":"2022-09-01T10:35:53.324328Z","iopub.execute_input":"2022-09-01T10:35:53.324981Z","iopub.status.idle":"2022-09-01T10:35:54.476413Z","shell.execute_reply.started":"2022-09-01T10:35:53.324939Z","shell.execute_reply":"2022-09-01T10:35:54.475307Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = create_model('efficientdet_d0' , bench_task='train' , num_classes=1 , image_size=(IMAGE_SIZE,IMAGE_SIZE),bench_labeler=True,pretrained=True)","metadata":{"execution":{"iopub.status.busy":"2022-09-01T10:36:30.753262Z","iopub.execute_input":"2022-09-01T10:36:30.754249Z","iopub.status.idle":"2022-09-01T10:36:32.942097Z","shell.execute_reply.started":"2022-09-01T10:36:30.754208Z","shell.execute_reply":"2022-09-01T10:36:32.941048Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epochs = 10\nlr = 0.0001","metadata":{"execution":{"iopub.status.busy":"2022-09-01T10:36:54.808837Z","iopub.execute_input":"2022-09-01T10:36:54.809270Z","iopub.status.idle":"2022-09-01T10:36:54.814485Z","shell.execute_reply.started":"2022-09-01T10:36:54.809237Z","shell.execute_reply":"2022-09-01T10:36:54.813270Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = 'cuda' if torch.cuda.is_available() else 'cpu'\nmodel = model.to(device)\noptimizer = optim.Adam(model.parameters(), lr=lr)","metadata":{"execution":{"iopub.status.busy":"2022-09-01T10:37:08.465024Z","iopub.execute_input":"2022-09-01T10:37:08.465516Z","iopub.status.idle":"2022-09-01T10:37:08.490112Z","shell.execute_reply.started":"2022-09-01T10:37:08.465465Z","shell.execute_reply":"2022-09-01T10:37:08.489198Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(1,epochs+1):\n    loss1= 0\n    c_loss1 = 0\n    b_loss1 = 0\n    model.train()\n    for images, targets, image_ids in tqdm(train_loader):\n        images = torch.stack(images)\n        images = images.to(device).float()\n        boxes = [target['boxes'].to(device).float() for target in targets]\n        labels = [target['labels'].to(device).float() for target in targets]\n        target = {}\n        target['bbox'] = boxes\n        target['cls'] = labels\n        output = model(images,target)\n        l1 = output['loss']\n        c1 = output['class_loss']\n        b1 = output['box_loss']\n        optimizer.zero_grad()\n        l1.backward()\n        optimizer.step()\n        loss1 = loss1+l1\n        c_loss1 = c_loss1+c1\n        b_loss1 = b_loss1+b1\n        \n    loss2= 0\n    c_loss2 = 0\n    b_loss2 = 0\n    model.eval()\n    for images, targets, image_ids in tqdm(valid_loader):\n        images = torch.stack(images)\n        images = images.to(device).float()\n        boxes = [target['boxes'].to(device).float() for target in targets]\n        labels = [target['labels'].to(device).float() for target in targets]\n        target = {}\n        target['bbox'] = boxes\n        target['cls'] = labels\n        target[\"img_scale\"] = torch.tensor([1.0] *len(images), dtype=torch.float).to(device)\n        target[\"img_size\"] = torch.tensor([images[0].shape[-2:]] * len(images), dtype=torch.float).to(device)\n        with torch.no_grad():\n            output = model(images,target)\n        l2 = output['loss']\n        c2 = output['class_loss']\n        b2 = output['box_loss']\n        loss2 = loss2+l2\n        c_loss2 = c_loss2+c2\n        b_loss2 = b_loss2+b2\n        \n    print(f'For epoch {i}/{epochs}')\n    print(f'average train total_loss {loss1/len(train_loader)},average val total_loss {loss2}')\n    print(f'average train class_loss {c_loss1/len(train_loader)},average val class_loss {c_loss2}')\n    print(f'average train box_loss {b_loss1/len(train_loader)},average val box_loss {b_loss2}' )","metadata":{"execution":{"iopub.status.busy":"2022-09-01T10:37:22.338367Z","iopub.execute_input":"2022-09-01T10:37:22.338727Z","iopub.status.idle":"2022-09-01T11:14:12.195840Z","shell.execute_reply.started":"2022-09-01T10:37:22.338698Z","shell.execute_reply":"2022-09-01T11:14:12.194760Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"torch.save(model.state_dict(),'./sub-6')","metadata":{"execution":{"iopub.status.busy":"2022-09-01T05:05:11.744322Z","iopub.execute_input":"2022-09-01T05:05:11.744699Z","iopub.status.idle":"2022-09-01T05:05:11.853370Z","shell.execute_reply.started":"2022-09-01T05:05:11.744669Z","shell.execute_reply":"2022-09-01T05:05:11.852283Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"box_true = []\nfor box in target['bbox']:\n    box = box.cpu()\n    box[:, 2] = box[:, 2] - box[:, 0]\n    box[:, 3] = box[:, 3] - box[:, 1]\n    box[:,[0,1,2,3]] = box[:,[1,0,3,2]]\n    dic = {}\n    dic['boxes'] = box\n    dic['boxes'][:,[0,1,2,3]] = dic['boxes'][:,[0,1,2,3]]\n    dic[\"labels\"] = torch.tensor([1.0] *len(box))\n    box_true.append(dic)\nboxes_pred = []\nscores2 =[]\nscore_threshold2 = 0.5\nfor boxes in output['detections']:\n    boxes = boxes.detach().cpu()\n    bboxes = boxes[:,:4]\n    scores = boxes[:,4]\n    labels = boxes[:,5]\n    indexes = np.where(scores >= 0)[0]\n    boxes1 = bboxes[indexes]\n    scores = scores[indexes]\n    boxes1[:, 2] = boxes1[:, 2] - boxes1[:, 0]\n    boxes1[:, 3] = boxes1[:, 3] - boxes1[:, 1]\n    dic = {}\n    dic['boxes'] = boxes1\n    dic['scores'] = scores\n    dic['labels'] = labels\n    boxes_pred.append(dic)","metadata":{"execution":{"iopub.status.busy":"2022-09-01T05:05:21.649009Z","iopub.execute_input":"2022-09-01T05:05:21.649678Z","iopub.status.idle":"2022-09-01T05:05:21.663766Z","shell.execute_reply.started":"2022-09-01T05:05:21.649641Z","shell.execute_reply":"2022-09-01T05:05:21.662507Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"score = MeanAveragePrecision(box_format= 'xywh',iou_type = 'bbox',iou_thresholds=[0.5,0.55,0.6,0.65,0.7,0.75])\nscore.update(boxes_pred,box_true)\nscore.compute()","metadata":{"execution":{"iopub.status.busy":"2022-09-01T05:05:23.048879Z","iopub.execute_input":"2022-09-01T05:05:23.049255Z","iopub.status.idle":"2022-09-01T05:05:23.657031Z","shell.execute_reply.started":"2022-09-01T05:05:23.049224Z","shell.execute_reply":"2022-09-01T05:05:23.655875Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n        ","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}