0

I am trying to pickle.dump() an object with a model property that stores a Keras and TensorFlow model.

admin.py

def build(self, request, queryset):
        count = 0
        for p in queryset:
            if build_id(p.project_management.id):
                count += 1
            else:
                messages.warning(request, f"Could not build model for {p}")
        messages.success(
            request, f"Successfully built models for {count} projects")
    build.short_description = "Build models for selected Projects"

build.py

def build_id(project_id):
    # get directory path to store models in
    path = fetch_model_path(project_id, True)

    # train model
    model, scaler_in, scaler_out = train_project_models(project_id)

    # ensure model was trained
    if model is None:
        return False

    # store models
    store_model(f'{path}/model.pkl', model)
    store_model(f'{path}/scaler_in.pkl', scaler_in)
    store_model(f'{path}/scaler_out.pkl', scaler_out)

    # clear current loaded model from memory
    keras_clear()

    return True

utils.py

# dump model to path
def store_model(path, model):
    with open(path, 'wb') as f:
        model_file = File(f)
        pickle.dump(model, model_file)
        

when I Build my model then I found this error. I am using the python Django project. enter image description here

When I Build model this show an internal server error too as shown in picture

enter image description here

  • Does [this](https://stackoverflow.com/questions/64665776/typeerror-cant-pickle-weakref-objects-when-pickling-a-deep-learning-model) help? Also, please don't post screen shots of stack traces, post the text directly into the question. – o-90 Oct 27 '21 at 13:56
  • I already check it out. But cannot able to solve my problem – Muhammad Fayzan Oct 28 '21 at 06:12

1 Answers1

0

Don't pickle tensorflow models by yourself, simply use their method save or save_weights.

Alex Deft
  • 2,531
  • 1
  • 19
  • 34