keras 无法使用tensorflow 保存CNN模型,除非再次对其进行训练

zvokhttg  于 2022-11-13  发布在  其他
关注(0)|答案(2)|浏览(302)

我正在尝试保存一个Sequential CNN模型。我发现我可以使用model.save()保存它,但是在我尝试使用keras.models.load_model()重新加载它之后,它又开始训练自己。
如何保存我的模型,以便不需要再次训练它?
在训练中,我也多次收到以下警告,内容如下:

/15 [=>............................] - ETA: 39s - loss: 0.6936 - accuracy: 0.50782022-10-11 
17:31:06.794142: W tensorflow/tsl/framework/cpu_allocator_impl.cc:82] 
Allocation of 358875136 exceeds 10% of free system memory.

这可能是一个原因吗?
下面是此模型的代码:

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
import pickle
PATH = 'cats_and_dogs\cats_and_dogs'

train_dir = os.path.join(PATH, 'train')
validation_dir = os.path.join(PATH, 'validation')
test_dir = os.path.join(PATH, 'test')


# Variables for pre-processing and training.
batch_size = 128
epochs =1
IMG_HEIGHT = 150
IMG_WIDTH = 150

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

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

val_data_gen = validation_image_generator.flow_from_directory(validation_dir, target_size=(IMG_HEIGHT, IMG_WIDTH),  class_mode='categorical',batch_size=batch_size)
test_data_gen = test_image_generator.flow_from_directory(test_dir, target_size=(IMG_HEIGHT, IMG_WIDTH), classes ='.',class_mode='categorical',  batch_size=batch_size, shuffle = False)
#I,ve found that you can use classes = ".", to get test data labels (labels when there are no subdirectories ))
from tensorflow.python.framework.func_graph import flatten

model = tf.keras.Sequential()   
model.add(tf.keras.layers.Conv2D(32, (3,3) , input_shape = (150,150,3)))
model.add(tf.keras.layers.MaxPool2D(pool_size=(2, 2)))
model.add(tf.keras.layers.Conv2D(64, (3,3),activation = 'relu'))
model.add(tf.keras.layers.MaxPool2D(pool_size=(2, 2)))
model.add(tf.keras.layers.Conv2D(64, (3,3),activation = 'relu'))
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(64))
model.add(tf.keras.layers.Dense(1,activation = 'sigmoid'))
model.compile(optimizer='adam',
              loss=tf.keras.losses.BinaryCrossentropy(),
              metrics=['accuracy'])
model.fit(train_data_gen,
            epochs=epochs, 
            batch_size = batch_size,
            validation_data=val_data_gen,
            steps_per_epoch =2000//batch_size, 
            validation_steps=800//batch_size)


model.save('CatDog.h5')

和代码的另一个文件,我试图上传模型:

import tensorflow as tf
import pandas
import tkinter
import os
from CNNmodel import IMG_HEIGHT, IMG_WIDTH
from tensorflow.keras.preprocessing.image import ImageDataGenerator #type: ignore
from tensorflow import keras
model = keras.models.load_model('CatDog.h5')```
csga3l58

csga3l581#

它会再次开始训练,因为您可能会再次呼叫model.fit(...)
这足以载入回模型:

from tensorflow import keras
model = keras.models.load_model('path/to/location')

如果你想获得预测,那么你将有这样的东西,不需要再次训练:

prediction = model(test_data, training=False)
ogq8wdun

ogq8wdun2#

编辑:实际上我修复了它,感谢帮助,在我的第二个文件中,我从模型创建文件中导入了一些变量,
from CNNmodel import IMG_HEIGHT, IMG_WIDTH
我猜model.fit不知怎么又被调用了

相关问题