在工作中我遇到了以下机器学习问题: 我的产品正在对半导体元件进行检查。产品将从组件经过的机器接收以下输入。机器将为我提供:
请注意,即使检查图像具有理想的形状和状态,检查图像和模板也不可能完全相同。这是因为拍摄这些图像时相机移动非常常见且无法避免
我想训练一个模型,它可以将模板+检查的图像作为输入,并能够将这两个图像之间的差异绑定到检查的图像上。我想要检测的差异是特定的,仅与材料变化相关 - 而不是其他差异,例如颜色变化 - 在检查的图像上有额外的材料,如额外的焊料或组件中的裂纹。
什么样的模型/架构可以用于解决这个问题?以及如何选择此类问题的损失函数?
我尝试了 Siamese 网络,但在我的情况下它有 2 个缺陷:
另外,当我尝试一些图像处理库来检测差异时,结果很幼稚,因为在生产线上,由于相机移动,模板和检查的图像不可能完全相同。
连体网络是一种流行的变化检测架构,因为它们能够学习比较两个图像并识别它们是否不同或相同。然而,正如您所观察到的,它们可能对对齐问题很敏感。
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
# Define the RPN as a custom layer
class RPN(keras.layers.Layer):
def __init__(self, num_anchors, **kwargs):
super(RPN, self).__init__(**kwargs)
self.num_anchors = num_anchors
# Define the RPN network with a 3x3 convolution and two 1x1 convolutions
# for the classification and regression outputs
self.conv = layers.Conv2D(512, 3, padding="same", activation="relu")
self.cls = layers.Conv2D(num_anchors, 1, activation="sigmoid")
self.reg = layers.Conv2D(num_anchors * 4, 1)
def call(self, inputs):
# inputs is a feature map of shape (batch_size, h, w, c)
# Compute the RPN outputs
x = self.conv(inputs)
cls = self.cls(x) # shape (batch_size, h, w, num_anchors)
reg = self.reg(x) # shape (batch_size, h, w, num_anchors * 4)
# Reshape the outputs to (batch_size, num_anchors * h * w, 1) and
# (batch_size, num_anchors * h * w, 4)
cls = tf.reshape(cls, [tf.shape(inputs)[0], -1, 1])
reg = tf.reshape(reg, [tf.shape(inputs)[0], -1, 4])
return cls, reg
# Define the RoI Head as a custom layer
class RoIHead(keras.layers.Layer):
def __init__(self, num_classes, **kwargs):
super(RoIHead, self).__init__(**kwargs)
self.num_classes = num_classes
# Define the RoI pooling layer using the keras ROIAlign layer
self.roi_pool = keras.layers.ROIAlign(crop_size=(7, 7))
# Define the RoI head network with two fully-connected layers
# and two output layers for classification and regression
self.fc1 = layers.Dense(1024, activation="relu")
self.fc2 = layers.Dense(1024, activation="relu")
# Modify the output layers to predict material changes
self.mat_change_cls = layers.Dense(1, activation="sigmoid")
self.mat_change_reg = layers.Dense(4)
def call(self, inputs):
# inputs is a list of [rois, feature_map, batch_indices]
# rois is a tensor of shape (num_rois, 4) with the proposed regions
# feature_map is a tensor of shape (batch_size, h, w, c) with the feature map
# batch_indices is a tensor of shape (num_rois,) with the batch indices
# Compute the RoI pooling output
x = self.roi_pool(inputs) # shape (num_rois, 7, 7, c)
# Flatten and pass through two fully-connected layers
x = tf.reshape(x, [tf.shape(x)[0], -1])
x = self.fc1(x)
x = self.fc2(x)
# Compute the RoI head outputs for material change detection
mat_change_cls = self.mat_change_cls(x) # shape (num_rois, 1)
mat_change_reg = self.mat_change_reg(x) # shape (num_rois, 4)
return mat_change_cls, mat_change_reg
您需要为此材料变化检测任务定义一个自定义损失函数,该函数应考虑分类和回归损失。 确保您的数据集包含检查图像中与模板相比的材料变化的注释,指定它们的位置和属性(例如,边界框和回归目标)。 您还可以针对您的问题定义特定于材料变化检测和边界框预测的适当评估指标。