【发布时间】:2021-06-05 17:11:33
【问题描述】:
我使用以下代码 sn-p 为训练和验证生成器创建了 <keras.preprocessing.image.DirectoryIterator> 对象。
class DataLoader:
@staticmethod
def load_data(data_config, prefix = "blond"):
train_datagen = tf.keras.preprocessing.image.ImageDataGenerator(
preprocessing_function=tf.keras.applications.inception_v3.preprocess_input,
rotation_range=30,
width_shift_range=0.2,
height_shift_range=0.2,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True
)
valid_datagen = tf.keras.preprocessing.image.ImageDataGenerator(
preprocessing_function=tf.keras.applications.inception_v3.preprocess_input,
rotation_range=30,
width_shift_range=0.2,
height_shift_range=0.2,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True
)
train_generator = train_datagen.flow_from_directory(
'data/celeba-dataset/{}-train'.format(prefix),
target_size=(data_config.data.IMG_HEIGHT, data_config.data.IMG_WIDTH),
batch_size=data_config.train.BATCH_SIZE)
然后我创建另一个 Model 类,其中包含以下 load_data 和 train 函数
class Model(BaseModel):
def __init__(self, config):
super().__init__(config)
self.img_height = int(self.config.data.IMG_HEIGHT)
self.img_width = int(self.config.data.IMG_WIDTH)
self.base_model = tf.keras.applications.InceptionV3(weights='imagenet',
include_top=False,
input_shape=(self.img_height, self.img_width, 3))
self.model = None
self.training_samples = int(self.config.data.TRAINING_SAMPLES)
self.batch_size = int(self.config.train.BATCH_SIZE)
self.steps_per_epoch = int(self.training_samples) // int(self.batch_size)
self.num_epochs = int(self.config.train.EPOCHS)
self.train_generator = None
self.validation_generator = None
def load_data(self):
self.train_generator, self.validation_generator = DataLoader().load_data(self.config)
def train(self):
self.model.compile(optimizer=tf.keras.optimizers.SGD(lr=0.0001, momentum=0.9),
loss='categorical_crossentropy',
metrics=[tf.keras.metrics.TopKCategoricalAccuracy(k = 1)])
checkpointer = tf.keras.callbacks.ModelCheckpoint(filepath='weights.best.inc.blond.hdf5',
verbose=1, save_best_only=True)
model_history = self.model.fit(self.train_generator,
validation_data = self.validation_generator,
steps_per_epoch= self.steps_per_epoch,
epochs = self.num_epochs,
class_weight='auto',
callbacks=[checkpointer])
return model_history.history['loss'], model_history.history['val_loss']
在运行以下代码时,
model = Model(CFG)
model.load_data()
model.build()
model.train()
我得到以下 Traceback
Traceback (most recent call last):
File "/Users/sauravmaheshkar/github/compression/train.py", line 14, in <module>
model.train()
File "/Users/sauravmaheshkar/github/compression/model/ForgetModel.py", line 70, in train
callbacks=[checkpointer, ],
File "/Users/sauravmaheshkar/opt/anaconda3/envs/compression/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 1064, in fit
steps_per_execution=self._steps_per_execution)
File "/Users/sauravmaheshkar/opt/anaconda3/envs/compression/lib/python3.6/site-packages/tensorflow/python/keras/engine/data_adapter.py", line 1112, in __init__
model=model)
File "/Users/sauravmaheshkar/opt/anaconda3/envs/compression/lib/python3.6/site-packages/tensorflow/python/keras/engine/data_adapter.py", line 898, in __init__
self._size = len(x)
File "/Users/sauravmaheshkar/opt/anaconda3/envs/compression/lib/python3.6/site-packages/keras_preprocessing/image/iterator.py", line 68, in __len__
return (self.n + self.batch_size - 1) // self.batch_size # round up
TypeError: unsupported operand type(s) for +: 'int' and 'str'
Config.py 文件
"""Project Config in JSON"""
CFG = {
"data": {
"data_folder" : "data/CelebA/",
"images_folder" : "data/CelebA/img_align_celeba/img_align_celeba/",
"IMG_HEIGHT": "218",
"IMG_WIDTH": "178",
"TRAINING_SAMPLES": "10000",
"VALIDATION_SAMPLES": "2000",
"TEST_SAMPLES": "2000",
},
"train": {
"BATCH_SIZE": "64",
"EPOCHS": "10",
}
}
软件包版本
-
tensorflow==2.4.1 -
Keras-Preprocessing==1.1.2
预期输出
运行正常的张量流训练循环
【问题讨论】:
-
堆栈跟踪中引用您的代码的第一行是:文件“/Users/sauravmaheshkar/github/compression/model/ForgetModel.py”,第 70 行,在 train callbacks=[checkpointer, ] , 你省略了
checkpointer的定义。请提供...代表的代码。最终,self.batch_size是str,但是如果没有丢失的代码,就很难确定哪里出了问题。 -
我刚刚添加了整个代码@MichaelRuth
标签: python tensorflow keras tensorflow2.0 tf.keras