培训型号CNN Keras

发布于 2025-01-22 16:09:53 字数 2168 浏览 0 评论 0原文

大家好,我试图使用CNN和Keras培训模型,但是培训还没有完成,我得到了此警告,它停止了培训,我不知道为什么,我不明白问题在哪里建议或我应该在代码中更改的内容

 def myModel():
        no_Of_Filters=60
        size_of_Filter=(5,5) # THIS IS THE KERNEL THAT MOVE AROUND THE IMAGE TO GET THE FEATURES.
                             # THIS WOULD REMOVE 2 PIXELS FROM EACH BORDER WHEN USING 32 32 IMAGE
        size_of_Filter2=(3,3)
        size_of_pool=(2,2)  # SCALE DOWN ALL FEATURE MAP TO GERNALIZE MORE, TO REDUCE OVERFITTING
        no_Of_Nodes = 500   # NO. OF NODES IN HIDDEN LAYERS
        model= Sequential()
        model.add((Conv2D(no_Of_Filters,size_of_Filter,input_shape=(imageDimesions[0],imageDimesions[1],1),activation='relu')))  # ADDING MORE CONVOLUTION LAYERS = LESS FEATURES BUT CAN CAUSE ACCURACY TO INCREASE
        model.add((Conv2D(no_Of_Filters, size_of_Filter, activation='relu')))
        model.add(MaxPooling2D(pool_size=size_of_pool)) # DOES NOT EFFECT THE DEPTH/NO OF FILTERS
     
        model.add((Conv2D(no_Of_Filters//2, size_of_Filter2,activation='relu')))
        model.add((Conv2D(no_Of_Filters // 2, size_of_Filter2, activation='relu')))
        model.add(MaxPooling2D(pool_size=size_of_pool))
        model.add(Dropout(0.5))
     
        model.add(Flatten())
        model.add(Dense(no_Of_Nodes,activation='relu'))
        model.add(Dropout(0.5)) # INPUTS NODES TO DROP WITH EACH UPDATE 1 ALL 0 NONE
        model.add(Dense(noOfClasses,activation='softmax')) # OUTPUT LAYER
        # COMPILE MODEL
        model.compile(Adam(lr=0.001),loss='categorical_crossentropy',metrics=['accuracy'])
        return model
     
     
    ############################### TRAIN
    model = myModel()
    print(model.summary())
    history=model.fit_generator(dataGen.flow(X_train,y_train,batch_size=batch_size_val),steps_per_epoch=steps_per_epoch_val,epochs=epochs_val,validation_data=(X_validation,y_validation),shuffle=1)
WARNING:tensorflow:Your input ran out of data; interrupting training. Make sure that your dataset or generator can generate at least `steps_per_epoch * epochs` batches (in this case, 20000 batches). You may need to use the repeat() function when building your dataset.

hello everyone i am trying to train a model using cnn and keras but the training don't finish and i got this warning and it stops training , i don't know why and i didn't understand where the problem is can anyone gives me a advice or what i should change in the code

 def myModel():
        no_Of_Filters=60
        size_of_Filter=(5,5) # THIS IS THE KERNEL THAT MOVE AROUND THE IMAGE TO GET THE FEATURES.
                             # THIS WOULD REMOVE 2 PIXELS FROM EACH BORDER WHEN USING 32 32 IMAGE
        size_of_Filter2=(3,3)
        size_of_pool=(2,2)  # SCALE DOWN ALL FEATURE MAP TO GERNALIZE MORE, TO REDUCE OVERFITTING
        no_Of_Nodes = 500   # NO. OF NODES IN HIDDEN LAYERS
        model= Sequential()
        model.add((Conv2D(no_Of_Filters,size_of_Filter,input_shape=(imageDimesions[0],imageDimesions[1],1),activation='relu')))  # ADDING MORE CONVOLUTION LAYERS = LESS FEATURES BUT CAN CAUSE ACCURACY TO INCREASE
        model.add((Conv2D(no_Of_Filters, size_of_Filter, activation='relu')))
        model.add(MaxPooling2D(pool_size=size_of_pool)) # DOES NOT EFFECT THE DEPTH/NO OF FILTERS
     
        model.add((Conv2D(no_Of_Filters//2, size_of_Filter2,activation='relu')))
        model.add((Conv2D(no_Of_Filters // 2, size_of_Filter2, activation='relu')))
        model.add(MaxPooling2D(pool_size=size_of_pool))
        model.add(Dropout(0.5))
     
        model.add(Flatten())
        model.add(Dense(no_Of_Nodes,activation='relu'))
        model.add(Dropout(0.5)) # INPUTS NODES TO DROP WITH EACH UPDATE 1 ALL 0 NONE
        model.add(Dense(noOfClasses,activation='softmax')) # OUTPUT LAYER
        # COMPILE MODEL
        model.compile(Adam(lr=0.001),loss='categorical_crossentropy',metrics=['accuracy'])
        return model
     
     
    ############################### TRAIN
    model = myModel()
    print(model.summary())
    history=model.fit_generator(dataGen.flow(X_train,y_train,batch_size=batch_size_val),steps_per_epoch=steps_per_epoch_val,epochs=epochs_val,validation_data=(X_validation,y_validation),shuffle=1)
WARNING:tensorflow:Your input ran out of data; interrupting training. Make sure that your dataset or generator can generate at least `steps_per_epoch * epochs` batches (in this case, 20000 batches). You may need to use the repeat() function when building your dataset.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

饭团 2025-01-29 16:09:53

在使用发电机时,您可以在没有step_per_epoch参数的情况下运行模型,然后让模型弄清楚那里有多少个步骤来覆盖一个时期。

    history=model.fit_generator(dataGen.flow(X_train,y_train,batch_size=batch_size_val),
            epochs=epochs_val,
            validation_data=(X_validation,y_validation),
            shuffle=1)

或者,

您必须计算step_per_epoch并在训练时使用它;

history=model.fit_generator(dataGen.flow(X_train,y_train,batch_size=batch_size_val),
            steps_per_epoch=(data_samples/batch_size)
            epochs=epochs_val,
            validation_data=(X_validation,y_validation),
            shuffle=1)

让我们知道问题是否仍然存在。谢谢!

While using generators, you can either run the model without the step_per_epoch parameter and let the model figure out how many steps are there to cover an epoch.

    history=model.fit_generator(dataGen.flow(X_train,y_train,batch_size=batch_size_val),
            epochs=epochs_val,
            validation_data=(X_validation,y_validation),
            shuffle=1)

OR

you'll have to calculate steps_per_epoch and use it while training as follows;

history=model.fit_generator(dataGen.flow(X_train,y_train,batch_size=batch_size_val),
            steps_per_epoch=(data_samples/batch_size)
            epochs=epochs_val,
            validation_data=(X_validation,y_validation),
            shuffle=1)

Let us know if the issue still persists. Thanks!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文