如何使用torchvision.datasets.imagefolder的is_valid_file

发布于 2025-01-22 22:25:07 字数 559 浏览 0 评论 0 原文

我正在尝试加载一些图像来训练小型型号。但是,我无法使用 is_valid_file 来区分我的火车和测试图像。例如,我的文件就是这样:

蔬菜/番茄/画廊.jpg

火车套装和

蔬菜/番茄/探针.jpg

用于测试组。基本上,诸如番茄和其他选定蔬菜之类的每个文件都有2张测试和火车图像。

问题:如何使用 IS_VALID_FILE 来区分它们。我检查了文档,但我不明白。

这是我的代码

trainset = torchvision.datasets.ImageFolder(
      root = "vegetables",
      transform=imagenet_transform
    )

testset = torchvision.datasets.ImageFolder(
  root = "vegetables",
  transform=imagenet_transform
)

I am trying to load some images to train a small model. However, I couldn't use is_valid_file to distinguish my train and test images. For example my file is like that :

vegetables/tomato/gallery.jpg

for train set and

vegetables/tomato/probe.jpg

for test set. Basically every file like tomato and other selected vegetables has 2 images both test and train image.

Question: How can I use is_valid_file to distinguish them. I checked the documentation but I didn't understand.

This is my code

trainset = torchvision.datasets.ImageFolder(
      root = "vegetables",
      transform=imagenet_transform
    )

testset = torchvision.datasets.ImageFolder(
  root = "vegetables",
  transform=imagenet_transform
)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一个人的夜不怕黑 2025-01-29 22:25:07

我也有问题!!!只是不解决蔬菜问题,而是解决猫鱼问题。您可以在此页面上在此处找到数据集()。下载的数据集必须保存在保存.IPYNB文件的同一文件夹中。
我使用Google Colab(使事情变得更容易),这意味着我有一个.ipynb文件。

import torchvision
from PIL import Image, ImageFile
from torchvision import transforms
from torchvision.datasets import ImageFolder 

from google.colab import drive
drive.mount('/content/drive')

ImageFile.LOAD_TRUNCATED_IMAGES = False

def check_Image(path):
  try:
    im = Image.open(path)
    return True
  except:
    return False
# train_data_path = "/.train/"

    

为了获得您的道路,您必须走(就我而言) - 驱动器 - > mydrive-> Pytorch项目 - > 02章 - >鱼vs-cats->火车 - >右边的3分 - >复制路径

train_data_path = "/content/drive/MyDrive/Pytorch Projects/02 Chapter/Fish-vs-Cats/train"



img_transform = transforms.Compose([
    transforms.Resize((64,64)),
    transforms.ToTensor(),
    transforms.Normalize(mean=[0.485, 0.456, 0.406],
                         std=[0.229, 0.224, 0.225])
])
   
train_data = ImageFolder(root=train_data_path, 
                         transform=img_transform, 
                         is_valid_file=check_Image)

I had the problem too!!! Only not to the vegetable problem, but to the cat-fish problem. You can find the datasets here on this page (https://www.kaggle.com/datasets/zuraiz/fish-vs-cats-imagenet-subdataset). The downloaded datasets have to be saved in the same folder where you saved your .ipynb file.
I use Google Colab (makes things a bit easier), which means I have an .ipynb file.

import torchvision
from PIL import Image, ImageFile
from torchvision import transforms
from torchvision.datasets import ImageFolder 

from google.colab import drive
drive.mount('/content/drive')

ImageFile.LOAD_TRUNCATED_IMAGES = False

def check_Image(path):
  try:
    im = Image.open(path)
    return True
  except:
    return False
# train_data_path = "/.train/"

    

in order to get your path you have to go (in my case) - drive -> MyDrive -> Pytorch Projects -> 02 Chapter -> Fish-vs-Cats -> train -> 3 points on the right -> copy path

train_data_path = "/content/drive/MyDrive/Pytorch Projects/02 Chapter/Fish-vs-Cats/train"



img_transform = transforms.Compose([
    transforms.Resize((64,64)),
    transforms.ToTensor(),
    transforms.Normalize(mean=[0.485, 0.456, 0.406],
                         std=[0.229, 0.224, 0.225])
])
   
train_data = ImageFolder(root=train_data_path, 
                         transform=img_transform, 
                         is_valid_file=check_Image)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文