keras sequential model for image data - python

i'm trying to train a dense network for images.
the train set shape returns:
train_X.shape
(26032, 32, 32)
and the network architecture is:
def get_model(input_shape):
model = Sequential([
Dense(16, activation='relu', input_shape=(input_shape[1],input_shape[2],1)),
Dense(8, activation='relu'),
Dense(64, activation='relu'),
Flatten(),
Dense(10, activation='softmax')])
return model
but i get an error when i try to train it :
Error when checking input: expected dense_17_input to have 4 dimensions, but got array with shape (73257, 32, 32)
can u assist please?

For this specific architecture, you don't need a 4th dimension. In input_shape, you won't need to add a 1.
input_shape=(input_shape[1], input_shape[2])
You will only need to do so for a CNN.

Related

keras sequential tensor as argument

I am trying to reproduce the image classification problem cat or dog using tensorflow and transfer learning (Xception model pretrained with imagenet). The code is:
base_model = keras.applications.Xception(
weights='imagenet',
# image shape = 128x128x3
input_shape=(128, 128, 3),
include_top=False)
# freeze layers
base_model.trainable = False
inputs = keras.Input(shape=(128, 128, 3))
x = data_augmentation(inputs)
x = tf.keras.applications.xception.preprocess_input(x)
x = base_model(x, training=False)
x = keras.layers.Flatten()(x)
x = keras.layers.Dense(128, activation='relu')(x)
outputs = keras.layers.Dense(1, activation='sigmoid')(x)
model = keras.Model(inputs, outputs)
I am now trying to make use of models.Sequential. So far my code looks like this:
theModel=models.Sequential([
tf.keras.Input(shape=(128, 128, 3)),
tf.keras.applications.xception.preprocess_input(), <-------- how to pass tensor as argument?
base_model,
Flatten(),
Dense(128, activation='relu'),
Dense(1,activation='sigmoid')
])
My question, is there a way to make use of models.Sequentials, defining everything as I've done but passing the tensor as argument like in the first code snipped?
Thanks in advance,
metc
You cannot use tf.keras.applications.xception.preprocess_input() inside the sequential model. You have to define it outside the model and can pass the output of it to the sequential model by assigning values to the tensor argument in the input layer.
x=tf.random.uniform(shape=(1,128,128,3))
x= tf.keras.applications.xception.preprocess_input(x)
theModel=tf.keras.models.Sequential([
tf.keras.Input(tensor=x),
base_model,
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dense(1,activation='sigmoid')
])
For more details, Please refer to this gist.Thank You!

Using LSTM layer without Embedding

I have been trying to train a model for tf.keras.datasets.imdb using LSTM in tensorflow.
After some processing i have input -> x_train shape: (25000, 100).
Since i cannot feed it in a LSTM layer directly, i used a lambda function to change dimension of input:
model = Sequential([
Lambda(lambda x: tf.expand_dims(x,axis=-1)),
LSTM(62, dropout=0.2, recurrent_dropout=0.2, return_sequences=True),
LSTM(32),
Dense(1, activation='sigmoid')
])
But i am getting a horrible accuracy (around 55%).
But upon using embedding layer the accuracy increases to 90% using same hyper parameters:
model = Sequential([
Embedding(5000, 32),
LSTM(62, dropout=0.2, recurrent_dropout=0.2, return_sequences=True),
LSTM(32),
Dense(1, activation='sigmoid')
])
Am i doing something wrong in the first case?

Re-called on a Tensor with incompatible shape

I'm trying to create a CNN + Regression model here through the code below:
# Create the base model from the pre-trained model MobileNet V2
cnn_model = keras.applications.MobileNetV2(input_shape=IMG_SHAPE,
include_top=False,
weights='imagenet')
# The Regression Model
regression_model = keras.Sequential([
keras.layers.Dense(64, activation='relu',
input_shape=cnn_model.output_shape),
keras.layers.Dense(64, activation='relu')
])
prediction_layer = tf.keras.layers.Dense(1)
# Final Model
model = keras.Sequential([
cnn_model,
regression_model,
prediction_layer
])
Now the issue is that I get the WARNING below:
WARNING:tensorflow:Model was constructed with shape
Tensor("dense_12_input:0", shape=(None, None, 7, 7, 1280),
dtype=float32) for input (None, None, 7, 7, 1280), but it was
re-called on a Tensor with incompatible shape (None, 7, 7, 1280).
Does anyone know as to why this warning is coming up and how I can combat it, unless it's harmless.
It seems as though adding a flatten after the CNN solved my problem. Since we want to pass a flattened vector to the fully connected layer. The model should look like:
model = keras.Sequential([
cnn_model, keras.layers.Flatten(),
regression_model,
prediction_layer
])

