Skip to content

Instantly share code, notes, and snippets.

View takahish's full-sized avatar

Takahiro Ishikawa takahish

View GitHub Profile
from keras.wrappers.scikit_learn import KerasClassifier
from sklearn.metrics import f1_score, make_scorer
from sklearn.model_selection import GridSearchCV
from keras.models import Sequential
from keras.layers import GlobalAveragePooling2D
from keras.layers import Dense
from keras.optimizers import RMSprop
def build_model(learning_rate):
Resnet50_model = Sequential()
from keras import backend as K
def recall(y_true, y_pred):
"""Recall for each labels.
Args:
y_ture: 1d array-like, or label indicator array.
y_pred: 1d array-like, or label indicator array.
Return:
import cv2
import numpy as np
from matplotlib import pyplot as plt
def intensity_hist(image_file):
"""show some descriptive stats of some of the most relevant aspects in the dataset.
Such as the min, max, median, mean of the image sizes.
Args:
image_files: string. Path of image file.
import cv2
import pandas as pd
def stats(image_files):
"""show some descriptive stats of some of the most relevant aspects in the dataset.
Such as the min, max, median, mean of the image sizes.
Args:
image_files: sequence of string. Paths of image files.
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
plt.figure(figsize=(24, 4))
plt.subplots_adjust(hspace=0.4)
ax1 = plt.subplot(1, 1, 1)
sns.countplot(x=(np.where(train_targets == 1))[1], ax=ax1)
@takahish
takahish / transfer_learning_test_my_algorithm_1.py
Created December 2, 2018 23:41
We write my algorithm all in one.
def Resnet50_predict_breed(img_path):
"""Return the dog breed that is predicted by the transer learing model.
Args:
img_path: string. a file path to a color images.
Returns:
dog breed: string, and probability: float.
"""
bottleneck_feature = extract_Resnet50(path_to_tensor(img_path))
@takahish
takahish / transfer_learning_create_cnn_transfer_learning_3.py
Created December 2, 2018 23:32
Our dog breed classification model.
Resnet50_model = Sequential()
Resnet50_model.add(GlobalAveragePooling2D(input_shape=train_Resnet50.shape[1:]))
Resnet50_model.add(Dense(133, activation='softmax'))
train_Resnet50 = extract_Resnet50(paths_to_tensor(train_files).astype('float32'))
valid_Resnet50 = extract_Resnet50(paths_to_tensor(valid_files).astype('float32'))
test_Resnet50 = extract_Resnet50(paths_to_tensor(test_files).astype('float32'))
@takahish
takahish / transfer_learning_create_cnn_transfer_learning_1.py
Created December 1, 2018 06:27
Extracting bottleneck features of ResNet-50 to use as input for our classifying dog breed model is defined.
from keras.applications.resnet50 import ResNet50, preprocess_input
def extract_Resnet50(tensor):
"""Extracting bottleneck features of ResNet-50 to use as input our classifying model.
Args:
tensor: numpy.array. a 4D tensor suitable for supplying to a Keras CNN.
Retruns:
numpy.array, bottleneck features of ResNet-50
@takahish
takahish / transfer_learning_create_cnn_from_scrach_3.py
Last active December 11, 2018 14:41
To comilie Keras model, we execute compile method.
model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=[f1])