How to implement CAM without visualize_cam in this code? - python

I want to make Class activation map, so I have write the code
from keras.datasets import mnist
from keras.layers import Conv2D, Dense, GlobalAveragePooling2D
from keras.models import Model, Input
from keras.utils import to_categorical
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train_resized = x_train.reshape((60000, 28, 28, 1))
x_test_resized = x_test.reshape((10000, 28, 28, 1))
y_train_hot_encoded = to_categorical(y_train)
y_test_hot_encoded = to_categorical(y_test)
inputs = Input(shape=(28,28, 1))
x = Conv2D(64, (3,3), activation='relu')(inputs)
x = Conv2D(64, (3,3), activation='relu')(x)
x = GlobalAveragePooling2D()(x)
predictions = Dense(10, activation='softmax')(x)
model = Model(inputs=inputs, outputs=predictions)
model.compile(optimizer='rmsprop',
loss='categorical_crossentropy',
metrics=['accuracy'])
model.fit(x_train_resized, y_train_hot_encoded, epochs=30, batch_size=256, shuffle=True, validation_split=0.3)
works fine, so I have imported visualize_cam module
from vis.visualization import visualize_cam
import matplotlib.pyplot as plt
import numpy as np
for i in range(10):
ind = np.where(y_test == i)[0][0]
plt.subplot(141)
plt.imshow(x_test_resized[ind].reshape((28,28)))
for j,modifier in enumerate([None, 'guided', 'relu']):
heat_map = visualize_cam(model, 4, y_test[ind], x_test_resized[ind], backprop_modifier=modifier)
plt.subplot(1,4,j+2)
plt.imshow(heat_map)
plt.show()
but the visualize_cam didn`t work well
I tried many times to fix the module but it doesn`t go well
(it depends on scipy which version is below 1.3. but )
so I have to implement cam without that module
Is there any solution to replace visualize_cam into other option to implement CAM?

Here is a scipy library independent implementation.
from keras.datasets import mnist
from keras.layers import Conv2D, Dense, GlobalAveragePooling2D
from keras.models import Model, Input
from keras.utils import to_categorical
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train_resized = x_train.reshape((60000, 28, 28, 1))
x_test_resized = x_test.reshape((10000, 28, 28, 1))
y_train_hot_encoded = to_categorical(y_train)
y_test_hot_encoded = to_categorical(y_test)
inputs = Input(shape=(28,28, 1))
x = Conv2D(64, (3,3), activation='relu')(inputs)
x = Conv2D(64, (3,3), activation='relu', name='final_conv')(x)
x = GlobalAveragePooling2D()(x)
predictions = Dense(10, activation='softmax')(x)
model = Model(inputs=inputs, outputs=predictions)
model.compile(optimizer='rmsprop',
loss='categorical_crossentropy',
metrics=['accuracy'])
model.fit(x_train_resized, y_train_hot_encoded, epochs=1, batch_size=256, shuffle=True, validation_split=0.3)
import numpy as np
import cv2
import io
import requests
from PIL import Image
import matplotlib.pyplot as plt
# Using Keras implementation from tensorflow
from tensorflow.python.keras import applications
from tensorflow.python.keras.preprocessing.image import load_img, img_to_array
from tensorflow.python.keras import backend as K
# Get the layer of the last conv layer
fianlconv = model.get_layer('final_conv')
# Get the weights matrix of the last layer
weight_softmax = model.layers[-1].get_weights()[0]
# Function to generate Class Activation Mapping
HEIGHT = 28
WIDTH = 28
def returnCAM(feature_conv, weight_softmax, class_idx):
size_upsample = (WIDTH, HEIGHT)
# Keras default is channels last, hence nc is in last
bz, h, w, nc = feature_conv.shape
output_cam = []
for idx in class_idx:
cam = np.dot(weight_softmax[:, idx], np.transpose(feature_conv.reshape(h*w, nc)))
cam = cam.reshape(h, w)
cam = cam - np.min(cam)
cam_img = cam / np.max(cam)
cam_img = np.uint8(255 * cam_img)
output_cam.append(cv2.resize(cam_img, size_upsample))
return output_cam
x = x_test_resized[0,:,:,0]
plt.imshow(x)
plt.show()
classes = {1:'1', 2: '2', 3: '3', 4:'4', 5:'5', 6:'6', 7:'7', 8:'8', 9:'9', 0:'0'}
probs_extractor = K.function([model.input], [model.output])
features_conv_extractor = K.function([model.input], [fianlconv.output])
probs = probs_extractor([np.expand_dims(x, 0).reshape(1,28,28,1)])[0]
features_blob = features_conv_extractor([np.expand_dims(x, 0).reshape(1,28,28,1)])[0]
features_blobs = []
features_blobs.append(features_blob)
idx = np.argsort(probs)
probs = np.sort(probs)
for i in range(-1, -6, -1):
print('{:.3f} -> {}'.format(probs[0, i], classes[idx[0, i]]))
CAMs = returnCAM(features_blobs[0], weight_softmax, [idx[0, -1]])
heatmap = cv2.applyColorMap(cv2.resize(CAMs[0], (28, 28)), cv2.COLORMAP_JET)
result = heatmap[:,:,0] * 0.3 + x * 0.5
print(result.shape)
plt.imshow(result)
plt.show()
N.B: I'm plotting normalized images so the result isn't great, I also trained only for 1 epoch. For better results, you may try training more, change to appropriate color space.

Related

Deep learning model not giving predictions as input layer is incompatible

bELOW IS A SIMPLE MODEL FOR IMAGE CLASSIFICATION OF HAND GESTURE RECOGNITION using Kaggle dataset
# -- coding: utf-8 --
"""kaggle_dataset_code.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1kfj2kPVrioXlWX_CDDOGEfxlwMUj5vs6
"""
!pip install kaggle
#You can download the kaggl.json file from your kaggle account. We are going to upload the kaggle.json file.
from google.colab import files
files.upload()
#making kaggle directory as kaggle website has guided.
!mkdir -p ~/.kaggle
!cp kaggle.json ~/.kaggle/
#Giving specical permissions to the kaggle.json file.
!chmod 600 ~/.kaggle/kaggle.json
downloading the kaggle dataset from the website by copying the API token
!kaggle datasets download -d gti-upm/leapgestrecog
#Unzip the dataset
zip_data_path = "/content/leapgestrecog.zip"
from zipfile import ZipFile
file_name = "leapgestrecog.zip"
with ZipFile(file_name,'r') as zip:
zip.extractall()
print("done")
import cv2
image_data = []
CATEGORIES = ["01_palm", '02_l','03_fist','04_fist_moved','05_thumb','06_index','07_ok','08_palm_moved','09_c','10_down']
IMG_SIZE = 50
import os
unzipped_data_path = "/content/leapgestrecog/leapGestRecog/"
print(os.listdir(unzipped_data_path))
for dr in os.listdir(unzipped_data_path):
for category in CATEGORIES:
class_index = CATEGORIES.index(category)
path = os.path.join(unzipped_data_path, dr, category)
for image in os.listdir(path):
image_array = cv2.imread(os.path.join(path, image), cv2.IMREAD_GRAYSCALE)
image_data.append([cv2.resize(image_array, (IMG_SIZE, IMG_SIZE)), class_index])
#image data of a 19000th image
image_data[19000]
import random
random.shuffle(image_data)
input_data = []
label = []
for X, y in image_data:
input_data.append(X)
label.append(y)
import matplotlib.pyplot as plt # for plotting
plt.figure(1, figsize=(10,10))
for i in range(1,10):
plt.subplot(3,3,i)
plt.imshow(image_data[i][0], cmap='hot')
plt.xticks([])
plt.yticks([])
plt.title(CATEGORIES[label[i]][3:])
plt.show()
import numpy as np
input_data = np.array(input_data)
label = np.array(label)
input_data = input_data/255.0
import keras
label = keras.utils.to_categorical(label, num_classes=10,dtype='i1')
label[0]
input_data.shape = (-1, IMG_SIZE, IMG_SIZE, 1)
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(input_data, label, test_size = 0.3, random_state=0)
from keras.layers import Conv2D, Activation, MaxPool2D, Dense, Flatten, Dropout
model = keras.models.Sequential()
model.add(Conv2D(filters = 32, kernel_size = (3,3), input_shape = (IMG_SIZE, IMG_SIZE, 1)))
model.add(Activation('relu'))
model.add(Conv2D(filters = 32, kernel_size = (3,3)))
model.add(Activation('relu'))
model.add(MaxPool2D(pool_size=(2,2)))
model.add(Dropout(0.3))
model.add(Conv2D(filters = 64, kernel_size = (3,3)))
model.add(Activation('relu'))
model.add(MaxPool2D(pool_size=(2,2)))
model.add(Dropout(0.3))
model.add(Flatten())
model.add(Dense(256, activation='relu'))
model.add(Dense(10, activation='softmax'))
model.compile(loss='categorical_crossentropy',
optimizer = 'rmsprop',
metrics = ['accuracy'])
model.summary()
model.fit(X_train, y_train, epochs = 7, batch_size=32, validation_data=(X_test, y_test))
score = model.evaluate(X_test, y_test, batch_size=128)
print(score)
model.save("kaggle_dataset_model.h5")
but i get the similar following error no matter which model i try
ValueError: Input 0 of layer sequential_2 is incompatible with the layer: expected axis -1 of input shape to have value 1 but received input with shape [None, 50, 50, 3]
The code where I want the model to make predictions is below
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1PWDO7aYA6Lhl9FgdgMHh8fj-vlLF_mTw
"""
from keras.models import load_model
from keras.preprocessing import image
import numpy as np
# dimensions of our images
img_width = 50
img_height = 50
# load the model we saved
model = load_model('KaggleModelLeapGesture.h5')
model.compile(loss='binary_crossentropy',
optimizer='rmsprop',
metrics=['accuracy'])
from google.colab import files
from keras.preprocessing import image
uploaded = files.upload()
for fn in uploaded.keys():
# predicting images
path = fn
img = image.load_img(path, target_size=(50, 50))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
images = np.vstack([x])
classes = model.predict(images, batch_size=10)
print(fn)
print(classes)
As Dr. Snoopy suggested, the model is trained on Grey scale images, but you are trying to predict on RGB image. Kindly use the grey scale version of the image.
Coming to your next question regarding the predictions, the last layer of your model is having model.add(Dense(10, activation='softmax')) - that means you have 10 class to be predicted and as you have used softmax function, it gives the probability of the image belonging to these 10 different classes. The sum of all the probability will be equal to 1.

the prediction function gives the same output everytime

from keras.datasets import mnist
from keras.models import Sequential, load_model
from keras.layers import Dense
from keras.layers import Dropout
from keras.layers import Flatten
from keras.layers.convolutional import Conv2D
from keras.layers.convolutional import MaxPooling2D
from keras.utils import np_utils
from keras.preprocessing import image
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
import cv2
(X_train, y_train), (X_test, y_test) = mnist.load_data()
X_train = X_train.reshape((X_train.shape[0], 28, 28, 1)).astype('float32')
X_test = X_test.reshape((X_test.shape[0], 28, 28, 1)).astype('float32')
X_train = X_train / 255
X_test = X_test / 255
y_train = np_utils.to_categorical(y_train)
y_test = np_utils.to_categorical(y_test)
num_classes = y_test.shape[1]
def larger_model():
model = Sequential()
model.add(Conv2D(30, (5, 5), input_shape=(28, 28, 1), activation='relu'))
model.add(MaxPooling2D())
model.add(Conv2D(15, (3, 3), activation='relu'))
model.add(MaxPooling2D())
model.add(Dropout(0.2))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dense(50, activation='relu'))
model.add(Dense(num_classes, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
return model
model = larger_model()
model.fit(X_train, y_train, validation_data=(X_test, y_test), epochs=10, batch_size=200)
scores = model.evaluate(X_test, y_test, verbose=0)
print("Large CNN Error: %.2f%%" % (100-scores[1]*100))
model.save('good_model.h5')
print("Model saved")
After running this code, we get a '.h5' model, then to predict this image
i added this code:
import cv2
model = load_model('good_model.h5')
file = cv2.imread('screenshot.png')
file = cv2.resize(file, (28, 28))
file = cv2.cvtColor(file, cv2.COLOR_BGR2GRAY)
file = file.reshape((-1, 28, 28,1))
result = model.predict(file)
print(result[0])
t = (np.argmax(result[0]))
print("I predict this number is a:", t)
But I always get the same answer which is 4. above I tried to load the image with cv and convert it to gray and then reshape to the size of the input. It takes the input correctly but the answer is always the same no matter what image I give it as input
You need to invert the image before prediction. Once you invert the image, it will predict correctly. The given example is predicting as "2" but I checked with other images such as "7" and it is correctly predicting.
file = cv2.bitwise_not(file)
Other than the above, I made one change. I imported modules from Tensorflow 2.x. Please check the full code here.

i need to make a confusion matrix for a CNN model

hi im new to machine learning and i just wanted to know how to make a confusion matrix from this code i just followed the instructions on youtube and i think im lost i just need to plot the confusion matrix my data sets is all about cancer and has 2 categories with and withought cancer i just followed the video of sentdex and changed his data sets
import tensorflow as tf
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 numpy
import matplotlib.pyplot as plt
import os
import cv2
DATADIR = "C:/Users/Acer/imagerec/MRI"
CATEGORIES = ["yes", "no"]
for category in CATEGORIES:
path = os.path.join(DATADIR,category)
for img in os.listdir(path):
img_array = cv2.imread(os.path.join(path,img) ,cv2.IMREAD_GRAYSCALE)
plt.imshow(img_array, cmap='gray')
plt.show()
break
break
print(img_array)
print(img_array.shape)
IMG_SIZE = 50
new_array = cv2.resize(img_array, (IMG_SIZE, IMG_SIZE))
plt.imshow(new_array, cmap='gray')
plt.show()
training_data = []
def create_training_data():
for category in CATEGORIES:
path = os.path.join(DATADIR, category)
class_num = CATEGORIES.index(category)
for img in os.listdir(path):
try:
img_array = cv2.imread(os.path.join(path, img), cv2.IMREAD_GRAYSCALE)
new_array = cv2.resize(img_array, (IMG_SIZE, IMG_SIZE))
training_data.append([new_array, class_num])
except Exception as e:
pass
create_training_data()
print(len(training_data))
import random
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 = numpy.array(X).reshape(-1, IMG_SIZE, IMG_SIZE, 1)
import pickle
pickle_in = open("X.pickle","rb")
X = pickle.load(pickle_in)
pickle_in = open("y.pickle","rb")
y = pickle.load(pickle_in)
X = X/255.0
model = Sequential()
model.add(Conv2D(256, (3, 3), input_shape=X.shape[1:]))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(256, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(64))
model.add(Activation('relu'))
model.add(Dense(1))
model.add(Activation('sigmoid'))
model.compile(loss='binary_crossentropy',
optimizer='adam',
metrics=['accuracy'])
model.fit(X, y, batch_size=5, epochs=1, validation_split=0.1)
model.save('64x2-CNN.model')
This will show you where the classifier predicted right/wrong on the training data (because there is not test set in your code).
from sklearn.metrics import confusion_matrix
pred = model.predict(X)
conf = confusion_matrix(y, pred)
Out[1]:
array([[5, 8], # rows are actual classes, columns are predicted classes
[9, 3]], dtype=int64)
To plot it (minimal example):
import seaborn as sns
sns.heatmap(conf, annot=True)

How to save images properly classified with CNN?

Handwriting recognition problem with CNN. There is a requirement: from 10000 test images, save 1000 images (.png or .jpg)accurate classified each folder of 100 images (0 -> 9). How do I do? i need an instruction about code. thanks! code :
import keras
from keras.datasets import mnist
from keras.layers import Dense, Activation, Flatten, Conv2D,
MaxPooling2D
from keras.models import Sequential
from keras.utils import to_categorical
import numpy as np
import matplotlib.pyplot as plt
(train_X,train_Y), (test_X,test_Y) = mnist.load_data()
train_X = train_X.reshape(-1, 28,28, 1)
test_X = test_X.reshape(-1, 28,28, 1)
train_X = train_X.astype('float32')
test_X = test_X.astype('float32')
test_X = test_X / 255
train_Y_one_hot = to_categorical(train_Y)
test_Y_one_hot = to_categorical(test_Y)
model = Sequential()
model.add(Conv2D(64, (3,3), input_shape=(28, 28, 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(10))
model.add(Activation('softmax'))
model.compile(loss=keras.losses.categorical_crossentropy,
optimizer=keras.optimizers.Adam(),metrics=['accuracy'])
model.fit(train_X, train_Y_one_hot, batch_size=64, epochs=1)
test_loss, test_acc = model.evaluate(test_X, test_Y_one_hot)
print('Test loss', test_loss)
print('Test accuracy', test_acc)
model.save('123.model')
predictions = model.predict(test_X)
print(np.argmax(np.round(predictions[235])))
plt.imshow(test_X[235].reshape(28, 28), cmap = 'Greys_r')
plt.show()
Complete Code for Saving the Correctly Classified Test Images in the respective folders of their Labels
(0 to 9), 100 images in each folder is mentioned below:
import keras
from keras.datasets import mnist
from keras.layers import Dense, Activation, Flatten, Conv2D, MaxPooling2D
from keras.models import Sequential
from keras.utils import to_categorical
import numpy as np
import matplotlib.pyplot as plt
(train_X,train_Y), (test_X,test_Y) = mnist.load_data()
train_X = train_X.reshape(-1, 28,28, 1)
test_X = test_X.reshape(-1, 28,28, 1)
train_X = train_X.astype('float32')
train_X = train_X/255
test_X = test_X.astype('float32')
test_X = test_X / 255
#train_Y_one_hot = to_categorical(train_Y)
#test_Y_one_hot = to_categorical(test_Y)
model = Sequential()
model.add(Conv2D(64, (3,3), input_shape=(28, 28, 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(10))
model.add(Activation('softmax'))
model.compile(loss=keras.losses.sparse_categorical_crossentropy,
optimizer=keras.optimizers.Adam(),metrics=['accuracy'])
#model.fit(train_X, train_Y_one_hot, batch_size=64, epochs=1)
model.fit(train_X, train_Y, batch_size=64, epochs=1)
#test_loss, test_acc = model.evaluate(test_X, test_Y_one_hot)
test_loss, test_acc = model.evaluate(test_X, test_Y)
print('Test loss', test_loss)
print('Test accuracy', test_acc)
predictions = model.predict(test_X)
#****Actual Code which you need is mentioned below
import matplotlib
import matplotlib.pyplot as plt
import os
def save_fig(fig_id, Label):
path = os.path.join('MNIST_Images', Label, fig_id + "." + "png")
plt.tight_layout()
plt.savefig(path, format="png", dpi=300)
plt.close()
%matplotlib agg
%matplotlib agg #These 2 lines are required to prohibit Graph being displayed in Jupyter Notebook. You can comment these if you are using other IDE
No_Of_Rows = predictions.shape[0]
Count_Dict = {}
for i in range(10):
key = 'Count_' + str(i)
Count_Dict[key] = 0
for Each_Row in range(No_Of_Rows):
if np.argmax(predictions[Each_Row]) == test_Y[Each_Row]:
Label = str(test_Y[Each_Row])
Count_Dict['Count_' + Label] = Count_Dict['Count_' + Label] + 1
Count_Of_Label = Count_Dict['Count_' + Label]
if Count_Of_Label <= 100:
plt.imshow(test_X[Each_Row].reshape(28, 28), cmap = 'Greys_r')
plt.show()
save_fig(str(Count_Of_Label), Label)
I've commented the below lines of code which are not required, as the Labels are already in Numeric Format.
train_Y_one_hot = to_categorical(train_Y)
test_Y_one_hot = to_categorical(test_Y)
Also, I've replaced categorical_crossentropy with sparse_categorical_crossentropy in model.compile, as we are not Encoding the Variables.

InvalidArgumentError: input_1:0 is both fed and fetched, error with tensorflow, python

I am trying to run a code that tells me number of fall and non fall in fall detection of a human and i am getting an error :
input_1:0 is both fed and fetch .
I tried running it alone but never works.
from keras.models import Model
from keras.utils import np_utils
import numpy as np
import pandas as pd
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
import scipy.io as sio
import matplotlib.pyplot as plt
import keras
from keras.callbacks import ReduceLROnPlateau
from keras.layers.core import Flatten, Dense, Dropout, Lambda
from keras import backend as K
import cv2
def global_average_pooling(x):
return K.mean(x, axis = (2))
def global_average_pooling_shape(input_shape):
return input_shape[0:2]
p=Lambda(global_average_pooling,
output_shape=global_average_pooling_shape)
X = sio.loadmat('/Users/fateh/Documents/Hamidreza Work/ConvFall/ts.mat')
X=X['Data']
import csv
with open('/Users/fateh/Documents/Hamidreza Work/ConvFall/lab.csv', 'r') as mf:
re = csv.reader(mf,delimiter=',',quotechar='|')
re=np.array(list(re))
label = re.astype(np.float64)
Y_t=np.squeeze(label)
nb_epochs = 3
y_train =Y_t[:158]
y_test =Y_t[158:]
x_train=X[:158]
x_test=X[158:]
nb_classes = len(np.unique(y_test))
batch_size = min(x_train.shape[0]/8, 16)
y_train = (y_train - y_train.min())/(y_train.max()-y_train.min())*(nb_classes-1)
y_test = (y_test - y_test.min())/(y_test.max()-y_test.min())*(nb_classes-1)
Y_train = np_utils.to_categorical(y_train, nb_classes)
Y_test = np_utils.to_categorical(y_test, nb_classes)
x_train_mean = x_train.mean()
x_train_std = x_train.std()
x_train = (x_train - x_train_mean)/(x_train_std)
x_test = (x_test - x_train_mean)/(x_train_std)
x_train = x_train.reshape(x_train.shape + (1,))
x_test = x_test.reshape(x_test.shape + (1,))
#x_train = np.transpose(x_train, (0, 2, 1))
#x_test = np.transpose(x_test, (0, 2, 1))
input_shape=x_train.shape[1:]
x = keras.layers.Input(x_train.shape[1:])
# drop_out = Dropout(0.2)(x)
conv1 = keras.layers.Convolution1D(300, 9, padding='same')(x)
conv1 = keras.layers.normalization.BatchNormalization()(conv1)
conv1 = keras.layers.Activation('relu')(conv1)
conv2 = keras.layers.Convolution1D(200, 5, padding='same')(conv1)
conv2 = keras.layers.normalization.BatchNormalization()(conv2)
conv2 = keras.layers.Activation('relu')(conv2)
conv3 = keras.layers.Convolution1D(100, 3, padding='same')(conv2)
conv3 = keras.layers.normalization.BatchNormalization()(conv3)
conv3 = keras.layers.Activation('relu')(conv3)
full = p(conv3)
out = keras.layers.Dense(nb_classes, activation='softmax')(full)
model = Model(input=x, output=out)
optimizer = keras.optimizers.Adam() #'sgd'
model.compile(loss='categorical_crossentropy',
optimizer=optimizer,
metrics=['accuracy'])
reduce_lr = ReduceLROnPlateau(monitor = 'loss', factor=0.5,
patience=500, min_lr=0.001)
hist = model.fit(x_train, Y_train, batch_size=batch_size, epochs=nb_epochs,
verbose=1, validation_data=(x_test, Y_test), callbacks = [reduce_lr])
predict = model.predict(x_test)
preds = np.argmax(predict, axis=1)
log = pd.DataFrame(hist.history)
print(log.loc[log['loss'].idxmin]['loss'], log.loc[log['loss'].idxmin]['val_acc'])
labels = {1:'Non-Fall', 2:'Fall'}
from sklearn.metrics import classification_report, confusion_matrix
print(classification_report(preds, y_test,
target_names=[l for l in labels.values()]))
conf_mat = confusion_matrix(preds, y_test)
fig = plt.figure(figsize=(2,2))
res = plt.imshow(np.array(conf_mat), cmap=plt.cm.summer, interpolation='nearest')
for i, row in enumerate(conf_mat):
for j, c in enumerate(row):
if c>0:
plt.text(j-.2, i+.1, c, fontsize=16)
#cb = fig.colorbar(res)
plt.title('Confusion Matrix')
_ = plt.xticks(range(2), [l for l in labels.values()], rotation=90)
_ = plt.yticks(range(2), [l for l in labels.values()])
inp = model.input # input placeholder
outputs = [layer.output for layer in model.layers] # all layer outputs
functor = K.function([inp]+ [K.learning_phase()], outputs ) # evaluation function
# Testing
test=x_test[2:3,:,:]
test = np.random.random(input_shape)[np.newaxis,:]
layer_outs = functor([test, 1.])
Trying to run this code used for letting you know the fall detection of files through deep Convolutional Neural Network, where radar data is used to collect raw data and processed.
Skip the input layer,
outputs = [layer.output for layer in model.layers[1:]]
if you still need it, you can add an identity function.
outputs.insert(0, K.identity(inp))

Categories