import matplotlib.pylab as plt
import tensorflow as tf
import tensorflow_hub as hub
import os
import numpy as np
import tensorflow_datasets as tfds
tf.__version__
'2.2.0'
datasets, info = tfds.load(name='rock_paper_scissors', with_info=True, as_supervised=True, split=['train','test'])
info
tfds.core.DatasetInfo( name='rock_paper_scissors', version=3.0.0, description='Images of hands playing rock, paper, scissor game.', homepage='http://laurencemoroney.com/rock-paper-scissors-dataset', features=FeaturesDict({ 'image': Image(shape=(300, 300, 3), dtype=tf.uint8), 'label': ClassLabel(shape=(), dtype=tf.int64, num_classes=3), }), total_num_examples=2892, splits={ 'test': 372, 'train': 2520, }, supervised_keys=('image', 'label'), citation="""@ONLINE {rps, author = "Laurence Moroney", title = "Rock, Paper, Scissors Dataset", month = "feb", year = "2019", url = "http://laurencemoroney.com/rock-paper-scissors-dataset" }""", redistribution_info=, )
train, info_train = tfds.load(name='rock_paper_scissors', with_info=True, split='test')
tfds.show_examples(info_train,train)
dataset=datasets[0].concatenate(datasets[1])
dataset=dataset.shuffle(3000)
rsp_val=dataset.take(600)
rsp_test_temp=dataset.skip(600)
rsp_test=rsp_test_temp.take(400)
rsp_train=rsp_test_temp.skip(400)
len(list(rsp_train))
1892
def scale(image, label):
image = tf.cast(image, tf.float32)
image /= 255.0
return tf.image.resize(image,[224,224]), tf.one_hot(label, 3)
def get_dataset(batch_size=64):
train_dataset_scaled = rsp_train.map(scale).shuffle(1900).batch(batch_size)
test_dataset_scaled = rsp_test.map(scale).batch(batch_size)
val_dataset_scaled = rsp_val.map(scale).batch(batch_size)
return train_dataset_scaled, test_dataset_scaled, val_dataset_scaled
train_dataset, test_dataset, val_dataset = get_dataset()
train_dataset.cache()
val_dataset.cache()
<DatasetV1Adapter shapes: ((None, 224, 224, 3), (None, 3)), types: (tf.float32, tf.float32)>
feature_extractor = "https://tfhub.dev/google/tf2-preview/mobilenet_v2/feature_vector/4"
feature_extractor_layer = hub.KerasLayer(feature_extractor, input_shape=(224,224,3))
feature_extractor_layer.trainable = False
model = tf.keras.Sequential([
feature_extractor_layer,
tf.keras.layers.Dropout(0.5),
tf.keras.layers.Dense(3,activation='softmax')
])
model.summary()
Model: "sequential" _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= keras_layer (KerasLayer) (None, 1280) 2257984 _________________________________________________________________ dropout (Dropout) (None, 1280) 0 _________________________________________________________________ dense (Dense) (None, 3) 3843 ================================================================= Total params: 2,261,827 Trainable params: 3,843 Non-trainable params: 2,257,984 _________________________________________________________________
model.compile(
optimizer=tf.keras.optimizers.Adam(),
loss=tf.keras.losses.CategoricalCrossentropy(from_logits=True),
metrics=['acc'])
class CollectBatchStats(tf.keras.callbacks.Callback):
def __init__(self):
self.batch_losses = []
self.batch_acc = []
def on_train_batch_end(self, batch, logs=None):
self.batch_losses.append(logs['loss'])
self.batch_acc.append(logs['acc'])
self.model.reset_metrics()
train_dataset
<DatasetV1Adapter shapes: ((None, 224, 224, 3), (None, 3)), types: (tf.float32, tf.float32)>
batch_stats_callback = CollectBatchStats()
history = model.fit_generator(train_dataset, epochs=2, validation_data=val_dataset,
callbacks = [batch_stats_callback])
WARNING:tensorflow:From <ipython-input-20-e8710c26e6fb>:4: Model.fit_generator (from tensorflow.python.keras.engine.training) is deprecated and will be removed in a future version. Instructions for updating: Please use Model.fit, which supports generators.
WARNING:tensorflow:From <ipython-input-20-e8710c26e6fb>:4: Model.fit_generator (from tensorflow.python.keras.engine.training) is deprecated and will be removed in a future version. Instructions for updating: Please use Model.fit, which supports generators.
Epoch 1/2 30/30 [==============================] - 10s 335ms/step - loss: 0.7599 - acc: 0.8333 - val_loss: 0.6874 - val_acc: 0.9683 Epoch 2/2 30/30 [==============================] - 9s 303ms/step - loss: 0.6159 - acc: 0.9722 - val_loss: 0.6092 - val_acc: 0.9833
plt.figure()
plt.ylabel("Loss")
plt.xlabel("Training Steps")
plt.ylim([0,2])
plt.plot(batch_stats_callback.batch_losses)
[<matplotlib.lines.Line2D at 0x7fddc13a3eb8>]
plt.figure()
plt.ylabel("Accuracy")
plt.xlabel("Training Steps")
plt.ylim([0,1])
plt.plot(batch_stats_callback.batch_acc)
[<matplotlib.lines.Line2D at 0x7fddcc41af60>]
result=model.evaluate(test_dataset)
7/7 [==============================] - 1s 88ms/step - loss: 0.6086 - acc: 0.9850
for test_sample in rsp_test.take(10):
image, label = test_sample[0], test_sample[1]
image_scaled, label_arr= scale(test_sample[0], test_sample[1])
image_scaled = np.expand_dims(image_scaled, axis=0)
img = tf.keras.preprocessing.image.img_to_array(image)
pred=model.predict(image_scaled)
print(pred)
plt.figure()
plt.imshow(image)
plt.show()
print("Actual Label: %s" % info.features["label"].names[label.numpy()])
print("Predicted Label: %s" % info.features["label"].names[np.argmax(pred)])
[[0.00237107 0.05616644 0.9414625 ]]
Actual Label: scissors Predicted Label: scissors [[9.2693794e-05 1.5490291e-03 9.9835831e-01]]
Actual Label: scissors Predicted Label: scissors [[0.00482513 0.16221532 0.83295953]]
Actual Label: scissors Predicted Label: scissors [[0.9920209 0.00575495 0.00222414]]
Actual Label: rock Predicted Label: rock [[0.992407 0.00276275 0.00483025]]
Actual Label: rock Predicted Label: rock [[0.0053984 0.44131666 0.5532849 ]]
Actual Label: scissors Predicted Label: scissors [[0.28744406 0.4744516 0.2381043 ]]
Actual Label: paper Predicted Label: paper [[0.32515365 0.3686446 0.30620176]]
Actual Label: paper Predicted Label: paper [[0.0038298 0.9901822 0.00598796]]
Actual Label: paper Predicted Label: paper [[0.00279038 0.13178374 0.8654259 ]]
Actual Label: scissors Predicted Label: scissors
np.argmax(model.predict(test_dataset),axis=1)
array([1, 1, 0, 1, 1, 2, 0, 0, 1, 0, 0, 2, 0, 2, 1, 1, 2, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 2, 2, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 2, 0, 2, 1, 2, 0, 0, 1, 1, 1, 2, 1, 0, 1, 0, 2, 2, 2, 2, 2, 1, 2, 0, 2, 2, 0, 1, 2, 0, 0, 2, 2, 2, 0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np.argmax(model.predict(f0),axis=1)
print(tf.math.confusion_matrix(labels=y, predictions=y_pred, num_classes=3))
tf.Tensor( [[121 3 0] [ 2 151 1] [ 0 0 122]], shape=(3, 3), dtype=int32)
model.save('./models/', save_format='tf')
WARNING:tensorflow:From /usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/resource_variable_ops.py:1817: calling BaseResourceVariable.__init__ (from tensorflow.python.ops.resource_variable_ops) with constraint is deprecated and will be removed in a future version. Instructions for updating: If using Keras pass *_constraint arguments to layers.
WARNING:tensorflow:From /usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/resource_variable_ops.py:1817: calling BaseResourceVariable.__init__ (from tensorflow.python.ops.resource_variable_ops) with constraint is deprecated and will be removed in a future version. Instructions for updating: If using Keras pass *_constraint arguments to layers.
INFO:tensorflow:Assets written to: ./models/assets
INFO:tensorflow:Assets written to: ./models/assets
!ls -alrt
total 20 drwxr-xr-x 1 root root 4096 May 13 16:29 sample_data drwxr-xr-x 1 root root 4096 May 20 16:14 .config drwxr-xr-x 1 root root 4096 May 23 22:09 .. drwxr-xr-x 1 root root 4096 May 23 22:13 . drwxr-xr-x 4 root root 4096 May 23 22:37 models
ls models
assets/ saved_model.pb variables/
ls -alrt models
total 2280 drwxr-xr-x 1 root root 4096 May 23 22:13 ../ drwxr-xr-x 2 root root 4096 May 23 22:13 assets/ drwxr-xr-x 2 root root 4096 May 23 22:37 variables/ -rw-r--r-- 1 root root 2314339 May 23 22:37 saved_model.pb drwxr-xr-x 4 root root 4096 May 23 22:37 ./
loaded_model = tf.keras.models.load_model('models')
for test_sample in rsp_test.take(10):
image, label = test_sample[0], test_sample[1]
image_scaled, label_arr= scale(test_sample[0], test_sample[1])
image_scaled = np.expand_dims(image_scaled, axis=0)
img = tf.keras.preprocessing.image.img_to_array(image)
pred=loaded_model.predict(image_scaled)
print(pred)
plt.figure()
plt.imshow(image)
plt.show()
print("Actual Label: %s" % info.features["label"].names[label.numpy()])
print("Predicted Label: %s" % info.features["label"].names[np.argmax(pred)])
[[0.00293518 0.02391643 0.97314835]]
Actual Label: scissors Predicted Label: scissors [[0.9623716 0.03483324 0.00279528]]
Actual Label: rock Predicted Label: rock [[0.06006305 0.9356905 0.00424642]]
Actual Label: paper Predicted Label: paper [[6.5244210e-05 9.9975747e-01 1.7734876e-04]]
Actual Label: paper Predicted Label: paper [[0.00393865 0.98000485 0.01605651]]
Actual Label: paper Predicted Label: paper [[7.4422051e-04 1.1825132e-02 9.8743057e-01]]
Actual Label: scissors Predicted Label: scissors [[0.01841751 0.16531426 0.8162682 ]]
Actual Label: scissors Predicted Label: scissors [[0.01066127 0.17194685 0.8173918 ]]
Actual Label: scissors Predicted Label: scissors [[0.92910755 0.06245857 0.00843395]]
Actual Label: rock Predicted Label: rock [[0.3728172 0.6210693 0.00611354]]
Actual Label: rock Predicted Label: paper