Lasgne performs much worse than Keras? - python

I'm doing a comparison between Keras (with Theano) and Lasagne on a toy regression problem in order to choose one of the two for my final application. As a result of this comparison, I see that Lasagne is performing so much worse than Keras that I'm starting to doubt about my code. Since I'm quite new to both Keras and Lasagne, I would like to check this with someone more experienced than me. The network should be trained to find the mean of a 16x16 matrix. I made different try: first, tried with a 2D conv layer + dense layer (since my final application will require using CNN). Then, since Lasagne results were horrible, I tried with a standard one layer MLP. Again, awful Lasagne performance. I tried to use same specs for both cases: same batch size, same initialization, same optimizer (tested both SGD with Nesterov momentum and ADAM), and of course, same number of epochs and network architecture. Can someone tell me what is going on? Is there something wrong in my code? Why so much difference in the performance? If everything is correct, why Keras perform so much better than Lasagne?
Here the codes I am using:
Keras:
# -*- coding: utf-8 -*-
import numpy as np
np.random.seed(1337) # for reproducibility
from keras.models import Sequential
from keras.layers import Dense, Activation, Flatten
from keras.layers import Convolution2D
from keras import backend as K
from keras.optimizers import SGD
import matplotlib.pyplot as plt
batch_size = 500
nb_output = 1
nb_epoch = 10
# input image dimensions
img_rows, img_cols = 16, 16
# number of convolutional filters to use
nb_filters = 20
# size of pooling area for max pooling
pool_size = (2, 2)
# convolution kernel size
kernel_size = (3, 3)
X_train = np.random.randn(10000, 16*16)
Y_train = np.mean(X_train, 1)
X_train = X_train.astype('float32')
X_test = np.random.randn(1000, 16*16)
Y_test = np.mean(X_test, 1)
if K._BACKEND == 'theano':
X_train = np.reshape(X_train, (10000, 1, 16, 16))
X_test = np.reshape(X_test, (1000, 1, 16, 16))
else:
X_train = np.reshape(X_train, (10000, 16, 16, 1))
X_test = np.reshape(X_test, (1000, 16, 16, 1))
print('X_train shape:', X_train.shape)
print(X_train.shape[0], 'train samples')
print(X_test.shape[0], 'test samples')
model = Sequential()
model.add(Convolution2D(nb_filters, kernel_size[0], kernel_size[1],
border_mode='same',
input_shape=X_train.shape[1:], init='glorot_uniform'))
model.add(Activation('relu'))
#model.add(Flatten(input_shape=X_train.shape[1:]))
model.add(Flatten())
model.add(Dense(10, init='glorot_uniform'))
model.add(Activation('sigmoid'))
model.add(Dense(nb_output, init='glorot_uniform'))
model.add(Activation('linear'))
sgd = SGD(lr=0.1, momentum=0.9, nesterov=True)#decay=1e-6,
model.compile(loss='mse',
optimizer=sgd)
model.fit(X_train, Y_train, batch_size=batch_size, nb_epoch=nb_epoch,
verbose=1, validation_data=(X_test, Y_test))
score = model.evaluate(X_test, Y_test, verbose=1)
predicts = model.predict(X_test, batch_size=1000, verbose=0)
print('Test score:', score[0])
plt.figure()
plt.scatter(Y_test, predicts)
Lasagne (adapted from mnist example):
# -*- coding: utf-8 -*-
from __future__ import print_function
import time
import numpy as np
import theano
import theano.tensor as T
import lasagne
import matplotlib.pyplot as plt
def load_dataset():
np.random.seed(1337)
X_train = np.random.randn(10000, 16*16)
X_train = X_train.astype('float32')
Y_train = np.mean(X_train, 1)
X_test = np.random.randn(1000, 16*16)
X_test = X_test.astype('float32')
Y_test = np.mean(X_test, 1)
X_train = np.reshape(X_train, (10000, 1, 16, 16))
X_test = np.reshape(X_test, (1000, 1, 16, 16))
return X_train, Y_train, X_test, Y_test
def build_cnn(input_var=None):
network = lasagne.layers.InputLayer(shape=(None, 1, 16, 16),
input_var=input_var)
network = lasagne.layers.Conv2DLayer(
network, num_filters=20, filter_size=(3, 3),
nonlinearity=lasagne.nonlinearities.rectify,
W=lasagne.init.GlorotUniform())
network = lasagne.layers.DenseLayer(
network,
num_units=10,
nonlinearity=lasagne.nonlinearities.sigmoid)
network = lasagne.layers.DenseLayer(
network,
num_units=1,
nonlinearity=lasagne.nonlinearities.linear)
return network
def iterate_minibatches(inputs, targets, batchsize, shuffle=False):
assert len(inputs) == len(targets)
if shuffle:
indices = np.arange(len(inputs))
np.random.shuffle(indices)
for start_idx in range(0, len(inputs) - batchsize + 1, batchsize):
if shuffle:
excerpt = indices[start_idx:start_idx + batchsize]
else:
excerpt = slice(start_idx, start_idx + batchsize)
yield inputs[excerpt], targets[excerpt]
def main(model='cnn', num_epochs=10):
print("Loading data...")
X_train, y_train, X_test, y_test = load_dataset()
input_var = T.tensor4('inputs')
target_var = T.vector('targets')
print("Building model and compiling functions...")
network = build_cnn(input_var)
prediction = lasagne.layers.get_output(network)
loss = lasagne.objectives.squared_error(prediction, target_var)
loss = loss.mean()
params = lasagne.layers.get_all_params(network, trainable=True)
updates = lasagne.updates.nesterov_momentum(
loss, params, learning_rate=0.1, momentum=0.9)
# updates = lasagne.updates.adam(loss, params)
test_prediction = lasagne.layers.get_output(network)
test_loss = lasagne.objectives.squared_error(test_prediction,
target_var)
test_loss = test_loss.mean()
train_fn = theano.function([input_var, target_var], loss, updates=updates)
val_fn = theano.function([input_var, target_var], test_loss)
preds = theano.function([input_var], test_prediction)
print("Starting training...")
for epoch in range(num_epochs):
train_err = 0.0
train_batches = 0
start_time = time.time()
for batch in iterate_minibatches(X_train, y_train, 500, shuffle=False):
inputs, targets = batch
train_err += train_fn(inputs, targets)
train_batches += 1
test_err = 0.0
test_batches = 0
for batch in iterate_minibatches(X_test, y_test, 500, shuffle=False):
inputs, targets = batch
err = val_fn(inputs, targets)
test_err += err
test_batches += 1
print("Epoch {} of {} took {:.3f}s".format(
epoch + 1, num_epochs, time.time() - start_time))
print(" training loss:\t\t{:.6f}".format(train_err / train_batches))
print(" test loss:\t\t{:.6f}".format(test_err / test_batches))
pds = preds(X_test)
plt.scatter(y_test, pds)
plt.show()
if __name__ == '__main__':
main()
Both codes are easily adaptable to a one layer MLP. If you run them, you will get this scatter plot at the end:
lasagne:
keras:
.
On x axis: true values, on y axis predicted values.

