Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hi! #2

Open
pentadotddot opened this issue Sep 9, 2020 · 2 comments
Open

Hi! #2

pentadotddot opened this issue Sep 9, 2020 · 2 comments

Comments

@pentadotddot
Copy link

pentadotddot commented Sep 9, 2020

Hi!

Is it possible to just run inference on a random image, like python app.py --image ./test.jpg --model custom_cnn ? Also could you please provide the .h5 files? Nice and clean implementation though, thanks for tis repo!

@Sanjana7395
Copy link
Owner

This is the code to run inference on a random image:

def plot(image, model, label, x_hog=None):
    plt.figure(figsize=(8, 6))
    plt.imshow(image)
    plt.axis('off')

    predictions, percent = model_predict(model, image, x_hog, label)
    print(predictions)
    print(percent)

    plt.suptitle('Predicted label: {} ({:.2f} %)'.format(predictions[0], np.max(percent[0]) * 100))

    make_folder('results/visualization')
    plt.savefig('results/visualization/app.png', bbox_inches='tight')

def model_predict(model, x, hog, y):
    predictions = []
    percent = []
    labeler = LabelEncoder()
    labeler.fit(y)
    if model == "custom_cnn":
        custom_model = load_model(os.path.join(ROOT_DIR, 'custom.h5'))

        x = np.expand_dims(x, axis=0)
        percent = custom_model.predict(x)
        predictions = np.argmax(percent, axis=-1)
        predictions = labeler.inverse_transform(predictions)
        
    return predictions, percent


def main():
    """ Predict the disease of the given image.

    Usage example:
        python app.py -m custom_cnn -i path/to/image

    """
    # construct the argument parser and parse the arguments
    ap = argparse.ArgumentParser()
    ap.add_argument("-i", "--image_index", type=int, help="index of the test image")
    ap.add_argument("-img", "--image", type=str, help="index of the test image")
    ap.add_argument("-m", "--model", type=str, required=True, choices=("vgg", "custom_cnn", "svm", random_forest", "majority_voting", "stacked_prediction"), help="model to be used")
    args = vars(ap.parse_args())

    X_image = np.load('data/test/ImageTest_input.npy')
    y = np.load('data/test/DiseaseTest_input.npy')

    if args["image_index"]:
        image = X_image[args["image_index"]]

    elif args["image"]:
        path = args["image"]
        image = plt.imread(path)
        image = cv2.resize(image, (180, 180), cv2.INTER_AREA)

    plot(image, args["model"], y)


if __name__ == "__main__":
    main()

Also I have attached the CNN-Custom trained model
custom.zip

Hope this helps!

@pentadotddot
Copy link
Author

Thank you it really helped me! I get you back with some results:) Thanks again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants