TensorFlow v.2.17.0
Keras v.3.4.1
Since Colab updated the libraries, I have been working towards making my code Keras 3 compliant. This is the error I am getting:
AttributeError Traceback (most recent call last)
<ipython-input-33-b4147f011b96> in <cell line: 12>()
10
11 # Flatten layer
---> 12 shape_before_flattening = K.int_shape(x)[1:]
13 x = Flatten()(x)
14
AttributeError: module 'keras.backend' has no attribute 'int_shape'
These are my relevent imports:
import os
os.environ["KERAS_BACKEND"] = "tensorflow"
import keras.backend as K
…and this is a snippet of my code:
# Example layers
input = Input(shape=(height, width, channels), name='input')
x = Conv2D(128, (3, 3), activation='relu', padding='same')(input)
# Flatten layer
shape_before_flattening = K.int_shape(x)[1:]
x = Flatten()(x)
I am assuming that my imports aren’t correct, or Keras is just handling things completely differently now. This worked fine in Keras 2.
I have been following the Keras 3 migration guide from https://keras.io/guides/migrating_to_keras_3/
I have tried multiple import configurations. This was my original import using Keras 2:
from keras import regularizers, backend as K, layers
It might be relevant that many of my Keras imports have syntax errors, which makes me think I’m not importing them correctly. The following are okay:
import keras
import tensorflow as tf
from keras import regularizers, layers
but the rest have a syntax error indicated on there module path:
import keras.backend as K
import keras.ops as ops
from keras.models import Sequential, Model, load_model
from keras.callbacks import EarlyStopping, ModelCheckpoint, History
from keras.layers import Conv2D, MaxPooling2D, UpSampling2D, Flatten, Dense, Reshape, Dropout, BatchNormalization, Activation, Lambda, Input
from keras.optimizers import Adam
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from keras.utils import plot_model, get_custom_objects
from keras.saving import register_keras_serializable
They seem to work fine, and I haven’t been able to get rid of them, but they probably aren’t meant to be there.
Errol DaRocha is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.