Created
March 4, 2021 15:32
-
-
Save hrisheekeshr/6283126509433e74d9c152028e234d54 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tensorflow as tf | |
import tensorflow_hub as hub | |
print("TF version:", tf.__version__) | |
print("Hub version:", hub.__version__) | |
print("GPU is", "available" if tf.test.is_gpu_available() else "NOT AVAILABLE") | |
model_name = "inception_v3" | |
model_handle_map = { | |
"efficientnet_b0": "https://tfhub.dev/tensorflow/efficientnet/b0/feature-vector/1", | |
"efficientnet_b1": "https://tfhub.dev/tensorflow/efficientnet/b1/feature-vector/1", | |
"efficientnet_b2": "https://tfhub.dev/tensorflow/efficientnet/b2/feature-vector/1", | |
"efficientnet_b3": "https://tfhub.dev/tensorflow/efficientnet/b3/feature-vector/1", | |
"efficientnet_b4": "https://tfhub.dev/tensorflow/efficientnet/b4/feature-vector/1", | |
"efficientnet_b5": "https://tfhub.dev/tensorflow/efficientnet/b5/feature-vector/1", | |
"efficientnet_b6": "https://tfhub.dev/tensorflow/efficientnet/b6/feature-vector/1", | |
"efficientnet_b7": "https://tfhub.dev/tensorflow/efficientnet/b7/feature-vector/1", | |
"bit_s-r50x1": "https://tfhub.dev/google/bit/s-r50x1/1", | |
"inception_v3": "https://tfhub.dev/google/imagenet/inception_v3/feature_vector/4", | |
"inception_resnet_v2": "https://tfhub.dev/google/imagenet/inception_resnet_v2/feature-vector/4", | |
"resnet_v1_50": "https://tfhub.dev/google/imagenet/resnet_v1_50/feature-vector/4", | |
"resnet_v1_101": "https://tfhub.dev/google/imagenet/resnet_v1_101/feature-vector/4", | |
"resnet_v1_152": "https://tfhub.dev/google/imagenet/resnet_v1_152/feature-vector/4", | |
"resnet_v2_50": "https://tfhub.dev/google/imagenet/resnet_v2_50/feature-vector/4", | |
"resnet_v2_101": "https://tfhub.dev/google/imagenet/resnet_v2_101/feature-vector/4", | |
"resnet_v2_152": "https://tfhub.dev/google/imagenet/resnet_v2_152/feature-vector/4", | |
"nasnet_large": "https://tfhub.dev/google/imagenet/nasnet_large/feature_vector/4", | |
"nasnet_mobile": "https://tfhub.dev/google/imagenet/nasnet_mobile/feature_vector/4", | |
"pnasnet_large": "https://tfhub.dev/google/imagenet/pnasnet_large/feature_vector/4", | |
"mobilenet_v2_100_224": "https://tfhub.dev/google/imagenet/mobilenet_v2_100_224/feature_vector/4", | |
"mobilenet_v2_130_224": "https://tfhub.dev/google/imagenet/mobilenet_v2_130_224/feature_vector/4", | |
"mobilenet_v2_140_224": "https://tfhub.dev/google/imagenet/mobilenet_v2_140_224/feature_vector/4", | |
"mobilenet_v3_small_100_224": "https://tfhub.dev/google/imagenet/mobilenet_v3_small_100_224/feature_vector/5", | |
"mobilenet_v3_small_075_224": "https://tfhub.dev/google/imagenet/mobilenet_v3_small_075_224/feature_vector/5", | |
"mobilenet_v3_large_100_224": "https://tfhub.dev/google/imagenet/mobilenet_v3_large_100_224/feature_vector/5", | |
"mobilenet_v3_large_075_224": "https://tfhub.dev/google/imagenet/mobilenet_v3_large_075_224/feature_vector/5", | |
} | |
model_image_size_map = { | |
"efficientnet_b0": 224, | |
"efficientnet_b1": 240, | |
"efficientnet_b2": 260, | |
"efficientnet_b3": 300, | |
"efficientnet_b4": 380, | |
"efficientnet_b5": 456, | |
"efficientnet_b6": 528, | |
"efficientnet_b7": 600, | |
"inception_v3": 299, | |
"inception_resnet_v2": 299, | |
"nasnet_large": 331, | |
"pnasnet_large": 331, | |
} | |
model_handle = model_handle_map.get(model_name) | |
pixels = model_image_size_map.get(model_name, 224) | |
print(f"Selected model: {model_name} : {model_handle}") | |
IMAGE_SIZE = (pixels, pixels) | |
print(f"Input size {IMAGE_SIZE}") | |
BATCH_SIZE = 32 | |
data_dir = "flower_photos" | |
print(data_dir) | |
datagen_kwargs = dict(rescale=1./255, validation_split=.20) | |
dataflow_kwargs = dict(target_size=IMAGE_SIZE, batch_size=BATCH_SIZE, | |
interpolation="bilinear") | |
valid_datagen = tf.keras.preprocessing.image.ImageDataGenerator( | |
**datagen_kwargs) | |
valid_generator = valid_datagen.flow_from_directory( | |
data_dir, subset="validation", shuffle=False, **dataflow_kwargs) | |
do_data_augmentation = False | |
if do_data_augmentation: | |
train_datagen = tf.keras.preprocessing.image.ImageDataGenerator( | |
rotation_range=40, | |
horizontal_flip=True, | |
width_shift_range=0.2, height_shift_range=0.2, | |
shear_range=0.2, zoom_range=0.2, | |
**datagen_kwargs) | |
else: | |
train_datagen = valid_datagen | |
train_generator = train_datagen.flow_from_directory( | |
data_dir, subset="training", shuffle=True, **dataflow_kwargs) | |
do_fine_tuning = False | |
print("Building model with", model_handle) | |
model = tf.keras.Sequential([ | |
# Explicitly define the input shape so the model can be properly | |
# loaded by the TFLiteConverter | |
tf.keras.layers.InputLayer(input_shape=IMAGE_SIZE + (3,)), | |
hub.KerasLayer("https://tfhub.dev/google/imagenet/inception_v3/feature_vector/4", trainable=do_fine_tuning), | |
tf.keras.layers.Dropout(rate=0.2), | |
tf.keras.layers.Dense(train_generator.num_classes, | |
kernel_regularizer=tf.keras.regularizers.l2(0.0001)) | |
]) | |
model.build((None,)+IMAGE_SIZE+(3,)) | |
model.summary() | |
model.compile( | |
optimizer=tf.keras.optimizers.SGD(lr=0.005, momentum=0.9), | |
loss=tf.keras.losses.CategoricalCrossentropy(from_logits=True, label_smoothing=0.1), | |
metrics=['accuracy']) | |
steps_per_epoch = train_generator.samples // train_generator.batch_size | |
validation_steps = valid_generator.samples // valid_generator.batch_size | |
hist = model.fit( | |
train_generator, | |
epochs=5, steps_per_epoch=steps_per_epoch, | |
validation_data=valid_generator, | |
validation_steps=validation_steps).history |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment