我正在使用python中的机器学习进行糖尿病性视网膜病变的检测。
我在训练数据中有一个35126图像,我想让它们进入数组,但是我在分配显示形状的数组(35126、224、224、3)时遇到问题
MemoryError:无法分配形状(35126、224、224、3)和数据类型为float32的数组
我有16GB内存
这是我使用keras的代码
import os, sys
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import cv2
from sklearn.svm import SVC
from PIL import Image
from sklearn.model_selection import train_test_split
from sklearn import preprocessing
import glob
import pywt
import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras.utils import to_categorical
from keras.preprocessing import image
from keras.utils import to_categorical
from tqdm import tqdm
from sklearn.neighbors import KNeighborsClassifier
from skimage import exposure
from sklearn.model_selection import KFold
from sklearn.ensemble import RandomForestClassifier
import warnings
warnings.filterwarnings("ignore")
train_image = []
df_train = pd.read_csv('trainlabel/trainLabels.csv')
y = df_train['level']
score_svm = []
score_rf = []
def getscore(model,x_train, x_test, y_train, y_test):
model.fit(x_train,y_train)
return model.score(x_test,y_test)
#tqdm(range(df_train.shape[0]))
fig = plt.figure(figsize=(25, 16))
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8))
for i in tqdm(range(df_train.shape[0])) :
img = image.load_img('train/'+'trainimage/'+'train/'+df_train['image'][i]+'.jpeg',target_size=(256,256,3))
img = image.img_to_array(img)
img = img / 255
print(type(img))
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img = exposure.equalize_hist(img)
img_adpeq = exposure.equalize_adapthist(img,clip_limit=0.03)
print(type(img_adpeq))
train_image.append(img_adpeq)
print(type(train_image))
X = np.asarray(train_image)
print(type(X))
nsamples, nx, ny = X.shape
X2 =X.reshape((nsamples,nx*ny))
print(X2.shape)
kf = KFold(n_splits=5)
for train_index, test_index in kf.split(X):
print("TRAIN:", train_index, "TEST:", test_index)
X_train, X_test = X[train_index],X[test_index]
y_train, y_test = y[train_index], y[test_index]
nsamples, nx, ny = X_train.shape
X_train = X_train.reshape((nsamples,nx*ny))
nsamples, nx, ny = X_test.shape
X_test = X_test.reshape((nsamples, nx * ny))
score_svm.append(getscore(SVC(),X_train,X_test,y_train,y_test))
score_rf.append(getscore(RandomForestClassifier(),X_train,X_test,y_train,y_test))
print("SVM")
print(score_svm)
print("Random Forest")
print(score_rf)
MemoryError:无法分配形状(35126、254、256、3)和数据类型为float32的数组
这是我的第一次,对不起,这似乎是一个肮脏的代码。