Related

Unsupervised Learning Autoencoder multiple output CNN for Feature Extraction on MNIST data

I want to get features from a dataset (here MNIST) to put them into a cluster algorithm like k-means. The feature generation should be unsuperviesed, so I choose autoencoding to do it. I want that the Autoencoder is only trained by the loss in the second output layer 'output_layer2' and after the training I want to get the features from the first output layer 'output_layer1'. To get two output layer I choose funtional API in keras. But I don't know how to get the features and how to train only on one loss. Furthermore I get error massages like the ones under my code:
import numpy as np
import sys
import sklearn
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.models import Model, Sequential
from tensorflow.keras.layers import Input, Reshape, Conv2D, MaxPool2D, Conv2DTranspose
from tensorflow.keras.optimizers import SGD
assert sys.version_info >= (3, 5)
# Scikit-Learn ≥0.20 is required
assert sklearn.__version__ >= "0.20"
# TensorFlow ≥2.0-preview is required
assert tf.__version__ >= "2.0"
def get_MNISTdata(data_sz=10000):
(X_train_full, y_train_full), (
X_test,
y_test,
) = keras.datasets.mnist.load_data()
X_train_full = X_train_full.astype(np.float32) / 255
X_test = X_test.astype(np.float32) / 255
data_tr = X_train_full.shape[0] - data_sz
X_train, X_valid = X_train_full[:-data_tr], X_train_full[-data_tr:]
y_train, y_valid = y_train_full[:-data_tr], y_train_full[-data_tr:]
return X_train, X_valid, y_train, y_valid
def rounded_accuracy(y_true, y_pred):
return keras.metrics.binary_accuracy(tf.round(y_true), tf.round(y_pred))
def convoluntional_autoencoder(
X_train,
X_valid,
shape1=28,
shape2=28,
channels=1,
initial_features=16,
kernel_sz=3,
padding1="SAME",
padding2="VALID",
activation1="selu",
activation2="sigmoid",
pool_sz=2,
strides=2,
loss1="binary_crossentropy",
loss2="softmax",
lr=1.0,
epochs=3,
):
tf.random.set_seed(42)
np.random.seed(42)
# encoder
input_layer = Input(shape=(shape1, shape2, channels))
Layer1 = Conv2D(
initial_features,
kernel_size=kernel_sz,
padding=padding1,
activation=activation1,
)(input_layer)
Layer2 = MaxPool2D(pool_size=pool_sz)(Layer1)
Layer3 = Conv2D(
initial_features * 2,
kernel_size=kernel_sz,
padding=padding1,
activation=activation1,
)(Layer2)
Layer4 = MaxPool2D(pool_size=pool_sz)(Layer3)
Layer5 = Conv2D(
initial_features * 4,
kernel_size=kernel_sz,
padding=padding1,
activation=activation1,
)(Layer4)
output_layer1 = MaxPool2D(pool_size=pool_sz)(Layer5)
n_poolingLayer = 3
# decoder
Layer7 = Conv2DTranspose(
initial_features * 2,
kernel_size=kernel_sz,
strides=strides,
padding=padding2,
activation=activation1,
input_shape=[
int(shape1 / (2 ** n_poolingLayer)),
int(shape2 / (2 ** n_poolingLayer)),
initial_features * 2 ** (n_poolingLayer - 1),
],
)(output_layer1)
Layer8 = Conv2DTranspose(
initial_features,
kernel_size=kernel_sz,
strides=strides,
padding=padding1,
activation=activation1,
)(Layer7)
output_layer2 = Conv2DTranspose(
channels,
kernel_size=kernel_sz,
strides=strides,
padding=padding1,
activation=activation2,
)(Layer8)
conv_ae = Model(inputs=input_layer, outputs=[output_layer1, output_layer2])
conv_ae.compile(
loss={"output_layer1": loss1, "output_layer2": loss1},
loss_weights=[0, 1],
optimizer=SGD(lr=lr),
metrics=[rounded_accuracy],
)
conv_ae.fit(
X_train,
X_train,
epochs=epochs,
validation_data=(X_valid, X_valid),
)
conv_ae.summary()
# features = ?
return # features
If I do this
X_train, X_valid, y_train, y_valid = get_MNISTdata(data_sz=10000)
and this
features = convoluntional_autoencoder(
X_train,
X_valid,
shape1=28,
shape2=28,
channels=1,
initial_features=16,
kernel_sz=3,
padding1="SAME",
padding2="VALID",
activation1="selu",
activation2="sigmoid",
pool_sz=2,
strides=2,
loss1="binary_crossentropy",
loss2="softmax",
lr=1.0,
epochs=1,
)
I get at the column conv_ae.fit(...) the error:
ValueError: Found unexpected losses or metrics that do not correspond to any Model
output: dict_keys(['output_layer1', 'output_layer2']). Valid mode output names: ['max_pooling2d_5', 'conv2d_transpose_5'].
Received struct is: {'output_layer1': 'binary_crossentropy', 'output_layer2': 'binary_crossentropy'}.
If I change the conv_ae.compile-column to that:
conv_ae.compile(
loss=loss1,
loss_weights=[0, 1],
optimizer=SGD(lr=lr),
metrics=[rounded_accuracy],
)
I get at the column conv_ae.fit(...) the error:
ValueError: Dimensions must be equal, but are 28 and 3 for '{{node binary_crossentropy/mul}}
= Mul[T=DT_FLOAT](IteratorGetNext:1, binary_crossentropy/Log)' with input shapes: [?,28,28], [?,3,3,64].

