中文亚洲精品无码_熟女乱子伦免费_人人超碰人人爱国产_亚洲熟妇女综合网

當(dāng)前位置: 首頁 > news >正文

做網(wǎng)站哪些公司好百度分析

做網(wǎng)站哪些公司好,百度分析,做證件的網(wǎng)站,愛站網(wǎng)是怎么回事引言 目標(biāo)檢測是計(jì)算機(jī)視覺中一個(gè)非常流行的任務(wù),在這個(gè)任務(wù)中,給定一個(gè)圖像,你預(yù)測圖像中物體的包圍盒(通常是矩形的) ,并且識別物體的類型。在這個(gè)圖像中可能有多個(gè)對象,而且現(xiàn)在有各種先進(jìn)的技術(shù)和框架來解決這個(gè)問…

引言

目標(biāo)檢測是計(jì)算機(jī)視覺中一個(gè)非常流行的任務(wù),在這個(gè)任務(wù)中,給定一個(gè)圖像,你預(yù)測圖像中物體的包圍盒(通常是矩形的) ,并且識別物體的類型。在這個(gè)圖像中可能有多個(gè)對象,而且現(xiàn)在有各種先進(jìn)的技術(shù)和框架來解決這個(gè)問題,例如 Faster-RCNN 和 YOLOv3。

本文討論將討論圖像中只有一個(gè)感興趣的對象的情況。這里的重點(diǎn)更多是關(guān)于如何讀取圖像及其邊界框、調(diào)整大小和正確執(zhí)行增強(qiáng),而不是模型本身。目標(biāo)是很好地掌握對象檢測背后的基本思想,你可以對其進(jìn)行擴(kuò)展以更好地理解更復(fù)雜的技術(shù)。

本文中的所有代碼都在下面的鏈接中:https://jovian.ai/aakanksha-ns/road-signs-bounding-box-prediction

問題陳述

給定一個(gè)由路標(biāo)組成的圖像,預(yù)測路標(biāo)周圍的包圍盒,并識別路標(biāo)的類型。這些路標(biāo)包括以下四種:

·?紅綠燈

·?停止

·?車速限制

·?人行橫道

這就是所謂的多任務(wù)學(xué)習(xí)問題,因?yàn)樗婕皥?zhí)行兩個(gè)任務(wù): 1)回歸找到包圍盒坐標(biāo),2)分類識別道路標(biāo)志的類型

圖片

數(shù)據(jù)集

我使用了來自 Kaggle 的道路標(biāo)志檢測數(shù)據(jù)集,鏈接如下:https://www.kaggle.com/andrewmvd/road-sign-detection

它由877張圖像組成。這是一個(gè)相當(dāng)不平衡的數(shù)據(jù)集,大多數(shù)圖像屬于限速類,但由于我們更關(guān)注邊界框預(yù)測,因此可以忽略不平衡。

加載數(shù)據(jù)

每個(gè)圖像的注釋都存儲(chǔ)在單獨(dú)的 XML 文件中。我按照以下步驟創(chuàng)建了訓(xùn)練數(shù)據(jù)集:

·?遍歷訓(xùn)練目錄以獲得所有.xml 文件的列表。

·?使用xml.etree.ElementTree解析.xml文件。

·?創(chuàng)建一個(gè)由文件路徑、寬度、高度、邊界框坐標(biāo)( xmin 、 xmax 、 ymin 、? ? ? ? ymax )和每個(gè)圖像的類組成的字典,并將字典附加到列表中。

·?使用圖像統(tǒng)計(jì)數(shù)據(jù)字典列表創(chuàng)建一個(gè) Pandas 數(shù)據(jù)庫。

def filelist(root, file_type):"""Returns a fully-qualified list of filenames under root directory"""return [os.path.join(directory_path, f) for directory_path, directory_name, files in os.walk(root) for f in files if f.endswith(file_type)]def generate_train_df (anno_path):annotations = filelist(anno_path, '.xml')anno_list = []for anno_path in annotations:root = ET.parse(anno_path).getroot()anno = {}anno['filename'] = Path(str(images_path) + '/'+ root.find("./filename").text)anno['width'] = root.find("./size/width").textanno['height'] = root.find("./size/height").textanno['class'] = root.find("./object/name").textanno['xmin'] = int(root.find("./object/bndbox/xmin").text)anno['ymin'] = int(root.find("./object/bndbox/ymin").text)anno['xmax'] = int(root.find("./object/bndbox/xmax").text)anno['ymax'] = int(root.find("./object/bndbox/ymax").text)anno_list.append(anno)return pd.DataFrame(anno_list)

