Issue
I'm having my first steps with tensorflow and keras, I have created a dataset containing tuples with embeddings of images from VGG16 model as the data and labels which are binary multilabel. for example here is a print of an element from my dataset:
(<tf.Tensor: shape=(4096,), dtype=float32, numpy=
array([0.32185513, 0.14869851, 0.4276036 , ..., 0. , 0. ,
1.7438936 ], dtype=float32)>, <tf.Tensor: shape=(12,), dtype=int32, numpy=array([0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0])>)
I built the following model:
inputs = Input(shape = (4096,), name = 'input_1')
dense_1 = Dense(units = 2048, name = "dense_1", activation = 'sigmoid')(inputs)
dense_2 = Dense(units = 2048, name = "dense_2", activation = 'sigmoid')(dense_1)
output = Dense(units = 12, name = "output", activation = 'sigmoid')(dense_2)
model = Model(inputs = inputs, outputs = output)
model.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = 'accuracy')
model.summary()
model.fit(train_ds, validation_data = val_ds)
when I try to fit the model I get the following error
ValueError: Input 0 of layer dense_1 is incompatible with the layer: expected axis -1 of input shape to have value 4096 but received input with shape [4096, 1]
from what I understand the model expects a 1D tensor as input but gets a 2D tensor, the arrays in the data set are 1D, hence I don't understand why they are passed as a 2D tensor from the Input layer. any help figuring out what causes this error and how to fix it?
edit: the code to create the dataset is:
embed_train = np.load("Desktop/DL projects/pawpularity/petfinder-pawpularity-score/VGG16_embed.npy")
print(embed_train.shape)
>>>(9912, 4096)
print(train_labels.shape)
>>>(9912, 12)
train_ds = tf.data.Dataset.from_tensor_slices((embed_train, train_labels))
train_ds = train_ds.shuffle(10000)
test_ds = train_ds.take(1000)
train_ds = train_ds.skip(1000)
val_ds = train_ds.take(2000)
train_ds = train_ds.skip(2000)
both embed_train and train_labels are numpy arrays, as you can see I have a total of 9912 examples, each containing 4096 features, that needs to be classified for each label (12 labels in total) independently.
Solution
I was able to get your code to work by not using the Dataset
, instead, passing the training data and labels straight ahead.
import tensorflow as tf
import numpy as np
from tensorflow.keras import Input
from tensorflow.keras.layers import Dense
from tensorflow.keras.models import Model
# random data in your shape - you can split for validation/validation labels if you want.
# You would use your own embed_train and labels here
embed_train = np.random.rand(9912, 4096)
train_labels= np.random.randint(0, 12, [9912, 12])
inputs = Input(shape=(4096,), name ='input_1')
dense_1 = Dense(units = 2048, name = "dense_1", activation = 'sigmoid', input_shape=(4096,1))(inputs)
dense_2 = Dense(units = 2048, name = "dense_2", activation = 'sigmoid')(dense_1)
output = Dense(units = 12, name = "output", activation = 'sigmoid')(dense_2)
model = Model(inputs = inputs, outputs = output)
model.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = 'accuracy')
model.summary()
model.fit(embed_train , train_labels)
If you want to shuffle the data, you can use this answer from another post:
indices = tf.range(start=0, limit=tf.shape(embed_train)[0], dtype=tf.int32)
idx = tf.random.shuffle(indices)
x_data = tf.gather(embed_train, idx)
y_data = tf.gather(train_labels, idx)
model.fit(x_data, y_data)
Edit: For Datasets, I believe you need to specify a batch size:
train_ds = train_ds.batch(32)
model.fit(train_ds)
this works on my PC.
Answered By - I M
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.