【问题标题】:How to get 50 samples from each class of imagenet?如何从每类 imagenet 中获取 50 个样本?
【发布时间】:2021-06-01 07:01:18
【问题描述】:

例如,我想在 imagenet 中拥有每个类的 50 个样本。我想访问他们的 URL。 我尝试使用这种方式来访问每个类的样本: http://www.image-net.org/api/text/imagenet.synset.geturls?wnid=n01484850 但有些 URL 不起作用。你知道我可以为每个班级访问 50 个正确 URL 的任何快速方法吗? 对于每个类,我保存示例的 .txt 文件,然后使用下面的代码将示例提供给我的深度 CNN,但由于某些 URL 不起作用,代码不起作用。

代码:

f = open("C:\\Windows\\System32\\whiteshark.txt", "r")
number_of_lines = 50
for i in range(number_of_lines):
    line = f.readline()
    url, filename = (line, "3000.jpg")
    #print( urllib.URLopener().retrieve(url, filename))
    try:
        urllib.URLopener().retrieve(url, filename)
    except:
        urllib.request.urlretrieve(url, filename)
    input_image = Image.open(filename)
    img_t = transform(input_image)
    batch_t = torch.unsqueeze(img_t, 0)
    print(url)

【问题讨论】:

  • 我在包含文件名的字符串中添加了一个缺失的 ``。

标签: python urllib imagenet


【解决方案1】:

一种解决方法是在文本中添加更多带有 url 的行(例如 1000 行),并继续尝试访问每个 url,直到达到 50 个样本。示例:

acquired_samples = 0
while acquired_samples < 50:
    ....
    try:
        urllib.URLopener().retrieve(url, filename)
        acquired_samples += 1
    ...

你明白了。

免责声明:您可能需要采取一些预防措施,以免陷入无限循环。

【讨论】:

    猜你喜欢
    • 2018-08-16
    • 1970-01-01
    • 2019-12-23
    • 2021-04-14
    • 2016-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多