·?標(biāo)簽編碼類列

#label encode target
class_dict = {'speedlimit': 0, 'stop': 1, 'crosswalk': 2, 'trafficlight': 3}
df_train['class'] = df_train['class'].apply(lambda x:  class_dict[x])

調(diào)整圖像和邊界框的大小

由于訓(xùn)練一個(gè)計(jì)算機(jī)視覺模型需要的圖像是相同的大小,我們需要調(diào)整我們的圖像和他們相應(yīng)的包圍盒。調(diào)整圖像的大小很簡單,但是調(diào)整包圍盒的大小有點(diǎn)棘手,因?yàn)槊總€(gè)包圍盒都與圖像及其尺寸相關(guān)。

下面是調(diào)整包圍盒大小的工作原理:

·?將邊界框轉(zhuǎn)換為與其對應(yīng)的圖像大小相同的圖像(稱為掩碼)。這個(gè)掩碼只有? ? ? ? 0 表示背景,1 表示邊界框覆蓋的區(qū)域。

圖片

圖片

·?將掩碼調(diào)整到所需的尺寸。

·?從調(diào)整完大小的掩碼中提取邊界框坐標(biāo)。

def create_mask(bb, x):"""Creates a mask for the bounding box of same shape as image"""rows,cols,*_ = x.shapeY = np.zeros((rows, cols))bb = bb.astype(np.int)Y[bb[0]:bb[2], bb[1]:bb[3]] = 1.return Ydef mask_to_bb(Y):"""Convert mask Y to a bounding box, assumes 0 as background nonzero object"""cols, rows = np.nonzero(Y)if len(cols)==0: return np.zeros(4, dtype=np.float32)top_row = np.min(rows)left_col = np.min(cols)bottom_row = np.max(rows)right_col = np.max(cols)return np.array([left_col, top_row, right_col, bottom_row], dtype=np.float32)def create_bb_array(x):"""Generates bounding box array from a train_df row"""return np.array([x[5],x[4],x[7],x[6]])def resize_image_bb(read_path,write_path,bb,sz):"""Resize an image and its bounding box and write image to new path"""im = read_image(read_path)im_resized = cv2.resize(im, (int(1.49*sz), sz))Y_resized = cv2.resize(create_mask(bb, im), (int(1.49*sz), sz))new_path = str(write_path/read_path.parts[-1])cv2.imwrite(new_path, cv2.cvtColor(im_resized, cv2.COLOR_RGB2BGR))return new_path, mask_to_bb(Y_resized)#Populating Training DF with new paths and bounding boxes
new_paths = []
new_bbs = []
train_path_resized = Path('./road_signs/images_resized')
for index, row in df_train.iterrows():new_path,new_bb = resize_image_bb(row['filename'], train_path_resized, create_bb_array(row.values),300)new_paths.append(new_path)new_bbs.append(new_bb)
df_train['new_path'] = new_paths
df_train['new_bb'] = new_bbs

數(shù)據(jù)增強(qiáng)

數(shù)據(jù)增強(qiáng)是一種通過使用現(xiàn)有圖像的不同變體創(chuàng)建新的訓(xùn)練圖像來更好地概括我們的模型的技術(shù)。我們當(dāng)前的訓(xùn)練集中只有 800 張圖像,因此數(shù)據(jù)增強(qiáng)對于確保我們的模型不會(huì)過擬合非常重要。

對于這個(gè)問題,我使用了翻轉(zhuǎn)、旋轉(zhuǎn)、中心裁剪和隨機(jī)裁剪。

這里唯一需要記住的是確保包圍盒也以與圖像相同的方式進(jìn)行轉(zhuǎn)換。

