【问题标题】:Tensorflow fails to interpret what is stored in numpy ndarrayTensorflow 无法解释存储在 numpy ndarray 中的内容
【发布时间】:2021-03-30 16:29:17
【问题描述】:

你好所以我有一个带有url的pandas df,然后我下载/加载缓存,然后将其存储到df中。出现问题是因为 pandas 将 numpy 数组存储为 ndarrays,因此它们的形状丢失了。 有没有办法告诉 tensorflow 存储数组的形状?

def NN(self):
    #Trains on validation then commence batch prediction
    data = self.category_validation.agg({'URL':self.process_image,'label':self.le.fit_transform}).dropna()

    print(data['URL'].values[0])
    print(data['URL'].values[0].shape)
    print(data['URL'].values.shape)
    exit(1)


    #One of Keras' best templates
    self.nn = model(...)

    #Compile the model
    self.nn.compile(...)

    #Fit the first instance of the data
    self.nn.fit(data['URL'].values,data['label'].values)

tf.Tensor(..., shape=(299, 299, 3), dtype=float32) (299, 299, 3) (490,)

ValueError:无法将 NumPy 数组转换为张量(不支持的对象类型 tensorflow.python.framework.ops.EagerTensor)。

    def process_image(self,url):
        #Read image from filepath and reshape it to the appropriate shape for model 
        path = "path/"+self.clean_url(url)

        #Checks if files exists, if not it tries to download if that doesn't work
        if os.path.exists(path):
            image = tf.keras.preprocessing.image.load_img(path,target_size=(299,299))
            image = tf.keras.preprocessing.image.img_to_array(image)
        elif self.get_image(url) == 0:
            return float('nan')
        else:
            image = tf.keras.preprocessing.image.load_img(path,target_size=(299,299))
            image = tf.keras.preprocessing.image.img_to_array(image)
            
        return image/255

【问题讨论】:

    标签: python pandas numpy tensorflow


    【解决方案1】:

    你愿意改变吗

    self.nn.fit(data['URL'].values,data['label'].values)

    self.nn.fit(data['URL'].to_numpy(),data['label'].to_numpy())

    【讨论】:

    • @user11145590 好的,我需要你发送存储在“self.process_image”变量中的数据。需要检查它以了解问题。
    • 我无法提供数据。但我可以显示 process_image 函数
    • 当然,搜索了一下,发现URL = np.asarray(data['URL']).astype("float32") label = np.asarray(data['label']).astype("float32") # Apply as the type of your label self.nn.fit(URL, label)
    • 遗憾的是没有运气,仍然说转换不够。我目前的想法是标签编码器来自 sklearn,因此可能与 TF 不兼容。
    • 拒绝我的最后一个假设,它仍然因为“URL”的东西而失败
    【解决方案2】:

    好的,df.apply/agg 函数假定 numpy 数组的形状不明确。因此,我需要手动使用列表和 for 循环来迭代值并将它们放入 tmp 列表中,然后转换为 numpy 数组,然后才能将它们转换为张量。好痛啊。

    l = []
    
    for image in df['URL'].values:
        l.append(image)
    x_train = np.array(l)
    ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-21
      相关资源
      最近更新 更多