AttributeError: 'ProgbarLogger' 对象没有属性 'log_values'

0 python numpy spyder deep-learning keras

我尝试使用 spyder运行这个分割模型。当我运行时data.py,我收到此消息

文件“C:/Users/achaire/Downloads/Compressed/ultrasound-nerve-segmentation-master/ultrasound-nerve-segmentation-master/data.py”, line 19, in create_train_data imgs = np.ndarray((total, image_rows, image_cols), dtype=np.uint8)

类型错误:“float”对象不能解释为整数

代码是:

**14** def create_train_data():
**15** train_data_path = os.path.join(data_path, 'train')
**16** images = os.listdir(train_data_path)
**17** total = len(images) / 2

**18** imgs = np.ndarray((total, image_rows, image_cols), dtype=np.uint8)
**19** imgs_mask = np.ndarray((total, image_rows, image_cols), dtype=np.uint8)
Run Code Online (Sandbox Code Playgroud)

我已经更换线17通过total = int(len(images) / 2)np.uint8通过float在线路1819。问题已经解决了。

当我跑步时,train.py我收到了这条消息

runfile('C:/Users/achaire/Downloads/Compressed/ultrasound-nerve-segmentation-master/ultrasound-nerve-segmentation-master/train.py', wdir='C:/Users/achaire/Downloads/Compressed/ultrasound-nerve-segmentation-master/ultrasound-nerve-segmentation-master')
------------------------------
Loading and preprocessing train data...
------------------------------
------------------------------
Creating and compiling model...
------------------------------
C:\Users\achaire\Anaconda3\lib\site-packages\numpy\core\fromnumeric.py:3118: RuntimeWarning: Mean of empty slice.
  out=out, **kwargs)
C:\Users\hamdi\Anaconda3\lib\site-packages\numpy\core\_methods.py:140: RuntimeWarning: Degrees of freedom <= 0 for slice
  keepdims=keepdims)
------------------------------
Fitting model...
------------------------------
C:/Users/achaire/Downloads/Compressed/ultrasound-nerve-segmentation-master/ultrasound-nerve-segmentation-master/train.py:119: UserWarning: The `nb_epoch` argument in `fit` has been renamed `epochs`.
  callbacks=[model_checkpoint])
Train on 0 samples, validate on 0 samples
Epoch 1/20
Traceback (most recent call last):

  File "<ipython-input-15-f713d62eb4dc>", line 1, in <module>
    runfile('C:/Users/achaire/Downloads/Compressed/ultrasound-nerve-segmentation-master/ultrasound-nerve-segmentation-master/train.py', wdir='C:/Users/achaire/Downloads/Compressed/ultrasound-nerve-segmentation-master/ultrasound-nerve-segmentation-master')

  File "C:\Users\achaire\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 827, in runfile
    execfile(filename, namespace)

  File "C:\Users\achaire\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "C:/Users/achaire/Downloads/Compressed/ultrasound-nerve-segmentation-master/ultrasound-nerve-segmentation-master/train.py", line 153, in <module>
    train_and_predict()

  File "C:/Users/achaire/Downloads/Compressed/ultrasound-nerve-segmentation-master/ultrasound-nerve-segmentation-master/train.py", line 119, in train_and_predict
    callbacks=[model_checkpoint])

  File "C:\Users\achaire\Anaconda3\lib\site-packages\keras\engine\training.py", line 1039, in fit
    validation_steps=validation_steps)

  File "C:\Users\achaire\Anaconda3\lib\site-packages\keras\engine\training_arrays.py", line 217, in fit_loop
    callbacks.on_epoch_end(epoch, epoch_logs)

  File "C:\Users\achaire\Anaconda3\lib\site-packages\keras\callbacks.py", line 79, in on_epoch_end
    callback.on_epoch_end(epoch, logs)

  File "C:\Users\achaire\Anaconda3\lib\site-packages\keras\callbacks.py", line 338, in on_epoch_end
    self.progbar.update(self.seen, self.log_values)

AttributeError: 'ProgbarLogger' object has no attribute 'log_values'
Run Code Online (Sandbox Code Playgroud)

我有最新版本的anacondakeraspython...

小智 5

Answer 1: If the error only occurs when you use smaller datasets, you’re very likely using datasets small enough to not have a single sample in the validation set.

Thus it cannot calculate a validation loss.

Answer 2: I up-voted the previous answer as it gave me the insight to verify the data and inputs to the fit_generator function and find out what the root cause of the issue actually was. In summary, in cases where my dataset was small, I calculated validation_steps and steps_per_epoch which turned out to be zero (0) which caused the error.

I suppose the better longer-term answer, perhaps for the Keras team, is to cause an error/exception in fit_generator when these values are zero, which would probably lead to a better understanding about how to address this issue.

Answer 3: The error occurs to us because we forgot to set validation_data in fit() method, while used 'callbacks': [keras.callbacks.EarlyStopping(monitor='val_loss', patience=1)],

Code causing error is:

self.model.fit(
        x=x_train,
        y=y_train,
        callbacks=[keras.callbacks.EarlyStopping(monitor='val_loss', patience=1)],
        verbose=True)
Adding validation_data=(self.x_validate, self.y_validate), in fit() fixed:

self.model.fit(
        x=x_train,
        y=y_train,
        callbacks=[keras.callbacks.EarlyStopping(monitor='val_loss', patience=1)],
        validation_data=(x_validate, y_validate),
        verbose=True)
Run Code Online (Sandbox Code Playgroud)

Answer 4: This error occurs due to the smaller dataset, to resolve this, increase the train times and split the train set in 80:20.

Reference: https://inneka.com/ml/kr/keras-early-stopping-callback-error-val_loss-metric-not-available/