Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

怎么用训练好的模型批量预测呀 #88

Open
yycrycry opened this issue Jan 22, 2024 · 4 comments
Open

怎么用训练好的模型批量预测呀 #88

yycrycry opened this issue Jan 22, 2024 · 4 comments

Comments

@yycrycry
Copy link

No description provided.

@Fafa-DL
Copy link
Owner

Fafa-DL commented Jan 22, 2024

模型评估

@yycrycry
Copy link
Author

yycrycry commented Jan 23, 2024

谢谢大神,批量预测时一直报错: File "tools/batch_test.py", line 66, in
main()
File "tools/batch_test.py", line 50, in main
result = inference_model(model, img, val_pipeline, classes_names, label_names)
File "E:\lyy\Awesome-Backbones-main (1)\Awesome-Backbones-main\utils\inference.py", line 97, in inference_model
processed_data = pipeline(data)
File "E:\lyy\Awesome-Backbones-main (1)\Awesome-Backbones-main\core\datasets\compose.py", line 40, in call
data = t(data)
File "E:\lyy\Awesome-Backbones-main (1)\Awesome-Backbones-main\core\datasets\loading.py", line 92, in call
if results['img_prefix'] is not None:
KeyError: 'img_prefix
我的输入数据是二通道的numpy数组,所以我将数据加载方式改成了这样:
class LoadNumpyFromArray(object):
"""Load an image from file.

Required keys are "img_prefix" and "img_info" (a dict that must contain the
key "filename"). Added or updated keys are "filename", "img", "img_shape",
"ori_shape" (same as `img_shape`) and "img_norm_cfg" (means=0 and stds=1).

Args:
    to_float32 (bool): Whether to convert the loaded image to a float32
        numpy array. If set to False, the loaded image is an uint8 array.
        Defaults to False.
    color_type (str): The flag argument for :func:`mmcv.imfrombytes()`.
        Defaults to 'color'.
    
"""

def __init__(self,
             to_float32=False,
             color_type='grayscale',
             file_client_args=dict(backend='disk')):
    self.to_float32 = to_float32
    self.color_type = color_type
    
def get(self, filepath):
    """Read image data from a given ``filepath``.

    Args:
        filepath (str or Path): Path to read image.

    Returns:
        np.ndarray: Expected numpy array representing the image.
    """
    try:
        img = np.load(filepath)
        if img is None:
            print(f"Error: Failed to read image from {filepath}")
        return img
    except Exception as e:
        print(f"Error: {str(e)}")
        return None


def __call__(self, results):
    if results['img_prefix'] is not None:
        filename = osp.join(results['img_prefix'], results['img_info']['filename'])
    else:
        filename = results['img_info']['filename']

    img = np.load(filename, allow_pickle=True)  # 读取时指定 allow_pickle=True

    if self.to_float32:
        img = img.astype(np.float32)

    results['filename'] = filename
    results['ori_filename'] = results['img_info']['filename']
    results['img'] = img
    results['img_shape'] = img.shape
    results['ori_shape'] = img.shape
    num_channels = img.shape[2] if len(img.shape) == 3 else 1  # 调整通道数
    results['img_norm_cfg'] = dict(
        mean=np.zeros(num_channels, dtype=np.float32),
        std=np.ones(num_channels, dtype=np.float32),
        to_rgb=False)
    return results

训练的时候没有报错,但预测的时候报错了,请问是什么原因呀

@Fafa-DL
Copy link
Owner

Fafa-DL commented Jan 30, 2024

训练的时候没问题然后预测有问题,是不是自己更换了测试集

@gepaohhh
Copy link

gepaohhh commented Feb 1, 2024

image
这个为啥会不匹配

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants