Stuck implementation for CNN - python

def get_three_classes(x, y):
indices_0, _ = np.where(y == 0.)
indices_1, _ = np.where(y == 1.)
indices_2, _ = np.where(y == 2.)
indices = np.concatenate([indices_0, indices_1, indices_2], axis=0)
x = x[indices]
y = y[indices]
count = x.shape[0]
indices = np.random.choice(range(count), count, replace=False)
x = x[indices]
y = y[indices]
y = tf.keras.utils.to_categorical(y)
return x, y
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.cifar10.load_data()
x_train, y_train = get_three_classes(x_train, y_train)
x_test, y_test = get_three_classes(x_test, y_test)
print(x_train.shape, y_train.shape)
print(x_test.shape, y_test.shape)
class_names = ['aeroplane', 'car', 'bird']
def show_random_examples (x, y, p):
indices = np.random.choice(range(x.shape[0]), 10, replace = False)
x= x [indices]
y= y [indices]
p = p[indices]
plt.figure(figsize = (10,5))
for i in range(10):
plt.subplot(2,5, 1+i)
plt.imshow(x[i])
plt.xticks([])
plt.yticks([])
col = 'green' if np.argmax(y[i]) == np.argmax(p[i]) else 'red'
plt.xlabel(class_names[np.argmax(p[i])], color = col)
plt.show()
#Creating model
def create_model():
def add_conv_block(model, num_filters):
model.add(Conv2D(num_filters, 3, activation='relu', padding='same'))
model.add(BatchNormalization())
model.add(Conv2D(num_filters, 3, activation='relu'))
model.add(MaxPooling2D(pool_size=2))
model.add(Dropout(0.5))
return model
model = tf.keras.models.Sequential()
model.add(Input(shape=(32,32,3)))
model= add_conv_block(model, 32)
model= add_conv_block(model, 64)
model= add_conv_block(model, 128)
model.add(Flatten())
model.add(Dense(3, activation='softmax'))
model.compile(
loss='categorical_crossentropy',
optimizer = 'adam', metrics = ['accuracy']
)
return model
h = model.fit (
x_train/255., y_train,
validation_data = (x_test/255., y_test),
epochs=10, batch_size=128,
callbacks=[tf.keras.callbacks.EarlyStopping(monitor='val_accuracy', patience=3),
tf.keras.callbacks.ModelCheckpoint(
'/content/drive/MyDrive/models/model_val{val_accuracy:.3f}.h5',
save_best_only=True, save_weights_only=False,
monitor='val_accuracy') ]
)
model = tf.keras.models.load_model('/content/drive/MyDrive/models/model_val0.896.h5')
preds= model.predict(x_test/255.)
image = np.array(Image.open("/content/drive/MyDrive/thy3.jpg").resize((32, 32)))
print("shape: ", image.shape)
shape: (32, 32)
pr_mask = model.predict(image).round()
ValueError: in user code:
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1621, in predict_function *
return step_function(self, iterator)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1611, in step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1604, in run_step **
outputs = model.predict_step(data)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1572, in predict_step
return self(x, training=False)
File "/usr/local/lib/python3.7/dist-packages/keras/utils/traceback_utils.py", line 67, in error_handler
raise e.with_traceback(filtered_tb) from None
File "/usr/local/lib/python3.7/dist-packages/keras/engine/input_spec.py", line 263, in assert_input_compatibility
raise ValueError(f'Input {input_index} of layer "{layer_name}" is '
ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 32, 32, 3), found shape=(32, 32, 3)
Hey everyone, this is code im working on it for project but problem is i cannot implement random photos on this model.Getting this error message, I just want to get result for random photos, and model predict it.Its basic tensorflow project only using cifar10 dataset, and only 3 classes outputs. Model predict is it car, airplane or bird thats it. Why i want is lets say i found airjet photo from web and wants this model predict it, is it airplane or bird? prediction could be wrong or accurate i dont care. But problem is i cannot format photos, or maybe you can give me idea how can i deal with that . Thanks for your answers.

The image used for the predict function is in your code defined as follows:
image = np.array(Image.open("/content/drive/MyDrive/thy3.jpg").resize((32, 32)))
Which corresponds with the following shape according to the error message you posted:
(32, 32, 3)
Error message posted:
ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 32, 32, 3), found shape=(32, 32, 3)
But the model requires the image to have a batch dimension for the prediciton, like:
(1, 32, 32, 3)
If your image is a 3-channel with width=32 and height=32 use to reshape it:
image = np.array(Image.open("/content/drive/MyDrive/thy3.jpg").reshape((1, 32, 32, 3)))

Related

ValueError: Can not squeeze dim[1], expected a dimension of 1, got 128

I am trying to build a transformer neural network for a text classification problem. I am following this tutorial but tweaking the encoder to include a layer with a softmax activation function.
This is the code that I changed:
class Encoder(tf.keras.layers.Layer):
def __init__(self,
*,
num_layers,
d_model, # Input/output dimensionality.
num_attention_heads,
dff, # Inner-layer dimensionality.
input_vocab_size, # Input vocabulary size.
dropout_rate=0.1
):
super().__init__()
self.d_model = d_model
self.num_layers = num_layers
# Embeddings + Positional encoding
self.pos_embedding = PositionalEmbedding(input_vocab_size, d_model)
# Encoder layers.
self.enc_layers = [
EncoderLayer(
d_model=d_model,
num_attention_heads=num_attention_heads,
dff=dff,
dropout_rate=dropout_rate)
for _ in range(num_layers)]
# Dropout.
self.dropout = tf.keras.layers.Dropout(dropout_rate)
self.flatten_layer = tf.keras.layers.GlobalAveragePooling1D()
self.dense_layer_1 = tf.keras.layers.Dense(256, activation='relu')
self.dense_layer_2 = tf.keras.layers.Dense(32, activation='relu')
self.final_layer = tf.keras.layers.Dense(2, activation='softmax')
# Masking.
def compute_mask(self, x, previous_mask=None):
return self.pos_embedding.compute_mask(x, previous_mask)
def call(self, x, training):
seq_len = tf.shape(x)[1]
# Sum up embeddings and positional encoding.
mask = self.compute_mask(x)
x = self.pos_embedding(x) # Shape `(batch_size, input_seq_len, d_model)`.
# Add dropout.
x = self.dropout(x, training=training)
# N encoder layers.
for i in range(self.num_layers):
x = self.enc_layers[i](x, training, mask)
# experimental thing, may not work
x = self.flatten_layer(x)
x = self.dense_layer_1(x)
x = self.dense_layer_2(x)
# x = tf.keras.layers.Dense(32, activation='relu')(x)
x = self.final_layer(x)
return x # Shape `(batch_size, input_seq_len, d_model)`.
I am using the Twitter sentiment analysis dataset to classify positive or negative sentences.
My sequence length is 128.
The shape of my features is (43012, 128), and the shape of the labels is (43012, 1).
The model definition is:
model = tf.keras.Sequential([
tf.keras.layers.InputLayer(input_shape=(128)),
Encoder(num_layers = 4, d_model = 128, dff = 512, num_attention_heads = 8, input_vocab_size=tokenizers.en.get_vocab_size())
])
Which I have compiled as:
model.compile(
optimizer="adam",
loss="sparse_categorical_crossentropy",
metrics=["accuracy"]
)
But when I try to train my model
history = model.fit(
x=new_features,
y=new_labels,
epochs=200,
batch_size=64,
validation_split=0.2
)
it is giving me the following error:
ValueError: in user code:
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1160, in train_function *
return step_function(self, iterator)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1146, in step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1135, in run_step **
outputs = model.train_step(data)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 994, in train_step
loss = self.compute_loss(x, y, y_pred, sample_weight)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1053, in compute_loss
y, y_pred, sample_weight, regularization_losses=self.losses
File "/usr/local/lib/python3.7/dist-packages/keras/engine/compile_utils.py", line 265, in __call__
loss_value = loss_obj(y_t, y_p, sample_weight=sw)
File "/usr/local/lib/python3.7/dist-packages/keras/losses.py", line 159, in __call__
losses, sample_weight, reduction=reduction
File "/usr/local/lib/python3.7/dist-packages/keras/utils/losses_utils.py", line 350, in compute_weighted_loss
) = squeeze_or_expand_dimensions(losses, None, sample_weight)
File "/usr/local/lib/python3.7/dist-packages/keras/utils/losses_utils.py", line 224, in squeeze_or_expand_dimensions
sample_weight = tf.squeeze(sample_weight, [-1])
ValueError: Can not squeeze dim[1], expected a dimension of 1, got 128 for '{{node sparse_categorical_crossentropy/weighted_loss/Squeeze}} = Squeeze[T=DT_FLOAT, squeeze_dims=[-1]](Cast)' with input shapes: [?,128].
I tested the model with a random input without training and it works fine.
input = np.random.rand(5, 128)
x = model((input))
x.shape # (5, 2) as expected
Why am I getting this error and how can I fix this?

