Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在tensorflow中使用夹带自动编码器模型计算新图像的分数以进行异常检测?_Python_Image_Tensorflow_Autoencoder_Anomaly Detection - Fatal编程技术网

Python 如何在tensorflow中使用夹带自动编码器模型计算新图像的分数以进行异常检测?

Python 如何在tensorflow中使用夹带自动编码器模型计算新图像的分数以进行异常检测?,python,image,tensorflow,autoencoder,anomaly-detection,Python,Image,Tensorflow,Autoencoder,Anomaly Detection,我是tensorflow的初学者,我正在尝试为图像创建一个简单的自动编码器来检测异常。首先,我使用dogs图像创建了一个简单的自动编码器,现在我想用这个模型来重建我的测试图像,并使用一些度量来比较结果。那么我如何在tensorflow上做到这一点(因为我是tensorflow的初学者) (我发现在数字数据集和MNIST数据集上实现了相同的想法)。 这是我的代码: from tensorflow.keras.models import Model from tensorflow.keras.lay

我是tensorflow的初学者,我正在尝试为图像创建一个简单的自动编码器来检测异常。首先,我使用dogs图像创建了一个简单的自动编码器,现在我想用这个模型来重建我的测试图像,并使用一些度量来比较结果。那么我如何在tensorflow上做到这一点(因为我是tensorflow的初学者) (我发现在数字数据集和MNIST数据集上实现了相同的想法)。 这是我的代码:

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)
我得到了这个错误:

回溯(最近一次呼叫最后一次): 文件“c:\autoencoder\u anotional.py”,第196行,在 分数1=np.sqrt(度量平均平方误差(pred,x列)) 文件“C:\Users\AppData\Local\Programs\Python\36\lib\site packages\sklearn\metrics\u regression.py”,第252行,均方误差 y_true,y_pred,多输出) 文件“C:\Users\AppData\Local\Programs\Python\36\lib\site packages\sklearn\metrics\u regression.py”,第84行,在检查注册目标中 检查长度是否一致(y\U true,y\U pred)

ValueError:找到样本数不一致的输入变量:[6,0] 请注意,我只使用了6个图像。
那么,如何使用tensorflow上的度量和自动编码器模型计算重建图像的误差呢?

这仅仅是因为形状不匹配

当计算均方误差时,它会计算基本真值和估计值的元素误差。因此,
pred.shape
train\u batches.shape
应该相等。检查输入数据形状,确保它们相等

步骤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]
步骤2:预测

pred=autoencoder.predict(testGene, steps=testGene.n/BATCH_SIZE)
步骤3:计算差值

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

你在哪一行得到这个错误?你能提供完整的堆栈跟踪吗?pred的形状是(2,64,64,3),当我尝试检查列批的形状时,我得到了以下错误:AttributeError:'DirectoryIterator'对象没有属性形状。我的inputdata:input_img=input(shape=(64,64,3))。那么我该如何计算分数呢?@Myra我已经更新了答案,仔细检查了每一步谢谢,但是我得到了这个错误:ValueError:找到了样本数不一致的输入变量:[6,0]你能发布完整的跟踪吗。错误的确切位置检查更新的答案,问题是您正在使用testGene生成器进行预测,并尝试获得预测图像(6)和训练图像的差异。因此大小是不同的