Search code examples
pythontensorflowkerasconv-neural-network

How can I read images in Kaggle with Python?


I am working in a analysis of data mining. I'm working with images from Kaggle, in a notebook in it. I have the root of this images given by .jpeg.

How can I read them? Then, I have to use this function tf.keras.preprocessing to get the variable and use it in a CNN model in Keras.

Thanks a lot!


Solution

  • As asked using keras, we can use :

    tf.keras.utils.image_dataset_from_directory(
        directory,
        labels="inferred",
        label_mode="int",
        class_names=None,
        color_mode="rgb",
        batch_size=32,
        image_size=(256, 256),
        shuffle=True,
        seed=None,
        validation_split=None,
        subset=None,
        interpolation="bilinear",
        follow_links=False,
        crop_to_aspect_ratio=False,
        **kwargs
    )
    

    However, if we want to have a look at some pictures first, we can use the PIL library to display it directly in the notebook like so :

    from PIL import Image
    
    img = Image.open('path/image.jpg')
    img
    

    This nice tutorial might help you even better (at least it helped me a lot).