TypeError: Value passed to parameter 'input' has DataType int64 not in list of allowed values: float16, bfloat16, float32, float64 - mnist

I am trying to run a model and predict a test data from mnist kaggle data set. But i am getting error when trying to predict.What is the reason and solution?
model = tf.keras.Sequential([
tf.keras.layers.Conv2D(32, (3,3), padding='same', activation=tf.nn.relu,
input_shape=(28, 28, 1)),
tf.keras.layers.MaxPooling2D((2, 2), strides=2),
tf.keras.layers.Conv2D(64, (3,3), padding='same', activation=tf.nn.relu),
tf.keras.layers.MaxPooling2D((2, 2), strides=2),
tf.keras.layers.Flatten(input_shape=(28, 28, 1)),
tf.keras.layers.Dense(128, activation=tf.nn.relu),
tf.keras.layers.Dense(10, activation=tf.nn.softmax)
])
test = pd.read_csv("test.csv")
test.head()
CHANNELS = 1
IMAGE_SIZE = 28
IMAGE_WIDTH, IMAGE_HEIGHT = IMAGE_SIZE, IMAGE_SIZE
test = test.values.reshape(-1, IMAGE_WIDTH, IMAGE_HEIGHT, CHANNELS)
predictions = model.predict_classes(test, verbose=1)
TypeError: Value passed to parameter 'input' has DataType int64 not in
list of allowed values: float16, bfloat16, float32, float64

As the TypeError is saying I think the test dataframe contains int values, so you have to change the type to float like this:
test = test.astype('float')
test = test.values.reshape(-1, IMAGE_WIDTH, IMAGE_HEIGHT, CHANNELS)
predictions = model.predict_classes(test, verbose=1)

Related

Loss is always nan

I have been working with CNN model recently. I always get loss nan for this model? How do I solve this?
My Model..
def CNN_Model(inputshape):
model = tf.keras.models.Sequential([
tf.keras.layers.Conv2D(96, kernel_size = (7, 7), strides= 2, activation='relu',kernel_initializer='glorot_uniform',input_shape = inputshape),
tf.keras.layers.MaxPooling2D((3,3), strides=(2,2)),
tf.keras.layers.ZeroPadding2D((2, 2), data_format="channels_last"),
#tf.keras.layers.Lambda(lambda x: tf.image.per_image_standardization(x)),
tf.keras.layers.Conv2D(256,kernel_size = (5, 5), strides= 1, activation='relu'),
tf.keras.layers.MaxPooling2D((3,3), strides=(2,2)),
#tf.keras.layers.Lambda(lambda x: tf.image.per_image_standardization(x)),
tf.keras.layers.Conv2D(384,kernel_size = (3, 3), activation='relu',strides=1),
tf.keras.layers.Conv2D(256, kernel_size = (3, 3), activation='relu',strides=1),
tf.keras.layers.MaxPooling2D((3,3), strides=(2,2)),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(1024, kernel_regularizer=l2(0.0005), activation='relu'),
tf.keras.layers.Dropout(0.5),
tf.keras.layers.Dense(1024),
tf.keras.layers.Dense(40, activation='softmax')
])
return model
My loss function
def contrastive_loss(y_true, y_pred):
'''Contrastive loss from Hadsell-et-al.'06
http://yann.lecun.com/exdb/publis/pdf/hadsell-chopra-lecun-06.pdf
'''
margin = 1
return K.mean(y_true * K.square(y_pred) + (1 - y_true) * K.square(K.maximum(margin - y_pred, 0)))
I tried to to change the layer config but nothing worked. Here

Unable to find Accuracy and the ROC curve of my CNN model

