"""
Downloading and saving Keras models as h5 files.

including the top, in order to do inference later on.

@Hans Dhondea

4 August 2020
"""

import tensorflow as tf
print('TensorFlow version')
print(tf.__version__)

print('VGG16')
vgg16_model = tf.keras.applications.VGG16(weights='imagenet',include_top=True)
vgg16_model.summary()

tf.keras.utils.plot_model(vgg16_model,to_file='main_save_keras_models_vgg16_model_architecture.png',show_shapes=True)
tf.keras.utils.plot_model(vgg16_model,to_file='main_save_keras_models_vgg16_model_architecture.pdf',show_shapes=True)
vgg16_model.save("vgg16_model.h5")

print('VGG19')
vgg19_model = tf.keras.applications.VGG19(weights='imagenet',include_top=True)
vgg19_model.summary()

tf.keras.utils.plot_model(vgg19_model,to_file='main_save_keras_models_vgg19_model_architecture.png',show_shapes=True)
tf.keras.utils.plot_model(vgg19_model,to_file='main_save_keras_models_vgg19_model_architecture.pdf',show_shapes=True)
vgg19_model.save("vgg19_model.h5")

print('Xception')
xception_model = tf.keras.applications.Xception(weights='imagenet',include_top=True)
xception_model.summary()

tf.keras.utils.plot_model(xception_model,to_file='main_save_keras_models_xception_model_architecture.png',show_shapes=True)
tf.keras.utils.plot_model(xception_model,to_file='main_save_keras_models_xception_model_architecture.pdf',show_shapes=True)
xception_model.save("xception_model.h5")

print('ResNet50')
resnet50_model = tf.keras.applications.ResNet50(weights='imagenet',include_top=True)
resnet50_model.summary()

tf.keras.utils.plot_model(resnet50_model,to_file='main_save_keras_models_resnet50_architecture.png',show_shapes=True)
tf.keras.utils.plot_model(resnet50_model,to_file='main_save_keras_models_resnet50_architecture.pdf',show_shapes=True)
resnet50_model.save("resnet50_model.h5")

print('ResNet50v2')
resnet50_v2_model = tf.keras.applications.ResNet50V2(weights='imagenet',include_top=True)
resnet50_v2_model.summary()

tf.keras.utils.plot_model(resnet50_v2_model,to_file='main_save_keras_models_resnet50_v2_model_architecture.png',show_shapes=True)
tf.keras.utils.plot_model(resnet50_v2_model,to_file='main_save_keras_models_resnet50_v2_model_architecture.pdf',show_shapes=True)
resnet50_v2_model.save("resnet50_v2_model.h5")

print('InceptionResNetV2')
inceptionresnet_v2_model = tf.keras.applications.InceptionResNetV2(weights='imagenet',include_top=True)
inceptionresnet_v2_model.summary()

tf.keras.utils.plot_model(inceptionresnet_v2_model,to_file='main_save_keras_models_inceptionresnet_v2_model_architecture.png',show_shapes=True)
tf.keras.utils.plot_model(inceptionresnet_v2_model,to_file='main_save_keras_models_inceptionresnet_v2_model_architecture.pdf',show_shapes=True)
inceptionresnet_v2_model.save("inceptionresnet_v2_model.h5")

print('Models saved. Architectures saved.')

tensorflow.keras.applications.ResNet50V2 tensorflow.keras.utils.plot_model tensorflow.keras.applications.VGG19 tensorflow.keras.applications.Xception tensorflow.keras.applications.ResNet50 tensorflow.keras.applications.VGG16 tensorflow.keras.applications.InceptionResNetV2
[(16, 'tensorflow.keras.applications.VGG16', 'tf.keras.applications.VGG16', ([], {'weights': '"""imagenet"""', 'include_top': '(True)'}), True, 'import tensorflow as tf\n'), (19, 'tensorflow.keras.utils.plot_model', 'tf.keras.utils.plot_model', (['vgg16_model'], {'to_file': '"""main_save_keras_models_vgg16_model_architecture.png"""', 'show_shapes': '(True)'}), True, 'import tensorflow as tf\n'), (20, 'tensorflow.keras.utils.plot_model', 'tf.keras.utils.plot_model', (['vgg16_model'], {'to_file': '"""main_save_keras_models_vgg16_model_architecture.pdf"""', 'show_shapes': '(True)'}), True, 'import tensorflow as tf\n'), (24, 'tensorflow.keras.applications.VGG19', 'tf.keras.applications.VGG19', ([], {'weights': '"""imagenet"""', 'include_top': '(True)'}), True, 'import tensorflow as tf\n'), (27, 'tensorflow.keras.utils.plot_model', 'tf.keras.utils.plot_model', (['vgg19_model'], {'to_file': '"""main_save_keras_models_vgg19_model_architecture.png"""', 'show_shapes': '(True)'}), True, 'import tensorflow as tf\n'), (28, 'tensorflow.keras.utils.plot_model', 'tf.keras.utils.plot_model', (['vgg19_model'], {'to_file': '"""main_save_keras_models_vgg19_model_architecture.pdf"""', 'show_shapes': '(True)'}), True, 'import tensorflow as tf\n'), (32, 'tensorflow.keras.applications.Xception', 'tf.keras.applications.Xception', ([], {'weights': '"""imagenet"""', 'include_top': '(True)'}), True, 'import tensorflow as tf\n'), (35, 'tensorflow.keras.utils.plot_model', 'tf.keras.utils.plot_model', (['xception_model'], {'to_file': '"""main_save_keras_models_xception_model_architecture.png"""', 'show_shapes': '(True)'}), True, 'import tensorflow as tf\n'), (36, 'tensorflow.keras.utils.plot_model', 'tf.keras.utils.plot_model', (['xception_model'], {'to_file': '"""main_save_keras_models_xception_model_architecture.pdf"""', 'show_shapes': '(True)'}), True, 'import tensorflow as tf\n'), (40, 'tensorflow.keras.applications.ResNet50', 'tf.keras.applications.ResNet50', ([], {'weights': '"""imagenet"""', 'include_top': '(True)'}), True, 'import tensorflow as tf\n'), (43, 'tensorflow.keras.utils.plot_model', 'tf.keras.utils.plot_model', (['resnet50_model'], {'to_file': '"""main_save_keras_models_resnet50_architecture.png"""', 'show_shapes': '(True)'}), True, 'import tensorflow as tf\n'), (44, 'tensorflow.keras.utils.plot_model', 'tf.keras.utils.plot_model', (['resnet50_model'], {'to_file': '"""main_save_keras_models_resnet50_architecture.pdf"""', 'show_shapes': '(True)'}), True, 'import tensorflow as tf\n'), (48, 'tensorflow.keras.applications.ResNet50V2', 'tf.keras.applications.ResNet50V2', ([], {'weights': '"""imagenet"""', 'include_top': '(True)'}), True, 'import tensorflow as tf\n'), (51, 'tensorflow.keras.utils.plot_model', 'tf.keras.utils.plot_model', (['resnet50_v2_model'], {'to_file': '"""main_save_keras_models_resnet50_v2_model_architecture.png"""', 'show_shapes': '(True)'}), True, 'import tensorflow as tf\n'), (52, 'tensorflow.keras.utils.plot_model', 'tf.keras.utils.plot_model', (['resnet50_v2_model'], {'to_file': '"""main_save_keras_models_resnet50_v2_model_architecture.pdf"""', 'show_shapes': '(True)'}), True, 'import tensorflow as tf\n'), (56, 'tensorflow.keras.applications.InceptionResNetV2', 'tf.keras.applications.InceptionResNetV2', ([], {'weights': '"""imagenet"""', 'include_top': '(True)'}), True, 'import tensorflow as tf\n'), (59, 'tensorflow.keras.utils.plot_model', 'tf.keras.utils.plot_model', (['inceptionresnet_v2_model'], {'to_file': '"""main_save_keras_models_inceptionresnet_v2_model_architecture.png"""', 'show_shapes': '(True)'}), True, 'import tensorflow as tf\n'), (60, 'tensorflow.keras.utils.plot_model', 'tf.keras.utils.plot_model', (['inceptionresnet_v2_model'], {'to_file': '"""main_save_keras_models_inceptionresnet_v2_model_architecture.pdf"""', 'show_shapes': '(True)'}), True, 'import tensorflow as tf\n')]