第99步 深度學習圖像目標檢測:SSDlite建模

基于WIN10的64位系統演示

一、寫在前面

本期,我們繼續學習深度學習圖像目標檢測系列,SSD(Single Shot MultiBox Detector)模型的后續版本,SSDlite模型。

二、SSDlite簡介

SSDLite 是 SSD 模型的一個變種,旨在為移動設備和邊緣計算設備提供更高效的目標檢測。SSDLite 的主要特點是使用了輕量級的骨干網絡和特定的卷積操作來減少計算復雜性,從而提高檢測速度,同時在大多數情況下仍保持了較高的準確性。

以下是 SSDLite 的主要特性和組件:

(1)輕量級骨干:

SSDLite 不使用 VGG 或 ResNet 這樣的重量級骨干。相反,它使用 MobileNet 作為骨干,特別是 MobileNetV2 或 MobileNetV3。這些網絡使用深度可分離的卷積和其他輕量級操作來減少計算成本。

(2)深度可分離的卷積:

這是 MobileNet 的核心組件,也被用于 SSDLite。深度可分離的卷積將傳統的卷積操作分解為兩個較小的操作:一個深度卷積和一個點卷積,這大大減少了計算和參數數量。

(3)多尺度特征映射:

與原始的 SSD 相似,SSDLite 也從不同的層級提取特征圖以檢測不同大小的物體。

(4)默認框:

SSDLite 也使用默認框(或稱為錨框)來進行邊界框預測。

(5)單階段檢測:

與 SSD 相同,SSDLite 也是一個單階段檢測器,同時進行邊界框回歸和分類。

(6)損失函數:

SSDLite 使用與 SSD 相同的組合損失,包括平滑 L1 損失和交叉熵損失。

綜上,SSDLite 是為了速度和效率而設計的,特別是針對計算和內存資源有限的設備。通過使用輕量級的骨干和深度可分離的卷積,它能夠在減少計算負擔的同時,仍然保持合理的檢測準確性。

三、數據源

來源于公共數據,文件設置如下:

大概的任務就是:用一個框框標記出MTB的位置。

四、SSDlite實戰

直接上代碼:

import os
import random
import torch
import torchvision
from torchvision.models.detection import ssdlite320_mobilenet_v3_large
from torchvision.transforms import functional as F
from PIL import Image
from torch.utils.data import DataLoader
import xml.etree.ElementTree as ET
import matplotlib.pyplot as plt
from torchvision import transforms
import albumentations as A
from albumentations.pytorch import ToTensorV2
import numpy as np# Function to parse XML annotations
def parse_xml(xml_path):tree = ET.parse(xml_path)root = tree.getroot()boxes = []for obj in root.findall("object"):bndbox = obj.find("bndbox")xmin = int(bndbox.find("xmin").text)ymin = int(bndbox.find("ymin").text)xmax = int(bndbox.find("xmax").text)ymax = int(bndbox.find("ymax").text)# Check if the bounding box is validif xmin < xmax and ymin < ymax:boxes.append((xmin, ymin, xmax, ymax))else:print(f"Warning: Ignored invalid box in {xml_path} - ({xmin}, {ymin}, {xmax}, {ymax})")return boxes# Function to split data into training and validation sets
def split_data(image_dir, split_ratio=0.8):all_images = [f for f in os.listdir(image_dir) if f.endswith(".jpg")]random.shuffle(all_images)split_idx = int(len(all_images) * split_ratio)train_images = all_images[:split_idx]val_images = all_images[split_idx:]return train_images, val_images# Dataset class for the Tuberculosis dataset
class TuberculosisDataset(torch.utils.data.Dataset):def __init__(self, image_dir, annotation_dir, image_list, transform=None):self.image_dir = image_dirself.annotation_dir = annotation_dirself.image_list = image_listself.transform = transformdef __len__(self):return len(self.image_list)def __getitem__(self, idx):image_path = os.path.join(self.image_dir, self.image_list[idx])image = Image.open(image_path).convert("RGB")xml_path = os.path.join(self.annotation_dir, self.image_list[idx].replace(".jpg", ".xml"))boxes = parse_xml(xml_path)# Check for empty bounding boxes and return Noneif len(boxes) == 0:return Noneboxes = torch.as_tensor(boxes, dtype=torch.float32)labels = torch.ones((len(boxes),), dtype=torch.int64)iscrowd = torch.zeros((len(boxes),), dtype=torch.int64)target = {}target["boxes"] = boxestarget["labels"] = labelstarget["image_id"] = torch.tensor([idx])target["iscrowd"] = iscrowd# Apply transformationsif self.transform:image = self.transform(image)return image, target# Define the transformations using torchvision
data_transform = torchvision.transforms.Compose([torchvision.transforms.ToTensor(),  # Convert PIL image to tensortorchvision.transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])  # Normalize the images
])# Adjusting the DataLoader collate function to handle None values
def collate_fn(batch):batch = list(filter(lambda x: x is not None, batch))return tuple(zip(*batch))def get_ssdlite_model_for_finetuning(num_classes):# Load an SSDlite model with a MobileNetV3 Large backbone without pre-trained weightsmodel = ssdlite320_mobilenet_v3_large(pretrained=False, num_classes=num_classes)return model# Function to save the model
def save_model(model, path="SSDlite_mtb.pth", save_full_model=False):if save_full_model:torch.save(model, path)else:torch.save(model.state_dict(), path)print(f"Model saved to {path}")# Function to compute Intersection over Union
def compute_iou(boxA, boxB):xA = max(boxA[0], boxB[0])yA = max(boxA[1], boxB[1])xB = min(boxA[2], boxB[2])yB = min(boxA[3], boxB[3])interArea = max(0, xB - xA + 1) * max(0, yB - yA + 1)boxAArea = (boxA[2] - boxA[0] + 1) * (boxA[3] - boxA[1] + 1)boxBArea = (boxB[2] - boxB[0] + 1) * (boxB[3] - boxB[1] + 1)iou = interArea / float(boxAArea + boxBArea - interArea)return iou# Adjusting the DataLoader collate function to handle None values and entirely empty batches
def collate_fn(batch):batch = list(filter(lambda x: x is not None, batch))if len(batch) == 0:# Return placeholder batch if entirely emptyreturn [torch.zeros(1, 3, 224, 224)], [{}]return tuple(zip(*batch))#Training function with modifications for collecting IoU and loss
def train_model(model, train_loader, optimizer, device, num_epochs=10):model.train()model.to(device)loss_values = []iou_values = []for epoch in range(num_epochs):epoch_loss = 0.0total_ious = 0num_boxes = 0for images, targets in train_loader:# Skip batches with placeholder dataif len(targets) == 1 and not targets[0]:continue# Skip batches with empty targetsif any(len(target["boxes"]) == 0 for target in targets):continueimages = [image.to(device) for image in images]targets = [{k: v.to(device) for k, v in t.items()} for t in targets]loss_dict = model(images, targets)losses = sum(loss for loss in loss_dict.values())optimizer.zero_grad()losses.backward()optimizer.step()epoch_loss += losses.item()# Compute IoU for evaluationwith torch.no_grad():model.eval()predictions = model(images)for i, prediction in enumerate(predictions):pred_boxes = prediction["boxes"].cpu().numpy()true_boxes = targets[i]["boxes"].cpu().numpy()for pred_box in pred_boxes:for true_box in true_boxes:iou = compute_iou(pred_box, true_box)total_ious += iounum_boxes += 1model.train()avg_loss = epoch_loss / len(train_loader)avg_iou = total_ious / num_boxes if num_boxes != 0 else 0loss_values.append(avg_loss)iou_values.append(avg_iou)print(f"Epoch {epoch+1}/{num_epochs} Loss: {avg_loss} Avg IoU: {avg_iou}")# Plotting loss and IoU valuesplt.figure(figsize=(12, 5))plt.subplot(1, 2, 1)plt.plot(loss_values, label="Training Loss")plt.title("Training Loss across Epochs")plt.xlabel("Epochs")plt.ylabel("Loss")plt.subplot(1, 2, 2)plt.plot(iou_values, label="IoU")plt.title("IoU across Epochs")plt.xlabel("Epochs")plt.ylabel("IoU")plt.show()# Save model after trainingsave_model(model)# Validation function
def validate_model(model, val_loader, device):model.eval()model.to(device)with torch.no_grad():for images, targets in val_loader:images = [image.to(device) for image in images]targets = [{k: v.to(device) for k, v in t.items()} for t in targets]model(images)# Paths to your data
image_dir = "tuberculosis-phonecamera"
annotation_dir = "tuberculosis-phonecamera"# Split data
train_images, val_images = split_data(image_dir)# Create datasets and dataloaders
train_dataset = TuberculosisDataset(image_dir, annotation_dir, train_images, transform=data_transform)
val_dataset = TuberculosisDataset(image_dir, annotation_dir, val_images, transform=data_transform)# Updated DataLoader with new collate function
train_loader = DataLoader(train_dataset, batch_size=4, shuffle=True, collate_fn=collate_fn)
val_loader = DataLoader(val_dataset, batch_size=4, shuffle=False, collate_fn=collate_fn)# Model and optimizer
model = get_ssdlite_model_for_finetuning(2)
optimizer = torch.optim.Adam(model.parameters(), lr=0.001)# Train and validate
train_model(model, train_loader, optimizer, device="cuda", num_epochs=10)
validate_model(model, val_loader, device="cuda")

需要從頭訓練的,就不跑了,擺爛了。

五、寫在后面

目標檢測模型門檻更高了,運行起來對硬件要求也很高,時間也很久,都是小時起步的。因此只是簡單介紹,算是入個門了。

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/166191.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/166191.shtml
英文地址,請注明出處:http://en.pswp.cn/news/166191.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

竹云參編《公共數據授權運營平臺技術要求》團體標準正式發布

2023年11月23日&#xff0c;第二屆全球數字貿易博覽會“數據要素治理與市場化論壇”于杭州成功召開&#xff0c;國家數據局黨組書記、局長劉烈宏&#xff0c;浙江省委常委、常務副省長徐文光出席會議并致辭。會上&#xff0c;國家工業信息安全發展研究中心發布并解讀了我國首部…

[Linux] 馮諾依曼體系結構 與 操作系統

文章目錄 1、馮諾依曼體系結構2、操作系統 1、馮諾依曼體系結構 馮諾依曼結構也稱普林斯頓結構&#xff0c;是一種將程序指令存儲器和數據存儲器合并在一起的存儲器結構。程序指令存儲地址和數據存儲地址指向同一個存儲器的不同物理位置&#xff0c;因此程序指令和數據的寬度相…

【鴻蒙應用ArkTS開發系列】- 云開發入門實戰二 實現省市地區三級聯動地址選擇器組件(下)

文章目錄 概述端云調用流程端側集成AGC SDK端側省市地區聯動的地址選擇器組件開發創建省市數據模型創建省市地區視圖UI子組件創建頁面UI視圖Page文件 打包測試總結 概述 我們在前面的課程&#xff0c;對云開發的入門做了介紹&#xff0c;以及使用一個省市地區聯動的地址選擇器…

三次輸錯密碼后,系統是怎么做到不讓我繼續嘗試的?

1故事背景 忘記密碼這件事&#xff0c;相信絕大多數人都遇到過&#xff0c;輸一次錯一次&#xff0c;錯到幾次以上&#xff0c;就不允許你繼續嘗試了。 但當你嘗試重置密碼&#xff0c;又發現新密碼不能和原密碼重復&#xff1a; 圖片 相信此刻心情只能用一張圖形容&#xf…

Mobaxterm 使用lrzsz傳輸文件(rz/sz)

Mobaxterm 使用lrzsz傳輸文件報錯 1. 現象 最近從xshell切換到Mobaxterm其他一切正常,就是使用rz傳輸文件時會出現錯誤,比較苦惱. 會出現以下錯誤 [rootcentos7 rpmbuild]# rz ?CCCCCCCCCCC23be50ive.**B0100000023be502. 解決方法 去官網(https://mobaxterm.mobatek.net…

2021年03月 Scratch(三級)真題解析#中國電子學會#全國青少年軟件編程等級考試

Scratch等級考試(1~4級)全部真題?點這里 一、單選題(共25題,每題2分,共50分) 第1題 在《采礦》游戲中,當角色撿到黃金時財富值加1分,撿到鉆石時財富值加2分,下面哪個程序實現這個功能? A: B: C: D: 答案:D A將變量值固定,BC為雙重判斷

練習七-在Verilog中使用任務task

在Verilog中使用任務task 1&#xff0c;任務目的2&#xff0c;RTL代碼&#xff0c;交換3&#xff0c;測試代碼4&#xff0c;波形顯示 1&#xff0c;任務目的 &#xff08;1&#xff09;掌握任務在verilog模塊設計中的應用&#xff1b; &#xff08;2&#xff09;學會在電平敏感…

Android Studio記錄一個錯誤:Execution failed for task ‘:app:lintVitalRelease‘.

Android出現Execution failed for task :app:lintVitalRelease.> Lint found fatal errors while assembling a release target. Execution failed for task :app:lintVitalRelease解決方法 Execution failed for task ‘:app:lintVitalRelease’ build project 可以正常執…

〖大前端 - 基礎入門三大核心之JS篇?〗- DOM事件對象及它的屬性

說明&#xff1a;該文屬于 大前端全棧架構白寶書專欄&#xff0c;目前階段免費&#xff0c;如需要項目實戰或者是體系化資源&#xff0c;文末名片加V&#xff01;作者&#xff1a;不渴望力量的哈士奇(哈哥)&#xff0c;十余年工作經驗, 從事過全棧研發、產品經理等工作&#xf…

進程已結束,退出代碼-1073741571 (0xC00000FD)

