1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# -*- coding: utf-8 -*-
"""ML_Day03_vision04_ModelTransfer.ipynb
Automatically generated by Colaboratory.
Original file is located at
"""
 
from google.colab import drive
drive.mount('/gdrive')
 
PATH = "/gdrive/My Drive/Colab Notebooks/resources/"
 
import matplotlib.pyplot as plt
from tensorflow import keras
import tensorflow as tf
import numpy as np
 
print(tf.__version__)
 
import os
 
## import layer to handle which layer to be taken
from tensorflow.keras import layers
from tensorflow.keras import Model
 
!wget --no-check-certificate \
    https://storage.googleapis.com/mledu-datasets/inception_v3_weights_tf_dim_ordering_tf_kernels_notop.h5 \
    -/tmp/inception_v3_weights_tf_dim_ordering_tf_kernels_notop.h5
 
## import model skelton
from tensorflow.keras.applications.inception_v3 import InceptionV3
 
## load weights
local_weights_file = '/tmp/inception_v3_weights_tf_dim_ordering_tf_kernels_notop.h5'
 
## setup model skelton
## make include_top to false, because there are fully connected dense layers
pre_trained_model = InceptionV3(input_shape = (1501503), 
                                include_top = False, 
                                weights = None)
 
## fit weights to the model skeleton
pre_trained_model.load_weights(local_weights_file)
 
## turn trainable option to false to lock upper layers in pretrained model
for layer in pre_trained_model.layers:
  layer.trainable = False
  
pre_trained_model.summary()
 
## cut from certain layer to generalize model
last_layer = pre_trained_model.get_layer('mixed7')
 
## check shape of last layer from which we cut
print('last layer output shape: ', last_layer.output_shape)
last_output = last_layer.output
 
from tensorflow.keras.optimizers import RMSprop
 
# Flatten the output layer to 1 dimension
= layers.Flatten()(last_output)
 
# Add a fully connected layer with 1,024 hidden units and ReLU activation
= layers.Dense(1024, activation='relu')(x)
 
# Add a dropout rate of 0.2
= layers.Dropout(0.2)(x)                  
 
# Add a final sigmoid layer for classification
= layers.Dense  (1, activation='sigmoid')(x)           
 
# Add our new layer to pretrained model
model = Model( pre_trained_model.input, x)
 
# specify optimizer and loss func
model.compile(optimizer = RMSprop(lr=0.0001), 
              loss = 'binary_crossentropy'
              metrics = ['acc'])
 
## download dog vs cat dataset
!wget --no-check-certificate \
        https://storage.googleapis.com/mledu-datasets/cats_and_dogs_filtered.zip \
       -/tmp/cats_and_dogs_filtered.zip
 
    
from tensorflow.keras.preprocessing.image import ImageDataGenerator
 
import os
import zipfile
 
local_zip = '//tmp/cats_and_dogs_filtered.zip'
 
zip_ref = zipfile.ZipFile(local_zip, 'r')
 
zip_ref.extractall('/tmp')
zip_ref.close()
 
# Define our example directories and files
base_dir = '/tmp/cats_and_dogs_filtered'
 
train_dir = os.path.join( base_dir, 'train')
validation_dir = os.path.join( base_dir, 'validation')
 
 
train_cats_dir = os.path.join(train_dir, 'cats'# Directory with our training cat pictures
train_dogs_dir = os.path.join(train_dir, 'dogs'# Directory with our training dog pictures
validation_cats_dir = os.path.join(validation_dir, 'cats'# Directory with our validation cat pictures
validation_dogs_dir = os.path.join(validation_dir, 'dogs')# Directory with our validation dog pictures
 
train_cat_fnames = os.listdir(train_cats_dir)
train_dog_fnames = os.listdir(train_dogs_dir)
 
# Add our data-augmentation parameters to ImageDataGenerator
train_datagen = ImageDataGenerator(rescale = 1./255.,
                                   rotation_range = 40,
                                   width_shift_range = 0.2,
                                   height_shift_range = 0.2,
                                   shear_range = 0.2,
                                   zoom_range = 0.2,
                                   horizontal_flip = True)
 
 
# Note that the validation data should not be augmented!
test_datagen = ImageDataGenerator( rescale = 1.0/255. )
 
# Flow training images in batches of 20 using train_datagen generator
train_generator = train_datagen.flow_from_directory(train_dir,
                                                    batch_size = 20,
                                                    class_mode = 'binary'
                                                    target_size = (150150))     
 
 
# Flow validation images in batches of 20 using test_datagen generator
validation_generator =  test_datagen.flow_from_directory( validation_dir,
                                                          batch_size  = 20,
                                                          class_mode  = 'binary'
                                                          target_size = (150150))
 
history = model.fit_generator(
            train_generator,
            validation_data = validation_generator,
            steps_per_epoch = 100,
            epochs = 20,
            validation_steps = 50,
            verbose = 2)
 
import matplotlib.pyplot as plt
acc = history.history['acc']
val_acc = history.history['val_acc']
loss = history.history['loss']
val_loss = history.history['val_loss']
 
epochs = range(len(acc))
 
plt.plot(epochs, acc, 'r', label='Training accuracy')
plt.plot(epochs, val_acc, 'b', label='Validation accuracy')
plt.title('Training and validation accuracy')
plt.legend(loc=0)
plt.figure()
 
 
plt.show()
 
cs



참고자료 및 출처 : Convolutional Neural Networks in TensorFlow - Weak 3 Exercise

+ Recent posts