---
# System prepended metadata

title: Daily Note 01/07/2020
tags: [Tensorflow, Python, Daily Notes]

---

# Daily Note 01/07/2020
###### tags: `Daily Notes` , `Tensorflow` , `Python`
## Name : Christofel Rio Goenawan
## University : Bandung Institute of Technology (ITB)
## Schedule:
0. First Meeting and Rule Explanation
1. Understand concept and work of Convolutional Neural Network ( CNN ). 
2. Create python code for training and predict Cat vs Dog.
3. Testing and Debugging the code.
4. Compare the result.
## Daily Log

### 0. First Meeting and Rule Explanation (8.00)

### 1. Understand concept and work of Convolutional Neural Network ( CNN ). <mark>(10.00)</mark>
- Learn code and concept from https://blog.csdn.net/qq_16137569/article/details/72802387
and ***Geron, Aurelien. Hands- On Machine Learning with Scikit- Learn & TensorFlow 1st edition. 2017. O'Reilly.***
- Learn from another example in Kaggle for Cat vs Net CNN to deeping understanding.

### 2. Create python code for training and predict Cat vs Dog. <mark>(11.30)</mark>
- Modify the Python code for Cat vs Python using another architecture.

### 3. Testing and Debugging the code. <mark>(12.00)</mark>
- First there are some problem in the code, after checking there are some version problem with the reference code. After resolve this the program works well.

### 4. Compare the result. <mark>(13.30)</mark>
- Testing performance accuracy of 3 different architecture and compared the result.

## Report
### 1. What is Convolutional Neural Network
From book, Convolutional Neural Network ( CNN ) is a class of deep neural networks, most commonly applied to analyzing visual imagery. It consist of some layer consist of a lot of neurons with each activation function like other DNN.

The main advantage of CNN for processing image is that the input not only 1 dimension , but can be 2 or more dimension. This give advantage for image processing more than normal DNN. One of the unique layer of CNN is caled **pool**. Pool is a layer that give "sampling" of some pixel at certain areas ( can be square , circle , etc ) using some statistic value , like maximum value ( maxpool ), average ( average pooling ) etc. This made the memory of data become so much smaller and force the model to learn only important feature ( feature extraction )

There are a lot of famous CNN architecture , like Le- Net , Goog-LeNet , ResNet etc. But almost every architecture have same pattern as picture below.

![](https://i.imgur.com/o308GFU.png)

### 2. Create and Testing CNN Code using Tensorflow Python
First writer read the reference from previous website. But writer found another tensorflow code that more easier to understand and implement in reference [1]. 

The coding consist of data preprocessing to make data picture become vector hence it can be input to the model. Then the model implemented by this code.

```python=
model = Sequential()
# Adds a densely-connected layer with 64 units to the model:
model.add(Conv2D(64,(3,3), activation = 'relu', input_shape = X.shape[1:]))
model.add(MaxPooling2D(pool_size = (2,2)))
# Add another:
model.add(Conv2D(64,(3,3), activation = 'relu'))
model.add(MaxPooling2D(pool_size = (2,2)))

# Add another 
model.add(Conv2D(32,(3,3), activation = 'relu'))
model.add(MaxPooling2D(pool_size = (2,2)))

model.add(Flatten())
model.add(Dense(64, activation='relu'))
# Add a softmax layer with 10 output units:
model.add(Dense(1, activation='sigmoid'))

model.compile(optimizer="adam",
              loss='binary_crossentropy',
              metrics=['accuracy'])

```

In that code first declared sequential model as placeholder. Than we create first layer using **keras** function ***Conv2D()*** to create convolutional layer with 64 neurons and kernel size 3 x 3. The activation function is "regression linear unit " that usually ysed un normal DNN. Than the pool layer with pool size 2 x 2 added to minimize the data. This time writer create 2 more layer as above with the last layer have kernel size 32 x 32. Than because the ouput is binary classification we used softmax layer with logits as activation function as above. The trained model valuated by cross validation with train : test = 80 : 20 , and result cool 89,2 % accuracy.

![](https://i.imgur.com/lRp83e8.png)


### 3. Compare the Result
Writer create 2 more architecture with a and two layer as above added to the last model ( 12 and 14 layer ). With same cross validation ratio it showed accuracy as below.

* CNN 10 Layer : **88,39%**
* CNN 12 layer : **89,40 %**
* CNN 14 layer : **88,97 %**

It is shown that more layer doesnt guarantee greater performance. It can be result of **overfitting**. The solution to avoid overfitting in CNN model is to use **regularization** , as **dropout , lasso reguralization , sparse reguralization etc**.


## Reference
1. https://www.kaggle.com/ruchibahl18/cats-vs-dogs-basic-cnn-tutorial/notebook