בינה מלאכותית RB108-6T : תרגיל בית
תרגיל 1 : שימוש ב GPU מול CPU
1.בדוק בעזרת בינה מלאכותית מה ההבדלים בין GPU מול CPU
2.איזה GPU יש מי החברות השולטות ומה ההבדל TPU
3. איזה GPU יתאים לשואב רובוטי לבית , רחפן אוטונומי, למחשב לחישוב קיפול חלבונים .
תרגיל 2 : תרגול עבודה עם GPU מול GPU
שים לב GPU שלך ב COLAB פעיל : T4
בחירה של GPU בקולאב : runtime -> change runtime type

בחר :

בחר :

תמונה לניסוי:
![]()
- נתח את הקוד הבא – נתח בעזרת
- מה תפקיד הטנזור ?
- מה תפקיד ספריית kornia
- למה עדיף להשתמש ב kornia ולא OPENCV עבור טנזורים
- הרץ את הקוד ב COLAB (לאחר הניתוח)
-
1pip install kornia pillow torchvision matplotlib
-
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283# Google Colab - upload image, choose CPU/GPU (with fallback), BLUR -> CANNY, print timing in ms!pip -q install kornia pillow torchvisionimport os, timeimport torchimport kornia as Kfrom PIL import Imageimport torchvision.transforms as Timport matplotlib.pyplot as pltfrom google.colab import files# -----------------------------# PARAM: choose where to run# "auto" = use GPU if available# "cuda" = force GPU (fallback to CPU if not available)# "cpu" = force CPU# -----------------------------RUN_ON = "cuda" # change to: "cpu" or "cuda" / "auto"has_cuda = torch.cuda.is_available()if RUN_ON == "cpu":device = "cpu"elif RUN_ON == "cuda":device = "cuda" if has_cuda else "cpu"else: # autodevice = "cuda" if has_cuda else "cpu"print("Requested:", RUN_ON, "| CUDA available:", has_cuda, "| Using device:", device)def t_ms_sync(t0, device):if device == "cuda":torch.cuda.synchronize()return (time.perf_counter() - t0) * 1000.0# ---- upload from your PC ----uploaded = files.upload()img_path = next(iter(uploaded.keys()))print("Loaded:", img_path)# ---- timing: load + to tensor ----t0 = time.perf_counter()img = Image.open(img_path).convert("RGB")x = T.ToTensor()(img).unsqueeze(0).to(device) # [1,3,H,W]load_ms = t_ms_sync(t0, device)# ---- timing: grayscale ----t0 = time.perf_counter()gray = K.color.rgb_to_grayscale(x) # [1,1,H,W]gray_ms = t_ms_sync(t0, device)# ---- timing: blur ----t0 = time.perf_counter()blur = K.filters.gaussian_blur2d(gray, kernel_size=(5, 5), sigma=(1.5, 1.5))blur_ms = t_ms_sync(t0, device)# ---- timing: canny ----t0 = time.perf_counter()edges, magnitude = K.filters.canny(blur, low_threshold=0.1, high_threshold=0.2)canny_ms = t_ms_sync(t0, device)# ---- timing: bring back + save ----t0 = time.perf_counter()edges_u8 = (edges[0, 0].detach().clamp(0, 1).cpu() * 255).to(torch.uint8).numpy()out_path = os.path.splitext(img_path)[0] + f"_edges_{device}.png"Image.fromarray(edges_u8).save(out_path)save_ms = (time.perf_counter() - t0) * 1000.0 # CPU workprint("\nTimings (ms):")print(f"Load + ToTensor + to(device): {load_ms:.3f} ms")print(f"Grayscale: {gray_ms:.3f} ms")print(f"Gaussian blur: {blur_ms:.3f} ms")print(f"Canny: {canny_ms:.3f} ms")print(f"CPU copy + save PNG: {save_ms:.3f} ms")print("Saved:", out_path)print("edges:", edges.shape, edges.device)# ---- plot ----plt.figure(figsize=(6, 6))plt.imshow(img); plt.axis("off"); plt.title("Original"); plt.show()plt.figure(figsize=(6, 6))plt.imshow(edges_u8, cmap="gray"); plt.axis("off"); plt.title(f"Canny edges ({device})"); plt.show()
- מה ההבדלים בין GPU ל CPU – בזמנים
- שינוי קוד : הוסף פילטר EMBUSS והצג גם אותו כתמונה