Transfer learning (vgg16)

##Reference
[Keras Documentation](https://keras.io/applications/)

##Pattern 1
```python
from keras.applications.vgg16 import VGG16

from keras.models import Model
from keras.layers import Input, Dense, Flatten, Dropout
```

```python
base_model = VGG16(weights='imagenet', include_top=False)

img_width = 512
img_height = 512

x_input = Input(shape=(img_width, img_height, 3))
    
x = base_model(x_input)
x = Flatten()(x)
x = Dense(32, activation='relu')(x)
x = Dense(32, activation='relu')(x)
x = Dropout(0.3)(x)
out = Dense(1, activation='sigmoid')(x)

model = Model(inputs=x_input, outputs=out)

for layer in base_model.layers:
    layer.trainable = False

model.summary()
```

![image.png](https://qiita-image-store.s3.amazonaws.com/0/254604/d27f8393-ad5c-e0d0-91ea-4bf50cc0e796.png)



##Pattern2 (U-net)
```python
from keras.applications.vgg16 import VGG16

from keras.models import Model

from keras.layers import Input, Conv2D, Conv2DTranspose
from keras.layers import concatenate
```

```python
img_width = 128
img_height = 128

input_tensor = Input(shape=(img_width, img_height, 3))
    
vgg_model = VGG16(weights='imagenet', include_top=False, input_tensor=input_tensor)

vgg_model.summary()
```

![image.png](https://qiita-image-store.s3.amazonaws.com/0/254604/48026b9c-d72c-0901-7b07-0d4784d4d8b3.png)

![image.png](https://qiita-image-store.s3.amazonaws.com/0/254604/55a6cdbf-1c8d-9f84-7a4c-e617dd76a1ae.png)



```python
vgg_top = vgg_model.get_layer('block5_conv2').output

block1_conv2 = vgg_model.get_layer('block1_conv2').output
block2_conv2 = vgg_model.get_layer('block2_conv2').output
block3_conv3 = vgg_model.get_layer('block3_conv3').output
block4_conv3 = vgg_model.get_layer('block4_conv3').output
```

```python
start_neurons = 64

# 8 -> 16
deconv4 = Conv2DTranspose(start_neurons * 8, (3, 3), strides=(2, 2), padding='same')(vgg_top)
uconv4 = concatenate([deconv4, block4_conv3])
uconv4 = Dropout(0.5)(uconv4)
uconv4 = Conv2D(start_neurons * 8, (3, 3), activation='relu', padding='same')(uconv4)
uconv4 = Conv2D(start_neurons * 8, (3, 3), activation='relu', padding='same')(uconv4)

# 16 -> 32
deconv3 = Conv2DTranspose(start_neurons * 4, (3, 3), strides=(2, 2), padding="same")(uconv4)
uconv3 = concatenate([deconv3, block3_conv3])
uconv3 = Dropout(0.5)(uconv3)
uconv3 = Conv2D(start_neurons * 4, (3, 3), activation="relu", padding="same")(uconv3)
uconv3 = Conv2D(start_neurons * 4, (3, 3), activation="relu", padding="same")(uconv3)

# 32 -> 64
deconv2 = Conv2DTranspose(start_neurons * 2, (3, 3), strides=(2, 2), padding="same")(uconv3)
uconv2 = concatenate([deconv2, block2_conv2])
uconv2 = Dropout(0.5)(uconv2)
uconv2 = Conv2D(start_neurons * 2, (3, 3), activation="relu", padding="same")(uconv2)
uconv2 = Conv2D(start_neurons * 2, (3, 3), activation="relu", padding="same")(uconv2)

# 64 -> 128
deconv1 = Conv2DTranspose(start_neurons * 1, (3, 3), strides=(2, 2), padding="same")(uconv2)
uconv1 = concatenate([deconv1, block1_conv2])
uconv1 = Dropout(0.5)(uconv1)
uconv1 = Conv2D(start_neurons * 1, (3, 3), activation="relu", padding="same")(uconv1)
uconv1 = Conv2D(start_neurons * 1, (3, 3), activation="relu", padding="same")(uconv1)

out = Conv2D(1, (1,1), padding="same", activation="sigmoid")(uconv1)

model = Model(inputs=input_tensor, outputs=out)

#for layer in model.layers[:17]:
#    layer.trainable = False
    
for layer in vgg_model.layers:
    layer.trainable = False 

model.summary()   
```

![image.png](https://qiita-image-store.s3.amazonaws.com/0/254604/bad679a5-68dd-8aff-c6f6-3a1048ec3739.png)

![image.png](https://qiita-image-store.s3.amazonaws.com/0/254604/2ccceaed-1454-2fa7-6ced-843493660cdb.png)

![image.png](https://qiita-image-store.s3.amazonaws.com/0/254604/e89370e9-d333-5534-083d-2428985259ef.png)

![image.png](https://qiita-image-store.s3.amazonaws.com/0/254604/fe9de3f1-20b5-5756-4b1b-3fd751dc03e1.png)