ValueError: Only instances of keras.Layer

ValueError: Only instances of keras.Layer can be added to a Sequential model. Received: <tensorflow_hub.keras_layer.KerasLayer object at 0x7c61f819f590> (of type <class ‘tensorflow_hub.keras_layer.KerasLayer’>)

code:
import kagglehub

Download latest version

path = kagglehub.model_download(“google/mobilenet-v2/tensorFlow2/tf2-preview-feature-vector”)
mobile_net = hub.KerasLayer(“https://tfhub.dev/google/tf2-preview/mobilenet_v2/feature_vector/4”,
input_shape=(224, 224, 3), # Change as per your input size
trainable=False) # Freeze weights if you don’t want to fine-tune

Define the Sequential model

model = tf.keras.models.Sequential([
mobile_net, # Use the pre-trained model as the first layer
tf.keras.layers.Dense(1, activation=’sigmoid’) # Final layer for binary classification
])

i want to solve the problem but i cant and i need help. if full code needed tell me to send to you in colab or other places
even i ask chatgpt but the solutions doesn’t word

New contributor

Moein Nouri is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

2

The error ValueError: Only instances of keras.Layer occurs when you add an invalid object (like a string or uninitialized layer) to a Keras model.

Common Causes:
Non-Layer Object: Adding something like a string or function instead of a Keras layer.

Fix: Ensure all objects in the model are valid Keras layers.
python
Copy code
model = Sequential([Dense(64, activation=’relu’)]) # Correct
Uninitialized Layer: Forgetting to initialize a layer.

Fix: Add parentheses when defining layers.
python
Copy code
layer = Dense(64) # Correct

enter link description here

New contributor

client work is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

The code is working fine in tensorflow==2.15 and tensorflow_hub==0.16.1. So, the error could be due to compatibility issues arising from the integration of Keras 3.0 in TensorFlow 2.17. Consider using tf-keras (Keras 2.0) may resolve the issue or using a Lambda layer to wrap hub.KerasLayer (mobile_net) ensures compatibility and allows you to build the model using tf.keras.models.Sequential .

Using lambda layer to wrap the hub layer

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import tensorflow as tf
import tensorflow_hub as hub
import kagglehub
path = kagglehub.model_download("google/mobilenet-v2/tensorFlow2/tf2-preview-feature-vector")
mobile_net = hub.KerasLayer(
"https://tfhub.dev/google/tf2-preview/mobilenet_v2/feature_vector/4")
model = tf.keras.Sequential([
tf.keras.layers.InputLayer(input_shape=(224, 224, 3)),
tf.keras.layers.Lambda(lambda x: mobile_net(x)),
tf.keras.layers.Dense(1, activation='sigmoid')
])
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
model.summary()
</code>
<code>import tensorflow as tf import tensorflow_hub as hub import kagglehub path = kagglehub.model_download("google/mobilenet-v2/tensorFlow2/tf2-preview-feature-vector") mobile_net = hub.KerasLayer( "https://tfhub.dev/google/tf2-preview/mobilenet_v2/feature_vector/4") model = tf.keras.Sequential([ tf.keras.layers.InputLayer(input_shape=(224, 224, 3)), tf.keras.layers.Lambda(lambda x: mobile_net(x)), tf.keras.layers.Dense(1, activation='sigmoid') ]) model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy']) model.summary() </code>
import tensorflow as tf
import tensorflow_hub as hub
import kagglehub

path = kagglehub.model_download("google/mobilenet-v2/tensorFlow2/tf2-preview-feature-vector")

mobile_net = hub.KerasLayer(
    "https://tfhub.dev/google/tf2-preview/mobilenet_v2/feature_vector/4")

model = tf.keras.Sequential([
    tf.keras.layers.InputLayer(input_shape=(224, 224, 3)),
    tf.keras.layers.Lambda(lambda x: mobile_net(x)),
    tf.keras.layers.Dense(1, activation='sigmoid')
])
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
model.summary()

output:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>Model: "sequential_5"
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━┓
Layer (type) ┃ Output Shape ┃ Param # ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━┩
lambda_2 (Lambda)(None, 1280)0
├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤
dense_5 (Dense)(None, 1)1,281
└──────────────────────────────────────┴─────────────────────────────┴─────────────────┘
Total params: 1,281 (5.00 KB)
Trainable params: 1,281 (5.00 KB)
Non-trainable params: 0 (0.00 B)
</code>
<code>Model: "sequential_5" ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━┓ ┃ Layer (type) ┃ Output Shape ┃ Param # ┃ ┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━┩ │ lambda_2 (Lambda) │ (None, 1280) │ 0 │ ├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤ │ dense_5 (Dense) │ (None, 1) │ 1,281 │ └──────────────────────────────────────┴─────────────────────────────┴─────────────────┘ Total params: 1,281 (5.00 KB) Trainable params: 1,281 (5.00 KB) Non-trainable params: 0 (0.00 B) </code>
Model: "sequential_5"
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━┓
┃ Layer (type)                         ┃ Output Shape                ┃         Param # ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━┩
│ lambda_2 (Lambda)                    │ (None, 1280)                │               0 │
├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤
│ dense_5 (Dense)                      │ (None, 1)                   │           1,281 │
└──────────────────────────────────────┴─────────────────────────────┴─────────────────┘
 Total params: 1,281 (5.00 KB)
 Trainable params: 1,281 (5.00 KB)
 Non-trainable params: 0 (0.00 B)

Kindly refer to to this gist, and documentation for more details.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật