【发布时间】:2021-11-23 17:44:21
【问题描述】:
当我尝试在 7 波段光谱数据上运行 1D CNN 时出现以下错误。类标签是二进制的,我想要一个输出。我附上代码和输出。显然,Conv1D 似乎在摄取 7 个输入波段时遇到了一些问题。我尝试查看已经发布的帖子,但没有取得多大成功。
import numpy as np
import rasterio
from rasterio.plot import show
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from tensorflow import keras
from sklearn.metrics import confusion_matrix, precision_score, recall_score
from tensorflow.keras.utils import plot_model
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Input
from tensorflow.keras.layers import Dense, Conv1D, MaxPooling1D
from tensorflow.keras.layers import Concatenate
from spectral import *
import pysptools.spectro as spectro
import copy
import os
import matplotlib
from sklearn.preprocessing import OneHotEncoder
import pandas as pd
import shutil
#%% DEFINE THE 1D CNN MODEL ARCHITECTURE
filters=4
kernel_size=4
visible = Input(shape=(7,1))
hidden1 = Conv1D (filters=filters, kernel_size=kernel_size, input_shape=(7,1), activation='relu', name="L1")(visible)
hidden2 = MaxPooling1D()(hidden1)
hidden3 = Dense(2, activation='relu', name="L3")(hidden2)
# hidden4 = Dense(2, activation='relu', name="L4")(hidden3)
output = Dense(1, activation='softmax', name="Output")(hidden3)
model = Model(inputs=visible, outputs=output)
# Shapes of the input data and label
print("\n7 band spectral data shape:", ern_T_c_d_v.shape)
print("Class labels shape:", ern_Mask_c_d_v.shape)
# Split into test and train
xTrain, xTest, yTrain, yTest = train_test_split(ern_T_c_d_v, ern_Mask_c_d_v, test_size=0.5, random_state=42)
# Note that I have transposed the arrays...
print("\nBefore reshaping")
print("xTrain:", xTrain.shape)
print("yTrain", yTrain.shape)
print("xTest:",xTest.shape)
print("yTest:",yTest.shape)
xTrain = np.reshape(xTrain, (xTrain.shape[0], 1, xTrain.shape[1]))
xTest = np.reshape(xTest, (xTest.shape[0], 1, xTest.shape[1]))
# xTrain=np.expand_dims(xTrain, axis=1)
# xTest=np.expand_dims(xTrain, axis=1)
print("\nReshaped...")
print("xTrain:", xTrain.shape)
print("yTrain:", yTrain.shape)
print("xTest:", xTest.shape)
print("yTest:",yTest.shape)
#%%
# Define the accuracy metrics and parameters
# opt=keras.optimizers.Adam(learning_rate=0.001)
model.compile(optimizer='adam', loss="binary_crossentropy", metrics=["accuracy"])
# Run the model
history = model.fit(xTrain, yTrain, epochs=1500)
pd.DataFrame(history.history).plot(figsize=(6,4))
plt.savefig(path+os.sep+'History')
plt.show()
print(history.params)
#%%
# Predict for test data
yTestPredicted = model.predict(xTest)
而且,这就是我得到的:
Warning 1: TIFFReadDirectoryCheckOrder:Invalid TIFF directory; tags are not sorted in ascending order
Warning 1: TIFFReadDirectory:Sum of Photometric type-related color channels and ExtraSamples doesn't match SamplesPerPixel. Defining non-color channels as ExtraSamples.
Model: "model_15"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_19 (InputLayer) [(None, 7, 1)] 0
_________________________________________________________________
L1 (Conv1D) (None, 4, 6) 30
_________________________________________________________________
max_pooling1d_15 (MaxPooling (None, 2, 6) 0
_________________________________________________________________
L3 (Dense) (None, 2, 4) 28
_________________________________________________________________
Output (Dense) (None, 2, 1) 5
=================================================================
Total params: 63
Trainable params: 63
Non-trainable params: 0
_________________________________________________________________
None
7 band spectral data shape: (1201, 7)
Class labels shape: (1201,)
Before reshaping
xTrain: (600, 7)
yTrain (600,)
xTest: (601, 7)
yTest: (601,)
Reshaped...
xTrain: (600, 1, 7)
yTrain: (600,)
xTest: (601, 1, 7)
yTest: (601,)
Traceback (most recent call last):
File "/Volumes/256GB/Ceres_Red_FC/NN_Codes/NN_Functional_CNN_1.py", line 181, in <module>
history = model.fit(xTrain, yTrain, epochs=1500)
File "/Users/pragya/opt/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/training.py", line 728, in fit
use_multiprocessing=use_multiprocessing)
File "/Users/pragya/opt/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/training_v2.py", line 224, in fit
distribution_strategy=strategy)
File "/Users/pragya/opt/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/training_v2.py", line 547, in _process_training_inputs
use_multiprocessing=use_multiprocessing)
File "/Users/pragya/opt/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/training_v2.py", line 594, in _process_inputs
steps=steps)
File "/Users/pragya/opt/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/training.py", line 2472, in _standardize_user_data
exception_prefix='input')
File "/Users/pragya/opt/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/training_utils.py", line 574, in standardize_input_data
str(data_shape))
ValueError: Error when checking input: expected input_19 to have shape (7, 1) but got array with shape (1, 7)
我无法理解为什么 Conv1D 不能接受 7 个输入波段。任何想法如何解决这个问题?提前谢谢...
【问题讨论】:
-
尝试将您的输入重塑为
(600, 7,1) -
@Nachiket 我已经尝试过了。没用...
-
好的,你能发布一个最小的可重现示例,包含所有数组,例如
ern_T_c_d_v已定义?如果您也可以减少依赖项的数量,那就太好了。 -
看来您需要设置
input_shape=(1,7)而不是(7, 1)。我不确定您的数据是什么样的 - 但在我看来,每个波段都是一个特征或不同的列等。
标签: python tensorflow keras conv-neural-network valueerror