codeShare commited on
Commit
fe88d09
Β·
verified Β·
1 Parent(s): d7ed1b0

Upload bria_rembg_background_removal.ipynb

Browse files
bria_rembg_background_removal.ipynb ADDED
@@ -0,0 +1 @@
 
 
1
+ {"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"provenance":[],"gpuType":"T4","mount_file_id":"11YEDI8zKDFOc43YcDo0oO4tnBOp7ZRkv","authorship_tag":"ABX9TyNk1M7PwU0OTwiJON2Z/2Wa"},"kernelspec":{"name":"python3","display_name":"Python 3"},"language_info":{"name":"python"},"accelerator":"GPU","widgets":{"application/vnd.jupyter.widget-state+json":{"ddcc887317f9469bba681e9528b78b85":{"model_module":"@jupyter-widgets/controls","model_name":"HBoxModel","model_module_version":"1.5.0","state":{"_dom_classes":[],"_model_module":"@jupyter-widgets/controls","_model_module_version":"1.5.0","_model_name":"HBoxModel","_view_count":null,"_view_module":"@jupyter-widgets/controls","_view_module_version":"1.5.0","_view_name":"HBoxView","box_style":"","children":["IPY_MODEL_5158e967f786430993723a0030c6c299","IPY_MODEL_1d75a97d4cd84c758c55d992e9946984","IPY_MODEL_90a0f7f5074a4f03ad41a83ecb8bd6c1"],"layout":"IPY_MODEL_1c8b798976f348ef92b1d507a989511d"}},"5158e967f786430993723a0030c6c299":{"model_module":"@jupyter-widgets/controls","model_name":"HTMLModel","model_module_version":"1.5.0","state":{"_dom_classes":[],"_model_module":"@jupyter-widgets/controls","_model_module_version":"1.5.0","_model_name":"HTMLModel","_view_count":null,"_view_module":"@jupyter-widgets/controls","_view_module_version":"1.5.0","_view_name":"HTMLView","description":"","description_tooltip":null,"layout":"IPY_MODEL_710ea9f738924f88960fd5bef9abb4aa","placeholder":"​","style":"IPY_MODEL_1d159acba667437e92f4b839f647eabf","value":"model.safetensors: 100%"}},"1d75a97d4cd84c758c55d992e9946984":{"model_module":"@jupyter-widgets/controls","model_name":"FloatProgressModel","model_module_version":"1.5.0","state":{"_dom_classes":[],"_model_module":"@jupyter-widgets/controls","_model_module_version":"1.5.0","_model_name":"FloatProgressModel","_view_count":null,"_view_module":"@jupyter-widgets/controls","_view_module_version":"1.5.0","_view_name":"ProgressView","bar_style":"success","description":"","description_tooltip":null,"layout":"IPY_MODEL_14db990941e94cac9f371603e823f424","max":884878856,"min":0,"orientation":"horizontal","style":"IPY_MODEL_368df45f19d04ef385c003ad9638e84c","value":884878856}},"90a0f7f5074a4f03ad41a83ecb8bd6c1":{"model_module":"@jupyter-widgets/controls","model_name":"HTMLModel","model_module_version":"1.5.0","state":{"_dom_classes":[],"_model_module":"@jupyter-widgets/controls","_model_module_version":"1.5.0","_model_name":"HTMLModel","_view_count":null,"_view_module":"@jupyter-widgets/controls","_view_module_version":"1.5.0","_view_name":"HTMLView","description":"","description_tooltip":null,"layout":"IPY_MODEL_294f08742b964edfaab06b480f1bb822","placeholder":"​","style":"IPY_MODEL_4363743a94c44338ac09b02694d320f2","value":" 885M/885M [00:06&lt;00:00, 123MB/s]"}},"1c8b798976f348ef92b1d507a989511d":{"model_module":"@jupyter-widgets/base","model_name":"LayoutModel","model_module_version":"1.2.0","state":{"_model_module":"@jupyter-widgets/base","_model_module_version":"1.2.0","_model_name":"LayoutModel","_view_count":null,"_view_module":"@jupyter-widgets/base","_view_module_version":"1.2.0","_view_name":"LayoutView","align_content":null,"align_items":null,"align_self":null,"border":null,"bottom":null,"display":null,"flex":null,"flex_flow":null,"grid_area":null,"grid_auto_columns":null,"grid_auto_flow":null,"grid_auto_rows":null,"grid_column":null,"grid_gap":null,"grid_row":null,"grid_template_areas":null,"grid_template_columns":null,"grid_template_rows":null,"height":null,"justify_content":null,"justify_items":null,"left":null,"margin":null,"max_height":null,"max_width":null,"min_height":null,"min_width":null,"object_fit":null,"object_position":null,"order":null,"overflow":null,"overflow_x":null,"overflow_y":null,"padding":null,"right":null,"top":null,"visibility":null,"width":null}},"710ea9f738924f88960fd5bef9abb4aa":{"model_module":"@jupyter-widgets/base","model_name":"LayoutModel","model_module_version":"1.2.0","state":{"_model_module":"@jupyter-widgets/base","_model_module_version":"1.2.0","_model_name":"LayoutModel","_view_count":null,"_view_module":"@jupyter-widgets/base","_view_module_version":"1.2.0","_view_name":"LayoutView","align_content":null,"align_items":null,"align_self":null,"border":null,"bottom":null,"display":null,"flex":null,"flex_flow":null,"grid_area":null,"grid_auto_columns":null,"grid_auto_flow":null,"grid_auto_rows":null,"grid_column":null,"grid_gap":null,"grid_row":null,"grid_template_areas":null,"grid_template_columns":null,"grid_template_rows":null,"height":null,"justify_content":null,"justify_items":null,"left":null,"margin":null,"max_height":null,"max_width":null,"min_height":null,"min_width":null,"object_fit":null,"object_position":null,"order":null,"overflow":null,"overflow_x":null,"overflow_y":null,"padding":null,"right":null,"top":null,"visibility":null,"width":null}},"1d159acba667437e92f4b839f647eabf":{"model_module":"@jupyter-widgets/controls","model_name":"DescriptionStyleModel","model_module_version":"1.5.0","state":{"_model_module":"@jupyter-widgets/controls","_model_module_version":"1.5.0","_model_name":"DescriptionStyleModel","_view_count":null,"_view_module":"@jupyter-widgets/base","_view_module_version":"1.2.0","_view_name":"StyleView","description_width":""}},"14db990941e94cac9f371603e823f424":{"model_module":"@jupyter-widgets/base","model_name":"LayoutModel","model_module_version":"1.2.0","state":{"_model_module":"@jupyter-widgets/base","_model_module_version":"1.2.0","_model_name":"LayoutModel","_view_count":null,"_view_module":"@jupyter-widgets/base","_view_module_version":"1.2.0","_view_name":"LayoutView","align_content":null,"align_items":null,"align_self":null,"border":null,"bottom":null,"display":null,"flex":null,"flex_flow":null,"grid_area":null,"grid_auto_columns":null,"grid_auto_flow":null,"grid_auto_rows":null,"grid_column":null,"grid_gap":null,"grid_row":null,"grid_template_areas":null,"grid_template_columns":null,"grid_template_rows":null,"height":null,"justify_content":null,"justify_items":null,"left":null,"margin":null,"max_height":null,"max_width":null,"min_height":null,"min_width":null,"object_fit":null,"object_position":null,"order":null,"overflow":null,"overflow_x":null,"overflow_y":null,"padding":null,"right":null,"top":null,"visibility":null,"width":null}},"368df45f19d04ef385c003ad9638e84c":{"model_module":"@jupyter-widgets/controls","model_name":"ProgressStyleModel","model_module_version":"1.5.0","state":{"_model_module":"@jupyter-widgets/controls","_model_module_version":"1.5.0","_model_name":"ProgressStyleModel","_view_count":null,"_view_module":"@jupyter-widgets/base","_view_module_version":"1.2.0","_view_name":"StyleView","bar_color":null,"description_width":""}},"294f08742b964edfaab06b480f1bb822":{"model_module":"@jupyter-widgets/base","model_name":"LayoutModel","model_module_version":"1.2.0","state":{"_model_module":"@jupyter-widgets/base","_model_module_version":"1.2.0","_model_name":"LayoutModel","_view_count":null,"_view_module":"@jupyter-widgets/base","_view_module_version":"1.2.0","_view_name":"LayoutView","align_content":null,"align_items":null,"align_self":null,"border":null,"bottom":null,"display":null,"flex":null,"flex_flow":null,"grid_area":null,"grid_auto_columns":null,"grid_auto_flow":null,"grid_auto_rows":null,"grid_column":null,"grid_gap":null,"grid_row":null,"grid_template_areas":null,"grid_template_columns":null,"grid_template_rows":null,"height":null,"justify_content":null,"justify_items":null,"left":null,"margin":null,"max_height":null,"max_width":null,"min_height":null,"min_width":null,"object_fit":null,"object_position":null,"order":null,"overflow":null,"overflow_x":null,"overflow_y":null,"padding":null,"right":null,"top":null,"visibility":null,"width":null}},"4363743a94c44338ac09b02694d320f2":{"model_module":"@jupyter-widgets/controls","model_name":"DescriptionStyleModel","model_module_version":"1.5.0","state":{"_model_module":"@jupyter-widgets/controls","_model_module_version":"1.5.0","_model_name":"DescriptionStyleModel","_view_count":null,"_view_module":"@jupyter-widgets/base","_view_module_version":"1.2.0","_view_name":"StyleView","description_width":""}},"57b528ec25ae423fa8628c3882a8a725":{"model_module":"@jupyter-widgets/controls","model_name":"HBoxModel","model_module_version":"1.5.0","state":{"_dom_classes":[],"_model_module":"@jupyter-widgets/controls","_model_module_version":"1.5.0","_model_name":"HBoxModel","_view_count":null,"_view_module":"@jupyter-widgets/controls","_view_module_version":"1.5.0","_view_name":"HBoxView","box_style":"","children":["IPY_MODEL_fc7cca6d65654e99a748566ca6602850","IPY_MODEL_d4eb9dd6e42b46c285d06d9734cba4dc","IPY_MODEL_42c60b8bf5a249d29413ed2a0b8ecb62"],"layout":"IPY_MODEL_959595c06c0d4c989b055882d410dcbf"}},"fc7cca6d65654e99a748566ca6602850":{"model_module":"@jupyter-widgets/controls","model_name":"HTMLModel","model_module_version":"1.5.0","state":{"_dom_classes":[],"_model_module":"@jupyter-widgets/controls","_model_module_version":"1.5.0","_model_name":"HTMLModel","_view_count":null,"_view_module":"@jupyter-widgets/controls","_view_module_version":"1.5.0","_view_name":"HTMLView","description":"","description_tooltip":null,"layout":"IPY_MODEL_c2af65beeba04fa7b114a668bf576ecf","placeholder":"​","style":"IPY_MODEL_93b04ade07e84ff7b413f75cf2ec64e0","value":"Loading weights: 100%"}},"d4eb9dd6e42b46c285d06d9734cba4dc":{"model_module":"@jupyter-widgets/controls","model_name":"FloatProgressModel","model_module_version":"1.5.0","state":{"_dom_classes":[],"_model_module":"@jupyter-widgets/controls","_model_module_version":"1.5.0","_model_name":"FloatProgressModel","_view_count":null,"_view_module":"@jupyter-widgets/controls","_view_module_version":"1.5.0","_view_name":"ProgressView","bar_style":"success","description":"","description_tooltip":null,"layout":"IPY_MODEL_66b4ce276258456d840ea04bc29e5ee0","max":754,"min":0,"orientation":"horizontal","style":"IPY_MODEL_931e187d9061408fb18e96d2a4f55f6a","value":754}},"42c60b8bf5a249d29413ed2a0b8ecb62":{"model_module":"@jupyter-widgets/controls","model_name":"HTMLModel","model_module_version":"1.5.0","state":{"_dom_classes":[],"_model_module":"@jupyter-widgets/controls","_model_module_version":"1.5.0","_model_name":"HTMLModel","_view_count":null,"_view_module":"@jupyter-widgets/controls","_view_module_version":"1.5.0","_view_name":"HTMLView","description":"","description_tooltip":null,"layout":"IPY_MODEL_a92e6e225b504dee9da61026ea6e7fb5","placeholder":"​","style":"IPY_MODEL_5b7215b71f454ba1a8ffe4ddbef1bff3","value":" 754/754 [00:01&lt;00:00, 651.75it/s, Materializing param=squeeze_module.0.dec_att.global_avg_pool.2.weight]"}},"959595c06c0d4c989b055882d410dcbf":{"model_module":"@jupyter-widgets/base","model_name":"LayoutModel","model_module_version":"1.2.0","state":{"_model_module":"@jupyter-widgets/base","_model_module_version":"1.2.0","_model_name":"LayoutModel","_view_count":null,"_view_module":"@jupyter-widgets/base","_view_module_version":"1.2.0","_view_name":"LayoutView","align_content":null,"align_items":null,"align_self":null,"border":null,"bottom":null,"display":null,"flex":null,"flex_flow":null,"grid_area":null,"grid_auto_columns":null,"grid_auto_flow":null,"grid_auto_rows":null,"grid_column":null,"grid_gap":null,"grid_row":null,"grid_template_areas":null,"grid_template_columns":null,"grid_template_rows":null,"height":null,"justify_content":null,"justify_items":null,"left":null,"margin":null,"max_height":null,"max_width":null,"min_height":null,"min_width":null,"object_fit":null,"object_position":null,"order":null,"overflow":null,"overflow_x":null,"overflow_y":null,"padding":null,"right":null,"top":null,"visibility":null,"width":null}},"c2af65beeba04fa7b114a668bf576ecf":{"model_module":"@jupyter-widgets/base","model_name":"LayoutModel","model_module_version":"1.2.0","state":{"_model_module":"@jupyter-widgets/base","_model_module_version":"1.2.0","_model_name":"LayoutModel","_view_count":null,"_view_module":"@jupyter-widgets/base","_view_module_version":"1.2.0","_view_name":"LayoutView","align_content":null,"align_items":null,"align_self":null,"border":null,"bottom":null,"display":null,"flex":null,"flex_flow":null,"grid_area":null,"grid_auto_columns":null,"grid_auto_flow":null,"grid_auto_rows":null,"grid_column":null,"grid_gap":null,"grid_row":null,"grid_template_areas":null,"grid_template_columns":null,"grid_template_rows":null,"height":null,"justify_content":null,"justify_items":null,"left":null,"margin":null,"max_height":null,"max_width":null,"min_height":null,"min_width":null,"object_fit":null,"object_position":null,"order":null,"overflow":null,"overflow_x":null,"overflow_y":null,"padding":null,"right":null,"top":null,"visibility":null,"width":null}},"93b04ade07e84ff7b413f75cf2ec64e0":{"model_module":"@jupyter-widgets/controls","model_name":"DescriptionStyleModel","model_module_version":"1.5.0","state":{"_model_module":"@jupyter-widgets/controls","_model_module_version":"1.5.0","_model_name":"DescriptionStyleModel","_view_count":null,"_view_module":"@jupyter-widgets/base","_view_module_version":"1.2.0","_view_name":"StyleView","description_width":""}},"66b4ce276258456d840ea04bc29e5ee0":{"model_module":"@jupyter-widgets/base","model_name":"LayoutModel","model_module_version":"1.2.0","state":{"_model_module":"@jupyter-widgets/base","_model_module_version":"1.2.0","_model_name":"LayoutModel","_view_count":null,"_view_module":"@jupyter-widgets/base","_view_module_version":"1.2.0","_view_name":"LayoutView","align_content":null,"align_items":null,"align_self":null,"border":null,"bottom":null,"display":null,"flex":null,"flex_flow":null,"grid_area":null,"grid_auto_columns":null,"grid_auto_flow":null,"grid_auto_rows":null,"grid_column":null,"grid_gap":null,"grid_row":null,"grid_template_areas":null,"grid_template_columns":null,"grid_template_rows":null,"height":null,"justify_content":null,"justify_items":null,"left":null,"margin":null,"max_height":null,"max_width":null,"min_height":null,"min_width":null,"object_fit":null,"object_position":null,"order":null,"overflow":null,"overflow_x":null,"overflow_y":null,"padding":null,"right":null,"top":null,"visibility":null,"width":null}},"931e187d9061408fb18e96d2a4f55f6a":{"model_module":"@jupyter-widgets/controls","model_name":"ProgressStyleModel","model_module_version":"1.5.0","state":{"_model_module":"@jupyter-widgets/controls","_model_module_version":"1.5.0","_model_name":"ProgressStyleModel","_view_count":null,"_view_module":"@jupyter-widgets/base","_view_module_version":"1.2.0","_view_name":"StyleView","bar_color":null,"description_width":""}},"a92e6e225b504dee9da61026ea6e7fb5":{"model_module":"@jupyter-widgets/base","model_name":"LayoutModel","model_module_version":"1.2.0","state":{"_model_module":"@jupyter-widgets/base","_model_module_version":"1.2.0","_model_name":"LayoutModel","_view_count":null,"_view_module":"@jupyter-widgets/base","_view_module_version":"1.2.0","_view_name":"LayoutView","align_content":null,"align_items":null,"align_self":null,"border":null,"bottom":null,"display":null,"flex":null,"flex_flow":null,"grid_area":null,"grid_auto_columns":null,"grid_auto_flow":null,"grid_auto_rows":null,"grid_column":null,"grid_gap":null,"grid_row":null,"grid_template_areas":null,"grid_template_columns":null,"grid_template_rows":null,"height":null,"justify_content":null,"justify_items":null,"left":null,"margin":null,"max_height":null,"max_width":null,"min_height":null,"min_width":null,"object_fit":null,"object_position":null,"order":null,"overflow":null,"overflow_x":null,"overflow_y":null,"padding":null,"right":null,"top":null,"visibility":null,"width":null}},"5b7215b71f454ba1a8ffe4ddbef1bff3":{"model_module":"@jupyter-widgets/controls","model_name":"DescriptionStyleModel","model_module_version":"1.5.0","state":{"_model_module":"@jupyter-widgets/controls","_model_module_version":"1.5.0","_model_name":"DescriptionStyleModel","_view_count":null,"_view_module":"@jupyter-widgets/base","_view_module_version":"1.2.0","_view_name":"StyleView","description_width":""}}}}},"cells":[{"cell_type":"code","source":["!pip install -q \\\n"," transformers \\\n"," accelerate \\\n"," pillow \\\n"," tqdm \\\n"," safetensors \\\n"," kornia"],"metadata":{"id":"Y3HzjfqPsP45","executionInfo":{"status":"ok","timestamp":1780091039585,"user_tz":-120,"elapsed":15714,"user":{"displayName":"No Name","userId":"10578412414437288386"}}},"execution_count":1,"outputs":[]},{"cell_type":"markdown","source":["Note: Bria AI remove background tool is a gated access model , meaning you will need a huggingface account , and you\n","will need to fetch your huggingface api token , the agree to open source terms of usage on https://huggingface.co/briaai/RMBG-2.0 while logged into your huggingface account"],"metadata":{"id":"TT87LNT9uAWR"}},{"cell_type":"code","source":["# =============================================================================\n","# BRIA RMBG 2.0 ZIP Background Removal\n","# =============================================================================\n","\n","import zipfile\n","import shutil\n","from pathlib import Path\n","\n","import torch\n","import numpy as np\n","\n","from PIL import Image\n","from tqdm import tqdm\n","\n","from transformers import AutoModelForImageSegmentation\n","\n","# =============================================================================\n","# SETTINGS\n","# =============================================================================\n","\n","zip_filepath = \"/content/drive/MyDrive/torembg.zip\" #@param {type:\"string\"}\n","\n","# =============================================================================\n","# MOUNT DRIVE\n","# =============================================================================\n","\n","from google.colab import drive\n","\n","drive.mount(\"/content/drive\")\n","\n","# =============================================================================\n","# VALIDATION\n","# =============================================================================\n","\n","zip_filepath = Path(zip_filepath)\n","\n","if not zip_filepath.exists():\n"," raise FileNotFoundError(zip_filepath)\n","\n","# =============================================================================\n","# WORK DIRS\n","# =============================================================================\n","\n","base_dir = Path(\"/content/bria_rmbg\")\n","\n","if base_dir.exists():\n"," shutil.rmtree(base_dir)\n","\n","input_dir = base_dir / \"input\"\n","output_dir = base_dir / \"output\"\n","\n","input_dir.mkdir(parents=True, exist_ok=True)\n","output_dir.mkdir(parents=True, exist_ok=True)\n","\n","# =============================================================================\n","# EXTRACT ZIP\n","# =============================================================================\n","\n","print(\"Extracting ZIP...\")\n","\n","with zipfile.ZipFile(zip_filepath, \"r\") as z:\n"," z.extractall(input_dir)\n","\n","# =============================================================================\n","# FIND IMAGES\n","# =============================================================================\n","\n","IMAGE_EXTENSIONS = {\n"," \".jpg\",\n"," \".jpeg\",\n"," \".png\",\n"," \".webp\",\n"," \".bmp\",\n"," \".tif\",\n"," \".tiff\"\n","}\n","\n","image_files = [\n"," p\n"," for p in input_dir.rglob(\"*\")\n"," if p.is_file() and p.suffix.lower() in IMAGE_EXTENSIONS\n","]\n","\n","print(f\"Found {len(image_files)} images\")\n","\n","# =============================================================================\n","# DEVICE\n","# =============================================================================\n","\n","device = \"cuda\" if torch.cuda.is_available() else \"cpu\"\n","\n","print(\"Using:\", device)\n","\n","# =============================================================================\n","# LOAD BRIA RMBG 2.0\n","# =============================================================================\n","\n","print(\"Loading model...\")\n","\n","model = AutoModelForImageSegmentation.from_pretrained(\n"," \"briaai/RMBG-2.0\",\n"," trust_remote_code=True\n",")\n","\n","model.to(device)\n","model.eval()\n","\n","print(\"Model loaded\")\n","\n","# =============================================================================\n","# PREPROCESS\n","# =============================================================================\n","\n","MODEL_SIZE = 1024\n","\n","def preprocess_image(image):\n","\n"," image = image.convert(\"RGB\")\n","\n"," original_size = image.size\n","\n"," image = image.resize(\n"," (MODEL_SIZE, MODEL_SIZE),\n"," Image.Resampling.LANCZOS\n"," )\n","\n"," image_np = np.array(image).astype(np.float32)\n","\n"," image_np = image_np / 255.0\n","\n"," image_np = image_np.transpose(2, 0, 1)\n","\n"," tensor = torch.from_numpy(image_np)\n","\n"," tensor = tensor.unsqueeze(0)\n","\n"," return tensor, original_size\n","\n","\n","def postprocess_mask(mask, original_size):\n","\n"," mask = mask.squeeze()\n","\n"," mask = mask.cpu().numpy()\n","\n"," mask = (mask * 255).astype(np.uint8)\n","\n"," mask_img = Image.fromarray(mask)\n","\n"," mask_img = mask_img.resize(\n"," original_size,\n"," Image.Resampling.LANCZOS\n"," )\n","\n"," return mask_img\n","\n","# =============================================================================\n","# PROCESS IMAGES\n","# =============================================================================\n","\n","print(\"Removing backgrounds...\")\n","\n","for image_path in tqdm(image_files):\n","\n"," try:\n","\n"," image = Image.open(image_path).convert(\"RGB\")\n","\n"," tensor, original_size = preprocess_image(image)\n","\n"," tensor = tensor.to(device)\n","\n"," with torch.no_grad():\n","\n"," pred = model(tensor)\n","\n"," if isinstance(pred, (tuple, list)):\n"," pred = pred[-1]\n","\n"," pred = torch.sigmoid(pred)\n","\n"," mask = postprocess_mask(\n"," pred[0][0],\n"," original_size\n"," )\n","\n"," rgba = image.convert(\"RGBA\")\n","\n"," rgba.putalpha(mask)\n","\n"," relative_path = image_path.relative_to(input_dir)\n","\n"," output_path = (\n"," output_dir /\n"," relative_path.with_suffix(\".png\")\n"," )\n","\n"," output_path.parent.mkdir(\n"," parents=True,\n"," exist_ok=True\n"," )\n","\n"," rgba.save(\n"," output_path,\n"," \"PNG\",\n"," optimize=True\n"," )\n","\n"," except Exception as e:\n","\n"," print(\n"," f\"Failed: {image_path.name}\"\n"," )\n"," print(e)\n","\n","# =============================================================================\n","# CREATE ZIP\n","# =============================================================================\n","\n","output_zip_name = (\n"," zip_filepath.stem +\n"," \"_transparent_images\"\n",")\n","\n","temp_zip_base = (\n"," Path(\"/content\") /\n"," output_zip_name\n",")\n","\n","print(\"Creating ZIP...\")\n","\n","shutil.make_archive(\n"," str(temp_zip_base),\n"," \"zip\",\n"," output_dir\n",")\n","\n","# =============================================================================\n","# SAVE TO DRIVE\n","# =============================================================================\n","\n","final_zip = (\n"," Path(\"/content/drive/MyDrive\") /\n"," f\"{output_zip_name}.zip\"\n",")\n","\n","if final_zip.exists():\n"," final_zip.unlink()\n","\n","shutil.move(\n"," str(temp_zip_base.with_suffix(\".zip\")),\n"," str(final_zip)\n",")\n","\n","# =============================================================================\n","# DONE\n","# =============================================================================\n","\n","print()\n","print(\"========================================\")\n","print(\"DONE\")\n","print(\"========================================\")\n","print(\"Transparent images:\")\n","print(output_dir)\n","print()\n","print(\"ZIP saved to:\")\n","print(final_zip)\n","print(\"========================================\")"],"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":1000,"referenced_widgets":["ddcc887317f9469bba681e9528b78b85","5158e967f786430993723a0030c6c299","1d75a97d4cd84c758c55d992e9946984","90a0f7f5074a4f03ad41a83ecb8bd6c1","1c8b798976f348ef92b1d507a989511d","710ea9f738924f88960fd5bef9abb4aa","1d159acba667437e92f4b839f647eabf","14db990941e94cac9f371603e823f424","368df45f19d04ef385c003ad9638e84c","294f08742b964edfaab06b480f1bb822","4363743a94c44338ac09b02694d320f2","57b528ec25ae423fa8628c3882a8a725","fc7cca6d65654e99a748566ca6602850","d4eb9dd6e42b46c285d06d9734cba4dc","42c60b8bf5a249d29413ed2a0b8ecb62","959595c06c0d4c989b055882d410dcbf","c2af65beeba04fa7b114a668bf576ecf","93b04ade07e84ff7b413f75cf2ec64e0","66b4ce276258456d840ea04bc29e5ee0","931e187d9061408fb18e96d2a4f55f6a","a92e6e225b504dee9da61026ea6e7fb5","5b7215b71f454ba1a8ffe4ddbef1bff3"]},"id":"-_YhFzolsQM2","outputId":"4aaa1bf6-2359-46c8-dbc2-106a90c8cb59"},"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["Collecting kornia\n"," Downloading kornia-0.8.3-py3-none-any.whl.metadata (22 kB)\n","Collecting kornia-rs>=0.1.9 (from kornia)\n"," Downloading kornia_rs-0.1.14-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (15 kB)\n","Requirement already satisfied: packaging in /usr/local/lib/python3.12/dist-packages (from kornia) (26.2)\n","Requirement already satisfied: torch>=2.0.0 in /usr/local/lib/python3.12/dist-packages (from kornia) (2.11.0+cu128)\n","Requirement already satisfied: filelock in /usr/local/lib/python3.12/dist-packages (from torch>=2.0.0->kornia) (3.29.0)\n","Requirement already satisfied: typing-extensions>=4.10.0 in /usr/local/lib/python3.12/dist-packages (from torch>=2.0.0->kornia) (4.15.0)\n","Requirement already satisfied: setuptools<82 in /usr/local/lib/python3.12/dist-packages (from torch>=2.0.0->kornia) (75.2.0)\n","Requirement already satisfied: sympy>=1.13.3 in /usr/local/lib/python3.12/dist-packages (from torch>=2.0.0->kornia) (1.14.0)\n","Requirement already satisfied: networkx>=2.5.1 in /usr/local/lib/python3.12/dist-packages (from torch>=2.0.0->kornia) (3.6.1)\n","Requirement already satisfied: jinja2 in /usr/local/lib/python3.12/dist-packages (from torch>=2.0.0->kornia) (3.1.6)\n","Requirement already satisfied: fsspec>=0.8.5 in /usr/local/lib/python3.12/dist-packages (from torch>=2.0.0->kornia) (2025.3.0)\n","Requirement already satisfied: cuda-toolkit==12.8.1 in /usr/local/lib/python3.12/dist-packages (from cuda-toolkit[cublas,cudart,cufft,cufile,cupti,curand,cusolver,cusparse,nvjitlink,nvrtc,nvtx]==12.8.1; platform_system == \"Linux\"->torch>=2.0.0->kornia) (12.8.1)\n","Requirement already satisfied: cuda-bindings<13,>=12.9.4 in /usr/local/lib/python3.12/dist-packages (from torch>=2.0.0->kornia) (12.9.6)\n","Requirement already satisfied: nvidia-cudnn-cu12==9.19.0.56 in /usr/local/lib/python3.12/dist-packages (from torch>=2.0.0->kornia) (9.19.0.56)\n","Requirement already satisfied: nvidia-cusparselt-cu12==0.7.1 in /usr/local/lib/python3.12/dist-packages (from torch>=2.0.0->kornia) (0.7.1)\n","Requirement already satisfied: nvidia-nccl-cu12==2.28.9 in /usr/local/lib/python3.12/dist-packages (from torch>=2.0.0->kornia) (2.28.9)\n","Requirement already satisfied: nvidia-nvshmem-cu12==3.4.5 in /usr/local/lib/python3.12/dist-packages (from torch>=2.0.0->kornia) (3.4.5)\n","Requirement already satisfied: triton==3.6.0 in /usr/local/lib/python3.12/dist-packages (from torch>=2.0.0->kornia) (3.6.0)\n","Requirement already satisfied: nvidia-cublas-cu12==12.8.4.1.* in /usr/local/lib/python3.12/dist-packages (from cuda-toolkit[cublas,cudart,cufft,cufile,cupti,curand,cusolver,cusparse,nvjitlink,nvrtc,nvtx]==12.8.1; platform_system == \"Linux\"->torch>=2.0.0->kornia) (12.8.4.1)\n","Requirement already satisfied: nvidia-cuda-runtime-cu12==12.8.90.* in /usr/local/lib/python3.12/dist-packages (from cuda-toolkit[cublas,cudart,cufft,cufile,cupti,curand,cusolver,cusparse,nvjitlink,nvrtc,nvtx]==12.8.1; platform_system == \"Linux\"->torch>=2.0.0->kornia) (12.8.90)\n","Requirement already satisfied: nvidia-cufft-cu12==11.3.3.83.* in /usr/local/lib/python3.12/dist-packages (from cuda-toolkit[cublas,cudart,cufft,cufile,cupti,curand,cusolver,cusparse,nvjitlink,nvrtc,nvtx]==12.8.1; platform_system == \"Linux\"->torch>=2.0.0->kornia) (11.3.3.83)\n","Requirement already satisfied: nvidia-cufile-cu12==1.13.1.3.* in /usr/local/lib/python3.12/dist-packages (from cuda-toolkit[cublas,cudart,cufft,cufile,cupti,curand,cusolver,cusparse,nvjitlink,nvrtc,nvtx]==12.8.1; platform_system == \"Linux\"->torch>=2.0.0->kornia) (1.13.1.3)\n","Requirement already satisfied: nvidia-cuda-cupti-cu12==12.8.90.* in /usr/local/lib/python3.12/dist-packages (from cuda-toolkit[cublas,cudart,cufft,cufile,cupti,curand,cusolver,cusparse,nvjitlink,nvrtc,nvtx]==12.8.1; platform_system == \"Linux\"->torch>=2.0.0->kornia) (12.8.90)\n","Requirement already satisfied: nvidia-curand-cu12==10.3.9.90.* in /usr/local/lib/python3.12/dist-packages (from cuda-toolkit[cublas,cudart,cufft,cufile,cupti,curand,cusolver,cusparse,nvjitlink,nvrtc,nvtx]==12.8.1; platform_system == \"Linux\"->torch>=2.0.0->kornia) (10.3.9.90)\n","Requirement already satisfied: nvidia-cusolver-cu12==11.7.3.90.* in /usr/local/lib/python3.12/dist-packages (from cuda-toolkit[cublas,cudart,cufft,cufile,cupti,curand,cusolver,cusparse,nvjitlink,nvrtc,nvtx]==12.8.1; platform_system == \"Linux\"->torch>=2.0.0->kornia) (11.7.3.90)\n","Requirement already satisfied: nvidia-cusparse-cu12==12.5.8.93.* in /usr/local/lib/python3.12/dist-packages (from cuda-toolkit[cublas,cudart,cufft,cufile,cupti,curand,cusolver,cusparse,nvjitlink,nvrtc,nvtx]==12.8.1; platform_system == \"Linux\"->torch>=2.0.0->kornia) (12.5.8.93)\n","Requirement already satisfied: nvidia-nvjitlink-cu12==12.8.93.* in /usr/local/lib/python3.12/dist-packages (from cuda-toolkit[cublas,cudart,cufft,cufile,cupti,curand,cusolver,cusparse,nvjitlink,nvrtc,nvtx]==12.8.1; platform_system == \"Linux\"->torch>=2.0.0->kornia) (12.8.93)\n","Requirement already satisfied: nvidia-cuda-nvrtc-cu12==12.8.93.* in /usr/local/lib/python3.12/dist-packages (from cuda-toolkit[cublas,cudart,cufft,cufile,cupti,curand,cusolver,cusparse,nvjitlink,nvrtc,nvtx]==12.8.1; platform_system == \"Linux\"->torch>=2.0.0->kornia) (12.8.93)\n","Requirement already satisfied: nvidia-nvtx-cu12==12.8.90.* in /usr/local/lib/python3.12/dist-packages (from cuda-toolkit[cublas,cudart,cufft,cufile,cupti,curand,cusolver,cusparse,nvjitlink,nvrtc,nvtx]==12.8.1; platform_system == \"Linux\"->torch>=2.0.0->kornia) (12.8.90)\n","Requirement already satisfied: cuda-pathfinder~=1.1 in /usr/local/lib/python3.12/dist-packages (from cuda-bindings<13,>=12.9.4->torch>=2.0.0->kornia) (1.5.4)\n","Requirement already satisfied: mpmath<1.4,>=1.1.0 in /usr/local/lib/python3.12/dist-packages (from sympy>=1.13.3->torch>=2.0.0->kornia) (1.3.0)\n","Requirement already satisfied: MarkupSafe>=2.0 in /usr/local/lib/python3.12/dist-packages (from jinja2->torch>=2.0.0->kornia) (3.0.3)\n","Downloading kornia-0.8.3-py3-none-any.whl (1.2 MB)\n","\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.2/1.2 MB\u001b[0m \u001b[31m27.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n","\u001b[?25hDownloading kornia_rs-0.1.14-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (3.7 MB)\n","\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m3.7/3.7 MB\u001b[0m \u001b[31m104.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n","\u001b[?25hInstalling collected packages: kornia-rs, kornia\n","Successfully installed kornia-0.8.3 kornia-rs-0.1.14\n","Drive already mounted at /content/drive; to attempt to forcibly remount, call drive.mount(\"/content/drive\", force_remount=True).\n","Extracting ZIP...\n","Found 574 images\n","Using: cuda\n","Loading model...\n"]},{"output_type":"stream","name":"stderr","text":["/usr/local/lib/python3.12/dist-packages/timm/models/layers/__init__.py:49: FutureWarning: Importing from timm.models.layers is deprecated, please import via timm.layers\n"," warnings.warn(f\"Importing from {__name__} is deprecated, please import via timm.layers\", FutureWarning)\n","/usr/local/lib/python3.12/dist-packages/timm/models/registry.py:4: FutureWarning: Importing from timm.models.registry is deprecated, please import via timm.models\n"," warnings.warn(f\"Importing from {__name__} is deprecated, please import via timm.models\", FutureWarning)\n"]},{"output_type":"display_data","data":{"text/plain":["model.safetensors: 0%| | 0.00/885M [00:00<?, ?B/s]"],"application/vnd.jupyter.widget-view+json":{"version_major":2,"version_minor":0,"model_id":"ddcc887317f9469bba681e9528b78b85"}},"metadata":{}},{"output_type":"display_data","data":{"text/plain":["Loading weights: 0%| | 0/754 [00:00<?, ?it/s]"],"application/vnd.jupyter.widget-view+json":{"version_major":2,"version_minor":0,"model_id":"57b528ec25ae423fa8628c3882a8a725"}},"metadata":{}},{"output_type":"stream","name":"stdout","text":["Model loaded\n","Removing backgrounds...\n"]},{"output_type":"stream","name":"stderr","text":[" 22%|β–ˆβ–ˆβ– | 125/574 [05:12<16:01, 2.14s/it]"]}]}]}