My sample CNN code looks below:
classifier = Sequential()
#1st Conv layer
classifier.add(Convolution2D(64, (9, 9), input_shape=(64, 64, 3), activation='relu'))
classifier.add(MaxPooling2D(pool_size=(4,4)))
#2nd Conv layer
classifier.add(Convolution2D(32, (3, 3), activation='relu'))
classifier.add(MaxPooling2D(pool_size=(2,2)))
classifier.add(Flatten())
classifier.add(Dense(units = 128, activation = 'relu'))
classifier.add(Dropout(0.1))
classifier.add(Dense(units = 128, activation = 'relu'))
classifier.add(Dropout(0.2))
classifier.add(Dense(units = 128, activation = 'relu'))
classifier.add(Dense(units = 2, activation = 'softmax'))
classifier.compile(optimizer = 'adam', loss = 'categorical_crossentropy', metrics = ['accuracy'])
from keras.preprocessing.image import ImageDataGenerator
train_datagen = ImageDataGenerator(rescale = 1./255,
shear_range = 0.2,
zoom_range = 0.2,
horizontal_flip = True)
training_set = train_datagen.flow_from_directory('D:/regionGrowing_MLT/png_orig_imgs/Training',
target_size = (64, 64),
batch_size = 32,
class_mode = 'categorical')
test_datagen = ImageDataGenerator(rescale = 1./255)
test_set = test_datagen.flow_from_directory('D:/regionGrowing_MLT/png_orig_imgs/Test',
target_size = (64, 64),
batch_size = 32,
class_mode = 'categorical'
)
probs=classifier.fit(x = training_set, validation_data = test_set, epochs = 50)
I tried the following line to find the ROC curve, but i get an error message:
predictions = classifier.predict(test_set)
fpr, tpr,threshold = roc_curve(test_set,predictions)
The following error message is displayed:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-52-2ea53b1ba7f1> in <module>
----> 1 fpr, tpr,threshold = roc_curve(test_set,predictions)
ValueError: Expected array-like (array or non-string sequence), got <keras.preprocessing.image.DirectoryIterator object at 0x000002D21D1B61C0>
Any suggestions would be appreciated.
Emm! From the error, I think you have to change keras.processing image object to array. Try this I think this will help you out.
Accuracy
fil_acc_orig = accuracy_score(y_test, predictions.to_array())
ROC Curve
fil_acc_orig = roc_curve(y_test, predictions.to_array())

Keras custom generator: Error when checking input: expected conv2d_1_input to have 4 dimensions, but got array with shape (256, 1)

I have a large dataset (30 000 images, 80kB each), and I'm trying to load it into my model in batches in order to not run of out memory on my GPU. My single image is a 200x200 numpy array, with values either 1 or 0, stored in csv file. So I don't need to resize it or use any image reader as it's already an array. I'm using a custom generator to achieve this:
class My_Custom_Generator(keras.utils.Sequence):
def __init__(self, image_filenames, labels, batch_size):
self.image_filenames = image_filenames
self.labels = labels
self.batch_size = batch_size
def __len__(self):
return (np.ceil(len(self.image_filenames) / float(self.batch_size))).astype(np.int)
def __getitem__(self, idx):
batch_x = self.image_filenames[idx * self.batch_size: (idx + 1) * self.batch_size]
batch_y = self.labels[idx * self.batch_size: (idx + 1) * self.batch_size]
return np.array(batch_x), np.array(batch_y)
My model looks like this:
X_train_filenames = np.load('X_train_filenames.npy')
y_train = np.load('y_train.npy')
X_val_filenames = np.load('X_val_filenames.npy')
y_val = np.load('y_val.npy')
batch_size = 256
my_training_batch_generator = My_Custom_Generator(X_train_filenames, y_train, batch_size)
my_validation_batch_generator = My_Custom_Generator(X_val_filenames, y_val, batch_size)
model = Sequential()
model.add(Conv2D(filters = 32, kernel_size = (3,3),input_shape=(200,200,1)))
#model.add(BatchNormalization(axis=3))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.2))
model.add(Conv2D(64, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.2))
model.add(Conv2D(128, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.15))
model.add(Conv2D(256, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.2))
model.add(Flatten())
model.add(Dense(32))
model.add(Activation('relu'))
model.add(Dense(1))
model.add(Activation('sigmoid'))
model.compile(optimizer="adam", loss='binary_crossentropy', metrics=['accuracy'])
model.summary()
model.fit_generator(generator=my_training_batch_generator,
steps_per_epoch = int(y_train.shape[0] // batch_size),
epochs = 10,
verbose = 1,
validation_data = my_validation_batch_generator,
validation_steps = int(y_val.shape[0] // batch_size))
I get an error:
ValueError: Error when checking input: expected conv2d_1_input to have 4 dimensions, but got array with shape (256, 1)
In my understanding the input should be a 4 dimensional array of shape (None,200,200,1), but I don't know how to achieve it, since I just started learning how to load a dataset in parts and not all at once.

ValueError: A target array with shape (32, 5) was passed for an output of shape (None, 4) while using as loss categorical_crossentropy

I am trying to train a model on a dataset of 40x40 grayscale images and I am getting this error:
ValueError: A target array with shape (32, 5) was passed for an output of shape (None, 4) while using as loss categorical_crossentropy. This loss expects targets to have the same shape as the output.
I don't know where the array of (32, 5) coming from as it should be (32, 4), so I didn't know what to change. Any suggestion?
image_generator = ImageDataGenerator(#rescale = 1/255,
shear_range = 0.3,
zoom_range = 0.1,
rotation_range = 30,
width_shift_range = 0.08,
height_shift_range = 0.08,
horizontal_flip = True,
fill_mode = 'nearest',
)
train_image_generator = image_generator.flow_from_directory('/data1/mypath/generated-images/train',
target_size = (40,40),
color_mode = 'grayscale',
batch_size = 32,
class_mode = 'categorical')
test_image_generator = image_generator.flow_from_directory('/data1/mypath/generated-images/test',
target_size = (40,40),
color_mode = 'grayscale',
batch_size = 32,
class_mode = 'categorical',
shuffle = False)
model = Sequential()
model.add(Conv2D(32, kernel_size=(3,3),input_shape=(40,40, 1), activation='relu', padding='same'))
model.add(BatchNormalization(axis=-1))
model.add(MaxPool2D(pool_size=(2, 2)))
model.add(Conv2D(32, kernel_size=(3,3),activation='relu', padding='same'))
model.add(MaxPool2D(pool_size=(2, 2)))
model.add(Conv2D(64, kernel_size=(3,3), activation='relu', padding='same'))
model.add(BatchNormalization())
model.add(MaxPool2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(512, activation='relu'))
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.4))
model.add(Dense(4))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
early_stopping = EarlyStopping(monitor='val_loss',patience=5)
model.fit_generator(train_image_generator, epochs=150,
validation_data = test_image_generator,
callbacks=[early_stopping]) ```
Check the number of classes in your dataset.
The array (32,5) is coming from the shape of your dataset.
The shape of Y(label), is showing as 5 classes, and you have declared it as 4 in the last output layer as (None, 4). Which means the code is reading 4 classes from the image path provided.

How should I fit the sample data in model combining CNN and LSTM

I have a sample data of page visits of one page for 803 days. I have extracted features from data like mean, median etc and final shape of data is (803, 25). I had taken a train set of 640 and a test set of 160. I am trying to use CNN+LSTM model using Keras. But I am getting an error in model.fit method.
I have tried permute layer and changed input shapes but still not able to fix it.
trainX.shape = (642, 1, 25)
trainY.shape = (642,)
testX.shape = (161, 1, 25)
testY.shape = (161,)
'''python
# Basic layer
model = Sequential()
model.add(TimeDistributed(Convolution2D(filters = 32, kernel_size = (3, 3), strides=1, padding='SAME', input_shape = (642, 25, 1), activation = 'relu')))
model.add(TimeDistributed(Convolution2D(filters = 32, kernel_size = (3, 3), activation = 'relu')))
model.add(TimeDistributed(MaxPooling2D(pool_size = (2, 2))))
model.add(TimeDistributed(Convolution2D(32, 3, 3, activation = 'relu')))
model.add(TimeDistributed(MaxPooling2D(pool_size = (2, 2))))
model.add(TimeDistributed(Flatten()))
model.add(Permute((2, 3), input_shape=(1, 25)))
model.add(LSTM(units=54, return_sequences=True))
# To avoid overfitting
model.add(Dropout(0.2))
# Adding 6 more layers
model.add(LSTM(units=25, return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM(units=50, return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM(units=50, return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM(units=50, return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM(units=50, return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM(units=54))
model.add(Dropout(0.2))
model.add(TimeDistributed(Dense(units = 1, activation='relu', kernel_regularizer=regularizers.l1(0.0001))))
model.add(PReLU(weights=None, alpha_initializer="zero")) # add an advanced activation
model.compile(optimizer = 'adam', loss = customSmapeLoss, metrics=['mae'])
model.fit(trainX, trainY, epochs = 50, batch_size = 32)
predictions = model.predict(testX)
'''
#Runtime Error
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-218-86932db86d0b> in <module>()
42
43 model.compile(optimizer = 'adam', loss = customSmapeLoss, metrics=['mae'])
---> 44 model.fit(trainX, trainY, epochs = 50, batch_size = 32)
Error - IndexError: list index out of range
The input_shape requires a tuple of length 4 when it using TimeDistributed with Conv2D
see https://keras.io/layers/wrappers/
input_shape=(10, 299, 299, 3))
Dont you think your data field is too little? usually, CNN+LSTM is meant for a more complicated task with thousands of sequential images/videos.

Resources