第一章 实验室的99%:深度学习落地的第一道鸿沟
2021年10月17日上午9点27分,深圳宝安区某3C制造企业的SMT产线质检工位传来刺耳的警报声,刚上线3小时的AI质检系统被紧急停用,传送带上堆积的1200多台待检主板在振动台上嗡嗡作响,质检员李姐摘掉防静电手环,指着工控机的屏幕吐槽:“这什么破AI,连我肉眼都能看出来的装配错误都漏检,之前吹的99%准确率是吹出来的吧?”
<!-- 标签:实验生产鸿沟 故障场景 工业质检 -->
Three days earlier, at 9:27 a.m. on October 14, 2021, the same production line was filled with the sound of popping champagne corks. Lin Shen, head of the AI team, stood in front of a 100-inch display showing the final test report of the quality inspection model: 99.2% accuracy for 12 types of defects including scratches, dents and assembly errors on a test set of 100,000 defect samples, far exceeding the industry average of 91%. The factory director patted Lin Shen on the shoulder and said, “This model will save us at least 2 million yuan a year in quality inspection costs, and it will be rolled out to all production lines next quarter.” No one expected that the “perfect model” would collapse within 3 hours of going online.
The root of the problem was revealed in a 4-hour review meeting that afternoon. The whiteboard on one side was posted with the laboratory test report, and the other side was posted with the fault log sent back from the production line: the inference time of a single image was as high as 2.3 seconds, far lower than the production line’s rhythm requirement of 12 pieces per second; when the light changed or the angle of the product deviated, the accuracy dropped to 72%; a total of 17 defects were missed, 3 of which were serious safety hazards that could cause product short circuits; and 23% of the images were uploaded to a third-party cloud server during inference, violating data compliance requirements. Lin Shen tore the test report off the whiteboard and posted it next to the fault log, saying, “All our previous indicators were measured in a greenhouse, and what we have to pass now is a thunderstorm.”
This is not an isolated case. According to the 2023 White Paper on AI Industry Implementation released by the Ministry of Industry and Information Technology, only 12% of AI projects in China can successfully pass production environment verification, and 68% of failed projects are directly due to the algorithm R&D stage being completely oriented by experimental accuracy and ignoring production environment constraints. The failure rate of AI implementation in discrete manufacturing industries such as 3C, automobiles and semiconductors is as high as 89%, far higher than 35% in Internet and financial industries.
This chapter will deconstruct the four core causes of the “experiment-production gap” of deep learning: environmental differences, performance constraints, stability requirements and compliance needs, and sort out the five core modules of engineering implementation, laying a cognitive foundation for the full-process actual combat in the following chapters.
---
一、实验室的幻象:99%准确率的陷阱 | 1.1 The Illusion of the Lab: The Trap of 99% Accuracy
The 99.2% accuracy reported by Lin Shen’s team is not false, but it is an “ideal indicator” measured under extremely ideal conditions. In order to obtain a high-quality data set, the team spent 3 months building a professional shooting platform in the laboratory: the temperature was controlled at 22℃±1℃, the humidity was 45%±5%, the vertical lightless lamp provided stable 500lux illumination, the industrial camera was fixed on a tripod, the shooting angle of the product was fixed at ±2°, and the background was pure black without any interference. Each sample was photographed 10 times, and the clearest picture was manually selected. The 100,000 samples in the test set were all typical, clear defect samples, and the extreme samples with oil stains covering scratches or small assembly errors were manually filtered out.
This is the common practice of most algorithm teams in the laboratory stage: in order to improve experimental indicators, they will “optimize” the data set as much as possible, filter out noisy samples, and control shooting conditions to be as consistent as possible. This approach is reasonable in the algorithm verification stage, but it also lays a huge hidden danger for subsequent landing. The experimental indicators measured in this way are often “best-case indicators”, which cannot represent the performance of the model in the real production environment.
<!-- 标签:实验室指标陷阱 数据集偏差 -->
This phenomenon has a precise theoretical explanation in machine learning: distribution shift. The data distribution of the laboratory data set $P_{lab}(X,Y)$ is significantly different from the data distribution of the production environment $P_{prod}(X,Y)$, where $X$ represents the input image and $Y$ represents the label. According to the theory of domain adaptation, the generalization error of the model in the target domain can be expressed as:
$$R_{target}(h) \leq R_{source}(h) + d_{\mathcal{H}}(P_{source}, P_{target}) + \lambda$$
Where $R_{source}(h)$ is the error of the model $h$ in the source domain (laboratory), $d_{\mathcal{H}}(P_{source}, P_{target})$ is the distribution distance between the source domain and the target domain, and $\lambda$ is the joint error of the two domains. When the distribution distance is large, even if the error in the source domain is close to 0, the error in the target domain may be very high.
Lin Shen’s team did not measure the distribution distance between the laboratory data set and the production environment data set during the R&D stage. Later, after they collected 10,000 real production line images for testing, they found that the average entropy of the laboratory images was 5.2, while the average entropy of the production line images was 7.8, an increase of 49%. The distribution distance measured by the maximum mean discrepancy (MMD) reached 0.87, far exceeding the safe threshold of 0.2, which means that the model’s performance in the production environment will be significantly degraded.
This is the first core cause of the experiment-production gap: environmental differences. The laboratory environment is a “controlled environment”, while the production environment is a “real-world environment” with various uncertainties. The model trained in the controlled environment often cannot adapt to the complex and changeable production environment.
---
二、性能的紧箍咒:算力、功耗与节拍的博弈 | 1.2 The Tight Curse of Performance: The Game of Computing Power, Power Consumption and Production Rhythm
After the review meeting, Lin Shen’s team first tested the inference speed of the model on the edge industrial control machine actually used on the production line. The result was 2.3 seconds per image, which is 27.7 times the requirement of 0.083 seconds per image (calculated as $T_{req} = \frac{1}{12} \approx 0.083s$, the inverse of the production line rhythm of 12 pieces per second).
Many people will ask: why not use a more powerful industrial control machine? This involves the hard constraints of the industrial scenario. The SMT production line where Lin Shen’s team is located produces high-end mobile phone motherboards, and the inspection station is located at the exit of the reflow soldering furnace, with a space of no more than 0.5 cubic meters. If a high-power industrial control machine is used, its heat dissipation fan will blow dust and soldering fumes onto the motherboard, causing new defects such as short circuits. In addition, the factory’s power distribution for each production station is limited to 15W, and high-power equipment cannot be used. The only edge device that meets the requirements is the Jetson Xavier NX, with a computing power of 21 TOPS and a power consumption of 15W, which is only 1/29.8 of the A100 GPU (625 TOPS) used in the laboratory.
<!-- 标签:性能约束 边缘计算 工业节拍 -->
This is the second core cause of the experiment-production gap: performance constraints. The laboratory stage usually uses high-performance server clusters for model training and inference, without considering the hardware constraints of the edge side. The inference speed meas