{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":114094,"databundleVersionId":13610510,"sourceType":"competition"},{"sourceId":642121,"sourceType":"modelInstanceVersion","isSourceIdPinned":true,"modelInstanceId":484259,"modelId":499747}],"dockerImageVersionId":31192,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!pip install albumentations --quiet","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!pip install effdet --quiet","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import cv2\nimport matplotlib.pyplot as plt\nimport pandas as pd\nimport numpy as np\nimport torch\nimport os\nimport albumentations as A\nimport effdet\nimport timm\nfrom torch.utils.data import DataLoader\nfrom effdet import get_efficientdet_config, EfficientDet, DetBenchTrain, DetBenchPredict\nfrom effdet.efficientdet import HeadNet\nfrom torch.optim import AdamW\nfrom tqdm import tqdm","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"img = '/kaggle/input/grocery-items-multi-class-object-detection/train/train/images/000000013.png'\nimg = cv2.imread(img)\nimg = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\nph,pw,_ = img.shape\nimg_label_details = '/kaggle/input/grocery-items-multi-class-object-detection/train/train/labels/000000013.txt'\nimg_label = open (img_label_details).readlines()\nbboxes = []\nclasses = []\n\nfor i in img_label:\n    if not i:\n        continue\n    i =i.split()\n    classes.append(i[0])\n    cx,cy,w,h = map(float,i[1:])\n    cx,cy,w,h = cx* pw, cy * ph, w * pw, h * ph\n    cx,cy,w,h = int(cx - w/2), int(cy - h/2), int(cx + w/2), int(cy + h/2)\n    \n    xmin, ymin, xmax, ymax = cx,cy,w,h \n    bboxes.append([xmin,ymin,xmax,ymax])\n    cv2.rectangle(img, (xmin,ymin), (xmax, ymax),(0,255,0), 4 )\nplt.imshow(img)\nprint('Shape', img.shape, 'bbox:', bboxes, 'cls:', classes)\n","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class grocery_dataset(torch.utils.data.Dataset):\n    def __init__(self,root,transform = None):\n        self.root = root\n        self.transform = transform\n        self.imgs = list(sorted(os.listdir(os.path.join(root,'images'))))\n        self.labels = list(sorted(os.listdir(os.path.join(root,'labels'))))\n\n       \n    def __len__(self):\n        return len(self.imgs)\n\n    def __getitem__(self,idx):\n        img_path = os.path.join(self.root,'images',self.imgs[idx])\n        label_path = os.path.join(self.root,'labels',self.labels[idx])\n        \n        img = cv2.imread(img_path)\n        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n        ph,pw,_ = img.shape\n        \n        img_label = open (label_path).readlines()\n        bboxes = []\n        classes = []\n         \n        for i in img_label:\n            if not i:\n                continue\n            i =i.split()\n            classes.append(i[0])\n            cx,cy,w,h = map(float,i[1:])\n            cx,cy,w,h = cx* pw, cy * ph, w * pw, h * ph\n            cx,cy,w,h = int(cx - w/2), int(cy - h/2), int(cx + w/2), int(cy + h/2)\n            \n            xmin, ymin, xmax, ymax = cx,cy,w,h \n            bboxes.append([xmin,ymin,xmax,ymax])\n\n        bboxes = np.array(bboxes, dtype = np.float32)\n        classes = np.array(classes, dtype = np.int64)\n\n\n\n        if self.transform:\n            transformed = self.transform(image=img, bboxes= bboxes, classes = classes)\n            t_img = transformed['image']\n            _,new_height,new_width = t_img.shape\n            t_classes = transformed['classes']\n            t_bboxes = transformed[\"bboxes\"]\n            for box in t_bboxes:\n                box[0],box[1],box[2],box[3] = box[1],box[0],box[3],box[2]\n            \n\n        target = {\n            \"bbox\": torch.as_tensor(t_bboxes, dtype = torch.float32),\n            \"classes\": torch.as_tensor(t_classes, dtype = torch.int64),\n            \"img_size\": (new_height,new_width),\n            \"img_scale\": torch.tensor([1.0])\n        }\n        \n        return t_img, target","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"img_size =512","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"transform= A.Compose([A.Resize(height=img_size,width=img_size, p = 1),A.ToTensorV2()],\n                     bbox_params=A.BboxParams(format=\"pascal_voc\", label_fields = ['classes'] ))\neval_transformation = A.Compose([A.Resize(height = img_size,width = img_size, p =1),A.ToTensorV2()],\n                                bbox_params=A.BboxParams(format=\"pascal_voc\",label_fields = ['classes']))","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def collate_fn(batch):\n        images, targets = zip(*batch)\n        #images = torch.stack(images)\n        #images = images.float()\n        bboxes = [target[\"bbox\"].float() for target in targets]\n        classes = [target[\"classes\"].float() for target in targets]\n        img_size = torch.tensor([target[\"img_size\"] for target in targets]).float()\n        img_scale = torch.tensor([target[\"img_scale\"] for target in targets]).float()\n    \n        details = {\n            \"bbox\": bboxes,\n            \"cls\": classes,\n            \"img_size\":None,\n            \"img_scale\":None\n        }\n        return images,details","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_root = '/kaggle/input/grocery-items-multi-class-object-detection/train/train/'\nval_root = '/kaggle/input/grocery-items-multi-class-object-detection/val/val/'\n\ndataset = grocery_dataset(train_root,transform)\neval_dataset = grocery_dataset(val_root,eval_transformation)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"dataset[1]","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"Train_loader = torch.utils.data.DataLoader(dataset, batch_size = 4, collate_fn = collate_fn, shuffle = False)\nEvaluation_loader = torch.utils.data.DataLoader(eval_dataset, batch_size = 4, collate_fn= collate_fn, shuffle = False)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.to(device)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def dev(t):\n    for i in range(len(t[\"bbox\"])):\n        t[\"bbox\"][i]=t[\"bbox\"][i].float().to(device)\n        t[\"cls\"][i]=t[\"cls\"][i].float().to(device)\n    torch.cuda.empty_cache()\n    return t\n    ","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"epochs=10\nfor i in range(epochs):\n    valloss,bxl,clsloss,runLoss=0.0,0.0,0.0,0.0\n    model.train()\n    for img,t in tqdm(Train_loader):\n        #print(t)\n        img=torch.stack(img).float().to(device)\n        torch.cuda.empty_cache()\n        optimizer.zero_grad()\n        ld=model(img,dev(t))\n        loss=ld[\"loss\"]\n        runLoss+=loss\n        loss.backward()\n        optimizer.step()\n        \n        torch.cuda.empty_cache()\n        break\n    print(\"train loss: \",runLoss/len(Train_loader))\n    model.eval()\n    for img,t in tqdm(Train_loader):\n        #print(t)\n        img=torch.stack(img).float().to(device)\n        torch.cuda.empty_cache()\n        ld=model(img,dev(t))\n        valloss+=ld[\"loss\"]\n        det=ld[\"detections\"]\n        bxl+=ld[\"box_loss\"]\n        clsloss+=ld[\"class_loss\"]\n        lll=len(Evaluation_loader)\n        torch.cuda.empty_cache()\n    \n    print(\"train loss: \",valloss/len(Train_loader),\"bxloss: \",bxl/lll,\"clsloss: \",clsloss/lll)\n\n        \n        \n        \n    \n    ","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for b in Train_loader:\n    # img = zip(*b)\n    # plt.imshow(img[0].numpy().transpose(1,2,0))\n    # plt.show()\n    image,t = b\n    print(t)\n    for img in image:\n        plt.imshow(img.numpy().transpose(1,2,0))\n        plt.show()\n       \n    break","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"device = torch.device('cuda')","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"config = get_efficientdet_config('efficientdetv2_dt')\nconfig.num_classes = 3\nconfig.image_size = (img_size,img_size)\nconfig.legacy_focal = False\nconfig.pad_type = 'same'\n\nnet = EfficientDet(config,pretrained_backbone = True)\nnet.class_net = HeadNet(config, num_outputs=config.num_classes)\nmodel =  DetBenchTrain(net,config)\nmodel.to(device)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import timm\nfrom effdet.config.model_config import efficientdet_model_param_dict\nfrom effdet.config.model_config import efficientdet_model_param_dict","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"timm.list_models('tf_efficientnetv2_*')","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def tf_efficientnetv2_b1():\n    efficientdet_model_param_dict['tf_efficientnetv2_b1'] = dict(\n        name='tf_efficientnetv2_b1',\n        backbone_name='tf_efficientnetv2_b1',\n        backbone_args=dict(drop_path_rate=0.2),\n        num_classes=3,\n        url='', )\n    \n    config = get_efficientdet_config(\"tf_efficientnetv2_b1\")\n    config.update({'num_classes': 3})\n    config.update({'image_size': (img_size, img_size)})\n    \n    print(config)\n\n    net = EfficientDet(config, pretrained_backbone=True)\n    net.class_net = HeadNet(\n        config,\n        num_outputs=config.num_classes,\n    )\n    return DetBenchTrain(net, config)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model_1 = tf_efficientnetv2_b1()\nmodel_1.to(device)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"total_params = 0\nfor param in model_1.parameters():\n    total_params += param.numel()\n\nprint(f\"Total parameters: {total_params}\")","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model_1.config","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"500*0.0004","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"optimizer = AdamW(model_1.parameters(),lr = 0.0001)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for images, targets in tqdm(Train_loader, desc=f\"Epoch {epoch+1}\"):\n    images = images\n    t = {k:[vv.to(device) for vv in v] if (isinstance(v,list) or isinstance(v,tuple)) else v.to(device) for k,v in targets.items()}\n    for i in range(len(images)):\n        images = images[i].numpy().transpose(1,2,0)\n        print(t)\n        \n        print(images.shape)\n        plt.imshow(images)\n        plt.show()\n        break\n","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"epochs_n = 10\nbest_loss = 0\nfor epoch in range(epochs_n):\n    count = 0\n    model.train()\n    \n    TrainLoss = 0\n    for images, targets in tqdm(Train_loader, desc=f\"Epoch {epoch+1}\"):\n        images = [img.float().to(device)]\n        t = {k:[vv.to(device) for vv in v] if (isinstance(v,list) or isinstance(v,tuple)) else v.to(device) for k,v in targets.items()}\n\n        optimizer.zero_grad()\n        loss_d = model_1(images, t)\n        loss=loss_d[\"loss\"]\n        loss.backward()\n        optimizer.step()    \n        TrainLoss += loss.item()\n    \n    print( 'Train_loss :',TrainLoss/len(Train_loader))\n\n    \n    \n    model.eval()\n\n    avg_Validation_Loss = 0 \n    avg_box_loss = 0\n    avg_class_loss = 0\n    with torch.no_grad():\n        for index, (images, targets) in enumerate(Evaluation_loader):\n            image_eval = [img.float().to(device)] \n            t = {k:[vv.to(device) for vv in v] if (isinstance(v,list) or isinstance(v,tuple)) else v.to(device) for k,v in targets.items()}\n            prediction = model_1(image_eval,t)\n            \n            loss_vv = prediction['loss']\n            box_loss = prediction['box_loss']\n            class_loss = prediction['class_loss']\n            avg_Validation_Loss += loss_vv.item()\n            avg_box_loss += box_loss.item()\n            avg_class_loss += class_loss.item()\n            torch.cuda.empty_cache()\n        \n\n        print(\" Val Loss: \",avg_Validation_Loss/len(Evaluation_loader),\n              'Val bbox: ',avg_box_loss/len(Evaluation_loader),\n              'class_loss: ',avg_class_loss/len(Evaluation_loader))\n        print(prediction)\n\n\n","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"counts = 0\nfor images, targets in tqdm(Train_loader, desc=f\"Epoch {epoch+1}\"):\n    \n    counts += len(images)\nprint(counts)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(prediction)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(get_efficientdet_config)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"torch.save(model_1.state_dict(), \"model_5.pth\")","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"all_labels = []\nfor _, target in dataset:\n    all_labels.extend(target['classes'].numpy())\nall_labels = np.array(all_labels)\n \nunique, counts = np.unique(all_labels, return_counts=True)\nlabel_counts = dict(zip(unique, counts))\nclass_dist = {'cheerios':label_counts[0], 'soup':label_counts[1], 'candles':label_counts[2]}\nprint(class_dist)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"inf_model_path = \"/kaggle/working/model_5.pth\"\ninf_config = get_efficientdet_config('tf_efficientnetv2_b1')\ninf_config.num_classes = 3\ninf_config.image_size = (512,512)\ninf_config.legacy_focal = False\ninf_config.max_det_per_image = 10\n\npredict_net = EfficientDet(inf_config, pretrained_backbone = True)\npredict_net.class_net = HeadNet(inf_config, num_outputs = inf_config.num_classes)\ninference_model = DetBenchPredict(predict_net)\ninference_model.load_state_dict(torch.load(inf_model_path, map_location= device))\ninference_model.to(device)\ninference_model.eval()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"iw_scale= iw/512\nih_scale = ih/512\nprint(iw_scale,ih_scale)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"inf_detections[0][: , [0,2]] *= iw_scale\ninf_detections[0][: , [1,3]] *= ih_scale\n#inf_detections","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# inf_root = '/kaggle/input/grocery-items-multi-class-object-detection/train/train/'\n# img_pat = list(sorted(os.listdir(os.path.join(inf_root,'images'))))\n# for idx,i in enumerate(img_pat):\n    \n#     print(inf_image_path)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"img_res = 512\ninf_root = '/kaggle/input/grocery-items-multi-class-object-detection/train/train/'\nimg_pat = list(sorted(os.listdir(os.path.join(inf_root,'images'))))\nfor idx,i in enumerate(img_pat):\n    inf_image_path = os.path.join(inf_root,'images',img_pat[idx])\n\n    #inf_image_path = \"/kaggle/input/grocery-items-multi-class-object-detection/train/train/images/000000121.png\"\n    inf_img = cv2.imread(inf_image_path)\n    #inf_img = cv2.cvtColor(inf_img, cv2.COLOR_BGR2RGB)\n    ih, iw, channels = inf_img.shape\n    inf_img = cv2.resize(inf_img, (img_res,img_res))\n    inf_img_tensor = torch.tensor(inf_img).permute(2, 0, 1).float().unsqueeze(0)\n    inf_img_tensor = inf_img_tensor.to(device)\n    inf_target = {\"img_size\": torch.tensor([[img_res,img_res]], device = device),\n                \"img_scale\": torch.tensor([1.0], device = device)}\n    with torch.no_grad():\n        inf_detections = inference_model(inf_img_tensor,inf_target)\n    inf_img_copy = inf_img.copy()\n    height, width, channels = inf_img_copy.shape\n    threshold = 0.1\n    \n    bboxes = inf_detections.detach().cpu().numpy()[0][: , :4]\n    confidence_scores =  inf_detections.detach().cpu().numpy()[0][: , 4]\n    labels = inf_detections.detach().cpu().numpy()[0][: , 5].astype(int)\n    \n    for bbox, confidence_score, label in zip(bboxes, confidence_scores, labels):\n        if confidence_score < threshold:\n            continue\n        x1, y1, x2, y2 = map(int,bbox)\n        cv2.rectangle(inf_img_copy, (x1, y1), (x2, y2), (0,255,0), 4)\n    plt.imshow(inf_img_copy)\n    \n    plt.show()\n    if idx == 15:\n        break","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\n\n#inf_detections\n","metadata":{"trusted":true,"scrolled":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true,"scrolled":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}