今天遇到了一個很邪門的問題&#xff0c;沒有報錯&#xff0c;只是提示“進程已結束,退出代碼-1073741571 (0xC00000FD)”。后來查資料說是棧溢出。 出問題的應該是上面這段代碼。 這里我想把一個128*128的矩陣進行剪枝操作。 傳入的128*128的矩陣太大了&#xff0c;兩組for循…

介紹GLFW庫和OpenGL和GLEW庫三者之間的關系

具體來說&#xff0c;OpenGL是一個開放的圖形庫&#xff0c;它規定了每個函數應該如何執行&#xff0c;以及它們的輸出值&#xff0c;但沒有具體實現。它提供了渲染2D和3D圖形的標準或規范。 GLEW&#xff0c;全稱OpenGL Extension Wrangler Library&#xff0c;是一個用于管理…

【Flink】狀態管理

目錄 1、狀態概述 1.1 無狀態算子 1.2 有狀態算子 2、狀態分類 ?編輯 2.1 算子狀態 2.1.1 列表狀態&#xff08;ListState&#xff09; 2.1.2 聯合列表狀態&#xff08;UnionListState&#xff09; 2.1.3 廣播狀態&#xff08;BroadcastState&#xff09; 2.2 按鍵分…

Redis Transaction事務

Redis 事務的目的是方便用戶一次執行多個命令。執行 Redis 事務可分為三個階段&#xff1a; 開始事務命令入隊執行事務 Redis事務特性 Redis 事務具有兩個重要特性&#xff1a; 1) 單獨的隔離操作 事務中的所有命令都會被序列化&#xff0c;它們將按照順序執行&#xff0c…

圖像標記上線,描點信息盡在掌握丨三疊云

圖像標記 路徑 表單設計 >> 組件 >> 增強組件 功能簡介 「圖像標記」字段是「增強字段」類型字段。用戶通過上傳圖片的方式構建一個背景圖片&#xff0c;并在構建的圖片背景上添加描點信息。搭配「儀表盤」中的「圖像軌跡」&#xff0c;可繪制出相應的數據軌跡…

界面組件DevExpress Reporting v23.1 - Web報表設計器功能升級

DevExpress Reporting是.NET Framework下功能完善的報表平臺&#xff0c;它附帶了易于使用的Visual Studio報表設計器和豐富的報表控件集&#xff0c;包括數據透視表、圖表&#xff0c;因此您可以構建無與倫比、信息清晰的報表 界面組件DevExpress Reporting v23.1已經發布一段…

基于JavaWeb+SSM+Vue微信閱讀小程序的設計和實現

基于JavaWebSSMVue微信閱讀小程序的設計和實現 源碼獲取入口Lun文目錄前言主要技術系統設計功能截圖訂閱經典源碼專欄[Java 源碼獲取 源碼獲取入口 Lun文目錄 第1章 緒論 1 1.1 課題背景 1 1.2 課題意義 1 1.3 研究內容 1 第2章 開發環境與技術 3 2.1 MYSQL數據庫 3 2.2 JSP技…

2016年8月15日 Go生態洞察:Go 1.7版本發布

&#x1f337;&#x1f341; 博主貓頭虎&#xff08;&#x1f405;&#x1f43e;&#xff09;帶您 Go to New World?&#x1f341; &#x1f984; 博客首頁——&#x1f405;&#x1f43e;貓頭虎的博客&#x1f390; &#x1f433; 《面試題大全專欄》 &#x1f995; 文章圖文…

解決traefik/nginx-ingress-controller配置正確的情況訪問域名仍然報錯: Connection Refused的問題

最近碰到一個很奇怪的問題&#xff1a; traefik/nginx-ingress-controller配置正確&#xff0c;但是訪問ingress配置的host域名就是死活報錯&#xff1a; Connection Refused 這樣怎么也找不到原因&#xff0c;然后一咬牙直接在其中一臺節點yum安裝nginx, 通過直接反向代理的方…

微信小程序開發資源匯總

本文收集了微信小程序開發過程中會使用到的資料、問題以及第三方組件庫。本文不是一篇關于如何學習微信小程序的入門指南&#xff0c;也非參考手冊&#xff0c;只是一些資料的整理。 本倉庫中的資料整理自網絡&#xff0c;也有一些來自網友的推薦。 官方文檔 小程序設計指南…

UE5 UI教程學習筆記

參考資料&#xff1a;https://item.taobao.com/item.htm?spma21n57.1.0.0.2b4f523cAV5i43&id716635137219&ns1&abbucket15#detail 基礎工程&#xff1a;https://download.csdn.net/download/qq_17523181/88559312 1. 介紹 工程素材 2. 創建Widget UE5 UI系統的…