Keras在远程服务器上返回不同的结果

时间:2019-03-21 04:14:22

标签: python tensorflow flask keras h5py

我训练了一个keras模型并将其保存在h5文件中。当我通常只使用load_weights和一个预测生成器进行预测时,它会返回较高的auc和各种预测。

import numpy as np
from keras.preprocessing.image import ImageDataGenerator
from keras.layers.core import Dense, Dropout, Activation, Flatten
from keras.layers.convolutional import Conv2D, SeparableConv2D
from keras.layers.pooling import MaxPooling2D, GlobalAveragePooling2D
from keras import backend as K
from keras.layers import Input
from keras.models import Model
import keras
from keras.layers.normalization import BatchNormalization
from keras.optimizers import Adam
import tensorflow as tf
valid_datagen = ImageDataGenerator(rescale=1./255.)
test_datagen = ImageDataGenerator(rescale=1./255.)

valid_generator = valid_datagen.flow_from_directory(
    directory="./valid/",
    target_size=(50, 50),
    color_mode="rgb",
    batch_size=32,
    class_mode="binary",
    shuffle=True,
    seed=42
)
test_generator = test_datagen.flow_from_directory(
    directory="./test/",
    target_size=(50, 50),
    color_mode="rgb",
    batch_size=1,
    class_mode=None,
    shuffle=False,
    seed=42
)

K.clear_session()
K.set_image_dim_ordering('tf')
reg = keras.regularizers.l1_l2(1e-5, 0.0)
def conv_layer(channels, kernel_size, input):
    output = SeparableConv2D(channels, kernel_size, padding='same',kernel_regularizer=reg)(input)
    output = BatchNormalization()(output)
    output = Activation('relu')(output)
    output = Dropout(0)(output)
    return output

input1 = Input(shape=(50, 50, 3))
output = conv_layer(32, (3,  3), input1)
output = MaxPooling2D(pool_size=(2, 2))(output)
output = conv_layer(64, (3,  3), output)
output = MaxPooling2D(pool_size=(2, 2))(output)
output = conv_layer(128, (5,  5), output)
output = MaxPooling2D(pool_size=(2, 2))(output)
output = conv_layer(256, (5,  5), output)
output = MaxPooling2D(pool_size=(2, 2))(output)
output = conv_layer(512, (7,  7), output)
output = MaxPooling2D(pool_size=(2, 2))(output)
output = conv_layer(1024, (7,  7), output)
output = GlobalAveragePooling2D()(output)
output = Dense(1)(output)
output = Activation('sigmoid')(output)

model = Model(inputs=input1, outputs=output)


def auc(y_true, y_pred):
    auc = tf.metrics.auc(y_true, y_pred)[1]
    K.get_session().run(tf.local_variables_initializer())
    return auc

model.compile(loss='binary_crossentropy',
              optimizer='adam',
              metrics=['accuracy', auc])


STEP_SIZE_VALID=valid_generator.n/valid_generator.batch_size
STEP_SIZE_TEST=test_generator.n/test_generator.batch_size

model.load_weights('model.h5')
model.evaluate_generator(generator=valid_generator, steps = STEP_SIZE_VALID
)

test_generator.reset()
model._make_predict_function()
pred=model.predict_generator(test_generator,steps = STEP_SIZE_TEST, verbose=1)
np.set_printoptions(threshold=np.nan)
print(pred)
print(test_generator.filenames)
f = open("demofile.txt", "a")
f.write(str(pred))
f.write(str(test_generator.filenames))

但是,当我将此h5文件保存在远程服务器中并尝试获取预测时,所有请求返回都是1.0。

import flask
import pandas as pd
import tensorflow as tf
import keras
from keras.models import load_model
from keras.preprocessing.image import img_to_array
from keras.applications import imagenet_utils
from PIL import Image
import numpy as np
import flask
import io
import sys

# instantiate flask
app = flask.Flask(__name__)
def prepare_image(image, target):
    # if the image mode is not RGB, convert it
    if image.mode != "RGB":
        image = image.convert("RGB")

    # resize the input image and preprocess it
    image = image.resize(target)
    image = img_to_array(image)
    image = np.expand_dims(image, axis=0)
    image = imagenet_utils.preprocess_input(image)

    # return the processed image
    return image
# we need to redefine our metric function in order
# to use it when loading the model
def auc(y_true, y_pred):
   auc = tf.metrics.auc(y_true, y_pred)[1]
   keras.backend.get_session().run(tf.local_variables_initializer())
   return auc

# load the model, and pass in the custom metric function

global graph
graph = tf.get_default_graph()
model = load_model('new.h5', custom_objects={'auc': auc})

# define a predict function as an endpoint
@app.route("/predict", methods=["GET","POST"])
def predict():
    data = {"success": False}
    params = flask.request.files["image"].read()
    if (params == None):
        params = flask.request.args

    # if parameters are found, return a prediction
    if (params != None):
        image = params
        image = Image.open(io.BytesIO(image))

        # preprocess the image and prepare it for classification
        image = prepare_image(image, target=(50, 50))

        # classify the input image and then initialize the list
        # of predictions to return to the client
        print(image)

        with graph.as_default():
            preds = model.predict(image)
            data["prediction"] = str(preds[0][0])
            data["success"] = True

    # return a response in json format
    return flask.jsonify(data)

# start the flask app, allow remote connections
app.debug=True
app.run(host='0.0.0.0')

您知道为什么会这样吗?

0 个答案:

没有答案