当目录不为空时,fastai抛出“训练集为空”错误

时间:2020-05-05 20:00:10

标签: python exception deep-learning google-colaboratory fast-ai

我正在尝试使用培训和测试集构建分类器。但是,尽管训练和测试集都不为空,但FastAI仍会引发UserWarning: Your training set is empty错误。这是终端命令的输出,以查看每个文件夹有多少项:

%cd Food-101/images/train/
!ls -1 | wc -l

%cd ../test
!ls -1 | wc -l

%cd ../../

/content/Food-101/images/train
75750
/content/Food-101/images/test
25250
/content/Food-101

这是我的代码:

import pandas as pd

from fastai import *
from fastai.vision import *
from fastai.callbacks.hooks import *
from pathlib import Path
from numba import vectorize
from subprocess import call, run
import os, git, glob, shutil

file_parse = r'/([^/]+)_\d+\.(png|jpg|jpeg)$'

np.random.seed(42)
data = ImageDataBunch.from_folder(path, train= path + '/images/train', test=path + '/images/test', valid_pct=0.2, ds_tfms=get_transforms(), size=224)
data.normalize(imagenet_stats)

这是错误:

/usr/local/lib/python3.6/dist-packages/fastai/data_block.py:458: UserWarning: Your training set is empty. If this is by design, pass `ignore_empty=True` to remove this warning.
  warn("Your training set is empty. If this is by design, pass `ignore_empty=True` to remove this warning.")
/usr/local/lib/python3.6/dist-packages/fastai/data_block.py:461: UserWarning: Your validation set is empty. If this is by design, use `split_none()`
                 or pass `ignore_empty=True` when labelling to remove this warning.
  or pass `ignore_empty=True` when labelling to remove this warning.""")
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
/usr/local/lib/python3.6/dist-packages/fastai/data_block.py in get_label_cls(self, labels, label_cls, label_delim, **kwargs)
    264         if label_delim is not None:             return MultiCategoryList
--> 265         try: it = index_row(labels,0)
    266         except: raise Exception("""Can't infer the type of your targets. 

7 frames
IndexError: index 0 is out of bounds for axis 0 with size 0

During handling of the above exception, another exception occurred:

Exception                                 Traceback (most recent call last)
/usr/local/lib/python3.6/dist-packages/fastai/data_block.py in get_label_cls(self, labels, label_cls, label_delim, **kwargs)
    265         try: it = index_row(labels,0)
    266         except: raise Exception("""Can't infer the type of your targets. 
--> 267 It's either because your data source is empty or because your labelling function raised an error.""")
    268         if isinstance(it, (float, np.float32)): return FloatList
    269         if isinstance(try_int(it), (str, Integral)):  return CategoryList

Exception: Can't infer the type of your targets. 
It's either because your data source is empty or because your labelling function raised an error.

我不明白为什么两个目录中都清楚地装有图像时,为什么会引发此错误。

2 个答案:

答案 0 :(得分:2)

您可以确保将<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Swiper demo</title> <!-- Link Swiper's CSS --> <link rel="stylesheet" href="https://unpkg.com/swiper/css/swiper.min.css"> </head> <body> <!-- Swiper --> <div class="swiper-container"> <div class="swiper-wrapper"> <div class="swiper-slide">Slide 1</div> <div class="swiper-slide">Slide 2</div> <div class="swiper-slide">Slide 3</div> <div class="swiper-slide">Slide 4</div> <div class="swiper-slide">Slide 5</div> <div class="swiper-slide">Slide 6</div> <div class="swiper-slide">Slide 7</div> <div class="swiper-slide">Slide 8</div> <div class="swiper-slide">Slide 9</div> <div class="swiper-slide">Slide 10</div> </div> <!-- Add Pagination --> <div class="swiper-pagination"></div> </div> <!-- Swiper JS --> <script src="https://unpkg.com/swiper/js/swiper.min.js"></script> <!-- Initialize Swiper --> <script> var swiper = new Swiper('.swiper-container', { pagination: { el: '.swiper-pagination', clickable: true, renderBullet: function (index, className) { return '<span class="' + className + '">' + (index + 1) + '</span>'; }, }, }); </script> </body> </html>变量设置为/ content / Food-101 /吗?

答案 1 :(得分:0)

我解决了这个问题:

data = ImageDataBunch.from_folder('/content/Food-101/images', train='/content/Food-101/images/train', test='/content/Food-101/images/test', valid_pct=0.2, ds_tfms=get_transforms(), size=224)