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.callbacks import ModelCheckpoint | |
| def train_model(model, train_tensors, train_targets, valid_tensors, valid_targets, save_filepath, epochs=20, batch_size=20): | |
| """Fit model to train dataset, and check accuracy for valid dataset. | |
| Args: | |
| model: complied keras model | |
| train_tensors: train datatset | |
| train_targets: train targets | |
| valid_tensors: valid dataset |
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.layers import Conv2D, MaxPooling2D, GlobalAveragePooling2D | |
| from keras.layers import Dropout, Flatten, Dense | |
| from keras.models import Sequential | |
| model = Sequential() | |
| model.add(Conv2D(32, 3, padding='same', activation='relu', input_shape=(224, 224, 3))) | |
| model.add(Conv2D(32, 3, padding='same', activation='relu')) | |
| model.add(MaxPooling2D(pool_size=2)) | |
| model.add(Dropout(0.2)) |
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
| human_files_short = human_files[:100] | |
| dog_files_short = train_files[:100] | |
| print('human_files: ' + str(sum([1 for human_file in human_files_short if dog_detector(human_file) == True])) + '%') | |
| print('dog_files: ' + str(sum([1 for dog_file in dog_files_short if dog_detector(dog_file) == True])) + '%') |
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
| ### returns "True" if a dog is detected in the image stored at img_path | |
| def dog_detector(img_path): | |
| """The function returns True if a dog is detected in an image (and False if not). | |
| In order to check to see if an image is predicted to contain a dog by the | |
| pre-trained ResNet-50 model, we need only check if the ResNet50_predict_labels | |
| function above returns a value between 151 and 268 (inclusive). | |
| Args: | |
| img_path: string. a file path to a color images. | |
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.resnet50 import preprocess_input, decode_predictions | |
| def ResNet50_predict_labels(img_path): | |
| """This is accomplished with the predict method, which returns an array | |
| whose i-th entry is the model's predicted probability that the image | |
| belongs to the i-th ImageNet category. | |
| Args: | |
| img_path: string. a file path to a color images. | |
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 import image | |
| from tqdm import tqdm | |
| def path_to_tensor(img_path): | |
| """The path_to_tensor function below takes a string-valued file path to a color | |
| image as input and returns a 4D tensor suitable for supplying to a Keras CNN. | |
| Args: | |
| img_path: string. a file path to a color images. | |
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.resnet50 import ResNet50 | |
| # define ResNet50 model | |
| ResNet50_model = ResNet50(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
| human_files_short = human_files[:100] | |
| dog_files_short = train_files[:100] | |
| print("human_files: " + str(sum([1 for human_file in human_files_short if face_detector(human_file) == True])) + "%") | |
| print("dog_files: " + str(sum([1 for dog_file in dog_files_short if face_detector(dog_file) == True])) + "%") |
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
| # returns "True" if face is detected in image stored at img_path | |
| def face_detector(img_path): | |
| """Returns True if a human face is detected in an image and False otherwise. | |
| Args: | |
| img_path: string. a file path to a color images. | |
| Returns: | |
| boolean, images show a human or not. | |
| """ |
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 random | |
| random.seed(8675309) | |
| # load filenames in shuffled human dataset | |
| human_files = np.array(glob("lfw/*/*")) | |
| random.shuffle(human_files) |