# modified from fast.ai
def crop(im, r, c, target_r, target_c): return im[r:r+target_r, c:c+target_c]# random crop to the original size
def random_crop(x, r_pix=8):""" Returns a random crop"""r, c,*_ = x.shapec_pix = round(r_pix*c/r)rand_r = random.uniform(0, 1)rand_c = random.uniform(0, 1)start_r = np.floor(2*rand_r*r_pix).astype(int)start_c = np.floor(2*rand_c*c_pix).astype(int)return crop(x, start_r, start_c, r-2*r_pix, c-2*c_pix)def center_crop(x, r_pix=8):r, c,*_ = x.shapec_pix = round(r_pix*c/r)return crop(x, r_pix, c_pix, r-2*r_pix, c-2*c_pix)def rotate_cv(im, deg, y=False, mode=cv2.BORDER_REFLECT, interpolation=cv2.INTER_AREA):""" Rotates an image by deg degrees"""r,c,*_ = im.shapeM = cv2.getRotationMatrix2D((c/2,r/2),deg,1)if y:return cv2.warpAffine(im, M,(c,r), borderMode=cv2.BORDER_CONSTANT)return cv2.warpAffine(im,M,(c,r), borderMode=mode, flags=cv2.WARP_FILL_OUTLIERS+interpolation)def random_cropXY(x, Y, r_pix=8):""" Returns a random crop"""r, c,*_ = x.shapec_pix = round(r_pix*c/r)rand_r = random.uniform(0, 1)rand_c = random.uniform(0, 1)start_r = np.floor(2*rand_r*r_pix).astype(int)start_c = np.floor(2*rand_c*c_pix).astype(int)xx = crop(x, start_r, start_c, r-2*r_pix, c-2*c_pix)YY = crop(Y, start_r, start_c, r-2*r_pix, c-2*c_pix)return xx, YYdef transformsXY(path, bb, transforms):x = cv2.imread(str(path)).astype(np.float32)x = cv2.cvtColor(x, cv2.COLOR_BGR2RGB)/255Y = create_mask(bb, x)if transforms:rdeg = (np.random.random()-.50)*20x = rotate_cv(x, rdeg)Y = rotate_cv(Y, rdeg, y=True)if np.random.random() > 0.5: x = np.fliplr(x).copy()Y = np.fliplr(Y).copy()x, Y = random_cropXY(x, Y)else:x, Y = center_crop(x), center_crop(Y)return x, mask_to_bb(Y)def create_corner_rect(bb, color='red'):bb = np.array(bb, dtype=np.float32)return plt.Rectangle((bb[1], bb[0]), bb[3]-bb[1], bb[2]-bb[0], color=color,fill=False, lw=3)def show_corner_bb(im, bb):plt.imshow(im)plt.gca().add_patch(create_corner_rect(bb))

圖片

PyTorch 數(shù)據(jù)集

現(xiàn)在我們已經(jīng)有了數(shù)據(jù)增強(qiáng),我們可以進(jìn)行訓(xùn)練驗(yàn)證拆分并創(chuàng)建我們的 PyTorch 數(shù)據(jù)集。我們使用 ImageNet 統(tǒng)計(jì)數(shù)據(jù)對圖像進(jìn)行標(biāo)準(zhǔn)化,因?yàn)槲覀兪褂玫氖穷A(yù)訓(xùn)練的 ResNet 模型并在訓(xùn)練時(shí)在我們的數(shù)據(jù)集中應(yīng)用數(shù)據(jù)增強(qiáng)。

X_train, X_val, y_train, y_val = train_test_split(X, Y, test_size=0.2, random_state=42)def normalize(im):"""Normalizes images with Imagenet stats."""imagenet_stats = np.array([[0.485, 0.456, 0.406], [0.229, 0.224, 0.225]])return (im - imagenet_stats[0])/imagenet_stats[1]
class RoadDataset(Dataset):def __init__(self, paths, bb, y, transforms=False):self.transforms = transformsself.paths = paths.valuesself.bb = bb.valuesself.y = y.valuesdef __len__(self):return len(self.paths)def __getitem__(self, idx):path = self.paths[idx]y_class = self.y[idx]x, y_bb = transformsXY(path, self.bb[idx], self.transforms)x = normalize(x)x = np.rollaxis(x, 2)return x, y_class, y_bb
train_ds = RoadDataset(X_train['new_path'],X_train['new_bb'] ,y_train, transforms=True)
valid_ds = RoadDataset(X_val['new_path'],X_val['new_bb'],y_val)
batch_size = 64
train_dl = DataLoader(train_ds, batch_size=batch_size, shuffle=True)
valid_dl = DataLoader(valid_ds, batch_size=batch_size)

