Danielvisky
Danielvisky

Reputation: 41

Getting "TypeError: cannot pickle '_thread.RLock' object" when saving model with pickle

I'm trying to save my keras model into a pickle file, but I'm getting this error. What could be a fix? Or what would be the better method to save and load the model? I'm binary predicting 480x640 grayscale images.

Follows my code:

def trainModel(data):
  batch_size = 3
  img_height = 480
  img_width = 640

  trainDataset = tf.keras.preprocessing.image_dataset_from_directory(
    data,
    validation_split=0.25,
    subset="training",
    seed=123,
    image_size=(img_height, img_width),
    batch_size=batch_size,
    #class_names={"nao_doentes", "doentes"}
  )

  valDataset = tf.keras.preprocessing.image_dataset_from_directory(
    data,
    validation_split=0.25,
    subset="validation",
    seed=123,
    image_size=(img_height, img_width),
    batch_size=batch_size,
    #class_names={"nao_doentes", "doentes"}
  )

  AUTOTUNE = tf.data.experimental.AUTOTUNE

  trainDataset = trainDataset.cache().prefetch(buffer_size=AUTOTUNE)
  valDataset = valDataset.cache().prefetch(buffer_size=AUTOTUNE)

  num_classes = 2

  model = tf.keras.Sequential([
    layers.experimental.preprocessing.Rescaling(1./255),
    layers.Conv2D(32, 3, activation='relu'),
    layers.MaxPooling2D(),
    layers.Conv2D(32, 3, activation='relu'),
    layers.MaxPooling2D(),
    layers.Conv2D(32, 3, activation='relu'),
    layers.MaxPooling2D(),
    layers.Flatten(),
    layers.Dense(128, activation='relu'),
    layers.Dense(num_classes)
  ])

  model.compile(
    optimizer='adam',
    loss=tf.keras.losses.BinaryCrossentropy(),
    metrics=['accuracy']
  )

  model.fit(
    trainDataset,
    validation_data=valDataset,
    epochs=10
  )
  return model

model = trainModel(training_data)
with open('model.sav', 'wb') as f:
  pickle.dump(model, f)

with open('model.sav', 'rb') as f:
  model = pickle.load(f)

testing = np.ndarray(shape=(1, 1, 480, 640), dtype=np.float32)
image = load_img(os.path.join(test_data, "doentes/doente_6.jpg"), target_size=(480,640))
x = img_to_array(image)
x = np.expand_dims(x, axis=0)
testing = np.vstack([x])
print(model.predict(testing))

Also, would you be so kind to advise with some good sources of good practices and explanations when the matter is working with image classification situations? I'm new to the area and as such I struggle a bit when searching and linking different sources' informations.

Upvotes: 0

Views: 3868

Answers (1)

0xDECAFC0FFEE
0xDECAFC0FFEE

Reputation: 380

Generally pickle has problems saving ml model weights for pytorch, tensorflow and keras. To save your keras model, check out their tutorials

specifically, try using the functions save and load_module in keras:

model.save('path/to/location')
reconstructed_model = keras.models.load_model("path/to/location")

Upvotes: 2

Related Questions