【发布时间】:2022-06-22 18:18:10
【问题描述】:
我正在关注 Sentdex 的初学者深度学习教程系列。使用python .\model.py 训练模型时,抛出以下错误:
2022-01-29 19:39:03.518539: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'cudart64_110.dll'; dlerror: cudart64_110.dll not found
2022-01-29 19:39:03.519272: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
(70, 70)
2022-01-29 19:39:11.738598: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'nvcuda.dll'; dlerror: nvcuda.dll not found
2022-01-29 19:39:11.738714: W tensorflow/stream_executor/cuda/cuda_driver.cc:269] failed call to cuInit: UNKNOWN ERROR (303)
2022-01-29 19:39:11.742605: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:169] retrieving CUDA diagnostic information for host: AishikWindows11
2022-01-29 19:39:11.742901: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:176] hostname: AishikWindows11
2022-01-29 19:39:11.747739: I tensorflow/core/platform/cpu_feature_guard.cc:151] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: AVX AVX2
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
Traceback (most recent call last):
File "D:\Programming-on-Linux\Ml\Cats and Dogs Classifier\model.py", line 101, in <module>
model.fit(X, y, batch_size=32, epochs=6, validation_split=0.3, callbacks=[tensorboard])
File "C:\Users\ujana\AppData\Local\Programs\Python\Python39\lib\site-packages\keras\utils\traceback_utils.py", line 67, in error_handler
raise e.with_traceback(filtered_tb) from None
File "C:\Users\ujana\AppData\Local\Programs\Python\Python39\lib\site-packages\keras\engine\data_adapter.py", line 1482, in train_validation_split
raise ValueError(
ValueError: `validation_split` is only supported for Tensors or NumPy arrays, found following types in the input: [<class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>,
并且<class 'int'> 的这种语法在突然停止时继续运行,不会引发任何其他错误。我尝试使用 tensorboard 跟踪我的模型是否正确训练,但它说当时没有模型在训练。最终,模型没有训练。怎么办?
下面是 create_data.py 的代码,它预处理数据并将它们保存到各自的 .pickle 文件中
import numpy as np
import os
import cv2
import random
import pickle
DATADIR = "path to the images"
CATEGORIES = ["Dog", "Cat"]
IMG_RESOLUTION = 70
training_data = []
def create_dataset():
for category in CATEGORIES:
path = os.path.join(DATADIR,category)
images = os.listdir(path)
class_num = CATEGORIES.index(category)
for img in images:
try:
old_img_array = cv2.imread(os.path.join(path,img), cv2.IMREAD_GRAYSCALE)
new_img_array = cv2.resize(old_img_array, (IMG_RESOLUTION,IMG_RESOLUTION))
training_data.append([new_img_array, class_num])
except Exception as e:
pass
create_dataset()
random.shuffle(training_data)
X = []
y = []
for features,labels in training_data:
X.append(features)
y.append(labels)
X = np.array(X).reshape(-1, IMG_RESOLUTION, IMG_RESOLUTION, 1)
pickle_out = open("training_data/X.pickle","wb")
pickle.dump(X, pickle_out)
pickle_out.close()
pickle_out = open("training_data/y.pickle","wb")
pickle.dump(y, pickle_out)
pickle_out.close()
下面是model.py的代码
from tensorflow.keras.layers import Activation,Dropout, Dense, Flatten, Conv2D, MaxPooling2D
from tensorflow.keras.models import Sequential
from tensorflow.keras.callbacks import TensorBoard
import pickle
import time
import tensorflow as tf
NAME = f"Cats and Dogs Classifier {time.time()}"
tensorboard = TensorBoard(log_dir=f"logs/{NAME}")
X = pickle.load(open("training_data/X.pickle", "rb"))
y = pickle.load(open("training_data/y.pickle", "rb"))
X = X/255.0
model = Sequential()
model.add(Conv2D(256, (3, 3), input_shape=X.shape[1:]))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(256, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(3, 3)))
model.add(Flatten())
model.add(Dense(256))
model.add(Dense(1))
model.add(Activation('sigmoid'))
model.compile(
optimizer='adam',
loss='binary_crossentropy',
metrics=['accuracy']
)
model.fit(X, y, batch_size=32, epochs=6, validation_split=0.3, callbacks=[tensorboard])
【问题讨论】:
-
我知道这可能是一个愚蠢的评论,但您是否尝试过使用 numpy.arrays() 将给定列表转换为 numpy 数组
-
@Prats 是的,我已经将特征列表 X 转换为一个 numpy 数组并对其进行了重构。但我还没有将标签列表 y 转换为 np 数组。
标签: python tensorflow keras