This file contains 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
# get some sample data | |
sample_test_data = test_images[580:590] | |
sample_test_labels = test_labels[580:590] | |
# pre-process data | |
IMG_DIMS = (32, 32) | |
sample_test_data_processed = (np.array([resize_image_array(img, | |
img_size_dims=IMG_DIMS) | |
for img in np.stack([sample_test_data]*3, | |
axis=-1)])) / 255. |
This file contains 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
# get some sample data | |
sample_test_data = test_images[580:590] | |
sample_test_labels = test_labels[580:590] | |
# pre-process data | |
sample_test_data_processed = np.expand_dims(sample_test_data / 255., axis=3) | |
# create payload | |
data = json.dumps({"signature_name": "serving_default", | |
"instances": sample_test_data_processed.tolist()}) |
This file contains 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
%%bash --bg | |
nohup /usr/bin/tensorflow_model_server \ | |
--rest_api_port=8501 \ | |
--model_config_file="/home/jupyter/tensorflow_serving/models.conf" >server.log 2>&1 | |
# checking the logs | |
!tail server.log |
This file contains 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
# saving model 1 | |
model1 = create_cnn_architecture_model1(input_shape=INPUT_SHAPE) | |
model1.load_weights('./model_weights/cnn_model1_wt.h5') | |
export_path = './tf_saved_models/1' | |
tf.saved_model.simple_save( | |
keras.backend.get_session(), | |
export_path, | |
inputs={'input_image': model1.input}, | |
outputs={t.name:t for t in model1.outputs}) |
This file contains 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
# save model | |
if not os.path.isdir('model_weights/'): | |
os.mkdir('model_weights/') | |
model2.save_weights(filepath='model_weights/cnn_model2_wt.h5', overwrite=True) | |
# load model (can be used in the future as needed once trained) | |
model2 = create_cnn_architecture_model2(input_shape=INPUT_SHAPE_RN) | |
model2.load_weights('model_weights/cnn_model2_wt.h5') | |
# predict and evaluate on test dataset |
This file contains 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
EPOCHS = 10 | |
train_images_3ch_scaled = train_images_3ch / 255. | |
model2.fit(train_images_3ch_scaled, train_labels, validation_split=0.1, | |
epochs=EPOCHS) | |
# Output | |
Train on 54000 samples, validate on 6000 samples | |
Epoch 1/10 | |
54000/54000 [====] - 100s 2ms/sample - loss: 0.5434 - acc: 0.8299 - val_loss: 0.3318 - val_acc: 0.9043 | |
Epoch 2/10 |
This file contains 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 cv2 | |
def resize_image_array(img, img_size_dims): | |
img = cv2.resize(img, dsize=img_size_dims, | |
interpolation=cv2.INTER_CUBIC) | |
img = np.array(img, dtype=np.float32) | |
return img | |
# convert single channel images to 3-channel images | |
train_images_3ch = np.stack([train_images]*3, axis=-1) |
This file contains 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
INPUT_SHAPE_RN = (32, 32, 3) | |
def create_cnn_architecture_model2(input_shape): | |
inc_net = keras.applications.resnet50.ResNet50(include_top=False, weights='imagenet', | |
input_shape=input_shape) | |
inc_net.trainable = True | |
# Fine-tune the layers | |
for layer in inc_net.layers: | |
layer.trainable = True | |
This file contains 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 os | |
from sklearn.metrics import confusion_matrix, classification_report | |
import pandas as pd | |
# save model | |
if not os.path.isdir('model_weights/'): | |
os.mkdir('model_weights/') | |
model.save_weights(filepath='model_weights/cnn_model1_wt.h5', overwrite=True) | |
# load model (can be used in the future as needed once trained) |
This file contains 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
EPOCHS = 10 | |
train_images_scaled = train_images_gr / 255. | |
model.fit(train_images_scaled, train_labels, validation_split=0.1, epochs=EPOCHS) | |
# Output | |
Train on 54000 samples, validate on 6000 samples | |
Epoch 1/10 | |
54000/54000 [====] - 7s 122us/sample - loss: 0.4614 - acc: 0.8323 - val_loss: 0.3462 - val_acc: 0.8725 | |
Epoch 2/10 | |
54000/54000 [====] - 5s 86us/sample - loss: 0.3073 - acc: 0.8892 - val_loss: 0.2825 - val_acc: 0.8950 |