提问者:小点点

Tensorflow无法解释存储在Numpy ndarray中的内容


你好,所以我有一个带有网址的熊猫df,然后下载/加载缓存,然后存储到df中。问题出现是因为熊猫将Numpy数组存储为ndarray,因此它们的形状会丢失。有什么方法可以告诉tenstorflow存储数组的形状吗?

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.张量(...,形状=(299, 299, 3),dtype=浮点32) (299, 299, 3) (490,)

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

    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

共2个答案

匿名用户

你愿意改变吗

self.nn.fit(data['URL']. value, data['tag']. value)

self.nn.fit数据['URL'].to_numpy(),数据['标签'].to_numpy())

匿名用户

好的,df.apply/agg函数假设Numpy数组的形状不明确。因此,我需要手动使用列表和for循环来迭代这些值,并将它们放入tmp列表中,然后将其转换为Numpy数组,只有这样才能将它们转换为张量。多么痛苦。

l = []

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