[Python] Tensorflow dataset crashing VsCode

Discussão em 'Python' iniciado por Stack, Agosto 17, 2025.

  1. Stack

    Stack Membro Participativo

    So I'm trying to make a simple image classifier using a CNN and an already labeled data set called "places 365". However, whenever I run model.fit() my RAM usage maxes out and VS Code crashes or I restart the kernel.

    In the past, I have gotten model.fit() to work just fine when I imported the cifar10 dataset for a previous model with tensorflow.keras.datasets.cifar10.load_data() so I am pretty sure I am missing something with how tfds.load() formats the dataset.

    Here is my entire setup:

    import tensorflow as tf
    import tensorflow_datasets as tfds
    from tensorflow.keras import datasets, layers, models
    import matplotlib.pyplot as plt
    import numpy as np

    (ds_train, ds_test), ds_info = tfds.load(
    'places365_small',
    split=['train', 'test'],
    shuffle_files=True,
    as_supervised=True,
    with_info=True,
    )

    def normalize_img(image, label , filename = ''):
    """Normalizes images: `uint8` -> `float32`."""
    return tf.cast(image, tf.float32) / 255., label

    ds_train = ds_train.map(
    normalize_img, num_parallel_calls=tf.data.AUTOTUNE)
    ds_train = ds_train.cache()
    ds_train = ds_train.shuffle(ds_info.splits['train'].num_examples)
    ds_train = ds_train.batch(128)
    ds_train = ds_train.prefetch(tf.data.AUTOTUNE)

    ds_test = ds_test.map(
    normalize_img, num_parallel_calls=tf.data.AUTOTUNE)
    ds_test = ds_test.batch(128)
    ds_test = ds_test.cache()
    ds_test = ds_test.prefetch(tf.data.AUTOTUNE)

    model = tf.keras.models.Sequential()
    model.add(tf.keras.layers.Conv2D(32, (3, 3), activation='relu', input_shape=(256, 256, 3)))
    model.add(tf.keras.layers.MaxPooling2D((2, 2)))
    model.add(tf.keras.layers.Conv2D(64, (3, 3), activation='relu'))
    model.add(tf.keras.layers.MaxPooling2D((2, 2)))
    model.add(tf.keras.layers.Conv2D(64, (3, 3), activation='relu'))

    model.add(tf.keras.layers.Flatten())
    model.add(tf.keras.layers.Dense(64, activation='relu'))
    model.add(tf.keras.layers.Dense(365))

    model.compile(
    optimizer=tf.keras.optimizers.Adam(0.001),
    loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
    metrics=[tf.keras.metrics.SparseCategoricalAccuracy()],
    )

    model.fit(
    ds_train,
    epochs=6,
    validation_data=ds_test,
    )


    I essentially followed the instructions here: Official TensorFlow documentation

    All I really changed was:


    • loaded places365_small dataset instead of MNIST


    • added the filename arg to normalize_img since the dataset has 3 features (image, label, filename)


    • implemented CNN model

      • changed input shape to match image dimensions for place365


      • final dense layer changed to 365 (365 categories)

    Everything else is the same, and all code blocks compile and run in Jupyter except for model.fit() so I have no idea why this stupid function is just gobbling my RAM and crashing VS Code.

    Continue reading...

Compartilhe esta Página