CNN on python with Keras - python

I made a simple CNN that classifies dogs and cats and I want this CNN to detect images that aren't cats or dogs and this must be a third different class. How to implement this? should I use R-CNN or something else?
P.S I use Keras for CNN

What you want to do is called "transfer learning" using the learned weights of a net to solve a new problem.
Please note that this is very hard and acts under many constraints i.e. using a CNN that can detect cars to detect trucks is simpler than using a CNN trained to detect people to also detect cats.
In any case you would use your pre-trained model, load the weights and continue to train it with new data and examples.
Whether this is faster or indeed even better than simply training a new model on all desired classes depends on the actual implementation and problem.
Tl:Dr
Transfer learning is hard! Unless you know what you are doing or have a specific reason, just train a new model on all classes.

You can train that almost with the same architecture (of course it depends on this architecture, if it is already bad then it will not be useful on more classes too. I would suggest to use the state of the art model architecture for dogs and cats classification) but you will also need the dogs and cats dataset in addition to this third class dataset. Unfortunately, it is not possible to use pre-trained for making predictions between all 3 classes by only training on the third class later.
So, cut to short, you will need to have all three datasets and train the model from scratch if you want to make predictions between these three classes otherwise use the pre-trained and after training it on third class it can predict if some image belongs to this third class of not.

You should train with new_category by add one more category, it contains images that are not in 2 category before. I mean
--cat_dir
-*.jpg
--dog_dir
-*.jpg
--not_at_all_dir
-*.jpg
so.. total categories you will train are 3 categories.
(categories or classes whatever it is)
then add the output of final dense fullyconnected (3 categories)

Related

how to use a machine learning model that evaluate U-Net segmentation(or any other models) predication?

i'm currently using u-net model with different backbone (resnet,mobilenet ... vs) to segment Ct-Scan image
and after i train the models and get the prediction masks i need to use another model that going to take test set model output and divide them into two group either good prediction or bad prediction the images bellow show two example where one is considered as good prediction and the other is not good predictionbad prediction
is there any pre-trained models that i can directly use if not what kind of model i should use ?
i just want to mention that i'm new to this staff

Using tensorflow classification for feature extraction

I am currently working on a system that extracts certain features out of 3D-objects (Voxelgrids to be precise), and i would like to compare those features to automatically made features when it comes to performance (classification) in a tensorflow cNN with some other data, but that is not the point here, just for background.
My idea now was, to take a dataset (modelnet10), train a tensorflow cNN to classify them, and then use what it learned there on my dataset - not to classify, but to extract features.
So i want to throw away everything the cnn does,except for what it takes from the objects.
Is there anyway to get these features? and how do i do that? i certainly have no idea.
Yes, it is possible to train models exclusively for feature extraction. This is called transfer learning where you can either train your own model and then extract the features or you can extract features from pre-trained models and then use it in your task if your task is similar in nature to that of what the pre-trained model was trained for. You can of course find a lot of material online for these topics. However, I am providing some links below which give details on how you can go about it:
https://keras.io/api/applications/
https://keras.io/guides/transfer_learning/
https://machinelearningmastery.com/how-to-use-transfer-learning-when-developing-convolutional-neural-network-models/
https://www.pyimagesearch.com/2019/05/27/keras-feature-extraction-on-large-datasets-with-deep-learning/
https://www.kaggle.com/angqx95/feature-extractor-fine-tuning-with-keras

How can I evaluate pre-trained model Tensorflow Object Detection API

I have a group of images with ground truth detection boxes and I want to simply run them through a pre-trained model from the Model Zoo and get the, say, precision/recall/mAP between the ground truth boxes and predicted detections. But all the evaluation methods I've seen use training/validation data that I never specified in the first place because I'm using a pre-trained model.
It seems like a simple question, but I can't seem to find an answer. How can I go about doing this?
If your images happen to be of something that one of the models in the model zoo is already trained on, then you could run inference on them and get predictions.
However, to get meaningful metrics on a custom dataset, you will have to do some training; starting with one of the models in the model zoo is a great idea. Here are a couple tutorials that might help:
https://pythonprogramming.net/custom-objects-tracking-tensorflow-object-detection-api-tutorial/
https://tensorflow-object-detection-api-tutorial.readthedocs.io/en/latest/

How to add a new class to an existing classifier in deep learning?

I trained a deep learning model to classify the given images into three classes. Now I want to add one more class to my model. I tried to check out "Online learning", but it seems to train on new data for existing classes. Do I need to train my whole model again on all four classes or is there any way I can just train my model on new class?
You probably have used a softmax after 3 neuron dense layer at the end of the architecture to classify into 3 classes. Adding a class will lead to doing a softmax over 4 neuron dense layer so there will be no way to accommodate that extra neuron in your current graph with frozen weights, basically you're modifying the graph and hence you'll have to train the whole model from scratch
-----or-----
one way would be loading the model and removing the last layer , changing it to 4 neurons and training the network again! This will basically train the weights of the last layer from scratch . I don't think there is anyway to keep these(weights of the last layer) weights intact while adding a new class .
You have to remove the final fully-connected layer, freeze the weights in the feature extraction layers, add a new fully-connected layer with four outputs and retrain the model with images of the original three classes and the new fourth class.
I tried to check out "Online learning", but it seems to train on new data for existing classes.
Online learning is a term used to refer to a model which takes a continual or sequential stream of input data while training, in contrast to offline learning (also called batch learning), where the model is pre-trained on a static predefined dataset.
Continual learning (also called incremental, continuous, lifelong learning) refers to a branch of ML working in an online learning context where models are designed to learn new tasks while maintaining performance on historic tasks. It can be applied to multiple problem paradigms (including Class-incremental learning, where each new task presents new class labels for an ever expanding super-classification problem).
Do I need to train my whole model again on all four classes or is there any way I can just train my model on new class?
Naively re-training the model on the updated dataset is indeed a solution. Continual learning seeks to address contexts where access to historic data (i.e. the original 3 classes) is not possible, or when retraining on an increasingly large dataset is impractical (for efficiency, space, privacy etc concerns). Multiple such models using different underlying architectures have been proposed, but almost all examples exclusively deal with image classification problems.
Related q's:
How to fine-tune a keras model with existing plus newer classes?

2 output CNN with training of 2nd output being based on 1st output

I am working on a multi label problem classifying images. I don't have enough data, so I am using transfer learning with CNN being feature extractor. As I have enough data for some of the classes, I have formulated problem in a way:
30 classes and 31st being "rest" of the images, so I can distinguish them.
The 31st rest class is mostly dragging my accuracy and other metrics down. I was thinking about creating multioutput network in Keras, where one output would be binary classification whatever it is "good" or "rest" image and the second would be trained only if the first would be classified as a good.
I do understand fact that I will need evaluate the second output too, as that's how computation graphs works, but is there an option how to tell the layer: Don't adapt on this bad example, based on input from another softmax?
Thanks
I think I understand to an extent what you want to achieve. The way to go for this would be to train two models - first would be a binary classification 'good' vs 'rest' and the output of this model, if 'good' would have to be passed to the second model - 30 class output model. This is actually fairly common way to go about problems as yours.
I worked on a helmet detection problem earlier - I noticed that instead of detecting helmets, it worked better if I detected persons with one model and pass these boxes to a classification model - 'helmet' or 'no helmet'.

Categories