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
|
| 6 |
+
|
| 7 |
+
1. **Install dependencies** (run once):
|
| 8 |
+
```bash
|
| 9 |
+
!pip install -U gdown ultralytics wandb scikit-learn requests
|
| 10 |
+
```
|
| 11 |
+
|
| 12 |
+
2. **Mount Google Drive and set working directory**:
|
| 13 |
+
```python
|
| 14 |
+
from google.colab import drive
|
| 15 |
+
drive.mount('/content/drive', force_remount=False)
|
| 16 |
+
import os
|
| 17 |
+
os.chdir('/content/drive/MyDrive/Colab/Watermark_remover')
|
| 18 |
+
```
|
| 19 |
+
|
| 20 |
+
3. **Download and prepare datasets**
|
| 21 |
+
The script downloads watermark datasets from Google Drive, extracts them, and collects images for watermarking.
|
| 22 |
+
|
| 23 |
+
4. **Generate watermarked images and YOLO labels**
|
| 24 |
+
Watermarks are added to images with bounding box labels created in YOLO format.
|
| 25 |
+
|
| 26 |
+
5. **Split dataset into training and validation sets** and create `data.yaml` for YOLOv11 training.
|
| 27 |
+
|
| 28 |
+
6. **Train the YOLOv11 model** with augmentations and tuned hyperparameters:
|
| 29 |
+
```python
|
| 30 |
+
from ultralytics import YOLO
|
| 31 |
+
import wandb
|
| 32 |
+
|
| 33 |
+
wandb.login() # Login to Weights & Biases for experiment tracking
|
| 34 |
+
|
| 35 |
+
model = YOLO("yolo11m.pt") # Load YOLOv11m base model
|
| 36 |
+
|
| 37 |
+
model.train(
|
| 38 |
+
data="data.yaml",
|
| 39 |
+
epochs=100,
|
| 40 |
+
batch=16,
|
| 41 |
+
imgsz=640,
|
| 42 |
+
project="logo_detection",
|
| 43 |
+
name="yolo11m_logo_run",
|
| 44 |
+
exist_ok=True,
|
| 45 |
+
save=True,
|
| 46 |
+
save_txt=True,
|
| 47 |
+
augment=True,
|
| 48 |
+
hsv_h=0.015,
|
| 49 |
+
hsv_s=0.7,
|
| 50 |
+
fliplr=0.5,
|
| 51 |
+
mixup=0.1,
|
| 52 |
+
mosaic=1.0,
|
| 53 |
+
scale=0.5,
|
| 54 |
+
shear=0.0,
|
| 55 |
+
perspective=0.0,
|
| 56 |
+
translate=0.1
|
| 57 |
+
)
|
| 58 |
+
```
|
| 59 |
+
|
| 60 |
+
### Testing and Visualization
|
| 61 |
+
|
| 62 |
+
1. **Load the trained model weights**:
|
| 63 |
+
```python
|
| 64 |
+
from ultralytics import YOLO
|
| 65 |
+
model = YOLO("logo_detection/yolo11m_logo_run/weights/best.pt")
|
| 66 |
+
```
|
| 67 |
+
|
| 68 |
+
2. **Select test images** from the validation set:
|
| 69 |
+
```python
|
| 70 |
+
from pathlib import Path
|
| 71 |
+
import random
|
| 72 |
+
|
| 73 |
+
test_folder = Path("dataset/images/val")
|
| 74 |
+
test_images = list(test_folder.glob("*.*"))
|
| 75 |
+
test_images = random.sample(test_images, min(10, len(test_images)))
|
| 76 |
+
```
|
| 77 |
+
|
| 78 |
+
3. **Run detection and watermark removal with visualization**:
|
| 79 |
+
```python
|
| 80 |
+
import cv2
|
| 81 |
+
import numpy as np
|
| 82 |
+
import matplotlib.pyplot as plt
|
| 83 |
+
|
| 84 |
+
def visualize_detection_and_removal(model, img_path):
|
| 85 |
+
results = model(str(img_path))[0]
|
| 86 |
+
img = cv2.imread(str(img_path))
|
| 87 |
+
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
| 88 |
+
|
| 89 |
+
# Draw detection boxes
|
| 90 |
+
img_boxes = img.copy()
|
| 91 |
+
for box in results.boxes:
|
| 92 |
+
xyxy = box.xyxy[0].cpu().numpy().astype(int)
|
| 93 |
+
cv2.rectangle(img_boxes, (xyxy[0], xyxy[1]), (xyxy[2], xyxy[3]), (0,255,0), 2)
|
| 94 |
+
|
| 95 |
+
# Create mask for inpainting
|
| 96 |
+
mask = np.zeros(img.shape[:2], dtype=np.uint8)
|
| 97 |
+
for box in results.boxes:
|
| 98 |
+
xyxy = box.xyxy[0].cpu().numpy().astype(int)
|
| 99 |
+
x1, y1, x2, y2 = xyxy
|
| 100 |
+
mask[y1:y2, x1:x2] = 255
|
| 101 |
+
|
| 102 |
+
# Remove watermark using inpainting
|
| 103 |
+
inpainted = cv2.inpaint(img, mask, 3, cv2.INPAINT_TELEA)
|
| 104 |
+
inpainted_rgb = cv2.cvtColor(inpainted, cv2.COLOR_BGR2RGB)
|
| 105 |
+
|
| 106 |
+
# Display images
|
| 107 |
+
plt.figure(figsize=(15,5))
|
| 108 |
+
plt.subplot(1,3,1)
|
| 109 |
+
plt.title("Original Image")
|
| 110 |
+
plt.imshow(img_rgb)
|
| 111 |
+
plt.axis('off')
|
| 112 |
+
|
| 113 |
+
plt.subplot(1,3,2)
|
| 114 |
+
plt.title("Detected Logos")
|
| 115 |
+
plt.imshow(cv2.cvtColor(img_boxes, cv2.COLOR_BGR2RGB))
|
| 116 |
+
plt.axis('off')
|
| 117 |
+
|
| 118 |
+
plt.subplot(1,3,3)
|
| 119 |
+
plt.title("Watermark Removed")
|
| 120 |
+
plt.imshow(inpainted_rgb)
|
| 121 |
+
plt.axis('off')
|
| 122 |
+
plt.show()
|
| 123 |
+
|
| 124 |
+
for img_path in test_images:
|
| 125 |
+
print(f"Testing image: {img_path.name}")
|
| 126 |
+
visualize_detection_and_removal(model, img_path)
|
| 127 |
+
```
|
| 128 |
+
|
| 129 |
+
---
|
| 130 |
+
|
| 131 |
+
### Summary
|
| 132 |
+
|
| 133 |
+
- This repository provides a pipeline to generate watermarked images with YOLO labels, train a YOLOv11 model to detect logos/watermarks, and remove them using inpainting.
|
| 134 |
+
- Training is done in Colab with Google Drive for storage.
|
| 135 |
+
- Testing visualizes detection and watermark removal results on sample validation images.
|
| 136 |
+
|
| 137 |
+
|
| 138 |
+
Citations:
|
| 139 |
+
[1] https://huggingface.co/templates/model-card-example/blob/f0ce9d5d178c10e164d406868f72b1f2f2158cde/README.md
|
| 140 |
+
[2] https://github.com/huggingface/datasets/blob/main/templates/README_guide.md
|
| 141 |
+
[3] https://huggingface.co/docs/hub/en/model-cards
|
| 142 |
+
[4] https://huggingface.co/templates/model-card-example/blame/f0ce9d5d178c10e164d406868f72b1f2f2158cde/README.md
|
| 143 |
+
[5] https://machinelearninglibrarian.substack.com/p/2023-03-07-readme-templatehtml
|
| 144 |
+
[6] https://huggingface.co/templates/model-card-example/commit/f0ce9d5d178c10e164d406868f72b1f2f2158cde
|
| 145 |
+
[7] https://huggingface.co/learn/llm-course/en/chapter4/4
|
| 146 |
+
[8] https://huggingface.co/SEBIS/code_trans_t5_base_code_documentation_generation_ruby/blame/2a39c4e86977714a6ed4aab478098a43e9751e05/README.md
|
| 147 |
+
|
| 148 |
+
---
|
| 149 |
+
Answer from Perplexity: pplx.ai/share
|