PyTorch 模型

對于這個(gè)模型,我使用了一個(gè)非常簡單的預(yù)先訓(xùn)練的 resNet-34模型。由于我們有兩個(gè)任務(wù)要完成,這里有兩個(gè)最后的層:?包圍盒回歸器和圖像分類器。

class BB_model(nn.Module):def __init__(self):super(BB_model, self).__init__()resnet = models.resnet34(pretrained=True)layers = list(resnet.children())[:8]self.features1 = nn.Sequential(*layers[:6])self.features2 = nn.Sequential(*layers[6:])self.classifier = nn.Sequential(nn.BatchNorm1d(512), nn.Linear(512, 4))self.bb = nn.Sequential(nn.BatchNorm1d(512), nn.Linear(512, 4))def forward(self, x):x = self.features1(x)x = self.features2(x)x = F.relu(x)x = nn.AdaptiveAvgPool2d((1,1))(x)x = x.view(x.shape[0], -1)return self.classifier(x), self.bb(x)

訓(xùn)練

對于損失,我們需要同時(shí)考慮分類損失和邊界框回歸損失,因此我們使用交叉熵和 L1 損失(真實(shí)值和預(yù)測坐標(biāo)之間的所有絕對差之和)的組合。我已經(jīng)將 L1 損失縮放了 1000 倍,因?yàn)榉诸惡突貧w損失都在相似的范圍內(nèi)。除此之外,它是一個(gè)標(biāo)準(zhǔn)的 PyTorch 訓(xùn)練循環(huán)(使用 GPU):

def update_optimizer(optimizer, lr):for i, param_group in enumerate(optimizer.param_groups):param_group["lr"] = lrdef train_epocs(model, optimizer, train_dl, val_dl, epochs=10,C=1000):idx = 0for i in range(epochs):model.train()total = 0sum_loss = 0for x, y_class, y_bb in train_dl:batch = y_class.shape[0]x = x.cuda().float()y_class = y_class.cuda()y_bb = y_bb.cuda().float()out_class, out_bb = model(x)loss_class = F.cross_entropy(out_class, y_class, reduction="sum")loss_bb = F.l1_loss(out_bb, y_bb, reduction="none").sum(1)loss_bb = loss_bb.sum()loss = loss_class + loss_bb/Coptimizer.zero_grad()loss.backward()optimizer.step()idx += 1total += batchsum_loss += loss.item()train_loss = sum_loss/totalval_loss, val_acc = val_metrics(model, valid_dl, C)print("train_loss %.3f val_loss %.3f val_acc %.3f" % (train_loss, val_loss, val_acc))return sum_loss/totaldef val_metrics(model, valid_dl, C=1000):model.eval()total = 0sum_loss = 0correct = 0 for x, y_class, y_bb in valid_dl:batch = y_class.shape[0]x = x.cuda().float()y_class = y_class.cuda()y_bb = y_bb.cuda().float()out_class, out_bb = model(x)loss_class = F.cross_entropy(out_class, y_class, reduction="sum")loss_bb = F.l1_loss(out_bb, y_bb, reduction="none").sum(1)loss_bb = loss_bb.sum()loss = loss_class + loss_bb/C_, pred = torch.max(out_class, 1)correct += pred.eq(y_class).sum().item()sum_loss += loss.item()total += batchreturn sum_loss/total, correct/totalmodel = BB_model().cuda()
parameters = filter(lambda p: p.requires_grad, model.parameters())
optimizer = torch.optim.Adam(parameters, lr=0.006)train_epocs(model, optimizer, train_dl, valid_dl, epochs=15)

測試

現(xiàn)在我們已經(jīng)完成了訓(xùn)練,我們可以選擇一個(gè)隨機(jī)圖像并在上面測試我們的模型。盡管我們只有相當(dāng)少量的訓(xùn)練圖像,但是我們最終在測試圖像上得到了一個(gè)相當(dāng)不錯(cuò)的預(yù)測。

使用手機(jī)拍攝真實(shí)照片并測試模型將是一項(xiàng)有趣的練習(xí)。另一個(gè)有趣的實(shí)驗(yàn)是不執(zhí)行任何數(shù)據(jù)增強(qiáng)并訓(xùn)練模型并比較兩個(gè)模型。


