Can't save CNN model using tensorflow without training it again

Issue

I’m trying to save a Sequential CNN model. I’ve found that I can save it using model.save() but after I try to load it back using keras.models.load_model() it starts training itself again.

How can I save my model so I don’t need to train it again?

Also while training I’ve got the following warning a couple of times, which says:

/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.

Might this be a cause?

Here is code for this model:

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')

And code of another file which i try to upload model to:

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')```

Solution

It starts training again because you are probably calling model.fit(...) again.

This is sufficient to load back a model:

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

If you want to obtain predictions then you will have something like this, no need to train again:

prediction = model(test_data, training=False)

Answered By – ClaudiaR

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published