Problems following CNN tutorial

I’m learning some Python using this tutorial for solving Vision Related tasks using CNNs, however there are several differences:

  1. Newest dataset is not anymore in tensorflow.keras.dataset as of tf 2.12 at least
    1.b In fact, keras is also in a different path, i.e tensorflow.python.keras
  2. Instead we need to install tensorflow-datasets

So this in the tutorial:

import tensorflow as tf
from tensorflow.keras import datasets, layers, models

Ends up being:

import tensorflow as tf 
from tensorflow.python.keras import layers, models
import tensorflow_datasets as tfds
  1. Now the API for this package is very different. So this in the tutorial:
class_names = ['airplane', 'automobile', 'bird', 'cat', 'deer',
               'dog', 'frog', 'horse', 'ship', 'truck']

plt.figure(figsize=(10,10))
for i in range(25):
    plt.subplot(5,5,i+1)
    plt.xticks([])
    plt.yticks([])
    plt.grid(False)
    plt.imshow(train_images[i])
    # The CIFAR labels happen to be arrays, 
    # which is why you need the extra index
    plt.xlabel(class_names[train_labels[i][0]])
plt.show()

Ends up using .take() because you cant use indexes:


(train, test), info = tfds.load('cifar10', split=['train', 'test'], with_info=True)

for i, item in enumerate(train.take(25)): 
    image = item["image"].numpy()
    plt.imsave(f'test_{i}.png', image)

I would be interesting hearing other options that you would show the images from the dataset.

Indexes can also be used item[0].numpy() if the function is called with arg as_supervised=True

There is a nice tutorial for Dataset thought, just found it: TensorFlow Datasets

I finally manage to run it, with several modifications, so I paste the notebook for others:

import tensorflow as tf 
layers = tf.keras.layers
models = tf.keras.models
import tensorflow_datasets as tfds

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

def normalize_img(image, label):
  """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.batch(32)
ds_train = ds_train.cache()
ds_train = ds_train.shuffle(ds_info.splits['train'].num_examples)

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(32)
ds_test = ds_test.cache()
ds_test = ds_test.prefetch(tf.data.AUTOTUNE)


model = models.Sequential()
model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.Flatten())
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dense(10))

model.summary()


model.compile(
    optimizer=tf.keras.optimizers.legacy.SGD(),
    loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
    metrics=[tf.keras.metrics.SparseCategoricalAccuracy()],
)
model.fit(ds_train, epochs=6, validation_data=ds_test)

@Mah_Neh,

I was able to run the tutorial successfully using TF 2.12 without any modifications. Please find the gist.

The tutorial was created long back using tensorflow.keras.datasets.

You can import layers and models as shown below

from tensorflow.keras import layers, models

Thank you for sharing working example using TFDS API.

Interesting, but I do not have keras in that path, but under tensorflow.python however, keras is installed as a separate module here, and I can actually run this:

import tensorflow as tf
from keras import datasets, layers, models, optimizers, losses, metrics

print(tf.__version__)
#2.12.0

I wonder what is going on, it looks like there is some mapping but certainly it seems that keras is a separate thing now and should be imported that way maybe @chunduriv ?

So the notebook throws no error if you instead write:

import tensorflow as tf

from keras import datasets, layers, models
import matplotlib.pyplot as plt

So I would update the tutorial for the second line

@Mah_Neh,

Since it is working as expected using Google Colab. Could you please share more details about your operating system to debug your issue further?

Thank you!