Update README.md
Browse files
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 =
|
43 |
image = Image.open(requests.get(url, stream=True).raw)
|
44 |
|
45 |
-
feature_extractor = DetrFeatureExtractor.from_pretrained(
|
46 |
-
model = DetrForObjectDetection.from_pretrained(
|
47 |
|
48 |
inputs = feature_extractor(images=image, return_tensors="pt")
|
49 |
outputs = model(**inputs)
|
50 |
|
51 |
-
#
|
52 |
-
|
53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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-101")
|
49 |
+
model = DetrForObjectDetection.from_pretrained("facebook/detr-resnet-101")
|
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 (something along the lines of):
|
68 |
+
```
|
69 |
+
Detected cat with confidence 0.998 at location [344.06, 24.85, 640.34, 373.74]
|
70 |
+
Detected remote with confidence 0.997 at location [328.13, 75.93, 372.81, 187.66]
|
71 |
+
Detected remote with confidence 0.997 at location [39.34, 70.13, 175.56, 118.78]
|
72 |
+
Detected cat with confidence 0.998 at location [15.36, 51.75, 316.89, 471.16]
|
73 |
+
Detected couch with confidence 0.995 at location [-0.19, 0.71, 639.73, 474.17]
|
74 |
```
|
75 |
|
76 |
Currently, both the feature extractor and model support PyTorch.
|