<jupyter_start><jupyter_text>Detectron2 z DeTr z Resnet50 LR=1-4 Gamma=0.1 Scheduler [300000,400000] Max 500k CROP.ENABLED = False  DocDeTr https://arxiv.org/pdf/2005.12872.pdf Detectron2  https://github.com/facebookresearch/detectron2 Detectron2 Beginner's Tutorial https://colab.research.google.com/drive/16jcaJoc6bCFAQ96jDe2HwtXj7BMD_-m5 Documentation https://detectron2.readthedocs.io<jupyter_code>from google.colab import drive
drive.mount('/content/drive')
import os
def Wider_load(val=True,train=True,test=False):
  os.makedirs('WIDER/', exist_ok=True)

  if val:
    #!gdown https://drive.google.com/uc?id=0B6eKvaijfFUDd3dIRmpvSk8tLUk
    !gdown https://drive.google.com/uc?id=1-5A_pa_jDS7gk8mHVCBB7ApV5KN8jWDr -O WIDER/tempv.zip
    !unzip -q WIDER/tempv.zip -d WIDER
    !rm WIDER/tempv.zip  

  if train:
    ### WIDER Face Training Images
    #!gdown https://drive.google.com/uc?id=0B6eKvaijfFUDQUUwd21EckhUbWs
    !gdown https://drive.google.com/uc?id=1-1iJfmXKYvAx9uLdRDX5W6HHG_KZv1jH -O WIDER/temptr.zip
    !unzip -q WIDER/temptr.zip -d WIDER
    !rm WIDER/temptr.zip
  
  if test:
    #!gdown https://drive.google.com/uc?id=0B6eKvaijfFUDbW4tdGpaYjgzZkU
    !gdown https://drive.google.com/uc?id=1tTpUJZEQMKDVxKT6100V5FwDuGX_8sDi -O WIDER/tempt.zip
    !unzip -q WIDER/tempt.zip -d WIDER
    !rm WIDER/tempt.zip


  ### Face annotations
  !wget mmlab.ie.cuhk.edu.hk/projects/WIDERFace/support/bbx_annotation/wider_face_split.zip -O WIDER/tempa.zip
  !unzip -q WIDER/tempa.zip -d WIDER
  !rm WIDER/tempa.zip

  #annotations
  !gdown https://drive.google.com/uc?id=1_9ydMZlTNFXBOMl16xsU8FSBmK2PW4lN -O WIDER/tools.py


  ### Examples and formats of the submissions
  #!wget mmlab.ie.cuhk.edu.hk/projects/WIDERFace/support/example/Submission_example.zip

def repo_load():
  !pip install cython pyyaml==5.1
  !pip install -U 'git+https://github.com/cocodataset/cocoapi.git#subdirectory=PythonAPI'

  # install detectron2:
  !git clone https://github.com/facebookresearch/detectron2 detectron2_repo
  !cd detectron2_repo && git reset a33fc53 --hard # v0.2
  !pip install -q -e detectron2_repo


repo_load()
Wider_load()
!git clone https://github.com/DarekGit/detr.git  
#!git clone https://github.com/facebookresearch/detr.git<jupyter_output>Cloning into 'detr'...
remote: Enumerating objects: 182, done.[K
remote: Total 182 (delta 0), reused 0 (delta 0), pack-reused 182[K
Receiving objects: 100% (182/182), 12.82 MiB | 45.11 MiB/s, done.
Resolving deltas: 100% (85/85), done.<jupyter_text>Restart runtime to continue... Crtl+M.<jupyter_code>!nvidia-smi
from psutil import virtual_memory
print('Your runtime has {:.1f} gigabytes of available RAM\n'.format(virtual_memory().total / 1e9))
import torch, torchvision
import detectron2
from detectron2.utils.logger import setup_logger
setup_logger()

from google.colab import drive
import os
import cv2
import random
import itertools
import shutil
import glob
import json
import numpy as np
import pandas as pd
from PIL import ImageDraw, Image
from collections import defaultdict
import matplotlib.pyplot as plt

from google.colab.patches import cv2_imshow

from detectron2 import model_zoo
import detectron2.utils.comm as comm
from detectron2.engine import DefaultPredictor, DefaultTrainer, HookBase
from detectron2.config import get_cfg
from detectron2.utils.visualizer import Visualizer
from detectron2.data import DatasetCatalog, MetadataCatalog, build_detection_train_loader
from detectron2.structures import BoxMode
from detectron2.data import build_detection_test_loader
from detectron2.data.datasets import register_coco_instances
from detectron2.evaluation import COCOEvaluator, inference_on_dataset
from WIDER.tools import annotations,output_Files
output_files=output_Files()
import sys
sys.path.append('/content/detr')
from d2.train_net import Trainer, setup
from d2.detr.config import add_detr_config<jupyter_output><empty_output><jupyter_text>Prepare the dataset WIDER FACE: A Face Detection Benchmarkhttp://shuoyang1213.me/WIDERFACE/ https://arxiv.org/pdf/1511.06523.pdf<jupyter_code># img list  limited to images with upto 100 objects
train = [m for m in annotations("train") if len(m['annotations']) <100]
val = [m for m in annotations("val") if len(m['annotations']) <100]
for d in ["train", "val"]:
  DatasetCatalog.register("face_" + d, lambda d=d: train if d == "train" else val)
  MetadataCatalog.get("face_" + d).set(thing_classes = ['face'])

faces_metadata = MetadataCatalog.get("face_train")

import pandas as pd
hist=dict(); hists={}
for i in (range(len(train))):
  f=len(train[i]['annotations'])
  hist[f]= 1 +hist.get(f,0)

for k in sorted(hist):hists[k]=hist[k]

pd.DataFrame([*hists.items()], index=None, columns=['ilosc twarzy','ilosc obrazow'])
len(train), len(val)<jupyter_output><empty_output><jupyter_text>"detr_256_6_6_torchvision.yaml"detr/configs<jupyter_code>cfg = get_cfg()
add_detr_config(cfg)

cfg_file='detr_256_6_6_torchvision.yaml'
cfg.merge_from_file('detr/d2/configs/'+cfg_file)

cfg.MODEL.PIXEL_MEAN =(119.857,110.808,104.148)
#cfg.MODEL.PIXEL_STD =(77.168,74.631,75.842)
cfg.MODEL.PIXEL_STD =(0.6076259372440945, 0.5876417505511812, 0.5971849743307086)
cfg.INPUT.FORMAT='RGB'
cfg.MODEL.FORMAT='RGB'
#cfg.MODEL.SEM_SEG_HEAD.NUM_CLASSES=1
#cfg.MODEL.ROI_HEADS.NUM_CLASSES = 1  # only has one class 
cfg.MODEL.DETR.NUM_CLASSES = 1
cfg.MODEL.DETR.NUM_OBJECT_QUERIES = 100 #max nb of objects
#pretrained model weights
!python detr/d2/converter.py --source_model https://dl.fbaipublicfiles.com/detr/detr-r50-e632da11.pth --output_model converted_model.pth >_
cfg.DATASETS.TRAIN = ("face_train",)
cfg.DATASETS.TEST = ("face_val",)
cfg.DATASETS.VAL = ("face_val",)
cfg.DATALOADER.NUM_WORKERS = 8

cfg.MODEL.RESNETS.DEPTH = 50
cfg.MODEL.BACKBONE.FREEZE_AT = 0
cfg.MODEL.WEIGHTS = "converted_model.pth" 
cfg.MODEL.RESNETS.NORM = 'BN' ##FrozeenBN

cfg.SOLVER.IMS_PER_BATCH = 1
cfg.SOLVER.BASE_LR = 0.0001  #  LR 1e-4
cfg.SOLVER.BACKBONE_MULTIPLIER = 1
cfg.SOLVER.MAX_ITER = 500000    

cfg.SOLVER.LR_SCHEDULER_NAME = "WarmupMultiStepLR"
cfg.SOLVER.WARMUP_ITERS = 1000
cfg.SOLVER.WARMUP_FACTOR = .01
cfg.SOLVER.STEPS =[300000,400000]
cfg.SOLVER.GAMMA = 0.1
cfg.SOLVER.CLIP_GRADIENTS.CLIP_VALUE = 0.1
cfg.SOLVER.CHECKPOINT_PERIOD = 10000
cfg.TEST.EVAL_PERIOD = 6440


cfg.INPUT.CROP.ENABLED = False
cfg.INPUT.MAX_SIZE_TEST = 2000
cfg.INPUT.MAX_SIZE_TRAIN = 2000
cfg.INPUT.MIN_SIZE_TRAIN = (512, 800, 1600)
cfg.INPUT.CROP.SIZE = (1600,2400)

drive.mount('/content/drive')

cfg.OUTPUT_DIR = os.path.join("./drive/My Drive/Face detection", "WIDER_DeTr_V2")
os.makedirs(cfg.OUTPUT_DIR, exist_ok=True)
#cfg.MODEL.WEIGHTS = os.path.join(cfg.OUTPUT_DIR,'model_2199999.pth') ###
print(cfg.dump())

import json
cfg_all=cfg_file.split('.')[-2]+'_ALL.json'
cfg_all = os.path.join(cfg.OUTPUT_DIR, cfg_all)
with open(cfg_all,'w') as f:
  json.dump(cfg,f)

os.makedirs(cfg.OUTPUT_DIR, exist_ok=True)
trainer = Trainer(cfg) 

'''
val_loss = Validation_Loss(cfg)  
trainer.register_hooks([val_loss])
trainer._hooks = trainer._hooks[:-2] + trainer._hooks[-2:][::-1]
'''

trainer.resume_or_load(resume=True)
trainer.train()<jupyter_output><empty_output><jupyter_text>**231 839 iterations**``` Average Precision  (AP) @[ IoU=0.50:0.95 | area=   all | maxDets=100 ] = 0.067 Average Precision  (AP) @[ IoU=0.50      | area=   all | maxDets=100 ] = 0.161 Average Precision  (AP) @[ IoU=0.75      | area=   all | maxDets=100 ] = 0.044 Average Precision  (AP) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.010 Average Precision  (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.161 Average Precision  (AP) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.406 Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets=  1 ] = 0.030 Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets= 10 ] = 0.080 Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets=100 ] = 0.125 Average Recall     (AR) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.028 Average Recall     (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.329 Average Recall     (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.568[12/04 13:54:44 d2.evaluation.coco_evaluation]: Evaluation results for bbox: |  AP   |  AP50  |  AP75  |  APs  |  APm   |  APl   ||:-----:|:------:|:------:|:-----:|:------:|:------:|| 6.677 | 16.076 | 4.442  | 0.952 | 16.069 | 40.595 |``` **392 839 iterations**``` Average Precision  (AP) @[ IoU=0.50:0.95 | area=   all | maxDets=100 ] = 0.108 Average Precision  (AP) @[ IoU=0.50      | area=   all | maxDets=100 ] = 0.234 Average Precision  (AP) @[ IoU=0.75      | area=   all | maxDets=100 ] = 0.087 Average Precision  (AP) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.013 Average Precision  (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.277 Average Precision  (AP) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.541 Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets=  1 ] = 0.040 Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets= 10 ] = 0.118 Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets=100 ] = 0.174 Average Recall     (AR) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.060 Average Recall     (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.422 Average Recall     (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.668[12/05 15:06:11 d2.evaluation.coco_evaluation]: Evaluation results for bbox: |   AP   |  AP50  |  AP75  |  APs  |  APm   |  APl   ||:------:|:------:|:------:|:-----:|:------:|:------:|| 10.761 | 23.447 | 8.692  | 1.330 | 27.686 | 54.144 |``` **431 479 iterations**``` Average Precision  (AP) @[ IoU=0.50:0.95 | area=   all | maxDets=100 ] = 0.121 Average Precision  (AP) @[ IoU=0.50      | area=   all | maxDets=100 ] = 0.254 Average Precision  (AP) @[ IoU=0.75      | area=   all | maxDets=100 ] = 0.101 Average Precision  (AP) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.018 Average Precision  (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.303 Average Precision  (AP) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.552 Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets=  1 ] = 0.043 Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets= 10 ] = 0.128 Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets=100 ] = 0.188 Average Recall     (AR) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.068 Average Recall     (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.456 Average Recall     (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.681[12/05 19:36:47 d2.evaluation.coco_evaluation]: Evaluation results for bbox: |   AP   |  AP50  |  AP75  |  APs  |  APm   |  APl   ||:------:|:------:|:------:|:-----:|:------:|:------:|| 12.084 | 25.430 | 10.089 | 1.769 | 30.310 | 55.197 |``` **470 119 iterations**``` Average Precision  (AP) @[ IoU=0.50:0.95 | area=   all | maxDets=100 ] = 0.122 Average Precision  (AP) @[ IoU=0.50      | area=   all | maxDets=100 ] = 0.259 Average Precision  (AP) @[ IoU=0.75      | area=   all | maxDets=100 ] = 0.102 Average Precision  (AP) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.018 Average Precision  (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.302 Average Precision  (AP) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.550 Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets=  1 ] = 0.043 Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets= 10 ] = 0.129 Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets=100 ] = 0.188 Average Recall     (AR) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.068 Average Recall     (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.455 Average Recall     (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.678[12/06 00:10:33 d2.evaluation.coco_evaluation]: Evaluation results for bbox: |   AP   |  AP50  |  AP75  |  APs  |  APm   |  APl   ||:------:|:------:|:------:|:-----:|:------:|:------:|| 12.213 | 25.859 | 10.177 | 1.819 | 30.150 | 55.028 |``` Użycie modelu<jupyter_code>cfg.MODEL.WEIGHTS = os.path.join(cfg.OUTPUT_DIR, "model_0469999.pth")
cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.05   # set the testing threshold for this model
cfg.DATASETS.TEST = ("face_val", )
predictor = DefaultPredictor(cfg)
dataset_dicts = val
for d in random.sample(dataset_dicts, 1):    
    im = cv2.imread(d["file_name"])
    outputs = predictor(im)
    v = Visualizer(im,metadata=faces_metadata, scale=0.5)
    v = v.draw_instance_predictions(outputs["instances"].to("cpu"))
    cv2_imshow(v.get_image())<jupyter_output><empty_output><jupyter_text>AP metric on val<jupyter_code>from detectron2.evaluation import COCOEvaluator, inference_on_dataset
from detectron2.data import build_detection_test_loader

trainer =Trainer(cfg)
trainer.resume_or_load(resume=False)
evaluator = COCOEvaluator("face_val", cfg, False, output_dir="./Output/")
val_loader = build_detection_test_loader(cfg, "face_val")
inference_on_dataset(trainer.model, val_loader, evaluator)
evaluator = COCOEvaluator("face_train", cfg, False, output_dir="./Output/")
val_loader = build_detection_test_loader(cfg, "face_train")
inference_on_dataset(trainer.model, val_loader, evaluator)<jupyter_output>[32m[12/06 09:25:01 d2.evaluation.coco_evaluation]: [0m'face_train' is not registered by `register_coco_instances`. Therefore trying to convert it to COCO format ...
[32m[12/06 09:25:01 d2.data.datasets.coco]: [0mConverting annotations of dataset 'face_train' to COCO format ...)
[32m[12/06 09:25:01 d2.data.datasets.coco]: [0mConverting dataset dicts into COCO format
[32m[12/06 09:25:17 d2.data.datasets.coco]: [0mConversion finished, #images: 12635, #annotations: 101174
[32m[12/06 09:25:17 d2.data.datasets.coco]: [0mCaching COCO format annotations at './Output/face_train_coco_format.json' ...
[32m[12/06 09:25:21 d2.data.common]: [0mSerializing 12635 elements to byte tensors and concatenating them all ...
[32m[12/06 09:25:21 d2.data.common]: [0mSerialized dataset takes 9.18 MiB
[32m[12/06 09:25:21 d2.data.dataset_mapper]: [0mAugmentations used in training: [ResizeShortestEdge(short_edge_length=(800, 800), max_size=2000, sample_style='choice')]
[32m[12/06 09:25:21 d2.eval[...]
{'fraction_non_alphanumeric': 0.1615536166619758, 'fraction_numerical': 0.09330143540669857, 'mean_word_length': 4.192911947387651, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 24, 'https://': 17, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '652034', 'license': 'Apache-2.0', 'n_tokens_mistral': 6643, 'n_tokens_neox': 5769, 'n_words': 1142, 'path': 'WIDERFACE_Detectron2_DD_DeTr_R50_V2.ipynb', 'repo_name': 'DarekGit/detr', 'size': '14212'}