CNN + LSTM implementation error for image classification

I am trying to implement a CNN network + LSTM to be able to predict 4 different classes based on the sequence of x-ray images, which were preprocessed to 150x150x3 shape. My X-train shape is (4067, 150, 150, 3). When I am executing the code
model.fit(), i am getting the error.
# x_train = np.reshape(x_train, (4067, 150, 150, 3))
# y_train = np.reshape(y_train, (4067, 4))
model = Sequential()
model.add(TimeDistributed(Conv2D(filters = 32,
kernel_size=(3,3),
padding='same',
activation = 'relu'),
input_shape=(None, 150, 150, 3)))
model.add(TimeDistributed(AveragePooling2D()))
model.add(TimeDistributed(Flatten()))
model.add(LSTM(100))
model.add(Dense(24, activation='relu',name='output'))
model.add(Dense(4, activation = 'softmax'))
from tensorflow.keras.optimizers import Adam
optimizer = Adam(lr=0.001)
model.compile(optimizer = optimizer,
loss = 'categorical_crossentropy',
metrics=['accuracy'])
from tensorflow.keras.callbacks import ReduceLROnPlateau
reduce_lr = ReduceLROnPlateau(monitor = 'val_accuracy',
factor = 0.3,
patience = 2,
min_delta = 0.001,
mode = 'auto',
verbose = 1)
hist_cnn_lstm = model.fit(x_train, y_train, batch_size=64, epochs=15,
validation_data = (x_valid, y_valid),
callbacks=reduce_lr
)
ERROR:
Epoch 1/15
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-24-3ec61fbabcf1> in <module>()
1 hist_cnn_lstm = model.fit(x_train, y_train, batch_size=64, epochs=15,
2 validation_data = (x_valid, y_valid),
----> 3 callbacks=reduce_lr
4 )
1 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/func_graph.py in autograph_handler(*args, **kwargs)
1145 except Exception as e: # pylint:disable=broad-except
1146 if hasattr(e, "ag_error_metadata"):
-> 1147 raise e.ag_error_metadata.to_exception(e)
1148 else:
1149 raise
ValueError: in user code:
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1021, in train_function *
return step_function(self, iterator)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1010, in step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1000, in run_step **
outputs = model.train_step(data)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 859, in train_step
y_pred = self(x, training=True)
File "/usr/local/lib/python3.7/dist-packages/keras/utils/traceback_utils.py", line 67, in error_handler
raise e.with_traceback(filtered_tb) from None
File "/usr/local/lib/python3.7/dist-packages/keras/engine/input_spec.py", line 264, in assert_input_compatibility
raise ValueError(f'Input {input_index} of layer "{layer_name}" is
ValueError: Input 0 of layer "sequential_1" is incompatible with the layer: expected shape=(None, None, 150, 150, 3), found shape=(None, 150, 150, 3)

Problems with using ResNet50

I know that reshape problems are a basic thing and that there are a lot of solutions out there, but I can't find one that works for me.
I'm currently trying to use ResNet50 to train with the Iceberg challenge (https://www.kaggle.com/competitions/statoil-iceberg-classifier-challenge):
import numpy as np, pandas as pd
from tensorflow.keras.optimizers import Adam
from keras.models import Model, Sequential
from keras.layers import Dense, Dropout, Conv2D, MaxPooling2D, Input, concatenate, GlobalMaxPooling2D
from tensorflow.keras.applications.mobilenet import MobileNet
vgg16_fl = "imagenet"
from tensorflow.keras.applications import VGG16, VGG19, ResNet50, Xception
def get_simple(dropout=0.5):
model = Sequential()
model.add(Conv2D(64, kernel_size=(3, 3), activation='relu', input_shape=(75, 75, 3)))
model.add(MaxPooling2D(pool_size=(3, 3), strides=(2, 2)))
model.add(Dropout(dropout))
model.add(Conv2D(128, kernel_size=(3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(3, 3), strides=(2, 2)))
model.add(Dropout(dropout))
model.add(Conv2D(256, kernel_size=(3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(3, 3), strides=(2, 2)))
model.add(Dropout(dropout))
return model
factory = {
'vgg16': lambda: VGG16(include_top=False, input_shape=(75, 75, 3), weights=vgg16_fl),
'mobilenetv2': lambda: MobileNet(include_top=False, input_shape=(75, 75, 3)),
'resnet50': lambda: ResNet50(include_top=False, input_shape=(200, 200, 3)),
}
def get_model(name='simple',train_base=True,use_angle=False,dropout=0.5,layers=(512,256)):
base = factory[name]()
inputs = [base.input]
x = GlobalMaxPooling2D()(base.output)
if use_angle:
angle_in = Input(shape=(1,))
angle_x = Dense(1, activation='relu')(angle_in)
inputs.append(angle_in)
x = concatenate([x, angle_x])
for l_sz in layers:
x = Dense(l_sz, activation='relu')(x)
x = Dropout(dropout)(x)
x = Dense(1, activation='sigmoid')(x)
for l in base.layers:
l.trainable = train_base
return Model(inputs=inputs, outputs=x)
data = pd.read_json('/content/drive/MyDrive/iceberg/train.json')
b1 = np.array(data["band_1"].values.tolist()).reshape(-1, 75, 75, 1)
b2 = np.array(data["band_2"].values.tolist()).reshape(-1, 75, 75, 1)
b3 = b1 + b2
X = np.concatenate([b1, b2, b3], axis=3)
y = np.array(data['is_iceberg'])
angle = np.array(pd.to_numeric(data['inc_angle'], errors='coerce').fillna(0))
model = get_model('vgg16', train_base=False, use_angle=True)
model.compile(loss='binary_crossentropy', optimizer=Adam(lr=1e-3), metrics=['accuracy'])
history = model.fit([X, angle], y, shuffle=True, verbose=1, epochs=5)
model = get_model('mobilenetv2', train_base=False, use_angle=True)
model.compile(loss='binary_crossentropy', optimizer=Adam(lr=1e-3), metrics=['accuracy'])
history = model.fit([X, angle], y, shuffle=True, verbose=1, epochs=5)
model = get_model('resnet50', train_base=False, use_angle=True)
model.compile(loss='binary_crossentropy', optimizer=Adam(lr=1e-3), metrics=['accuracy'])
history = model.fit([X, angle], y, shuffle=True, verbose=1, epochs=5)
I can use VGG16 and MobileNet easly, but I can't do the same with ResNet, here's the error:
ValueError Traceback (most recent call last)
<ipython-input-58-cb998dc5f0be> in <module>()
1 model = get_model('resnet50', train_base=False, use_angle=True)
2 model.compile(loss='binary_crossentropy', optimizer=Adam(lr=1e-3), metrics=['accuracy'])
----> 3 history = model.fit([X, angle], y, shuffle=True, verbose=1, epochs=5)
1 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/func_graph.py in autograph_handler(*args, **kwargs)
1145 except Exception as e: # pylint:disable=broad-except
1146 if hasattr(e, "ag_error_metadata"):
-> 1147 raise e.ag_error_metadata.to_exception(e)
1148 else:
1149 raise
ValueError: in user code:
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1021, in train_function *
return step_function(self, iterator)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1010, in step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1000, in run_step **
outputs = model.train_step(data)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 859, in train_step
y_pred = self(x, training=True)
File "/usr/local/lib/python3.7/dist-packages/keras/utils/traceback_utils.py", line 67, in error_handler
raise e.with_traceback(filtered_tb) from None
File "/usr/local/lib/python3.7/dist-packages/keras/engine/input_spec.py", line 264, in assert_input_compatibility
raise ValueError(f'Input {input_index} of layer "{layer_name}" is '
ValueError: Input 0 of layer "model_13" is incompatible with the layer: expected shape=(None, 200, 200, 3), found shape=(None, 75, 75, 3)
If I try to modify the RESHAPE function (b1 = np.array(data["band_1"].values.tolist()).reshape(-1, 200, 200, 1)...) I get:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-4-14c39c176685> in <module>()
1 data = pd.read_json('/content/drive/MyDrive/iceberg/train.json')
----> 2 b1 = np.array(data["band_1"].values.tolist()).reshape(-1, 200, 200, 1)
3 b2 = np.array(data["band_2"].values.tolist()).reshape(-1, 200, 200, 1)
4 b3 = b1 + b2
5
ValueError: cannot reshape array of size 9022500 into shape (200,200,1)
Is there any way to fix this?
The problem is those 2 lines:
ResNet50(include_top=False, input_shape=(200, 200, 3)),
^^^^^^^^^^^^^^^^^^^^^^^^^^
np.array(data["band_2"].values.tolist()).reshape(-1, 75, 75, 1)
^^^^^^^^^^^^^
and since a sample of your dataset is 75x75, you can't obviously be reshaped to become 200x200
probably worth just using
ResNet50(include_top=False, input_shape=(75, 75, 1)),
instead of your current one

How to fit image data correctly to a model in python?

i am trying to trained a cnn model, but i really don't understand how to do it properly. i still learning about this kind of stuff so i'm really lost. I already tried doing stuff with it but still cannot get my head around it. can someone explain to me how to do it properly. when i try to fit the train data to the model this error pops up.
WARNING:tensorflow:Model was constructed with shape (None, 224, 224, 3) for input KerasTensor(type_spec=TensorSpec(shape=(None, 224, 224, 3), dtype=tf.float32, name='input_1'), name='input_1', description="created by layer 'input_1'"), but it was called on an input with incompatible shape (None,).
Traceback (most recent call last):
File "G:/Skripsi/Program/training.py", line 80, in <module>
train.train()
File "G:/Skripsi/Program/training.py", line 70, in train
model.fit(self.x_train, self.y_train, epochs=2, verbose=1)
File "G:\Skripsi\Program\venv\lib\site-packages\keras\utils\traceback_utils.py", line 67, in error_handler
raise e.with_traceback(filtered_tb) from None
File "G:\Skripsi\Program\venv\lib\site-packages\tensorflow\python\framework\func_graph.py", line 1129, in autograph_handler
raise e.ag_error_metadata.to_exception(e)
ValueError: in user code:
File "G:\Skripsi\Program\venv\lib\site-packages\keras\engine\training.py", line 878, in train_function *
return step_function(self, iterator)
File "G:\Skripsi\Program\venv\lib\site-packages\keras\engine\training.py", line 867, in step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "G:\Skripsi\Program\venv\lib\site-packages\keras\engine\training.py", line 860, in run_step **
outputs = model.train_step(data)
File "G:\Skripsi\Program\venv\lib\site-packages\keras\engine\training.py", line 808, in train_step
y_pred = self(x, training=True)
File "G:\Skripsi\Program\venv\lib\site-packages\keras\utils\traceback_utils.py", line 67, in error_handler
raise e.with_traceback(filtered_tb) from None
File "G:\Skripsi\Program\venv\lib\site-packages\keras\engine\input_spec.py", line 227, in assert_input_compatibility
raise ValueError(f'Input {input_index} of layer "{layer_name}" '
ValueError: Exception encountered when calling layer "model" (type Functional).
Input 0 of layer "conv2d" is incompatible with the layer: expected min_ndim=4, found ndim=1. Full shape received: (None,)
Call arguments received:
• inputs=tf.Tensor(shape=(None,), dtype=int32)
• training=True
• mask=None
this is my code for training the model.
from sklearn.model_selection import train_test_split
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Input
from densenet201 import DenseNet201
import tensorflow as tf
import pandas as pd
import numpy as np
import cv2
import os
dataset_folder = "./datasets/train_datasets"
class TrainingPreprocessing:
#staticmethod
def preprocessing_train(path):
images = cv2.imread(path, 3)
images_resize = cv2.resize(src=images, dsize=(224, 224), interpolation=cv2.INTER_LINEAR)
images_normalize = cv2.normalize(images_resize, None, alpha=0, beta=1, norm_type=cv2.NORM_MINMAX,
dtype=cv2.CV_32F)
return images_normalize.reshape(224, 224, 3)
class Training:
#staticmethod
def load_data():
"""Loads and Preprocess dataset"""
train_labels_encode = []
train_labels = []
train_data = []
file_list = os.listdir(dataset_folder)
for folder in file_list:
file_list2 = os.listdir(str(dataset_folder) + '/' + str(folder))
for images in file_list2:
train_labels_encode.append(folder)
train_labels.append(folder)
train_data.append(np.array(TrainingPreprocessing.preprocessing_train(
str(dataset_folder) + '/' + str(folder) + '/' + str(images)
)))
labels = np.array(train_labels_decode)
data = np.array(train_data)
return labels, data
def split_data(self):
"""Split the preprocessed dataset to train and test data"""
x, y = self.load_data()
self.x_train, self.x_test, self.y_train, self.y_test = train_test_split(x, y, test_size=0.20, random_state=0)
print('Training data shape : ', self.x_train.shape, self.y_train.shape)
print('Testing data shape : ', self.x_test.shape, self.y_test.shape)
def train(self):
"""Compile dan fit DenseNet model"""
input_shape = 224, 224, 3
number_classes = 2
model = DenseNet201.densenet(input_shape, number_classes)
model.summary()
model.compile(loss='binary_crossentropy', optimizer='Adam', metrics=["accuracy"])
model.fit(self.x_train, self.y_train, epochs=2, verbose=1)
model.save_weights('densenet201_best_model.h5', overwrite=True)
loss, accuracy = model.evaluate(self.x_test, self.y_test)
print("[INFO] accuracy: {:.2f}%".format(accuracy * 100))
train = Training()
train.split_data()
train.train()
and this is the code for the cnn network
from tensorflow.keras.layers import AveragePooling2D, GlobalAveragePooling2D, MaxPool2D
from tensorflow.keras.layers import Input, Conv2D, BatchNormalization, Dense
from tensorflow.keras.layers import ReLU, concatenate, Dropout
from tensorflow.keras.models import Model
import tensorflow.keras.layers as layers
import tensorflow.keras.backend as K
import tensorflow as tf
class DenseNet201:
def densenet(image_shape, number_classes, growth_rate=32):
def batch_relu_conv(x, growth_rate, kernel=1, strides=1):
x = BatchNormalization()(x)
x = ReLU()(x)
x = Conv2D(growth_rate, kernel, strides=strides, padding='same', kernel_initializer="he_uniform")(x)
return x
def dense_block(x, repetition):
for _ in range(repetition):
y = batch_relu_conv(x, 4 * growth_rate)
y = batch_relu_conv(y, growth_rate, 3)
x = concatenate([y, x])
return x
def transition_layer(x):
x = batch_relu_conv(x, K.int_shape(x)[-1] // 2)
x = AveragePooling2D(2, strides=2, padding='same')(x)
return x
inputs = Input(image_shape)
x = Conv2D(64, 7, strides=2, padding='same', kernel_initializer="he_uniform")(inputs)
x = MaxPool2D(3, strides=2, padding='same')(x)
for repetition in [6, 12, 48, 32]:
d = dense_block(x, repetition)
x = transition_layer(d)
x = GlobalAveragePooling2D ()(d)
output = Dense(number_classes, activation='softmax')(x)
model = Model(inputs, output)
return model
It seems you inverted data and labels (x and y) in the function:
def load_data(): which returns: return labels, data
I think you are calling model.fit(self.x_train, self.y_train, epochs=2, verbose=1)
with label and then data. Hence the model complaining about not getting the expected data shape.

Input 0 of layer sequential is incompatible with the layer: expected axis -1 of input shape to have value 3 but received input with shape

it is my first time with tensorflow and I have problem that I can't solve.
I found a lot of same problems on stackoverflow, but they don't help me. (probably I just don't know how use it correctly)
I have a model, that trained by this algorithm
import seaborn as sns
import matplotlib.pyplot as plt
from tensorflow import keras
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Conv2D, MaxPool2D, Flatten, Dropout
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras import layers
from sklearn.metrics import classification_report, confusion_matrix
import tensorflow as tf
import cv2
import os
import numpy as np
from resources.Globals import *
labels = ['houses', 'other']
img_size = 400
def get_data(data_dir):
data = []
for label in labels:
path = os.path.join(data_dir, label)
class_num = labels.index(label)
for img in os.listdir(path):
try:
img_arr = cv2.imread(os.path.join(path, img))[..., ::-1] # Convert BGR to RGB format
resized_arr = cv2.resize(img_arr, (img_size, img_size)) # Reshaping images to preferred size
data.append([resized_arr, class_num])
except Exception as e:
print(e)
return np.array(data, dtype="object")
def main():
train = get_data('E:/Projects/Pycharm Projects/sapper/files/Neural_networks/Train')
val = get_data('E:/Projects/Pycharm Projects/sapper/files/Neural_networks/Test')
# Visualize the data
l = []
for i in train:
if i[1] != 0:
l.append("houses")
else:
l.append("other")
sns.set_style('darkgrid')
sns.countplot(l)
# House
plt.figure(figsize=(5, 5))
plt.imshow(train[1][0])
plt.title(labels[train[0][1]])
# Other
plt.figure(figsize=(5, 5))
plt.imshow(train[-1][0])
plt.title(labels[train[-1][1]])
# Data Preprocessing
x_train = []
y_train = []
x_val = []
y_val = []
for feature, label in train:
x_train.append(feature)
y_train.append(label)
for feature, label in val:
x_val.append(feature)
y_val.append(label)
# Normalize the data
x_train = np.array(x_train) / 255
x_val = np.array(x_val) / 255
x_train.reshape(-1, img_size, img_size, 1)
y_train = np.array(y_train)
x_val.reshape(-1, img_size, img_size, 1)
y_val = np.array(y_val)
# Data augmentation
datagen = ImageDataGenerator(
featurewise_center=False, # set input mean to 0 over the dataset
samplewise_center=False, # set each sample mean to 0
featurewise_std_normalization=False, # divide inputs by std of the dataset
samplewise_std_normalization=False, # divide each input by its std
zca_whitening=False, # apply ZCA whitening
rotation_range=30, # randomly rotate images in the range (degrees, 0 to 180)
zoom_range=0.2, # Randomly zoom image
width_shift_range=0.1, # randomly shift images horizontally (fraction of total width)
height_shift_range=0.1, # randomly shift images vertically (fraction of total height)
horizontal_flip=True, # randomly flip images
vertical_flip=False) # randomly flip images
print('Pretrain')
datagen.fit(x_train)
print('After train')
# Define the Model
model = Sequential()
model.add(Conv2D(32, 3, padding="same", activation="relu", input_shape=(img_size, img_size, 3)))
model.add(MaxPool2D())
model.add(Conv2D(64, 3, padding="same", activation="relu"))
model.add(MaxPool2D())
model.add(Dropout(DROPOUT))
model.add(Flatten())
model.add(Dense(128, activation="relu"))
model.add(Dense(2, activation="softmax"))
model.summary()
# Compile the model
opt = keras.optimizers.Adam(lr=0.000001) # Adam as optimizer and SparseCategoricalCrossentropy as the loss function
model.compile(optimizer=opt, loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['accuracy'])
print('Pretrain #2')
# Train model
history = model.fit(x_train, y_train, epochs=AMOUNT_OF_EPOCHS, validation_data=(x_val, y_val))
print('After train #2')
model.save("../../files/Neural_networks/model/training_3")
print('Model saved')
# Evaluating the result
acc = history.history['accuracy']
val_acc = history.history['val_accuracy']
loss = history.history['loss']
val_loss = history.history['val_loss']
epochs_range = range(1, AMOUNT_OF_EPOCHS + 1)
plt.figure(figsize=(15, 15))
plt.subplot(2, 2, 1)
plt.plot(epochs_range, acc, label='Training Accuracy')
plt.plot(epochs_range, val_acc, label='Validation Accuracy')
plt.legend(loc='lower right')
plt.title('Training and Validation Accuracy')
plt.subplot(2, 2, 2)
plt.plot(epochs_range, loss, label='Training Loss')
plt.plot(epochs_range, val_loss, label='Validation Loss')
plt.legend(loc='upper right')
plt.title('Training and Validation Loss')
# Precision and accuracy report
predictions = model.predict_classes(x_val)
predictions = predictions.reshape(1, -1)[0]
print(classification_report(y_val, predictions, target_names=['Rugby (Class 0)', 'Soccer (Class 1)']))
# Show all plots
plt.show()
if __name__ == '__main__':
main()
Also I have code, that should predict using my model
import cv2
import tensorflow as tf
import numpy as np
from keras_preprocessing.image import ImageDataGenerator
CATEGORIES = ['houses', 'other']
def prepare(filepath):
IMG_SIZE = 400 # 50 in txt-based
img_array = cv2.imread(filepath, cv2.IMREAD_GRAYSCALE)
new_array = cv2.resize(img_array, (IMG_SIZE, IMG_SIZE))
new_array = np.array(new_array) / 255
return new_array.reshape(-1, IMG_SIZE, IMG_SIZE, 1)
model = tf.keras.models.load_model("../../files/Neural_networks/model/training_3")
model.summary()
test = prepare("E:/Projects/Pycharm Projects/sapper/files/Neural_networks/Test/houses/5_frontal.jpg")
pred = test
prediction = model.predict([pred])
print('after predict')
print(prediction) # will be a list in a list.
print(CATEGORIES[int(prediction[0][0])])
And in second code in line
prediction = model.predict([pred])
I have this error:
ValueError: Input 0 of layer sequential is incompatible with the layer: expected axis -1 of input shape to have value 3 but received input with shape [None, 400, 400, 1]
Whole error:
Traceback (most recent call last):
File "E:/Projects/Pycharm Projects/sapper/bin/Main/test_2.py", line 25, in <module>
prediction = model.predict([pred])
File "E:\Python versions\Python37\lib\site-packages\tensorflow\python\keras\engine\training.py", line 130, in _method_wrapper
return method(self, *args, **kwargs)
File "E:\Python versions\Python37\lib\site-packages\tensorflow\python\keras\engine\training.py", line 1599, in predict
tmp_batch_outputs = predict_function(iterator)
File "E:\Python versions\Python37\lib\site-packages\tensorflow\python\eager\def_function.py", line 780, in __call__
result = self._call(*args, **kwds)
File "E:\Python versions\Python37\lib\site-packages\tensorflow\python\eager\def_function.py", line 823, in _call
self._initialize(args, kwds, add_initializers_to=initializers)
File "E:\Python versions\Python37\lib\site-packages\tensorflow\python\eager\def_function.py", line 697, in _initialize
*args, **kwds))
File "E:\Python versions\Python37\lib\site-packages\tensorflow\python\eager\function.py", line 2855, in _get_concrete_function_internal_garbage_collected
graph_function, _, _ = self._maybe_define_function(args, kwargs)
File "E:\Python versions\Python37\lib\site-packages\tensorflow\python\eager\function.py", line 3213, in _maybe_define_function
graph_function = self._create_graph_function(args, kwargs)
File "E:\Python versions\Python37\lib\site-packages\tensorflow\python\eager\function.py", line 3075, in _create_graph_function
capture_by_value=self._capture_by_value),
File "E:\Python versions\Python37\lib\site-packages\tensorflow\python\framework\func_graph.py", line 986, in func_graph_from_py_func
func_outputs = python_func(*func_args, **func_kwargs)
File "E:\Python versions\Python37\lib\site-packages\tensorflow\python\eager\def_function.py", line 600, in wrapped_fn
return weak_wrapped_fn().__wrapped__(*args, **kwds)
File "E:\Python versions\Python37\lib\site-packages\tensorflow\python\framework\func_graph.py", line 973, in wrapper
raise e.ag_error_metadata.to_exception(e)
ValueError: in user code:
E:\Python versions\Python37\lib\site-packages\tensorflow\python\keras\engine\training.py:1462 predict_function *
return step_function(self, iterator)
E:\Python versions\Python37\lib\site-packages\tensorflow\python\keras\engine\training.py:1452 step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
E:\Python versions\Python37\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:1211 run
return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
E:\Python versions\Python37\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:2585 call_for_each_replica
return self._call_for_each_replica(fn, args, kwargs)
E:\Python versions\Python37\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:2945 _call_for_each_replica
return fn(*args, **kwargs)
E:\Python versions\Python37\lib\site-packages\tensorflow\python\keras\engine\training.py:1445 run_step **
outputs = model.predict_step(data)
E:\Python versions\Python37\lib\site-packages\tensorflow\python\keras\engine\training.py:1418 predict_step
return self(x, training=False)
E:\Python versions\Python37\lib\site-packages\tensorflow\python\keras\engine\base_layer.py:976 __call__
self.name)
E:\Python versions\Python37\lib\site-packages\tensorflow\python\keras\engine\input_spec.py:216 assert_input_compatibility
' but received input with shape ' + str(shape))
ValueError: Input 0 of layer sequential is incompatible with the layer: expected axis -1 of input shape to have value 3 but received input with shape [None, 400, 400, 1]
How I should solve it?
There is a problem with your input shape obviously.
The architecture of your model is as below. So the input shape should be (1,400,400,3).
In the function prepare that feeds the data to the network, the output shape is (400,400,1). And that is why you get an error.
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d (Conv2D) (None, 400, 400, 32) 896
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 200, 200, 32) 0
_________________________________________________________________
conv2d_1 (Conv2D) (None, 200, 200, 64) 18496
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 100, 100, 64) 0
_________________________________________________________________
dropout (Dropout) (None, 100, 100, 64) 0
_________________________________________________________________
flatten (Flatten) (None, 640000) 0
_________________________________________________________________
dense (Dense) (None, 128) 81920128
_________________________________________________________________
dense_1 (Dense) (None, 2) 258
=================================================================
Total params: 81,939,778
Trainable params: 81,939,778
Non-trainable params: 0
Start by removing the CV2.IMREAD_GRAYSCALE argument because cv2.imread won't load the three RGB channels of the image and the size would only be (IMG_SIZE,IMG_SIZE).
`
a = cv2.imread('Downloads/21.png')
a.shape
(652, 1366,3)
a = cv2.imread('Downloads/21.png',cv2.IMREAD_GRAYSCALE)
a.shape
(652, 1366)
`
Then reshape the loaded image to (1,IMG_SIZE,IMG_SIZE,3)
I tried to run the model on np.zeros(1,IMG_SIZE,IMG_SIZE,3) and it works well.

Categories