【问题标题】:Cannot create model for machine learning无法为机器学习创建模型
【发布时间】:2020-07-19 22:16:50
【问题描述】:

我正在创建一个用于检测脑肿瘤的 python 应用程序。

关于数据: 数据集包含 2 个文件夹:yes 和 no,其中包含 253 个脑 MRI 图像。文件夹 yes 包含 155 个脑部 MRI 肿瘤图像,文件夹 no 包含 98 个非肿瘤脑 MRI 图像。

# tensorboard
log_file_name = f'brain_tumor_detection_cnn_{int(time.time())}'
tensorboard = TensorBoard(log_dir=f'logs/{log_file_name}')

# checkpoint
# unique file name that will include the epoch and the validation (development) accuracy
filepath="cnn-parameters-improvement-{epoch:02d}-{val_acc:.2f}"
# save the model with the best validation (development) accuracy till now
checkpoint = ModelCheckpoint("models/{}.model".format(filepath, monitor='val_acc', verbose=1, save_best_only=True, mode='max'))


# ## Train the model

model.fit(x=X_train, y=y_train, batch_size=32, epochs=10, validation_data=(X_val, y_val), callbacks=[tensorboard, checkpoint])

在训练模型时,我收到以下错误:

Epoch 1/10
91/91 [==============================] - ETA: 0s - loss: 0.7457 - accuracy: 0.6735
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
c:\python38\lib\site-packages\tensorflow\python\keras\callbacks.py in _get_file_path(self, epoch, logs)
   1243         # placeholders can cause formatting to fail.
-> 1244         return self.filepath.format(epoch=epoch + 1, **logs)
   1245       except KeyError as e:

KeyError: 'val_acc'

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
<ipython-input-20-b50661a1419b> in <module>
      1 start_time = time.time()
      2 
----> 3 model.fit(x=X_train, y=y_train, batch_size=32, epochs=10, validation_data=(X_val, y_val), callbacks=[tensorboard, checkpoint])
      4 
      5 end_time = time.time()

c:\python38\lib\site-packages\tensorflow\python\keras\engine\training.py in _method_wrapper(self, *args, **kwargs)
     64   def _method_wrapper(self, *args, **kwargs):
     65     if not self._in_multi_worker_mode():  # pylint: disable=protected-access
---> 66       return method(self, *args, **kwargs)
     67 
     68     # Running inside `run_distribute_coordinator` already.

c:\python38\lib\site-packages\tensorflow\python\keras\engine\training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_batch_size, validation_freq, max_queue_size, workers, use_multiprocessing, **kwargs)
    811           epoch_logs.update(val_logs)
    812 
--> 813         callbacks.on_epoch_end(epoch, epoch_logs)
    814         if self.stop_training:
    815           break

c:\python38\lib\site-packages\tensorflow\python\keras\callbacks.py in on_epoch_end(self, epoch, logs)
    363     logs = self._process_logs(logs)
    364     for callback in self.callbacks:
--> 365       callback.on_epoch_end(epoch, logs)
    366 
    367   def on_train_batch_begin(self, batch, logs=None):

c:\python38\lib\site-packages\tensorflow\python\keras\callbacks.py in on_epoch_end(self, epoch, logs)
   1175           self._save_model(epoch=epoch, logs=logs)
   1176       else:
-> 1177         self._save_model(epoch=epoch, logs=logs)
   1178     if self.model._in_multi_worker_mode():
   1179       # For multi-worker training, back up the weights and current training

c:\python38\lib\site-packages\tensorflow\python\keras\callbacks.py in _save_model(self, epoch, logs)
   1194                   int) or self.epochs_since_last_save >= self.period:
   1195       self.epochs_since_last_save = 0
-> 1196       filepath = self._get_file_path(epoch, logs)
   1197 
   1198       try:

c:\python38\lib\site-packages\tensorflow\python\keras\callbacks.py in _get_file_path(self, epoch, logs)
   1244         return self.filepath.format(epoch=epoch + 1, **logs)
   1245       except KeyError as e:
-> 1246         raise KeyError('Failed to format this callback filepath: "{}". '
   1247                        'Reason: {}'.format(self.filepath, e))
   1248     else:

KeyError: 'Failed to format this callback filepath: "models/cnn-parameters-improvement-{epoch:02d}-{val_acc:.2f}.model". Reason: \'val_acc\''

【问题讨论】:

  • 请删除与错误无关的所有内容(您的crop_brain_contour 函数以及model.fit 之后的所有内容,因此永远不会作为第一个明显的候选人执行) . SO 不能通过按原样抛出我们所有的代码来工作,并且期望您在调整代码方面付出最小的努力。看看为什么a wall of code isn't helpful。
  • @desertnaut 我已经编辑了代码。
  • 你可以做得更好(已编辑);尝试完全删除检查点,fit 是否运行没有问题?

标签: python tensorflow machine-learning keras deep-learning


【解决方案1】:

如果您在 keras 中使用 tensorflow,请尝试使用 val_accuracy 而不是 val_acc

【讨论】:

    猜你喜欢
    • 2019-09-05
    • 2020-12-02
    • 2019-03-21
    • 2017-06-02
    • 1970-01-01
    • 2017-09-16
    • 2016-10-27
    • 1970-01-01
    • 2022-12-10
    相关资源
    最近更新 更多