Fast, Visual, and Explainable ML Modeling With PerceptiLabs

import plotly.express as px
import matplotlib.pyplot as plt
import seaborn as sns
import tensorflow as tf
train_dir = '../input/blood-cells/dataset2-master/dataset2-master/images/TRAIN'
test_dir = '../input/blood-cells/dataset2-master/dataset2-master/images/TEST'
# Create generators
train_gen = tf.keras.preprocessing.image.ImageDataGenerator(
preprocessing_function = tf.keras.applications.mobilenet_v2.preprocess_input ,
validation_split= 0.2
)
test_gen = tf.keras.preprocessing.image.ImageDataGenerator(
preprocessing_function = tf.keras.applications.mobilenet_v2.preprocess_input ,
validation_split= 0.2
)
# Flow image data
train_images = train_gen.flow_from_directory(
directory = train_dir , target_size = (224,224) , color_mode = 'rgb' ,
class_mode = 'categorical' , batch_size = 32 , shuffle= True , seed = 42,
subset = 'training'
)
val_images = train_gen.flow_from_directory(
directory = train_dir , target_size = (224,224) , color_mode = 'rgb' ,
class_mode = 'categorical' , batch_size = 32 , shuffle= True , seed = 42,
subset = 'validation'
)
test_images = test_gen.flow_from_directory(
directory = test_dir , target_size = (224,224) , color_mode = 'rgb' ,
class_mode = 'categorical' , batch_size = 32 , shuffle= False , seed = 42
)
pretrained_model = tf.keras.applications.MobileNetV2(
input_shape=(224, 224, 3),
include_top=False,
weights='imagenet',
pooling='avg'
)
pretrained_model.trainable = False
inputs = pretrained_model.input
x = tf.keras.layers.Dense(128,activation = 'relu')(pretrained_model.output)
outputs = tf.keras.layers.Dense(4,activation='softmax')(x)
model = tf.keras.Model(inputs = inputs , outputs = outputs)
model.compile(
optimizer = 'adam' ,
loss = 'categorical_crossentropy' ,
metrics = ['accuracy']
)
print(model.summary())
# Training
history = model.fit(
train_images ,
validation_data = val_images ,
epochs = 100 ,
callbacks = [
tf.keras.callbacks.EarlyStopping(
monitor = 'val_loss' ,
patience = 3,
restore_best_weights = True
)
]
)

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store