0% found this document useful (0 votes)
10 views10 pages

Lucrare 7 AI

The document discusses the application of artificial neural networks, specifically using the YOLOv5 algorithm for detecting airplanes in images. It outlines the process of creating image tiles, calculating bounds, width, and height, and saving the resulting tiles and labels. The YOLO algorithm is highlighted for its efficiency and precision in object detection by predicting bounding boxes and associated probabilities simultaneously.

Uploaded by

Cristian Lai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views10 pages

Lucrare 7 AI

The document discusses the application of artificial neural networks, specifically using the YOLOv5 algorithm for detecting airplanes in images. It outlines the process of creating image tiles, calculating bounds, width, and height, and saving the resulting tiles and labels. The YOLO algorithm is highlighted for its efficiency and precision in object detection by predicting bounding boxes and associated probabilities simultaneously.

Uploaded by

Cristian Lai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Laborator 7

Tema: Aplicații ale Rețelelor Neuronale Artificiale


def getBounds(geometry):
try:
arr = np.array(geometry).T
xmin = np.min(arr[0])
ymin = np.min(arr[1])
xmax = np.max(arr[0])
ymax = np.max(arr[1])
return (xmin, ymin, xmax, ymax)
except:
return np.nan

def getWidth(bounds):
try:
(xmin, ymin, xmax, ymax) = bounds
return np.abs(xmax - xmin)
except:
return np.nan

def getHeight(bounds):
try:
(xmin, ymin, xmax, ymax) = bounds
return np.abs(ymax - ymin)
except:
return np.nan

# Create bounds, width and height


df.loc[:,'bounds'] = df.loc[:,'geometry'].apply(getBounds)
df.loc[:,'width'] = df.loc[:,'bounds'].apply(getWidth)
df.loc[:,'height'] = df.loc[:,'bounds'].apply(getHeight)
df.head(10)
import os
import tqdm.notebook

TILE_WIDTH = 512
TILE_HEIGHT = 512
TILE_OVERLAP = 64
TRUNCATED_PERCENT = 0.3
_overwriteFiles = True

TILES_DIR = {'train': Path('train/images/'),


'val': Path('val/images/')}
for _, folder in TILES_DIR.items():
if not os.path.isdir(folder):
os.makedirs(folder)

LABELS_DIR = {'train': Path('train/labels/'),


'val': Path('val/labels/')}
for _, folder in LABELS_DIR.items():
if not os.path.isdir(folder):
os.makedirs(folder)

def tag_is_inside_tile(bounds, x_start, y_start, width, height,


truncated_percent):
x_min, y_min, x_max, y_max = bounds
x_min, y_min, x_max, y_max = x_min - x_start, y_min - y_start, x_max -
x_start, y_max - y_start

if (x_min > width) or (x_max < 0.0) or (y_min > height) or (y_max <
0.0):
return None

x_max_trunc = min(x_max, width)


x_min_trunc = max(x_min, 0)
if (x_max_trunc - x_min_trunc) / (x_max - x_min) < truncated_percent:
return None

y_max_trunc = min(y_max, width)


y_min_trunc = max(y_min, 0)
if (y_max_trunc - y_min_trunc) / (y_max - y_min) < truncated_percent:
return None
x_center = (x_min_trunc + x_max_trunc) / 2.0 / width
y_center = (y_min_trunc + y_max_trunc) / 2.0 / height
x_extend = (x_max_trunc - x_min_trunc) / width
y_extend = (y_max_trunc - y_min_trunc) / height

return (0, x_center, y_center, x_extend, y_extend)

for img_path in tqdm.notebook.tqdm(img_list):


pil_img = PIL.Image.open(img_path, mode='r')
np_img = np.array(pil_img, dtype=np.uint8)

img_labels = df[df["image_id"] == img_path.name]

X_TILES = (IMAGE_WIDTH + TILE_WIDTH - TILE_OVERLAP - 1) // (TILE_WIDTH


- TILE_OVERLAP)
Y_TILES = (IMAGE_HEIGHT + TILE_HEIGHT - TILE_OVERLAP - 1) //
(TILE_HEIGHT - TILE_OVERLAP)

for x in range(X_TILES):
for y in range(Y_TILES):

x_end = min((x + 1) * TILE_WIDTH - TILE_OVERLAP * (x != 0),


IMAGE_WIDTH)
x_start = x_end - TILE_WIDTH
y_end = min((y + 1) * TILE_HEIGHT - TILE_OVERLAP * (y != 0),
IMAGE_HEIGHT)
y_start = y_end - TILE_HEIGHT

folder = 'val' if img_path.name in val_indexes else 'train'


save_tile_path = TILES_DIR[folder].joinpath(img_path.stem + "_"
+ str(x_start) + "_" + str(y_start) + ".jpg")
save_label_path = LABELS_DIR[folder].joinpath(img_path.stem +
"_" + str(x_start) + "_" + str(y_start) + ".txt")

if _overwriteFiles or not os.path.isfile(save_tile_path):


cut_tile = np.zeros(shape=(TILE_WIDTH, TILE_HEIGHT, 3),
dtype=np.uint8)
cut_tile[0:TILE_HEIGHT, 0:TILE_WIDTH, :] =
np_img[y_start:y_end, x_start:x_end, :]
cut_tile_img = PIL.Image.fromarray(cut_tile)
cut_tile_img.save(save_tile_path)

found_tags = [
tag_is_inside_tile(bounds, x_start, y_start, TILE_WIDTH,
TILE_HEIGHT, TRUNCATED_PERCENT)
for i, bounds in enumerate(img_labels['bounds'])]
found_tags = [el for el in found_tags if el is not None]

with open(save_label_path, 'w+') as f:


for tags in found_tags:
f.write(' '.join(str(x) for x in tags) + '\n')
import glob
from IPython.display import Image, display

for image_path in glob.glob('yolov5/runs/detect/exp2/*.jpg'):


display(Image(filename=image_path, width=1024))
print("\n")
Concluzie:
În lucrarea dată, am utilizat algoritmul YOLOv5 pentru a detecta avioanele din imagini.
YOLO (You Only Look Once) este un algoritm de detecție a obiectelor care se remarcă prin
eficiența și precizia sa.
YOLO funcționează prin împărțirea imaginii într-o grilă și aplicarea unor regresii directe
pentru a prezice boxele încadratoare ale obiectelor și probabilitatea asociată fiecărei clase
de obiecte în fiecare celulă a grilei. În comparație cu alte metode care aplică clasificări și
regresii separate, YOLO prezice simultan localizările și clasele obiectelor, ceea ce îl face
extrem de rapid.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy