You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

fastai v2.3.1中show_batch与show_results函数无输出问题求助

FastAI v2.3.1: show_batch() and show_results() Not Displaying Content

I built a model using FastAI v2.3.1, but the show_batch() and show_results() functions aren't displaying any content. The model itself works normally during training, but these visualization functions fail to render output. Here's my code:

from fastai.vision.all import *
from fastai.data.all import *
import fastai.vision
import zipfile as zf
import random
import timeit

fields = DataBlock(blocks=(ImageBlock, CategoryBlock),
                  get_items=get_image_files,
                  get_y=yer,
                  splitter=RandomSplitter(valid_pct=0.2, seed=random.randint(0, 10)),
                  item_tfms=RandomResizedCrop(224, min_scale=0.5),
                  batch_tfms=aug_transforms() )
dls = fields.dataloaders(os.path.join(Path(os.getcwd()), "train"), num_workers=0, bs=32)
dls.show_batch()

learn = cnn_learner(dls, resnet18, metrics=error_rate)
learn.fine_tune(2)
learn.show_results()

I've run into similar visualization hiccups with FastAI v2 before, so let's walk through practical fixes and checks to get those previews working:

1. Force Matplotlib to Render Output

FastAI relies on Matplotlib for visualization, and sometimes Jupyter environments don't auto-trigger the render. Try these steps:

  • Add this line at the very top of your notebook to enable inline rendering:
    %matplotlib inline
    
    If you're using JupyterLab, swap it for %matplotlib widget for interactive plots.
  • Explicitly call plt.show() right after each visualization function to force the plot to display:
    import matplotlib.pyplot as plt
    
    dls.show_batch()
    plt.show()
    
    learn.show_results()
    plt.show()
    

2. Verify Your Label Extraction Function (yer)

Your code uses get_y=yer but doesn't include its definition. While you mentioned the model trains fine, a flaky label function could still break visualization. Double-check:

  • Does yer return valid string labels? If your data is organized in class-named folders, use FastAI's built-in parent_label to rule out custom function issues:
    fields = DataBlock(blocks=(ImageBlock, CategoryBlock),
                      get_items=get_image_files,
                      get_y=parent_label,  # Replace yer with built-in label extractor
                      splitter=RandomSplitter(valid_pct=0.2, seed=random.randint(0, 10)),
                      item_tfms=RandomResizedCrop(224, min_scale=0.5),
                      batch_tfms=aug_transforms() )
    

3. Adjust Visualization Size

Sometimes plots render but are too small to notice. Try specifying a larger figsize:

dls.show_batch(figsize=(12, 12))
learn.show_results(figsize=(12, 12))

4. Validate Your Data Loader

Make sure your data loader is actually loading images correctly. Print a sample from your dataset to confirm:

print(dls.train_ds[0])  # Should output an image tensor and its corresponding label

If this returns valid data, your issue is definitely with visualization rendering, not data loading.

5. Check Matplotlib Version Compatibility

FastAI v2.3.1 works best with specific Matplotlib versions. Outdated or overly new versions can cause rendering bugs. Pin or upgrade to a compatible version:

pip install --upgrade matplotlib==3.5.3  # A version known to play well with FastAI v2.3.1

内容的提问来源于stack exchange,提问作者Yunus Gedik

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 15:47:43