我是tensorflow的初学者,我正在尝试创建一个简单的图像自动编码器来检测异常。首先,我使用狗的图像创建了一个简单的自动编码器,现在我想使用这个模型来重建我的测试图像,并使用一些度量来比较结果。那么我该如何在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_exception.py",第196行,位于score1=np.sqrt(metrics.mean_squared_error(pred,x_train((文件"C:\Users\AppData\Local\Programs\Python36\lib\site packages\sklearn\metrics_regression.py",第252行,mean_squared_errory_true,y_pred,多输出(文件"C:\Users\AppData\Local\Programs\Python36\lib\site packages\sklearn\metrics_regression.py",第84行,在_check_reg_targets中check_consistent_length(y_true,y_pred(
ValueError:发现样本数不一致的输入变量:[6,0]请注意,我只使用了6个图像。那么,如何使用tensorflow上的度量和自动编码器模型来计算重建图像的误差呢?
这只是因为形状不匹配。
当计算均方误差时,它会计算地面实况值和估计值的元素误差。因此CCD_ 1和CCD_ 2应该相等。检查输入数据形状并确保它们相等。
步骤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))