【发布时间】:2021-08-03 12:33:32
【问题描述】:
我使用功能 API 和两种不同类型的预训练模型训练了一个模型:EfficientNet B5 和 MobileNet V2。在使用保存的模型进行训练后,我正在运行一个应用程序,该应用程序使用该模型进行一些预测。
我提出了一个疑问,即将图像传递给“model.prediction()”参数的正确方法是什么。
型号:
self.feature_extractor1 = EfficientNetB5(#weights='imagenet',
input_shape=self.input_shape,
include_top=False)
self.feature_extractor2 = MobileNetV2(#weights='imagenet',
input_shape=self.input_shape,
include_top=False)
for layer in self.feature_extractor1.layers:
layer.trainable = False
for layer in self.feature_extractor2.layers:
layer.trainable = False
input_ = Input(shape=self.input_shape)
processed_input1 = b5_preprocess_input(input_)
processed_input2 = mbv2_preprocess_input(input_)
x1 = self.feature_extractor1(processed_input1)
x1 = GlobalAveragePooling2D()(x1)
x1 = Dropout(0.2)(x1)
x1 = Flatten()(x1)
x2 = self.feature_extractor2(processed_input2)
x2 = GlobalAveragePooling2D()(x2)
x2 = Dropout(0.2)(x2)
x2 = Flatten()(x2)
x = Concatenate()([x1, x2])
x = Dense(512, activation='relu')(x) #,kernel_initializer=initializer,kernel_regularizer=regularizers.l2(0.001))
x = Dense(1024, activation='relu')(x)
output_shape = Dense(shape_categories, activation='softmax', name='shape')(x)
model = Model(inputs=input_,
outputs=output_shape)
adam_kwargs = {'beta_1': 0.9, 'beta_2': 0.9, 'epsilon': 1e-7}
sgd_kwargs = {'decay': 1e-6, 'momentum': 0.9, 'nesterov': True}
optimizer = self.optimizers(kwargs=adam_kwargs)
model.compile(loss='categorical_crossentropy',
optimizer=optimizer,
metrics=['accuracy'])
model.summary()
STEP_SIZE_TRAIN = self.phase_gen[0].n// self.phase_gen[0].batch_size
STEP_SIZE_VALID = self.phase_gen[1].n// self.phase_gen[1].batch_size
if self.phases == 3:
STEP_SIZE_TEST = self.phase_gen[2].n// self.phase_gen[2].batch_size
checkpoint = ModelCheckpoint(self.model_dir,
monitor='val_accuracy',
verbose=1,
save_best_only=True,
mode='max')
tensorboard = TensorBoard(log_dir=self.model_dir + '/logs',
histogram_freq=5,
embeddings_freq=5)
#[EarlyStopping(monitor='val_loss', patience=8)]
callbacks = [checkpoint, tensorboard]
hist = model.fit_generator(generator=self.phase_gen[0],
steps_per_epoch=STEP_SIZE_TRAIN,
validation_data=self.phase_gen[1],
validation_steps=STEP_SIZE_VALID,
epochs=self.epochs,
callbacks=callbacks
)
在另一个脚本中,我有预测方法:
from tensorflow.keras.applications.mobilenet_v2 import preprocess_input as mbv2_preprocess_input
from tensorflow.keras.applications.efficientnet import preprocess_input as b5_preprocess_input
def preprocess_image(img):
img = Image.open(io.BytesIO(img))
img = img.resize((224, 224), Image.ANTIALIAS)
img = image.img_to_array(img)
img = np.expand_dims(img, axis=0)
#return [b5_preprocess_input(img), mbv2_preprocess_input(img)]
return [img, img]
modelSHP = get_modelSHP()
@app.route('/part_numbers', methods=['POST'])
def part_number():
img = request.files.get('image').read()
processed_image = preprocess_image(img)
predict_shape = modelSHP.predict(processed_image)
我的第一个想法是我需要传递由正确函数预处理的输入(图像),并且按照我在模型训练期间使用它的顺序。但是当我完成它时,我的预测准确性保持在零左右。只传图片,不做任何预处理,效果更好。
我将图像输入传递给 model.prediction 的方式是否正确(无需预处理)?我想知道是否使用 Functional API 以及在我构建模型的方式中,预处理变成了每个分支模型中的一个层。
【问题讨论】:
标签: python tensorflow computer-vision conv-neural-network image-preprocessing