Skip to content

Instantly share code, notes, and snippets.

View takahish's full-sized avatar

Takahiro Ishikawa takahish

View GitHub Profile
@takahish
takahish / transfer_learning_create_cnn_from_scrach_2.py
Created December 1, 2018 06:01
Training and testing Keras CNN are defined.
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
@takahish
takahish / transfer_learning_create_cnn_from_scrach_1.py
Created December 1, 2018 05:58
We will create a CNN that classifies dog breeds.
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))
@takahish
takahish / transfer_learning_detect_dogs_5.py
Created December 1, 2018 05:55
Test the performance of the dog_detector function.
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])) + '%')
@takahish
takahish / transfer_learning_detect_dogs_4.py
Created December 1, 2018 05:53
The function returns True if a dog is detected in an image (and False if not).
### 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.
@takahish
takahish / transfer_learning_detect_dogs_3.py
Last active December 1, 2018 05:48
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.
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.
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.
@takahish
takahish / transfer_learning_detect_dogs_1.py
Created December 1, 2018 05:34
This pre-trained ResNet-50 model returns a prediction for the object that is contained in the image.
from keras.applications.resnet50 import ResNet50
# define ResNet50 model
ResNet50_model = ResNet50(weights='imagenet')
@takahish
takahish / transfer_learning_detect_humans_2.py
Created December 1, 2018 05:31
Test the performance of the face_detector function.
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])) + "%")
@takahish
takahish / transfer_learning_detect_humans_1.py
Last active December 12, 2018 10:53
We can use Haar feature-based cascade classifiers to write a function that returns True if a human face is detected in an image and False otherwise.
# 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.
"""
@takahish
takahish / transfer_learning_import_datasets_2.py
Created December 1, 2018 05:23
we import a dataset of human images, where the file paths are stored in the numpy array human_files.
import random
random.seed(8675309)
# load filenames in shuffled human dataset
human_files = np.array(glob("lfw/*/*"))
random.shuffle(human_files)