# -*- coding: utf-8 -*-
"""
Created on Thu Feb  9 06:51:40 2023

@author: errobin
"""

import matplotlib.pyplot as plt
import tensorflow as tf
import random as ra
import numpy as np


#lecture image ######################################
from PIL import Image #pour lire une image
image_file = Image.open("arbre.bmp") #mettre le nom de l'image qui doit etre ds le meme répertoire que le programme IMAQ_exercices.py
image_bmp = 1.*np.array(image_file) # on transforme le fichier en matrice nommée image_bmp
image_file.close()
#####################################################


### Affichage image ##################################
plt.imshow(image_bmp,'gray')
plt.show()
####################################################

# recupère les dimension de l'image ###############
dim=image_bmp.shape

dim_imageX=dim[0]
dim_imageY=dim[1]
image_traitee=np.zeros((dim_imageX,dim_imageY))
###################################################

n_rep=6

X_train=np.zeros((n_rep*dim_imageX*dim_imageY,2)) 
Y_target=np.zeros((n_rep*dim_imageX*dim_imageY,1))

compt=0
for k in range(0,n_rep):
    for i in range(0,dim_imageX):
        for j in range(0,dim_imageY):
            
            X_train[compt,0]=i/(1*dim_imageX)
            X_train[compt,1]=j/(1*dim_imageY)
            Y_target[compt,0]=image_bmp[i,j]/255.+ra.uniform(-0.05,0.05)
            compt=compt+1
  
            
from sklearn.utils import shuffle            
X_train,Y_target=shuffle(X_train,Y_target, random_state=0)          
################################################################### RESEAU NEURONES
# Flatten
model = tf.keras.models.Sequential()

# Add the layers
# model.add(tf.keras.layers.Dense(2048, activation="relu"))
# model.add(tf.keras.layers.Dense(2048, activation="relu"))
# model.add(tf.keras.layers.Dense(2048, activation="relu"))
# model.add(tf.keras.layers.Dense(2048, activation="relu"))
# model.add(tf.keras.layers.Dense(1024, activation="relu"))
# model.add(tf.keras.layers.Dense(1024, activation="relu"))
# model.add(tf.keras.layers.Dense(1024, activation="relu"))
# model.add(tf.keras.layers.Dense(1024, activation="relu"))
# model.add(tf.keras.layers.Dense(512, activation="relu"))
# model.add(tf.keras.layers.Dense(512, activation="relu"))
# model.add(tf.keras.layers.Dense(512, activation="relu"))
# model.add(tf.keras.layers.Dense(512, activation="relu"))
# model.add(tf.keras.layers.Dense(128, activation="relu"))
# model.add(tf.keras.layers.Dense(128, activation="relu"))
# model.add(tf.keras.layers.Dense(128, activation="relu"))
# model.add(tf.keras.layers.Dense(128, activation="relu"))
# model.add(tf.keras.layers.Dense(64, activation="relu"))
# model.add(tf.keras.layers.Dense(64, activation="relu"))
# model.add(tf.keras.layers.Dense(64, activation="relu"))
# model.add(tf.keras.layers.Dense(64, activation="relu"))
# model.add(tf.keras.layers.Dense(32, activation="relu"))
# model.add(tf.keras.layers.Dense(32, activation="relu"))
# model.add(tf.keras.layers.Dense(32, activation="relu"))
# model.add(tf.keras.layers.Dense(32, activation="relu"))
# model.add(tf.keras.layers.Dense(16, activation="relu"))
# model.add(tf.keras.layers.Dense(16, activation="relu"))
# model.add(tf.keras.layers.Dense(16, activation="relu"))
# model.add(tf.keras.layers.Dense(16, activation="relu"))
# model.add(tf.keras.layers.Dense(8, activation="relu"))
# model.add(tf.keras.layers.Dense(8, activation="relu"))
# model.add(tf.keras.layers.Dense(8, activation="relu"))
# model.add(tf.keras.layers.Dense(8, activation="relu"))
# model.add(tf.keras.layers.Dense(1))

"""
model.add(tf.keras.layers.Dense(256, activation="relu"))
model.add(tf.keras.layers.Dense(256, activation="relu"))
model.add(tf.keras.layers.Dense(256, activation="relu"))
model.add(tf.keras.layers.Dense(256, activation="relu"))
model.add(tf.keras.layers.Dense(128, activation="relu"))
model.add(tf.keras.layers.Dense(128, activation="relu"))
model.add(tf.keras.layers.Dense(128, activation="relu"))
model.add(tf.keras.layers.Dense(1))
"""

model.add(tf.keras.layers.Dense(256, activation="relu"))
model.add(tf.keras.layers.Dense(128, activation="relu"))
model.add(tf.keras.layers.Dense(64, activation="relu"))
model.add(tf.keras.layers.Dense(32, activation="relu"))
model.add(tf.keras.layers.Dense(1))

####################################################################

################################################################## Optimisation du reseau
# Compile the model
model.compile(
    loss="mse",
    optimizer="adam"
)
##

def scheduler(epochs):
      
    return  0.01 

    if epochs>5:
        return 0.01

callback = tf.keras.callbacks.LearningRateScheduler(scheduler)
 
    
################################################################ ENTRAINEMENT DU RESEAU
model.fit(X_train, Y_target, epochs=20, callbacks=[callback],verbose=1,validation_split=0.2)
###############################################################

#sauvegarde ANN
model.save('arbre.h5') 