Skip to content

Instantly share code, notes, and snippets.

View Tusharsharma118's full-sized avatar
💭
Learning and then some more Learning!

Tushar Sharma Tusharsharma118

💭
Learning and then some more Learning!
  • University of Southern California
  • LA, CA
View GitHub Profile
@Tusharsharma118
Tusharsharma118 / loss_visualize.py
Last active October 21, 2019 11:12
visualize losses for a model
monitor = tf.keras.callbacks.EarlyStopping(monitor = 'val_loss',patience = 8,restore_best_weights = True, min_delta = 0.01)
history = model_regularized.fit_generator(train_generator, validation_data=validation_generator,steps_per_epoch=(len(train_split_images) / 32),epochs = 52,verbose=1,callbacks=[monitor]) # 32 = batch size
# Visualization code begins
training_loss = history.history['loss']
validation_loss = history.history['val_loss']
# Create count of the number of epochs
@Tusharsharma118
Tusharsharma118 / callback_example.py
Created October 21, 2019 11:04
Early Stopping Callback
monitor = tf.keras.callbacks.EarlyStopping(monitor = 'val_loss',patience = 8,restore_best_weights = True, min_delta = 0.01)
# Using the Callback
history = model_regularized.fit_generator(train_generator, validation_data=validation_generator,steps_per_epoch=(len(train_split_images) / 32),epochs = 52,verbose=1,callbacks=[monitor])
@Tusharsharma118
Tusharsharma118 / DataGenerators.py
Created October 21, 2019 09:06
visualize using data generator
training_datagen = tf.keras.preprocessing.image.ImageDataGenerator(zoom_range=0.3,width_shift_range=0.15,height_shift_range=0.15,shear_range=0.2,fill_mode='nearest')
validation_datagen = tf.keras.preprocessing.image.ImageDataGenerator()
# try and visualize your images
sign_generator = training_datagen.flow(train_images128[110:111], training_labels[110:111],batch_size=1)
sign = [next(sign_generator) for i in range(0,10)]
fig, ax = plt.subplots(1,10, figsize=(16, 6))
print('Labels:', [item[1][0] for item in sign])
l = [ax[i].imshow(sign[i][0][0]) for i in range(0,10)]
@Tusharsharma118
Tusharsharma118 / print_images.py
Last active October 23, 2019 03:28
print a specified number of random images from the provided image list
def print_random_images(images, labels, number_of_images_to_display, color_map='hsv'):
if number_of_images_to_display % 4 == 0:
num_rows = number_of_images_to_display / 4
else:
num_rows = int(number_of_images_to_display / 4) + 1
random_indices = [np.random.randint(0, images.shape[0]) for n in range(number_of_images_to_display)]
# print(random_indices)
for counter in range(number_of_images_to_display):
index = random_indices[counter]
plt.subplot(num_rows, 4, counter + 1)
@Tusharsharma118
Tusharsharma118 / class_labels.py
Created October 18, 2019 03:45
Class labels for 60 out or 62 classes
{
0: 'Warning for a bad road surface',
1: 'Warning for a speed bump',
2: 'Warning for a slippery road surface',
3: 'Warning for a curve to the left',
4: 'Warning for a curve to the right',
5: 'Warning for a double curve, first left then right', # Merge Classes 5 & 6 later
6: 'Warning for a double curve, first left then right',
7: 'Watch out for children ahead',
8: 'Watch out for cyclists',
@Tusharsharma118
Tusharsharma118 / load_images.py
Created October 14, 2019 10:17
method for loading traffic sign images into memory
def load_images(data_directory):
# lists to store Images and labels
images = []
labels = []
log_index = 0
# get list of all directories present in the data_directory path
directories = [dir for dir in os.listdir(data_directory)
if os.path.isdir(os.path.join(data_directory,dir))] # to make sure that we include only directories and not any files present in the folder
print(len(directories))
@Tusharsharma118
Tusharsharma118 / extract.py
Last active October 14, 2019 10:08
Snippet for extracting traffic dataset
print(os.getcwd()) # returns your current working directory
DATASET_PATH = """/My Drive/Ml Datasets/Belgian Img Traffic Signs Datasets""" # Insert Data Path here relative to current working directory
os.chdir(DATASET_PATH)
print(os.getcwd()) # make sure you're in the directory containing dataset zipfiles
# Extracting Training Data
with zipfile.ZipFile('BelgiumTSC_Training.zip', 'r') as zipObj:
zipObj.extractall('TrainingData')
# Extracting Testing Data
with zipfile.ZipFile('BelgiumTSC_Testing.zip', 'r') as zipObj:
zipObj.extractall('TestingData')