import numpy as np
np.random.seed(42)

"""
Explanation: AlexNet in Keras
In this notebook, we leverage an AlexNet-like deep, convolutional neural network to classify flowers into the 17 categories of the Oxford Flowers data set. Derived from this earlier notebook.
Set seed for reproducibility
End of explanation
"""


import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPooling2D
from keras.layers.normalization import BatchNormalization
from keras.callbacks import TensorBoard # for part 3.5 on TensorBoard

"""
Explanation: Load dependencies
End of explanation
"""


import tflearn.datasets.oxflower17 as oxflower17
X, Y = oxflower17.load_data(one_hot=True)

"""
Explanation: Load and preprocess data
End of explanation
"""


model = Sequential()

model.add(Conv2D(96, kernel_size=(11, 11), strides=(4, 4), activation='relu', input_shape=(224, 224, 3)))
model.add(MaxPooling2D(pool_size=(3, 3), strides=(2, 2)))
model.add(BatchNormalization())

model.add(Conv2D(256, kernel_size=(5, 5), activation='relu'))
model.add(MaxPooling2D(pool_size=(3, 3), strides=(2, 2)))
model.add(BatchNormalization())

model.add(Conv2D(256, kernel_size=(3, 3), activation='relu'))
model.add(Conv2D(384, kernel_size=(3, 3), activation='relu'))
model.add(Conv2D(384, kernel_size=(3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(3, 3), strides=(2, 2)))
model.add(BatchNormalization())

model.add(Flatten())
model.add(Dense(4096, activation='tanh'))
model.add(Dropout(0.5))
model.add(Dense(4096, activation='tanh'))
model.add(Dropout(0.5))

model.add(Dense(17, activation='softmax'))

model.summary()

"""
Explanation: Design neural network architecture
End of explanation
"""


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

"""
Explanation: Configure model
End of explanation
"""


tensorbrd = TensorBoard('logs/alexnet') 

"""
Explanation: Configure TensorBoard (for part 5 of lesson 3)
End of explanation
"""


model.fit(X, Y, batch_size=64, epochs=100, verbose=1, validation_split=0.1, shuffle=True, 
          callbacks=[tensorbrd])

"""
Explanation: Train!
End of explanation
"""


