【实战精选】基于TinyNet-BackBone改进YOLOv5的胡萝卜表面缺陷分类系统

1.研究背景与意义

项目参考AAAI Association for the Advancement of Artificial Intelligence

研究背景与意义:

近年来,随着计算机视觉技术的快速发展,图像识别和目标检测成为了热门的研究领域。在农业领域,对农作物的质量和安全性进行准确评估和检测变得越来越重要。特别是对于蔬菜类农作物,如胡萝卜,表面缺陷的检测对于农民和消费者来说都具有重要意义。

胡萝卜是一种常见的蔬菜,广泛种植和消费。然而,胡萝卜的表面缺陷可能会影响其外观和品质,从而降低其市场价值。因此,开发一种准确、高效的胡萝卜表面缺陷分类系统对于农民和食品加工行业来说具有重要意义。

目前,基于深度学习的目标检测算法已经取得了显著的进展。其中,YOLOv5是一种快速、准确的目标检测算法,已经在许多领域取得了良好的效果。然而,对于胡萝卜表面缺陷的分类任务,YOLOv5仍然存在一些挑战。一方面,胡萝卜表面缺陷的种类繁多,形状和大小各异,传统的目标检测算法很难准确地识别和分类这些缺陷。另一方面,YOLOv5的网络结构相对简单,可能无法充分提取和表示胡萝卜表面缺陷的特征。

因此,本研究旨在基于TinyNet-BackBone改进YOLOv5的胡萝卜表面缺陷分类系统。TinyNet-BackBone是一种轻量级的网络结构,具有较少的参数和计算量,适合在资源受限的环境下使用。通过将TinyNet-BackBone集成到YOLOv5中,我们可以提高对胡萝卜表面缺陷的检测和分类准确性,并降低计算资源的消耗。

本研究的意义主要体现在以下几个方面:

  1. 提高胡萝卜表面缺陷的检测准确性:通过改进YOLOv5的网络结构,我们可以提高对胡萝卜表面缺陷的检测准确性。这将有助于农民和食品加工行业准确评估胡萝卜的质量和安全性,提高产品的市场竞争力。

  2. 降低计算资源的消耗:TinyNet-BackBone是一种轻量级的网络结构,具有较少的参数和计算量。通过将其集成到YOLOv5中,我们可以在保持较高检测准确性的同时,降低计算资源的消耗。这对于资源受限的环境下的胡萝卜表面缺陷分类系统的应用具有重要意义。

  3. 推动农业智能化发展:随着农业智能化的不断推进,自动化的胡萝卜表面缺陷分类系统将成为农业生产的重要组成部分。本研究的成果可以为农业智能化发展提供技术支持,促进农业生产的效率和质量的提高。

总之,基于TinyNet-BackBone改进YOLOv5的胡萝卜表面缺陷分类系统具有重要的研究意义和应用价值。通过提高胡萝卜表面缺陷的检测准确性和降低计算资源的消耗,该系统可以为农民和食品加工行业提供准确评估胡萝卜质量的工具,推动农业智能化发展。

2.图片演示

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

3.视频演示

基于TinyNet-BackBone改进YOLOv5的胡萝卜表面缺陷分类系统_哔哩哔哩_bilibili

4.数据集的采集&标注和整理

图片的收集

首先,我们需要收集所需的图片。这可以通过不同的方式来实现,例如使用现有的公开数据集LBLDatasets。

在这里插入图片描述

下面是一个简单的方法是使用Python脚本,该脚本读取分类图片文件,然后将其转换为所需的格式。

import os
import shutil
import random

# 指定输入和输出文件夹的路径
input_dir = 'train'
output_dir = 'output'

# 确保输出文件夹存在
if not os.path.exists(output_dir):
    os.makedirs(output_dir)

