.. _sec_bbox:
Nesne Algılama ve Kuşatan Kutular
=================================
Önceki bölümlerde (örn.
:numref:`sec_alexnet`—-:numref:`sec_googlenet`), imge sınıflandırma
için çeşitli modeller tanıttık. İmge sınıflandırma görevlerinde, resimde
sadece *bir* ana nesne olduğunu varsayıyoruz ve sadece kategorisini
nasıl tanıyacağımıza odaklanıyoruz. Bununla birlikte, ilgili imgede
sıklıkla *çoklu* nesne vardır. Sadece kategorilerini değil, aynı zamanda
imgedeki belirli konumlarını da bilmek istiyoruz. Bilgisayarla görmede,
*nesne algılama* (veya *nesne tanıma*) gibi görevlere atıfta
bulunuyoruz.
Nesne algılama birçok alanda yaygın olarak uygulanmıştır. Örneğin, kendi
kendine sürüş, çekilen video görüntülerinde araçların, yayaların,
yolların ve engellerin konumlarını tespit ederek seyahat rotalarını
planlamalıdır. Ayrıca, robotlar bu tekniği, bir ortamdaki gezinti
boyunca ilgilenen nesneleri tespit etmek ve yerini belirlemek için
kullanabilir. Ayrıca, güvenlik sistemlerinin davetsiz misafir veya bomba
gibi sıradışı nesneleri algılaması gerekebilir.
Sonraki birkaç bölümde, nesne algılama için çeşitli derin öğrenme
yöntemlerini tanıtacağız. Nesnelerin *konumlarına* bir giriş ile
başlayacağız.
.. raw:: html
.. raw:: html
.. raw:: latex
\diilbookstyleinputcell
.. code:: python
#@save
def box_corner_to_center(boxes):
"""(sol üst, sağ alt) konumundan (merkez, genişlik, yükseklik) konumuna dönüştür."""
x1, y1, x2, y2 = boxes[:, 0], boxes[:, 1], boxes[:, 2], boxes[:, 3]
cx = (x1 + x2) / 2
cy = (y1 + y2) / 2
w = x2 - x1
h = y2 - y1
boxes = np.stack((cx, cy, w, h), axis=-1)
return boxes
#@save
def box_center_to_corner(boxes):
"""(sol üst, sağ alt) konumundan (merkez, genişlik, yükseklik) konumuna dönüştür."""
cx, cy, w, h = boxes[:, 0], boxes[:, 1], boxes[:, 2], boxes[:, 3]
x1 = cx - 0.5 * w
y1 = cy - 0.5 * h
x2 = cx + 0.5 * w
y2 = cy + 0.5 * h
boxes = np.stack((x1, y1, x2, y2), axis=-1)
return boxes
.. raw:: html
.. raw:: html
.. raw:: latex
\diilbookstyleinputcell
.. code:: python
#@save
def box_corner_to_center(boxes):
"""(sol üst, sağ alt) konumundan (merkez, genişlik, yükseklik) konumuna dönüştür."""
x1, y1, x2, y2 = boxes[:, 0], boxes[:, 1], boxes[:, 2], boxes[:, 3]
cx = (x1 + x2) / 2
cy = (y1 + y2) / 2
w = x2 - x1
h = y2 - y1
boxes = torch.stack((cx, cy, w, h), axis=-1)
return boxes
#@save
def box_center_to_corner(boxes):
"""(sol üst, sağ alt) konumundan (merkez, genişlik, yükseklik) konumuna dönüştür."""
cx, cy, w, h = boxes[:, 0], boxes[:, 1], boxes[:, 2], boxes[:, 3]
x1 = cx - 0.5 * w
y1 = cy - 0.5 * h
x2 = cx + 0.5 * w
y2 = cy + 0.5 * h
boxes = torch.stack((x1, y1, x2, y2), axis=-1)
return boxes
.. raw:: html
.. raw:: html
.. raw:: latex
\diilbookstyleinputcell
.. code:: python
#@save
def box_corner_to_center(boxes):
"""(sol üst, sağ alt) konumundan (merkez, genişlik, yükseklik) konumuna dönüştür."""
x1, y1, x2, y2 = boxes[:, 0], boxes[:, 1], boxes[:, 2], boxes[:, 3]
cx = (x1 + x2) / 2
cy = (y1 + y2) / 2
w = x2 - x1
h = y2 - y1
boxes = tf.stack((cx, cy, w, h), axis=-1)
return boxes
#@save
def box_center_to_corner(boxes):
"""(sol üst, sağ alt) konumundan (merkez, genişlik, yükseklik) konumuna dönüştür."""
cx, cy, w, h = boxes[:, 0], boxes[:, 1], boxes[:, 2], boxes[:, 3]
x1 = cx - 0.5 * w
y1 = cy - 0.5 * h
x2 = cx + 0.5 * w
y2 = cy + 0.5 * h
boxes = tf.stack((x1, y1, x2, y2), axis=-1)
return boxes
.. raw:: html
.. raw:: html
Koordinat bilgilerine göre resimdeki köpek ve kedinin kuşatan kutularını
tanımlayacağız. İmgedeki koordinatların orijini imgenin sol üst
köşesidir ve sağ ve aşağı sırasıyla :math:`x` ve :math:`y` eksenlerinin
pozitif yönleridir.
.. raw:: latex
\diilbookstyleinputcell
.. code:: python
# Burada `bbox` kuşatan kutunun kısaltmasıdır
dog_bbox, cat_bbox = [60.0, 45.0, 378.0, 516.0], [400.0, 112.0, 655.0, 493.0]
İki kuşatan kutu dönüştürme işlevinin doğruluğunu iki kez dönüştürerek
doğrulayabiliriz.
.. raw:: html
.. raw:: html
.. raw:: latex
\diilbookstyleinputcell
.. code:: python
boxes = np.array((dog_bbox, cat_bbox))
box_center_to_corner(box_corner_to_center(boxes)) == boxes
.. raw:: latex
\diilbookstyleoutputcell
.. parsed-literal::
:class: output
array([[ True, True, True, True],
[ True, True, True, True]])
.. raw:: html
.. raw:: html
.. raw:: latex
\diilbookstyleinputcell
.. code:: python
boxes = torch.tensor((dog_bbox, cat_bbox))
box_center_to_corner(box_corner_to_center(boxes)) == boxes
.. raw:: latex
\diilbookstyleoutputcell
.. parsed-literal::
:class: output
tensor([[True, True, True, True],
[True, True, True, True]])
.. raw:: html
.. raw:: html
.. raw:: latex
\diilbookstyleinputcell
.. code:: python
boxes = tf.constant((dog_bbox, cat_bbox))
box_center_to_corner(box_corner_to_center(boxes)) == boxes
.. raw:: latex
\diilbookstyleoutputcell
.. parsed-literal::
:class: output
.. raw:: html
.. raw:: html
Doğru olup olmadıklarını kontrol etmek için resimdeki kuşatan kutuları
çizelim. Çizmeden önce, ``bbox_to_rect`` yardımcı işlevini
tanımlayacağız. ``matplotlib`` paketinin kuşatan kutu biçimindeki
kuşatan kutusunu temsil eder.
.. raw:: latex
\diilbookstyleinputcell
.. code:: python
#@save
def bbox_to_rect(bbox, color):
"""Kuşatan kutuyu matplotlib biçimine dönüştürün."""
# Kuşatan kutunun (sol üst x, sol üst y, sağ alt x, sağ alt y) biçimini matplotlib biçimine dönüştürün: ((sol üst x, sol üst y), genişlik, yükseklik)
return d2l.plt.Rectangle(
xy=(bbox[0], bbox[1]), width=bbox[2]-bbox[0], height=bbox[3]-bbox[1],
fill=False, edgecolor=color, linewidth=2)
İmgeye kuşatan kutuları ekledikten sonra, iki nesnenin ana hatlarının
temelde iki kutunun içinde olduğunu görebiliriz.
.. raw:: html