Deep Learning Tensorflow reshape is not working? - python

I try to get a prediction out of a trained model but i need to reshape it for tensorflow but it gives me this error all the time:
ValueError: in user code:
WARNING:tensorflow:Model was constructed with shape (None, 100, 100, 3) for input KerasTensor(type_spec=TensorSpec(shape=(None, 100, 100, 3), dtype=tf.float32, name='conv2d_input'), name='conv2d_input', description="created by layer 'conv2d_input'"), but it was called on an input with incompatible shape (None, 100, 100, 1).
Traceback (most recent call last):
File "c:\Dev\PM_AI\partthree.py", line 16, in <module>
prediction = model.predict([prepare("wurmbefall.jpg")])
File "C:\Users\danie\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\keras\utils\traceback_utils.py", line 67, in error_handler
raise e.with_traceback(filtered_tb) from None
File "C:\Users\danie\AppData\Local\Temp\__autograph_generated_filenl1hl3g5.py", line 15, in tf__predict_function
retval_ = ag__.converted_call(ag__.ld(step_function), (ag__.ld(self), ag__.ld(iterator)), None, fscope)
ValueError: in user code:
File "C:\Users\danie\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\keras\engine\training.py", line 1845, in predict_function *
return step_function(self, iterator)
File "C:\Users\danie\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\keras\engine\training.py", line 1834, in step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "C:\Users\danie\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\keras\engine\training.py", line 1823, in run_step **
outputs = model.predict_step(data)
File "C:\Users\danie\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\keras\engine\training.py", line 1791, in predict_step
return self(x, training=False)
File "C:\Users\danie\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\keras\utils\traceback_utils.py", line 67, in error_handler
raise e.with_traceback(filtered_tb) from None
File "C:\Users\danie\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\keras\engine\input_spec.py", line 248, in assert_input_compatibility
raise ValueError(
ValueError: Exception encountered when calling layer "sequential" (type Sequential).
Input 0 of layer "conv2d" is incompatible with the layer: expected axis -1 of input shape to have value 3, but received input with shape (None, 100, 100, 1)
Call arguments received by layer "sequential" (type Sequential):
• inputs=('tf.Tensor(shape=(None, 100, 100, 1), dtype=uint8)',)
• training=False
• mask=None
MY Code: Part One
import numpy as np
import matplotlib.pyplot as plt
import os
import random
import pickle
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Activation, Flatten, Conv2D, MaxPooling2D
from tqdm import tqdm
DATADIR = 'C:\Dev\PM_AI\FlowerImages'
CATEGORIES = ['Apfelschorf', 'Blattlaeuse', 'echterMehltau', 'falscherMehltau', 'Grauschimmel', 'KrautBraunFaeule', 'Raupen', 'Rostpilze']
training_data = []
def create_training_data():
for category in CATEGORIES:
path = os.path.join(DATADIR, category) #Path to images
class_num = CATEGORIES.index(category)
for img in tqdm(os.listdir(path)):
try:
img_array = cv2.imread(os.path.join(path,img))
new_array = cv2.resize(img_array, (IMG_Size, IMG_Size))
training_data.append([new_array, class_num])
except Exception as e:
pass
IMG_Size = 100
create_training_data()
print(len(training_data))
random.shuffle(training_data)
for sample in training_data[:10]:
print(sample[1])
X = []
y = []
for features, label in training_data:
X.append(features)
y.append(label)
X = np.array(X).reshape(-1, IMG_Size, IMG_Size, 3)
pickle_out = open("X.pickle", "wb")
pickle.dump(X, pickle_out)
pickle_out.close()
pickle_out = open("y.pickle", "wb")
pickle.dump(y, pickle_out)
pickle_out.close()
Part Two:
import tensorflow as tf
import numpy as np
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Activation, Flatten, Conv2D, MaxPooling2D
from tensorflow.keras.callbacks import TensorBoard
NAME = "PflanzenApp"
X = pickle.load(open("X.pickle", "rb"))
y = pickle.load(open("Y.pickle", "rb"))
y = np.array(y)
X = np.array(X)
X = X/255.0
model = Sequential()
model.add(Conv2D(64, (3,3), input_shape = X.shape[1:]))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Conv2D(64, (3,3)))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Flatten())
model.add(Dense(64))
model.add(Dense(1))
model.add(Activation('sigmoid'))
tensorboard = TensorBoard(log_dir="logs/{}".format(NAME))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(tf.expand_dims(X, axis=-1), y, batch_size=10, epochs=5, validation_split=0.1, callbacks=[tensorboard])
model.save('PflanzenApp.model')
Part Three:
import tensorflow as tf
import numpy as np
CATEGORIES = ['Apfelschorf', 'Blattlaeuse', 'echterMehltau', 'falscherMehltau', 'Grauschimmel', 'KrautBraunFaeule', 'Raupen', 'Rostpilze']
def prepare(filepath):
IMG_Size = 100
img_array = cv2.imread(filepath)
new_array = cv2.resize(img_array, (IMG_Size, IMG_Size))
reshaped_array = np.array(new_array).reshape(-1, IMG_Size, IMG_Size, 1)
return reshaped_array
model = tf.keras.models.load_model("PflanzenApp.model")
prediction = model.predict([prepare("wurmbefall.jpg")])
print(prediction)
The Error is in Part Three. I already tried to find the error why it says shape (None, 100, 100,1) if the none is the error but i couldnt find anything. Has somebody an Idea?

As lejlot explains in the comments above, the model expects colour pictures not greyscale and thus [100,100,3] not [100,100,1].

Related

Tensoflow: Input 0 of layer "conv1d_17" is incompatible with the layer: expected min_ndim=3, found ndim=2. Full shape received: (None, 16)

Although I could find similar questions, I have not been able to fix this issue.
I am trying to classify text, but got this error:
Input 0 of layer "conv1d_17" is incompatible with the layer: expected min_ndim=3, found ndim=2. Full shape received: (None, 16)
Blockquote
# Training a Text Classification Model using a Convolutional Layer
from sklearn.model_selection import train_test_split
import pandas as pd
import numpy as np
df = pd.read_csv("Financeiro_Teste_V3.csv")
df.head(3)
df.info()
df = df.dropna()
X = df.loc[:, "HISTÓRICO"]
X
y = df.loc[:, ['PROCESSO']]
y.head(7)
training_sentences, testing_sentences, training_labels, testing_labels = train_test_split(X, y, random_state=0, train_size = .75)
training_sentences = training_sentences.to_numpy()
testing_sentences = testing_sentences.to_numpy()
# testing_sentences
#for x in testing_sentences:
# print(type(x), x)
## Data preprocessing
import numpy as np
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences
vocab_size = 10000
max_length = 120
trunc_type='post'
padding_type='post'
oov_tok = "<OOV>"
# Initialize the Tokenizer class
tokenizer = Tokenizer(num_words=vocab_size, oov_token=oov_tok)
# Generate the word index dictionary
tokenizer.fit_on_texts(training_sentences)
word_index = tokenizer.word_index
# Generate and pad the training sequences
training_sequences = tokenizer.texts_to_sequences(training_sentences)
training_padded = pad_sequences(training_sequences, maxlen=max_length, padding=padding_type, truncating=trunc_type)
# Generate and pad the testing sequences
testing_sequences = tokenizer.texts_to_sequences(testing_sentences)
testing_padded = pad_sequences(testing_sequences, maxlen=max_length, padding=padding_type, truncating=trunc_type)
# Convert the labels lists into numpy arrays
training_labels = np.array(training_labels)
testing_labels = np.array(testing_labels)
## Build and Compile the Model
import tensorflow as tf
# Parameters
embedding_dim = 16
filters = 128
kernel_size = 1
dense_dim = 6
# Model Definition with Conv1D
model_conv = tf.keras.Sequential([
tf.keras.layers.Embedding(vocab_size, embedding_dim, input_length=max_length),
tf.keras.layers.Conv1D(filters, kernel_size, activation='relu'),
tf.keras.layers.GlobalMaxPooling1D(),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dense(9, activation='softmax')
])
# Set the training parameters
model_conv.compile(loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),optimizer='adam',metrics=['accuracy'])
# Print the model summary
model_conv.summary()
## Train the Model
NUM_EPOCHS = 20
# Train the model
history_conv = model_conv.fit(training_padded, training_labels, epochs=NUM_EPOCHS, validation_data=(testing_padded, testing_labels))
import matplotlib.pyplot as plt
# Plot Utility
def plot_graphs(history, string):
plt.plot(history.history[string])
plt.plot(history.history['val_'+string])
plt.xlabel("Epochs")
plt.ylabel(string)
plt.legend([string, 'val_'+string])
plt.show()
# Plot the accuracy and loss history
plot_graphs(history_conv, 'accuracy')
plot_graphs(history_conv, 'loss')
model_conv.predict(testing_sentences)
WARNING:tensorflow:Model was constructed with shape (None, 120) for input KerasTensor(type_spec=TensorSpec(shape=(None, 120), dtype=tf.float32, name='embedding_3_input'), name='embedding_3_input', description="created by layer 'embedding_3_input'"), but it was called on an input with incompatible shape (None,).
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-69-98046d2f0914> in <module>
----> 1 model_conv.predict(testing_sentences)
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 1801, in predict_function *
return step_function(self, iterator)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1790, 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 1783, in run_step **
outputs = model.predict_step(data)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1751, 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 228, in assert_input_compatibility
raise ValueError(f'Input {input_index} of layer "{layer_name}" '
ValueError: Exception encountered when calling layer "sequential_3" (type Sequential).
Input 0 of layer "conv1d_3" is incompatible with the layer: expected min_ndim=3, found ndim=2. Full shape received: (None, 16)

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.

CNN prediction using tensorflow

I'm quite new to python and tensorflow, but already managed to build, train and validate a CNN with my own database of images saved as tf.records.
Now I want the model to read in a single picture and predict in real-time. Therefore, I wanted to modify my validation script by getting rid of the parser (which decoded my images saved as tf.records) and didn't batch the input images, since I only want to predict one. Somehow I always get the following Error:
TypeError: Value passed to parameter 'input' has DataType uint8 not in list of allowed values: float16, bfloat16, float32, float64
I took a closer look at the script I used to create the tf.records and compared them to the parser I used in the scripts for training and validation, but wasn't able to find the mistake.
I would be thankful, if you could help me to find the mistake or show me an easier way to predict the classes with an already trained CNN.
import tensorflow as tf
import cv2
num_classes = 2
crop_top = 5
crop_bottom = 10
crop_sides = 5
img_size_height = 80
img_size_width = 100
model_dir = "./2cv_128fc"
def load_image():
img = cv2.imread('./dir_pred_img/img_2.jpg')
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img = cv2.resize(img, (img_size_width + (2 * crop_sides), img_size_height + crop_top + crop_bottom),
interpolation=cv2.INTER_CUBIC)
img = img[crop_top:(img_size_height + crop_top), crop_sides:(img_size_width + crop_sides)]
features = {'image': img}
return features
def conv_nn(input_layer):
conv_1 = tf.layers.conv2d(inputs=input_layer, name='conv_layer_1', filters=32, kernel_size=3, padding='same',
activation=tf.nn.relu)
pool_1 = tf.layers.max_pooling2d(inputs=conv_1, pool_size=2, strides=2)
conv_2 = tf.layers.conv2d(inputs=pool_1, name='conv_layer_2', filters=32, kernel_size=3, padding='same',
activation=tf.nn.relu)
pool_2 = tf.layers.max_pooling2d(inputs=conv_2, pool_size=2, strides=2)
flatten = tf.contrib.layers.flatten(pool_2)
fc_layer = tf.layers.dense(inputs=flatten, name='fully_connected_layer', units=128, activation=tf.nn.relu)
fc_layer = tf.layers.dropout(fc_layer, rate=0.5, noise_shape=None, seed=None)
output_layer = tf.layers.dense(inputs=fc_layer, name='output_layer', units=num_classes)
return output_layer
def model_fn(features):
input_layer = features["image"]
input_layer = tf.identity(input_layer, name="input_tensor")
input_layer = tf.reshape(input_layer, [-1, img_size_height, img_size_width, 1]) # 1.tensor 2.shape
input_layer = tf.identity(input_layer, name="input_tensor_reshaped")
logits = conv_nn(input_layer)
pred = tf.nn.softmax(logits=logits)
return pred
model = tf.estimator.Estimator(model_fn=model_fn, model_dir=model_dir)
prediction = list(model.predict(input_fn=load_image))
print(prediction[0])
full error message:
WARNING:tensorflow:Input graph does not use tf.data.Dataset or contain a QueueRunner. That means predict yields forever. This is probably a mistake.
Traceback (most recent call last):
File "C:/Users/Dell/PycharmProjects/create_data/pred_img.py", line 54, in <module>
prediction = list(model.predict(input_fn=load_image))
File "C:\Users\Dell\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\estimator\estimator.py", line 577, in predict
features, None, model_fn_lib.ModeKeys.PREDICT, self.config)
File "C:\Users\Dell\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\estimator\estimator.py", line 1195, in _call_model_fn
model_fn_results = self._model_fn(features=features, **kwargs)
File "C:/Users/Dell/PycharmProjects/create_data/pred_img.py", line 47, in model_fn
logits = conv_nn(input_layer)
File "C:/Users/Dell/PycharmProjects/create_data/pred_img.py", line 27, in conv_nn
activation=tf.nn.relu)
File "C:\Users\Dell\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\layers\convolutional.py", line 417, in conv2d
return layer.apply(inputs)
File "C:\Users\Dell\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\keras\engine\base_layer.py", line 817, in apply
return self.__call__(inputs, *args, **kwargs)
File "C:\Users\Dell\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\layers\base.py", line 374, in __call__
outputs = super(Layer, self).__call__(inputs, *args, **kwargs)
File "C:\Users\Dell\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\keras\engine\base_layer.py", line 757, in __call__
outputs = self.call(inputs, *args, **kwargs)
File "C:\Users\Dell\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\keras\layers\convolutional.py", line 194, in call
outputs = self._convolution_op(inputs, self.kernel)
File "C:\Users\Dell\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\ops\nn_ops.py", line 868, in __call__
return self.conv_op(inp, filter)
File "C:\Users\Dell\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\ops\nn_ops.py", line 520, in __call__
return self.call(inp, filter)
File "C:\Users\Dell\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\ops\nn_ops.py", line 204, in __call__
name=self.name)
File "C:\Users\Dell\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\ops\gen_nn_ops.py", line 1043, in conv2d
data_format=data_format, dilations=dilations, name=name)
File "C:\Users\Dell\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 609, in _apply_op_helper
param_name=input_name)
File "C:\Users\Dell\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 60, in _SatisfiesTypeConstraint
", ".join(dtypes.as_dtype(x).name for x in allowed_list)))
TypeError: Value passed to parameter 'input' has DataType uint8 not in list of allowed values: float16, bfloat16, float32, float64
The following code descirbes how you can implement this with keras.
import keras
from keras.layers import Input, Convolution2D, MaxPooling2D, Cropping2D, Dense, Flatten, Dropout
from keras.preprocessing import image
from keras.models import Model
from keras.optimizers import Adam
import cv2
import numpy as np
# parameters
num_classes = 2
crop_top = 5
crop_bottom = 10
crop_sides = 5
img_size_height = 80
img_size_width = 100
channels = 3
input_shape = (img_size_height, img_size_width, channels)
activation = 'relu'
learning_rate = 0.0001
if num_classes == 2:
loss = 'binary_crossentropy'
else:
loss = 'categorical_crossentropy'
test_image = image.load_img('./data/img.png')
test_image = image.img_to_array(test_image)
input_shape = test_image.shape
def model(input_shape=input_shape):
inputs = Input(shape=input_shape)
# cropping=((pixels_from_top, pixels_from_bottom), (pixels_from_left, pixels_from_right))
cropping = Cropping2D(cropping=((crop_top, crop_bottom), (crop_sides, crop_sides)))(inputs)
conv_1 = Convolution2D(32, kernel_size=(3, 3), padding='same', activation=activation)(cropping)
pool_1 = MaxPooling2D(pool_size=(2, 2), strides=(2, 2))(conv_1)
conv_2 = Convolution2D(32, kernel_size=(3, 3), padding='same', activation=activation)(pool_1)
pool_2 = MaxPooling2D(pool_size=(2, 2), strides=(2, 2))(conv_2)
flatten = Flatten()(pool_2)
dense_1 = Dense(128, activation=activation, name='fully_connected_layer')(flatten)
dropout = Dropout(0.5)(dense_1)
outputs = Dense(num_classes, activation='softmax')(dropout)
model = Model(inputs=inputs, outputs=outputs)
adam = Adam(lr=learning_rate)
model.compile(optimizer=adam, loss=loss, metrics=['acc', 'mse', 'mae'])
model.summary()
return model
test_image = np.expand_dims(test_image, axis=0)
model = model(input_shape)
# note that without training we will only get random results
prediction = model.predict_on_batch(test_image)
print(prediction)
I found a solution.
I added following line to the end of def load_image() and returned dataset instead of feautures.
dataset = tf.data.Dataset.from_tensors(features)
return dataset

Categories