Hello folks! Are you happy or are you not sure? Alright, let's build a model that will help you find out if you're happy or not.
Well, let's start with some basic understanding of this tutorial and later dive deeper into the neural networks. We're very well known what popular Computer Vision is. It is one of the most popular field of machine learning. Happiness Detection is also one of such field where we apply Computer Vision techniques. This is a binary classification type of problem where we'll building a model that will detect whether the input image is either smiling or not.
The dataset is already labeled as smiling or not smiling. We'll be using 600 images for training and 150 images as test dataset. Before we get our hands into the core part, let's first import some libraries.
Now let's know more about the data.
After the execution, you'll be able to look at the number of data we've taken for training and testing the prepared model. Now we'll be using Keras to build and compile our model. If you don't have enough knowledge about using keras library, you can refer to the references section for additional resources. Note: Here, Tensorflow is being used in the backend. For knowing how to install it in backend, follow a stackoverflow question here.
Here we are creating a simple convolutional network. The network architecture goes like this:
INPUT layer -> CONV -> BN -> RELU activatation -> MAXPOOL -> FLATTEN X (means convert it to a vector) + FULLYCONNECTED
have now built a function to describe your model. To train and test this model, there are four steps in Keras:
1. Create the model by calling the function above
2. Compile the model by calling model.compile(optimizer = "...", loss = "...", metrics = ["accuracy"])
3. Train the model on train data by calling model.fit(x = ..., y = ..., epochs = ..., batch_size = ...)
4. Test the model on test data by calling model.evaluate(x = ..., y = ...)
If you want to know more about model.compile()
, model.fit()
, model.evaluate()
and their arguments, refer to the official Keras documentation.
By 'Create the model', we mean calling the function created above by passing X_train.shape[1:] as a parameter. The second step is to compile the model with a defined optimizer function, a loss function and a metric class. You can try and include 'sgd', 'adam' or others for optimizer (optimizer documentation). Similarly, the loss function that you can use in this case is only 'binary_cross_entropy' since "happiness detection" is a binary classification problem.
Now it's time we train the model. For training, we simply need to pass the training set, the number of epochs and the batch size as parameter. Epoch generally means the number of passes of the entire training dataset the machine learning algorithm has completed. Since we're defining the batch size as well, it's the number of iterations for passing the training dataset in a single batch.
After this, we evaluate the model with the test data which we just completed training. As similar to training the model, we pass test data (150 images) and batch size as the parameter to the evaluate method.
You can analyze your models accuracy and loss. If not very accurate, you can still improve it by making some changes.
Tips for improving your model
If you have not yet achieved a very good accuracy (>= 80%), here are some things tips:
- Use blocks of CONV->BATCHNORM->RELU such as:
until your height and width dimensions are quite low and your number of channels quite large (≈32 for example).X = Conv2D(32, (3, 3), strides = (1, 1), name = 'conv0')(X) X = BatchNormalization(axis = 3, name = 'bn0')(X) X = Activation('relu')(X)
You can then flatten the volume and use a fully-connected layer. - Use MAXPOOL after such blocks. It will help you lower the dimension in height and width.
- Change your optimizer. We find 'adam' works well.
- If you get memory issues, lower your batch_size (e.g. 12 )
- Run more epochs until you see the train accuracy no longer improves.
Now let's pass our own image to the model and predict.
Congratulations, you have successfully used Keras to build "Happiness Detection". You can take a look at a summary of your model by executing the code below.
You can clone notebook and other essentials from this link. You can find this article in my github page as well.
References:
- Coursera deeplearning.ai course: Convolutional Neural Network
Comments
Post a Comment