Search code examples
python-3.xgoogle-colaboratoryversion

how do I update the version of the python libraries used in a trained model


I trained a model on Google colab , and dumped it, then I was finishing the project on VS Code on my system. I got an error saying the versions don't match since I had the latest version of libraries and the colab had an older version. I had the retrain the model on my system, which took a lot of time, as my System has a basic configuration. My only use of colab was to put the training stress on colab rather than my system

I didn't know there will be a version conflict as I thought Colab will have the latest version of libraries


Solution

  • I have in colab (Google) tensorflow version 2.9.2 and in my Raspberry 4 tensorflow versio 2.4.1. So diferent versions. I made in colab a pre-training model VGG19 with input_shape(220,220,3). And I classificated 2 types image

    So once I have trained the model in the Colab.Google environment, then from Colab except the model and its weights.

    # serialize model to JSON
    model_json =  loaded_model2.to_json()
    with open('/content/drive/MyDrive/dataset/extract/model_5.json', "w") as json_file:
        json_file.write(model_json)
    # serialize weights to HDF5
    
    loaded_model2.save_weights('/content/drive/MyDrive/model_5.h5')
    print("Saved model to disk")
    

    As I have indicated, then I intend to use that trained model on my Raspberry4. So I create a model on the Raspberry just like I did in Colab, but I don't do 'fit'. What I do next is load the .h5 file with the 'weights' that was generated in Colab.Google. And in principle this has worked for me

    model_new = tf.keras.Sequential()
    model_new.add(tf.keras.applications.VGG19(include_top=false, weights='imagenet',pooling='avg',input_shape=(220,220,3)))
    model_new.add(tf.keras.layers.Dense(2,activation="softmax"))
    opt = tf.keras.optimizers.SGC(0,004)
    model_new.compile(loss='categorical_crossentropy',optimizer=opt,metrics=['accuracy'])
    
    model_new.load_weights('/home/pi/projects/models/model_5.h5)