ROC curve looks too sharp and not smooth - python

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)

Related

Unable to find the cause of AssertionError: group argument must be None for now

I'm recieving the error: assert group is None, 'group argument must be None for now', AssertionError: group argument must be None for now when running the code below. Would anyone be able to advise what is causing the error/how to fix this? The error seems to be within the opt = GridSearch block of code.
import os
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt;
# Importing sklearn libraries
from sklearn.model_selection import GridSearchCV
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import confusion_matrix, accuracy_score
# Importing hypopt library for grid search
from hypopt import GridSearch
# Importing Keras libraries
from keras.utils import np_utils
from keras.models import Sequential
from keras.applications import VGG16
from keras.applications import InceptionV3
from keras.applications import imagenet_utils
from keras.callbacks import ModelCheckpoint
from tensorflow.keras.utils import load_img
from tensorflow.keras.utils import img_to_array
from keras.layers import Dense, Conv2D, MaxPooling2D
from keras.layers import Dropout, Flatten, GlobalAveragePooling2D
import tensorflow as tf
from PIL import Image
import warnings
warnings.filterwarnings('ignore')
#below 3 lines set paths for training, validation and test folders - this instance just focuses on the carrots class.
train = [os.path.join("C:/Users/lboyd/OneDrive/Desktop/Year 3/Research Project/Food images/training_test",img) for img in os.listdir("C:/Users/lboyd/OneDrive/Desktop/Year 3/Research Project/Food images/training_test")]
val = [os.path.join("C:/Users/lboyd/OneDrive/Desktop/Year 3/Research Project/Food images/validation_test",img) for img in os.listdir("C:/Users/lboyd/OneDrive/Desktop/Year 3/Research Project/Food images/validation_test")]
test = [os.path.join("C:/Users/lboyd/OneDrive/Desktop/Year 3/Research Project/Food images/testing_test",img) for img in os.listdir("C:/Users/lboyd/OneDrive/Desktop/Year 3/Research Project/Food images/testing_test")]
len(train),len(val),len(test) #gets the length of the training, validation and test folders.
train_y = [int(img.split("\\")[-1].split("_")[0]) for img in train]
val_y = [int(img.split("\\")[-1].split("_")[0]) for img in val]
test_y = [int(img.split("\\")[-1].split("_")[0]) for img in test]
num_classes = 3
# Convert class labels in one hot encoded vector
y_train = np_utils.to_categorical(train_y, num_classes)
y_val = np_utils.to_categorical(val_y, num_classes)
y_test = np_utils.to_categorical(test_y, num_classes)
train_y[0:3]
print(y_train[0:3])
print("Training data available in 3 classes")
print([train_y.count(i) for i in range(0,3)])
food_classes = ('Carrots','Butter','Eggs')
y_pos = np.arange(len(food_classes))
counts = [train_y.count(i) for i in range(0,3)]
plt.barh(y_pos, counts, align='center', alpha=0.5)
plt.yticks(y_pos, food_classes)
plt.xlabel('Amount of Images')
plt.ylabel('Food Item')
plt.title('Training Data Distribution')
plt.show()
print("Validation data available in 3 classes")
print([val_y.count(i) for i in range(0,3)])
print ("Test data available in 3 classes")
print([test_y.count(i) for i in range(0,3)])
#show images from dataset
def show_imgs(X):
plt.figure(figsize=(8, 8))
k = 0
for i in range(0,4):
for j in range(0,4):
image = load_img(train[k], target_size=(224, 224))
plt.subplot2grid((4,4),(i,j))
plt.imshow(image)
k = k+1
# show the plot
plt.show()
show_imgs(train)
#start loading VGG16 model - this can be changed for a different model after completed testing
model = VGG16(weights="imagenet", include_top=False)
model.summary()
#extract features of images contained within food image dataset using the VGG16 model
def create_features(dataset, pre_model):
x_scratch = []
#loop over images in dataset
for imagePath in dataset:
#load the input image and resize image
image = load_img(imagePath, target_size=(224, 224))
image = img_to_array(image)
#preprocess the loaded image by expanding the dimensions of the image and subtracting the mean RGB pixel intensity from the ImageNet dataset
image = np.expand_dims(image, axis=0)
image = np.squeeze(image, axis=0)
image = imagenet_utils.preprocess_input(image)
#add image to the batch of images
x_scratch.append(image)
x = np.stack(x_scratch)
features = pre_model.predict(x, batch_size = 32)
features_flatten = features.reshape((features.shape[0], 7 * 7 * 512))
return x, features, features_flatten
train_x, train_features, train_features_flatten = create_features(train, model)
val_x, val_features, val_features_flatten = create_features(val, model)
test_x, test_features, test_features_flatten = create_features(test, model)
print(train_x.shape, train_features.shape, train_features_flatten.shape)
print(val_x.shape, val_features.shape, val_features_flatten.shape)
print(test_x.shape, test_features.shape, test_features_flatten.shape)
#train CNN model on limited images from test dataset
# Creating a checkpointer
checkpointer = ModelCheckpoint(filepath='scratchmodel.best.hdf5',
verbose=1,save_best_only=True)
# Building up a Sequential model
model_scratch = Sequential() #uses sequential model
model_scratch.add(Conv2D(32, (3, 3), activation='relu',input_shape = train_x.shape[1:]))
model_scratch.add(MaxPooling2D(pool_size=(2, 2)))
model_scratch.add(Conv2D(64, (3, 3), activation='relu'))
model_scratch.add(MaxPooling2D(pool_size=(2, 2)))
model_scratch.add(Conv2D(64, (3, 3), activation='relu'))
model_scratch.add(MaxPooling2D(pool_size=(2, 2)))
model_scratch.add(Conv2D(128, (3, 3), activation='relu'))
model_scratch.add(MaxPooling2D(pool_size=(2, 2)))
model_scratch.add(GlobalAveragePooling2D())
model_scratch.add(Dense(64, activation='relu'))
model_scratch.add(Dense(3, activation='softmax'))
model_scratch.summary()
model_scratch.compile(loss='categorical_crossentropy', optimizer='adam',
metrics=['accuracy'])
#Fitting the model on the train data and labels.
history = model_scratch.fit(train_x, y_train,
batch_size=32, epochs=10,
verbose=1, callbacks=[checkpointer],
validation_data=(val_x, y_val), shuffle=True)
#creating plot to show accuracy of baseline CNN model
def plot_acc_loss(history):
fig = plt.figure(figsize=(10,5))
plt.subplot(1, 2, 1)
plt.plot(history.history['accuracy'])
plt.plot(history.history['val_accuracy'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'validation'], loc='upper left')
plt.subplot(1, 2, 2)
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper right')
plt.show()
plot_acc_loss(history)
#evaluate baseline CNN model - create confusion matrix for each of the classes
preds = np.argmax(model_scratch.predict(test_x), axis=1)
print("\nAccuracy on Test Data: ", accuracy_score(test_y, preds))
print("\nNumber of correctly identified imgaes: ",
accuracy_score(test_y, preds, normalize=False),"\n")
confusion_matrix(test_y, preds, labels=range(0,3))
#use extracted features from last maxpooling layer of VGG16 as an input for a shallow NN
model_transfer = Sequential()
model_transfer.add(GlobalAveragePooling2D(input_shape=train_features.shape[1:]))
model_transfer.add(Dropout(0.2))
model_transfer.add(Dense(100, activation='relu'))
model_transfer.add(Dense(3, activation='softmax'))
model_transfer.summary()
model_transfer.compile(loss='categorical_crossentropy', optimizer='adam',
metrics=['accuracy'])
history = model_transfer.fit(train_features, y_train, batch_size=32, epochs=10,
validation_data=(val_features, y_val), callbacks=[checkpointer],
verbose=1, shuffle=True)
plot_acc_loss(history)
#evaluate transfer learnt features based shallow NN - confusion matrixes created for each food class.
preds = np.argmax(model_transfer.predict(test_features), axis=1)
print("\nAccuracy on Test Data: ", accuracy_score(test_y, preds))
print("\nNumber of correctly identified images: ",
accuracy_score(test_y, preds, normalize=False),"\n")
print(confusion_matrix(test_y, preds, labels=range(0,3)))
param_grid = [{'C': [0.1,1,10],'solver': ['newton-cg','lbfgs']}]
# Grid-search all parameter combinations using a validation set.
opt = GridSearch(model = LogisticRegression(class_weight='balanced', multi_class="auto",
max_iter=200, random_state=1),param_grid = param_grid)
opt.fit(train_features_flatten, train_y, val_features_flatten, val_y, scoring = 'accuracy')
print(opt.get_best_params())
The error I'm recieving is:
Traceback (most recent call last):
File "C:\Users\lboyd\OneDrive\Desktop\modeltest2.py", line 238, in <module>
opt.fit(train_features_flatten, train_y, val_features_flatten, val_y, scoring = 'accuracy')
File "C:\ProgramData\Anaconda3\lib\site-packages\hypopt\model_selection.py", line 360, in fit
results = _parallel_param_opt(params, self.num_threads)
File "C:\ProgramData\Anaconda3\lib\site-packages\hypopt\model_selection.py", line 185, in _parallel_param_opt
with multiprocessing_context(
File "C:\ProgramData\Anaconda3\lib\multiprocessing\pool.py", line 212, in __init__
self._repopulate_pool()
File "C:\ProgramData\Anaconda3\lib\multiprocessing\pool.py", line 303, in _repopulate_pool
return self._repopulate_pool_static(self._ctx, self.Process,
File "C:\ProgramData\Anaconda3\lib\multiprocessing\pool.py", line 319, in _repopulate_pool_static
w = Process(ctx, target=worker,
File "C:\ProgramData\Anaconda3\lib\multiprocessing\process.py", line 82, in __init__
assert group is None, 'group argument must be None for now'
AssertionError: group argument must be None for now

ROC Curve area is nan CNN model

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.

AttributeError: 'TensorShape' object has no attribute 'assert_has_rank' (when trying to predict on an image classification keras model)

I took over a simple convolutional network from the internet for a simple binary image classification task, it's my first try on image classification. It trains well on the sets of images I collected. This is the code for training:
import matplotlib.pyplot as plt
import seaborn as sns
import keras
from keras.models import Sequential
from keras.layers import Dense, Conv2D , MaxPool2D , Flatten , Dropout
from keras.preprocessing.image import ImageDataGenerator
from keras.optimizers import Adam
from sklearn.metrics import classification_report
import tensorflow as tf
import cv2
import os
import numpy as np
from pathlib import Path
cwd = os.getcwd()
my_file = Path(cwd + "/ddornotbiclassifier/keras_model.h5")
labels = ['dd', 'notadd']
img_size = 224
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)
train = get_data(cwd + "/images/dd_notadd/train")
val = get_data(cwd + "/images/dd_notadd/valid")
l = []
for i in train:
if(i[1] == 0):
l.append("dd")
else:
l.append("notdd")
sns.set_style('darkgrid')
sns.countplot(l)
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)
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
datagen.fit(x_train)
if not my_file.is_file():
print("There was no saved model so model will be trained from scratch")
model = Sequential()
model.add(Conv2D(32,3,padding="same", activation="relu", input_shape=(224,224,3)))
model.add(MaxPool2D())
model.add(Conv2D(32, 3, padding="same", activation="relu"))
model.add(MaxPool2D())
model.add(Conv2D(64, 3, padding="same", activation="relu"))
model.add(MaxPool2D())
model.add(Dropout(0.4))
model.add(Flatten())
model.add(Dense(128,activation="relu"))
model.add(Dense(2, activation="softmax"))
model.summary()
opt = Adam(lr=0.000001)
model.compile(optimizer = opt , loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True) , metrics = ['accuracy'])
else:
print("There was a saved model so that model will be loaded and continued to train")
pathtomodel = cwd + '/ddornotbiclassifier/keras_model.h5'
model = keras.models.load_model(pathtomodel)
opt = Adam(lr=0.000001)
model.compile(optimizer = opt , loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True) , metrics = ['accuracy'])
history = model.fit(x_train,y_train,epochs = 3085, validation_data = (x_val, y_val))
acc = history.history['accuracy']
val_acc = history.history['val_accuracy']
loss = history.history['loss']
val_loss = history.history['val_loss']
epochs_range = range(3085)
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')
plt.show()
predictions = model.predict_classes(x_val)
predictions = predictions.reshape(1,-1)[0]
print(classification_report(y_val, predictions, target_names = ['dd (Class 0)','notadd (Class 1)']))
model.save("./ddornotbiclassifier/keras_model.h5")
Then I created another python file to predict on the model. This is what I have so far:
import os
import numpy as np
from keras.models import Sequential
from keras.layers import Dense, Conv2D , MaxPool2D , Flatten , Dropout
from keras.optimizers import Adam
import tensorflow as tf
import cv2
cwd = os.getcwd()
model = Sequential()
model.add(Conv2D(32,3,padding="same", activation="relu", input_shape=(224,224,3)))
model.add(MaxPool2D())
model.add(Conv2D(32, 3, padding="same", activation="relu"))
model.add(MaxPool2D())
model.add(Conv2D(64, 3, padding="same", activation="relu"))
model.add(MaxPool2D())
model.add(Dropout(0.4))
model.add(Flatten())
model.add(Dense(128,activation="relu"))
model.add(Dense(2, activation="softmax"))
pathtomodel = cwd + '/ddornotbiclassifier/keras_model.h5'
model.load_weights(pathtomodel)
opt = Adam(lr=0.000001)
model.compile(optimizer = opt , loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True) , metrics = ['accuracy'])
path = cwd + "/images/dd_notadd/testimage/testimage.jpeg"
img = cv2.imread(path)
img = cv2.resize(img,(224,224))
img = np.reshape(img,[1,224,224,3])
model.predict(img)
Then this is the output:
WARNING:tensorflow:9 out of the last 11 calls to <function Model.make_predict_function.<locals>.predict_function at 0x7fe6e5799430> triggered tf.function retracing. Tracing is expensive and the excessive number of tracings could be due to (1) creating #tf.function repeatedly in a loop, (2) passing tensors with different shapes, (3) passing Python objects instead of tensors. For (1), please define your #tf.function outside of the loop. For (2), #tf.function has experimental_relax_shapes=True option that relaxes argument shapes that can avoid unnecessary retracing. For (3), please refer to https://www.tensorflow.org/tutorials/customization/performance#python_or_tensor_args and https://www.tensorflow.org/api_docs/python/tf/function for more details.
Traceback (most recent call last):
File "/Users/lennartkonst/Documents/Chatbots/Pythonbots/DreamDestinationsBot/ML/ddornotinference2.py", line 44, in <module>
model.predict(img)
File "/opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py", line 130, in _method_wrapper
return method(self, *args, **kwargs)
File "/opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py", line 1614, in predict
all_outputs = nest.map_structure_up_to(batch_outputs, concat, outputs)
File "/opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/util/nest.py", line 1135, in map_structure_up_to
return map_structure_with_tuple_paths_up_to(
File "/opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/util/nest.py", line 1234, in map_structure_with_tuple_paths_up_to
results = [func(*args, **kwargs) for args in zip(flat_path_list,
File "/opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/util/nest.py", line 1234, in <listcomp>
results = [func(*args, **kwargs) for args in zip(flat_path_list,
File "/opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/util/nest.py", line 1137, in <lambda>
lambda _, *values: func(*values), # Discards the path arg.
File "/opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py", line 2673, in concat
return array_ops.concat(tensors, axis=axis)
File "/opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/util/dispatch.py", line 201, in wrapper
return target(*args, **kwargs)
File "/opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/ops/array_ops.py", line 1650, in concat
ops.convert_to_tensor(
AttributeError: 'TensorShape' object has no attribute 'assert_has_rank'
I'm stuck and it gives this error on many type of tries. A numpy array of shape 1,224,224,3 should do it right?
What should I do?
Thank you.

My model got 94% I dont think its right andit doesnt make sense

Hi so I recently made an AI program that can classify flowers and it says when I ran Model.fit_generator
it says I got 94% accuracy and when I searched up the record for highest accuracy is was also 94% and it just doesn't sound right like I just started neural networks 2 months ago and it says I got the same accuracy of a company and I just started.Also I used a TPU from colab
import os
import numpy as np
import pickle
from tensorflow import keras
import cv2
import matplotlib.pyplot as plt
import random
from keras.preprocessing.image import ImageDataGenerator
os.getcwd()
data = []
img_size = 175
categories = ["daisy", "dandelion", "rose", "sunflower", "tulip","Lily","aster","blue bonet","Camellia","gardinia","Lavander","Azalea"]
def create_data():
for category in categories:
path = os.path.join('image_data/image_data/Train', category)
class_num = categories.index(category)
for img in os.listdir(path):
img_arr = cv2.imread(os.path.join(path, img))
try:
new_arr = cv2.resize(img_arr, (img_size, img_size))
except cv2.error as e:
print('Not valid')
cv2.waitKey()
data.append([new_arr, class_num])
create_data()
random.shuffle(data)
X=[]
y=[]
for features, labels in data:
X.append(features)
y.append(labels)
X = np.array(X).reshape(-1, img_size, img_size, 3)
y = np.array(y)
print('Shape of X: ', X.shape)
print('Shape of y: ', y.shape)
pickle_out = open('X.pickle', 'wb')
pickle.dump(X, pickle_out)
pickle_out_2 = open('y.pickle', 'wb')
pickle.dump(y, pickle_out_2)
X = X / 255.0
from sklearn.preprocessing import LabelEncoder
from keras.utils import to_categorical
le = LabelEncoder()
y= le.fit_transform(y)
y = to_categorical(y,12)
y.shape
from sklearn.model_selection import train_test_split
X_train, X_valid, y_train, y_valid = train_test_split(X, y, train_size = 0.8, random_state= 7)
datagen = ImageDataGenerator(
featurewise_center=False,
samplewise_center=False,
featurewise_std_normalization=False,
samplewise_std_normalization=False,
zca_whitening=False,
rotation_range=10,
zoom_range = 0.1,
width_shift_range=0.2,
height_shift_range=0.2,
horizontal_flip=True,
vertical_flip=False)
datagen.fit(X_train)
model = keras.models.Sequential([keras.layers.Conv2D(32, 5, activation='relu', padding='SAME', input_shape=X.shape[1:]),
keras.layers.MaxPooling2D(2),
keras.layers.Conv2D(64, 3, activation='relu', padding='SAME'),
keras.layers.MaxPooling2D(pool_size=2),
keras.layers.Conv2D(96, 3, activation="relu", padding="same"),
keras.layers.MaxPooling2D(pool_size=2),
keras.layers.Conv2D(128, 3, activation="relu", padding="same"),
keras.layers.MaxPooling2D(pool_size=2),
keras.layers.Flatten(),
keras.layers.Dense(500, activation='relu'),
keras.layers.Dropout(0.7),
keras.layers.Dense(12, activation='softmax')
])
model.compile(optimizer = keras.optimizers.Adam(learning_rate=0.001), loss = 'categorical_crossentropy', metrics=['accuracy'])
#history = model.fit_generator(datagen.flow(X_train, y_train, batch_size=128), epochs=70, validation_data=(X_valid, y_valid))
model.fit(X_train, y_train, batch_size=128, epochs=70,validation_data=(X_valid, y_valid))
model.save("model1.h5")
could you please see if I actually got 94% acc. Sorry for my English it is not my first language
First of all, you need to go through the basic ML course and build up some base to know what exactly you're trying to achieve here. Getting your model to overfit and to get 99.9% accuracy is pretty straightforward and EASY, but also wrong. I'm pretty sire your code can overfit and give you the accuracy that you've mentioned. There's nothing wrong with your code.
I would recommand you to plot validation loss and accuracy, might help you to understand what happened and will definitely tells you if your model overfit.
You can also use model.predicton new images preprocessed to actually check if your network performs well.
I would recommand you splitting your data into three different sets : training, validation and test.

I scripted a simple convLSTM for video classification but i get an error

i created a convLSTM network that classifies videos. The dataset consist of 6 classes with videos inside it.
I get the error "NameError: name 'y_pred' is not defined" and it's related to the third last line of code. You don't need to run the code because it's probably a syntax error or something like this.
Why i get this error ?
import keras
from keras import applications
from keras.preprocessing.image import ImageDataGenerator
from keras import optimizers
from keras.models import Sequential, Model
from keras.layers import *
from keras.callbacks import ModelCheckpoint, LearningRateScheduler, TensorBoard, EarlyStopping
import os
import cv2
import numpy as np
from sklearn.model_selection import train_test_split
import keras_metrics as km
from sklearn.metrics import accuracy_score
from sklearn.metrics import precision_score
from sklearn.metrics import recall_score
from sklearn.metrics import f1_score
from sklearn.metrics import cohen_kappa_score
from sklearn.metrics import roc_auc_score
from sklearn.metrics import multilabel_confusion_matrix
data_dir = "video_data/"
img_height , img_width = 64, 64
seq_len = 70
classes = ["Apply Eye Makeup", "Archery", "Apply Lipstick", "Baby Crawling", "Balance Beam", "Band Marching"]
# Creating frames from videos
def frames_extraction(video_path):
frames_list = []
vidObj = cv2.VideoCapture(video_path)
# Used as counter variable
count = 1
while count <= seq_len:
success, image = vidObj.read()
if success:
image = cv2.resize(image, (img_height, img_width))
frames_list.append(image)
count += 1
else:
print("Defected frame")
break
return frames_list
def create_data(input_dir):
X = []
Y = []
classes_list = os.listdir(input_dir)
for c in classes_list:
print(c)
files_list = os.listdir(os.path.join(input_dir, c))
for f in files_list:
frames = frames_extraction(os.path.join(os.path.join(input_dir, c), f))
if len(frames) == seq_len:
X.append(frames)
y = [0]*len(classes)
y[classes.index(c)] = 1
Y.append(y)
X = np.asarray(X)
Y = np.asarray(Y)
return X, Y
X, Y = create_data(data_dir)
X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.20, shuffle=True, random_state=0)
model = Sequential()
model.add(ConvLSTM2D(filters = 64, kernel_size = (3, 3), return_sequences = False, data_format = "channels_last", input_shape = (seq_len, img_height, img_width, 3)))
model.add(Dropout(0.2))
model.add(Flatten())
model.add(Dense(256, activation="relu"))
model.add(Dropout(0.3))
model.add(Dense(6, activation = "softmax"))
model.summary()
opt = keras.optimizers.SGD(lr=0.001)
model.compile(loss='categorical_crossentropy', optimizer=opt, metrics=["accuracy"])
earlystop = EarlyStopping(patience=7)
callbacks = [earlystop]
history = model.fit(x = X_train, y = y_train, epochs=40, batch_size = 8 , shuffle=True, validation_split=0.2, callbacks=callbacks)
y_pred = np.argmax(y_pred, axis = 1)
y_test = np.argmax(y_test, axis = 1)
print(classification_report(y_test, y_pred))
The error is self-explanatory from the message: y_pred is not defined...
You have to predict your data first, doing something like y_pred = model.predict(X_test) then, you can argmax, etc.
[EDIT]: to sum up, replace your three last lines with that
y_pred = model.predict(X_test)
y_pred = np.argmax(y_pred, axis = 1)
print(classification_report(y_test, y_pred))

Categories

Resources