Author Archives: Harsh Thaker

About Harsh Thaker

Computer Vision Researcher and programmer who likes to create amazing projects and iOS apps. Also a Robotics enthusiast and love to write blogs.

Move specific number of files mac/unix terminal

Move specific number of files mac/unix terminal

In order to move specific number of files from one directory to another on mac, use the following command in mac terminal. This command is very useful in data science for segregating the data for training and testing purpose.

Credit for this post goes to @hardikthaker. Thanks to him !

Instead of -n, write number of files to be moved. For example, -10 will move 10 files to the path you specified at the end.

Kaggle Digit Recognizer using Softmax regression

Kaggle Digit Recognizer using Softmax regression

As a machine learning enthusiast, one would definitely want to solve some practical problems. Kaggle is one of the best platform to do so. It provides list of data science problems, including paid competitions. Kaggle digit recognizer is one of those problems.

MNIST dataset

One of the tutorial problem is called digit recognizer. We are given the MNIST dataset, which contains images of handwritten digits. Our task is to build a model, train the model with MNINST data. After training, we have to use the test data to predict scores. The final task is to submit predicted scores/labels to Kaggle. The Kaggle is having the ground truth labels for the test dataset. Comparing the ground truth labels with our submitted prediction labels, Kaggle gives us accuracy score. For my submission using simple softmax regression with tensorflow, I received an accuracy score of 91.40%.

You can find the code on GitHub here! or alternatively check the following code:

 

 

Install OpenCV 3 + Python 3.5 macOS tutorial

Install OpenCV 3 + Python 3.5 macOS tutorial

This is a rapid tutorial to install OpenCV 3 on macOS. I have eliminated long description and focused only on installation commands more.

1. Install XCode

In order to compile OpenCV on macOS, we need to install XCode first. The simplest way to get XCode is right from your App Store application in your Mac. Open App Store -> Search “XCode” -> Get.

Although, I prefer to get it from my apple developer account. If you have a developer’s account, then you can download the latest version from here.

Once you are done installing XCode, you need to now agree the developer license. Run the following command in the terminal and follow the instructions in terminal to agree the license:

2. Install Command Line Tools

Next step is to install Apple Command Line Tools. Run the following command in terminal:

Upon executing the above command, a pop will appear. Click “install”.

3. Install Homebrew

Homebrew is a package manager for macOS. To install homebrew run the following command in terminal:

Update homebrew

Now we need to update our ~/.bash_profile file. You can run the following command to open up the file in nano editor. I am using nano editor.

Add following lines to the file to update the path variable to look for the libraries in Homebrew path before system path:

Don’t forget to save and exit the file. Then use the source command to with your bash profile:

4. Install Python 3.5

Use the following command to install python:

After installation finishes, create symbolic links:

5. Install Python Virtual Environments and NumPy

In simple terms, virtual environments is something that lets us use different versions of libraries, softwares on same system. For example, in a virtual environment named cv2, I can install OpenCV 2 and in virtual environment cv3, I can install OpenCV 3. Now when I wish to work with the older version of the library then I work in cv2 environment and for newer I use cv3 environment. Similarly, different Python versions in different virtual environments.

Use following command to install virtualenv and virtualenvwrapper.

Once again update the bash profile file:

Add following lines to it:

Then once again source your bash profile:

Create Python 3 Virtual Environment

Use the following command to create a virtual environment:

where -p is to determine which python version we are using for this environment.

After creating the virtual environment, in order to work within it, use ‘workon’ command:

OpenCV is going to need NumPy for math operations. Install it using following command:

6. Install Prerequisite for OpenCV

Use the following commands to install them:

7. Downloading OpenCV

Configure OpenCV and Python using CMake

Create directory ‘build’ in OpenCV directory using:

Run the cmake command as following:

In the above command, you need to fill your PYTHON3_LIBRARY and PYTHON3_INCLUDE_DIR path.

8. Compile OpenCV

Compile OpenCV using the following command:

Once it finishes, install it using the command:

9. Check Installation

Note: If you are willing to use TensoFlow along with OpenCV and in case, if you have installed TensorFlow using virtualenv tutorial, then you won’t be able to work on both. You will have to install TensorFlow again in the OpenCV virtualenv we created. In our example, we have to install TensoFlow in cv3 virtualenv.

create generic stack swift programming

create generic stack swift programming

Swift has inbuilt generic data type Array and Dictionary. Here’s a simple example of a generic stack written in swift.

First we’ve created a generic stack that can create stack of any data type. push() function adds elements in stack and pop() takes them out.

In my custom stack I have given functions like count() that returns number of elements in stack and printElements() function prints all stack elements.

 

generic function example code swift

generic function example code swift

Generic means that “something that is general”. In programming, generic data type means that when we make a generic type that means we can use it as any other data type like Int, Float or String.

For example, we need to swap to values. So, we will write a function for swapping two values. When we write a normal function then either we can pass two Int parameters and swap them but if we try to pass two Float or String then will lead into error. So, we will create two more functions to deal with this having Float and String parameters.

In swift, we can write generic function. That means no matter which two data type values we need to exchange we can write one common function for that. This function is called generic function.

In following example, we have used the same function “swapValues” to exchange two Int values of a and b as well as two String values of name and anothername.

Note: Here, we can only exchange the same datatype values and pass it to the function parameters. Two Int or Two Float can be exchanged. But an Int value can not be exchanged with a Float. Swift has its own “swap” function for this purpose in swift standard library.

failable initialiser swift simple example

failable initialiser swift simple example

In swift, Failable initialisers are used with structure,class and enumeration. When it is not sure that a property within class or structure or enumeration will be initialised then failable initialisers are used.

For this, we write initialiser with init? syntax and if initialisation succeeds then value is assigned or else nil value is assigned.

Example Code

 

designated convenience automatic initialiser swift

designated convenience automatic initialiser swift 

Example code below demonstrates the concept of all of these initialisers in swift in a simple way. We have three classes here. One is the base class, Two is its subclass and Three is subclass of Two.

Example code – designated convenience automatic initialiser swift 

How these initializers work is described in the comments of the code.

class property initialisers inheritance swift

class property initialisers inheritance swift

As we know, In swift, we must initialise the property values within the class definition. It can be done using default initialisers – that means assigning values to variables directly when we know that the value is never going to be changed in any of its instances or by using init function we do so.

When we are dealing with inheritance and have to modify certain property value within subclass then we use init function which is overridden using override keyword.

For example

Here, in subclass bicycle we override the super class initialiser. Then we call super.init() function. This calls the default initialiser and assigns the value 0 to a. Then we change the value of a.

Creating the object of bicycle and accessing a’s value will result into a = 2 whereas with super class object that supObj accessing a will call default initialiser and result into a =0 for that instance.

init method simple example swift programming

init method simple example swift programming

Example below shows use of init method. It is used for initialization of the structure,class or enumeration property.

In swift, we must initialise the properties before using them. So either you can do it while declaring them if they are not going to change for all instances.

In case, if they are going to be changed for different instances then you must use init method. The example is for init method with parameters passed. init functions do not have names like other functions so they are distinguished by the parameters we passed.

Example Code

Structure CheckInit has property a that has to initialised. If we expect a’s value different for different instances created for different purpose then we write to init method with different parameters.

When we create an instance and pass b value then accordingly the first init is called an a’s value is assined that is b+2 will give 5 and when we create ci instance and pass value of c = 3 then it calls the second initialiser and a’s value for that instance will be 6.