为Google Cloud导出keras模型时,请向estimator.export_saved_model添加标签

时间:2019-02-26 15:04:24

标签: tensorflow keras google-cloud-ml tensorflow-estimator

我正在尝试将Keras培训创建的hdf5模型导出到Google Cloud ML Engine。进行在线预测后,除了标签外,我什么都拥有,我希望在进行预测后,具有几率的标签。

这是经过训练并使用Keras创建hdf5模型之后的代码。

首先,我从keras模型中创建一个估算器。

estimator = keras.estimator.model_to_estimator(
              keras_model_path="model.hdf5",
              model_dir="output/")

现在,我像这样导出模型:

estimator.export_saved_model(
"output/model"
serving_input_receiver_fn=serving_input_receiver_fn)

具有函数serving_input_receiver_fn,该功能将允许我接受基本的64 json文件作为Google云端在线预测的输入。

def serving_input_receiver_fn():

   def prepare_image(image_str_tensor):
       image = tf.image.decode_jpeg(image_str_tensor, channels=3)
       return image_preprocessing(image)

   input_ph = tf.placeholder(tf.string, shape=[None])

   images_tensor = tf.map_fn(
       prepare_image, input_ph, back_prop=False, dtype=tf.uint8)

   images_tensor = tf.image.convert_image_dtype(images_tensor, 
                   dtype=tf.float32)

   return tf.estimator.export.ServingInputReceiver(
       {'input_1': images_tensor},
       {'image_bytes': input_ph})

但是,我想要一个带有标签的分类结果(我的结果中有大约10个类)。现在我唯一的结果是这样的:

{“ input_1”:[0.001,0.9,...]}

我想要带有标签的结果。是否可以进行少量更改而不通过其他培训而是通过保留我的hdf5模型文件来做到这一点?

谢谢。

1 个答案:

答案 0 :(得分:1)

对于演示,我们有类似的要求: 我有一个字典类: https://gist.github.com/yrevar/942d3a0ac09ec9e5eb3a

{0: 'tench, Tinca tinca',
 1: 'goldfish, Carassius auratus', ...}

以此类推。

我在客户级别进行了转换:

def get_classes():
  url = 'https://gist.githubusercontent.com/yrevar/942d3a0ac09ec9e5eb3a/raw' \
        '/238f720ff059c1f82f368259d1ca4ffa5dd8f9f5' \
        '/imagenet1000_clsidx_to_labels.txt'
  response = requests.get(url)
  classes = literal_eval(response.text)
  return classes

...

classes = get_classes()
response = model_predict(predict_request)
if response:
  prediction_class = response.get('predictions')[0].get('classes') - 1
  prediction_probabilities = response.get('predictions')[0].get('probabilities')
  print(
    'Prediction: [%d] %s Probability [%.2f] ' % (
      prediction_class, classes[prediction_class], max(prediction_probabilities)))

此处的代码:

https://github.com/GoogleCloudPlatform/ml-on-gcp/blob/master/dlvm/nvidia/inference.py

响应如下所示:

