首页 > 解决方案 > ValueError:请提供单个数组或数组列表作为模型输入

问题描述


我正在尝试训练一个 resnet 模型,我的代码如下:

resnet = models.resnet50(pretrained=True)
for param in resnet.parameters():
    param.requires_grad = False

import torch.nn as nn
n_inputs = resnet.fc.in_features
resnet.fc = nn.Linear(n_inputs,5)

^ 这里我刚刚导入了模型

然后我像这样导入了我的图像:

from tqdm import tqdm
import csv
from tensorflow.keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array,array_to_img, load_img,save_img
import cv2 
ImageNameDataHash = {}
global ImageNameDataHash
path = '/content/drive/My Drive/Colab Notebooks/Dataset'
directories = ['train/1', 'test/1', 'val/1']
for dir in directories: 
  images = os.listdir(os.path.join(path,dir))
  print("Number of files in " + dir + " is " + str(len(images)))
  for imageFileName in tqdm(images):
    imgFullPath = os.path.join(path,dir,imageFileName)
    img = load_img(imgFullPath)
    arr = img_to_array(img)
    del(img)
    arr = cv2.resize(arr, (224,224))
    arr = cv2.addWeighted(arr,4,cv2.GaussianBlur(arr,(0,0),10),-4,128) / 255.0
    print(arr.shape)
    print(arr.shape)
    ImageNameDataHash[str(imageFileName)] = arr
    del(arr)
    

然后最后将我所有的数据放入一个看起来像这样的数据框中

index   image       data
802 4232_left   [[[0.5019608, 0.5019608, 0.5019608], [0.501960...
334 762_right   [[[0.50148463, 0.5013814, 0.50132805], [0.5014...
1190    5319_right  [[[0.5019608, 0.5019608, 0.5019608], [0.501960...
633 3940_left   [[[0.49936426, 0.4985614, 0.4992031], [0.49936...
345 783_right   [[[0.50195587, 0.5019481, 0.50195193], [0.5019...
... ... ...
167 492_left    [[[0.5019608, 0.5019608, 0.5019608], [0.501960...
463 963_left    [[[0.5018181, 0.5018181, 0.5018181], [0.501817...
609 3907_left   [[[0.5019608, 0.5019608, 0.5019608], [0.501960...
1498    7182_right  [[[0.5019608, 0.5019608, 0.5019608], [0.501960...
1162    5220_left   [[[0.5018678, 0.5018602, 0.5018561], [0.501860...

其中 data 是图像数据

model = Model(resnet)
# Set Optimizer
from keras.optimizers import adam

opt = adam(lr=0.001, decay=1e-6)

# Compile model
model.compile(
    loss='sparse_categorical_crossentropy',
    optimizer=opt,
    metrics=['accuracy'])

history = model.fit(X_train,Y_train,epochs=3,batch_size = 20)

当我尝试拟合我的模型时,出现以下错误

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-103-e2b53994c24f> in <module>()
----> 1 history = model.fit(X_train,Y_train,epochs=3,batch_size = 20)

1 frames
/usr/local/lib/python3.6/dist-packages/keras/engine/training.py in _standardize_user_data(self, x, y, sample_weight, class_weight, check_array_lengths, batch_size)
    495                                      'either a single '
    496                                      'array or a list of arrays. '
--> 497                                      'You passed: x=' + str(x))
    498                 all_inputs.append(x)
    499 

ValueError: Please provide as model inputs either a single array or a list of arrays. You passed: x=           image                                               data
802    4232_left  [[[0.5019608, 0.5019608, 0.5019608], [0.501960...
334    762_right  [[[0.50148463, 0.5013814, 0.50132805], [0.5014...
1190  5319_right  [[[0.5019608, 0.5019608, 0.5019608], [0.501960...
633    3940_left  [[[0.49936426, 0.4985614, 0.4992031], [0.49936...
345    783_right  [[[0.50195587, 0.5019481, 0.50195193], [0.5019...
...          ...                                                ...
167     492_left  [[[0.5019608, 0.5019608, 0.5019608], [0.501960...
463     963_left  [[[0.5018181, 0.5018181, 0.5018181], [0.501817...
609    3907_left  [[[0.5019608, 0.5019608, 0.5019608], [0.501960...
1498  7182_right  [[[0.5019608, 0.5019608, 0.5019608], [0.501960...
1162   5220_left  [[[0.5018678, 0.5018602, 0.5018561], [0.501860...

[1057 rows x 2 columns]

我不明白,我传递数据的方式有问题吗?或者这里有什么问题?我无法弄清楚我的生活。

标签: pythonkerasdeep-learning

解决方案


推荐阅读