def loading_embedding(self, imagepath, model, data, layer_num):
        """
        :param imagepath: path to the image folder
        :param model: model
        :param data: dataset
        :param layer_num: position of the layer starting from the end of the summary. Determine where to cut the model
        This function takes a model, cut the layer and save the embedding for each images in a dataset
        """
        model = Model(inputs=model.input, outputs=model.layers[-layer_num].output)
        list_x = []
        for img in data['files'].tolist():
            if self.model_name not in ['vgg19', 'MobileNetV2', 'vggface', 'vgg16']:
                img = image.load_img(os.path.join(imagepath, img), target_size=(160, 160))
            else:
                img = image.load_img(os.path.join(imagepath, img), target_size=(224, 224))
            x = image.img_to_array(img)
            x = np.expand_dims(x, axis=0)
            if self.model_name == 'vgg19':
                x = preprocess_input_VGG19(x)
            elif self.model_name == 'MobileNetV2':
                x = preprocess_input_MNV2(x)
            elif self.model_name == 'vgg16':
                x = preprocess_input_VGG16(x)
            elif self.model_name == 'ResNet50':
                x = Preprocess_RESNET50(x)
            else:
                x = x.astype('float32') / 255.
            list_x.append(x)
        feature_x = np.vstack(list_x)
        label = data['label'].tolist()
        feature = model.predict(feature_x)

        return feature, label
예제 #2
0
    def predict_label_multi(model, labels, imagepath, preprocess=None):

        img = tf.keras.preprocessing.image.load_img(os.path.join(imagepath), target_size=(224, 224))
        img_array = tf.keras.preprocessing.image.img_to_array(img)
        img_array = np.expand_dims(img_array, 0)  # Create batch axis

        if preprocess == 'vgg16':
            img_array = preprocess_input_VGG16(img_array)
        elif preprocess == 'vgg19':
            img_array = preprocess_input_VGG19(img_array)
        elif preprocess == 'MobileNetV2':
            img_array = preprocess_input_MNV2(img_array)
        elif preprocess == 'ResNet50':
            img_array = Preprocess_RESNET50(img_array)

        predictions = model.predict(img_array)[0]
        index = np.argmax(predictions)
        score = predictions[int(index)]
        result = list(labels.values())[int(index)]
        # imge = mpimg.imread(imagepath)
        # plt.figure(figsize=(5, 5))
        # plt.imshow(imge)
        # plt.title(list(labels.keys())[int(score)])
        # plt.xticks([])
        # plt.yticks([])
        # plt.show()
        return result, score
예제 #3
0
def read_and_prep_image(image_path, model_name, img_width=IMAGE_WIDTH, img_height=IMAGE_HEIGHT):
    image_raw = load_img(image_path, target_size=(img_height, img_width))
    image_array = np.array([img_to_array(image_raw)])
    if model_name == "ResNet50":
        image = preprocess_input_ResNet50(image_array)
    elif model_name == "VGG16":
        image = preprocess_input_VGG16(image_array)
    elif model_name == "VGG19":
        image = preprocess_input_VGG19(image_array)
    return image