校园春色亚洲色图_亚洲视频分类_中文字幕精品一区二区精品_麻豆一区区三区四区产品精品蜜桃

主頁(yè) > 知識(shí)庫(kù) > Pytorch 如何加速Dataloader提升數(shù)據(jù)讀取速度

Pytorch 如何加速Dataloader提升數(shù)據(jù)讀取速度

熱門標(biāo)簽:天津電話機(jī)器人公司 開封語(yǔ)音外呼系統(tǒng)代理商 應(yīng)電話機(jī)器人打電話違法嗎 400電話辦理哪種 開封自動(dòng)外呼系統(tǒng)怎么收費(fèi) 河北防封卡電銷卡 電銷機(jī)器人的風(fēng)險(xiǎn) 地圖標(biāo)注線上如何操作 手機(jī)網(wǎng)頁(yè)嵌入地圖標(biāo)注位置

在利用DL解決圖像問(wèn)題時(shí),影響訓(xùn)練效率最大的有時(shí)候是GPU,有時(shí)候也可能是CPU和你的磁盤。

很多設(shè)計(jì)不當(dāng)?shù)娜蝿?wù),在訓(xùn)練神經(jīng)網(wǎng)絡(luò)的時(shí)候,大部分時(shí)間都是在從磁盤中讀取數(shù)據(jù),而不是做 Backpropagation 。

這種癥狀的體現(xiàn)是使用 Nividia-smi 查看 GPU 使用率時(shí),Memory-Usage 占用率很高,但是 GPU-Util 時(shí)常為 0% ,如下圖所示:

如何解決這種問(wèn)題呢?

在 Nvidia 提出的分布式框架 Apex 里面,我們?cè)谠创a里面找到了一個(gè)簡(jiǎn)單的解決方案:

https://github.com/NVIDIA/apex/blob/f5cd5ae937f168c763985f627bbf850648ea5f3f/examples/imagenet/main_amp.py#L256 ​

class data_prefetcher():
    def __init__(self, loader):
        self.loader = iter(loader)
        self.stream = torch.cuda.Stream()
        self.mean = torch.tensor([0.485 * 255, 0.456 * 255, 0.406 * 255]).cuda().view(1,3,1,1)
        self.std = torch.tensor([0.229 * 255, 0.224 * 255, 0.225 * 255]).cuda().view(1,3,1,1)
        # With Amp, it isn't necessary to manually convert data to half.
        # if args.fp16:
        #     self.mean = self.mean.half()
        #     self.std = self.std.half()
        self.preload()

    def preload(self):
        try:
            self.next_input, self.next_target = next(self.loader)
        except StopIteration:
            self.next_input = None
            self.next_target = None
            return
        with torch.cuda.stream(self.stream):
            self.next_input = self.next_input.cuda(non_blocking=True)
            self.next_target = self.next_target.cuda(non_blocking=True)
            # With Amp, it isn't necessary to manually convert data to half.
            # if args.fp16:
            #     self.next_input = self.next_input.half()
            # else:
            self.next_input = self.next_input.float()
            self.next_input = self.next_input.sub_(self.mean).div_(self.std)

我們能看到 Nvidia 是在讀取每次數(shù)據(jù)返回給網(wǎng)絡(luò)的時(shí)候,預(yù)讀取下一次迭代需要的數(shù)據(jù),

那么對(duì)我們自己的訓(xùn)練代碼只需要做下面的改造:

training_data_loader = DataLoader(
    dataset=train_dataset,
    num_workers=opts.threads,
    batch_size=opts.batchSize,
    pin_memory=True,
    shuffle=True,
)
for iteration, batch in enumerate(training_data_loader, 1):
    # 訓(xùn)練代碼

#-------------升級(jí)后---------

data, label = prefetcher.next()
iteration = 0
while data is not None:
    iteration += 1
    # 訓(xùn)練代碼
    data, label = prefetcher.next()

這樣子我們的 Dataloader 就像打了雞血一樣提高了效率很多,如下圖:

當(dāng)然,最好的解決方案還是從硬件上,把讀取速度慢的機(jī)械硬盤換成 NVME 固態(tài)吧~

補(bǔ)充:Pytorch設(shè)置多線程進(jìn)行dataloader時(shí)影響GPU運(yùn)行

使用PyTorch設(shè)置多線程(threads)進(jìn)行數(shù)據(jù)讀取時(shí),其實(shí)是假的多線程,他是開了N個(gè)子進(jìn)程(PID是連續(xù)的)進(jìn)行模擬多線程工作。

以載入cocodataset為例

DataLoader

dataloader = torch.utils.data.DataLoader(COCODataset(config["train_path"],
                                                     (config["img_w"], config["img_h"]),
                                                     is_training=True),
                                         batch_size=config["batch_size"],
                                         shuffle=True, num_workers=32, pin_memory=True)

numworkers就是指定多少線程的參數(shù),原為32。

檢查GPU是否運(yùn)行該程序

查看運(yùn)行在gpu上的所有程序:

fuser -v /dev/nvidia*

如果沒有返回,則該程序并沒有在GPU上運(yùn)行

指定GPU運(yùn)行

將num_workers改成0即可

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

您可能感興趣的文章:
  • pytorch鎖死在dataloader(訓(xùn)練時(shí)卡死)
  • pytorch Dataset,DataLoader產(chǎn)生自定義的訓(xùn)練數(shù)據(jù)案例
  • 解決Pytorch dataloader時(shí)報(bào)錯(cuò)每個(gè)tensor維度不一樣的問(wèn)題
  • pytorch中DataLoader()過(guò)程中遇到的一些問(wèn)題
  • Pytorch dataloader在加載最后一個(gè)batch時(shí)卡死的解決
  • pytorch DataLoader的num_workers參數(shù)與設(shè)置大小詳解
  • pytorch 實(shí)現(xiàn)多個(gè)Dataloader同時(shí)訓(xùn)練

標(biāo)簽:蘭州 駐馬店 宿遷 江蘇 常州 成都 山東 六盤水

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Pytorch 如何加速Dataloader提升數(shù)據(jù)讀取速度》,本文關(guān)鍵詞  Pytorch,如何,加速,Dataloader,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《Pytorch 如何加速Dataloader提升數(shù)據(jù)讀取速度》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于Pytorch 如何加速Dataloader提升數(shù)據(jù)讀取速度的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    主站蜘蛛池模板: 抚顺县| 睢宁县| 松潘县| 杭锦后旗| 滨州市| 鄱阳县| 定州市| 泰顺县| 台山市| 河池市| 云安县| 基隆市| 荥经县| 枝江市| 海原县| 百色市| 高州市| 大方县| 十堰市| 兰考县| 罗山县| 乾安县| 辉南县| 施甸县| 合肥市| 乐至县| 长丰县| 莱州市| 大冶市| 闽侯县| 蒲城县| 阳原县| 诏安县| 沧州市| 泰州市| 哈尔滨市| 济源市| 于都县| 焦作市| 缙云县| 寿阳县|