python - 如何使用 fastai unet_learner?
问题描述
概括
- 细节利用 fastai(v2)
unet_learner
函数在迁移学习中利用 resnet34。 .fine_tune(n)
通过构建阶段 ( )的预期结果学习者。- 错误 IndexError:目标 20 超出范围。
尝试的补救措施
运行与此处所示相同的过程,没有问题。该过程顺利进行,学习者毫无问题地完成了微调和预测(在camvid_tiny数据集上)。
检查我处理的数据,包括“标签”、“图像”和“代码”与 camvid 进程中的数据,它们几乎相同(我的 21 个类与 camvid 的 ~30,图像和标签是 256x256 与 camvid 的 96、128) .
for i in range(len(lnames)):
y = Image.open(lnames[i])
y_array = np.array(y)
print(np.unique(y_array))
[20]
[20]
[20]
[ 5 7 8 11 12 13 14 17 20]
[ 5 12 13 14 15 16 17 20]
...
[14 17 20]
[14 17 20]
[ 8 9 10 11 12 13 14 16 17 19 20]
[ 1 2 3 4 7 8 9 10 11 12 13 14 16 17 18 19 20]
[ 2 3 9 10 12 13 14 16 17 19 20]
[ 1 2 3 4 7 8 9 10 11 12 13 14 16 17 18 19 20]
代码
import os
import json
import numpy as np
from pathlib import Path
from fastai.vision.all import *
>>path = Path(r"D:\EuroSATDS")
>>json_file = r"D:\EuroSATDS\esri_accumulated_stats.json"
>>>with open(json_file, 'r') as f:
data = json.load(f)
>>>classes = data['Classes']
>>>classes_list = []
>>>classes_value = []
>>>for i in classes:
x = i['ClassName']
y = i['ClassValue']
classes_list.append(x)
classes_value.append(y)
>>>classes_list[20]
'Palustrine Aquatic Bed'
>>>codes = np.asarray(classes_list, dtype='<U17')
>>>codes
array(['High Intensity De', 'Medium Intensity ', 'Low Intensity Dev',
'Developed Open Sp', 'Cultivated', 'Pasture/Hay', 'Grassland',
'Deciduous Forest', 'Evergreen Forest', 'Mixed Forest',
'Scrub/Shrub', 'Palustrine Forest', 'Palustrine Scrub/',
'Palustrine Emerge', 'Estuarine Foreste', 'Estuarine Scrub/S',
'Estuarine Emergen', 'Unconsolidated Sh', 'Bare Land', 'Water'],
dtype='<U17')
>>fnames = get_image_files(path/"images")
>>fnames[0]
Path('../000000000.jpg')
>>def label_func(fn): return pathB/"labels"/f"{fn.stem}_P.png"
>>dls = SegmentationDataLoaders.from_label_func(pathB, bs=8, fnames = fnames, label_func = label_func, codes = codes)
>>dls.show_batch(max_n=6)
>> learn = unet_learner(dls, resnet34)
>> learn.fine_tune(1)
IndexError: Target 20 is out of bounds.
IndexError Traceback (most recent call last)
~TEMP/ipykernel_14508/3714593663.py in <module>
2 import time
3 start = time.time()
----> 4 learn.fine_tune(1)
5 end = time.time()
6 print("The time of execution of above program is :", end-start)
解决方案
推荐阅读
- javascript - 如何覆盖 Google Cloud Tasks Node.js 客户端的重试配置
- postgresql-10 - 撤消在另一个函数中调用的函数事务
- android - Android BottomNavigationBar 每个选项卡的唯一图标色调列表?
- mercurial - 如何从自定义挂钩中读取配置设置?
- java - 如何与加密的 SQS 队列交互?KMS 密钥访问不起作用
- python - 哪种文件处理方式更好:'r' + 'a' 或 'r+'?
- android - 可绘制的正确位置不正确
- tensorflow - pytorch 中 tf.nn.softmax_cross_entropy_with_logits 的等价物是什么?
- yocto - 带有 Yocto 和 GUI 的 Odroid XU4(未找到 gtk+3 错误 wayland-egl)
- node.js - SailsJS / Waterline:仅从关联中获取特定属性