【问题标题】:How to accelerate the code which convert tensor to numpy array in tensorflow_datasets?如何加速将张量转换为 tensorflow_datasets 中的 numpy 数组的代码?
【发布时间】:2020-02-05 14:27:49
【问题描述】:

虽然我想在 tensorflow_datasets 中将张量转换为 numpy 数组,但我的代码逐渐变慢。 现在,我使用 lsun/bedroom 数据集,它有超过 300 万张图像。 如何加速我的代码?

我的代码保存每 100,000 张图像有一个 numpy 数组的元组。

train_tf = tfds.load("lsun/bedroom", data_dir="{$my_directory}", download=False)
train_tf = train_tf["train"]
for data in train_tf:
    if d_cnt==0 and d_cnt%100001==0:
        train = (tfds.as_numpy(data["image"]), )
    else:
        train += (tfds.as_numpy(data["image"]), )

    if d_cnt%100000==0 and d_cnt!=0:
        with open("{$my_directory}/lsun.pickle%d"%(d_cnt), "wb") as f:
            pickle.dump(train, f)

    d_cnt += 1

【问题讨论】:

    标签: python numpy tensorflow tensor tensorflow-datasets


    【解决方案1】:

    您的if 条件在第一次通过后永远不会被执行,因此您的train 变量会不断累积。

    我认为你希望有如下条件:

    if d_cnt!=0 and d_cnt%100001==0:
        train = (tfds.as_numpy(data["image"]), )
    

    【讨论】:

    • 谢谢你教我!!我犯了一个简单的错误。我修改代码后,代码速度大大加快!
    • 没问题。 :) 快乐编码
    猜你喜欢
    • 2021-01-08
    • 1970-01-01
    • 1970-01-01
    • 2020-12-21
    • 2021-10-17
    • 2018-11-28
    • 2019-06-13
    • 2019-03-19
    • 2021-08-25
    相关资源
    最近更新 更多