Keras model.fit 返回 NoneType 对象。

huangapple go评论54阅读模式
英文:

Keras model.fit returns NoneType object

问题

我正在处理大型图片数据集(1920x1088),所以我决定使用 `tf.keras.utils.Sequence()` 加载它
这是代码的样子:
class Pictures(keras.utils.Sequence):
    
    def __init__(self, x_set, y_set, batch_size):
        self.x, self.y = x_set, y_set
        self.batch_size = batch_size
      
    def load_sample(self, file):
        image = Image.open(file)    # Opening the image
        image.load()    # Loading it
        return image
  
    def __len__(self):
        return math.ceil(len(self.x) / self.batch_size)

    def __getitem__(self, idx):
        batch_x = self.x[idx * self.batch_size:(idx + 1) * self.batch_size]
        batch_y = self.y[idx * self.batch_size:(idx + 1) * self.batch_size]
        
        return np.array([cv2.resize(cv2.imread(file_name), (1920, 1088)) for file_name in batch_x]).astype('float32') / 255.0, np.array([cv2.resize(cv2.imread(file_name), (1920, 1088)) for file_name in batch_y]).astype('float32') / 255.0 
我的模型如下:
model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.001), loss='mean_absolute_error', metrics=['acc'])
model.fit(Pictures(high_q, low_q, 1), epochs=7)

high_qlow_q 是图片路径的列表

我一直收到错误:TypeError: 'NoneType' object is not callable

在不使用 tf.keras.utils.Sequence() 的情况下,代码可以正常工作,但我需要使用它

我做错了什么?

此外,有人要求我在 Keras 上完成,如果可能的话,我宁愿使用 Torch

我尝试了不同的文件、不同的格式等。如果删除类,它可以运行,但我需要它


<details>
<summary>英文:</summary>

I&#39;m working with heavy dataset of pictures (1920x1088), that&#39;s why I&#39;ve decided to load it with `tf.keras.utils.Sequence()`
This is how it looks like:

class Pictures(keras.utils.Sequence):

def __init__(self, x_set, y_set, batch_size):
    self.x, self.y = x_set, y_set
    self.batch_size = batch_size
  
def load_sample(self, file):
    image = Image.open(file)    # Открываем изображение
    image.load()    # Загружаем его
    return image

def __len__(self):
    return math.ceil(len(self.x) / self.batch_size)

def __getitem__(self, idx):
    batch_x = self.x[idx * self.batch_size:(idx + 1) *
    self.batch_size]
    batch_y = self.y[idx * self.batch_size:(idx + 1) *
    self.batch_size]
    
    return np.array([cv2.resize(cv2.imread(file_name), (1920, 1088)) for file_name in batch_x]).astype(&#39;float32&#39;) / 255.0, np.array([cv2.resize(cv2.imread(file_name), (1920, 1088)) for file_name in batch_y]).astype(&#39;float32&#39;) / 255.0 

My model looks like:

    model.compile(optimizer = tf.keras.optimizers.Adam(learning_rate = 0.001), loss = &#39;mean_absolute_error&#39;,
                  metrics = [&#39;acc&#39;])
    model.fit(Pictures(high_q, low_q, 1), epochs = 7)

`high_q` and `low_q` are lists of paths to the pictures

I keep getting an error: `TypeError: &#39;NoneType&#39; object is not callable`

The code works fine without `tf.keras.utils.Sequence()`, but I need to keep it

What am I doing wrong?

Also, I was asked to do it on Keras, if I could, I would rather do it with Torch

I tried different files, different formats etc.
It works if I&#39;ll delete the class, but I need it

</details>


# 答案1
**得分**: 0

这将正常工作,如果您指定fit方法的参数x:`model.fit(x = Pictures(high_q, low_q, 1), epochs = 7)`。

<details>
<summary>英文:</summary>

It should work if you specify the parameter x of the fit method: `model.fit(x = Pictures(high_q, low_q, 1), epochs = 7)`


</details>



huangapple
  • 本文由 发表于 2023年2月19日 19:59:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/75499991.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定