I have this error in Stacked-autoencoder algorithm, ValueError: logits and labels must have the same shape ((None, 20) vs (None, 1))

I have this error when I apply stacked-autoencoder as feature selection.
ValueError: logits and labels must have the same shape ((None, 20) vs (None, 1))
This is the code of the Stacked-autoencoder::
import tensorflow
from tensorflow.keras import layers
from tensorflow.keras import activations
from tensorflow.python.keras.layers import Input, Dense
from tensorflow.python.keras.models import Model
from sklearn.preprocessing import MinMaxScaler
def StackedAutoencoder(x_train, x_test, y_train, y_test):
scalar = MinMaxScaler()
x_train = scalar.fit_transform(x_train)
x_test = scalar.fit_transform(x_test)
input_size = x_train.shape[1]
hidden_size = 9
code_size = 3
input_data = Input(shape=(input_size,))
hidden_1 = Dense(hidden_size, activation='relu')(input_data)
code = Dense(code_size, activation='relu')(hidden_1)
hidden_2 = Dense(hidden_size, activation='relu')(code)
# decode
output_data = Dense(input_size, activation='sigmoid')(hidden_2)
autoencoder = Model(input_data, output_data)
autoencoder.compile(optimizer='adam', loss='binary_crossentropy')
autoencoder.fit(x_train, y_train, epochs=30, verbose=0,
batch_size=10, validation_data=(x_test, y_test))
reconstructions = autoencoder.predict(x_test)
return reconstructions
This is the code where I call the algorithm in main:
import SAE as FS # SAE the file of stacked-autoencoder algorithm
print('CurrentFeatureSelection = ', 'Stacked_Autoencoder')
X_train_fs, X_test_fs = FS.StackedAutoencoder(X_train, X_test, y_train, y_test)

