Average weights from two .h5 folders in keras - keras

I have trained two models on different datasets and saved weights of each model as ModelA.h5 and ModelB.h5
I want to average these weights and create a new folder called ModelC.h5 and load it on the same model architechture.
How do I do it?

Model trained on different datasets can't just be added like this. It looks something like this. Let's say like this, train one person to classify 1000 images into 5 classes, then, train another person to classify another 1000 images into same 5 classes. Now, you want to combine them into one.
Rather, what you can do is take ensemble of both the networks. There are multiple ways to ensemble the predictions of both models using Max Voting, Averaging or Weighted Average, Bagging and Boosting, etc. Ensemble helps to boost the weak classifiers into one strong classifier.
You can refer to this link to read more about different types of ensemble: Link

Related

How to split classes in few-shot classification using CIFAR-10?

I want to train a model that perform a few-shot image classification using CIFAR-10. So I have to train the model with a small amount of classes and use the rest of the classes for the testing. I'm wondering if I have only 10 classes, how can i do the split? (For example 6 classes for training and 4 for testing, is it ok?)
I have not completely understand your question. We have three options to say it crudely:
Training a model from scratch
Fine-tuning a model
Use a pre-trained model and use it for few-shot learning
If it is the number 3 is what you are looking for, I would recommend that you look into models like CLIP and see whether they work out for your use case.

Aggregate tensorflow keras models

Let say I have trained 300 different models, they all have the same architecture, only the weights are different.
Now, rather than calling my 300 models one after the others, how can I aggregate those models together so effectively having one aggregated X feature that would be fed into that model, and output results for the 300 model in one go?

Large dataset - ANN

I am trying to classify around 400K data with 13 attributes. I have used python sklearn's SVM package, but it didn't work, and then I learned that SVM's are not suitable for large dataset classification. Then I used the (sklearn) ANN using the following MLPClassifier:
MLPClassifier(solver='adam', alpha=1e-5, random_state=1,activation='relu', max_iter=500)
and trained the system using 200K samples, and tested the model on the remaining ones. The classification worked well. However, my concern is that the system is over trained or overfit. Can you please guide me on the number of hidden layers and node sizes to make sure that there is no overfit? (I have learned that the default implementation has 100 hidden neurons. Is it ok to use the default implementation as is?)
To know if your are overfitting you have to compute:
Training set accuracy
Test set accuracy
Once you have calculated this scores, compare it. If training set score is much better than your test set score, then you are overfitting. This means that your model is "memorizing" your data, instead of learning from it to make future predictions.
If you are overfitting with Neuronal Networks you probably have to reduce the number of layers and reduce the number of neurons per layer. There isn't any strict rule that says the number of layer or neurons you need depending on you dataset size. Every dataset can behaves completely different with the same dataset size.
So, to conclude, if you are overfitting, you would have to evaluate your model accuracy using different parameters of layers and number of neurons, and, then, observe with which values you obtain the best results. There are some methods you can use to find the best parameters, is like gridsearchCV.

To classify custom classes (Basically classify types of icecream) in ResNet50 Keras

Resnet50 is cool when we need classify different objects, say tree, dogs, tampons etc. But what if we want further classify say types of trees, or icecreams(Cone, candystick, cup) using ResNet50. Is there a way this would work? PyTorch answers are also welcome.
Yes it is possible.
ResNet50 is just an architecture of a artificial neural network. What you want to classify depends on the training data you feed it or the data it was trained on if you use pretrained weights.
If you want to classify types of trees, you would need to create (or find) a data set that show different type of trees with the appropriate label. Then you can train on the different tree types.
I suggest that you go through some tutorials, as explaining the whole process of data collection, data preprocessing, data annotation, and training an ANN or classic machine learning models would be a bit much here.
Best of luck

is there way to ensemble the prediction other than the take the mean average?

right now I'm just taking the mean average of 3 models predictions
predictions_model = [y_pred_xceptionAug,y_pred_Dense121_Aug,y_pred_resnet50Aug]
predictions = np.mean(predictions_model,axis=0)
is there a better way to ensemble other than just take a mean average?
One neural network based approach is to use the 3 model predictions as input to a further neural network.
More advanced approaches include bootstrap aggregating, where each model trains on a subset of the entire dataset before predictions are aggregated across models.

Resources