Skip to content

Commit

Permalink
trained Model and added requirements text file
Browse files Browse the repository at this point in the history
  • Loading branch information
namannarula committed May 20, 2021
1 parent 460bb0c commit 5036586
Showing 1 changed file with 237 additions and 0 deletions.
237 changes: 237 additions & 0 deletions RockPaperScissors.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import tensorflow as tf
import tensorflow_datasets as tfds
import matplotlib.pyplot as plt
import numpy as np
Expand Down Expand Up @@ -31,5 +32,241 @@ def preview_dataset(dataset):

preview_dataset(dataset_train_raw)

#Defining the number of TRAIN, TEST & labels.
NUM_TRAIN_EXAMPLES = dataset_info.splits['train'].num_examples
NUM_TEST_EXAMPLES = dataset_info.splits['test'].num_examples
NUM_CLASSES = dataset_info.features['label'].num_classes

#Defining the reduced image size before pre-processing
INPUT_IMG_SIZE_ORIGINAL = dataset_info.features['image'].shape[0]
INPUT_IMG_SHAPE_ORIGINAL = dataset_info.features['image'].shape

INPUT_IMG_SIZE_REDUCED = INPUT_IMG_SIZE_ORIGINAL // 2
INPUT_IMG_SHAPE_REDUCED = (
INPUT_IMG_SIZE_REDUCED,
INPUT_IMG_SIZE_REDUCED,
INPUT_IMG_SHAPE_ORIGINAL[2]
)

INPUT_IMG_SIZE = INPUT_IMG_SIZE_REDUCED
INPUT_IMG_SHAPE = INPUT_IMG_SHAPE_REDUCED

#Pre-processing the dataset
def format_example(image, label):
# Make image color values to be float.
image = tf.cast(image, tf.float32)
# Make image color values to be in [0..1] range.
image = image / 255.
# Make sure that image has a right size
image = tf.image.resize(image, [INPUT_IMG_SIZE, INPUT_IMG_SIZE])
return image, label

dataset_train = dataset_train_raw.map(format_example)
dataset_test = dataset_test_raw.map(format_example)

#Augmentation of data to prevent overfitting
def augment_flip(image: tf.Tensor) -> tf.Tensor:
image = tf.image.random_flip_left_right(image)
image = tf.image.random_flip_up_down(image)
return image

def augment_color(image: tf.Tensor) -> tf.Tensor:
image = tf.image.random_hue(image, max_delta=0.08)
image = tf.image.random_saturation(image, lower=0.7, upper=1.3)
image = tf.image.random_brightness(image, 0.05)
image = tf.image.random_contrast(image, lower=0.8, upper=1)
image = tf.clip_by_value(image, clip_value_min=0, clip_value_max=1)
return image

def augment_rotation(image: tf.Tensor) -> tf.Tensor:
# Rotate 0, 90, 180, 270 degrees
return tf.image.rot90(
image,
tf.random.uniform(shape=[], minval=0, maxval=4, dtype=tf.int32)
)

def augment_inversion(image: tf.Tensor) -> tf.Tensor:
random = tf.random.uniform(shape=[], minval=0, maxval=1)
if random > 0.5:
image = tf.math.multiply(image, -1)
image = tf.math.add(image, 1)
return image

def augment_zoom(image: tf.Tensor, min_zoom=0.8, max_zoom=1.0) -> tf.Tensor:
image_width, image_height, image_colors = image.shape
crop_size = (image_width, image_height)

# Generate crop settings, ranging from a 1% to 20% crop.
scales = list(np.arange(min_zoom, max_zoom, 0.01))
boxes = np.zeros((len(scales), 4))

for i, scale in enumerate(scales):
x1 = y1 = 0.5 - (0.5 * scale)
x2 = y2 = 0.5 + (0.5 * scale)
boxes[i] = [x1, y1, x2, y2]

def random_crop(img):
# Create different crops for an image
crops = tf.image.crop_and_resize(
[img],
boxes=boxes,
box_indices=np.zeros(len(scales)),
crop_size=crop_size
)
# Return a random crop
return crops[tf.random.uniform(shape=[], minval=0, maxval=len(scales), dtype=tf.int32)]

choice = tf.random.uniform(shape=[], minval=0., maxval=1., dtype=tf.float32)

# Only apply cropping 50% of the time
return tf.cond(choice < 0.5, lambda: image, lambda: random_crop(image))

def augment_data(image, label):
image = augment_flip(image)
image = augment_color(image)
image = augment_rotation(image)
image = augment_zoom(image)
image = augment_inversion(image)
return image, label

dataset_train_augmented = dataset_train.map(augment_data)

#Previewing the augmented datase
preview_dataset(dataset_train_augmented)


#Shuffling dataset to prevent learning anything from the order of the dataset
BATCH_SIZE = 32
dataset_train_augmented_shuffled = dataset_train_augmented.shuffle(
buffer_size=NUM_TRAIN_EXAMPLES
)
dataset_train_augmented_shuffled = dataset_train_augmented.batch(
batch_size=BATCH_SIZE
)
# Prefetch will enable the input pipeline to asynchronously fetch batches while your model is training.
dataset_train_augmented_shuffled = dataset_train_augmented_shuffled.prefetch(
buffer_size=tf.data.experimental.AUTOTUNE
)
dataset_test_shuffled = dataset_test.batch(BATCH_SIZE)

# Debugging the batches using conversion to Numpy arrays.
batches = tfds.as_numpy(dataset_train_augmented_shuffled)
for batch in batches:
image_batch, label_batch = batch
print('Label batch shape:', label_batch.shape, '\n')
print('Image batch shape:', image_batch.shape, '\n')
print('Label batch:', label_batch, '\n')

for batch_item_index in range(len(image_batch)):
print('First batch image:', image_batch[batch_item_index], '\n')
plt.imshow(image_batch[batch_item_index])
plt.show()
# Break to shorten the output.
break
# Break to shorten the output.
break

# ----- Creating the model -----
model = tf.keras.models.Sequential()

# First convolution.
model.add(tf.keras.layers.Convolution2D(
input_shape=INPUT_IMG_SHAPE,
filters=64,
kernel_size=3,
activation=tf.keras.activations.relu
))
model.add(tf.keras.layers.MaxPooling2D(
pool_size=(2, 2),
strides=(2, 2)
))

# Second convolution.
model.add(tf.keras.layers.Convolution2D(
filters=64,
kernel_size=3,
activation=tf.keras.activations.relu
))
model.add(tf.keras.layers.MaxPooling2D(
pool_size=(2, 2),
strides=(2, 2)
))

# Third convolution.
model.add(tf.keras.layers.Convolution2D(
filters=128,
kernel_size=3,
activation=tf.keras.activations.relu
))
model.add(tf.keras.layers.MaxPooling2D(
pool_size=(2, 2),
strides=(2, 2)
))

# Fourth convolution.
model.add(tf.keras.layers.Convolution2D(
filters=128,
kernel_size=3,
activation=tf.keras.activations.relu
))
model.add(tf.keras.layers.MaxPooling2D(
pool_size=(2, 2),
strides=(2, 2)
))

# Flatten the results to feed into dense layers.
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dropout(0.5))

# 512 neuron dense layer.
model.add(tf.keras.layers.Dense(
units=512,
activation=tf.keras.activations.relu
))

# Output layer.
model.add(tf.keras.layers.Dense(
units=NUM_CLASSES,
activation=tf.keras.activations.softmax
))

model.summary()

# ----- compiling the model -----
rmsprop_optimizer = tf.keras.optimizers.RMSprop(learning_rate=0.001)

model.compile(
optimizer=rmsprop_optimizer,
loss=tf.keras.losses.sparse_categorical_crossentropy,
metrics=['accuracy']
)

# ----- Training the model -----
steps_per_epoch = NUM_TRAIN_EXAMPLES // BATCH_SIZE
validation_steps = NUM_TEST_EXAMPLES // BATCH_SIZE

print('steps_per_epoch:', steps_per_epoch)
print('validation_steps:', validation_steps)

#Defining Callbacks

DESIRED_ACCURACY = 0.90
class myCallback(tf.keras.callbacks.Callback):
def on_epoch_end(self, epoch, logs={}):
if(logs.get('accuracy') is not None and logs.get('accuracy') >= DESIRED_ACCURACY):
print("\nReached 90% accuracy so cancelling training!!")
self.model.stop_training = True


callbacks = myCallback()

training_history = model.fit(
x=dataset_train_augmented_shuffled.repeat(),
validation_data=dataset_test_shuffled.repeat(),
epochs=15,
steps_per_epoch=steps_per_epoch,
validation_steps=validation_steps,
verbose=1,
callbacks=[callbacks]
)

0 comments on commit 5036586

Please sign in to comment.