步骤数未知-在Google Colab Pro上训练卷积神经网络 [英] Unknown number of steps - Training convolution neural network at Google Colab Pro

查看:91
本文介绍了步骤数未知-在Google Colab Pro上训练卷积神经网络的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Google Colab Pro 上运行(训练)我的 CNN ,当我运行代码时,一切都还好,但是它不知道步骤,因此创建了无限循环.

I am trying to run (training) my CNN at Google Colab Pro, when I run my code, all is allright, but It does not know the number of steps, so an infinite loop is created.

Mounted at /content/drive
2.2.0-rc3
Found 10018 images belonging to 2 classes.
Found 1336 images belonging to 2 classes.
WARNING:tensorflow:`period` argument is deprecated. Please use `save_freq` to specify the frequency in number of batches seen.
Epoch 1/300
      8/Unknown - 364s 45s/step - loss: 54.9278 - accuracy: 0.5410

我正在使用 ImageDataGenerator()加载图像.我该如何解决?

I am using ImageDataGenerator() for loadings images. How can I fix it?

推荐答案

迭代器不存储任何内容,它动态地生成数据.使用数据集或数据集迭代器时,必须提供 steps_per_epoch .在迭代之前,迭代器的长度是未知的.您可以将 len(datafiles)显式传递给 .fit 函数.因此,您需要提供steps_per_epoch,如下所示.

An iterator does not store anything, it generates the data dynamically. When you are using a dataset or dataset iterator, you must provide steps_per_epoch. The length of an iterator is unknown until you iterate through it. You could explicitly pass len(datafiles) into the .fit function. So, You need to provide steps_per_epoch as shown below.

model.fit_generator(
    train_data_gen,
    steps_per_epoch=total_train // batch_size,
    epochs=epochs,
    validation_data=val_data_gen,
    validation_steps=total_val // batch_size
)

此处

steps_per_epoch:整数或无.总步骤数(批次样本),然后再声明一个纪元并开始下一个纪元时代.使用输入张量进行训练时,例如TensorFlow数据张量,默认值None等于您的样本数量数据集除以批处理大小;如果无法确定,则为1.如果x是tf.data数据集,并且'steps_per_epoch'为None,则该纪元将一直运行到输入数据集用尽为止.这个说法不是数组输入支持.

steps_per_epoch: Integer or None. Total number of steps (batches of samples) before declaring one epoch finished and starting the next epoch. When training with input tensors such as TensorFlow data tensors, the default None is equal to the number of samples in your dataset divided by the batch size, or 1 if that cannot be determined. If x is a tf.data dataset, and 'steps_per_epoch' is None, the epoch will run until the input dataset is exhausted. This argument is not supported with array inputs.

我注意到您正在使用二进制分类.使用 ImageDataGenerator 时要记住的另一件事是提供 class_mode ,如下所示.否则,将存在错误(在keras中)或50%的准确性(在tf.keras中).

I notice you are using binary classification. One more thing to remember when you use ImageDataGenerator is to provide class_mode as shown below. Otherwise, there will be a bug (in keras) or 50% accuracy (in tf.keras).

train_data_gen = train_image_generator.flow_from_directory(batch_size=batch_size,
                                                           directory=train_dir,
                                                           shuffle=True,
                                                           target_size=(IMG_HEIGHT, IMG_WIDTH),class_mode='binary') #

这篇关于步骤数未知-在Google Colab Pro上训练卷积神经网络的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