I have implemented a CNN based Classification of image datasets but the problem is it provides a nan value of the ROC_Curve's area.
Here is the coding part,
#Package Initilize
import numpy as np
from sklearn import metrics
import matplotlib.pyplot as plt
import tensorflow as tf
import keras
from keras.preprocessing import image
from keras.models import Sequential
from keras.layers import Convolution2D
from keras.layers import MaxPooling2D
from keras.layers import Flatten
from keras.layers import Dense
from keras.layers import Dropout
train_datagen = image.ImageDataGenerator(
rescale=1/255,
shear_range = 0.3,
zoom_range = 0.3,
horizontal_flip = True,
)
validation_datagen = image.ImageDataGenerator(
rescale = 1/255
)
target_size = (100,100,3)
train = train_datagen.flow_from_directory(
'Dataset/Train',
target_size = target_size[:-1],
batch_size = 32,
class_mode = 'categorical'
)
validation = validation_datagen.flow_from_directory(
'Dataset/Validation',
target_size = target_size[:-1],
batch_size = 32,
class_mode = 'categorical'
)
test = validation_datagen.flow_from_directory(
'Dataset/Test',
target_size = target_size[:-1],
batch_size = 32,
shuffle = False,
class_mode = 'categorical'
)
input_layer = keras.layers.Input(shape=target_size)
#Model Define
conv2d_1 = keras.layers.Conv2D(filters=64, kernel_size=(3,3), strides=1, padding='same',
activation='relu', kernel_initializer='he_normal')(input_layer)
batchnorm_1 = keras.layers.BatchNormalization()(conv2d_1)
maxpool1=keras.layers.MaxPool2D(pool_size=(2,2))(batchnorm_1)
conv2d_2 = keras.layers.Conv2D(filters=32, kernel_size=(3,3), strides=1, padding='same',
activation='relu',kernel_initializer='he_normal')(maxpool1)
batchnorm_2 = keras.layers.BatchNormalization()(conv2d_2)
maxpool2=keras.layers.MaxPool2D(pool_size=(2,2))(batchnorm_2)
flatten = keras.layers.Flatten()(maxpool2)
dense_1 = keras.layers.Dense(256, activation='relu')(flatten)
dense_2 = keras.layers.Dense(n_classes, activation='softmax')(dense_1)
dense_3 = keras.layers.Dense(n_classes, activation='softmax')(dense_2)
model = keras.models.Model(input_layer, dense_3)
#Compile Define
model.compile(optimizer=keras.optimizers.Adam(0.001),
loss='categorical_crossentropy',
metrics=['acc'])
model.summary()
#Fit the model
history = model.fit_generator(generator=train, validation_data=validation,
epochs=2)
#ROC Curve Define
x, y = validation.next()
prediction = model.predict(x)
predict_label1 = np.argmax(prediction, axis=-1)
true_label1 = np.argmax(y, axis=-1)
y = np.array(true_label1)
scores = np.array(predict_label1)
fpr, tpr, thresholds = metrics.roc_curve(y, scores, pos_label=9)
roc_auc = metrics.auc(fpr, tpr)
plt.figure()
lw = 2
plt.plot(fpr, tpr, color='darkorange',
lw=lw, label='ROC curve (area = %0.2f)' % roc_auc)
plt.plot([0, 1], [0, 1], color='navy', lw=lw, linestyle='--')
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('Receiver operating characteristic (ROC)')
plt.legend(loc="lower right")
plt.show()
The problem of ROC_Curve is given in the attached file, please check it.
The doc of sklearn.metrics.roc_curve() states, right at the top:
Note: this implementation is restricted to the binary classification task.
But it seems you have a multi-class model.
You can check this site for options regarding multi-class ROC with sklearn.
Related
I would like to integrate a custom loss function for my LSTM in python. The code shows my approach so far.
How would I best implement the loss function shown in the images? How would I handle the constraint <0?
Thanks for any help!
Code
# Importing the libraries
ep=25 #Epochs
bs=32 #Batch-Size
vs=0.2 #Validation-Split
r=ep+1 #Range
# Importing the training set
dataset_train = pd.read_csv(r'C:\Users\Name\Desktop\Recurrent Neural Networks\JPM_train.csv',delimiter =';')
training_set = dataset_train.iloc[:, 1:2].values
# Feature Scaling
from sklearn.preprocessing import MinMaxScaler
sc = MinMaxScaler(feature_range = (0, 1))
training_set_scaled = sc.fit_transform(training_set)
# Creating a data structure with 60 timesteps and 1 output
X_train = []
y_train = []
X_val=[]
y_val=[]
for i in range(60, 1516):
X_train.append(training_set_scaled[i-60:i, 0])
y_train.append(training_set_scaled[i, 0])
X_train, y_train, X_val, y_val = np.array(X_train), np.array(y_train), np.array(X_val), np.array(y_val)
# Reshaping
X_train = np.reshape(X_train, (X_train.shape[0], X_train.shape[1], 1))
# Importing the Keras libraries and packages
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM
from keras.layers import Dropout
def custom_loss(y_true, y_pred):
if(#HERE):
loss=(predicted_stock_price-real_stock_price)^2
else:
loss=0
return loss
# Initialising the RNN
model = Sequential()
# Adding the first LSTM layer and some Dropout regularisation
model.add(LSTM(units = 50, return_sequences = True, input_shape = (X_train.shape[1], 1)))
model.add(Dropout(0.2))
# Adding a second LSTM layer and some Dropout regularisation
model.add(LSTM(units = 50, return_sequences = True))
model.add(Dropout(0.2))
# Adding a third LSTM layer and some Dropout regularisation
model.add(LSTM(units = 50, return_sequences = True))
model.add(Dropout(0.2))
# Adding a fourth LSTM layer and some Dropout regularisation
model.add(LSTM(units = 50))
model.add(Dropout(0.2))
# Adding the output layer
model.add(Dense(units = 1))
# Compiling the RNN
model.compile(optimizer = 'adam', loss = custom_loss ,metrics=['accuracy'])
# Fitting the RNN to the Training set
history=model.fit(X_train, y_train, epochs = ep, batch_size = bs, validation_split=vs)
# Getting the real stock price of 2017
dataset_test = pd.read_csv(r'C:\Users\Name\Desktop\Recurrent Neural Networks\JPM_test.csv',delimiter =';')
real_stock_price = dataset_test.iloc[:, 1:2].values
dataset_total = pd.concat((dataset_train['Preis'], dataset_test['Preis']), axis = 0)
inputs = dataset_total[len(dataset_total) - len(dataset_test) - 60:].values
inputs = inputs.reshape(-1,1)
inputs = sc.transform(inputs)
X_test = []
for i in range(60, 80):
X_test.append(inputs[i-60:i, 0])
X_test = np.array(X_test)
X_test = np.reshape(X_test, (X_test.shape[0], X_test.shape[1], 1))
predicted_stock_price = model.predict(X_test)
predicted_stock_price = sc.inverse_transform(predicted_stock_price)
history_dict = history.history
print(history_dict.keys())
accuracy = history_dict['accuracy']
validation_accuracy = history_dict['val_accuracy']
loss = history_dict['loss']
validation_loss = history_dict['val_loss']
gs = gridspec.GridSpec(2, 2)
#plt.tight_layout()
#plt.subplots_adjust(hspace=1.0)
fig = plt.figure(figsize=(16,16))
# Visualising the results
ax = plt.subplot(gs[1, :]) # row 1, span all columns
plt.plot(real_stock_price, color = 'red', label = 'Real Google Stock Price')
plt.plot(predicted_stock_price, color = 'blue', label = 'Predicted Google Stock Price')
plt.title('Google Stock Price Prediction')
plt.xlabel('Time')
plt.ylabel('Google Stock Price')
plt.legend()
plt.show()
Only the Custom loss function
def custom_loss(y_true, y_pred):
if(#HERE):
loss=(predicted_stock_price-real_stock_price)^2
else:
loss=0
return loss
Pictures of the targeted loss function
Here is the link to the original text:
https://www.researchgate.net/publication/342094242_Deep_Stock_Predictions
You can use this loss function that calculates the current prediction (t1) minus the previous real_stock_price (t-1) :
def custom_loss(y_true, y_pred):
if((y_true[0]-y_true[1])*(y_pred-y_true[1])):
loss=(y_pred -y_true[0] )^2
else:
loss=0
return loss
I think that the derivatives in the backpropagation will not be affected by this shifting of time.
I'm trying to train a CNN on a set of images. There are 2 folders: training_set and test_set, each containing 2 classes.
They look like this:
training_set/
classA/
img1.png
img2.png
...
classB/
img1.png
img2.png
...
test_set/
classA/
img1.png
img2.png
...
classB/
img1.png
img2.png
...
Code looks like this, where the training set is split into a training and validation set:
import os
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.python.client import device_lib
import numpy as np
import matplotlib.pyplot as plt
print("Num GPUs Available: ", len(tf.config.list_physical_devices('GPU')))
print(device_lib.list_local_devices())
# Set image properties
img_height = 369
img_width = 496
batch_size = 32
# Import data set from directory
train_images = tf.keras.preprocessing.image_dataset_from_directory(
"path_to_training_set",
labels='inferred',
label_mode="binary", # not sure about this one though, as the classes are not called '0' and '1'
class_names = ['classA', 'classB'],
color_mode = 'rgb',
batch_size = batch_size,
image_size = (img_height, img_width),
shuffle = True,
seed = 123,
validation_split = 0.2,
subset = "training"
)
val_images = tf.keras.preprocessing.image_dataset_from_directory(
"path_to_training_set",
labels='inferred',
label_mode="binary", # not sure about this one though, as the classes are not called '0' and '1'
class_names = ['classA', 'classB'],
color_mode = 'rgb',
batch_size = batch_size,
image_size = (img_height, img_width),
shuffle = True,
seed = 123,
validation_split = 0.2,
subset = "validation"
)
Then:
from matplotlib import pyplot
img_height = 369
img_width = 496
epochs = 25
model = tf.keras.Sequential()
model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(img_height, img_width, 3)))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Flatten())
model.add(layers.Dense(64, activation='relu'))
# Since we have two classes:
model.add(layers.Dense(1, activation='sigmoid'))
# BinaryCrossentropy because there are 2 classes
optimizer = tf.keras.optimizers.Adam(learning_rate=0.0001)
model.compile(optimizer=optimizer, loss=tf.keras.losses.BinaryCrossentropy(from_logits=False), metrics=['accuracy'])
# Feed the model
history = model.fit(train_images, epochs=epochs, batch_size=32, verbose=1, validation_data=val_images)
# Plot
acc = history.history['accuracy']
val_acc = history.history['val_accuracy']
loss = history.history['loss']
val_loss = history.history['val_loss']
epochs_range = range(epochs)
plt.figure(figsize=(8, 8))
plt.subplot(1, 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(1, 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')
plt.show()
Now that the model is trained, it shows plots of the training and validation accuracy and loss. I try to load my test set using:
test_images = tf.keras.preprocessing.image_dataset_from_directory(
"path_to_test_set",
labels='inferred',
label_mode="binary",
class_names = ['classA', 'classB'],
color_mode = 'rgb',
batch_size = batch_size, # not really applicable as I want to use the whole set?
image_size = (img_height, img_width),
shuffle = True,
seed = 123,
validation_split = None
)
But is this the correct way? How do I deal with the batch_size? I think I'd evaluate the model with my test set using:
test_loss, test_acc = model.evaluate(test_images, verbose=2)
print('\nTest accuracy:', test_acc)
but I don't think this is sufficient as I'd like the accuracy, precision, recall and F1-score. I'm also not even sure the right thing is happening here (with how the test set is loaded).
So basically: How do I load my test set and calculate accuracy, precision, recall and F1-score?
You need to iterate over the data, then you can collect predictions and true classes.
predicted_probs = np.array([])
true_classes = np.array([])
for images, labels in test_images:
predicted_probs = np.concatenate([predicted_probs,
model(images)])
true_classes = np.concatenate([true_classes, labels.numpy()])
Since they are sigmoid outputs, you need to transform them into classes with a threshold, i.e 0.5 here:
predicted_classes = [1 * (x[0]>=0.5) for x in predicted_probs]
After that you can get the confusion matrix etc:
conf_matrix = tf.math.confusion_matrix(true_classes, predicted_classes)
Training and Validation curves have spikes for loss and accuracy when training vgg16. I am using transfer learning technique and have changed the classifier for binary class problem of classifying genders. Can someone suggest me why am i getting such spikes and how can i reduce it.
The code is as follows :
from keras.layers import Input, Lambda, Dense, Flatten, Dropout
from keras.models import Model
from keras.applications.vgg16 import VGG16
from keras.applications.vgg16 import preprocess_input
from keras.preprocessing import image
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
import numpy as np
from glob import glob
import matplotlib.pyplot as plt
# re-size all the images to this
IMAGE_SIZE = [224, 224]
train_path = 'E:/decompressed_images/data_set/train'
valid_path = 'E:/decompressed_images/data_set/validation'
# add preprocessing layer to the front of VGG
vgg = VGG16(input_shape=IMAGE_SIZE + [3], weights='imagenet', include_top=False)
# don't train existing weights
for layer in vgg.layers:
layer.trainable = False
# useful for getting number of classes
folders = glob('E:/decompressed_images/data_set/train*')
x = Flatten()(vgg.output)
# x = Dense(1000, activation='relu')(x)
prediction = Dense(len(folders), activation='sigmoid')(x)
# create a model object
model = Model(inputs=vgg.input, outputs=prediction)
# view the structure of the model
model.summary()
# tell the model what cost and optimization method to use
model.compile(
loss='binary_crossentropy',
optimizer='adam',
metrics=['accuracy']
)
from keras.preprocessing.image import ImageDataGenerator
train_datagen = ImageDataGenerator(rescale = 1./255,
horizontal_flip = True,
vertical_flip = True,
width_shift_range = 0.1,
height_shift_range = 0.1,
zoom_range = 0.1,
rotation_range = 10)
test_datagen = ImageDataGenerator(rescale = 1./255)
training_set = train_datagen.flow_from_directory('E:/Ullu/new_trial__/balanced_dataset/train',
target_size = (224, 224),
batch_size = 64,
class_mode = 'binary')
test_set = test_datagen.flow_from_directory('E:/Ullu/new_trial__/balanced_dataset/test',
target_size = (224, 224),
batch_size = 64,
class_mode = 'binary')
r = model.fit_generator(
training_set,
validation_data=test_set,
epochs=100,
steps_per_epoch=len(training_set),
validation_steps=len(test_set)
)
plt.plot(r.history['loss'], label='train loss')
plt.plot(r.history['val_loss'], label='val loss')
plt.legend()
plt.show()
plt.savefig('E:/Model_128_30/LossVal_loss.png')
# accuracies
plt.plot(r.history['accuracy'], label='train acc')
plt.plot(r.history['val_accuracy'], label='val acc')
plt.legend()
plt.show()
plt.savefig('E:/Model_128_30/AccVal_acc.png')
import tensorflow as tf
from keras.models import load_model
model.save('E:/Model_128_30/128_30_wt.h5')
High and fluctuating training and validation accuracy image
High and fluctuating training and validation loss image
I tried using dropout layer(0.5) for the final layers but my accuracy and loss for training and validation are the same. Could anyone please suggest me where i am going wrong. Thanks.
I am trying to do image recognition with ResNet50 in Python (keras). I tried to do the same task with VGG16, and I got some results like these (which seem okay to me):
resultsVGG16 . The training and validation accuracy/loss functions are getting better with each step, so the network must learn.
However, with ResNet50 the training functions are betting better, while the validation functions are not changing: resultsResNet
I've used the same code and data in both of the times, only the model is changed.
So what are the reasons of ResNet50 learning only on the training data?
My ResNet model looks like this:
'''python
model = Sequential()
base_model = VGG16(weights='imagenet', include_top=False,input_shape=
(image_size,image_size,3))
for layer in base_model.layers[:-4]:
layer.trainable=False
model.add(base_model)
model.add(Flatten())
model.add(Dense(256, activation='relu'))
model.add(Dropout(0.4))
model.add(Dense(NUM_CLASSES, activation='softmax'))
The VGG is very similar:
model = Sequential()
base_model = ResNet50(include_top=False, weights='imagenet', input_shape=
(image_size,image_size,3))
for layer in base_model.layers[:-8]:
layer.trainable=False
model.add(base_model)
model.add(Flatten())
model.add(Dense(256, activation='relu'))
model.add(Dropout(0.4))
model.add(Dense(NUM_CLASSES, activation='softmax'))
There is no mistake in your Model but this might be the issue with ResNet as such, because there are many issues raised, 1,2,3, in Github and Stack Overflow, already regarding this Pre-Trained Model.
Having said that, I found out a workaround, which worked for me, and hopefully works for you as well.
Workaround was to replace the Data Augmentation step,
Train_Datagen = ImageDataGenerator(rescale=1./255, rotation_range=40, width_shift_range=0.2,
height_shift_range=0.2, brightness_range=(0.2, 0.7), shear_range=45.0, zoom_range=60.0,
horizontal_flip=True, vertical_flip=True)
Val_Datagen = ImageDataGenerator(rescale=1./255, rotation_range=40, width_shift_range=0.2,
height_shift_range=0.2, brightness_range=(0.2, 0.7), shear_range=45.0, zoom_range=60.0,
horizontal_flip=True, vertical_flip=True)
with tf.keras.applications.resnet.preprocess_input, as shown below:
Train_Datagen = ImageDataGenerator(dtype = 'float32', preprocessing_function=tf.keras.applications.resnet.preprocess_input)
Val_Datagen = ImageDataGenerator(dtype = 'float32', preprocessing_function=tf.keras.applications.resnet.preprocess_input)
By modifying the Data Augmentation as shown above, my Validation Accuracy, which got stuck at 50% increased gradually up to 97%. Reason for this might be that ResNet might expect specific Pre-Processing Operations (not quite sure).
Complete working code which resulted in more than 95% of both Train and Validation Accuracy (for Cat and Dog Dataset) using ResNet50 is shown below:
import tensorflow as tf
from tensorflow.keras.applications import ResNet50
import os
import numpy as np
from keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.layers import Dense, Dropout, Flatten
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.models import Sequential
# The Convolutional Base of the Pre-Trained Model will be added as a Layer in this Model
Conv_Base = ResNet50(include_top = False, weights = 'imagenet', input_shape = (150,150, 3))
for layer in Conv_Base.layers[:-8]:
layer.trainable = False
model = Sequential()
model.add(Conv_Base)
model.add(Flatten())
model.add(Dense(units = 256, activation = 'relu'))
model.add(Dropout(0.5))
model.add(Dense(units = 1, activation = 'sigmoid'))
model.summary()
base_dir = 'Deep_Learning_With_Python_Book/Dogs_Vs_Cats_Small'
if os.path.exists(base_dir):
train_dir = os.path.join(base_dir, 'train')
validation_dir = os.path.join(base_dir, 'validation')
test_dir = os.path.join(base_dir, 'test')
else:
print("The Folder, {}, doesn't exist'".format(base_dir))
batch_size = 20
Train_Datagen = ImageDataGenerator(dtype = 'float32', preprocessing_function=tf.keras.applications.resnet.preprocess_input)
Val_Datagen = ImageDataGenerator(dtype = 'float32', preprocessing_function=tf.keras.applications.resnet.preprocess_input)
train_gen = Train_Datagen.flow_from_directory(directory = train_dir, target_size = (150,150),
batch_size = batch_size, class_mode = 'binary')
val_gen = Val_Datagen.flow_from_directory(directory = validation_dir, target_size = (150,150),
batch_size = batch_size, class_mode = 'binary')
epochs = 15
Number_Of_Training_Images = train_gen.classes.shape[0]
steps_per_epoch = Number_Of_Training_Images/batch_size
model.compile(optimizer = 'Adam', loss = 'binary_crossentropy', metrics = ['accuracy'])
history = model.fit(train_gen, epochs = epochs,
#batch_size = batch_size,
validation_data = val_gen, steps_per_epoch = steps_per_epoch)
import matplotlib.pyplot as plt
train_acc = history.history['accuracy']
val_acc = history.history['val_accuracy']
train_loss = history.history['loss']
val_loss = history.history['val_loss']
No_Of_Epochs = range(epochs)
plt.plot(No_Of_Epochs, train_acc, marker = 'o', color = 'blue', markersize = 12,
linewidth = 2, label = 'Training Accuracy')
plt.plot(No_Of_Epochs, val_acc, marker = '.', color = 'red', markersize = 12,
linewidth = 2, label = 'Validation Accuracy')
plt.title('Training Accuracy and Testing Accuracy w.r.t Number of Epochs')
plt.legend()
plt.figure()
plt.plot(No_Of_Epochs, train_loss, marker = 'o', color = 'blue', markersize = 12,
linewidth = 2, label = 'Training Loss')
plt.plot(No_Of_Epochs, val_acc, marker = '.', color = 'red', markersize = 12,
linewidth = 2, label = 'Validation Loss')
plt.title('Training Loss and Testing Loss w.r.t Number of Epochs')
plt.legend()
plt.show()
Metrics are shown in the below graph,
I am learning to write CNNs in Keras on Kaggle using one of the datasets I found there.
The link to my notebook is
https://www.kaggle.com/vj6978/brain-tumor-vimal?scriptVersionId=16814133
The code, the dataset and the ROC curve are available at the link. The ROC curve I am plotting seems to look sharp and not smooth.
The code is as follows:
import os
import cv2
import random
import numpy as np
from numpy.lib.stride_tricks import as_strided
from sklearn.model_selection import train_test_split
from sklearn.metrics import roc_curve
from sklearn.metrics import auc
import matplotlib.pyplot as plt
import keras
from keras.models import Sequential
from keras.layers import Dense, Activation, Conv2D, MaxPooling2D, AveragePooling2D, Dropout, Flatten
from PIL import Image
data_set = []
data_label = []
training_data = []
input_path = "../input/brain_tumor_dataset"
CATEGORIES = ["no", "yes"]
"""
The show function simply takes in a numpy array and displays it as an image.
"""
def show(img_input):
plt.imshow(img_input)
plt.show()
def create_training_data():
for category in CATEGORIES:
path = os.path.join(input_path, category)
category_index = CATEGORIES.index(category)
for image in os.listdir(path):
try:
img_array = cv2.imread(os.path.join(path, image), cv2.IMREAD_GRAYSCALE)
img_array = img_array.astype(np.float32)
img_array = cv2.resize(img_array, (128, 128))
training_data.append([img_array, category_index])
except Exception as e:
print(e)
create_training_data()
random.shuffle(training_data)
for feature, label in training_data:
data_set.append(feature)
data_label.append(label)
x_train, x_test, y_train, y_test = train_test_split(data_set, data_label, test_size = 0.1,
random_state = 45)
data_set = np.array(x_train).reshape(-1, 128, 128, 1)
x_test = np.array(x_test).reshape(-1, 128, 128, 1)
data_set = data_set/255.0
model = Sequential()
model.add(Conv2D(128, (3,3), input_shape = data_set.shape[1:]))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size = (2,2)))
model.add(Conv2D(128, (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"))
model.summary()
model.compile(optimizer = "adam", loss = "binary_crossentropy", metrics = ['accuracy'])
model.fit(data_set, y_train, batch_size = 32, epochs = 15, validation_split = 0.1)
score = model.evaluate(x_test, y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])
y_pred_keras = model.predict(x_test).ravel()
fpr_keras, tpr_keras, thresholds_keras = roc_curve(y_test, y_test)
auc_keras = auc(fpr_keras, tpr_keras)
plt.figure(1)
plt.plot([0, 1], [0, 1], 'k--')
plt.plot(fpr_keras, tpr_keras, label='Keras (area = {:.3f})'.format(auc_keras))
plt.xlabel('False positive rate')
plt.ylabel('True positive rate')
plt.title('ROC curve')
plt.legend(loc='best')
plt.show()
The curve looks like this
Any help would be appreciated.
Thanks Vimal James
fpr_keras, tpr_keras, thresholds_keras = roc_curve(y_test, y_test) this line is where your error is. I think you meant :
fpr_keras, tpr_keras, thresholds_keras = roc_curve(y_test, y_pred_keras)