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

图像分类加载EuroSAT数据集时Resize报错:Size需为int/sequence,实际为dict

问题解决:Size should be int or sequence. Got <class 'dict'>

错误原因

你的代码中feature_extractor.size返回的是字典类型(比如{'height': 224, 'width': 224}),而torchvision的Resize、CenterCrop等变换需要的参数是整数(等比例缩放短边至该值)或长度为2的序列(指定固定的高和宽),直接传入字典就会触发类型错误。

解决方案

从feature_extractor.size字典中提取height和width值,组成元组传入变换即可。修改后的代码如下:

dataset = load_dataset("imagefolder", data_dir="/content/drive/MyDrive/datasets/EuroSAT_RGB/")

model_checkpoint = "microsoft/swin-tiny-patch4-window7-224"
feature_extractor = AutoFeatureExtractor.from_pretrained(model_checkpoint)

# 提取高宽组成元组
target_size = (feature_extractor.size["height"], feature_extractor.size["width"])

val_transforms = Compose(
        [
            Resize(target_size),
            CenterCrop(target_size),
            ToTensor(),
            normalize,
        ]
    )

如果模型输入是正方形尺寸(比如Swin的224x224),也可以简化为直接取其中一个值(因为Resize接受整数时会等比例缩放短边到该值,后续CenterCrop再裁剪到固定尺寸):

target_size = feature_extractor.size["height"]

val_transforms = Compose(
        [
            Resize(target_size),
            CenterCrop(target_size),
            ToTensor(),
            normalize,
        ]
    )

内容的提问来源于stack exchange,提问作者Subhram Dasgupta

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.23 03:04:58