我尝试使用预训练的vgg16网络进行回归。由于损失和度量,我选择了平均绝对误差。我想检查这个分数是否真的正确,并在回调中自己实现了平均绝对分数。但是,结果与输出可以看出的结果不同:
Training MAE:126.649451276
Epoch 1/100
638/638 [==============================] - 406s - loss: 38.9601 - mean_absolute_error: 38.9601
Training MAE:40.7683742351
Epoch 2/100
638/638 [==============================] - 362s - loss: 19.8719 - mean_absolute_error: 19.8719
Training MAE:43.2516028945
训练MAE应该与上面的纪元中的损失或mean_absolute_error相同(或至少几乎相同)。对于第一个时代,这是可以的。对于第二个时代,事实并非如此。 MAE为43.24,但损失为19.87,keras提供的mean_absolute_error为19.87。
我已经清理了我的代码并试图找到原因,但我无法找到它。为什么会这样?
我的代码:
from keras.layers.core import Flatten, Dense, Dropout
import numpy as np
from keras.preprocessing import image
from keras.applications.vgg16 import VGG16
from keras import optimizers
from keras.models import Model
import os
from keras.layers.core import *
from keras.callbacks import Callback, ModelCheckpoint
os.environ["CUDA_VISIBLE_DEVICES"]="2"
model_checkpoints = "/home/usr/PycharmProjects/RSNA/model_checkpoints/model2.hdf5"
data_dir = "/home/usr/PycharmProjects/RSNA/data/"
data_training = "dataset/training"
training_images = "boneage-training-dataset/"
training_gt = "training_gt/"
n_batch = 16
n_training_samples = 10213
n_validation_samples = 1136
n_testing_samples = 1262
def mae(X,y,mdl):
pred = mdl.predict(X)
gt = y
return str(np.mean(np.abs(np.array(gt)-np.array(pred))))
class LossHistory(Callback):
def on_epoch_begin(self, epoch, logs={}):
mae_score = mae(X_train,y_train,self.model)
print "Training MAE:" + mae_score
def regression_flow_from_directory(flow_from_directory_gen, rev_indices):
for x, y in flow_from_directory_gen:
yield x, [float(rev_indices[val]) for val in y]
if __name__ == '__main__':
width = 224
height = 224
X_train = []
y_train = []
train_datagen = image.ImageDataGenerator(
rescale=1./255,
width_shift_range=0.2,
height_shift_range= 0.2,
)
train_generator = train_datagen.flow_from_directory(
data_dir+data_training,
target_size=(width, height),
batch_size=n_batch,
color_mode='rgb',
class_mode='sparse',
seed=42)
indices = train_generator.class_indices
rev_indices = dict((v,k) for k, v in indices.iteritems())
train_generator = regression_flow_from_directory(train_generator,rev_indices)
i = 0
print "Epcohs: " + str(n_training_samples//n_batch)
for x,y in train_generator:
if i <= n_training_samples//n_batch:
X_train.extend(x)
y_train.extend(y)
i +=1
else:
break;
print "Maximum: " + str(np.max(y_train))
X_train = np.array(X_train)
print X_train.shape
model = VGG16(weights='imagenet', include_top=False,input_shape = (224, 224, 3))
last = model.output
x = Flatten(name='flatten')(last)
x = Dense(4096, activation='relu', name='fc1')(x)
x = Dropout(0.5, noise_shape=None, seed=None)(x)
x = Dense(4096, activation='relu', name='fc2')(x)
x = Dense(1, activation='relu', name='predictions')(x)
my_model = Model(input=model.input, output=x)
my_model.compile(loss="mae", optimizer=optimizers.SGD(lr=0.00001, momentum=0.9),
metrics=["mae"])
history = LossHistory()
print my_model.summary()
print n_validation_samples//n_batch
my_model.fit_generator(
train_generator,
steps_per_epoch=n_training_samples//n_batch,
epochs=100,
callbacks=[history],
)