我正在尝试运行一个代码,该代码告诉我人类跌倒检测中的跌倒次数和非跌倒次数,并且出现错误:
input_1:0被馈送和提取。
我尝试单独运行它,但从未成功。
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.])
尝试运行此代码,以使您知道通过深层卷积神经网络对文件的跌落检测,在深层卷积神经网络中,雷达数据用于收集原始数据并进行处理。
答案 0 :(得分:0)
跳过输入层
outputs = [layer.output for layer in model.layers[1:]]
如果仍然需要,可以添加一个身份功能。
outputs.insert(0, K.identity(inp))