{"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":"import os\nimport glob\nimport gc\nimport shutil\nimport time\nimport matplotlib.pyplot as plt\nimport cv2\nimport PIL\nPIL.Image.MAX_IMAGE_PIXELS = 10000000000\nfrom PIL import Image\nimport numpy as np\nimport timm\nimport torch\nimport tqdm","metadata":{"execution":{"iopub.status.busy":"2023-10-07T05:33:02.755634Z","iopub.execute_input":"2023-10-07T05:33:02.756342Z","iopub.status.idle":"2023-10-07T05:33:07.655933Z","shell.execute_reply.started":"2023-10-07T05:33:02.756311Z","shell.execute_reply":"2023-10-07T05:33:07.655012Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_patch_coors(h_img, w_img, size, overlap):\n    cut_ponit = []\n    step = size - overlap\n    w = size\n    for x in range(0, w_img, step):\n        h = size\n        if x + w > w_img:\n            x = w_img - w\n        for y in range(0, h_img, step):\n            if y + h > h_img:\n                y = h_img - h\n            cut_ponit.append([x, y, w, h])\n    return cut_ponit\n\n\ndef image2patch(img_path,patch_save_dir):\n    size, overlap = 224, 0\n    name=img_path.split('/')[-1].split('.')[0]\n    img_save_path=os.path.join(patch_save_dir,name)\n    if not os.path.exists(img_save_path):\n        os.makedirs(img_save_path)\n\n    img = np.array(Image.open(img_path))\n\n    h_img, w_img, _ = img.shape\n    coors = get_patch_coors(h_img, w_img, size, overlap)\n    for coor in coors:\n        x, y, w, h = coor\n        patch = img[y:y + h, x:x + w]\n        if len(patch[patch != 0]) > size*size*0.5:\n            cv2.imwrite(os.path.join(img_save_path, name + '-' + str(x) + '-' + str(y) + '.png'), patch)\n    \n    return img_save_path\n\ndef extract_feas(patch_save_dir,f_save_dir):\n    print(patch_save_dir)\n    bag_name=patch_save_dir.split('/')[-1]\n    all_instances=os.listdir(patch_save_dir)\n    embed=[]\n    for instance in tqdm.tqdm(all_instances):\n        instance_path=os.path.join(patch_save_dir,instance)\n        patch=cv2.imread(instance_path)\n        cv2.cvtColor(patch,cv2.COLOR_BGR2RGB)\n        patch=torch.tensor(np.transpose(patch,(2,0,1)))\n        patch=patch.cuda().float().unsqueeze(0)\n\n        f=model(patch)\n        f_np=f.detach().cpu().numpy().squeeze(0)\n        embed.append(f_np)\n        gc.collect()\n    \n    feas=np.array(embed)\n    print(feas.shape)\n\n    np.save(os.path.join(f_save_dir,bag_name+'.npy'),feas)\n\ndef process(img_path,patch_save_dir,f_save_dir):\n    img_save_path = image2patch(img_path,patch_save_dir)\n    extract_feas(img_save_path,f_save_dir)\n    shutil.rmtree(img_save_path)\n\nif __name__ == '__main__':\n    patch_save_dir=os.path.join('/kaggle/working/','patch_dir')\n    if not os.path.exists(patch_save_dir):\n        os.makedirs(patch_save_dir)\n    f_save_dir=os.path.join('/kaggle/working/','fea_dir')\n    if not os.path.exists(f_save_dir):\n        os.makedirs(f_save_dir)\n    img_root_dir='/kaggle/input/UBC-OCEAN/train_images'\n    \n    all_img=glob.glob(img_root_dir+'/*')\n    print(len(all_img))\n    \n    \n    # model\n    model = timm.create_model('resnet50', pretrained=True)\n    model.fc=torch.nn.Identity()\n\n    model.cuda()\n    model.eval()\n\n    for img_path in all_img:\n        print(img_path)\n        process(img_path,patch_save_dir,f_save_dir)\n        break\n","metadata":{"execution":{"iopub.status.busy":"2023-10-07T05:46:06.257295Z","iopub.execute_input":"2023-10-07T05:46:06.257619Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}