{"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":"gpu","dataSources":[{"sourceId":113558,"databundleVersionId":14174843,"isSourceIdPinned":false,"sourceType":"competition"},{"sourceId":13541522,"sourceType":"datasetVersion","datasetId":8596517}],"dockerImageVersionId":31153,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Segmentation of Forgery Detection with YOLO\n\nThis notebook explores the application of YOLO (You Only Look Once) segmentation models for detecting image forgeries. Image forgery detection has become increasingly important in the digital age, where sophisticated editing tools make it easy to manipulate visual content. The notebook demonstrates how modern deep learning approaches, specifically YOLO's segmentation capabilities, can be leveraged to identify tampered or forged regions in images—a critical task for digital forensics, journalism verification, and maintaining content authenticity.\n\nThe dataset conversion is in https://www.kaggle.com/code/jirkaborovec/forgery-detection-convert-to-coco-yolo\n\nConverted dataset ready to be used in https://www.kaggle.com/datasets/jirkaborovec/forgerydetection-yolo-segmentation","metadata":{}},{"cell_type":"code","source":"! pip install -q -U ultralytics \"opencv-python<4.11\"","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-10-29T17:41:52.152973Z","iopub.execute_input":"2025-10-29T17:41:52.153240Z","iopub.status.idle":"2025-10-29T17:43:14.395342Z","shell.execute_reply.started":"2025-10-29T17:41:52.153218Z","shell.execute_reply":"2025-10-29T17:43:14.394602Z"},"_kg_hide-input":true,"_kg_hide-output":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Load a model","metadata":{}},{"cell_type":"code","source":"from ultralytics import YOLO\n\n# load a pretrained model (recommended for training)\nmodel = YOLO(\"yolo11s-seg.pt\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-29T17:43:14.396299Z","iopub.execute_input":"2025-10-29T17:43:14.396583Z","iopub.status.idle":"2025-10-29T17:43:18.484318Z","shell.execute_reply.started":"2025-10-29T17:43:14.396559Z","shell.execute_reply":"2025-10-29T17:43:18.483424Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Train the model","metadata":{}},{"cell_type":"code","source":"import os\n\nCOCO_YOLO_DATASET = \"/kaggle/input/forgerydetection-yolo-segmentation/dataset.yaml\"\nprint(f\"dataset is {os.path.exists(COCO_YOLO_DATASET)}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-29T17:43:18.485898Z","iopub.execute_input":"2025-10-29T17:43:18.486233Z","iopub.status.idle":"2025-10-29T17:43:18.491884Z","shell.execute_reply.started":"2025-10-29T17:43:18.486215Z","shell.execute_reply":"2025-10-29T17:43:18.491249Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"results = model.train(data=COCO_YOLO_DATASET, epochs=100, imgsz=640)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-29T17:43:18.492586Z","iopub.execute_input":"2025-10-29T17:43:18.492812Z","execution_failed":"2025-10-29T17:53:58.013Z"},"_kg_hide-output":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Results","metadata":{}},{"cell_type":"code","source":"!ls /kaggle/working/runs/segment/train","metadata":{"trusted":true,"execution":{"execution_failed":"2025-10-29T17:53:58.014Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from IPython.display import display, Image as ipyImage\n\n# Display the results image\ndisplay(ipyImage('/kaggle/working/runs/segment/train/results.png'))","metadata":{"trusted":true,"execution":{"execution_failed":"2025-10-29T17:53:58.014Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Predict","metadata":{}},{"cell_type":"code","source":"from PIL import Image\n\n# Run prediction on a single image\n# Replace 'path/to/your/image.jpg' with the actual path to your image file\nresults = model.predict('/kaggle/input/recodai-luc-scientific-image-forgery-detection/test_images/45.png')\n\n# Display the results (optional)\nfor r in results:\n    im_array = r.plot()  # plot a BGR numpy array of predictions\n    im = Image.fromarray(im_array[..., ::-1])  # RGB PIL image\n    display(im)","metadata":{"trusted":true,"execution":{"execution_failed":"2025-10-29T17:53:58.014Z"}},"outputs":[],"execution_count":null}]}