[英]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 ).我是 tensorflow 的初学者,我正在尝试为图像创建一个简单的自动编码器来检测异常。首先,我使用狗图像创建了一个简单的自动编码器,现在我想使用这个模型来重建我的测试图像并使用一些指标比较结果.那么我怎么能在 tensorflow 上做到这一点(因为我是 tensorflow 的初学者)(我发现在数值数据集和 MNIST 数据集上实现了相同的想法)。 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)回溯(最近一次调用):文件“c:\\autoencoder_anomaly.py”,第 196 行,在 score1=np.sqrt(metrics.mean_squared_error(pred,x_train)) 文件“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) 文件 "C:\\Users\\AppData\\Local\\Programs\\Python\\Python36\\lib\\site-packages \\sklearn\\metrics_regression.py",第 84 行,在 _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. ValueError:发现样本数量不一致的输入变量:[6, 0]请注意,我只使用了 6 个图像。 So how can I calculate the error of the reconstructed image using metrics and the autoencoder Model on tensorflow ?那么如何使用 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.所以pred.shape
和train_batches.shape
应该相等。检查输入数据形状并确保它们相等。
step 1: get all training images from the generator and add to one array第 1 步:从生成器中获取所有训练图像并添加到一个数组中
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第 2 步:预测
pred=autoencoder.predict(testGene, steps=testGene.n/BATCH_SIZE)
step 3 : calculate the difference第 3 步:计算差异
score1=np.sqrt(metrics.mean_squared_error(pred,testGene))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.