TensorFlow: How do I use make a convolutional layer for tabular (1-D) features?

Using TensorFlow in Python, I am making a neural network that has a 1 dimensional array as input. I would like to add a convolutional layer to the network, but can't seem to get it to work.
My training data looks something like this:
n_samples = 20
length_feature = 10
features = np.random.random((n_samples, length_feature))
labels = np.array([1 if sum(e)>5 else 0 for e in features])
If I make a neural network like this one
model = keras.Sequential([
keras.layers.Dense(10, activation='relu', input_shape=(length_feature, )),
keras.layers.Dense(2, activation='softmax')
])
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
history = model.fit(features, labels, batch_size=5, validation_split = 0.2, epochs=10)
and this works just fine. But if I add a convolutional layer like this
model = keras.Sequential([
keras.layers.Dense(10, activation='relu', input_shape=(length_feature, )),
keras.layers.Conv1D(kernel_size = 3, filters = 2),
keras.layers.Dense(2, activation='softmax')
])
then I get the error
ValueError: Input 0 of layer conv1d_4 is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: [None, 10]
How can I add a convolutional layer to my neural network?
Conv1D expects a 3D output( batch_size, width, channels). But the dense layers produces a 2D output. Simply change your model to the following,
model = keras.Sequential([
keras.layers.Dense(10, activation='relu', input_shape=(length_feature, )),
keras.layers.Lambda(lambda x: K.expand_dims(x, axis=-1))
keras.layers.Conv1D(kernel_size = 3, filters = 2),
keras.layers.Dense(2, activation='softmax')
])
Where K is either keras.backend or tf.keras.backend depending on which one you used to get layers.

Keras - "ValueError: Error when checking target: expected activation_1 to have shape (None, 9) but got array with shape (9,1)

I'm building a model to classify text into one of 9 layers, and am having this error when running it. Activation 1 seems to refer to the Convolutional layer's input, but I'm unsure about what's wrong with the input.
num_classes=9
Y_train = keras.utils.to_categorical(Y_train, num_classes)
#Reshape data to add new dimension
X_train = X_train.reshape((100, 150, 1))
Y_train = Y_train.reshape((100, 9, 1))
model = Sequential()
model.add(Conv1d(1, kernel_size=3, activation='relu', input_shape=(None, 1)))
model.add(Dense(num_classes))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(x=X_train,y=Y_train, epochs=200, batch_size=20)
Running this results in the following error:
"ValueError: Error when checking target: expected activation_1 to have shape (None, 9) but got array with shape (9,1)
There are several typos and bugs in your code.
Y_train = Y_train.reshape((100,9))
Since you reshape X_train to (100,150,1), I guess your input step is 150, and channel is 1. So for the Conv1D, (there is a typo in your code), input_shape=(150,1).
You need to flatten your output of conv1d before feeding into Dense layer.
import keras
from keras import Sequential
from keras.layers import Conv1D, Dense, Flatten
X_train = np.random.normal(size=(100,150))
Y_train = np.random.randint(0,9,size=100)
num_classes=9
Y_train = keras.utils.to_categorical(Y_train, num_classes)
#Reshape data to add new dimension
X_train = X_train.reshape((100, 150, 1))
Y_train = Y_train.reshape((100, 9))
model = Sequential()
model.add(Conv1D(2, kernel_size=3, activation='relu', input_shape=(150,1)))
model.add(Flatten())
model.add(Dense(num_classes, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(x=X_train,y=Y_train, epochs=200, batch_size=20)

Categories