Scalar quantization of a specific layer in keras & python

I'm doing a experiment in which I need to quantize a specific layer of a convolutional model. However, even using high number of bits (e.g 12 bits with SQNR of ~60dB), when I set the quantized weights and bias into the model, the accuracy reduces from ~99% to ~10%.
I'm quantizing using this function:
def scalar_quantization(signal, bits):
print('bits used: ', bits)
min_val = np.min(signal)
max_val = np.max(signal)
codebook = np.linspace(min_val, max_val, 2**bits)
quantized_signal = np.zeros(len(signal))
for i in range(0, len(signal)):
quantized_signal[i] = closest(codebook, signal[i])
noise = signal- quantized_signal
snr = 10*math.log10(np.var(signal)/np.var(noise))
return quantized_signal, snr
#Auxiliar function
def closest(lst, K):
return lst[min(range(len(lst)), key = lambda i: abs(lst[i]-K))]
I'm set the weights using the following code (I'm trying to quantize the first layer):
quantized_layer = []
layer_names_it = 1
quantized_layer.append(np.array(np.reshape(quantized_weights, layer_shape),dtype=np.float32))
quantized_layer.append(np.array(np.reshape(quantized_bias, bias_shape), dtype=np.float32))
model.layers[layer_names_it].set_weights(quantized_layer)
Then, when I test the model again, I've got an accuracy of 10%. Without quantization, the accuracy was ~99%.
Therefore, what I'm doing wrong?
PS: Bellow is the entire code used here for reproduction
import math
import keras
import numpy as np
import math
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras import backend as K
def create_and_train_model():
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='softmax'))
model.compile(loss=keras.losses.categorical_crossentropy,
optimizer=keras.optimizers.Adadelta(),
metrics=['accuracy'])
return model
def scalar_quantization(signal, bits):
min_val = np.min(signal)
max_val = np.max(signal)
codebook = np.linspace(min_val, max_val, 2**bits)
quantized_signal = np.zeros(len(signal))
for i in range(0, len(signal)):
quantized_signal[i] = closest(codebook, signal[i])
noise = signal- quantized_signal
snr = 10*math.log10(np.var(signal)/np.var(noise))
return quantized_signal, snr
def closest(lst, K):
return lst[min(range(len(lst)), key = lambda i: abs(lst[i]-K))]
batch_size = 128
num_classes = 10
epochs = 12
# input image dimensions
img_rows, img_cols = 28, 28
# the data, split between train and test sets
(x_train, y_train), (x_test, y_test) = mnist.load_data()
if K.image_data_format() == 'channels_first':
x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols)
x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols)
input_shape = (1, img_rows, img_cols)
else:
x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)
x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)
input_shape = (img_rows, img_cols, 1)
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
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 = create_and_train_model()
model.fit(x_train, y_train,
batch_size=batch_size,
epochs=epochs,
verbose=1,
validation_data=(x_test, y_test))
score = model.evaluate(x_test, y_test, verbose=0)
print('Test loss without quantization:', score[0])
print('Test accuracy without quantization:', score[1])
layer_names=[layer.name for layer in model.layers] #Get the names of all layers
layer_names_it = 1 #In this example, I'm only quantizing the first layer
layer = model.get_layer(layer_names[layer_names_it]) #Get the desired layer
layer_shape = np.shape(layer.get_weights()[0])
bias_shape = np.shape(layer.get_weights()[1])
weights = np.reshape(layer.get_weights()[0].tolist(),-1,1) #Convert it to a vector of one dimension to quantize it
bias = np.reshape(layer.get_weights()[1].tolist(),-1,1)#Convert it to a vector of one dimension to quantize it
print('quantization step...')
quantized_weights, snr = scalar_quantization(weights, bits = 12)
noise_weights = weights - quantized_weights
snr_weights = 10*math.log10(np.var(weights) / np.var(noise_weights))
print('SNR quantized weights: ', snr_weights)
quantized_bias, snr = scalar_quantization(bias, bits = 12)
noise_bias = bias - quantized_bias
snr_bias = 10*math.log10(np.var(bias) / np.var(noise_bias))
print('SNR quantized bias: ', snr_bias)
quantized_layer = []
quantized_layer.append(np.array(np.reshape(quantized_weights, layer_shape), dtype = np.float32))
quantized_layer.append(np.array(np.reshape(quantized_bias, bias_shape), dtype = np.float32))
model.layers[layer_names_it].set_weights(quantized_layer)
score = model.evaluate(x_test, y_test, verbose=1)
print('Test loss with quantization:', score[0])
print('Test accuracy with quantization:', score[1]) ```

Fix the reshaping target when combining Keras CNN with SVM clasifier

I was trying to to use the combination of SVM with my CNN code, so I used this code. However, I got some problems in the part of reshaping the target to fit SVM.
The orignal X_train.shape, X_test.shape, y_train.shape, y_test.shape are correspondingly :
(2480, 1, 513, 125)
(560, 1, 513, 125)
(2480, 2)
(560, 2)
After that when I tried this :
exTrain = getFeature([X_train[:50], 0])[0]
exTest = getFeature([X_test[:10], 0])[0]
y_train = y_train[:50].reshape(y_train[:50].shape[0],)
I got this error message :
ValueError: cannot reshape array of size 100 into shape (50,)
This is my code
import os
import numpy as np
from sklearn.metrics import confusion_matrix
from plot_metrics import plot_accuracy, plot_loss, plot_roc_curve
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras.utils import np_utils
from keras import backend as K
K.set_image_dim_ordering('th')
np.random.seed(15)
"""
CNN used to classify spectrograms of normal participants (0) or depressed
participants (1). Using Theano backend and Theano image_dim_ordering:
(# channels, # images, # rows, # cols)
(1, 3040, 513, 125)
"""
def preprocess(X_train, X_test):
"""
Convert from float64 to float32 and normalize normalize to decibels
relative to full scale (dBFS) for the 4 sec clip.
"""
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
X_train = np.array([(X - X.min()) / (X.max() - X.min()) for X in X_train])
X_test = np.array([(X - X.min()) / (X.max() - X.min()) for X in X_test])
return X_train, X_test
def prep_train_test(X_train, y_train, X_test, y_test, nb_classes):
"""
Prep samples ands labels for Keras input by noramalzing and converting
labels to a categorical representation.
"""
print('Train on {} samples, validate on {}'.format(X_train.shape[0],
X_test.shape[0]))
# normalize to dBfS
X_train, X_test = preprocess(X_train, X_test)
# Convert class vectors to binary class matrices
Y_train = np_utils.to_categorical(y_train, nb_classes)
Y_test = np_utils.to_categorical(y_test, nb_classes)
return X_train, X_test, Y_train, Y_test
def keras_img_prep(X_train, X_test, img_dep, img_rows, img_cols):
"""
Reshape feature matrices for Keras' expexcted input dimensions.
For 'th' (Theano) dim_order, the model expects dimensions:
(# channels, # images, # rows, # cols).
"""
if K.image_dim_ordering() == 'th':
X_train = X_train.reshape(X_train.shape[0], 1, img_rows, img_cols)
X_test = X_test.reshape(X_test.shape[0], 1, img_rows, img_cols)
input_shape = (1, img_rows, img_cols)
else:
X_train = X_train.reshape(X_train.shape[0], img_rows, img_cols, 1)
X_test = X_test.reshape(X_test.shape[0], img_rows, img_cols, 1)
input_shape = (img_rows, img_cols, 1)
return X_train, X_test, input_shape
def cnn(X_train, y_train, X_test, y_test, batch_size,
nb_classes, epochs, input_shape):
"""
The Convolutional Neural Net architecture for classifying the audio clips
as normal (0) or depressed (1).
"""
model = Sequential()
model.add(Conv2D(32, (3, 3), padding='valid', strides=1,
input_shape=input_shape, activation='relu'))
model.add(MaxPooling2D(pool_size=(4, 3), strides=(1, 3)))
model.add(Conv2D(32, (1, 3), padding='valid', strides=1,
input_shape=input_shape, activation='relu'))
model.add(MaxPooling2D(pool_size=(1, 3), strides=(1, 3)))
model.add(Flatten())
model.add(Dense(512, activation='relu'))
model.add(Dense(512, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(nb_classes))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy',
optimizer='adadelta',
metrics=['accuracy'])
history = model.fit(X_train, y_train, batch_size=batch_size, epochs=epochs,
verbose=1, validation_data=(X_test, y_test))
# Evaluate accuracy on test and train sets
score_train = model.evaluate(X_train, y_train, verbose=0)
print('Train accuracy:', score_train[1])
score_test = model.evaluate(X_test, y_test, verbose=0)
print('Test accuracy:', score_test[1])
return model, history
def model_performance(model, X_train, X_test, y_train, y_test):
"""
Evaluation metrics for network performance.
"""
y_test_pred = model.predict_classes(X_test)
y_train_pred = model.predict_classes(X_train)
y_test_pred_proba = model.predict_proba(X_test)
y_train_pred_proba = model.predict_proba(X_train)
# Converting y_test back to 1-D array for confusion matrix computation
y_test_1d = y_test[:, 1]
# Computing confusion matrix for test dataset
conf_matrix = standard_confusion_matrix(y_test_1d, y_test_pred)
print("Confusion Matrix:")
print(conf_matrix)
return y_train_pred, y_test_pred, y_train_pred_proba, \
y_test_pred_proba, conf_matrix
def standard_confusion_matrix(y_test, y_test_pred):
"""
Make confusion matrix with format:
-----------
| TP | FP |
-----------
| FN | TN |
-----------
Parameters
----------
y_true : ndarray - 1D
y_pred : ndarray - 1D
Returns
-------
ndarray - 2D
"""
[[tn, fp], [fn, tp]] = confusion_matrix(y_test, y_test_pred)
return np.array([[tp, fp], [fn, tn]])
if __name__ == '__main__':
print('Retrieving locally')
X_train = np.load('E:/depression detection/data/processed/train_samples.npz')
y_train = np.load('E:/depression detection/data/processed/train_labels.npz')
X_test = np.load('E:/depression detection/data/processed/test_samples.npz')
y_test = np.load('E:/depression detection/data/processed/test_labels.npz')
X_train, y_train, X_test, y_test = \
X_train['arr_0'], y_train['arr_0'], X_test['arr_0'], y_test['arr_0']
# CNN parameters
batch_size = 32
nb_classes = 2
epochs = 1
# normalalize data and prep for Keras
print('Processing images for Keras...')
X_train, X_test, y_train, y_test = prep_train_test(X_train, y_train,
X_test, y_test,
nb_classes=nb_classes)
# 513x125x1 for spectrogram with crop size of 125 pixels
img_rows, img_cols, img_depth = X_train.shape[1], X_train.shape[2], 1
# reshape image input for Keras
# used Theano dim_ordering (th), (# chans, # images, # rows, # cols)
X_train, X_test, input_shape = keras_img_prep(X_train, X_test, img_depth,
img_rows, img_cols)
# run CNN
print('Fitting model...')
model, history = cnn(X_train, y_train, X_test, y_test, batch_size,
nb_classes, epochs, input_shape)
# evaluate model
print('Evaluating model...')
y_train_pred, y_test_pred, y_train_pred_proba, y_test_pred_proba, \
conf_matrix = model_performance(model, X_train, X_test, y_train, y_test)
for l in range(len(model.layers)):
print(l, model.layers[l])
# feature extraction layer
getFeature = K.function([model.layers[0].input, K.learning_phase()],
[model.layers[7].output])
# classification layer
getPrediction = K.function([model.layers[8].input, K.learning_phase()],
[model.layers[9].output])
exTrain = getFeature([X_train[:50], 0])[0]
exTest = getFeature([X_test[:10], 0])[0]
y_train = y_train[:50].reshape(y_train[:50].shape[0],)
y_test = y_test[:10]
print(exTrain.shape, exTest.shape, y_train.shape, y_test.shape)
from sklearn.svm import SVC
clf = SVC(gamma='auto')
clf.fit(exTrain, y_train)
score_train = model.evaluate(exTrain, y_train, verbose=0)
print('Train accuracy:', score_train[1])
score_test = model.evaluate(exTest, y_test, verbose=0)
print('Test accuracy:', score_test[1])
I don't know how to fix this problem.

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