Last active
July 23, 2020 06:44
-
-
Save akash-ch2812/d70ec2cc911750696f545dac88c0873e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tensorflow as tf | |
from keras.preprocessing import image | |
import numpy as np | |
# function to extract features from image | |
def extract_image_features(): | |
model = tf.keras.models.Sequential() | |
# adding first layers of convolution and pooling layers to network | |
model.add(tf.keras.layers.Conv2D(filters=64, kernel_size=(3,3), input_shape=(90,90,3), padding="VALID", activation="relu")) | |
model.add(tf.keras.layers.Conv2D(filters=64, kernel_size=(3,3), activation="relu")) | |
model.add(tf.keras.layers.MaxPool2D(pool_size=2, strides=2)) | |
# adding second layers of convolution and pooling layers to network | |
model.add(tf.keras.layers.Conv2D(filters=32, kernel_size=(3,3), padding="VALID", activation="relu")) | |
model.add(tf.keras.layers.Conv2D(filters=32, kernel_size=(3,3), activation="relu")) | |
model.add(tf.keras.layers.AveragePooling2D(pool_size=2, strides=1)) | |
# flattening the output using flatten layer, since the input to neural net has to be flat | |
model.add(tf.keras.layers.Flatten()) | |
# model summary | |
model.summary() | |
return model | |
for file in os.listdir(image_path): | |
path = image_path + "//" + file | |
img = image.load_img(path, target_size=(90, 90)) | |
img_data = image.img_to_array(img) | |
img_data = np.expand_dims(img_data, axis=0) | |
img_data = preprocess_input(img_data) | |
feature = extract_image_features.predict(img_data) | |
feature = np.reshape(feature, feature.shape[1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment