英文:
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_q
和 low_q
是图片路径的列表
我一直收到错误:TypeError: 'NoneType' object is not callable
在不使用 tf.keras.utils.Sequence()
的情况下,代码可以正常工作,但我需要使用它
我做错了什么?
此外,有人要求我在 Keras 上完成,如果可能的话,我宁愿使用 Torch
我尝试了不同的文件、不同的格式等。如果删除类,它可以运行,但我需要它
<details>
<summary>英文:</summary>
I'm working with heavy dataset of pictures (1920x1088), that's why I'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('float32') / 255.0, np.array([cv2.resize(cv2.imread(file_name), (1920, 1088)) for file_name in batch_y]).astype('float32') / 255.0
My model looks like:
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_q` and `low_q` are lists of paths to the pictures
I keep getting an error: `TypeError: 'NoneType' 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'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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论