Number of categories , keras - keras

I have a keras model that classifies images into a number of categories. How do I print the number of categories that my model is classifying the images into?

You can always check the structure of the model using model.summary() in Keras which gives you the information of each and every layer of your model sequentially as shown in the image below.
So in the last layer, which is my output layer, you can see it says output shape is (None, 10), which shows my classifier contains 10 classes or 10 categories. (Do not worry about None it just means batch size).

Related

Predict a class that is not in the training dataset

Suppose we have five classes: Dog, Cat, Banana, Apple, and Tree.
If we train a CNN with all and want to predict the class of unknown images like an image of "Car", the model gives one of the classes each time.
Can you please tell me how we can tell the model if the data is not part of the training dataset, say "I did not detect the class" or something like that?
Thank you
You can solve this problem either by adding a neutral class or by transforming your problem into a multi-classification and adding thresholding.
for the neutral class, you have to go back to the labeling phase and add some random image (be careful that this random image does not contain an image of the other class, for example in your case an image of a dog because this would hurt your model) with the label "other".
in case of multi-classification, you don't need additional images, but you need to change the activation function of your classification layer (the last layer of your network), from softmax to sigmoid
for exemple
from :
keras.layers.Dense(5, activation='softmax')
to
keras.layers.Dense(5, activation='sigmoid')
The main difference between the sigmoid model and the softmax model is that the softmax model is guaranteed to contain values whose sum is equal to 1, whereas the output of the sigmoid model will contain values, each between 0 and 1.
So your model in this case learns to make an independent prediction of each class for example if the output is [0.7,0.4,0.4,0.5,0.8] if we take 0.6 as the threshold, then your model is almost sure that the image contains a dog and a tree, in the case we take for example 0.9 as the threshold this image does not contain any object of your 5 - classes so you can say "other".

What do units and layers do in neural network?

model = keras.Sequential([
# the hidden ReLU layers
layers.Dense(units=4, activation='relu', input_shape=[2]),
layers.Dense(units=3, activation='relu'),
# the linear output layer
layers.Dense(units=1),
])
The above is a Keras sequential model example from Kaggle. I'm having a problem understanding these two things.
Are the units the number of nodes in a hidden layer? I see some people put 250 or what ever. What does the number do when it gets changed higher or lower?
Why would another hidden layer need to be added? What does it actually do the data to add more and more layers?
Answers in brief
units is representing how many neurons in a particular layer.When you have higher number,model has higher parameters to update during learning.Same thing goes to layers as well.(more layers and more neurons take more time to train the model).selecting how many neurons is depend on the use case and dataset and model architecture.
When you have more hidden layers, you have more parameters to update.More parameters and layers meaning model is able to understand complex relationships hidden in the data. For example when you have a image classification(multiple), you need more deep layers with neurons to understand the features in the image, which use to classify in final layer.
play with tensorflow playground,it will give great idea when you change the layers and neurons.

Training features with different dimensions

I have two types of features that should be concatenated and trained, features are as follow:
Images with dimension (277,217,3)
vector of real numbers (40,1)
What I did is after training the model on all images, I extracted the output of predict() function of all samples, this outputs vector, then I concatenated this vector with the real number features. But the problem is when I trained the values of predict() alone, the result was lower than the base model's result.
So I am trying to add the features after the convolutional layers while the model is training
Thanks in advance!!

A multi-input (text and numeric) model for regression giving same output

I have a problem where I need to predict the number of clicks based on Date, CPC(cost per click), Market(US or UK) and keywords(average length 3-4 words, max 6). So the features that I decided to input the model were:
Day(1-31) WeekDay(0-6) Month(1-12) US-Market(0 or 1) UK-Market(0 or 1) CPC(continuous numeric)
And the output is the continuous numeric Number of Clicks.
For Keywords I used keras tokenizer to convert to sequences and the padded those sequences. The I used the glove word embeddings and created an embedding matrix and fed to the nueral Network model as described here in pretrained glove embeddings section.
The model that I used is:
The last Dense layer has linear activation. The model has two inputs (nlp_input for text data) and meta_input for (numerical,categorical data). Both models are concatenated after the Bidirectional LSTM to the nlp_input
The loss is :
model.compile(loss="mean_absolute_percentage_error", optimizer=opt,metrics=['acc'])
where opt = Adam(lr=1e-3, decay=1e-3 / 200)
I trained the model for 100 epochs and the loss was close to 8000 at that point.
But when I apply prediction to the test they result in the same number for all test inputs and that number is even negative -4.5 * e^-5. Could someone guide me as to how should I approach this problem and what improvements could I do to the model.

Keras, siamese network, how to abstract feature?

I'm trying to modify Keras Siamese Network example to get image feature.
The problem is, how can I get image features? The output of last layer is only a number. What should I do to get the feature before euclidean_distance?
You can try to first train the model on the entire dataset and save it.
Load the model back again, now set the output layers to be processed_a and processed_b
Now call the model.predict() function on the entire dataset once again and you'll have the features for each image in the dataset.
Have a look at this
Hope this helps!
To get the embeddings from the Keras siamese network MNIST example after training:
model_a = Model(inputs=model.input, outputs=processed_a)
model_a.predict([tr_pairs[:, 0], tr_pairs[:, 1]])
I did it as follows (reference from my github post):
My trained siamese model looked like this:
siamese_model.summary()
Note that my newly redefined model is basically the same as the one highlighted in yellow
I then redefined my model which I wanted to use for extracting embeddings (It should be the same model you defined except now it will not have those multiple inputs like siamese) which looked like this:
siamese_embeddings_model = build_siamese_model(input_shape)
siamese_embeddings_model .summary()
Then I just extracted the weights from my trained siamese model and set them into my new model
embeddings_weights = siamese_model.layers[-3].get_weights()
siamese_embeddings_model.set_weights(embeddings_weights )
Then you can supply the new Image to extract the embeddings from the new model
vector = siamese.predict(image)
len(vector[0]) it will print 150 because of my fine dense layer (which are the output vector)

Resources