# 遍历输入文件夹中的所有子文件夹
for subdir in os.listdir(input_dir):
    input_subdir_path = os.path.join(input_dir, subdir)

    # 确保它是一个子文件夹
    if os.path.isdir(input_subdir_path):
        output_subdir_path = os.path.join(output_dir, subdir)

        # 在输出文件夹中创建同名的子文件夹
        if not os.path.exists(output_subdir_path):
            os.makedirs(output_subdir_path)

        # 获取所有文件的列表
        files = [f for f in os.listdir(input_subdir_path) if os.path.isfile(os.path.join(input_subdir_path, f))]

        # 随机选择四分之一的文件
        files_to_move = random.sample(files, len(files) // 4)

        # 移动文件
        for file_to_move in files_to_move:
            src_path = os.path.join(input_subdir_path, file_to_move)
            dest_path = os.path.join(output_subdir_path, file_to_move)
            shutil.move(src_path, dest_path)

print("任务完成!")


整理数据文件夹结构

我们需要将数据集整理为以下结构:

-----dataset
	-----dataset
           |-----train
           |   |-----class1
           |   |-----class2
           |   |-----.......
           |
           |-----valid
           |   |-----class1
           |   |-----class2
           |   |-----.......
           |
           |-----test
           |   |-----class1
           |   |-----class2
           |   |-----.......

模型训练
 Epoch   gpu_mem       box       obj       cls    labels  img_size
 1/200     20.8G   0.01576   0.01955  0.007536        22      1280: 100%|██████████| 849/849 [14:42<00:00,  1.04s/it]
           Class     Images     Labels          P          R     mAP@.5 mAP@.5:.95: 100%|██████████| 213/213 [01:14<00:00,  2.87it/s]
             all       3395      17314      0.994      0.957      0.0957      0.0843

 Epoch   gpu_mem       box       obj       cls    labels  img_size
 2/200     20.8G   0.01578   0.01923  0.007006        22      1280: 100%|██████████| 849/849 [14:44<00:00,  1.04s/it]
           Class     Images     Labels          P          R     mAP@.5 mAP@.5:.95: 100%|██████████| 213/213 [01:12<00:00,  2.95it/s]
             all       3395      17314      0.996      0.956      0.0957      0.0845

 Epoch   gpu_mem       box       obj       cls    labels  img_size
 3/200     20.8G   0.01561    0.0191  0.006895        27      1280: 100%|██████████| 849/849 [10:56<00:00,  1.29it/s]
           Class     Images     Labels          P          R     mAP@.5 mAP@.5:.95: 100%|███████   | 187/213 [00:52<00:00,  4.04it/s]
             all       3395      17314      0.996      0.957      0.0957      0.0845

5.核心代码讲解

5.1 export.py


def export_formats():
    # YOLOv5 export formats
    x = [
        ['PyTorch', '-', '.pt', True, True],
        ['TorchScript', 'torchscript', '.torchscript', True, True],
        ['ONNX', 'onnx', '.onnx', True, True],
        ['OpenVINO', 'openvino', '_openvino_model', True, False],
        ['TensorRT', 'engine', '.engine', False, True],
        ['CoreML', 'coreml', '.mlmodel', True, False],
        ['TensorFlow SavedModel', 'saved_model', '_saved_model', True, True],
        ['TensorFlow GraphDef', 'pb', '.pb', True, True],
        ['TensorFlow Lite', 'tflite', '.tflite', True, False],
        ['TensorFlow Edge TPU', 'edgetpu', '_edgetpu.tflite', False, False],
        ['TensorFlow.js', 'tfjs', '_web_model', False, False],
        ['PaddlePaddle', 'paddle', '_paddle_model', True, True],]
    return pd.DataFrame(x, columns=['Format', 'Argument', 'Suffix', 'CPU', 'GPU'])


def try_export(inner_func):
    # YOLOv5 export decorator, i..e @try_export
    inner_args = get_default_args(inner_func)

    def outer_func(*args, **kwargs):
        prefix = inner_args['prefix']
        try:
            with Profile() as dt:
                f, model = inner_func(*args, **kwargs)
            LOGGER.info(f'{prefix} export success ✅ {dt.t:.1f}s, saved as {f} ({file_size(f):.1f} MB)')
            return f, model
        except Exception as e:
            LOGGER.info(f'{prefix} export failure ❌ {dt.t:.1f}s: {e}')
            return None, None

    return outer_func


@try_export
def export_torchscript(model, im, file, optimize, prefix=colorstr('TorchScript:')):
    # YOLOv5 TorchScript model export
    LOGGER.info(f'\n{prefix} starting export with torch {torch.__version__}...')
    f = file.with_suffix('.torchscript')

    ts = torch.jit.trace(model, im, strict=False)
    d = {'shape': im.shape, 'stride': int(max(model.stride)), 'names': model.names}
    extra_files = {'config.txt': json.dumps(d)}  # torch._C.ExtraFilesMap()
    if optimize:  # https://pytorch.org/tutorials/recipes/mobile_interpreter.html
        optimize_for_mobile(ts)._save_for_lite_interpreter(str(f), _extra_files=extra_files)
    else:
        ts.save(str(f), _extra_files=extra_files)
    return f, None


@try_export
def export_onnx(model, im, file, opset, dynamic, simplify, prefix=colorstr('ONNX:')):
    # YOLOv5 ONNX export
    check_requirements('onnx>=1.12.0')
    import onnx

    LOGGER.info(f'\n{prefix} starting export with onnx {onnx.__version__}...')
    f = file.with_suffix('.onnx')

    output_names = ['output0', 'output1'] if isinstance(model, SegmentationModel) else ['output0']
    if dynamic:
        dynamic = {'images': {0: 'batch', 2: 'height', 3: 'width'}}  # shape(1,3,640,640)
        if isinstance(model, SegmentationModel):
            dynamic['output0'] = {0: 'batch', 1: 'anchors'}  # shape(1,25200,85)
            dynamic['output1'] = {0: 'batch', 2: 'mask_height', 3: 'mask_width'}  # shape(1,32,160,160)
        elif isinstance(model, DetectionModel):
            dynamic['output0'] = {0: 'batch', 1: 'anchors'}  # shape(1,25200,85)

    torch.onnx.export(
        model.cpu() if dynamic else model,  # --dynamic only compatible with cpu
        im.cpu() if dynamic else im,
        f,
        verbose=False,
        opset_version=opset,
        do_constant_folding=True,  # WARNING: DNN inference with torch>=1.12 may require do_constant_folding=False
        input_names=['images'],
        output_names=output_names,
        dynamic_axes=dynamic or None)

    # Checks
    model_onnx = onnx.load(f)  # load onnx model
    onnx.checker.check_model(model_onnx)  # check onnx model

    # Metadata
    d = {'stride': int(max(model.stride)), 'names': model.names}
    for k, v in d.items():
        meta = model_onnx.metadata_props.add()
        meta.key, meta.value = k, str(v)
    onnx.save(model_onnx, f)

    # Simplify
    if simplify:
        try:
            cuda = torch.cuda.is_available()
            check_requirements(('onnxruntime-gpu' if cuda else 'onnxruntime', 'onnx-simplifier>=0.4.1'))
            import onnxsim

            LOGGER.info(f'{prefix} simplifying with onnx-simplifier {onnxsim.__version__}...')
            model_onnx, check = onnxsim.simplify(model_onnx)
            assert check, 'assert check failed'
            onnx.save(model_onnx, f)
        except Exception as e:
            LOGGER.info(f'{prefix} simplifier failure ❌ {e}')
    return f, None

export.py是一个用于将YOLOv5 PyTorch模型导出为其他格式的程序文件。它支持导出为多种格式,包括TorchScript、ONNX、OpenVINO、TensorRT、CoreML、TensorFlow SavedModel、TensorFlow GraphDef、TensorFlow Lite、TensorFlow Edge TPU、TensorFlow.js和PaddlePaddle等。你可以使用命令行参数来指定要导出的格式。该程序还提供了一个try_export装饰器,用于捕获导出过程中的异常,并提供相应的错误提示。

5.2 loss.py

class CrossEntropyLoss:
    def forward(self, predicted: Tensor, target: Tensor) -> Tensor:
        """
        Computes the cross entropy loss between predicted and target

        predicted: (batch_size, n_classes) raw, un-normalized scores for each class
        target: range [0, n_classes)
        """
        batch_size, n_classes = predicted.shape

        predicted = predicted.logsoftmax()
        target = to_one_hot(target, n_classes)

        return (-target * predicted).mean()

这个程序文件名为loss.py,它定义了一个Loss类和一个CrossEntropyLoss类。Loss类是一个抽象基类,它有一个forward方法用于计算损失,但需要子类实现具体的计算逻辑。CrossEntropyLoss类继承自Loss类,它实现了forward方法,用于计算预测值和目标值之间的交叉熵损失。

具体来说,CrossEntropyLoss的forward方法接受两个参数:predicted和target。predicted是一个(batch_size, n_classes)形状的张量,表示每个类别的原始、未归一化的分数。target是一个取值范围为[0, n_classes)的张量,表示目标类别。

在forward方法中,首先获取predicted的形状(batch_size, n_classes),然后对predicted进行logsoftmax操作,得到归一化的预测概率。接着,使用to_one_hot函数将target转换为一个与predicted形状相同的独热编码张量。最后,计算交叉熵损失,即将target与predicted相乘并取负数,然后求平均值。

这个程序文件提供了一个通用的损失函数框架,可以根据需要继承Loss类并实现自定义的损失计算逻辑。而CrossEntropyLoss类则是一个具体的交叉熵损失函数的实现。

5.3 model.py

class ModelProfiler:
    def __init__(self, model_name, input_shape):
        self.model_name = model_name
        self.input_shape = input_shape
        self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
        self.model = None

    def load_model(self):
        self.model = timm.create_model(self.model_name, pretrained=False, features_only=True)
        self.model.to(self.device)
        self.model.eval()

    def print_model_info(self):
        print(self.model.feature_info.channels())
        for feature in self.model(self.dummy_input):
            print(feature.size())

    def profile_model(self):
        flops, params = profile(self.model.to(self.device), (self.dummy_input,), verbose=False)
        flops, params = clever_format([flops * 2, params], "%.3f")
        print('Total FLOPS: %s' % (flops))
        print('Total params: %s' % (params))

    def run(self):
        self.load_model()
        self.print_model_info()
        self.profile_model()


这个程序文件名为model.py,主要功能是使用torch和timm库来计算一个模型的FLOPS和参数数量。

首先,程序会导入torch、timm和thop库。然后,使用timm库的list_models()函数列出所有可用的模型,并将它们打印出来。

接下来,程序会判断当前是否有可用的GPU,如果有则使用cuda设备,否则使用cpu设备。然后,创建一个随机输入张量dummy_input,并将其发送到设备上。

然后,程序会使用timm库的create_model()函数创建一个名为’tinynet_a’的模型,该模型不使用预训练权重,并且只返回特征。然后,将模型发送到设备上,并将其设置为评估模式。

接下来,程序会打印模型的特征通道数,然后遍历模型对dummy_input的输出,并打印每个输出的大小。

最后,程序会使用thop库的profile()函数来计算模型在给定输入上的FLOPS和参数数量,并使用clever_format()函数将其格式化为易读的形式。然后,打印出总的FLOPS和参数数量。

总的来说,这个程序文件的功能是列出可用的模型,计算一个特定模型的FLOPS和参数数量,并打印出来。

5.4 ops_cpu.py


class ReLU(Function):
    @staticmethod
    def forward(ctx: Context, x: Tensor) -> Tensor:
        ctx.save_for_backward(x)
        return Tensor(np.maximum(0, x.data), requires_grad=x.requires_grad)

    @staticmethod
    def backward(ctx: Context, grad_output: Tensor) -> Tensor:
        x, = ctx.saved_tensors
        grad = unbroadcast(grad_output.data * (x.data >= 0), x.shape)
        return Tensor(grad)

class Exp(Function):
    @staticmethod
    def forward(ctx: Context, x: Tensor) -> Tensor:
        ctx.save_for_backward(x)
        return Tensor(np.exp(x.data), requires_grad=x.requires_grad)

    @staticmethod
    def backward(ctx: Context, grad_output: Tensor) -> Tensor:
        x, = ctx.saved_tensors
        grad = unbroadcast(grad_output.data * np.exp(x.data), x.shape)
        return Tensor(grad)

class Log(Function):
    @staticmethod
    def forward(ctx: Context, x: Tensor) -> Tensor:
        ctx.save_for_backward(x)
        return Tensor(np.log(x.data), requires_grad=x.requires_grad)

    @staticmethod
    def backward(ctx: Context, grad_output: Tensor) -> Tensor:
        x, = ctx.saved_tensors
        grad = unbroadcast(grad_output.data / x.data, x.shape)
        return Tensor(grad)

class Softmax(Function):
    @staticmethod
    def forward(ctx: Context, x: Tensor) -> Tensor:
        e_x = np.exp(x.data - np.max(x.data, axis=1, keepdims=True))
        softmax = e_x / np.sum(e_x, axis=1, keepdims=True)
        softmax = Tensor(softmax, requires_grad=x.requires_grad)
        ctx.save_for_backward(softmax)
        return softmax

    @staticmethod
    def backward(ctx: Context, grad_output: Tensor) -> Tensor:
        softmax, = ctx.saved_tensors

        def softmax_vector_derivative(softmax: np.ndarray, grad_output: np.ndarray) -> np.ndarray:
            shape = softmax.shape
            softmax = np.reshape(softmax, (1, -1))
            grad_output = np.reshape(grad_output, (1, -1))
            d_softmax = softmax * np.identity(softmax.size) - softmax.T @ softmax
            return (grad_output @ d_softmax).reshape(shape)

        grad = np.empty_like(softmax.data)
        for i in range(softmax.shape[0]):
            grad[i] = softmax_vector_derivative(softmax.data[i], grad_output.data[i])

        return Tensor(grad)

class LogSoftmax(Function):
    @staticmethod
    def forward(ctx: Context, x: Tensor) -> Tensor:
        b = np.max(x.data, axis=1)
        softmax = x.data - (b + np.log(np.sum(np.exp(x.data - b[:, np.newaxis]), axis=1))).reshape((-1, 1))
        softmax = Tensor(softmax, requires_grad=x.requires_grad)
        ctx.save_for_backward(softmax)
        return softmax

    @staticmethod
    def backward(ctx: Context, grad_output: Tensor) -> Tensor:
        softmax, = ctx.saved_tensors
        grad = grad_output.data - np.exp(softmax.data) * grad_output.data.sum(axis=1, keepdims=True)
        return Tensor(grad)

class Add(Function):
    @staticmethod
    def forward(ctx: Context, x: Tensor, y: Tensor) -> Tensor:
        ctx.save_for_backward(x, y)
        requires_grad = x.requires_grad or y.requires_grad
        return Tensor(x.data + y.data, requires_grad=requires_grad)

    @staticmethod
    def backward(ctx: Context, grad_output: Tensor) -> tuple[Tensor, Tensor]:
        x, y = ctx.saved_tensors
        grad_x = unbroadcast(np.ones(x.shape) * grad_output.data, x.shape)
        grad_y = unbroadcast(np.ones(y.shape) * grad_output.data, y.shape)
        return Tensor(grad_x), Tensor(grad_y)

class Sub(Function):
    @staticmethod
    def forward(ctx: Context, x: Tensor, y: Tensor) -> Tensor:
        ctx.save_for_backward(x, y)
        requires_grad = x.requires_grad or y.requires_grad
        return Tensor(x.data - y.data, requires_grad=requires_grad)

    @staticmethod
    def backward(ctx: Context, grad_output: Tensor) -> tuple[Tensor, Tensor]:
        x, y = ctx.saved_tensors
        grad_x = unbroadcast(np.ones(x.shape) * grad_output.data, x.shape)
        grad_y = unbroadcast(-np.ones(y.shape) * grad_output.data, y.shape)
        return Tensor(grad_x), Tensor(grad_y)

class Pow(Function):
    @staticmethod
    def forward(ctx: Context, x: Tensor, y: Tensor) -> Tensor:
        ctx.save_for_backward(x, y)
        requires_grad = x.requires_grad or y.requires_grad
        return Tensor(x.data ** y.data, requires_grad=requires_grad)

    @staticmethod
    def backward(ctx: Context, grad_output: Tensor) -> tuple[Tensor, Tensor]:
        x, y = ctx.saved_tensors
        x = x.data
        y = y.data
        grad_x = Tensor(y * (x ** (y - 1)) * grad_output.data)
        grad_y = Tensor(x ** y * np.log(x) * grad_output.data)
        return grad_x, grad_y

class Mul(Function):
    @staticmethod
    def forward(ctx: Context, x: Tensor, y: Tensor) -> Tensor:
        ctx.save_for_backward(x, y)
        requires_grad = x.requires_grad or y.requires_grad
        return Tensor(x.data * y.data, requires_grad=requires_grad)

    @staticmethod
    def backward(ctx: Context, grad_output: Tensor) -> tuple[Tensor, Tensor]:
        x, y = ctx.saved_tensors
        grad_x = unbroadcast(y.data * grad_output.data, x.shape)
        grad_y = unbroadcast(x.data * grad_output.data, y.shape)
        return Tensor(grad_x), Tensor(grad_y)

class Matmul(Function):
    @staticmethod
    def forward(ctx: Context, x: Tensor, y: Tensor) -> Tensor:
        ctx.save_for_backward(x, y)
        requires_grad = x.requires_grad or y.requires_grad
        return Tensor(x.data @ y.data, requires_grad=requires_grad)

    @staticmethod
    def backward(ctx: Context, grad_output: Tensor) -> tuple[Tensor, Tensor]:
        x, y = ctx.saved_tensors
        grad_x = unbroadcast(grad_output.data @ y.data.T, x.shape)
        grad_y = unbroadcast(x.data.T @ grad_output.data, y.shape)
        return Tensor(grad_x), Tensor(grad_y)

class Sum(Function):
    @staticmethod
    def forward(ctx: Context, x: Tensor, axis: int | tuple[int, ...] = None, keepdims: bool = False) -> Tensor:
        ctx.input_shape = x.shape
        ctx.axis = axis if isinstance(axis, (tuple, list)) or axis is None else [axis]
        ctx.keepdims = keepdims
        out = np.sum(x.data, axis=axis, keepdims=keepdims)
        return Tensor(out if keepdims else np.squeeze(out), requires_grad=x.requires_grad)

    @staticmethod
    def backward(ctx: Context, grad_output: Tensor) -> Tensor:
        if ctx.keepdims or ctx.axis is None:
            return Tensor(np.broadcast_to(grad_output.data, ctx.input_shape))
        shape = [1 if i in ctx.axis else ctx.input_shape[i] for i in range(len(ctx.input_shape))]
        return Tensor(np.broadcast_to(grad_output.data.reshape(shape), ctx.input_shape))

class Max(Function):
    @staticmethod
    def forward(ctx: Context, x: Tensor, axis: int  = None, keepdims: bool = False) -> Tensor:
        ctx.input = x.data
        ctx.input_shape = x.shape
        ctx.axis = axis if axis is None else [axis]
        ctx.keepdims = keepdims
        out = np.max(x.data, axis=axis, keepdims=keepdims)
        ctx.out = out
        return Tensor(out if keepdims else np.squeeze(out), requires_grad=x.requires_grad)

    @staticmethod
    def backward(ctx: Context, grad_output: Tensor) -> Tensor:
        max_pos = (ctx.out == ctx.input).astype(np.float32)
        return Tensor(max_pos * grad_output.data)

class Reshape(Function):
    @staticmethod
    def forward(ctx: Context, x: Tensor, shape: tuple) -> Tensor:
        ctx.save_for_backward(x)
        return Tensor(x.data.reshape(shape), requires_grad=x.requires_grad)

    @staticmethod
    def backward(ctx: Context, grad_output: Tensor) -> Tensor:
        x, = ctx.saved_tensors
        return Tensor(grad_output.data.reshape(x.shape))

class Permute(Function):
    @staticmethod
    def forward(ctx: Context, x: Tensor, axis: tuple) -> Tensor:
        ctx.axis = axis
        return Tensor(np.transpose(x.data, axis), requires_grad=x.requires_grad)

    @staticmethod
    def backward(ctx: Context, grad_output: Tensor) -> Tensor:
        return Tensor(np.transpose(grad_output.data, np.argsort(ctx.axis).tolist()))

这个程序文件是一个用于定义操作(ops)的模块,文件名为ops_cpu.py。这个模块中定义了一些常见的操作函数,包括一元操作(unary ops)、二元操作(binary ops)、归约操作(reduce ops)和移动操作(movement ops)等。

其中,一元操作包括ReLU、Exp、Log、Softmax和LogSoftmax等函数,这些函数分别实现了ReLU函数、指数函数、对数函数、Softmax函数和LogSoftmax函数的前向传播和反向传播过程。

二元操作包括Add、Sub、Pow、Mul和Matmul等函数,这些函数分别实现了加法、减法、乘法、幂运算和矩阵乘法的前向传播和反向传播过程。

归约操作包括Sum和Max等函数,这些函数分别实现了求和和求最大值的操作。

移动操作包括Reshape和Permute等函数,这些函数分别实现了改变张量形状和改变维度顺序的操作。

这些操作函数都是通过继承自Function类,并注册到Function类的函数注册表中,以便在计算图中使用。

此外,还定义了一个辅助函数unbroadcast,用于处理梯度的广播操作。

总的来说,这个程序文件是一个用于定义各种操作函数的模块,可以用于构建计算图并进行前向传播和反向传播计算。

5.5 ops_gpu.py



def new_cl_buffer(ctx: cl.Context, output_shape: tuple[int, ...]) -> cl._cl.Buffer:
    buffer = cl.Buffer(ctx, cl.mem_flags.WRITE_ONLY, np.prod(output_shape) * 4)
    buffer.shape = output_shape

    return buffer


class ReLU(Function):
    @staticmethod
    def forward(ctx: Context, x: Tensor) -> Tensor:
        ctx.save_for_backward(x)

        ret = new_cl_buffer(ctx.cl_ctx, x.shape)
        prg = cl.Program(ctx.cl_ctx, """
            __kernel void relu(__global float* x, __global float* ret) {
                int g_id = get_global_id(0);
                float a = x[g_id];
                ret[g_id] = max(a, (float)0.);
            }
        """).build()
        prg.relu(ctx.cl_queue, (ret.size,), None, x.data, ret)

        return Tensor(ret, requires_grad=x.requires_grad)

    @staticmethod
    def backward(ctx: Context, grad_output: Tensor) -> Tensor:
        x, = ctx.saved_tensors

        ret = new_cl_buffer(ctx.cl_ctx, x.shape)
        prg = cl.Program(ctx.cl_ctx, """
            __kernel void relu(__global float* x, __global float* grad_output, __global float* ret) {
                int g_id = get_global_id(0);
                ret[g_id] = grad_output[g_id] * (float)(x >= 0);
            }
        """).build()
        prg.relu(ctx.cl_queue, (ret.size,), None, x.data, grad_output.data, ret)

        return Tensor(ret)


class Exp(Function):
    @staticmethod
    def forward(ctx: Context, x: Tensor) -> Tensor:
        ctx.save_for_backward(x)

        ret = new_cl_buffer(ctx.cl_ctx, x.shape)
        prg = cl.Program(ctx.cl_ctx, """
            __kernel void exp(__global float* x, __global float* ret) {
                int g_id = get_global_id(0);
                ret[g_id] = exp(x[g_id]);
            }
        """).build()
        prg.exp(ctx.cl_queue, (ret.size,), None, x.data, ret)

        return Tensor(ret, requires_grad=x.requires_grad)

    @staticmethod
    def backward(ctx: Context, grad_output: Tensor) -> Tensor:
        x, = ctx.saved_tensors

        ret = new_cl_buffer(ctx.cl_ctx, x.shape)
        prg = cl.Program(ctx.cl_ctx, """
            __kernel void exp(__global float* x, __global float* grad_output, __global float* ret) {
                int g_id = get_global_id(0);
                ret[g_id] = grad_output[g_id] * exp(x[g_id]);
            }
        """).build()
        prg.exp(ctx.cl_queue, (ret.size,), None, x.data, grad_output.data, ret)

        return Tensor(ret)


def determine_shape(x_shape: tuple[int, ...], y_shape: tuple[int, ...]) -> tuple[int, ...]:
    return np.broadcast_shapes(x_shape, y_shape)


def binary_op(ctx: Context, name: str, op_fn: str, x: Tensor, y: Tensor) -> Tensor:
    ret = new_cl_buffer(ctx.cl_ctx, determine_shape(x.shape, y.shape))
    prg = cl.Program(ctx.cl_ctx, f"""
        __kernel void {name}(__global const float *x, __global const float *y, __global float *ret) {{
            int g_id = get_global_id(0);
            ret[g_id] = x[g_id] {op_fn} y[g_id]; 
        }}
    """).build()
    prg.__getattr__(name)(ctx.cl_queue, (ret.size // 4,), None, x.data, y.data, ret)
    requires_grad = x.requires_grad or y.requires_grad

    return Tensor(ret, requires_grad=requires_grad)


class Add(Function):
    @staticmethod
    def forward(ctx: Context, x: Tensor, y: Tensor) -> Tensor:
        return binary_op(ctx, "add", "+", x, y)

    @staticmethod
    def backward(ctx: Context, grad_output: Tensor) -> tuple[Tensor, Tensor]:
        return grad_output, grad_output


class Sub(Function):
    @staticmethod
    def forward(ctx: Context, x: Tensor, y: Tensor) -> Tensor:
        return binary_op(ctx, "sub", "-", x, y)

    @staticmethod
    def backward(ctx: Context, grad_output: Tensor) -> tuple[Tensor, Tensor]:
        return grad_output, -grad_output


class Mul(Function):
    @staticmethod
    def forward(ctx: Context, x: Tensor, y: Tensor) -> Tensor:
        return binary_op(ctx, "mul", "*", x, y)

    @staticmethod
    def backward(ctx: Context, grad_output: Tensor) -> tuple[Tensor, Tensor]:
        return grad_output, grad_output


class Sum(Function):  # only fully reduced sum for now
    @staticmethod
    def forward(ctx: Context, x: Tensor, axis=None, keepdims=False) -> Tensor:
        ctx.save_for_backward(x)

        ret = new_cl_buffer(ctx.cl_ctx, (1,))
        prg = cl.Program(ctx.cl_ctx, """
            __kernel void sum(__global const float* x, __global float* ret) {
                int g_id = get_global_id(0);
                ret[g_id] += x[g_id];
            }
        """).build()
        prg.sum(ctx.cl_queue, (ret.size // 4,), None, x.data, ret)

        return Tensor(ret, requires_grad=x.requires_grad)

    @staticmethod
    def backward(ctx: Context, grad_output: Tensor) -> Tensor:
        x, = ctx.saved_tensors

        ret = new_cl_buffer(ctx.cl_ctx, x.shape)
        prg = cl.Program(ctx.cl_ctx, """
            __kernel void sum(__global const float* x, __global const float* grad_output, __global float* ret) {
                int g_id = get_global_id(0);
                ret[g_id] = grad_output[0] * (float)1.;
            }
        """).build()
        prg.sum(ctx.cl_queue, (ret.size // 4,), None, x.data, grad_output.data, ret)

        return Tensor(ret)

这个程序文件是一个用于在GPU上执行张量操作的Python模块。它包含了一些常见的张量操作,如ReLU、指数函数、加法、减法、乘法和求和。该模块使用了pyopencl库来进行GPU计算。

该模块定义了一个名为ops_gpu.py的文件,其中包含了一些函数和类。这些函数和类都是基于tinynet库中的TensorFunction类进行扩展的。

其中,new_cl_buffer函数用于创建一个新的OpenCL缓冲区,用于存储计算结果。ReLUExp类分别实现了ReLU函数和指数函数的前向传播和反向传播。AddSubMul类分别实现了加法、减法和乘法的前向传播和反向传播。Sum类实现了求和操作的前向传播和反向传播。

这些类的前向传播方法使用OpenCL编写的内核函数来执行计算,并将结果存储在新创建的OpenCL缓冲区中。反向传播方法使用相同的内核函数来计算梯度,并将结果存储在新创建的OpenCL缓冲区中。

总之,这个程序文件是一个用于在GPU上执行张量操作的模块,它使用了pyopencl库来进行GPU计算,并提供了一些常见的张量操作的实现。

6.系统整体结构

根据以上分析,该程序是一个用于改进YOLOv5模型的胡萝卜表面缺陷分类系统。它包含了许多不同的模块和文件,每个文件都有不同的功能,共同构建了整个系统。

下面是每个文件的功能的整理:

文件功能
constants.py定义了一些常量
export.py导出模型的功能
function.py定义和应用自定义函数的功能
loss.py定义损失函数的功能
model.py定义模型的功能
ops_cpu.py在CPU上执行张量操作的功能
ops_gpu.py在GPU上执行张量操作的功能
optim.py定义优化器的功能
tensor.py定义张量操作的功能
train.py训练模型的功能
ui.py用户界面的功能
utils.py提供一些通用的工具函数
val.py验证模型的功能
yolo.pyYOLO模型的功能
__init__.py初始化文件
classify/predict.py分类模型的预测功能
classify/train.py分类模型的训练功能
classify/val.py分类模型的验证功能
models/common.py通用模型定义的功能
models/experimental.py实验性模型定义的功能
models/tf.pyTensorFlow模型定义的功能
models/yolo.pyYOLO模型定义的功能
models/__init__.py模型初始化文件
nn/activations.py激活函数的功能
nn/linear.py线性层的功能
nn/module.py模块定义的功能
nn/sequential.py顺序模型定义的功能
nn/__init__.py模型初始化文件
segment/predict.py分割模型的预测功能
segment/train.py分割模型的训练功能
segment/val.py分割模型的验证功能
utils/activations.py激活函数的功能
utils/augmentations.py数据增强的功能
utils/autoanchor.py自动锚框的功能
utils/autobatch.py自动批处理的功能
utils/callbacks.py回调函数的功能
utils/dataloaders.py数据加载器的功能
utils/downloads.py下载功能
utils/general.py通用功能
utils/loss.py损失函数的功能
utils/metrics.py评估指标的功能
utils/plots.py绘图功能
utils/torch_utils.pyPyTorch工具函数的功能
utils/triton.pyTriton Inference Server的功能
utils/__init__.py工具函数初始化文件
utils/aws/resume.pyAWS恢复功能
utils/aws/__init__.pyAWS初始化文件
utils/flask_rest_api/example_request.pyFlask REST API示例请求的功能
utils/flask_rest_api/restapi.pyFlask REST API的功能
utils/loggers/__init__.py日志记录器初始化文件
utils/loggers/clearml/clearml_utils.pyClearML日志记录器的功能
utils/loggers/clearml/hpo.pyClearML超参数优化的功能
utils/loggers/comet/comet_utils.pyComet日志记录器的功能
utils/loggers/comet/hpo.pyComet超参数优化的功能
utils/loggers/wandb/wandb_utils.pyWandB日志记录器的功能
utils/loggers/wandb/__init__.pyWandB初始化文件
utils/segment/augmentations.py分割模型的数据增强功能
utils/segment/dataloaders.py分割模型的数据加载器功能
utils/segment/general.py分割模型的通用功能
utils/segment/loss.py分割模型的损失函数功能
utils/segment/metrics.py分割模型的评估指标功能
utils/segment/plots.py分割模型的绘图功能
utils/segment/__init__.py分割模型初始化文件

这个表格整理了每个文件的功能,以便更好地理解整个程序的结构和功能。

7.TinyNet-BackBone改进YOLOv5

TinyNets是一种轻量级神经网络结构,由华为诺亚方舟实验室、北京邮电大学以及香港科技大学的研究者们提出。相比于EfficientNet的复合放缩范式(compound scaling),通过TinyNet范式得到的模型在ImageNet上的精度要优于相似计算量(FLOPs)的EfficientNet模型 。

TinyNets提供了一种策略和可实现的软件堆栈,可以让机器人学家将适应性强、实时性强的网络应用于高度互联和复杂的机器人系统。在“基于TinyNet-BackBone改进YOLOv5的胡萝卜表面缺陷分类系统”中,我们可以使用TinyNets作为骨干网络来改进YOLOv5。

具体来说,我们可以使用TinyNets中的一些基本模块来构建我们的骨干网络。这些基本模块包括卷积层、激活函数、池化层等。我们可以根据需要选择合适的模块来构建我们的骨干网络。例如,如果我们需要提取更多的特征信息,我们可以增加卷积层的深度;如果我们需要减少计算量,我们可以减少卷积层的深度或者使用更小的卷积核大小。

此外,我们还可以使用TinyNets中的一些高级模块来进一步优化我们的骨干网络。这些高级模块包括注意力机制、分组卷积等。这些高级模块可以帮助我们更好地利用输入数据中的信息,从而提高我们的分类性能。

总之,在“基于TinyNet-BackBone改进YOLOv5的胡萝卜表面缺陷分类系统”中,我们可以充分利用TinyNets的优势来构建一个高效、准确的骨干网络,从而帮助我们更好地完成胡萝卜表面缺陷分类任务。

8.训练结果可视化分析

评价指标

epoch:表示训练周期。
train/loss:每个 epoch 在训练期间记录的损失。
test/loss:每个时期测试期间记录的损失。
metrics/accuracy_top1:Top-1 准确率指标,通常表示模型最可能预测的准确度。
metrics/accuracy_top5:前 5 名准确率指标,指示真实标签在模型的前 5 名预测中的频率。
lr/0:每个时期的学习率。

结果可视化

为了可视化这些数据并执行详细分析,我将:

可视化趋势:绘制跨周期的训练和测试损失,并绘制跨跨周期的前 1 和前 5 准确率指标。
数据分析:
分析损失和准确性的趋势,例如随时间推移的改进或下降。
检查学习率和性能指标之间的关系。
识别数据中的任何异常或值得注意的模式。

import matplotlib.pyplot as plt

# Setting up the plots
fig, axs = plt.subplots(3, 1, figsize=(12, 18))

# Plotting train and test loss
axs[0].plot(data['epoch'], data['train/loss'], label='Train Loss', color='blue')
axs[0].plot(data['epoch'], data['test/loss'], label='Test Loss', color='red')
axs[0].set_title('Training and Testing Loss Over Epochs')
axs[0].set_xlabel('Epoch')
axs[0].set_ylabel('Loss')
axs[0].legend()

# Plotting top-1 and top-5 accuracy
axs[1].plot(data['epoch'], data['metrics/accuracy_top1'], label='Top-1 Accuracy', color='green')
axs[1].plot(data['epoch'], data['metrics/accuracy_top5'], label='Top-5 Accuracy', color='orange')
axs[1].set_title('Top-1 and Top-5 Accuracy Over Epochs')
axs[1].set_xlabel('Epoch')
axs[1].set_ylabel('Accuracy')
axs[1].legend()

# Plotting learning rate
axs[2].plot(data['epoch'], data['lr/0'], label='Learning Rate', color='purple')
axs[2].set_title('Learning Rate Over Epochs')
axs[2].set_xlabel('Epoch')
axs[2].set_ylabel('Learning Rate')
axs[2].legend()

# Displaying the plots
plt.tight_layout()
plt.show()

在这里插入图片描述
现在显示可视化效果,显示训练和测试损失、前 1 和前 5 准确率以及跨时期的学习率的趋势。让我们继续对这些结果进行详细分析:

训练和测试损失分析

趋势观察:该图显示了训练和测试损失值在各个时期内的变化情况。理想的趋势是两者都减少,表明学习和泛化。
过拟合迹象:如果训练损失减少,而测试损失增加或停滞,则可能表示过拟合,即模型在训练数据上表现良好,但在新的、看不见的数据上表现不佳。
最小测试损耗的周期:确定测试损耗最小的时期对于模型选择至关重要。

Top-1 和 Top-5 准确性分析

随时间推移的改进:理想情况下,这两种精度都应随时间推移而增加,表明模型正在有效地学习对数据进行分类。
Top-1 与 Top-5:这两者之间的显着差异可以表明模型对其预测的置信度。前 5 名准确率较高,前 1 名准确率较低,这可能表明正确的预测通常在前 5 名猜测范围内,但并不总是最高猜测。
停滞或下降:准确性指标的任何停滞或下降都可能表明学习过程存在问题或需要超参数调整。

学习率趋势分析

对学习的影响:学习率会显著影响模型的学习方式。非常高的学习率会导致模型过快收敛到次优解,而非常低的学习率会减慢学习过程。
跨纪元的调整:观察学习率随纪元的变化可以提供对训练过程的见解。例如,随着时间推移降低速率的学习速率计划通常用于在模型接近优化时微调学习。

9.系统整合

下图[完整源码&数据集&环境部署视频教程&自定义UI界面]
在这里插入图片描述

参考博客《基于TinyNet-BackBone改进YOLOv5的胡萝卜表面缺陷分类系统》

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

群马视觉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值