nielsr HF staff commited on
Commit
8c174de
1 Parent(s): 2729413

Improve README

Browse files
Files changed (1) hide show
  1. README.md +26 -6
README.md CHANGED
@@ -26,6 +26,8 @@ The DETR model is an encoder-decoder transformer with a convolutional backbone.
26
 
27
  The model is trained using a "bipartite matching loss": one compares the predicted classes + bounding boxes of each of the N = 100 object queries to the ground truth annotations, padded up to the same length N (so if an image only contains 4 objects, 96 annotations will just have a "no object" as class and "no bounding box" as bounding box). The Hungarian matching algorithm is used to create an optimal one-to-one mapping between each of the N queries and each of the N annotations. Next, standard cross-entropy (for the classes) and a linear combination of the L1 and generalized IoU loss (for the bounding boxes) are used to optimize the parameters of the model.
28
 
 
 
29
  ## Intended uses & limitations
30
 
31
  You can use the raw model for object detection. See the [model hub](https://huggingface.co/models?search=facebook/detr) to look for all available DETR models.
@@ -36,21 +38,39 @@ Here is how to use this model:
36
 
37
  ```python
38
  from transformers import DetrFeatureExtractor, DetrForObjectDetection
 
39
  from PIL import Image
40
  import requests
41
 
42
- url = 'http://images.cocodataset.org/val2017/000000039769.jpg'
43
  image = Image.open(requests.get(url, stream=True).raw)
44
 
45
- feature_extractor = DetrFeatureExtractor.from_pretrained('facebook/detr-resnet-50')
46
- model = DetrForObjectDetection.from_pretrained('facebook/detr-resnet-50')
47
 
48
  inputs = feature_extractor(images=image, return_tensors="pt")
49
  outputs = model(**inputs)
50
 
51
- # model predicts bounding boxes and corresponding COCO classes
52
- logits = outputs.logits
53
- bboxes = outputs.pred_boxes
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
  ```
55
 
56
  Currently, both the feature extractor and model support PyTorch.
 
26
 
27
  The model is trained using a "bipartite matching loss": one compares the predicted classes + bounding boxes of each of the N = 100 object queries to the ground truth annotations, padded up to the same length N (so if an image only contains 4 objects, 96 annotations will just have a "no object" as class and "no bounding box" as bounding box). The Hungarian matching algorithm is used to create an optimal one-to-one mapping between each of the N queries and each of the N annotations. Next, standard cross-entropy (for the classes) and a linear combination of the L1 and generalized IoU loss (for the bounding boxes) are used to optimize the parameters of the model.
28
 
29
+ ![model image](https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/model_doc/detr_architecture.png)
30
+
31
  ## Intended uses & limitations
32
 
33
  You can use the raw model for object detection. See the [model hub](https://huggingface.co/models?search=facebook/detr) to look for all available DETR models.
 
38
 
39
  ```python
40
  from transformers import DetrFeatureExtractor, DetrForObjectDetection
41
+ import torch
42
  from PIL import Image
43
  import requests
44
 
45
+ url = "http://images.cocodataset.org/val2017/000000039769.jpg"
46
  image = Image.open(requests.get(url, stream=True).raw)
47
 
48
+ feature_extractor = DetrFeatureExtractor.from_pretrained("facebook/detr-resnet-50")
49
+ model = DetrForObjectDetection.from_pretrained("facebook/detr-resnet-50")
50
 
51
  inputs = feature_extractor(images=image, return_tensors="pt")
52
  outputs = model(**inputs)
53
 
54
+ # convert outputs (bounding boxes and class logits) to COCO API
55
+ target_sizes = torch.tensor([image.size[::-1]])
56
+ results = feature_extractor.post_process(outputs, target_sizes=target_sizes)[0]
57
+
58
+ for score, label, box in zip(results["scores"], results["labels"], results["boxes"]):
59
+ box = [round(i, 2) for i in box.tolist()]
60
+ # let's only keep detections with score > 0.9
61
+ if score > 0.9:
62
+ print(
63
+ f"Detected {model.config.id2label[label.item()]} with confidence "
64
+ f"{round(score.item(), 3)} at location {box}."
65
+ )
66
+ ```
67
+ This should output:
68
+ ```
69
+ Detected remote with confidence 0.998 at location [40.16, 70.81, 175.55, 117.98]
70
+ Detected remote with confidence 0.996 at location [333.24, 72.55, 368.33, 187.66]
71
+ Detected couch with confidence 0.995 at location [-0.02, 1.15, 639.73, 473.76]
72
+ Detected cat with confidence 0.999 at location [13.24, 52.05, 314.02, 470.93]
73
+ Detected cat with confidence 0.999 at location [345.4, 23.85, 640.37, 368.72]
74
  ```
75
 
76
  Currently, both the feature extractor and model support PyTorch.