예제 #1
0
def predict(model, img):
    x = img_to_array(img)
    x = np.expand_dims(x, axis=0)
    x = preprocess_input(x)
    preds = model.predict(x)
    prediction_index = np.argmax(preds[0])
    return CATEGORIES[prediction_index]
def read_and_prep_images(img_paths,
                         img_height=image_size,
                         img_width=image_size):
    imgs = [
        load_img(img_path, target_size=(img_height, img_width))
        for img_path in img_paths
    ]
    img_array = np.array([img_to_array(img) for img in imgs])
    output = preprocess_input(img_array)
    return (output)
예제 #3
0
    def preprocess_image(path):
        '''Process an image to numpy array.

        Args:
            path: the path of the image.

        Returns:
            Numpy array of the image.
        '''
        img = process_image.load_img(path, target_size=(224, 224))
        x = process_image.img_to_array(img)
        # x = np.expand_dims(x, axis=0)
        x = preprocess_input(x)
        return x
예제 #4
0
def predict(image1):
    model = MobileNet()
    image = load_img(image1, target_size=(224, 224))
    # convert the image pixels to a numpy array
    image = img_to_array(image)
    # reshape data for the model
    image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2]))
    # prepare the image for the VGG model
    image = preprocess_input(image)
    # predict the probability across all output classes
    yhat = model.predict(image)
    # convert the probabilities to class labels
    label = decode_predictions(yhat)
    # retrieve the most likely result, e.g. highest probability
    label = label[0][0]
    return label
예제 #5
0
def predict_image(model, path: str, temperature_scaling: float = 1.0) -> str:
    source_img = imread(path)
    img = preprocess_input(make_image_square(source_img))
    logit_model = get_logits_friendly_model(model)
    logits = logit_model.predict(img.reshape((1, HEIGHT, WIDTH, 3)))
    calibrated_logits = logits / temperature_scaling
    prediction = softmax(calibrated_logits).numpy()
    prediction_list = prediction.tolist()[0]
    max_index = np.argmax(prediction)
    second_max_index = prediction_list.index(
        max(prediction_list[:max_index] + prediction_list[max_index + 1:]))
    trash = return_trash_label(max_index)
    print(CLASSES[max_index] + ": " +
          str(round(max(prediction_list) * 100, 2)) + "%")
    print(CLASSES[second_max_index] + ": " +
          str(round(prediction_list[second_max_index] * 100, 2)) + "%")
    return WASTE_TYPE[trash]
def preprocess_input(x):
    # return imagenet_utils.preprocess_input(x, mode='tf')
    return preprocess_input(x)
예제 #7
0
def prepare_image(file):
    img = image.load_img(file, target_size=(224, 224))
    img_array = image.img_to_array(img)
    img_array_expanded_dims = np.expand_dims(img_array, axis=0)
    return mobilenet.preprocess_input(
        img_array_expanded_dims)  #imagei 0 ile 1 arasında normalize eder
예제 #8
0
def calibrate_on_validation(model_path: str, val_generator) -> float:
    logit_model = get_logits_friendly_model(load_model(model_path))
    x_val = val_generator.x
    y_val = val_generator.y
    x_val_pre = preprocess_input(x_val)
    return calibrate(logit_model, x_val_pre, y_val)