输入数组和目标数组有问题

时间:2019-07-25 06:34:52

标签: keras

我有425个256和155个光谱带的Hyperion图像要分类为8个类别.im编码新手,我用自己的自定义数据替换了印度松树数据集。当我执行一些代码进行分类时出现此错误高光谱数据显示为“输入阵列应具有与目标阵列相同的样本数。找到了24485个输入样本和23321个目标样本。”即使误差仍然相同,我也已将批次大小从32更改为16。

batch_size = 16
nb_epoch = 80
ITER = 10
PATCH_LENGTH = 4

img_rows = 2*PATCH_LENGTH+1
img_cols = 2*PATCH_LENGTH+1
img_channels = data_hsi.shape[2]
INPUT_DIMENSION = data_hsi.shape[2]

VAL_SIZE = int(0.5*TRAIN_SIZE)
TEST_SIZE = TOTAL_SIZE - TRAIN_SIZE

data = preprocessing.scale(data)
data_ = data.reshape(data_hsi.shape[0], data_hsi.shape[1], data_hsi.shape[2])
whole_data = data_
padded_data = np.lib.pad(whole_data, ((PATCH_LENGTH, PATCH_LENGTH), (PATCH_LENGTH, PATCH_LENGTH), (0, 0)),
                         'constant', constant_values=0)

day = datetime.datetime.now()
day_str = day.strftime('%m_%d_%H_%M')

KAPPA = []
OA = []
AA = []
TRAINING_TIME = []
TESTING_TIME = []
ELEMENT_ACC = np.zeros((ITER, nb_classes))

seeds = [1331, 1332, 1333, 1334, 1335, 1336, 1337, 1338, 1339, 1340, 1341]


for index_iter in range(ITER):
    print("-----Starting the  %d Iteration-----" % (index_iter + 1))
    '''
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=VALIDATION_SPLIT , random_state=index_iter)
    '''
    best_weights_path = 'D:/Myfiles/Conferences/ISRS/code/FDSSC-master/models/FDSSC'+day_str+'@'+str(index_iter+1)+'.hdf5'

    np.random.seed(seeds[index_iter])
    train_indices, test_indices = sampling(VALIDATION_SPLIT, gt)

    TRAIN_SIZE = len(train_indices)
    print('Train size: ', TRAIN_SIZE)
    TEST_SIZE = TOTAL_SIZE - TRAIN_SIZE
    print('Test size: ', TEST_SIZE)
    VAL_SIZE = int(0.5*TRAIN_SIZE)
    print('Validation size: ', VAL_SIZE)

    y_train = gt[train_indices]-1
    y_train = to_categorical(np.asarray(y_train))

    y_test = gt[test_indices]-1
    y_test = to_categorical(np.asarray(y_test))

    print('-----Selecting Small Pieces from the Original Cube Data-----')
    train_data = extract_samll_cubic.select_small_cubic(TRAIN_SIZE, train_indices, whole_data,
                                                        PATCH_LENGTH, padded_data, INPUT_DIMENSION)
    test_data = extract_samll_cubic.select_small_cubic(TEST_SIZE, test_indices, whole_data,
                                                       PATCH_LENGTH, padded_data, INPUT_DIMENSION)

    x_train = train_data.reshape(train_data.shape[0], train_data.shape[1], train_data.shape[2], INPUT_DIMENSION)
    x_test_all = test_data.reshape(test_data.shape[0], test_data.shape[1], test_data.shape[2], INPUT_DIMENSION)

    x_val = x_test_all[-VAL_SIZE:]
    y_val = y_test[-VAL_SIZE:]

    x_test = x_test_all[:-VAL_SIZE]
    y_test = y_test[:-VAL_SIZE]

在这里我附上了我得到的错误

  File "<ipython-input-1-e1fca114f860>", line 1, in <module>
    runfile('D:/Myfiles/Conferences/ISRS/code/FDSSC-master/FDSSY-cnn.py', wdir='D:/Myfiles/Conferences/ISRS/code/FDSSC-master')

  File "C:\Users\user\Anaconda3\envs\FDSSc\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 668, in runfile
    execfile(filename, namespace)

  File "C:\Users\user\Anaconda3\envs\FDSSc\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 108, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "D:/Myfiles/Conferences/ISRS/code/FDSSC-master/FDSSY-cnn.py", line 185, in <module>
    batch_size=batch_size)

  File "C:\Users\user\Anaconda3\envs\FDSSc\lib\site-packages\keras\engine\training.py", line 1100, in evaluate
    batch_size=batch_size)

  File "C:\Users\user\Anaconda3\envs\FDSSc\lib\site-packages\keras\engine\training.py", line 802, in _standardize_user_data
    check_array_length_consistency(x, y, sample_weights)

  File "C:\Users\user\Anaconda3\envs\FDSSc\lib\site-packages\keras\engine\training_utils.py", line 236, in check_array_length_consistency
    'and ' + str(list(set_y)[0]) + ' target samples.')

ValueError: Input arrays should have the same number of samples as target arrays. Found 24485 input samples and 23321 target samples.

0 个答案:

没有答案