title: trying to pickle ML model can't pickle _thread.RLock objects in google colab
body: <p>I am training a MNIST dataset using CNN in google colab and want to save the model using pickle and when i try saving the model I get the error <strong>can't pickle _thread.RLock objects</strong></p>
  <h1>My Code</h1>
  <pre><code>import pickle
  import numpy as np
  import tensorflow as tf
  import matplotlib.pyplot as plt
  import keras
  from keras.datasets import mnist
  from keras.utils import to_categorical
  from keras.models import Sequential
  from keras.layers import Conv2D , MaxPooling2D, Dense, Flatten,Dropout
  from keras.optimizers import Adam
  from keras.preprocessing.image import ImageDataGenerator
  from sklearn.model_selection import train_test_split
  
  
  testRatio = 0.2
  valRatio = 0.2
  imageDimensions = (28,28,3)
  
  batchSizeVal = 50
  EPOCHS = 2
  stepsPerEpoch = 2000
  
  
  
  
  (X_train, y_train), (X_test, y_test) = mnist.load_data()
  X_train, X_validation , y_train , y_validation = train_test_split(X_train, y_train, test_size= valRatio)
  
  
  X_train = X_train.reshape((48000, 28, 28, 1))
  X_test = X_test.reshape((10000, 28, 28, 1))
  X_validation = X_validation.reshape((12000, 28, 28, 1))
  
  
  
  
  dataGen = ImageDataGenerator(width_shift_range = 0.1,
                                     height_shift_range = 0.1,
                                     zoom_range = 0.2,
                                     shear_range = 0.1,
                                     rotation_range= 10)
  
  
  dataGen.fit(X_train)
  
  y_train = to_categorical(y_train,10)
  y_test= to_categorical(y_test,10)
  y_validation = to_categorical(y_validation,10)
  
  
  def myModel():
      noOfFiters = 60
      sizeOfFilter1 = (5,5)
      sizeOfFilter2 = (3,3)
      sizeOfPool = (2,2)
      noOfNode = 500
  
      model = Sequential()
      model.add((Conv2D(noOfFiters, sizeOfFilter1,input_shape=(imageDimensions[0]
                                                               ,imageDimensions[1],
                                                               1),
                                                              activation = &quot;relu&quot;)))
  
      model.add((Conv2D(noOfFiters, sizeOfFilter1, activation = &quot;relu&quot;)))
      model.add(MaxPooling2D(pool_size=sizeOfPool))
      model.add((Conv2D(noOfFiters//2, sizeOfFilter2, activation = &quot;relu&quot;)))
      model.add((Conv2D(noOfFiters//2, sizeOfFilter2, activation = &quot;relu&quot;)))
      model.add(MaxPooling2D(pool_size=sizeOfPool))
      model.add(Dropout(0.5))
  
      model.add(Flatten())
      model.add(Dense(noOfNode,activation='relu'))
      model.add(Dropout(0.5))
      model.add(Dense(10,activation='softmax'))
      model.compile(Adam(lr=0.001),loss='categorical_crossentropy',
                    metrics=['accuracy'])
      
      return model
  
  model = myModel()
  
  
  
  
  history = model.fit(dataGen.flow(X_train, y_train,
                                   batch_size= batchSizeVal),
                                   steps_per_epoch = stepsPerEpoch,
                                   epochs =EPOCHS,
                                   validation_data = (X_validation,y_validation),
                                   shuffle= True)
  
  plt.figure(1)
  plt.plot(history.history['loss'])
  plt.plot(history.history['val_loss'])
  plt.legend(['training ', 'validation'])
  plt.title(&quot;Loss&quot;)
  plt.xlabel('epoch')
  
  plt.figure(2)
  plt.plot(history.history['accuracy'])
  plt.plot(history.history['val_accuracy'])
  plt.legend(['training ', 'validation'])
  plt.title(&quot;Accuracy&quot;)
  plt.xlabel('epoch')
  plt.show()
  
  score = model.evaluate(X_test,y_test,verbose=0)
  print(&quot;Test Score = &quot;,score[0])
  print(&quot;Test Accuracy = &quot;,score[1])
  
  
  pickle_out = open(&quot;model_trained.pickle&quot;,&quot;wb&quot; )
  model = pickle.dump(model,pickle_out)
  pickle_out.close()
  </code></pre>
  <p>What should I do to get it working.
  I tried to change the runtime to cpu because I thought this is causing by gpu but even then it is not working</p>