from IPython.display import YouTubeVideo
YouTubeVideo('ypt_BAotCLo', width=800, height=450)

"""
Explanation: Data Augmentation
Let's start things off with a video, shall we?
End of explanation
"""


from tensorflow.python.keras.applications import ResNet50
from tensorflow.python.keras.models import Sequential
from tensorflow.python.keras.layers import Dense, Flatten, GlobalAveragePooling2D

num_classes = 2
resnet_weights_path = 'inputs/resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5'

my_new_model = Sequential()
my_new_model.add(ResNet50(include_top=False, pooling='avg', weights=resnet_weights_path))
my_new_model.add(Dense(num_classes, activation='softmax'))

# Don't forget, your first layer is already trained.
my_new_model.layers[0].trainable = False

my_new_model.compile(optimizer='sgd', loss='categorical_crossentropy', metrics=['accuracy'])

my_new_model

dir(my_new_model)

"""
Explanation: You can embed videos from YouTube or Vimeo.
Have fun with that.
Anyhow:
End of explanation
"""


from tensorflow.python.keras.applications.resnet50 import preprocess_input
from tensorflow.python.keras.preprocessing.image import ImageDataGenerator

image_size = 224

data_generator_with_aug = ImageDataGenerator(horizontal_flip=True,
                                             width_shift_range=0.2,
                                             height_shift_range=0.2)

train_generator = data_generator_with_aug.flow_from_directory('inputs/rural_and_urban_photos/train',
                                                             target_size=(image_size, image_size),
                                                             batch_size=24,
                                                             class_mode='categorical')

data_generator_no_aug = ImageDataGenerator()
validation_generator = data_generator_no_aug.flow_from_directory('inputs/rural_and_urban_photos/val',
                                                               target_size=(image_size, image_size),
                                                               class_mode='categorical')

my_new_model.fit_generator(train_generator,
                          steps_per_epoch=3,
                          epochs=2,
                          validation_data=validation_generator,
                          validation_steps=1)

"""
Explanation: Fitting a Model Using Data Augmentation
End of explanation
"""


from tensorflow.python.keras.applications import ResNet50
from tensorflow.python.keras.models import Sequential
from tensorflow.python.keras.layers import Dense, Flatten, GlobalAveragePooling2D

num_classes = 2
resnet_weights_path = 'inputs/resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5'

my_new_model = Sequential()
my_new_model.add(ResNet50(include_top=False, pooling='avg', weights=resnet_weights_path))
my_new_model.add(Dense(num_classes, activation='softmax'))

my_new_model.layers[0].trainable = False

my_new_model.compile(optimizer='sgd', loss='categorical_crossentropy', metrics=['accuracy'])
my_new_model

"""
Explanation: Exercise: Data Augmentation
Repetition helps it sink in, so here we go: 
Specify and Compile the Model
End of explanation
"""


from tensorflow.python.keras.applications.resnet50 import preprocess_input
from tensorflow.python.keras.preprocessing.image import ImageDataGenerator

image_size = 224

data_generator_with_aug = ImageDataGenerator(horizontal_flip = image_size,
                                            width_shift_range = image_size,
                                            height_shift_range = image_size)

data_generator_no_aug = ImageDataGenerator()

train_generator = data_generator_with_aug.flow_from_directory(directory='inputs/dogs_gone_sideways/images/train',
                                                             target_size=(image_size, image_size),
                                                             batch_size=12,
                                                             class_mode='categorical')

validation_generator = data_generator_no_aug.flow_from_directory(directory='inputs/dogs_gone_sideways/images/val',
                                                                target_size=(image_size, image_size),
                                                                class_mode='categorical')

my_new_model.fit_generator(train_generator,
                          epochs=3,
                          steps_per_epoch=19,
                          validation_data=validation_generator)

"""
Explanation: Fit the Model Using Data Augmentation:
The following model should achieve ~90% accuracy.
You can use data augmentation to cut the error rate in half.
End of explanation
"""