# resizing test image
im = read_image('./road_signs/images_resized/road789.png')
im = cv2.resize(im, (int(1.49*300), 300))
cv2.imwrite('./road_signs/road_signs_test/road789.jpg', cv2.cvtColor(im, cv2.COLOR_RGB2BGR))# test Dataset
test_ds = RoadDataset(pd.DataFrame([{'path':'./road_signs/road_signs_test/road789.jpg'}])['path'],pd.DataFrame([{'bb':np.array([0,0,0,0])}])['bb'],pd.DataFrame([{'y':[0]}])['y'])
x, y_class, y_bb = test_ds[0]xx = torch.FloatTensor(x[None,])
xx.shape# prediction
out_class, out_bb = model(xx.cuda())
out_class, out_bb

圖片

總結(jié)

現(xiàn)在我們已經(jīng)介紹了目標(biāo)檢測的基本原理,并從頭開始實(shí)現(xiàn)它,您可以將這些想法擴(kuò)展到多對象情況,并嘗試更復(fù)雜的模型,如 RCNN 和 YOLO!

http://www.risenshineclean.com/news/9585.html

相關(guān)文章:

  • 常德網(wǎng)站建設(shè)案例教程360優(yōu)化大師app
  • 自己做網(wǎng)站 為什么出現(xiàn)403營銷渠道名詞解釋
  • 使用他人注冊商標(biāo)做網(wǎng)站湖南網(wǎng)站托管
  • 醫(yī)療器械做網(wǎng)站到哪里先備案網(wǎng)絡(luò)營銷方法
  • 黔東網(wǎng)站建設(shè)什么網(wǎng)站都能打開的瀏覽器
  • 聊天網(wǎng)站制作教程武漢seo廣告推廣
  • 淮北網(wǎng)站建設(shè)制作衡陽網(wǎng)站優(yōu)化公司
  • 旅游網(wǎng)站模板源碼媒體發(fā)稿網(wǎng)
  • 最新中國b2b網(wǎng)站排名南寧seo做法哪家好
  • 獨(dú)立網(wǎng)站怎么做seo當(dāng)陽seo外包
  • php 開源企業(yè)網(wǎng)站百度網(wǎng)盤網(wǎng)頁版入口
  • 懷化 網(wǎng)站建設(shè)2345網(wǎng)址導(dǎo)航設(shè)為主頁
  • 合肥萬戶網(wǎng)絡(luò)科技有限公司搜索引擎優(yōu)化的主要工作
  • 上虞網(wǎng)站設(shè)計(jì)城關(guān)網(wǎng)站seo
  • 沈陽做網(wǎng)站公司重慶seo排名
  • 國內(nèi)課程網(wǎng)站建設(shè)現(xiàn)狀永久免費(fèi)低代碼開發(fā)平臺
  • 新華社兩學(xué)一做網(wǎng)站seo整合營銷
  • 編輯網(wǎng)站內(nèi)容有沒有批量辦法什么是seo關(guān)鍵詞
  • 網(wǎng)站程序和空間區(qū)別網(wǎng)絡(luò)推廣優(yōu)化網(wǎng)站
  • 寧波網(wǎng)站建設(shè)推廣公司青島百度快速排名優(yōu)化
  • 零食網(wǎng)站頁面模板湖南專業(yè)seo公司
  • 房產(chǎn)信息網(wǎng)站模板電商平臺怎么加入
  • 音樂網(wǎng)站開發(fā)參考文獻(xiàn)百度指數(shù)代表什么意思
  • 雞西公司做網(wǎng)站關(guān)鍵詞分為哪幾類
  • 哪有專做注冊小網(wǎng)站的客戶資源買賣平臺
  • wordpress懸浮bar深圳seo網(wǎng)站優(yōu)化公司
  • 企業(yè)網(wǎng)站怎么做推廣比較好網(wǎng)站模板建站公司
  • 二手車網(wǎng)站建站海豹直播nba
  • 做蛋糕有哪些網(wǎng)站域名查詢網(wǎng)
  • 網(wǎng)站360做的標(biāo)記如何取消市場營銷策劃方案3000字