manually download dataset and import in tensorflow

somtimes, you may have problem with tensorflow build-in downloading function like using

tfds.load(name="the300w_lp", split="train", with_info=True)

This could be different reason. One way to workaround is to download the dataset manually as mentioned in https://www.tensorflow.org/datasets/overview#manual_download_if_download_fails

The official doc is quite general. It took me hours to figure out how to build/import the downloaded dataset in tensorflow. At this time, the tensorflow I use is 2.6.0.

here, I want to use 300W-LP dataset in my project.

First, I manually download the dataset from the official project space, the downloaded file is named as 300W-LP.zip
(if you don’t know the official dataset download address, then you can go to the corresponding source code to find out, Here, when I open /home/seamanj/Software/miniconda3/envs/tf2/lib/python3.8/site-packages/tensorflow_datasets/image/the300w_lp.py I could find

_DATASET_URL = "https://drive.google.com/uc?export=download&id=0B7OEHD3T4eCkVGs0TkhUWFN6N1k"

_PROJECT_URL = "http://www.cbsr.ia.ac.cn/users/xiangyuzhu/projects/3DDFA/main.htm"

try both in your browser.

)

Second and most important step, you need to find out where you should put the downloaded file or point the related link to your location.

To do that, you need to set a breakpoint in download_manager.py (mine location is /home/seamanj/Software/miniconda3/envs/tf2/lib/python3.8/site-packages/tensorflow_datasets/core/download/download_manager.py) to find out what manual_path is

def _get_manually_downloaded_path(
    manual_dir: Optional[ReadOnlyPath],
    expected_url_info: Optional[checksums.UrlInfo],
) -> Optional[ReadOnlyPath]:
  """Checks if file is already downloaded in manual_dir."""
  if not manual_dir:  # Manual dir not passed
    return None

  if not expected_url_info or not expected_url_info.filename:
    return None  # Filename unknown.

  manual_path = manual_dir / expected_url_info.filename
  if not manual_path.exists():  # File not manually downloaded
    return None

here, my manual_path is /home/seamanj/tensorflow_datasets/downloads/manual/300W-LP.zip, then just put the downloaded file to this location.

Next, you need to prepare the dataset including extracting and creating tfrecords, this is done by builder’s download_and_prepare function, the complete code is as follows:

import tensorflow_datasets as tfds
from tensorflow_datasets import *


builder = tfds.image.The300wLp() # reference https://www.tensorflow.org/datasets/overview#manual_download_if_download_fails to construct the builder
download_config = download.DownloadConfig(manual_dir="~/tensorflow_datasets/downloads/manual",
                                          try_download_gcs=False,
                                          )

builder.download_and_prepare(download_dir="~/tensorflow_datasets", download_config=download_config)

note that you could skip the download_config unless you want to specify a different dir for your download dataset, the default manual_dir is ~/tensorflow_datasets/downloads/manual on my ubuntu 20.04

after this step, you will find the downloaded dataset is precossed
在这里插入图片描述
在这里插入图片描述
now, you can use it as normally you do

dataset, info = tfds.load(name="the300w_lp", split="train", with_info=True)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值