运行download_pgms.py显示 import urllib.request ImportError: No module named request 的解决方法

 查看代码的时候发现里面的原理是在网站中下载安装包然后解压缩的过程。

    with urllib.request.urlopen("http://yann.lecun.com/exdb/mnist/train-images-idx3-ubyte.gz") as res:
        data = load_mnist_data(gzip.decompress(res.read()))
    with urllib.request.urlopen("http://yann.lecun.com/exdb/mnist/train-labels-idx1-ubyte.gz") as res:
        labels = load_mnist_labels(gzip.decompress(res.read()))

 python2不支持urllib.request的借口,那么可以手动进入上面两个网站,可以依次下载两个文件

将解压缩得到的(白色的)放在自己电脑的 /TensorRT-7.2.1.6/data/mnist  下,然后更改download_pgms.py为如下格式。

主要将  np.fromstring 修改为 np.fromfile

将   

    with urllib.request.urlopen("http://yann.lecun.com/exdb/mnist/train-images-idx3-ubyte.gz") as res:
        data = load_mnist_data(gzip.decompress(res.read()))
    with urllib.request.urlopen("http://yann.lecun.com/exdb/mnist/train-labels-idx1-ubyte.gz") as res:
        labels = load_mnist_labels(gzip.decompress(res.read()))

修改为:

    with open('train-images-idx3-ubyte') as res:
        data = load_mnist_data(res)
    with open('train-labels-idx1-ubyte') as res:
        labels = load_mnist_labels(res)

整体代码如下:

#!/usr/bin/env python3
from PIL import Image
#import urllib.request
import urllib
import numpy as np
import argparse
import gzip
import os


# Returns a numpy buffer of shape (num_images, 28, 28)
def load_mnist_data(buffer):
    raw_buf = np.fromfile(buffer, dtype=np.uint8)
    # Make sure the magic number is what we expect
    assert raw_buf[0:4].view(">i4")[0] == 2051
    num_images = raw_buf[4:8].view(">i4")[0]
    image_h = raw_buf[8:12].view(">i4")[0]
    image_w = raw_buf[12:16].view(">i4")[0]
    # Colors in the dataset are inverted vs. what the samples expect.
    return np.ascontiguousarray(255 - raw_buf[16:].reshape(num_images, image_h, image_w))

# Returns a list of length num_images
def load_mnist_labels(buffer):
    raw_buf = np.fromfile(buffer, dtype=np.uint8)
    # Make sure the magic number is what we expect
    assert raw_buf[0:4].view(">i4")[0] == 2049
    num_labels = raw_buf[4:8].view(">i4")[0]
    return list(raw_buf[8:].astype(np.int32).reshape(num_labels))

def main():
    parser = argparse.ArgumentParser(description="Extracts 10 PGM files from the MNIST dataset", formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument("-o", "--output", help="Path to the output directory.", default=os.getcwd())

    args, _ = parser.parse_known_args()

    with open('train-images-idx3-ubyte') as res:
        data = load_mnist_data(res)
    with open('train-labels-idx1-ubyte') as res:
        labels = load_mnist_labels(res)

    output_dir = args.output

    # Find one image for each digit.
    for i in range(10):
        index = labels.index(i)
        image = Image.fromarray(data[index], mode="L")
        path = os.path.join(output_dir, "{:}.pgm".format(i))
        image.save(path)

if __name__ == '__main__':
    main()

参考链接:

TensorRT系列1--安装配置 - 知乎

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值