将numpy操作转换为张量流层的输出

时间:2019-03-25 13:44:38

标签: python numpy tensorflow keras

我正在尝试使用tensorflow和keras转换一个numpy层

PackageManager manager = getPackageManager();
Intent intent = manager.getLaunchIntentForPackage("com.lafitness.lafitness.apk");
intent.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(intent);

如何从过滤后的索引中获取第一个作为模型的输出?我正在使用keras和TF。

输入:来自模型[1,64,64,16]的op

输出:过滤操作后-形状[16,3]#[x,y,confidence_score]

1 个答案:

答案 0 :(得分:0)

下面是用于转换模型的等效代码

model_output = op_from_orig_model

import tensorflow as tf
from keras.layers import Lambda
from keras.models import Model

def gaussian_kernel(size: int, mean: float, std: float, ):
    d = tf.distributions.Normal(mean, std)
    vals = d.prob(tf.range(start = -size, limit = size + 1, dtype = tf.float32))
    gauss_kernel = tf.einsum('i,j->ij', vals, vals)
    return gauss_kernel / tf.reduce_sum(gauss_kernel)

gauss_kernel = gaussian_kernel(5, 0.0, 0.3)

def gaussian(x):
    return tf.nn.conv2d(x, gauss_kernel, strides=[1, 1, 1, 1], padding="SAME")

def pooling(x):
    return tf.nn.max_pool(x, ksize=[1,3,3,1], strides= [1,1,1,1], padding="SAME")

def keepers(x):
    return tf.where(tf.equal(x, tf.reduce_max(tf.reduce_max(x, axis=1), axis=1)))


gauss = Lambda(lambda x: gaussian(x))(model_output)
filter_op = Lambda(lambda x:pooling(x))(gauss)
vals = Lambda(lambda x:keepers(x))(filter_op)

new_model = Model(inputs=original_model.inputs, outputs=[vals])

希望这对某人有帮助。