<reponame>larry1995/U-Net-based-image-segmentation
from model import *
from data import *
import pandas as pd
import numpy as np
from tensorflow.keras.callbacks import EarlyStopping,ModelCheckpoint
#os.environ["CUDA_VISIBLE_DEVICES"] = "0"
import os
import cv2
import matplotlib as plt
import tensorflow as tf

data_gen_args = dict(
					rotation_range=0.2,
                    width_shift_range=0.05,
                    height_shift_range=0.05,
                    shear_range=0.05,
                    zoom_range=0.01,
                    horizontal_flip=True,
                    fill_mode='nearest'
                    
                    )






#image_normalized('data/membrane/train/image_ilker','data/membrane/train/new_image')
#image_normalized('data/membrane/train/masks_ilker','data/membrane/train/new_mask')

#histoequalization('data/membrane/raw',20)
myGene = trainGenerator(5,'data/membrane/train','raw','mitochondria',data_gen_args,save_to_dir ="data/membrane/train/augment")
vali = validationGenerator(5,'data/membrane/validation','testing','testing_groundtruth')


 
model = unet()
#earlyStopping=EarlyStopping(monitor='val_loss', patience=80, verbose=1, mode='auto')
filepath="weights_best.hdf5"

checkpoint = ModelCheckpoint(filepath, monitor='val_acc', verbose=1, save_best_only=True,mode='max')
#callbacks_list = [checkpoint]
#earlyStopping=keras.callbacks.EarlyStopping(monitor='val_loss', patience=0, verbose=1, mode='auto')

#model_checkpoint = ModelCheckpoint(monitor='val_acc',verbose=1, save_best_only=True)
#history = model.fit_generator(myGene,steps_per_epoch=10,epochs=100,verbose = 1,validation_data= vali,validation_steps=5,max_queue_size = 1)
#history = model.fit_generator(myGene,steps_per_epoch=9,epochs=130,validation_data= vali,validation_steps=5,max_queue_size = 1,verbose = 1)
history = model.fit_generator(myGene,steps_per_epoch=10,epochs=200,verbose = 1,validation_data= vali,class_weight = [1.3,0.7],validation_steps=5,max_queue_size = 1)
model.save('trained.h5')
print("Second Training")
model = load_model('weights_best.hdf5')
#model = load_model('trained.h5')
model.fit_generator(myGene,steps_per_epoch=10,epochs=130,verbose = 1,validation_data= vali,class_weight = [1.3,0.7],validation_steps=5,max_queue_size = 1)
testGene = testGenerator("data/membrane/055_HIPPO")
#results = model.predict_generator(testGene,11,verbose=1)
results = model.predict_generator(testGene,23,verbose=1)



saveResult("data/membrane/055_HIPPO",results)
post_processing("data/membrane/055_HIPPO","data/membrane/testdata_processed",23) 

from model import *
from data import *
import pandas as pd
import numpy as np
from tensorflow.keras.callbacks import EarlyStopping,ModelCheckpoint
#os.environ["CUDA_VISIBLE_DEVICES"] = "0"
import os
import cv2
import matplotlib as plt
import tensorflow as tf

data_gen_args = dict(
					rotation_range=0.2,
                    width_shift_range=0.05,
                    height_shift_range=0.05,
                    shear_range=0.05,
                    zoom_range=0.01,
                    horizontal_flip=True,
                    fill_mode='nearest'
                    
                    )






#image_normalized('data/membrane/train/image_ilker','data/membrane/train/new_image')
#image_normalized('data/membrane/train/masks_ilker','data/membrane/train/new_mask')

#histoequalization('data/membrane/raw',20)
myGene = trainGenerator(5,'data/membrane/train','raw','mitochondria',data_gen_args,save_to_dir ="data/membrane/train/augment")
vali = validationGenerator(5,'data/membrane/validation','testing','testing_groundtruth')


 
model = unet()
#earlyStopping=EarlyStopping(monitor='val_loss', patience=80, verbose=1, mode='auto')
filepath="weights_best.hdf5"

checkpoint = ModelCheckpoint(filepath, monitor='val_acc', verbose=1, save_best_only=True,mode='max')
#callbacks_list = [checkpoint]
#earlyStopping=keras.callbacks.EarlyStopping(monitor='val_loss', patience=0, verbose=1, mode='auto')

#model_checkpoint = ModelCheckpoint(monitor='val_acc',verbose=1, save_best_only=True)
#history = model.fit_generator(myGene,steps_per_epoch=10,epochs=100,verbose = 1,validation_data= vali,validation_steps=5,max_queue_size = 1)
#history = model.fit_generator(myGene,steps_per_epoch=9,epochs=130,validation_data= vali,validation_steps=5,max_queue_size = 1,verbose = 1)
history = model.fit_generator(myGene,steps_per_epoch=10,epochs=200,verbose = 1,validation_data= vali,class_weight = [1.3,0.7],validation_steps=5,max_queue_size = 1)
model.save('trained.h5')
print("Second Training")
model = load_model('weights_best.hdf5')
#model = load_model('trained.h5')
model.fit_generator(myGene,steps_per_epoch=10,epochs=130,verbose = 1,validation_data= vali,class_weight = [1.3,0.7],validation_steps=5,max_queue_size = 1)
testGene = testGenerator("data/membrane/055_HIPPO")
#results = model.predict_generator(testGene,11,verbose=1)
results = model.predict_generator(testGene,23,verbose=1)



saveResult("data/membrane/055_HIPPO",results)
post_processing("data/membrane/055_HIPPO","data/membrane/testdata_processed",23)
#os.environ["CUDA_VISIBLE_DEVICES"] = "0" #image_normalized('data/membrane/train/image_ilker','data/membrane/train/new_image') #image_normalized('data/membrane/train/masks_ilker','data/membrane/train/new_mask') #histoequalization('data/membrane/raw',20) #earlyStopping=EarlyStopping(monitor='val_loss', patience=80, verbose=1, mode='auto') #callbacks_list = [checkpoint] #earlyStopping=keras.callbacks.EarlyStopping(monitor='val_loss', patience=0, verbose=1, mode='auto') #model_checkpoint = ModelCheckpoint(monitor='val_acc',verbose=1, save_best_only=True) #history = model.fit_generator(myGene,steps_per_epoch=10,epochs=100,verbose = 1,validation_data= vali,validation_steps=5,max_queue_size = 1) #history = model.fit_generator(myGene,steps_per_epoch=9,epochs=130,validation_data= vali,validation_steps=5,max_queue_size = 1,verbose = 1) #model = load_model('trained.h5') #results = model.predict_generator(testGene,11,verbose=1)