简体   繁体   English

我如何修复网络输入形状

[英]How can i fix network input shape

There is 1275 images which each image has dimension of (128,19,1).有 1275 张图像,每张图像的维度为 (128,19,1)。 these images are divided into groups of five, so there is 255 (1275/5) samples which each sample has 5 images and final shape of data is (255, 5, 128, 19, 1).这些图像被分成五组,所以有 255 (1275/5) 个样本,每个样本有 5 张图像,数据的最终形状是 (255, 5, 128, 19, 1)。 this data must be feed to a CONVLSTM2D.network which its code is as below.此数据必须提供给 CONVLSTM2D.network,其代码如下。 the process of training is done completely but at start of evaluation process it gives below error.培训过程已完全完成,但在评估过程开始时出现以下错误。 thanks if anyone can helps me to fix it.谢谢,如果有人可以帮助我修复它。

Error:错误:

IndexError: list index out of range IndexError:列表索引超出范围

File "", line 1, in runfile('D:/thesis/Paper 3/Feature Extraction/two_dimension_Feature_extraction/stft_feature/Training_set/P300/Afrah_convlstm2d.py', wdir='D:/thesis/Paper 3/Feature Extraction/two_dimension_Feature_extraction/stft_feature/Training_set/P300')文件“”,第 1 行,在 runfile('D:/thesis/Paper 3/Feature Extraction/two_dimension_Feature_extraction/stft_feature/Training_set/P300/Afrah_convlstm2d.py', wdir='D:/thesis/Paper 3/Feature Extraction/two_dimension_Feature_extraction /stft_feature/Training_set/P300')

File "C:\Users\pouyaandish\AppData\Local\conda\conda\envs\kafieh\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 786, in runfile execfile(filename, namespace)文件“C:\Users\pouyaandish\AppData\Local\conda\conda\envs\kafieh\lib\site-packages\spyder_kernels\customize\spydercustomize.py”,第 786 行,在运行文件 execfile(文件名,命名空间)中

File "C:\Users\pouyaandish\AppData\Local\conda\conda\envs\kafieh\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile exec(compile(f.read(), filename, 'exec'), namespace)文件“C:\Users\pouyaandish\AppData\Local\conda\conda\envs\kafieh\lib\site-packages\spyder_kernels\customize\spydercustomize.py”,第 110 行,在 execfile exec(compile(f.read() , 文件名, 'exec'), 命名空间)

File "D:/thesis/Paper 3/Feature Extraction/two_dimension_Feature_extraction/stft_feature/Training_set/P300/Afrah_convlstm2d.py", line 111, in test_loss, test_acc = seq.evaluate(test_data)文件“D:/thesis/Paper 3/Feature Extraction/two_dimension_Feature_extraction/stft_feature/Training_set/P300/Afrah_convlstm2d.py”,第 111 行,在 test_loss 中,test_acc = seq.evaluate(test_data)

File "C:\Users\pouyaandish\AppData\Local\conda\conda\envs\kafieh\lib\site-packages\keras\engine\training.py", line 1361, in evaluate callbacks=callbacks)文件“C:\Users\pouyaandish\AppData\Local\conda\conda\envs\kafieh\lib\site-packages\keras\engine\training.py”,第 1361 行,在 evaluate callbacks=callbacks 中)

File "C:\Users\pouyaandish\AppData\Local\conda\conda\envs\kafieh\lib\site-packages\keras\engine\training_arrays.py", line 403, in test_loop if issparse(ins[i]) and not K.is_sparse(feed[i]):文件“C:\Users\pouyaandish\AppData\Local\conda\conda\envs\kafieh\lib\site-packages\keras\engine\training_arrays.py”,第 403 行,在 test_loop if issparse(ins[i]) 和不是 K.is_sparse(feed[i]):

IndexError: list index out of range IndexError:列表索引超出范围

#Importing libraries
#-------------------------------------------------
from PIL import Image
from keras.models import Sequential
from keras.layers import Flatten
from keras.layers import Dense
from keras.layers.convolutional_recurrent import ConvLSTM2D
from keras.layers.normalization import BatchNormalization
import numpy as np
import os
from matplotlib import pyplot as plt


#Data Preprocessing
#-----------------------------------------------------------------
Data = np.zeros((255,5,128,19,1),dtype=np.uint8)

image_folder = 'D:\\thesis\\Paper 3\\Feature Extraction\\two_dimension_Feature_extraction\\stft_feature\\Training_set\\P300'
images = [img for img in os.listdir(image_folder) if img.endswith(".png")]

for image in images:
    img = Image.open(image).convert('L')
    array = np.array(img)
    array = np.expand_dims(np.array(img), axis=2)
    for i in range(0, len(Data)):
        for j in range(0, 4):
            Data[i,j] = array

           

labels = np.zeros((2,len(Data)), dtype=np.uint8)
labels = np.transpose(labels)
for i in range(0, len(Data) ):
    if i <= 127:
        labels[i][0] = 1
    elif i > 127 :
        labels[i][1] = 1            
            
#Network Configuration
#--------------------------------------------------------------------------------------------------------------------------
seq = Sequential()
seq.add(ConvLSTM2D(filters=40, kernel_size=(3, 3),
                   input_shape=(5, 128, 19, 1),
                   padding='same', return_sequences=True))
seq.add(BatchNormalization())

seq.add(ConvLSTM2D(filters=40, kernel_size=(3, 3),
                   padding='same', return_sequences=True))
seq.add(BatchNormalization())

seq.add(ConvLSTM2D(filters=40, kernel_size=(3, 3),
                   padding='same', return_sequences=True))
seq.add(BatchNormalization())

seq.add(ConvLSTM2D(filters=40, kernel_size=(3, 3),
                   padding='same', return_sequences=True))
seq.add(BatchNormalization())

seq.add(Flatten())
seq.add(Dense(output_dim = 128, activation = 'relu'))
seq.add(Dense(output_dim = 2, activation = 'relu'))
seq.compile(loss='binary_crossentropy', optimizer='adadelta', metrics=['acc'])

#Fit the Data on Model
#--------------------------------------------------------------------------------------
train_data_1 = Data[0:84]
train_data_2 = Data[127:212]
train_data = np.concatenate([train_data_1, train_data_2])
label_train_1 = labels[0:84]
label_train_2 = labels[127:212]
label_train = np.concatenate([label_train_1, label_train_2])

val_data_1 = Data[84:104]
val_data_2 = Data[212:232]
val_data = np.concatenate([val_data_1, val_data_2])
label_val_1 = labels[84:104]
label_val_2 = labels[212:232]
label_val = np.concatenate([label_val_1, label_val_2])


test_data_1 = Data[104:127]
test_data_2 = Data[232:]
test_data = np.concatenate([test_data_1, test_data_2])
label_test_1 = labels[104:127]
label_test_2 = labels[232:]
label_test = np.concatenate([label_test_1, label_test_2])


history = seq.fit(train_data,label_train, validation_data=( val_data, label_val), epochs = 2 , batch_size = 10)

#Visualize the Result
#---------------------------------------------------------------------------------------
acc = history.history['acc']
val_acc = history.history['val_acc']
loss = history.history['loss']
val_loss = history.history['val_loss']
epochs = range(1, len(acc) + 1)
plt.plot(epochs, acc, 'r', label='Training acc')
plt.plot(epochs, val_acc, 'b', label='Validation acc')
plt.title('Training and validation accuracy')
plt.legend()
plt.figure()
plt.plot(epochs, loss, 'r', label='Training loss')
plt.plot(epochs, val_loss, 'b', label='Validation loss')
plt.title('Training and validation loss')
plt.plot()
plt.legend()
plt.show()
#Evaluate Model on test Data
#----------------------------------------------------------------------------------------------
test_loss, test_acc = seq.evaluate(test_data)
print('test_acc:', test_acc)





     

The problem is at the end, when you evaluate your model, you just forgot to give the y argument.问题在最后,当你评估你的 model 时,你只是忘了给出y参数。 This modification should fix the problem:此修改应解决问题:

test_loss, test_acc = seq.evaluate(test_data, y=label_test)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何修复Conv1D的输入形状? -音频分类模型 - How can I fix my input shape for Conv1D? - Audio classification model 神经网络的输入形状 - Input shape of a neural network 如何修复崇高的文本输入? - How can I fix sublime text input? 如果我尝试拟合来自pandas数据框的数据,如何将input_shape赋予卷积神经网络? - How to give input_shape to Convolutional Neural Network if I am trying to fit the data from pandas dataframe? 如果我试图拟合熊猫数据帧中的数据,如何找到卷积神经网络的 input_shape? - How to find input_shape of Convolutional Neural Network if I am trying to fit the data from pandas dataframe? 如何解决 CNN 中的输入形状问题? - How can I solve the input shape issue in CNN? tfp.keras.layers:如果数据格式是 &#39;channels_first&#39; ,我可以将网络输入形状指定为 &#39;channels_last&#39; 吗? - tfp.keras.layers: If the data format is 'channels_first' , can I give the network input shape as 'channels_last'? 任意形状输入的简单网络 - Simple network for arbitrary shape input 如何校准我的分类神经网络的输入形状? - How to calibrate the input shape of my classification neural network? 如何在keras神经网络中正确设置input_shape? - How to properly set input_shape in a keras neural network?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM