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
| history = googlenet.fit(data_loader.train_batches, | |
| epochs=10, | |
| validation_data=data_loader.validation_batches) |
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
| history = vgg16.fit(data_loader.train_batches, | |
| epochs=10, | |
| validation_data=data_loader.validation_batches) |
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
| steps_per_epoch = round(data_loader.num_train_examples)//BATCH_SIZE | |
| validation_steps = 20 | |
| loss1, accuracy1 = vgg16.evaluate(data_loader.validation_batches, steps = 20) | |
| loss2, accuracy2 = googlenet.evaluate(data_loader.validation_batches, steps = 20) | |
| loss3, accuracy3 = resnet.evaluate(data_loader.validation_batches, steps = 20) | |
| print("--------VGG16---------") | |
| print("Initial loss: {:.2f}".format(loss1)) | |
| print("Initial accuracy: {:.2f}".format(accuracy1)) |
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
| base_learning_rate = 0.0001 | |
| vgg16_base.trainable = False | |
| vgg16 = Wrapper(vgg16_base) | |
| vgg16.compile(optimizer=tf.keras.optimizers.RMSprop(lr=base_learning_rate), | |
| loss='binary_crossentropy', | |
| metrics=['accuracy']) | |
| googlenet_base.trainable = False | |
| googlenet = Wrapper(googlenet_base) |
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
| vgg16_base = tf.keras.applications.VGG16(input_shape=IMG_SHAPE, include_top=False, weights='imagenet') | |
| googlenet_base = tf.keras.applications.InceptionV3(input_shape=IMG_SHAPE, include_top=False, weights='imagenet') | |
| resnet_base = tf.keras.applications.ResNet101V2(input_shape=IMG_SHAPE, include_top=False, weights='imagenet') |
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
| class Wrapper(tf.keras.Model): | |
| def __init__(self, base_model): | |
| super(Wrapper, self).__init__() | |
| self.base_model = base_model | |
| self.average_pooling_layer = tf.keras.layers.GlobalAveragePooling2D() | |
| self.output_layer = tf.keras.layers.Dense(1) | |
| def call(self, inputs): | |
| x = self.base_model(inputs) |
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
| vgg16_base = tf.keras.applications.VGG16(input_shape=IMG_SHAPE, include_top=False, weights='imagenet') | |
| googlenet_base = tf.keras.applications.InceptionV3(input_shape=IMG_SHAPE, include_top=False, weights='imagenet') | |
| resnet_base = tf.keras.applications.ResNet101V2(input_shape=IMG_SHAPE, include_top=False, weights='imagenet') |
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 numpy as np | |
| import matplotlib.pyplot as plt | |
| import tensorflow as tf | |
| import tensorflow_datasets as tfds | |
| IMG_SIZE = 160 | |
| BATCH_SIZE = 32 | |
| SHUFFLE_SIZE = 1000 | |
| IMG_SHAPE = (IMG_SIZE, IMG_SIZE, 3) |
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
| data_loader = DataLoader(IMG_SIZE, BATCH_SIZE) | |
| plt.figure(figsize=(10, 8)) | |
| i = 0 | |
| for img, label in data_loader.get_random_raw_images(20): | |
| plt.subplot(4, 5, i+1) | |
| plt.imshow(img) | |
| plt.title("{} - {}".format(data_loader.get_label_name(label), img.shape)) | |
| plt.xticks([]) | |
| plt.yticks([]) |
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
| def __init__(self, image_size, batch_size): | |
| self.image_size = image_size | |
| self.batch_size = batch_size | |
| # 80% train data, 10% validation data, 10% test data | |
| split_weights = (8, 1, 1) | |
| splits = tfds.Split.TRAIN.subsplit(weighted=split_weights) | |
| (self.train_data_raw, self.validation_data_raw, self.test_data_raw), self.metadata = tfds.load( |