def trainTopModelMobilenet(): print( '\n*************************Train top models Mobilenet*****************************' ) mobilenet = classes.ComplexModel(baseModels.Mobilenet(), (299, 299), "mobilenet/") data, labels, numClasses = mobilenet.loadAndPreprocessData( destPathSelected, log=True) numberSplits = classes.obtainNumSplitKFold(len(labels), 20) folds = list( StratifiedKFold(n_splits=numberSplits, shuffle=True).split(data, labels)) for fold, (trainIdx, validIdx) in enumerate(folds): print('\n[INFO] Executing fold ', fold) trainData = data[trainIdx] trainLabels = labels[trainIdx] validData = data[validIdx] validLabels = labels[validIdx] mobilenet.buildTopModel(classes.modelType.typeA) trainingTime, historyGenerated = mobilenet.trainTopModel( trainData, trainLabels, validData, validLabels, fold) mobilenet.testTopModel(validData, validLabels, trainingTime, historyGenerated, fold) print('\n************END ' + str(fold) + '***************')
def fineTuneMobilenet(): mobilenet = classes.ComplexModel(baseModels.Mobilenet(), (299, 299), "mobilenet/") data, labels, numClasses = mobilenet.loadAndPreprocessData( destPathSelected, log=True) numberSplits = classes.obtainNumSplitKFold(len(labels), 20) folds = list( StratifiedKFold(n_splits=numberSplits, shuffle=True).split(data, labels)) informationBestModel = classes.obtainBestModel("mobilenet/topModel/") print( '\n*************************Fine Tune Mobilenet*****************************' ) for fold, (trainIdx, validIdx) in enumerate(folds): print('\n[INFO] Executing fold ', fold) trainData = data[trainIdx] trainLabels = labels[trainIdx] validData = data[validIdx] validLabels = labels[validIdx] trainingTime, historyGenerated = mobilenet.fineTune( trainData, trainLabels, validData, validLabels, 76, informationBestModel[1], informationBestModel[0], fold) mobilenet.testFineTuneModel(validData, validLabels, trainingTime, historyGenerated, fold, 15) print('\n************END ' + str(fold) + '***************')
def trainTopModelsVgg16(): # Necessary for split data print( '\n*************************Train top models VGG16*****************************' ) vgg16Model = classes.Model(baseModels.VGG16(), (224, 224), "vgg16/") data, labels, numClasses = vgg16Model.loadAndPreprocessData( destPathSelected, log=True) numberSplits = classes.obtainNumSplitKFold(len(labels), 20) folds = list( StratifiedKFold(n_splits=numberSplits, shuffle=True).split(data, labels)) for fold, (trainIdx, validIdx) in enumerate(folds): print('\n[INFO] Executing fold ', fold) trainData = data[trainIdx] trainLabels = labels[trainIdx] validData = data[validIdx] validLabels = labels[validIdx] vgg16Model = classes.Model(baseModels.VGG16(), (224, 224), "vgg16/") vgg16Model.numClasses = numClasses vgg16Model.generateBottlenecks(trainData, trainLabels, validData, validLabels, fold) vgg16Model.buildTopModel(classes.modelType.typeA) trainingTime, historyGenerated = vgg16Model.trainTopModel( trainLabels, validLabels, fold) vgg16Model.testTopModel(validData, validLabels, trainingTime, historyGenerated, fold) vgg16Model.buildTopModel(classes.modelType.typeB) trainingTime, historyGenerated = vgg16Model.trainTopModel( trainLabels, validLabels, fold) vgg16Model.testTopModel(validData, validLabels, trainingTime, historyGenerated, fold) vgg16Model.buildTopModel(classes.modelType.typeC) trainingTime, historyGenerated = vgg16Model.trainTopModel( trainLabels, validLabels, fold) vgg16Model.testTopModel(validData, validLabels, trainingTime, historyGenerated, fold) # Free memory del vgg16Model gc.collect() K.clear_session() print('\n************END ' + str(fold) + '***************')
def fineTuneVgg16(): print( '\n*************************Fine Tune VGG16*****************************' ) vgg16Model = classes.Model(baseModels.VGG16(), (224, 224), "vgg16/") data, labels, numClasses = vgg16Model.loadAndPreprocessData( destPathSelected, log=True) numberSplits = classes.obtainNumSplitKFold(len(labels), 20) folds = list( StratifiedKFold(n_splits=numberSplits, shuffle=True).split(data, labels)) informationBestModel = classes.obtainBestModel("vgg16/topModel/") for fold, (trainIdx, validIdx) in enumerate(folds): print('\n[INFO] Executing fold ', fold) trainData = data[trainIdx] trainLabels = labels[trainIdx] validData = data[validIdx] validLabels = labels[validIdx] vgg16Model = classes.Model(baseModels.VGG16(), (224, 224), "vgg16/") vgg16Model.numClasses = numClasses vgg16Model.buildTopModel(informationBestModel[1]) trainingTime, historyGenerated = vgg16Model.fineTune( trainData, trainLabels, validData, validLabels, 15, informationBestModel[1], informationBestModel[0], fold) vgg16Model.testFineTuneModel(validData, validLabels, trainingTime, historyGenerated, fold, 15) # Free memory del vgg16Model gc.collect() K.clear_session() #vgg16Model = classes.Model(baseModels.VGG16(), (224, 224), "vgg16/") #vgg16Model.numClasses = numClasses #vgg16Model.buildTopModel(informationBestModel[1]) #trainingTime, historyGenerated = vgg16Model.fineTune(trainData, trainLabels, validData, validLabels, 12, informationBestModel[1], informationBestModel[0], fold) #vgg16Model.testFineTuneModel(validData, validLabels, trainingTime, historyGenerated, fold, 12) print('\n************END Fold ' + str(fold) + '***************')