import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Activation, Flatten, Conv2D, MaxPooling2D
from tensorflow.keras.callbacks import TensorBoard
import pickle, os, time


DATADIR="data/"
NAME="cachorros-gatos-cnn-128-128-128-{}".format(int(time.time()))

tensorboard = TensorBoard(log_dir="logs/{}".format(NAME))


gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.4)
sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options))


def getData():
	X = pickle.load(open(DATADIR + "X.pickle", "rb"))
	y = pickle.load(open(DATADIR + "y.pickle", "rb"))

	return X, y


def normalizeData(X):
	return X/255.0	# já que numa imagem o valor máximo é 255 para cada pixels, é só dividir por 255.


def saveModel(model):
	model.save("128-128-128-CNN-noDense.model")


def trainModel(model, training_set):
	X, y = training_set

	model.compile(loss="binary_crossentropy", optimizer="adam", metrics=["accuracy"])
	model.fit(X, y, batch_size=32, validation_split=0.1, epochs=7, callbacks=[tensorboard])
	return model


def createModel(X):
	model = Sequential()

	model.add(Conv2D(128, (3,3), input_shape=X.shape[1:]))
	model.add(Activation("relu"))
	model.add(MaxPooling2D(pool_size=(2, 2)))

	model.add(Conv2D(128, (4,4)))
	model.add(Activation("relu"))
	model.add(MaxPooling2D(pool_size=(2, 2)))

	model.add(Conv2D(128, (3,3)))
	model.add(Activation("relu"))
	model.add(MaxPooling2D(pool_size=(2, 2)))

	model.add(Flatten())

	model.add(Dense(1))
	model.add(Activation("sigmoid"))

	return model


def main():
	X, y = getData()
	X = normalizeData(X)
	model = createModel(X)
	model = trainModel(model, (X, y))
	#saveModel(model)



main()
tensorflow.keras.layers.Activation tensorflow.keras.layers.Dense tensorflow.keras.layers.Conv2D tensorflow.ConfigProto tensorflow.GPUOptions tensorflow.keras.layers.MaxPooling2D tensorflow.keras.models.Sequential tensorflow.keras.layers.Flatten
[(14, 'tensorflow.GPUOptions', 'tf.GPUOptions', ([], {'per_process_gpu_memory_fraction': '(0.4)'}), True, 'import tensorflow as tf\n'), (42, 'tensorflow.keras.models.Sequential', 'Sequential', ([], {}), False, 'from tensorflow.keras.models import Sequential\n'), (9, 'time.time', 'time.time', ([], {}), False, 'import pickle, os, time\n'), (15, 'tensorflow.ConfigProto', 'tf.ConfigProto', ([], {'gpu_options': 'gpu_options'}), True, 'import tensorflow as tf\n'), (44, 'tensorflow.keras.layers.Conv2D', 'Conv2D', (['(128)', '(3, 3)'], {'input_shape': 'X.shape[1:]'}), False, 'from tensorflow.keras.layers import Dense, Dropout, Activation, Flatten, Conv2D, MaxPooling2D\n'), (45, 'tensorflow.keras.layers.Activation', 'Activation', (['"""relu"""'], {}), False, 'from tensorflow.keras.layers import Dense, Dropout, Activation, Flatten, Conv2D, MaxPooling2D\n'), (46, 'tensorflow.keras.layers.MaxPooling2D', 'MaxPooling2D', ([], {'pool_size': '(2, 2)'}), False, 'from tensorflow.keras.layers import Dense, Dropout, Activation, Flatten, Conv2D, MaxPooling2D\n'), (48, 'tensorflow.keras.layers.Conv2D', 'Conv2D', (['(128)', '(4, 4)'], {}), False, 'from tensorflow.keras.layers import Dense, Dropout, Activation, Flatten, Conv2D, MaxPooling2D\n'), (49, 'tensorflow.keras.layers.Activation', 'Activation', (['"""relu"""'], {}), False, 'from tensorflow.keras.layers import Dense, Dropout, Activation, Flatten, Conv2D, MaxPooling2D\n'), (50, 'tensorflow.keras.layers.MaxPooling2D', 'MaxPooling2D', ([], {'pool_size': '(2, 2)'}), False, 'from tensorflow.keras.layers import Dense, Dropout, Activation, Flatten, Conv2D, MaxPooling2D\n'), (52, 'tensorflow.keras.layers.Conv2D', 'Conv2D', (['(128)', '(3, 3)'], {}), False, 'from tensorflow.keras.layers import Dense, Dropout, Activation, Flatten, Conv2D, MaxPooling2D\n'), (53, 'tensorflow.keras.layers.Activation', 'Activation', (['"""relu"""'], {}), False, 'from tensorflow.keras.layers import Dense, Dropout, Activation, Flatten, Conv2D, MaxPooling2D\n'), (54, 'tensorflow.keras.layers.MaxPooling2D', 'MaxPooling2D', ([], {'pool_size': '(2, 2)'}), False, 'from tensorflow.keras.layers import Dense, Dropout, Activation, Flatten, Conv2D, MaxPooling2D\n'), (56, 'tensorflow.keras.layers.Flatten', 'Flatten', ([], {}), False, 'from tensorflow.keras.layers import Dense, Dropout, Activation, Flatten, Conv2D, MaxPooling2D\n'), (58, 'tensorflow.keras.layers.Dense', 'Dense', (['(1)'], {}), False, 'from tensorflow.keras.layers import Dense, Dropout, Activation, Flatten, Conv2D, MaxPooling2D\n'), (59, 'tensorflow.keras.layers.Activation', 'Activation', (['"""sigmoid"""'], {}), False, 'from tensorflow.keras.layers import Dense, Dropout, Activation, Flatten, Conv2D, MaxPooling2D\n')]