使用 Keras CNN Ml 模型验证标签布局模式

问题描述 投票:0回答:0

我有一项任务是创建标签布局模式验证系统。

可以尝试哪些模型。以便检测假标签。

目前遵循以下方法

  1. 收集原创标签
  2. 创建边界框/注释文件注释工具。
    3使用keras库。下面是代码。
def preprocess_image(image):
    image = image / 255.0
    return image
def preprocess_annotation(annotation_path):
    tree = ET.parse(annotation_path)
    root = tree.getroot()

    boxes = []

    for obj in root.findall('object'):
        label = obj.find('name').text

        if label == 'logo':
            box = obj.find('bndbox')
            xmin = int(box.find('xmin').text)
            ymin = int(box.find('ymin').text)
            xmax = int(box.find('xmax').text)
            ymax = int(box.find('ymax').text)
            boxes.append([xmin, ymin, xmax, ymax])

    mask = np.zeros((224, 224), dtype=np.float32)

    for box in boxes:
        xmin, ymin, xmax, ymax = box
        mask[ymin:ymax, xmin:xmax] = 1.0

    return mask
def preprocess_data(image_dir, annotation_dir):
    datagen = ImageDataGenerator(preprocessing_function=preprocess_image)

    image_generator = datagen.flow_from_directory(
        image_dir,
        target_size=(224, 224),
        batch_size=32,
        class_mode=None
    )

    mask_generator = datagen.flow_from_directory(
        annotation_dir,
        target_size=(224, 224),
        batch_size=32,
        class_mode=None
    )
    generator = zip(image_generator, mask_generator)
    for image_batch, mask_batch in generator:
        mask_batch = np.array([preprocess_annotation(annotation_path) for annotation_path in mask_batch])
        yield (image_batch, mask_batch)
# Define the model
model = Sequential([
    Conv2D(32, (3, 3), activation='relu', input_shape=(224, 224, 3)),
    MaxPooling2D((2, 2)),
    Dropout(0.25),
    Conv2D(64, (3, 3), activation='relu'),
    MaxPooling2D((2, 2)),
    Dropout(0.25),
    Conv2D(128, (3, 3), activation='relu'),
    MaxPooling2D((2, 2)),
    Dropout(0.25),
    Flatten(),
    Dense(256, activation='relu'),
    Dropout(0.5),
    Dense(1, activation='sigmoid')
])
# Compile the model
model.compile(optimizer='adam', 
              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True), 
              metrics=['accuracy'])

有没有其他方法或技巧可以尝试

标签如下所示

python image opencv keras deep-learning
© www.soinside.com 2019 - 2024. All rights reserved.