{'predictions': [{'probabilities': [1.55508e-05, 8.52272e-05, 0.000124575, 0.000202289, 7.25561e-05, 0.00125153, 0.000195685, 0.000298364, 6.305e-05, 0.000101759, 0.000189796, 9.83266e-06, 6.09115e-06, 2.93628e-05, 3.79306e-05, 1.80906e-05, 3.27449e-05, 1.44569e-05, 2.08072e-05, 0.000211307, 2.92737e-05, 2.62217e-05, 5.72919e-05, 0.000113042, 2.7489e-05, 4.75314e-05, 3.24912e-05, 9.47271e-06, 0.000175823, 1.07195e-05, 2.23769e-05, 2.77867e-05, 9.41769e-06, 2.75326e-06, 5.0539e-05, 0.000196899, 1.57362e-05, 9.59799e-05, 3.38195e-05, 7.26347e-06, 6.13557e-05, 5.6595e-05, 2.1883e-05, 3.92613e-05, 2.65449e-05, 5.75036e-05, 0.000152569, 8.00665e-05, 2.52358e-05, 7.63134e-05, 1.58771e-05, 0.00046693, 8.97672e-05, 2.64159e-05, 0.000107967, 0.000105322, 2.51052e-05, 0.000134213, 2.02501e-05, 8.42264e-05, 5.74879e-05, 0.000147237, 8.60201e-05, 0.000159229, 2.82999e-05, 7.0453e-05, 9.804e-05, 1.53984e-05, 0.000442353, 4.83388e-05, 0.000111974, 1.64856e-05, 3.9036e-05, 8.38488e-06, 8.2569e-05, 4.60937e-05, 1.9807e-05, 0.000101196, 0.00014236, 0.000169874, 0.000836153, 9.40354e-05, 4.6951e-05, 0.000131597, 2.86648e-05, 0.000158368, 5.29119e-05, 3.52403e-05, 7.17581e-05, 0.000116447, 0.000253711, 5.35324e-05, 8.56567e-06, 4.87063e-05, 0.000110679, 2.18005e-05, 8.59478e-06, 7.40535e-05, 2.38494e-05, 3.12719e-05, 0.000714874, 0.000145422, 0.000137946, 9.94839e-05, 0.000283478, 0.000357132, 2.73016e-05, 0.0002482, 8.15625e-05, 7.40048e-05, 3.81499e-05, 9.95147e-06, 2.86458e-05, 6.22204e-05, 0.000123885, 8.62779e-05, 3.16152e-05, 2.91354e-05, 5.67827e-05, 0.000652813, 0.000101906, 1.61919e-05, 2.92731e-05, 4.40727e-05, 8.18691e-06, 2.21699e-05, 5.32086e-05, 3.21545e-05, 3.22796e-05, 2.6318e-05, 1.88785e-05, 2.11514e-05, 1.48076e-05, 7.21377e-05, 7.36493e-06, 0.000353744, 0.000141821, 8.97949e-06, 1.61471e-05, 0.000122686, 4.4602e-05, 2.3205e-05, 4.94825e-05, 1.67007e-05, 6.61634e-05, 8.84246e-05, 0.000172353, 7.35944e-05, 0.000391683, 0.000185004, 0.00039224, 0.000324578, 0.000331324, 1.30768e-05, 0.000374572, 9.66308e-05, 0.000116723, 9.00387e-05, 9.85038e-05, 6.74917e-05, 0.000493128, 7.29576e-05, 0.00450054, 0.00298045, 0.00203722, 0.00164224, 0.000846203, 0.00017548, 0.000317891, 0.000788288, 0.000143928, 0.000854663, 0.000351869, 0.000692566, 0.000377429, 0.000629245, 6.21199e-05, 0.000200465, 0.00307031, 0.000723996, 0.000117597, 0.000115785, 5.71359e-05, 0.000496389, 8.51815e-05, 0.000474041, 0.000364161, 2.79947e-05, 6.08602e-05, 0.000209289, 0.00255232, 8.92871e-05, 0.00296907, 0.000557994, 3.34299e-05, 2.18733e-05, 0.00575903, 0.000440953, 0.00127525, 6.44013e-05, 0.0101199, 0.000215603, 4.69506e-05, 0.000803412, 0.0136009, 0.000157733, 0.000238714, 0.000401412, 0.000817349, 0.00053235, 0.00576978, 0.0001202, 0.00019474, 0.00292783, 0.000253787, 0.000986474, 0.000137718, 3.35719e-05, 0.00109508, 3.28824e-05, 0.00050906, 8.51038e-05, 0.000519757, 0.000235538, 0.00788667, 0.00105736, 0.000530407, 0.000325091, 0.00107374, 0.000230244, 0.00076778, 0.000659183, 0.000224508, 0.000202612, 0.000678197, 0.00448022, 0.00289446, 9.32325e-05, 0.00977335, 0.000435942, 0.004958, 0.000386235, 0.00736235, 0.00115626, 0.000177735, 2.56265e-05, 0.0015713, 0.000136755, 0.000602412, 0.0012884, 0.00719902, 0.0119457, 0.000945569, 0.000219249, 0.000347739, 0.00519515, 0.000225742, 0.00246375, 0.000453139, 0.0029661, 0.000804986, 0.000914697, 0.000287989, 0.000264725, 0.000630987, 0.000351136, 0.000733556, 2.37422e-05, 0.000517571, 4.77645e-05, 0.0003319, 0.000405211, 0.000283034, 0.00159746, 0.00177935, 5.88713e-05, 7.11461e-05, 5.91759e-05, 0.000503971, 8.6978e-06, 0.00120087, 0.000813549, 0.000343971, 0.000687556, 0.000340041, 0.000355475, 0.000275208, 0.000696902, 8.2474e-05, 7.0166e-05, 0.000210933, 3.90627e-05, 1.89524e-05, 0.000368501, 0.000351007, 0.00060404, 0.000106084, 0.000331506, 8.32554e-05, 2.15891e-05, 0.000193192, 6.84955e-06, 0.000331523, 3.66652e-05, 1.12194e-05, 1.92205e-05, 2.50185e-05, 5.33112e-05, 1.32986e-05, 8.2467e-05, 5.92258e-05, 0.00283379, 2.84016e-05, 5.96508e-05, 0.000182353, 0.000241587, 4.9405e-05, 0.000256186, 6.64698e-06, 4.18161e-05, 4.28466e-05, 1.34901e-05, 1.77119e-05, 1.13135e-05, 1.55582e-05, 1.01246e-05, 2.15158e-06, 4.84249e-06, 0.000493645, 5.86358e-05, 3.79742e-05, 0.000325346, 0.000130454, 5.81232e-05, 6.95239e-05, 0.000220773, 5.40073e-05, 0.000293129, 9.38502e-05, 3.10892e-05, 0.000135327, 0.000216272, 0.000535513, 0.000271741, 0.000101224, 7.6724e-05, 0.000640805, 0.000273377, 0.00658155, 0.000621818, 0.00113372, 0.000305679, 3.32551e-05, 2.73262e-05, 0.000193103, 0.000359639, 0.00254883, 0.000114939, 5.88418e-05, 8.44255e-05, 8.56286e-05, 0.00014276, 9.35917e-05, 0.000153505, 6.59843e-05, 0.000114629, 0.000208262, 4.78108e-05, 3.78621e-05, 2.35319e-05, 1.77599e-05, 1.24966e-05, 6.25085e-05, 0.000493294, 9.21813e-05, 5.45179e-05, 4.59296e-05, 4.71062e-06, 7.51103e-05, 0.000342672, 0.000133267, 3.98604e-05, 0.000152585, 7.34054e-05, 0.000268039, 3.68195e-05, 0.000300117, 6.67261e-05, 3.72905e-05, 1.53376e-05, 5.58414e-05, 3.97029e-05, 0.00076366, 6.3162e-06, 1.66135e-05, 0.000325842, 0.000388785, 7.0573e-06, 1.1251e-05, 0.000252132, 0.000904097, 5.66675e-05, 5.92502e-05, 0.000555106, 3.88775e-05, 4.98873e-05, 0.000385996, 3.6577e-05, 0.000351803, 0.000243754, 0.0002531, 0.00544143, 0.000563464, 0.00103145, 0.00181722, 0.000466597, 9.86767e-06, 0.000341469, 0.0015705, 0.000206699, 7.92459e-05, 0.000345893, 0.000593111, 3.06207e-05, 8.75688e-05, 3.0376e-05, 0.00197111, 1.03584e-05, 0.000251229, 0.000428186, 2.60413e-05, 1.62735e-05, 0.000118388, 0.000196341, 0.000216079, 0.000269054, 9.98427e-05, 7.99718e-05, 0.000267069, 2.71418e-06, 1.367e-05, 0.000239831, 0.000567004, 0.000292397, 0.000207042, 0.000774038, 0.000156459, 0.00022772, 0.000720916, 0.00062859, 0.00150372, 0.000512822, 0.000119685, 3.59513e-05, 1.95859e-05, 0.000101747, 6.89584e-05, 0.000450811, 0.000977539, 0.000681986, 6.28705e-05, 0.000568139, 0.000504166, 0.000472052, 0.000321606, 0.000639555, 9.82591e-05, 0.000272632, 2.82421e-05, 0.00426243, 0.000141967, 9.05156e-05, 0.000199219, 0.00452672, 3.99683e-05, 0.000368264, 0.0296196, 0.0004709, 7.90032e-05, 0.000176198, 0.000204283, 0.00739515, 1.6471e-05, 4.34332e-05, 0.000124695, 0.000136671, 2.44544e-05, 4.26264e-05, 8.98381e-05, 0.000859267, 0.00728019, 0.00102531, 0.000398277, 0.000125742, 5.01795e-05, 0.000100727, 1.86502e-05, 0.000123926, 0.000238162, 0.000418217, 0.000152855, 0.0018571, 0.000290411, 8.05784e-05, 0.000291634, 0.000174392, 0.000212666, 0.000200596, 6.78554e-05, 0.000258088, 1.28704e-05, 0.000396029, 8.78131e-05, 0.000241314, 0.000312604, 0.000107392, 0.00110502, 0.000166141, 0.000168017, 0.000474729, 6.27281e-05, 3.09645e-05, 2.28196e-05, 0.000283914, 0.00209177, 0.000227949, 0.000394017, 5.04358e-05, 0.00118297, 9.57517e-06, 0.000171489, 0.00078891, 4.71934e-05, 2.50277e-05, 0.000438497, 4.65839e-05, 3.58285e-05, 0.000123489, 0.0143988, 0.000553773, 0.000116806, 0.000316215, 0.000281624, 9.31204e-05, 0.000223639, 0.000307707, 0.000271174, 4.04945e-05, 3.46877e-05, 0.000431537, 0.00151671, 5.3359e-06, 7.53046e-05, 0.000235918, 2.56014e-05, 0.000112486, 0.0014654, 0.000480544, 0.000316342, 0.000544367, 0.000444946, 0.000607547, 0.000481535, 0.000104002, 3.70468e-05, 2.8174e-05, 0.00106104, 5.66228e-05, 0.000109865, 0.000220595, 0.000238312, 0.000102476, 0.000189638, 0.000149645, 0.00118128, 0.000259041, 0.00139206, 8.37191e-05, 0.000226957, 0.000306372, 0.000450006, 0.000106509, 0.000303578, 3.29038e-05, 0.000159814, 9.35702e-05, 0.000326935, 7.2301e-05, 5.81706e-05, 0.000159592, 0.000618135, 0.000134879, 0.000182668, 7.6661e-05, 0.00198678, 0.000125605, 0.000412593, 0.000615485, 4.16444e-05, 0.000203402, 0.00362499, 0.000604073, 0.000812219, 5.87236e-05, 0.000445537, 7.08274e-05, 0.000538608, 0.000330699, 1.94176e-05, 0.000821484, 0.0112315, 0.000932525, 4.65331e-05, 0.000127888, 0.000212339, 9.2608e-05, 0.000237337, 0.000529908, 3.73669e-05, 0.000106617, 0.00372401, 0.00353945, 0.000357516, 0.000152526, 0.000225924, 2.47649e-05, 5.32593e-05, 0.00123404, 0.000897687, 4.55089e-05, 0.000217386, 0.000406262, 3.08005e-05, 0.00455928, 0.000140245, 0.00053089, 0.000170293, 0.000447217, 0.000803549, 5.35199e-05, 4.1243e-05, 5.38527e-05, 0.00086276, 0.000112361, 0.000115841, 0.000209599, 8.73793e-05, 0.015637, 1.19475e-05, 1.12752e-05, 0.00139052, 0.00375697, 0.000647192, 6.92945e-05, 6.92814e-05, 0.00316057, 0.000166111, 0.00758618, 0.000948753, 0.000197994, 5.97069e-05, 0.000606452, 6.79047e-05, 5.83162e-05, 7.09706e-05, 0.00241091, 0.000311754, 8.73588e-05, 4.62264e-05, 7.9378e-05, 0.00141774, 0.00103621, 0.0379155, 0.0388486, 0.000159568, 9.80266e-05, 0.000818292, 0.000181057, 0.000274421, 0.000267676, 0.000226017, 5.04306e-05, 0.000275776, 0.000747819, 0.000149055, 0.0002615, 0.000160578, 8.37152e-05, 1.11533e-05, 9.96574e-05, 6.02464e-05, 0.000143476, 0.000239814, 0.000773004, 0.00441533, 0.000319619, 0.0006442, 5.03002e-05, 1.85892e-05, 8.72792e-06, 0.000120659, 0.00128101, 0.00125803, 7.76589e-05, 0.0045897, 0.000584449, 6.5189e-05, 0.0010469, 9.78898e-05, 1.77179e-05, 3.70118e-05, 5.48802e-06, 0.000212075, 0.000140139, 3.12479e-05, 0.000133606, 6.63303e-05, 0.00745455, 0.000123391, 0.00053998, 0.000146543, 4.19799e-05, 0.000315619, 4.18607e-05, 8.34441e-05, 0.000146504, 0.000260877, 6.31252e-06, 8.27619e-05, 0.00193441, 3.89437e-05, 0.000232354, 0.00077451, 4.3871e-05, 0.000841214, 2.80011e-05, 0.0023251, 0.000533739, 5.10349e-05, 7.1796e-05, 2.07323e-05, 0.000167656, 0.000143678, 0.00014051, 0.000108807, 0.000233951, 0.000541688, 0.000581492, 0.000194971, 6.06314e-05, 7.98194e-05, 0.000168223, 0.000221727, 4.17056e-05, 0.000429627, 0.000114364, 0.000403723, 0.000223281, 0.00434498, 0.000408545, 9.39891e-06, 0.000177339, 0.000601626, 0.000167407, 0.000210305, 0.00486655, 0.000501063, 0.000437227, 0.000286798, 5.71393e-05, 6.61158e-05, 8.59685e-05, 3.14945e-05, 0.000633855, 0.00105124, 0.000168122, 0.000471016, 0.000197255, 0.000241811, 2.63944e-05, 0.000267025, 9.11511e-05, 0.000679203, 0.0557757, 0.00381419, 0.000337966, 0.00482909, 5.72956e-05, 0.000130052, 2.01935e-05, 6.77933e-05, 4.27964e-05, 0.00175907, 0.000649202, 0.000907705, 0.00146462, 0.0166741, 0.0075755, 0.0245835, 4.10026e-06, 0.000940845, 0.000576762, 0.000410642, 0.0242613, 0.00392809, 1.0285e-05, 0.000278193, 8.99299e-05, 0.000228249, 9.60876e-05, 6.41274e-06, 1.52377e-05, 0.000132558, 0.000359305, 0.000768309, 0.000233007, 0.00724834, 4.80489e-05, 3.41695e-05, 0.00499713, 1.23432e-05, 3.43374e-05, 4.30402e-05, 5.59372e-05, 2.3765e-05, 0.00300365, 0.00431638, 0.000161293, 2.90676e-05, 0.000763955, 0.000167918, 0.000183525, 0.000683445, 0.00106749, 2.89752e-05, 0.000364255, 2.42001e-05, 0.00205144, 0.00235832, 0.00116137, 0.00301419, 0.000934434, 0.000845667, 7.64749e-05, 0.000820362, 0.000122261, 9.32608e-05, 0.000223282, 7.85736e-05, 2.60183e-05, 5.02393e-05, 2.43877e-05, 0.0105237, 0.000292717, 0.00139134, 0.00010222, 0.000164221, 3.3359e-05, 6.15358e-05, 8.36216e-05, 0.000747535, 4.55981e-06, 0.000331875, 0.00021054, 0.000624574, 0.000307265, 6.56963e-06, 0.00236067, 0.00044436, 0.000450789, 0.000353041, 0.000100967, 3.37242e-05, 0.00720478, 0.000148686, 3.37161e-05, 0.000218007, 0.000307866, 0.000852728, 1.23465e-05, 0.000321398, 0.00219071, 0.000254209, 0.000577832, 0.000126005, 0.000151009, 9.99839e-05, 0.000903734, 1.52066e-05, 0.000521119, 5.78883e-05, 1.16647e-05, 8.59855e-05, 9.17357e-05, 0.000196617, 1.47663e-05, 4.3448e-05, 0.000330379, 8.82993e-05, 0.00075393, 0.00018865, 2.31207e-05, 4.4101e-05, 0.00244245, 0.000283023, 0.0106583, 0.000117731, 2.23972e-05, 0.000153825, 0.000967888, 0.0001583, 2.60177e-05, 0.000290019, 0.00297588, 0.000398894, 1.71261e-05, 0.00858669, 0.00211729, 1.6242e-05, 0.000301345, 0.00137275, 0.000372956, 3.09104e-05, 0.000351122, 4.80173e-05, 1.63996e-05, 4.22602e-05, 3.19259e-06, 3.63146e-05, 4.45177e-05, 7.56894e-05, 5.70432e-05, 0.000136668, 0.000120945, 7.85435e-05, 4.80372e-05, 9.4714e-06, 0.000304405, 1.76088e-05, 3.26987e-05, 0.00010463, 5.63494e-05, 3.0221e-05, 2.3453e-05, 0.00012479, 8.95265e-06, 6.9025e-05, 1.08093e-05, 0.000699259, 0.000548753, 0.000196507, 0.00022843, 0.00051543, 0.000110283, 0.000173086, 0.000424951, 0.00035482, 1.48939e-05, 1.94998e-05, 0.000484427, 2.33123e-05, 4.86105e-05, 0.000148831, 4.71529e-05, 1.15327e-05, 1.46404e-05, 0.00138581, 9.02665e-05, 4.18611e-05, 0.000265539, 3.91982e-05, 0.0805373, 0.000834312, 0.00716731, 0.000183447, 0.00333792, 0.010186, 0.00180289, 0.00658991, 0.00702397, 0.0418009, 0.00235253, 0.00024491, 0.000449987, 9.01764e-05, 0.000527748, 0.000131736, 3.40312e-05, 0.00435704, 6.92113e-05, 0.000118533, 0.000149927, 4.5531e-05, 3.98839e-05, 5.29356e-06, 4.02724e-05, 2.74253e-05, 8.40502e-05, 6.12322e-05, 0.00344411, 0.000671335], 'classes': 971}]}