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
| from keras.applications.vgg16 import VGG16 | |
| model = VGG16(weights='imagenet') | |
| from keras.preprocessing import image | |
| from keras.applications.vgg16 import preprocess_input, decode_predictions | |
| import numpy as np | |
| image_path = '/content/drive/My Drive/Image Datasets/random_pictures/sample_cat.jpeg' | |
| img = image.load_img(image_path, target_size=(224, 224)) |
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
| from keras import models | |
| from keras import layers | |
| model = models.Sequential() | |
| model.add(conv_base) | |
| model.add(layers.Flatten()) | |
| model.add(layers.Dense(256, activation='relu')) | |
| model.add(layers.Dense(1, activation='sigmoid')) |
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
| conv_base.trainable = True | |
| set_trainable = False | |
| for layer in conv_base.layers: | |
| if layer.name == 'block5_conv1': | |
| set_trainable = True | |
| if set_trainable: | |
| layer.trainable = True | |
| else: |
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
| from keras.applications import VGG16 | |
| conv_base = VGG16(weights='imagenet', | |
| include_top = False, | |
| input_shape=(150, 150, 3)) | |
| conv_base.summary() |
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
| from keras import layers | |
| from keras import models | |
| model = models.Sequential() | |
| model.add(layers.Conv2D(96, (11, 11), strides=(4, 4), activation='relu', input_shape=(224, 224, 3))) | |
| model.add(layers.MaxPooling2D((3, 3), strides=(2, 2)) | |
| model.add(layers.Conv2D(256, (5, 5), padding='same', activation='relu')) | |
| model.add(layers.MaxPooling2D((3, 3), strides=(2, 2))) | |
| model.add(layers.Conv2D(384, (3, 3), padding='same', activation='relu')) | |
| model.add(layers.Conv2D(384, (3, 3), padding='same', activation='relu')) |
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 = model.fit_generator(train_generator, steps_per_epoch=100, epochs=20, validation_data=validation_generator, validation_steps=50) | |
| acc = history.history['accuracy'][-1] | |
| val_acc = history.history['val_accuracy'][-1] | |
| loss = history.history['loss'][-1] | |
| val_loss = history.history['val_loss'][-1] | |
| print("Training accuracy: ", acc) | |
| print("Training loss: ", loss) |
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
| train_datagen = ImageDataGenerator(rescale = 1./255, | |
| rotation_range = 40, | |
| width_shift_range = 0.2, | |
| height_shift_range = 0.2, | |
| shear_range = 0.2, | |
| zoom_range = 0.2, | |
| horizontal_flip = True) | |
| val_datagen = ImageDataGenerator(rescale = 1./255) |
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
| from keras.preprocessing.image import ImageDataGenerator | |
| train_dir = os.path.join(base_dir, 'train') | |
| test_dir = os.path.join(base_dir, 'test') | |
| validation_dir = os.path.join(base_dir, 'validation') | |
| train_datagen = ImageDataGenerator(rescale=1./255) | |
| test_datagen = ImageDataGenerator(rescale=1./255) | |
| # class_mode: binary because we need binary labels (from using binary_crossentropy) |
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
| from keras import layers | |
| from keras import models | |
| model = models.Sequential() | |
| model.add(layers.Conv2D(32, (11, 11), activation='relu', input_shape=(224, 224, 3))) | |
| model.add(layers.MaxPooling2D((3, 3))) | |
| model.add(layers.Conv2D(64, (5, 5), activation='relu')) | |
| model.add(layers.MaxPooling2D((3, 3))) | |
| model.add(layers.Conv2D(128, (3, 3), activation='relu')) | |
| model.add(layers.Conv2D(256, (3, 3), activation='relu')) |
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 | |
| from tensorflow.image import ResizeMethod | |
| from keras.preprocessing import image | |
| import numpy as np | |
| from PIL import Image | |
| resized_shape = (224, 224, 3) | |
| alexnet_base_folder = '/content/drive/My Drive/Image Datasets/cats_and_dogs_alexnet' | |
| alexnet_cats_train_dir = os.path.join(alexnet_base_folder, 'train/cats') |
NewerOlder