Extracting Feature Vectors using VGG16

In this new exciting blog, I’m gonna help you to extract the features vectors from images and use that features vectors to build a Random Forest (RF) model.

So, we are going to use the VGG16 model ( you are free to use any pre-trained models based on your problem statement ) to extract the feature vectors of images.

The extraction part begins with specifying the directory of images and using VGG16 model to predict the feature vectors and appending the feature vectors in to the list.

img_path = r'E:\Thesis\Try1\green'
feature_green = []

for each in os.listdir(img_path):
    path = os.path.join(img_path,each)
    img = image.load_img(path, target_size=(224, 224))
    img_data = image.img_to_array(img)
    img_data = np.expand_dims(img_data, axis=0)
    img_data = preprocess_input(img_data)
    feature = model.predict(img_data)

Since we have totally 3 classes of images we need to repeat this for all the three classes and write them into a dataframe along with their labels.

After that, it’s as usual to train and test the data and build the random forest model and evaluate its accuracy.

Cheers 🙂

One Reply to “Extracting Feature Vectors using VGG16”

Leave a Reply to Sarah Cancel reply

Your email address will not be published. Required fields are marked *