99999久久久久久亚洲,欧美人与禽猛交狂配,高清日韩av在线影院,一个人在线高清免费观看,啦啦啦在线视频免费观看www

熱線電話:13121318867

登錄
首頁大數(shù)據(jù)時代tensorflow_datasets 如何load本地的數(shù)據(jù)集?
tensorflow_datasets 如何load本地的數(shù)據(jù)集?
2025-03-26
收藏

TensorFlow Datasets(TFDS)是一個用于下載、管理和預處理機器學習數(shù)據(jù)集的庫。它提供了易于使用的API,允許用戶從現(xiàn)有集合中選擇并下載各種數(shù)據(jù)集。然而,在一些情況下,用戶可能需要使用本地數(shù)據(jù)集進行模型訓練和測試。在本文中,我們將介紹如何使用TFDS加載本地數(shù)據(jù)集。

為了加載本地數(shù)據(jù)集,我們需要做以下幾個步驟:

1.準備數(shù)據(jù)集 2.創(chuàng)建TFDS數(shù)據(jù)集描述文件 3.使用描述文件加載數(shù)據(jù)集

準備數(shù)據(jù)集

首先,我們需要準備我們要使用的數(shù)據(jù)集。這通常涉及到收集、清洗和組織數(shù)據(jù),以便可以輕松地訪問數(shù)據(jù)。在本例中,我們將使用一個簡單的示例數(shù)據(jù)集,其中包含數(shù)字圖像和相應的標簽。

該數(shù)據(jù)集的目錄結構類似于以下內容:

data/
    0/
        image1.png
        image2.png
        ...
    1/
        image1.png
        image2.png
        ...
    ...

在上面的目錄結構中,每個數(shù)字目錄代表一個唯一的標簽,并包含與該標簽相關聯(lián)的所有圖像。

創(chuàng)建TFDS數(shù)據(jù)集描述文件

接下來,我們需要創(chuàng)建一個TFDS數(shù)據(jù)集描述文件。該文件告訴TFDS如何讀取和使用我們的本地數(shù)據(jù)集。描述文件通常是一個Python模塊,其中包含有關數(shù)據(jù)集的元數(shù)據(jù)和函數(shù),該函數(shù)將數(shù)據(jù)集加載到內存中。

在描述文件中,我們需要定義以下元數(shù)據(jù):

1.名稱:數(shù)據(jù)集的名稱。 2.版本:數(shù)據(jù)集的版本號。 3.描述:數(shù)據(jù)集的簡短描述。 4.特征:數(shù)據(jù)集的特征(例如,輸入和輸出的形狀、數(shù)據(jù)類型等)。 5.拆分:數(shù)據(jù)集應該如何劃分以進行訓練、驗證和測試。 6.下載URL(可選):如果數(shù)據(jù)集沒有被打包成一個文件,請?zhí)峁┮粋€URL以下載數(shù)據(jù)集。

以下是一個簡單的描述文件示例:

import tensorflow_datasets as tfds import os # Define the metadata for the dataset _DESCRIPTION = 'A dataset containing images of digits.' _VERSION = tfds.core.Version('1.0.0')
_NAME = 'my_dataset' def my_dataset(split): # Define the path to the data directory data_dir = os.path.join(os.getcwd(), 'data') # Define the classes classes = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'] # Load the data dataset_builder = tfds.builder(_NAME)
    dataset_builder.data_dir = data_dir
    dataset_builder.add_images(
        os.path.join(data_dir, '*/*'), 
        labels=classes,
    ) return dataset_builder.as_dataset(split=split)

在上面的代碼中,我們定義了一個名為my_dataset的函數(shù),該函數(shù)將數(shù)據(jù)集加載到內存中。我們還定義了元數(shù)據(jù),包括數(shù)據(jù)集的名稱、版本和描述,以及數(shù)據(jù)集的特征和拆分方式。

最后,我們使用tfds.builder()函數(shù)創(chuàng)建了一個dataset_builder對象,并使用add_images()方法將圖像添加到數(shù)據(jù)集中。請注意,此處我們使用了data_dir變量來指定數(shù)據(jù)集的路徑。如果您的數(shù)據(jù)集存在其他位置,則需要更改此變量的值以反映正確的路徑。

使用描述文件加載數(shù)據(jù)集

使用上述描述文件,我們可以通過調用tfds.load()函數(shù)來加載本地數(shù)據(jù)集。這個函數(shù)需要傳遞三個參數(shù):數(shù)據(jù)集名稱、數(shù)據(jù)集拆分方式和描述文件的路徑或模塊。

以下是一個簡單的例子:

import tensorflow_datasets as tfds # Load the data my_dataset = tfds.load(
    name='my_dataset',
    split='train',
    data_dir='./data',
    download=False,
    with_info=True,
) # Print

在上面的代碼中,我們使用tfds.load()函數(shù)來加載名為my_dataset的數(shù)據(jù)集,使用了train拆分并指定了數(shù)據(jù)集路徑。此外,我們將with_info參數(shù)設置為True以獲取有關數(shù)據(jù)集的元信息。

一旦數(shù)據(jù)集被加載到內存中,我們可以像其他TFDS數(shù)據(jù)集一樣使用它進行訓練或測試。

總結

在本文中,我們介紹了如何使用TFDS加載本地數(shù)據(jù)集。首先,我們準備了數(shù)據(jù)集,并創(chuàng)建了一個TFDS數(shù)據(jù)集描述文件。然后,我們使用tfds.load()函數(shù)將數(shù)據(jù)集加載到內存中,并使用它來訓練或測試模型。雖然這種方法可能需要更多的手動操作,但它允許用戶使用自己的數(shù)據(jù)集進行機器學習,從而獲得更好的控制和靈活性。

相信讀完上文,你對算法已經有了全面認識。若想進一步探索機器學習的前沿知識,強烈推薦機器學習之半監(jiān)督學習課程。

學習入口:https://edu.cda.cn/goods/show/3826?targetId=6730&preview=0
涵蓋核心算法,結合多領域實戰(zhàn)案例,還會持續(xù)更新,無論是新手入門還是高手進階都很合適。趕緊點擊鏈接開啟學習吧!

數(shù)據(jù)分析咨詢請掃描二維碼

若不方便掃碼,搜微信號:CDAshujufenxi

數(shù)據(jù)分析師考試動態(tài)
數(shù)據(jù)分析師資訊
更多

OK
客服在線
立即咨詢
客服在線
立即咨詢
') } function initGt() { var handler = function (captchaObj) { captchaObj.appendTo('#captcha'); captchaObj.onReady(function () { $("#wait").hide(); }).onSuccess(function(){ $('.getcheckcode').removeClass('dis'); $('.getcheckcode').trigger('click'); }); window.captchaObj = captchaObj; }; $('#captcha').show(); $.ajax({ url: "/login/gtstart?t=" + (new Date()).getTime(), // 加隨機數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調用 initGeetest 進行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調,回調的第一個參數(shù)驗證碼對象,之后可以使用它調用相應的接口 initGeetest({ // 以下 4 個配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺檢測極驗服務器是否宕機 new_captcha: data.new_captcha, // 用于宕機時表示是新驗證碼的宕機 product: "float", // 產品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說明請參見:http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計時完成 $(".getcheckcode").removeClass('dis').html("重新獲取"); }else{ $(".getcheckcode").addClass('dis').html("重新獲取("+_wait+"s)"); _wait--; setTimeout(function () { codeCutdown(); },1000); } } function inputValidate(ele,telInput) { var oInput = ele; var inputVal = oInput.val(); var oType = ele.attr('data-type'); var oEtag = $('#etag').val(); var oErr = oInput.closest('.form_box').next('.err_txt'); var empTxt = '請輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請輸入正確的'+oInput.attr('placeholder')+'!'; var pattern; if(inputVal==""){ if(!telInput){ errFun(oErr,empTxt); } return false; }else { switch (oType){ case 'login_mobile': pattern = /^1[3456789]\d{9}$/; if(inputVal.length==11) { $.ajax({ url: '/login/checkmobile', type: "post", dataType: "json", data: { mobile: inputVal, etag: oEtag, page_ur: window.location.href, page_referer: document.referrer }, success: function (data) { } }); } break; case 'login_yzm': pattern = /^\d{6}$/; break; } if(oType=='login_mobile'){ } if(!!validateFun(pattern,inputVal)){ errFun(oErr,'') if(telInput){ $('.getcheckcode').removeClass('dis'); } }else { if(!telInput) { errFun(oErr, errTxt); }else { $('.getcheckcode').addClass('dis'); } return false; } } return true; } function errFun(obj,msg) { obj.html(msg); if(msg==''){ $('.login_submit').removeClass('dis'); }else { $('.login_submit').addClass('dis'); } } function validateFun(pat,val) { return pat.test(val); }