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
STEP_SIZE_TEST = testing_generator.n // testing_generator.batch_size | |
testing_generator.reset() | |
predictions = malaria_model.predict_generator(testing_generator, | |
steps=STEP_SIZE_TEST, | |
verbose=1) | |
img_name = testing_generator.filenames | |
results = pd.DataFrame({'img_name': img_name, | |
'prediction': predicted_class}) | |
_this = results.img_name.str.split('\\', n=1, expand=True) | |
results['reference'] = _this[0] |
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
sess = K.get_session() | |
init = tf.global_variables_initializer() | |
sess.run(init) | |
STEP_SIZE_TRAIN = training_generator.samples // training_generator.batch_size | |
STEP_SIZE_VALID = validatation_generator.n // validatation_generator.batch_size | |
training_stats = CollectStats() | |
history = malaria_model.fit_generator(training_generator, | |
epochs=50, | |
steps_per_epoch=STEP_SIZE_TRAIN, | |
validation_data=validatation_generator, |
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
malaria_model = tf.keras.Sequential([features_extractor_layer, | |
layers.Dense(training_generator.num_classes, | |
activation='softmax') | |
]) | |
malaria_model.summary() | |
malaria_model.compile(optimizer=tf.keras.optimizers.RMSprop(lr=0.001), | |
loss='categorical_crossentropy', | |
metrics=['acc']) | |
""" |
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
training_datagen = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1./255., | |
rotation_range=30, | |
zoom_range=0.3, | |
width_shift_range=0.2, | |
height_shift_range=0.2, | |
fill_mode='nearest', | |
horizontal_flip=True) | |
validation_datagen = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1./255.) | |
testing_datagen = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1./255.) | |
training_generator = training_datagen.flow_from_directory('../data/Train/', |
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
feature_extractor_url = "https://tfhub.dev/google/imagenet/mobilenet_v2_140_224/feature_vector/3" | |
def feature_extractor(x): | |
feature_extractor_module = hub.Module(feature_extractor_url) | |
return feature_extractor_module(x) | |
IMAGE_SIZE = hub.get_expected_image_size(hub.Module(feature_extractor_url)) | |
print('Expected size : %s' % IMAGE_SIZE) | |
# Expected 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
np.random.seed(SEED) | |
tf.random.set_random_seed(SEED) | |
model = Sequential() | |
model.add(Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=(IMG_ROWS, IMG_COLS, 1))) | |
model.add(Conv2D(32, (3, 3), activation='relu')) | |
model.add(MaxPooling2D(pool_size=(2, 2))) | |
model.add(Conv2D(64, (3, 3), activation='relu')) | |
model.add(MaxPooling2D(pool_size=(2, 2))) | |
model.add(Flatten()) | |
model.add(Dense(128, 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.python import keras | |
from tensorflow.python.keras.models import Sequential | |
from tensorflow.python.keras.layers import Dense, Flatten, Conv2D, Dropout | |
from tensorflow.python.keras.layers import MaxPooling2D, BatchNormalization | |
from keras.utils.vis_utils import model_to_dot | |
from keras.utils import plot_model | |
from tensorflow.python.keras.callbacks import ModelCheckpoint | |
from tensorflow.keras.callbacks import EarlyStopping | |
from sklearn.model_selection import train_test_split |
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
cv_params = {'C': [0.001, 0.01, 0.1, 1., 10., 100.], | |
'penalty': ['l1', 'l2'], | |
'class_weight': [None, 'balanced'] | |
} | |
fix_params = {'random_state': SEED} | |
log_cv_1 = GridSearchCV(LogisticRegression(**fix_params), cv_params, scoring='f1', cv=5) | |
log_cv_1.fit(X_train, y_train) | |
log_clf_all = LogisticRegression(**{**fix_params, **log_cv_1.best_params_}) | |
_ = myUtilityFunction.prediction_evaluation(log_clf_all, X_train, X_test, y_train, y_test, | |
X_train.columns, "coefficients") |
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 sklearn.model_selection import RandomizedSearchCV | |
xgb_clf = xgboost.XGBClassifier(random_state=SEED, n_jobs=-1) | |
params = {'n_estimators': [50, 100, 200, 300], | |
'learning_rate': [0.01, 0.05, 0.1, 0.15], | |
'min_child_weight': [1, 2, 3, 5, 10], | |
'gamma': [0.1, 0.2, 0.3, 0.4, 0.5, 1], | |
'subsample': [0.6, 0.7, 0.8], | |
'colsample_bytree': [0.6, 0.7, 0.8], | |
'max_depth': [3, 4, 5], |
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
xgb_clf = xgboost.XGBClassifier(random_state=SEED, n_jobs=-1, learning_rate=0.1, | |
max_depth=3, n_estimators=100) | |
_ = myUtilityFunction.prediction_evaluation(xgb_clf, X_train, X_test, | |
y_train, y_test, X_train.columns, "features") |