简体   繁体   English

在 Colab OS 中保存并加载 keras tensorflow model 错误消息

[英]Save and load keras tensorflow model in Colab OSError message

I want to run/fit a model, save this and then load it.我想运行/安装一个 model,保存然后加载它。 I would like to take advantage of the SavedModel format and save the entire model and have a solution for this.我想利用SavedModel格式并保存整个 model 并为此提供解决方案。 This is not the HDF5 format, so not the.h5 format, but turns out that even with.h5 it is not working (see answer/comment below).这不是 HDF5 格式,所以不是 .h5 格式,但事实证明即使使用 .h5 它也不起作用(请参阅下面的答案/评论)。 I work in Colab.我在 Colab 工作。 My code is as follows:我的代码如下:

import tensorflow as tf

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Conv2D, Flatten, Dropout, MaxPooling2D
from tensorflow.keras.preprocessing.image import ImageDataGenerator

import os
import numpy as np
import matplotlib.pyplot as plt
from tensorflow.python.keras.utils.version_utils import training
from tensorflow.keras.optimizers import RMSprop

_URL = 'https://storage.googleapis.com/mledu-datasets/cats_and_dogs_filtered.zip'

path_to_zip = tf.keras.utils.get_file('cats_and_dogs.zip', origin=_URL, extract=True, cache_subdir = '/tmp/catsdogs')

PATH = os.path.join(os.path.dirname(path_to_zip), 'cats_and_dogs_filtered')

training_dir = os.path.join(PATH, 'train')
validation_dir = os.path.join(PATH, 'validation')


train_image_generator = ImageDataGenerator(rescale=1./255)
validation_image_generator = ImageDataGenerator(rescale=1./255)

train_datagen = train_image_generator.flow_from_directory(
  directory=training_dir,
  target_size=(300, 300),
  shuffle=True,
  batch_size=128,
  class_mode='binary'
)

val_datagen = validation_image_generator.flow_from_directory(
  directory=validation_dir,
  target_size=(300, 300),
  batch_size=128,
  class_mode='binary'
)

model = Sequential([
    Conv2D(16, 3, padding = 'same', activation='relu', input_shape=(300, 300 ,3)),
    MaxPooling2D(),
    Conv2D(32, 3, padding = 'same', activation='relu'),
    MaxPooling2D(),
    Conv2D(64, 3, padding = 'same', activation='relu'),
    MaxPooling2D(),
    Dropout(0.2),
    Flatten(),
    Dense(512, activation='relu'),
    Dense(1)
])

model.compile(optimizer='Adam', loss=tf.keras.losses.BinaryCrossentropy(from_logits=True), metrics=['acc'])

model.summary()

history = model.fit(train_datagen,validation_data=val_datagen,epochs=1)

Now I want to save this model and load it:现在我想保存这个 model 并加载它:

model.save('saved_model')

from keras.models import load_model
modeldownload = load_model('saved_model')

However, this does not work (same issue, when I put from keras-models import load_model directly in the beginning where the other imports are).但是,这不起作用(同样的问题,当我将from keras-models import load_model直接放在其他导入所在的开头时)。 I get the following error message:我收到以下错误消息:

OSError: Unable to open file

whole text:全文:

---------------------------------------------------------------------------

OSError                                   Traceback (most recent call last)

<ipython-input-24-1b29d8169144> in <module>()
      1 model.save('saved_model')
----> 2 modeldownload = load_model('saved_model')

4 frames

/usr/local/lib/python3.6/dist-packages/h5py/_hl/files.py in make_fid(name, mode, userblock_size, fapl, fcpl, swmr)
    171         if swmr and swmr_support:
    172             flags |= h5f.ACC_SWMR_READ
--> 173         fid = h5f.open(name, flags, fapl=fapl)
    174     elif mode == 'r+':
    175         fid = h5f.open(name, h5f.ACC_RDWR, fapl=fapl)

h5py/_objects.pyx in h5py._objects.with_phil.wrapper()

h5py/_objects.pyx in h5py._objects.with_phil.wrapper()

h5py/h5f.pyx in h5py.h5f.open()

OSError: Unable to open file (file read failed: time = Wed Jul  8 11:17:03 2020
, filename = 'saved_model', file descriptor = 62, errno = 21, error message = 'Is a directory', buf = 0x7fff87aca540, total read size = 8, bytes this sub-read = 8, bytes actually read = 18446744073709551615, offset = 0)

I tried to play around with us, using different folders, subdirectories and so.我试着和我们一起玩,使用不同的文件夹、子目录等等。 But I did not get it working.但我没有让它工作。 How to do it correctly?如何正确执行? I also tried model.fit(train_datagen,validation_data=val_datagen,epochs=1) , so without the history = , but same error message.我也试过model.fit(train_datagen,validation_data=val_datagen,epochs=1) ,所以没有history = ,但同样的错误信息。

Try this尝试这个

model.save('modelname.h5') 

Then load model using然后使用加载 model

from keras.models import load_model  

modeldownload = load_model('modelname.h5')

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM