【问题标题】:Error when using Inception on TensorFlow (Same output for all pictures)在 TensorFlow 上使用 Inception 时出错(所有图片的输出相同)
【发布时间】:2016-10-09 23:30:29
【问题描述】:

我正在尝试在 cifar-10 数据集上训练一个网络,但我不想使用图片,而是使用 Inceptions 在最后一层之前的特征。

于是我写了一个小平安pf代码来传递Inception中的所有图片并获取特征,这里是:

def run_inference_on_images(images):
 #Creates graph from saved GraphDef.
 create_graph()

 features_vec = np.ndarray(shape=(len(images),2048),dtype=np.float32)

 with tf.Session() as sess:
   # Some useful tensors:
   # 'pool_3:0': A tensor containing the next-to-last layer containing 2048
   #   float description of the image.
   # 'DecodeJpeg:0': A numpy array of the image
   # Runs the softmax tensor by feeding the image data as input to the graph.
   length = len(images)
   for i in range(length):
       print ('inferencing image number',i,'out of', length)
       features_tensor = sess.graph.get_tensor_by_name('pool_3:0')
       features = sess.run(features_tensor,
                        {'DecodeJpeg:0': images[i]})
       features_vec[i] = np.squeeze(features)
 return features_vec

“图像”是 CIFAR-10 数据集。这是一个形状为 (50000,32,32,3) 的 numpy 数组

我面临的问题是,即使我将不同的图片提供给“sess.run”部分,“特征”输出总是相同的。 我错过了什么吗?

【问题讨论】:

    标签: python tensorflow deep-learning


    【解决方案1】:

    我能够解决这个问题。似乎 Inception 不像我想的那样使用 numPy 数组,所以我将数组转换为 JPEG 图片,然后才将其输入网络。

    以下是有效的代码(其余相同):

    def run_inference_on_images(images):
      # Creates graph from saved GraphDef.
      create_graph()
    
      features_vec = np.ndarray(shape=(len(images),2048),dtype=np.float32)
      with tf.Session() as sess:
        features_tensor = sess.graph.get_tensor_by_name('pool_3:0')
        length = len(images)
        for i in range(length):
            im = Image.fromarray(images[i],'RGB')
            im.save("tmp.jpeg")
            data = tf.gfile.FastGFile("tmp.jpeg", 'rb').read()
            print ('inferencing image number',i,'out of', length)
            features = sess.run(features_tensor,
                            {'DecodeJpeg/contents:0': data})
            features_vec[i] = np.squeeze(features)       
       return features_vec
    

    【讨论】:

    • 我遇到了同样的问题。你能给我一些你似乎正在使用的 Image lib 的信息吗?您还能够找到将图像(im)直接输入模型的方法,而无需写入磁盘并在其上调用 FastGFile 吗?这似乎是一件低效的事情,特别是如果我们要处理视频数据等
    • 我正在使用 CIFAR-10 数据集。其他的方法我没试过,暂时不用了,不知道有没有更高效的方法
    【解决方案2】:

    不确定。但你可能会尝试移动你的线路

    features_tensor = sess.graph.get_tensor_by_name('pool_3:0')
    

    作为

    features_tensor = tf.get_tensor_by_name('pool_3:0')
    

    从推理部分到模型创建部分

    【讨论】:

    • 这不会编译。也尝试了 tf.graph.get_tensor_by_name('pool_3:0'),也没有编译
    猜你喜欢
    • 1970-01-01
    • 2017-10-24
    • 2022-11-29
    • 1970-01-01
    • 2017-08-18
    • 1970-01-01
    • 2017-05-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多