Create a twin model with tensorflow


I would like to create a program in python that can detect deviation in images. There are subtle deviation but still. The regular deep learning method I use is training on a lot of images with a defect and classifying those.
I want to do the opposite. I want to tell the AI that this is good. Anything else is crap. How can I do that?
Anyone knows how? Where to start? I tried openAI but it just brings back a solution that always results in 0 prediction.


TensorRT Version: Not working
GPU Type: ~
Nvidia Driver Version:
CUDA Version:
CUDNN Version:
Operating System + Version: Ubuntu 20.04
Python Version (if applicable): 3.8.10
TensorFlow Version (if applicable): Latest
PyTorch Version (if applicable):
Baremetal or Container (if container which image + tag):

import cv2
import numpy as np
import os
from tensorflow import keras
from tensorflow.keras.applications import ResNet50
from tensorflow.keras.applications.resnet50 import preprocess_input

# Function to load and preprocess images from a directory
def load_images_from_folder(folder):
    images = []
    for filename in os.listdir(folder):
        if filename.endswith('.jpg') or filename.endswith('.png'):
            image_path = os.path.join(folder, filename)
            image = cv2.imread(image_path)
            image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
            image = cv2.resize(image, (224, 224))
            image = preprocess_input(image)
    return images

# Load the reference images from a folder
reference_images_folder = 'path/to/reference/images/folder'
reference_images = load_images_from_folder(reference_images_folder)

# Load the pre-trained ResNet50 model
base_model = ResNet50(weights='imagenet', include_top=False, input_shape=(224, 224, 3))

# Freeze the pre-trained layers
for layer in base_model.layers:
    layer.trainable = False

# Add custom classification layers on top of the pre-trained model
model = keras.Sequential([
    keras.layers.Dense(256, activation='relu'),
    keras.layers.Dense(1, activation='sigmoid')

# Compile the model
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])

# Create labels for the reference images (all labeled as 0 for no deviation)
reference_labels = np.zeros(len(reference_images))

# Train the model with the reference images, reference_labels, epochs=10, batch_size=1)

# Perform inference on multiple input images
input_images_folder = 'path/to/input/images/folder'
input_images = load_images_from_folder(input_images_folder)

# Define a threshold for deviation prediction
threshold = 0.5

# Iterate over the input images
for i, input_image in enumerate(input_images):
    input_image = np.expand_dims(input_image, axis=0)
    # Predict deviation using the input image
    prediction = model.predict(input_image)[0][0]
    # Print the result
    if prediction >= threshold:
        print(f"Deviation detected in input image {i+1}!")
        print(f"Prediction: {prediction:.4f}")
        print(f"No deviation detected in input image {i+1}.")
        print(f"Prediction: {prediction:.4f}")

We recommend you to check the below samples links in case of tf-trt integration issues.

If issue persist, We recommend you to reach out to Tensorflow forum.

Thanks. I was looking more for a general approach on what to use in order to make this anomaly detection. Normally you know what good is (thats what I have 99% of the times). So how, without having the faulty ones, can I get the anomalies.


The above query looks generic and is not related to TensorRT.
Please refer to the following, which may help you.

If you need further assistance, please reach out to the relevant forum.

Thank you.

Thank you for the response. I will look into that link.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.