CNN trained model not saved properly - python

I am a novice in using tensorflow and I built a CNN which is trained and tested both with 80-85% accuracy. I tried to save my trained model using model.save('example.h5'), and download the file using files.download('example.h5')
Afterwards I tried to load it to my flask back-end using model = tf.keras.models.load_model('example.h5').
When I tried using it with random images, it's like the model has never been trained before. Any solutions? thank you

A common reason to this might be that you are normalizing your data on training but not before predicting. Make sure you normalize the input data before you run predictions, if you have trained on normalized data.

Related

Keras ASR model validation steps

I am a newbie in Machine learning. I would like to design a model of ASR(Automatic speech recognition), so I came across the following link is:
https://keras.io/examples/audio/transformer_asr/
I am able to understand the training model concept. I also saved the above model using the following code:
model.save_weights('data_sa',save_format='hdf5')
then I load it again using:
model.load_weights('/content/data_sa')
Now, I would like to predict the model, by giving the validation data set. But I am facing an issue.
I am using the following step for prediction:
model.predict(np.array(val_ds)) #this is the validation dataset
It is giving an error of tensor conversion.
I tried to search the possible solution to this step but couldn't able to find the it.
tf.cast() and tf.to_float() are TensorFlow functions, so you'd get them using import tensorflow as tf.

Shall I update my training data in real-time?

I tried image classification using trained model and its working well but some images could not find perfectly in that time have to get that image and label from users so my doubt is..Is it possible to add new data into already trained model?
No, during inference time you use the weights of the trained model for predictions. Which basically means that at the time your model is deployed the capabilities of your image classifier are fixed by the weights. If you wish to improve your model, you would have to retrain your model with the new - data. However, there is another paradigm of learning called "Online Learning" where the model is continuously learning and modifying the weights. In this case your weights are not fixed and your model is continuously updating its weights with each training input. However afaik this is not usually recommended for CNNs, because the backward pass of gradients is computationally intensive and your inference will be slow because of this.
No model can predict with 100% accuracy if it does it's an ideal model. And if you want to add more data to your train model you have to retrain the model with the new data. Having more data is always a good idea. It allows the “data to tell for itself,” instead of relying on assumptions and weak correlations. Presence of more data results in better and accurate models. So if you want to get better accuracy you have to train your model with more data. Without retraining, you can't add data to your trained model.

keras model prediction is nan after saving and loading

I trained a neural network with google colab.
I saved the neural network using joblib.dump()
I then loaded the model on my PC using joblib.load()
I made a prediction on the exact same sample, using the same model, on both colab and my PC. On colab, it has an output of [[0.51]]. On my pc, it has an output of [[nan]].
The model summary reports that the architecture of the model is the same.
I checked the weights of the model I loaded on my PC, and the model on colab, and the weights are the exact same.
Any ideas as to what I can do? Thank you.
Quick update: even if I change all of my inputs to zero, the prediction is still nan.
As far as I know keras has its own function to save the model such as model.save('file.h5'), and the joblib library is used to save sklearn models.

How can I deploy a trained CNN model to production on a ARM?

I have trained a CNN model with Keras for semantic segmentation of craneal images and saved the weights and this trained model.
Now, I want to put it into production on a microprocessor. The pipeline of the process in the micro involves reading an image from a sensor and using it as input for the CNN model (U-Net). Then, the resulted binary image is used as a mask for an area of interest from which a variable is measured. Finally, a number is given as a result.
So, is it possible to load a trained model on a microprocessor? And if so, how?
Which features should have the microprocessor in order to work with CNN models?
Thanks in advance!

Use Machine Learning Model in Pretrained Manner Keras, Tensorflow

I built a CNN model for image classification using the Keras library. However training takes many hours. Once I trained my model, how can I use it without training once more? I mean after I trained my model, I want to use it many times.
Because I will use my model in android studio.
Any help is appreciated
Thank YOU...
EDIT
When I wrote this question, I did not know the save model and load.model, in the answers you see the appropriate usage of them.
You can easily save your model after the training process by using:
model.save('my_model.h5')
you can later load that model by using:
model = load_model('my_model.h5')
for more details have a look at the documentation: https://keras.io/getting-started/faq/#how-can-i-save-a-keras-model

Categories

Resources