[英]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.