$$\rightleftharpoonup{xx}$$
$$\longleftharp{xx}$$,
$$\longrightharp{xx}$$,
The dataset used in this article was acquired from the AS/RS field of the Logistics Department of Longyan Tobacco Industry Co., Ltd. This study did not involve human participants or animals. Ethical approval was not required.
Dataset acquisition and setup
Hardware configuration: Mount an industrial camera (e.g., MV-CA013-A0GM) equipped with an 8 mm focal length lens (e.g., MVL-HF0828M-6MPE) onto the cargo platform of the stacker crane. Connect the camera to a control PC via a GigE cable and a wireless AP device (e.g., BH-ANT5158S-14HV, BH-MS-AC1600HWH). Position a strip LED light (e.g., MV-LLDS-1002-38-W) around the camera to ensure uniform illumination.
Camera parameter setting: Configure the camera using the manufacturer's software (e.g., MVS). Set the acquisition resolution to 1280 x 1024 pixels. Set the trigger mode to Hardware Trigger and synchronize it with the stacker crane's positioning system. Set the exposure time to 100 ms and the gain to 5 dB to minimize motion blur and noise. The working distance between the camera and the cigarette boxes is approximately 550 mm.
Image collection: An industrial camera with a trigger captures an image automatically each time a tray is positioned during the storage and retrieval of cigarette boxes. Collect a total of 4,835 raw images; typical images are shown in Figure 1.
Image annotation: Use the open-source tool LabelImg. For each image, draw bounding boxes around every visible cigarette brand feature. Assign the correct brand name (e.g., Hongzhuang, Gutian) as the class label for each bounding box. Save the annotations in a standard single-stage detection format, with one txt file per image.
Quality control: A second annotator reviews a randomly selected 20% of the annotated images. Any discrepancies are discussed and resolved, ensuring labeling consistency.
Data augmentation strategy
This section details both traditional and advanced augmentation techniques applied to the datasets. The initial data and augmented data are combined into a new dataset, which is then divided into training set, validation set, and test set to ensure fairness in evaluation.
Traditional data augmentation32: These transformations are applied in real-time by the training pipeline (e.g., using the Albumentations or PyTorch Transforms libraries) with a defined probability for each batch.
Geometric transformations: Rotation: Randomly rotate images by an angle between -45 ° and +45°. Scaling: Randomly scale images by a factor between 0.8 and 1.2. Horizontal flip: Randomly flip images horizontally with a 100% probability. Random cropping: Randomly crop a section between 80% and 100% of the original image area.
Photometric transformations: Brightness and Contrast: Adjust brightness and contrast by a factor randomly chosen from [0.6, 1.4]. Gaussian noise: Add Gaussian noise with a standard deviation randomly chosen from [0, 0.01 * 255] to 10% of the images. Gaussian blur: Apply a Gaussian blur with a kernel size of 3x3 to 10% of the images.
Occlusion simulation: Randomly place 1 to 3 rectangular occlusion patches (covering up to 5% of the image area each) on the images. The patches are filled with random noise or mean image pixel values.
Advanced data augmentation: region-specific copy-paste
Motivation and impact: The primary motivation for this targeted augmentation was to address a critical data issue: several cigarette brands had extremely few instances (as low as one image). A standard random train-validation test split could potentially allocate all instances of a rare brand to a single set (e.g., all to the test set), resulting in the model having no opportunity to learn or validate that brand. This method artificially increased the sample size for these underrepresented brands, ensuring that every brand has a sufficient number of instances distributed across the training, validation, and test sets. This foundational step prevents catastrophic failure in rare categories and is a prerequisite for any meaningful model performance and generalization on the complete set of brands.
This step requires a pre-trained segmental baseline model on the initial dataset and the Segment Anything Model (SAM)33.
Segment source objects: For cigarette box images from underrepresented brands, use the SAM model to generate precise segmentation masks. Use the point prompt mode, clicking on the Brand Feature of the Cigarette Box to initiate segmentation. Manually validate and refine generated masks to ensure accuracy. Save the segmented cigarette box images with transparent backgrounds as PNG files. This creates a library of isolated object instances.
Generate background masks: Use the pre-trained segmental baseline model to perform instance segmentation on a set of background images (images with ample free space or simple backgrounds). The model will output binary masks indicating the regions already occupied by objects.
Process masks and paste objects: For each background mask, use the OpenCV library ('cv2.approxPolyDP' function with an epsilon factor of 0.02 * contour perimeter) to perform curve fitting and linearize the mask edges, obtaining a simplified polygon representation of the free space. Identify the largest contiguous polygon area within the background mask as the target paste region. Randomly select a segmented source object from the library. Resize it (maintaining aspect ratio) and apply an affine transformation (minor rotation between -5 ° and +5°, and slight shear) to fit naturally within the target paste region, ensuring it does not overlap with the boundaries of existing objects. Use alpha blending to seamlessly paste the transformed object onto the background image at the calculated location. The overall framework of data augmentation technology based on the copy-paste technique in specific areas is shown in Figure 2. Programmatically generate a corresponding new txt annotation file for the pasted object, updating the bounding box coordinates and class label.
Final augmented dataset: This offline process generates 600 new synthetic images. Combine them with the original 4,835 images and an additional 1,167 images generated by applying the traditional augmentation techniques as described above to form the final dataset of 6,602 images. Split the final dataset into training (70%, 4,621 images), validation (20%, 1,320 images), and test (10%, 661 images) sets, ensuring that images from the same original sequence are kept within the same split to prevent data leakage.
Model modification for constructing the proposed improved detector
Optimized algorithms of object detection34 have achieved notable progress in industrial inspection in recent years. This section provides a detailed, step-by-step procedure for modifying the baseline model architecture to create the proposed model. The modifications are implemented by editing the model's configuration file (e.g., config.yaml) and ensuring that corresponding custom module definitions are included in the codebase. The rationale for each modification is provided to elucidate its role in addressing specific detection challenges. The structure of the proposed model is shown in Figure 3.
Replacement of downsampling modules with the ADown module: The standard Convolution-BatchNorm-SiLU (CBS) module used for downsampling employs a single strided convolution, which can act as an information bottleneck35, potentially discarding fine-grained features crucial for recognizing small cigarette boxes and detailed brand logos. The ADown module is designed to alleviate this by implementing a dual-branch structure that captures and preserves a richer set of features during spatial resolution reduction. One branch prioritizes the retention of high-frequency local details, while the other captures a broader, context-rich overview. The fusion of these complementary features results in a more robust and informative representation for subsequent layers.
Action-implementation steps
Module definition: Ensure that a custom PyTorch module named ADown is defined within the project's model definition files. This module must contain two parallel branches. The first branch should consist of a two-dimensional convolution layer with a 3x3 kernel and a stride of 2. The second branch should sequentially consist of a 2x2 max-pooling layer (with stride 2) followed by a 1x1 convolution layer. The outputs of these two branches are to be concatenated along the channel dimension, and the resulting feature map is then passed through a final 1x1 convolution layer to integrate the channels and produce the output. The structure of the ADown module is shown in Figure 4.
Configuration file editing: Open the baseline model configuration file (config.yaml). Systematically identify every instance of the CBS module that is configured for downsampling (i.e., where the stride parameter is set to 2). Replace each of these CBS module entries with the new ADown module.
Design motivation: The design of ADown is motivated by the need to mitigate information loss in standard strided convolutions, which can be detrimental for small objects. Its dual-branch structure is inspired by the idea of parallel processing for capturing both high-frequency spatial details (via convolution) and low-frequency contextual information (via pooling), thereby providing a richer multi-scale feature representation for subsequent layers.
Integration of the iEMA module
Rationale and design principle: To enhance the model's capability to detect small targets and those that are partially obscured, it is essential to incorporate a mechanism that can effectively model multi-scale spatial context. The iEMA module achieves this by embedding an Efficient Multi-scale Attention (EMA)36 component within an inverted residual block (iRMB)37 structure. The iRMB provides a computationally efficient foundation using depthwise separable convolutions, while the EMA component explicitly captures cross-scale spatial interactions through a parallel multi-branch design. This integration allows the model to dynamically recalibrate feature weights, focusing attention on the most discriminative spatial regions across different scales, thereby improving recognition under occlusion and for small objects.
Action-Implementation steps
Module definition: Ensure that a custom PyTorch module named iEMA is defined. This module should first implement an inverted residual block. This block typically begins with a pointwise convolution for channel expansion, followed by a depthwise convolution (e.g., 3x3) for spatial feature extraction, and finally a pointwise convolution for channel projection. Immediately following this block, the EMA attention mechanism should be implemented. The EMA mechanism typically employs grouped convolutions and cross-dimensional interactions to efficiently generate a multi-scale spatial attention map without excessive computational overhead. The structure of the iEMA module is shown in Figure 5.
Configuration file editing: In the config.yaml configuration file, locate the sections defining the neck network, specifically the layers immediately following the C2f modules. At these strategic locations, insert a new layer that calls the iEMA module.
Design motivation: The iEMA module is founded on the principle of enhancing feature discriminability by integrating local feature extraction (via depth-wise convolution) with a lightweight yet effective global context modeling mechanism (EMA). This hybrid approach allows the model to adaptively focus on informative regions across different scales, which is crucial for recognizing partially visible brand logos and text.
Replacement of the detection head with the DynamicHead module
Rationale and design principle: The original detection head may not optimally handle the significant scale variation of cigarette boxes and the complex interplay between localization and classification tasks. The DynamicHead module addresses this by unifying three forms of attention into a coherent structure: scale-aware, spatial-aware, and task-aware attention. The scale-aware component dynamically fuses features from different levels of the feature pyramid, ensuring that objects of all sizes are represented effectively. The spatial-aware component employs a sparse sampling strategy to focus computational resources on the most informative regions within the feature maps. The task-aware component adapts the feature representation specifically for the distinct objectives of bounding box regression and category classification. This unified approach leads to more accurate and robust predictions.
Action-Implementation steps
Module definition: This is a complex module that encompasses the three attention mechanisms described by Equations (1) through (4). Its internal structure will include layers for computing scale-wise weights, performing deformable spatial attention, and applying task-specific feature transformations.
(1)
(2)
(3)
(4)
Where WL(F) denotes the final attention-refined feature map, obtained by sequentially applying the scale-aware πL(F), spatial-aware πS(F), and task-aware πC(F) attention mechanisms to the input feature F. Specifically, in Eq. (2), πL(F) computes a scale-wise attention weight by first averaging across spatial (S) and channel (C) dimensions, passing it through a linear function f(⋅) and a hard-sigmoid activation σ(⋅). In Eq. (3), πS(F) performs sparse spatial attention by aggregating features from K sampled key points pk, each with a learnable offset Δpk to focus on discriminative regions and an importance scalar Δmk. In Eq. (4), πC(F) implements a task-aware attention by applying a linear transformation (governed by learned parameters (α1, β1,α2,β2)) to each channel and selecting the maximum response, allowing the head to specialize for classification and regression tasks. The structure of the DynamicHead module is shown in Figure 6.
Configuration file editing: In the config.yaml file, find the section that defines the model's head, which originally contains the Detect module. Replace it with a new entry that calls the DynamicHead module.
Design motivation: The DynamicHead module is grounded in the observation that object detection heads should be adaptive to scale, spatial location, and task. Its unified attention framework, formalized in Eqs. (1-4), allows the model to dynamically recalibrate feature responses based on these three dimensions, leading to more robust predictions against scale variation and background clutter.
Model training
Ensure PyTorch 2.1.0, CUDA 12.1, and all dependencies are installed.
Training command
Before retraining, prepare the divided image data and the annotation data in a standard single-stage detection format. Create a .yaml file containing the image path and data category. According to the model improvement, the original detector .yaml file is replaced by the proposed model .yaml file. Write the train.py file, import the single-stage detector package, load the training model and data path, and set some training parameters, including imgze=640, epochs=100, batch=4, workers=0, device='0', optimizer='SGD', close_mosaic=10, etc.
Hyperparameters: The key parameters are: input image size (640 x 640), batch size (4), initial learning rate (0.01) with cosine annealing scheduler, SGD optimizer with momentum (0.937), and weight decay (0.0005). Mosaic augmentation is enabled by default.
Training monitoring: The training process will output metrics for each epoch. Monitor the curves for training/validation loss and mAP at 0.5 to ensure convergence and avoid overfitting. Training for 100 epochs is typically sufficient.
Model evaluation and deployment
Evaluation: After training, the best model is saved as runs/train/exp/weights/best.pt. Evaluate it on the val set using: bash python val.py --data cigarette_dataset.yaml --weights runs/train/exp/weights/best.pt. The script will output Precision, Recall, mAP at 0.5, etc. Confirm the mAP meets the required threshold (e.g., 97.9%).
Inference for verification: Run inference on sample images to visually verify detection results: bash python detect.py --weights runs/train/exp/weights/best.pt --source path/to/test/images --conf 0.5. By verifying the reasoning, the detection results of each image and the detection speed of the model can be obtained.
Deployment: For real-time deployment on the stacker crane's system, convert the PyTorch model (best.pt, ~4.7 MB) to a format like TensorRT or ONNX for optimized inference speed. The final output is bounding boxes with class labels (brand names) and confidence scores.