Skip to content

Instantly share code, notes, and snippets.

@MohanaRC
Last active December 29, 2016 10:19
Show Gist options
  • Save MohanaRC/103dff0edaf9f163af7918545ee0ae6b to your computer and use it in GitHub Desktop.
Save MohanaRC/103dff0edaf9f163af7918545ee0ae6b to your computer and use it in GitHub Desktop.
Pseudocode for object classification using Sparse Autoencoder and Softmax Classifier
import all the required packages
'''tagging_function : loads training images and allows the user to tag them into categories
input variables:
path_of_images : indicates the path of the input images
path_to_store_tagged_images : indicates the path where the tagged images are renamed and stored'''
define tagging_function(path_of_images, path_to_store_tagged_images):
get the names of all the images in path_of_images and store it in variable listing
open the file where the path of the images and tags will be saved and store in variable file
loop through listing:
read the image
print all the options available for tagging
resize the image
show the image
ask the user to input the key corresponding to the correct tag and store it in variable tag
if tag is category_1a:
store the image in path_to_store_tagged_images with 1a in the name
write the path and the tag in file
elif tag is category_1b:
store the image in path_to_store_tagged_images with 1b in the name
write the path and the tag in file
elif tag is category_1c:
store the image in path_to_store_tagged_images with 1c in the name
write the path and the tag in file
elif tag is category_1d:
store the image in path_to_store_tagged_images with 1d in the name
write the path and the tag in file
elif tag is category_1e:
store the image in path_to_store_tagged_images with 1e in the name
write the path and the tag in file
elif tag is category_2a:
store the image in path_to_store_tagged_images with 2a in the name
write the path and the tag in file
elif tag is category_2b:
store the image in path_to_store_tagged_images with 2b in the name
write the path and the tag in file
elif tag is category_2c:
store the image in path_to_store_tagged_images with 2c in the name
write the path and the tag in file
elif tag is category_2d:
store the image in path_to_store_tagged_images with 2d in the name
write the path and the tag in file
elif tag is category_2e:
store the image in path_to_store_tagged_images with 2e in the name
write the path and the tag in file
elif tag is category_3a:
store the image in path_to_store_tagged_images with 3a in the name
write the path and the tag in file
elif tag is category_3b:
store the image in path_to_store_tagged_images with 3b in the name
write the path and the tag in file
elif tag is category_3c:
store the image in path_to_store_tagged_images with 3c in the name
write the path and the tag in file
elif tag is category_3d:
store the image in path_to_store_tagged_images with 3d in the name
write the path and the tag in file
elif tag is category_3e:
store the image in path_to_store_tagged_images with 3e in the name
write the path and the tag in file
elif tag is a 4 (swatch):
store the image in path_to_store_tagged_images with 4 in the name
write the path and the tag in file
else:
ask the user to input a correct option
'''preprocess_and_save_as_numpy : loads the images, converts to grayscale, resizes the image, performs histogram equalization and saves the images as numpy arrays along with their tages
input variables :
tagged_file_name : name of the text file that saves the path of the images and their tags
path_for_numpy_arrays : path of folder in which images will be saved as numpy arrays along with their tags'''
define preprocess_and_save_as_numpy(tagged_file_name, path_for_numpy_arrays):
loop through each line in tagged_file_name
read the image
convert the image to grayscale
resize the crop
apply histogram equalization
save the numpy arrays along with the tags
'''createX_for_sparse_autoencoder : creates the inputs (and labels) for the sparse autoencoder from the numpy arrays
input variables :
path_to_numpy_arrays : path of the folder containing the labeled numpy arrays'''
define createX_for_sparse_autoencoder(path_to_numpy_arrays):
initialize x with zeros
initalize counter for images
loop through all the files in the folder:
load the numpy array
flatten the array
store the flattened array in x[counter]
increment counter
save x as numpy array
'''createXY_for_softmax_classifier : creates the inputs and the labels for the softmax classifier from the numpy arrays
input variables :
path_to_numpy_arrays : path of the folder containing the labeled numpy arrays'''
define createXY_for_softmax_classifier(path_to_numpy_arrays):
initialize X and Y
initialize counter for images
loop through all the files in the folder:
split the name of the image file and extract the label and save in variable label
flatten the image
store the flattened image in X[counter]
if label == 1a:
Y[counter][1]=1
elif label == 1b:
Y[counter][2]=1
elif label == 1c:
Y[counter][3]=1
elif label == 1d:
Y[counter][4]=1
elif label == 1e:
Y[counter][5]=1
elif label == 2a:
Y[counter][6]=1
elif label == 2b:
Y[counter][7]=1
elif label == 2c:
Y[counter][8]=1
elif label == 2d:
Y[counter][9]=1
elif label == 2e:
Y[counter][10]=1
elif label == 3a:
Y[counter][11]=1
elif label == 3b:
Y[counter][12]=1
elif label == 3c:
Y[counter][13]=1
elif label == 3d:
Y[counter][14]=1
elif label == 3e:
Y[counter][15]=1
elif label == 4:
Y[counter][4]=1
increment counter
'''training_function : trains the sparse autoencoder and softmax classifier models using the numpy arrays of images and labels'''
define training_function():
x=load the numpy array x
initialize an array input_data to zeros
X_labeled=load the labeled numpy array X
Y_labeled=load the labeled numpy array Y
### training starts
##### Training for Sparse Autoencoder
initialize a variable called input data to hold the product of X and weights
set the hyperparameters of the encoder and decoder:
set the activation function, regularizers of the encoder
set the activation function, regularizers of the decoder
build the sparseAutoEncoder model using the output as the decoder
set the loss function, optimizer and metrics hyperparameters of the models
set the hyperparameters: batch size, number of epochs and training/validation split
fit the model and update the weights of the model and save it
plot the loss curve of training and validation
show the curve
##### Training for Softmax Classifier
initialize W1 with weights loaded from the retrained Sparse Autoencoder
loop through X_labeled with i as counter:
input_data[i]=dot product of weight W1 with each row of X_labeled, corresponding to each image
increment i
build the softmax classifier by setting the hyperparameters:
set the classifier to sequential
add number of dense layers, input dimensions
set the fully connected layer as softmax
set compliation loss metrics, optimizer
set the number of epochs, batch size and training/validation split
train the model
update the weights of the model and save it
plot the loss curve of training and validation
show the curve
'''perform_training : calls all the necessary functions to finish the training process
Input variables :
path_of_images : path to the folder containing raw training images
path_to_store_tagged_images : path to the folder where tagged images are stored
tagged_file_name : name of the file which contains path of the tagged images and their tags
path_to_numpy arrays : path of the folder containing the preprocessed numpy arrays'''
define perform_training(path_of_images, path_to_store_tagged_images, tagged_file_name, path_to_numpy_arrays):
tagging_function(path_of_images, path_to_store_tagged_images)
preprocess_and_save_as_numpy(tagged_file_name, path_to_numpy_arrays)
createX_for_sparse_autoencoder(path_to_numpy_arrays)
createXY_for_softmax_classifier(path_to_numpy_arrays)
training_function()
'''preprocess_frames : performs preprocessing on each of the frames
input variables :
image : image frame on which the manipulations have to be performed
output variables :
flattened_image : image post all the preprocessing'''
define preprocess_frames(image):
save a copy of the image in image_copy
convert image_copy to grayscale and save in gray_copy
resize gray_copy
perform histogram equalization on gray_copy
flatten gray_copy as save as flattened_image
return flattened_image
############################################# MAIN FUNCTION #######################################################
'''main_function : takes an input of S images and categorizes the image as category 1, 2, 3 or swatch'''
define main_function():
call perform_training(path_of_images, path_to_store_tagged_images, tagged_file_name, path_to_numpy_arrays)
path=path to the folder containing images S
load weights of sparse autoencoder
initialize variable input_img
initialize the encoder and it's hyperparameters (activation, regularizers)
initialize the decoder and it's hyperparameters (activation, regularizers)
set the model input as variable input_img and output as decoder
initialize the loss function, optimizers
load the weights and hidden weights of the sparse autoencoder
initialize the loss function, optimizer, input dimensions of the CNN and set the fully connect layer as softmax
loop through all the files in folder location specified in path:
read the image
preprocess each frame by calling preprocess_frames(image) and save the preprocessed frame as preprocess
find the dot product of the preprocessed frames and hidden layers of sparse autoencoder and save it as classifier_input
predict the class of the image by calling the softmax classifier with classifier_input as input parameters save it as output
find the maximum value of ouput and print the corresponding class
if maximum value is for category 1:
the object belongs to category 1a
elif maximum value is for category 2:
the object belongs to category 1b
elif maximum value is for category 3:
the object belongs to category 1c
elif maximum value is for category 4:
the object belongs to category 1d
elif maximum value is for category 5:
the object belongs to category 1e
elif maximum value is for category 6:
the object belongs to category 2a
elif maximum value is for category 7:
the object belongs to category 2b
elif maximum value is for category 8:
the object belongs to category 2c
elif maximum value is for category 9:
the object belongs to category 2d
elif maximum value is for category 10:
the object belongs to category 2e
elif maximum value is for category 11:
the object belongs to category 3a
elif maximum value is for category 12:
the object belongs to category 3b
elif maximum value is for category 13:
the object belongs to category 3c
elif maximum value is for category 14:
the object belongs to category 3d
elif maximum value is for category 15:
the object belongs to category 3e
elif maximum value is for category 16:
the object is a swatch
delete the swatch
if object is not a swatch:
store the probability values in a numpy array called probability_array, where each row corresponds to an image
find the unique columns in probability array
initialize duplicate_list
for every unique column:
find indices of probability array where column matches the unique column
find the count of the indices
duplicate_list.append(count of indices-1) ### Since the first exact match is the original image
print total number of duplicates as sum of duplicate_list
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment