简体   繁体   English

尝试在 Colab 中拟合模型时出错,但在 Jupyter 笔记本中工作正常

[英]Error when trying to fit the model in Colab but works fine in Jupyter notebook

I tried to fit the model with my train and test data (about 7GB) in Colab as when using Jupyter notebook with my local machine it takes too long to train.我尝试在 Colab 中使用我的训练和测试数据(大约 7GB)来拟合模型,因为在我的本地机器上使用 Jupyter 笔记本时训练需要很长时间。 However when I tried to use Colab it gives me the below error but it works fine in Jupyter.但是,当我尝试使用 Colab 时,它给了我以下错误,但它在 Jupyter 中运行良好。

    Epoch 1/20
---------------------------------------------------------------------------
UnimplementedError                        Traceback (most recent call last)
<ipython-input-60-677f43e317d7> in <module>()
      6   epochs=20,
      7   steps_per_epoch=len(training_set),
----> 8   validation_steps=len(testing_set)
      9 )

1 frames
/usr/local/lib/python3.7/dist-packages/keras/utils/traceback_utils.py in error_handler(*args, **kwargs)
     65     except Exception as e:  # pylint: disable=broad-except
     66       filtered_tb = _process_traceback_frames(e.__traceback__)
---> 67       raise e.with_traceback(filtered_tb) from None
     68     finally:
     69       del filtered_tb

/usr/local/lib/python3.7/dist-packages/tensorflow/python/eager/execute.py in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
     53     ctx.ensure_initialized()
     54     tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
---> 55                                         inputs, attrs, num_outputs)
     56   except core._NotOkStatusException as e:
     57     if name is not None:

UnimplementedError: Graph execution error:

And I have changed the Runtime type to GPU in colab and I'm currently not using the Colab pro version, datasets are stored in the Google drive.我已经在 colab 中将运行时类型更改为 GPU,我目前没有使用 Colab 专业版,数据集存储在 Google 驱动器中。 I'm a bit confused as the code was working fine without any issue in Jupyter notebook.我有点困惑,因为代码在 Jupyter 笔记本中没有任何问题。

You can view the colab file using the below GitHub link.您可以使用下面的 GitHub 链接查看 colab 文件。https://github.com/ArchieVon/DL/blob/main/ResNet_Test1.ipynbhttps://github.com/ArchieVon/DL/blob/main/ResNet_Test1.ipynb

Changing the IMAGE_SIZE from list to tuple resolves the issue.将 IMAGE_SIZE 从列表更改为元组可以解决此问题。 Please find the working code below.请在下面找到工作代码。

from tensorflow.keras.layers import Input, Lambda, Dense, Flatten
from tensorflow.keras.models import Model
from tensorflow.keras.applications.resnet50 import ResNet50
from tensorflow.keras.applications.resnet50 import preprocess_input
from tensorflow.keras.preprocessing import image
from tensorflow.keras.preprocessing.image import ImageDataGenerator,load_img
from tensorflow.keras.models import Sequential
import numpy as np
from glob import glob

IMAGE_SIZE = (224, 224, 3)

train_path = '/content/dogs_vs_cats/train'
valid_path = '/content/dogs_vs_cats/test'


resnet = ResNet50(input_shape=IMAGE_SIZE, weights='imagenet', include_top=False)
for layer in resnet.layers:
    layer.trainable = False


folders = glob('/content/dogs_vs_cats/train/*')
len(folders) 

x = Flatten()(resnet.output)
prediction = Dense(len(folders), activation='softmax')(x)

model = Model(inputs=resnet.input, outputs=prediction)
#model.summary()


model.compile(
  loss='categorical_crossentropy',
  optimizer='adam',
  metrics=['accuracy']
)

from tensorflow.keras.preprocessing.image import ImageDataGenerator

train_datagen = ImageDataGenerator(rescale = 1./255,
                                   shear_range = 0.2,
                                   zoom_range = 0.2,
                                   horizontal_flip = True)

test_datagen = ImageDataGenerator(rescale = 1./255)

training_set = train_datagen.flow_from_directory(train_path,
                                                 target_size = (224, 224),
                                                 batch_size = 32,
                                                 class_mode = 'categorical')
testing_set = test_datagen.flow_from_directory(valid_path,
                                            target_size = (224, 224),
                                            batch_size = 32,
                                            class_mode = 'categorical')


len(testing_set)
len(training_set)


# fit the model
# Run the cell. It will take some time to execute
r = model.fit(
  training_set,
  validation_data=testing_set,
  epochs=1,
  steps_per_epoch=len(training_set),
  validation_steps=len(testing_set)
)

The output is as follows:输出如下:

Downloading data from https://storage.googleapis.com/tensorflow/keras-applications/resnet/resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5
94773248/94765736 [==============================] - 0s 0us/step
94781440/94765736 [==============================] - 0s 0us/step
Found 20000 images belonging to 2 classes.
Found 5000 images belonging to 2 classes.
625/625 [==============================] - ETA: 0s - loss: 0.9511 - accuracy: 0.6004

Let us know if the issue still persists.让我们知道问题是否仍然存在。 Thanks!谢谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:ModuleNotFoundError:spyder 中没有名为“torch”的模块,在 jupyter notebook 中工作正常 - Error: ModuleNotFoundError: No module named 'torch' in spyder, works fine in jupyter notebook 使用 VSCode 时找不到 sklearn 模块,但在 Jupyter Notebook 中工作正常? - sklearn module not found when using VSCode, but works fine in Jupyter Notebook? 尝试启动 Jupyter 笔记本时出现 DLL 错误 - DLL Error when trying to launch Jupyter notebook 尝试在 jupyter notebook 中拆分数据时出错 - Error when trying to split the data in jupyter notebook 图形在 Jupyter 笔记本中运行良好,但在 pycharm 中运行良好 - Graph works fine in Jupyter notebook but not pycharm 递归错误(Jupyter Notebook/Google Colab) - Recursion Error(Jupyter Notebook/Google Colab) Import flask_wtf 与 jupyter notebook 一起工作正常,但命令提示符出现错误 - Import flask_wtf works fine with jupyter notebook but command prompt rises error Pytorch 运行时错误:Cuda 超出 memory。 与 jupyter notebook 一起工作正常,但不能作为脚本 - Pytorch runtime error: Cuda Out of memory. Works fine with jupyter notebook but doesn't as a script 尝试运行我的 Jupyter Notebook 时出现错误 13 - Error 13 When trying to run my Jupyter Notebook 尝试在 jupyter notebook 上运行 sql 命令时出错 - Error when Trying to run an sql command on jupyter notebook
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM