0

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
encoder_input = Input(shape=(height, width, channels), name='encoder_input')
x = Conv2D(128, (3, 3), activation='relu', padding='same')(encoder_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 their 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

Most of them seem to work fine, but the syntax errors probably aren't meant to be there. I've tried to correct them, but didn't get anywhere.

2
  • I assume you can use x.shape rather than int_shape(x)
    – Frightera
    Commented Aug 10, 2024 at 14:04
  • @Frightera Yes, that works as an alternative. I'm still having trouble with other variables from the backend, so I'm hoping someone can come along and tell me what I need to do to fix my imports.
    – Errol
    Commented Aug 11, 2024 at 17:01

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.