在推理期间将自定义图像馈入张量流

时间:2018-04-17 20:30:34

标签: python numpy tensorflow machine-learning

我有一个模特喜欢这个

import os
import subprocess
import httplib, urllib

for ip in range(1,255):

   ip_addr = "192.168.1." + str(ip)
   res = subprocess.call(["ping", ip_addr, "-c1", "-W1", "-q"], stdout=open(os.devnull,'w'))
   if res == 0:
      print (ip_addr) + " reachable"
   else:
      print (ip_addr) + " not reachable"

   f = open('state_All.txt','r')
   prestate = f.readline(ip)
   f.close()
   prestate = ord(prestate)
   res = res + 97
   if not prestate == res:
      if prestate == 97:
         conn = httplib.HTTPSConnection("api.pushover.net:443")
         ## The Code in here which I have deleted out is definitely not the problem,
         ## works 100% safe and can't be share beacuse of security problems.
          }), { "Content-type": "application/x-www-form-urlencoded" })
         conn.getresponse()
      else:
         conn = httplib.HTTPSConnection("api.pushover.net:443")
         ## The Code in here which I have deleted out is definitely not the problem,
         ## works 100% safe and can't be share beacuse of security problems.
          }), { "Content-type": "application/x-www-form-urlencoded" })
         conn.getresponse()
   f = open('state_All.txt','w')
   res = str(unichr(res))
   data = (res[ip])
   f.writelines(data)
   f.close()

我用

来调用它
Traceback (most recent call last):
  File "ping_All.py", line 48, in <module>
    data = (res[ip])
IndexError: string index out of range

这里的图像基本上是从像这样的

这样的queuerunner中读取的
def inference(images, reuse=False, trainable=True):
    coarse1_conv = conv2d('coarse1', images, [11, 11, 3, 96], [96], [1, 4, 4, 1], padding='VALID', reuse=reuse, trainable=trainable)
    coarse1 = tf.nn.max_pool(coarse1_conv, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1], padding='VALID', name='pool1')
    coarse2_conv = conv2d('coarse2', coarse1, [5, 5, 96, 256], [256], [1, 1, 1, 1], padding='VALID', reuse=reuse, trainable=trainable)
    coarse2 = tf.nn.max_pool(coarse2_conv, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1], padding='SAME', name='pool1')
    coarse3 = conv2d('coarse3', coarse2, [3, 3, 256, 384], [384], [1, 1, 1, 1], padding='VALID', reuse=reuse, trainable=trainable)
    coarse4 = conv2d('coarse4', coarse3, [3, 3, 384, 384], [384], [1, 1, 1, 1], padding='VALID', reuse=reuse, trainable=trainable)
    coarse5 = conv2d('coarse5', coarse4, [3, 3, 384, 256], [256], [1, 1, 1, 1], padding='VALID', reuse=reuse, trainable=trainable)
    coarse6 = fc('coarse6', coarse5, [6*10*256, 4096], [4096], reuse=reuse, trainable=trainable)
    coarse7 = fc('coarse7', coarse6, [4096, 4070], [4070], reuse=reuse, trainable=trainable)
    coarse7_output = tf.reshape(coarse7, [-1, 55, 74, 1])
    return coarse7_output

我现在正试图通过提供单个图像来运行模型。

logits = model.inference(images, keep_conv, keep_hidden)

我尝试了不同的阅读和插入图片的方法。但是,我不认为我正确地做到了。如何将数据输入模型?

1 个答案:

答案 0 :(得分:0)

嗯,你必须把它放在其他地方! :)即,在feed_dict参数中。 sess.run()的第一个参数是您想要返回的值,因此只是logits。因此,从csv_inputs()的返回值中选择一个图像,假设您将其分配给my_image(确保即使它只是1,也要保留批量维度),然后命令为推断它上面的网络将是:

logits_val = sess.run([ logits ], feed_dict={ images: my_image, keep_conv: 0.8, keep_hidden: 0.5})

请注意,您必须小心使用images,因为它代表了代码不同部分的不同内容。一旦它成为数据的占位符,在其他部分它就会保存数据。