我正在尝试创建一个用于图像分类的卷积神经网络。我遇到一个问题,我的代码的最后两个单元导致我的内核死机。这是我的代码:
import numpy as np
import matplotlib.pyplot as plt
import glob
import cv2
import os
main_path = '/Users/myusername/Downloads/Rice_Image_Dataset/'
data_images = []
data_labels = []
for directory_path in glob.glob('/Users/myusername/Downloads/Rice_Image_Dataset/*'):
label = directory_path.split('/')[-1]
for img_path in glob.glob(os.path.join(directory_path, '*.jpg')):
img = cv2.imread(img_path, cv2.IMREAD_COLOR)
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
data_images.append(img)
data_labels.append(label)
class_count = []
for i in os.listdir(main_path):
if i == '.DS_Store':
continue
class_count.append(i)
class_count
import matplotlib.image as mpimg
k = 0
for cla in class_count:
if cla == 'Rice_Citation_Request.txt':
continue
for file in os.listdir(main_path + '/' + cla)[0:1]:
img=mpimg.imread(main_path+'/'+cla+'/'+file)
k=k+1
plt.subplot(3, 3, k)
plt.title(cla)
plt.imshow(img)
from sklearn.model_selection import train_test_split
data_images = np.array(data_images)
data_labels = np.array(data_labels)
from sklearn import preprocessing
label_encoding = preprocessing.LabelEncoder()
label_encoding.fit(data_labels)
data_encoded_labels = label_encoding.transform(data_labels)
data_encoded_labels
X = data_images
y = data_encoded_labels
X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.2,shuffle=True,random_state=0)
## normalize data
X_train, X_test = X_train / 255.0, X_test / 255.0
X_train.shape
X_train.shape 线似乎一直导致这个问题,不知道为什么。这是使用的数据集: https://www.kaggle.com/datasets/muratkokludataset/rice-image-dataset