简体   繁体   中英

How can I calculate score of a new image using entrained autoencoder model for anomaly detection in tensorflow?

I am beginner in tensorflow and I am trying to create a simple autoencoder for images to detect anomalies.Firstly, I created a simple autoencoder using dogs images , now I want to use this model to reconstruct my tests images and compare the result using some metrics.So how can I do it on tensorflow (because I am beginner on tensorflow ) (I found the same idea implemented on numerical datasets , and also on MNIST dataset ). this is my code:

from tensorflow.keras.models import Model
from tensorflow.keras.layers import Input, Dense, Conv2D, MaxPooling2D, UpSampling2D
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.callbacks import LearningRateScheduler

BATCH_SIZE = 256
EPOCHS = 2
train_datagen = ImageDataGenerator(rescale=1./255)
train_batches = train_datagen.flow_from_directory('C:/MyPath/PetImages1',
    target_size=(64,64), shuffle=True, class_mode='input', batch_size=BATCH_SIZE)




input_img = Input(shape=(64, 64, 3)) 

x = Conv2D(48, (3, 3), activation='relu', padding='same')(input_img)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(96, (3, 3), activation='relu', padding='same')(x)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(192, (3, 3), activation='relu', padding='same')(x)
x = MaxPooling2D((2, 2), padding='same')(x)
 encoded = Conv2D(32, (1, 1), activation='relu', padding='same')(x)


 latentSize = (8,8,32)


 # DECODER
 direct_input = Input(shape=latentSize)
 x = Conv2D(192, (1, 1), activation='relu', padding='same')(direct_input)
 x = UpSampling2D((2, 2))(x)
 x = Conv2D(192, (3, 3), activation='relu', padding='same')(x)
 x = UpSampling2D((2, 2))(x)
 x = Conv2D(96, (3, 3), activation='relu', padding='same')(x)
 x = UpSampling2D((2, 2))(x)
 x = Conv2D(48, (3, 3), activation='relu', padding='same')(x)
 decoded = Conv2D(3, (3, 3), activation='sigmoid', padding='same')(x)
# COMPILE

encoder = Model(input_img, encoded)
decoder = Model(direct_input, decoded)
autoencoder = Model(input_img, decoder(encoded))

autoencoder.compile(optimizer='Adam', loss='binary_crossentropy')
autoencoder.save_weights('autoencoder_DogsAuto.h5')
history=autoencoder.fit_generator(train_batches,steps_per_epoch=10,epochs = 
 EPOCHS)

#Images for tests
 testGene = train_datagen.flow_from_directory('C:/PetImages/',
    target_size=(64,64), shuffle=True, class_mode='input', 
  batch_size=BATCH_SIZE)

  restored = autoencoder.predict_generator(testGene, 
 steps=testGene.n/BATCH_SIZE)

 image_height=64
 image_width=64
 image_channels=3



 x_train = np.zeros((0, image_height, image_width, image_channels), dtype=float)
for x, _ in train_batches :
    if train_batches.total_batches_seen > train_batches.n/BATCH_SIZE:
        break
   else:
       x_train = np.r_[x_train,x]
pred=autoencoder.predict(train_batches, steps=train_batches.n/BATCH_SIZE)
from sklearn import metrics


score1=np.sqrt(metrics.mean_squared_error(pred,x_train ))
print(score1)

And I got this error:

Traceback (most recent call last): File "c:\\autoencoder_anomaly.py", line 196, in score1=np.sqrt(metrics.mean_squared_error(pred,x_train )) File "C:\\Users\\AppData\\Local\\Programs\\Python\\Python36\\lib\\site-packages\\sklearn\\metrics_regression.py", line 252, in mean_squared_error y_true, y_pred, multioutput) File "C:\\Users\\AppData\\Local\\Programs\\Python\\Python36\\lib\\site-packages\\sklearn\\metrics_regression.py", line 84, in _check_reg_targets check_consistent_length(y_true, y_pred)

ValueError: Found input variables with inconsistent numbers of samples: [6, 0] Note that I am using only 6 images. So how can I calculate the error of the reconstructed image using metrics and the autoencoder Model on tensorflow ?

This is simply because of shape mismatch.

when you calculate mean squared error,it calculates the element wise error of ground truth values and estimated values. so pred.shape and train_batches.shape should be equal.check the input data shapes and make sure they are equal.

step 1: get all training images from the generator and add to one array

x_test = np.zeros((0, image_height, image_width, image_color), dtype=float)
for x, _ in testGene:
    if testGene.total_batches_seen > testGene.n/BATCH_SIZE:
        break
    else:
        x_test = np.r_[x_test , x]

step 2 : prediction

pred=autoencoder.predict(testGene, steps=testGene.n/BATCH_SIZE)

step 3 : calculate the difference

score1=np.sqrt(metrics.mean_squared_error(pred,testGene))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM