使用Keras 的Model.fit_generator报错StopIteration

使用Keras 的Model.fit_generator报错StopIteration

之前也遇到过这个问题,解决了之后没记下来,最近跑之前代码又出现这个,废了时间去找答案,还是要勤劳点做学习记录才行。

报错如下,问题就是批量产生的数据没有成功一批批地导入。

Epoch 1/100

Epoch 00001: CosineAnnealingScheduler setting learning rate to 0.001.
    1/20843 [..............................] - ETA: 145:53:01 - loss: 13.0753 - acc: 0.0000e+00Traceback (most recent call last):
  File "train-v2-20191123.py", line 311, in <module>
    main()
  File "train-v2-20191123.py", line 275, in main
    verbose=1)
  File "/home/jiajie/anaconda3/envs/py35/lib/python3.5/site-packages/keras/legacy/interfaces.py", line 91, in wrapper
    return func(*args, **kwargs)
  File "/home/jiajie/anaconda3/envs/py35/lib/python3.5/site-packages/keras/engine/training.py", line 1415, in fit_generator
    initial_epoch=initial_epoch)
  File "/home/jiajie/anaconda3/envs/py35/lib/python3.5/site-packages/keras/engine/training_generator.py", line 177, in fit_generator
    generator_output = next(output_generator)
  File "/home/jiajie/anaconda3/envs/py35/lib/python3.5/site-packages/keras/utils/data_utils.py", line 785, in get
    raise StopIteration()
StopIteration

解决方法如下:
要注意两点:

  • 第一点是:要在循环输出部分加上while 1 :
  • 第二点是:计算数据累计次数的参数要记得归零,即下面代码中的cnt
cnt=0
x=[]
y=[]
while 1for path in dirpath:
        X = cv2.imread(picpath,cv2.IMREAD_COLOR).astype(np.float32) / 255
        label = getTwoDimensionListIndex(gt2,name)
        x.append(X)
        y.append(label)
        cnt += 1
        if cnt == batch_size :
            cnt=0  #don't forget to set this number to 0
            yield (np.array(x),np.array(y))
            x=[]
            y=[]

基本问题就是上面两点,不要漏了就能解决StopIteration的问题了。

你可能感兴趣的:(Keras,ISSUES,Keras,StopIteration,fit_generation)