I have a numpy array of features, four features:
array([[ 4.34027783e+01, -6.24961333e+00, 2.71744622e+04,
1.75683192e+00],
[ 4.34011450e+01, -5.97251833e+00, 9.95175315e+02,
2.91536305e+00],
[ 4.29139867e+01, -8.01821833e+00, 1.33682360e+03,
2.40589737e+03],
...,
[ 4.28848133e+01, -8.62725000e+00, 1.56982904e+03,
2.36951067e+03],
[ 4.29183150e+01, -8.24824333e+00, 2.92462317e+03,
2.89438919e+03],
[ 4.34007467e+01, -5.96473500e+00, 1.32260525e+03,
4.77600491e+00]])
The labels is a dataframe with this type of values:
0 -108
1 -104
2 -92
3 -86
4 2147483647
...
189223 -95
189224 -101
189225 -106
189226 -94
189227 -109
Name: rssi, Length: 189228, dtype: int64
The model I've used is this one:
# build a model
model = Sequential()
model.add(Dense(16, input_shape=(X.shape[1],), activation='relu')) # input shape is (features,)
model.add(Dense(3, activation='sigmoid')) #softmax
model.summary()
# compile the model
model.compile(optimizer='adam',loss = keras.losses.SparseCategoricalCrossentropy(from_logits=True), metrics=['accuracy'])
import keras
from keras.callbacks import EarlyStopping
# early stopping callback
# This callback will stop the training when there is no improvement in
# the validation loss for 10 consecutive epochs.
es = keras.callbacks.EarlyStopping(monitor='val_loss',
mode='min',
patience=10,
restore_best_weights=True) # important - otherwise you just return the last weigths...
# now we just update our model fit call
history = model.fit(X, np.array(y), callbacks=[es], #dummy_y
epochs=8000000, # you can set this to a big number!
batch_size=10,
shuffle=True,
validation_split=0.2,
verbose=1)
However, I get this error:
Node: 'sparse_categorical_crossentropy/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits'
Received a label value of -116 which is outside the valid range of [0, 3). Label values: -97 -106 -116 -104 -103 -113 -112 -105 -106 -103
[[{{node sparse_categorical_crossentropy/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits}}]] [Op:__inference_train_function_6644]
I don't know how to modify my code to fit it with the input data.
Any clue?
Related
I am trying to build a model and train it and predict my trained model and see its accuracy and generate a classification report.
I do not understand why is my no accident precision is zero. Whereas I provided 286 images for accident and 3249 for no accident. I was hoping that the model will have high precision for no accident but its opposite, could someone please tell me what could be the issue?
Does this happen because of the very unbiased dataset? or the model I am using is not good? Could someone please post your opinion?
code
X = np.array(X)
y = np.array(y)
X.shape
(3535, 224, 224, 3)
y.shape
(3535,)
np.unique(y)
array(['accident', 'noaccident'], dtype=object)
Frequency of unique values of the said array:
[['accident' 'noaccident']
[286 3249]]
lb = LabelBinarizer()
labels = lb.fit_transform(y)
np.unique(labels)
array([0, 1]) ---> two classes are there
(trainX, testX, trainY, testY) = train_test_split(X, labels,
test_size=0.20, stratify=labels, random_state=42)
trainAug = ImageDataGenerator(
rotation_range=30,
zoom_range=0.15,
width_shift_range=0.2,
height_shift_range=0.2,
shear_range=0.15,
horizontal_flip=True,
fill_mode="nearest")
valAug = ImageDataGenerator()
mean = np.array([123.68, 116.779, 103.939], dtype="float32")
trainAug.mean = mean
valAug.mean = mean
baseModel = ResNet50(weights="imagenet", include_top=False,
input_tensor=Input(shape=(224, 224, 3)))
headModel = baseModel.output
headModel = AveragePooling2D(pool_size=(7, 7))(headModel)
headModel = Flatten(name="flatten")(headModel)
headModel = Dense(512, activation="relu")(headModel)
headModel = Dropout(0.5)(headModel)
headModel = Dense(len(lb.classes_), activation="softmax")(headModel)
model = Model(inputs=baseModel.input, outputs=headModel)
for layer in baseModel.layers:
layer.trainable = False
print("[INFO] compiling model...")
opt = SGD(lr=1e-4, momentum=0.9, decay=1e-4 / 50)
model.compile(loss="categorical_crossentropy", optimizer=opt,
metrics=["accuracy"])
print("[INFO] training head...")
H = model.fit(
x=trainAug.flow(trainX, trainY, batch_size=32),
steps_per_epoch=len(trainX) // 32,
validation_data=valAug.flow(testX, testY),
validation_steps=len(testX) // 32,
epochs= 50)
print("[INFO] evaluating network...")
predictions = model.predict(x=testX.astype("float32"), batch_size=32)
print(classification_report(testY.argmax(axis=1),
predictions.argmax(axis=1), target_names=lb.classes_))
Output:
precision recall f1-score support
accident 1.00 0.57 0.72 707
noaccident 0.00 0.00 0.00 0
accuracy 0.57 707
macro avg 0.50 0.28 0.36 707
weighted avg 1.00 0.57 0.72 707
Try using -
print(classification_report(testY, predictions.argmax(axis=1), target_names=lb.classes_))
textY is an already (n,1) shaped vector with 0,1 classes and will always give same value for testY.argmax(axis=1), which is equal to 0. That's why your first class has a precision of 1 while the second has a precision of 0. Because you have predicted all 0s correctly.
predictions.argmax(axis=1) is a (n,2) shaped vector, has probability values for class 0 and class 1. You will have to convert it to its classes by taking the argmax so don't change that.
I am working on a dataset which imbalanced. I am trying to build the model with the help of class_weights. So on what basis can, I determine the class_weights.
The labels and its count are as below:
label Count
2 47213
3 2096
4 2021
1 737
0 176
So what values should I give for the class_weight variable:
model.fit(X_train, Y_train, nb_epoch=5, batch_size=32, class_weight=class_weight)
You can employ compute_class_weight from sklearn for that.
from sklearn.utils import compute_class_weight
class_weights = compute_class_weight("balanced", np.unique(Y_train), Y_train)
I am a total beginner and trying to implement image classifier using CIFAR 10 data set using Keras, i used the following code here, i learnt how it works and
I tried this small snippet of code for learning to implement CIFAR 10 but its not working, its not giving any error but the process doesn't start at all. I don't know what am i missing here.
'''
#Train a simple deep CNN on the CIFAR10 small images dataset.
It gets to 75% validation accuracy in 25 epochs, and 79% after 50 epochs.
(it's still underfitting at that point, though).
'''
from __future__ import print_function
import keras
from keras.datasets import cifar10
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import Conv2D, MaxPooling2D
import os
batch_size = 32
num_classes = 10
epochs = 100
data_augmentation = True
num_predictions = 20
save_dir = os.path.join(os.getcwd(), 'saved_models')
model_name = 'keras_cifar10_trained_model.h5'
# The data, split between train and test sets:
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
print('x_train shape:', x_train.shape)
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')
# Convert class vectors to binary class matrices.
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)
model = Sequential()
model.add(Conv2D(32, (3, 3), padding='same',
input_shape=x_train.shape[1:]))
model.add(Activation('relu'))
model.add(Conv2D(32, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Conv2D(64, (3, 3), padding='same'))
model.add(Activation('relu'))
model.add(Conv2D(64, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(512))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes))
model.add(Activation('softmax'))
# initiate RMSprop optimizer
opt = keras.optimizers.rmsprop(lr=0.0001, decay=1e-6)
# Let's train the model using RMSprop
model.compile(loss='categorical_crossentropy',
optimizer=opt,
metrics=['accuracy'])
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
if not data_augmentation:
print('Not using data augmentation.')
model.fit(x_train, y_train,
batch_size=batch_size,
epochs=epochs,
validation_data=(x_test, y_test),
shuffle=True)
else:
print('Using real-time data augmentation.')
# This will do preprocessing and realtime 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
zca_epsilon=1e-06, # epsilon for ZCA whitening
rotation_range=0, # randomly rotate images in the range (degrees, 0 to 180)
# randomly shift images horizontally (fraction of total width)
width_shift_range=0.1,
# randomly shift images vertically (fraction of total height)
height_shift_range=0.1,
shear_range=0., # set range for random shear
zoom_range=0., # set range for random zoom
channel_shift_range=0., # set range for random channel shifts
# set mode for filling points outside the input boundaries
fill_mode='nearest',
cval=0., # value used for fill_mode = "constant"
horizontal_flip=True, # randomly flip images
vertical_flip=False, # randomly flip images
# set rescaling factor (applied before any other transformation)
rescale=None,
# set function that will be applied on each input
preprocessing_function=None,
# image data format, either "channels_first" or "channels_last"
data_format=None,
# fraction of images reserved for validation (strictly between 0 and 1)
validation_split=0.0)
# Compute quantities required for feature-wise normalization
# (std, mean, and principal components if ZCA whitening is applied).
datagen.fit(x_train)
# Fit the model on the batches generated by datagen.flow().
model.fit_generator(datagen.flow(x_train, y_train,
batch_size=batch_size),
epochs=epochs,
validation_data=(x_test, y_test),
workers=4)
# Save model and weights
if not os.path.isdir(save_dir):
os.makedirs(save_dir)
model_path = os.path.join(save_dir, model_name)
model.save(model_path)
print('Saved trained model at %s ' % model_path)
# Score trained model.
scores = model.evaluate(x_test, y_test, verbose=1)
print('Test loss:', scores[0])
print('Test accuracy:', scores[1])
Response on Python Ide 3.6.8 :
Python 3.6.8 (tags/v3.6.8:3c6b436a57, Dec 24 2018, 00:16:47) [MSC v.1916 64
bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>>
======================== RESTART: D:\TrainTheModel.py ========================
Using TensorFlow backend.
x_train shape: (50000, 32, 32, 3)
50000 train samples
10000 test samples
Using real-time data augmentation.
Epoch 1/100
=============================== RESTART: Shell ===============================
>>>
I fixed your errors. If you using TensorFlow as backend, better use Keras from TensorFlow libraries.
# Train a simple deep CNN on the CIFAR10 small images dataset.
# It gets to 75% validation accuracy in 25 epochs, and 79% after 50 epochs.
# (it's still underfitting at that point, though).
from __future__ import print_function
from tensorflow.keras.utils import to_categorical
from tensorflow.python.keras import optimizers
from tensorflow.keras.datasets import cifar10
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Activation, Flatten
from tensorflow.keras.layers import Conv2D, MaxPooling2D
import os
batch_size = 32
num_classes = 10
epochs = 100
data_augmentation = True
num_predictions = 20
save_dir = os.path.join(os.getcwd(), 'saved_models')
model_name = 'keras_cifar10_trained_model.h5'
# The data, split between train and test sets:
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
print('x_train shape:', x_train.shape)
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')
# Convert class vectors to binary class matrices.
y_train = to_categorical(y_train, num_classes)
y_test = to_categorical(y_test, num_classes)
model = Sequential()
model.add(Conv2D(32, (3, 3), padding='same',
input_shape=x_train.shape[1:]))
model.add(Activation('relu'))
model.add(Conv2D(32, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Conv2D(64, (3, 3), padding='same'))
model.add(Activation('relu'))
model.add(Conv2D(64, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(512))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes))
model.add(Activation('softmax'))
# initiate RMSprop optimizer
opt = optimizers.rmsprop(lr=0.0001, decay=1e-6)
# Let's train the model using RMSprop
model.compile(loss='categorical_crossentropy',
optimizer=opt,
metrics=['accuracy'])
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
if not data_augmentation:
print('Not using data augmentation.')
model.fit(x_train, y_train,
batch_size=batch_size,
epochs=epochs,
validation_data=(x_test, y_test),
shuffle=True)
else:
print('Using real-time data augmentation.')
# This will do preprocessing and realtime 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
zca_epsilon=1e-06, # epsilon for ZCA whitening
rotation_range=0, # randomly rotate images in the range (degrees, 0 to 180)
# randomly shift images horizontally (fraction of total width)
width_shift_range=0.1,
# randomly shift images vertically (fraction of total height)
height_shift_range=0.1,
shear_range=0., # set range for random shear
zoom_range=0., # set range for random zoom
channel_shift_range=0., # set range for random channel shifts
# set mode for filling points outside the input boundaries
fill_mode='nearest',
cval=0., # value used for fill_mode = "constant"
horizontal_flip=True, # randomly flip images
vertical_flip=False, # randomly flip images
# set rescaling factor (applied before any other transformation)
rescale=None,
# set function that will be applied on each input
preprocessing_function=None,
# image data format, either "channels_first" or "channels_last"
data_format=None,
# fraction of images reserved for validation (strictly between 0 and 1)
validation_split=0.0)
# Compute quantities required for feature-wise normalization
# (std, mean, and principal components if ZCA whitening is applied).
datagen.fit(x_train)
# Fit the model on the batches generated by datagen.flow().
model.fit_generator(datagen.flow(x_train, y_train,
batch_size=batch_size),
epochs=epochs,
validation_data=(x_test, y_test),
workers=4)
# Save model and weights
if not os.path.isdir(save_dir):
os.makedirs(save_dir)
model_path = os.path.join(save_dir, model_name)
model.save(model_path)
print('Saved trained model at %s ' % model_path)
# Score trained model.
scores = model.evaluate(x_test, y_test, verbose=1)
print('Test loss:', scores[0])
print('Test accuracy:', scores[1])
Response (For test I use only 2 epochs):
x_train shape: (50000, 32, 32, 3)
50000 train samples
10000 test samples
Using real-time data augmentation.
Epoch 1/2
2019-02-09 16:26:13.219359: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1432] Found device 0 with properties:
name: GeForce GTX 750 major: 5 minor: 0 memoryClockRate(GHz): 1.137
pciBusID: 0000:04:00.0
totalMemory: 1.95GiB freeMemory: 1.32GiB
2019-02-09 16:26:13.219405: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1511] Adding visible gpu devices: 0
2019-02-09 16:26:13.550797: I tensorflow/core/common_runtime/gpu/gpu_device.cc:982] Device interconnect StreamExecutor with strength 1 edge matrix:
2019-02-09 16:26:13.550848: I tensorflow/core/common_runtime/gpu/gpu_device.cc:988] 0
2019-02-09 16:26:13.550865: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1001] 0: N
2019-02-09 16:26:13.551055: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1115] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 1059 MB memory) -> physical GPU (device: 0, name: GeForce GTX 750, pci bus id: 0000:04:00.0, compute capability: 5.0)
1563/1563 [==============================] - 44s 28ms/step - loss: 1.8483 - acc: 0.3220 - val_loss: 1.5551 - val_acc: 0.4414
Epoch 2/2
1563/1563 [==============================] - 42s 27ms/step - loss: 1.5651 - acc: 0.4263 - val_loss: 1.3814 - val_acc: 0.5065
Saved trained model at /home/mid/Documents/saved_models/keras_cifar10_trained_model.h5
10000/10000 [==============================] - 2s 189us/step
Test loss: 1.3814242065429687
Test accuracy: 0.5065
I'm trying to use Eli5 for explaining an LSTM keras model for time series prediction. The keras model receives as input an array with shape (nsamples, timesteps, nfeatures).
This is my code:
def baseline_model():
model = Sequential()
model.add(LSTM(32, input_shape=(X_train.shape[1], X_train.shape[2])))
model.add(Dropout(0.2))
model.add(Dense(1))
model.compile(loss='logcosh', optimizer='adam')
return model
from keras.wrappers.scikit_learn import KerasClassifier, KerasRegressor
import eli5
from eli5.sklearn import PermutationImportance
my_model = KerasRegressor(build_fn= baseline_model, nb_epoch= 30, batch_size= 32, verbose= False)
history = my_model.fit(X_train, y_train)
So far, everything is ok. The problem is when I execute the following line that launchs an error:
# X_train has a shape equal to (nsamples, timesteps, nfeatures) and y_train has a shape (nsamples)
perm = PermutationImportance(my_model, random_state=1).fit(X_train, y_train)
Error:
ValueError Traceback (most recent call last)
in ()
2 d2_train_dataset = X_train.reshape((nsamples, timesteps * features))
3
----> 4 perm = PermutationImportance(my_model, random_state=1).fit(X_train, y_train)
5 #eli5.show_weights(perm, feature_names = X.columns.tolist())
~/anaconda3/lib/python3.6/site-packages/eli5/sklearn/permutation_importance.py in fit(self, X, y, groups, **fit_params)
183 self.estimator_.fit(X, y, **fit_params)
184
--> 185 X = check_array(X)
186
187 if self.cv not in (None, "prefit"):
~/anaconda3/lib/python3.6/site-packages/sklearn/utils/validation.py in check_array(array, accept_sparse, accept_large_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, ensure_min_samples, ensure_min_features, warn_on_dtype, estimator)
568 if not allow_nd and array.ndim >= 3:
569 raise ValueError("Found array with dim %d. %s expected <= 2."
--> 570 % (array.ndim, estimator_name))
571 if force_all_finite:
572 _assert_all_finite(array,
ValueError: Found array with dim 3. Estimator expected <= 2.
What can I do to fix this error? How can I use eli5 with my LSTM Keras Model?
I am new to Keras and Theano, and now trying to implement my own loss function on Keras. But this error showed up. I thought the problem lies in my own loss function, but I have now idea how to fix it. Could someone help me figure this out?
import theano
import theano.tensor as T
def cost_estimation(y_true, y_pred):
for k in range(10):
d=T.log(1+T.exp((int(bool(y_true[k]==min(y_true)))*2-1)*(y_pred[k]-y_true[k])))
cost=cost+d
return d
The keras layers:
model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3),
activation='relu',
input_shape=input_shape))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='relu'))
#loss=keras.losses.categorical_crossentropy,
model.compile(loss='cost_estimation',
optimizer='adam',
metrics=['accuracy'])
model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs,
verbose=1, validation_data=(x_test, y_test))
Here is the error:
---------------------------------------------------------------------------
UnboundLocalError Traceback (most recent call last)
<ipython-input-6-d63101c47c94> in <module>()
130 model.compile(loss='cost_estimation',
131 optimizer='adam',
--> 132 metrics=['accuracy'])
133
134 model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs,
/usr/local/lib/python2.7/dist-packages/keras/models.pyc in compile(self, optimizer, loss, metrics, sample_weight_mode, **kwargs)
764 metrics=metrics,
765 sample_weight_mode=sample_weight_mode,
--> 766 **kwargs)
767 self.optimizer = self.model.optimizer
768 self.loss = self.model.loss
/usr/local/lib/python2.7/dist-packages/keras/engine/training.pyc in compile(self, optimizer, loss, metrics, loss_weights, sample_weight_mode, **kwargs)
738 loss_functions = [losses.get(l) for l in loss]
739 else:
--> 740 loss_function = losses.get(loss)
741 loss_functions = [loss_function for _ in range(len(self.outputs))]
742 self.loss_functions = loss_functions
/usr/local/lib/python2.7/dist-packages/keras/losses.pyc in get(identifier)
88 if isinstance(identifier, six.string_types):
89 identifier = str(identifier)
---> 90 return deserialize(identifier)
91 elif callable(identifier):
92 return identifier
/usr/local/lib/python2.7/dist-packages/keras/losses.pyc in deserialize(name, custom_objects)
80 module_objects=globals(),
81 custom_objects=custom_objects,
---> 82 printable_module_name='loss function')
83
84
/usr/local/lib/python2.7/dist-packages/keras/utils/generic_utils.pyc in deserialize_keras_object(identifier, module_objects, custom_objects, printable_module_name)
155 if fn is None:
156 raise ValueError('Unknown ' + printable_module_name,
--> 157 ':' + class_name)
158 return fn
159 else:
UnboundLocalError: local variable 'class_name' referenced before assignment
This seems to be an issue in keras codebase. It seems that if the a string is passed to the loss parameter this error arises. To fix this, pass
cost_estimation itself to loss, this way you avoid that branch of code.
model.compile(optimizer='rmsprop',
loss=cost_estimation, # not 'cost_estimation'
metrics=['accuracy'])