简体   繁体   中英

How to pickle model in pycharm?

My CNN Model Here I have written the code for my model and I am trying to pickle this model but it is giving me error

    cnn = Sequential([
            # layers.Conv2D(filters=32, kernel_size=(3, 3), activation='relu', input_shape=(50, 50, 3)),
            # layers.MaxPooling2D((2, 2),padding="valid"),
            # testinaccur=68,trainaccur=75
        
            layers.Conv2D(filters=64, kernel_size=(3, 3), activation='relu', input_shape=(50, 50, 3), padding="same"),
            layers.MaxPooling2D((2, 2)),
        
            layers.Conv2D(filters=128, kernel_size=(3, 3), activation='relu', padding="same"),
            layers.MaxPooling2D((2, 2)),
        
            layers.Conv2D(filters=256, kernel_size=(3, 3), activation='relu', padding="same"),
            layers.MaxPooling2D((2, 2)),
        
            layers.Conv2D(filters=512, kernel_size=(3, 3), activation='relu', padding="same"),
            layers.MaxPooling2D((2, 2)),
        
            layers.Flatten(),
            layers.Dense(64, activation='relu'),
            # layers.Dropout(0.3),
            layers.Dense(7, activation='softmax')
        ])
        
        cnn.compile(optimizer='adam',
                      loss='sparse_categorical_crossentropy',
                      metrics=['accuracy'])
        cnn.fit(trainx, y_train,batch_size=128, epochs=30)
    
        y_pred = cnn.predict(testx)
        y_pred_classes = [np.argmax(element) for element in y_pred]

   
   

 **I had written code for pickle**

This pickle file is also created in directory

 pickle.dump(cnn, open('model.pkl', 'wb'))

  **After running this line I am getting error**

After loading the model this is the problem I am facing

pickled_model = 
     pickle.load(open('C:/Users/ABHISHEK/PycharmProjects/cervical_project/model.pkl', 'rb')) 


  **Error**


    FileNotFoundError: Unsuccessful TensorSliceReader constructor: Failed to find any matching files for ram://5662344f-e2fd-4175-9583-4ed4fec7fc7f/variables/variables
 You may be trying to load on a different device from the computational device. Consider setting the `experimental_io_device` option in `tf.saved_model.LoadOptions` to the io_device such as '/job:localhost'.

Don't pickle tensorflow / keras models. Use the saver/loader (eg .h5 format) provided with your sequential model as described in the documentation .

Saving a Keras model:

model = ...  # Get model (Sequential, Functional Model, or Model subclass)
model.save('path/to/location')

Loading the model back:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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