machine-learning - FastAI PyTorch Train_loss 和 valid_loss 看起来非常好,但模型无法识别任何内容

标签 machine-learning computer-vision pytorch fast-ai

更新1

我认为这可能是我的检测器代码中的错误。 因此,这是我使用经过训练的学习器/模型来预测图像的代码。

import requests
import cv2

bytes = b''
stream = requests.get(url, stream=True)
bytes = bytes + stream.raw.read(1024) # I have my mobile video streaming to this url. the resolution for the video streaming is: 2048 x 1080
a = bytes.find(b'\xff\xd8')
b = bytes.find(b'\xff\xd9')
if a != -1 and b != -1:
      jpg = bytes[a:b+2]
      bytes = bytes[b+2:]
      img = cv2.imdecode(np.fromstring(jpg, dtype=np.uint8), cv2.IMREAD_COLOR)
      processedImg = Image(pil2tensor(img, np.float32).div_(255))
      predict = learn.predict(processedImg)
      self.objectClass = predict[0].obj

我阅读了imdecode()方法的文档,它以B G R顺序返回图像。

是否是因为训练和检测时使用的 channel 数据不同?

或者

是否是因为我使用图像尺寸 299 x 450 进行训练,但从视频流中检测到输入图像尺寸为 2048 x 1080 且未调整其大小?

<小时/>

FastAi、ML 和 Python 新手。我训练了我的“鸟或非鸟”模型。 train_loss、valid_loss 和 error_rate 都在改善。如果我只训练 3 个 epoch,那么模型就可以工作(意味着它可以识别图像中是否有鸟),然后我增加到 30 个 epoch,所有指标看起来都很好,但是模型不再识别任何东西了我输入的图像,模型总是返回非鸟类。

这是训练输出:

enter image description here

这是 learn.recorder 的绘图

enter image description here

这是我的代码:

from fastai.vision import *
from fastai.metrics import error_rate
from fastai.callbacks import EarlyStoppingCallback,SaveModelCallback
from datetime import datetime as dt
from functools import partial

path_img = '/minidata'
train_folder = 'train'
valid_folder = 'validation'

tunedTransform = partial(get_transforms, max_zoom=1.5)

data = ImageDataBunch.from_folder(path=path_img, train=train_folder, valid=valid_folder, ds_tfms=tunedTransform(), 
                                  size=(299, 450), bs=40, classes=['birds', 'others'], 
                                  resize_method=ResizeMethod.SQUISH)
data = data.normalize(imagenet_stats)

learn = cnn_learner(data, models.resnet50, metrics=error_rate)
learn.fit_one_cycle(30, max_lr=slice(5e-5,5e-4))

learn.recorder.plot_lr()
learn.recorder.plot()
learn.recorder.plot_losses()

这是我的数据集文件夹结构:

  • 小型数据
    • 火车
      • 鸟类(7500 张图片)
      • 其他(约 7300 张图片)
    • 验证
      • 鸟类(1008 张图片)
      • 其他(约 872 张图片)

最佳答案

您的学习率计划对于该数据集来说不是最优的。首先尝试找出该网络和数据集的最佳学习率 LRFinder 。这可以通过使用

探索不同学习率的损失行为来完成
learn.lr_find()
learn.recorder.plot()

编辑:

看起来您正在重新训练网络中的最后一层。相反,尝试从头开始训练更多层。如:

learn.unfreeze(2)

关于machine-learning - FastAI PyTorch Train_loss 和 valid_loss 看起来非常好,但模型无法识别任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59548794/

相关文章:

python - 将预测结果合并到原始数据帧?

python - 如何将自己的数据集提供给keras image_ocr

python - SVM - 将字符串传递给 Python 中的 CountVectorizer 向量化每个字符?

computer-vision - Yolov4 是否已有更大的权重?

neural-network - 使用 DataLoaders 在 PyTorch 中验证数据集

python - xgboost: AttributeError: 'DMatrix' 对象没有属性 'handle'

opencv - 尺度不变性和方向不变性是什么意思?

computer-vision - QR 码 - 相机方向/投影

python - 在哪里可以找到 PyTorch 的 Tensor.unfold() 用于获取图像补丁的直观解释?

machine-learning - 如何在PyTorch中实现学习率的随机对数空间搜索?