如何在不给模型充电的情况下进行预测呢?

2024-04-19 01:48:17 发布

您现在位置:Python中文网/ 问答频道 /正文

嗨,我正在和tf.估计器,我想用我训练过的模型来预测,但是当我上传一个图像时,总是加载并关闭模型,如何使它保持加载状态并继续上传图像来进行预测tf服务但我只想在我的电脑上运行

这是我的代码:

#Convolutional Funcion .......................................

def cnn_model_fn(features, labels, mode):

    """Model"""

    input_layer = tf.reshape(features["image"], [-1, 224, 224, 3])

    # Convolutional Layer #1
    conv1 = tf.layers.conv2d(
      inputs=input_layer,
      filters=64,
      kernel_size=[7, 7],
      padding="same",
      activation=tf.nn.relu)

    # Pooling Layer #1
    pool1 = tf.layers.max_pooling2d(inputs=conv1, pool_size=[2, 2], strides=2)

    # Convolutional Layer #2
    conv2 = tf.layers.conv2d(
      inputs=pool1,
      filters=128,
      kernel_size=[5, 5],
      padding="same",
      activation=tf.nn.relu)

    # Pooling Layer #2
    pool2 = tf.layers.max_pooling2d(inputs=conv2, pool_size=[2, 2], strides=2)

    # Convolutional Layer #3
    conv3 = tf.layers.conv2d(
      inputs=pool2,
      filters=192,
      kernel_size=[5, 5],
      padding="same",
      activation=tf.nn.relu)

    # Pooling Layer #3
    pool3 = tf.layers.max_pooling2d(inputs=conv3, pool_size=[2, 2], strides=2)

    # Convolutional Layer #4
    conv4 = tf.layers.conv2d(
      inputs=pool3,
      filters=192,
      kernel_size=[3, 3],
      padding="same",
      activation=tf.nn.relu)

    # Pooling Layer #4
    pool4 = tf.layers.max_pooling2d(inputs=conv4, pool_size=[2, 2], strides=2)

    # Convolutional Layer #5

    conv5 = tf.layers.conv2d(
      inputs=pool4,
      filters=128,
      kernel_size=[3, 3],
      padding="same",
      activation=tf.nn.relu)

    # Pooling Layer #5

    pool5 = tf.layers.max_pooling2d(inputs=conv5, pool_size=[2, 2], strides=2)

    # Flatten tensor into a batch of vectors
    pool5_flat = tf.reshape(pool5, [-1, 7 * 7 * 128])

    # Dense Layer
    dense = tf.layers.dense(inputs=pool5_flat, units=2048, activation=tf.nn.relu)

    # Add dropout operation; 0.6 probability that element will be kept
    dropout = tf.layers.dropout(inputs=dense, rate=0.4, training=mode == tf.estimator.ModeKeys.TRAIN)

    dense_1 = tf.layers.dense(inputs=dropout, units=2048, activation=tf.nn.relu)

    dropout_1 = tf.layers.dropout(inputs=dense_1, rate=0.4, training=mode == tf.estimator.ModeKeys.TRAIN)

    #Final Layer with 2 outputs
    logits = tf.layers.dense(inputs=dropout_1, units=2)

    #Predictions
    output=tf.nn.softmax(logits)
    predictions=tf.argmax(input=output, axis=1)
    predictions={'Probabilities':output,'Prediction':predictions}

    if mode == tf.estimator.ModeKeys.PREDICT:
        return tf.estimator.EstimatorSpec(mode=mode,predictions=predictions)

def read_img():

    filenames = tf.constant([data_path])

    dataset = tf.data.Dataset.from_tensor_slices(filenames)

    def _parse_function(filename):
        image_string = tf.read_file(filename)
        image_decoded = tf.image.decode_jpeg(image_string)
        image_resized = tf.image.resize_images(image_decoded, [224, 224])
        return {'image':image_resized}

    dataset = dataset.map(_parse_function)

    return dataset

def main(params):

    det = tf.estimator.Estimator(model_fn=cnn_model_fn, model_dir='/Users/David/Desktop/David/Tesis')

    pred_results=det.predict(input_fn=read_img)

    print(next(pred_results))

编辑: 我每次都得到这个:

results

每次恢复参数

<2

我有这样的代码:

^{pr2}$

Tags: imagelayersizemodelayerstfnnactivation
1条回答
网友
1楼 · 发布于 2024-04-19 01:48:17

一种选择是完全交换到TF Eager,然后简单地重写main,如下所示:

tf.enable_eager_execution()

def main(params):
  for data in read_img():
    print(det.model_fn(data, None, tf.estimator.ModeKeys.PREDICT).predictions)

另一种方法是使用会话:

^{pr2}$

这两个都只是伪代码,所以API名称等可能会关闭。在

相关问题 更多 >