'How the anomaly detection with AnoGAN works?

I'm creating an AnoGAN trained on the hazelnut image dataset from mvtec which gives me some problem in the anomaly detection phase.

Here is my GAN so far:

def generator_model(noise_dim = 100):
  model = Sequential(name = "Generator")

  model.add(Reshape(target_shape = (1, 1, noise_dim), input_shape = [noise_dim]))
  model.add(Conv2DTranspose(256, activation = tf.keras.layers.LeakyReLU(), kernel_size = 4))

  model.add(Conv2DTranspose(128, activation = tf.keras.layers.LeakyReLU(), kernel_size = 4, padding = "same"))
  model.add(tf.keras.layers.BatchNormalization())
  model.add(tf.keras.layers.UpSampling2D((4, 4)))

  model.add(Conv2DTranspose(64, activation = tf.keras.layers.LeakyReLU(), kernel_size = 4, padding = "same"))
  model.add(tf.keras.layers.BatchNormalization())
  model.add(tf.keras.layers.UpSampling2D((4, 4)))

  model.add(Conv2DTranspose(32, activation = tf.keras.layers.LeakyReLU(), kernel_size = 4, padding = "same"))
  model.add(tf.keras.layers.BatchNormalization())
  model.add(tf.keras.layers.UpSampling2D((4, 4)))

  model.add(Conv2DTranspose(3, activation = "sigmoid", kernel_size = 3, padding = "same"))

  model.summary()

  return model
def discriminator_model():
  model = Sequential(name = "Discriminator")

  model.add(Input([256, 256, 3]))

  model.add(Conv2D(64, (3, 3), strides = (4, 4), padding = "same", activation = tf.keras.layers.LeakyReLU()))
  
  model.add(Conv2D(128, (3, 3), strides = (4, 4), padding = "same", activation = tf.keras.layers.LeakyReLU()))

  model.add(Flatten())

  model.add(Dense(1, activation = "sigmoid"))

  model.summary()

  return model

After 200 - 300 epochs of training, the generator produce some gome good images (not the bests tho).

In according to this paper to measure the anomaly score I used the following losses:

  • Residual Loss: ||x - G(z)||1
  • Discriminator Loss: ||f(x) - f(G(z))||1

Where f gives the output of the hiddens layers of the discriminator. If i understand it correctly, this is done to learn the mapping between the image x and the latent space z. To do this, I defined the following function:

def extract_feature_layers(model):
  feature_layers = Sequential()
  feature_layers.add(Input(img_shape))

  #maybe this is a little naife but it gets the work done

  for i in range(2):
    layer = model.get_layer(index = i)
    feature_layers.add(layer)

  feature_layers.summary()
  return feature_layers

Putting together the previous losses here is the anomaly score function:

A(x) = (1 − λ) · R(x) + λ · D(x).

Unfortunately, this seems to not working because the test images are getting similar values no matter if they are good or bad pictures. Am I missing something about the AnoGAN architecture or the GAN model is not good?

Thanks in advice for the help



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source