KNN图像分类器:权限和内存错误

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了KNN图像分类器:权限和内存错误相关的知识,希望对你有一定的参考价值。

我正在尝试创建一个区分猫与狗的KNN图像分类器。但是,每次尝试进行预测时,都会遇到内存错误和权限错误:

import numpy as np
import matplotlib.pyplot as plt
import os
import cv2
import tensorflow as tf

Datadir ="C:/Users/NIMROID/Music/PetImages"
typess =["Dog", "Cat"]

for types in typess:
   path =os.path.join(Datadir, types)
      for img in os.listdir(path):
          img_array =cv2.imread(os.path.join(path,img), cv2.IMREAD_GRAYSCALE)
IMG_SIZE= 50
training_data = []

def create_training_data():
    for types in typess:
        path =os.path.join(Datadir, types)
        class_num = typess.index(types)
    for img in os.listdir(path):
        try:
            img_array =cv2.imread(os.path.join(path,img), cv2.IMREAD_GRAYSCALE)
            new_array =cv2.resize(img_array, (IMG_SIZE ,IMG_SIZE))
            training_data.append([new_array,class_num])
        except Exception as e:
            pass

        create_training_data()
        import random
        random.shuffle(training_data)
        X= []
        y= []
        for features, label in training_data:
             X.append(features)
            y.append(features)

       X= np.array(X)
       y=np.array(y)
       from sklearn.model_selection import train_test_split
       X_train,X_test , y_train,y_test = train_test_split(X,y, test_size=0.1)
       X_train=tf.keras.utils.normalize(X_train, axis=1)
       X_test= tf.keras.utils.normalize(X_test,axis=1)
       X_train= X_train.reshape(-1,IMG_SIZE*IMG_SIZE)
       X_test= X_test.reshape(-1,IMG_SIZE*IMG_SIZE)
       from sklearn.neighbors import KNeighborsClassifier as kn
       from sklearn.multioutput import MultiOutputClassifier
       knni= kn(n_neighbors =5)
       knn = MultiOutputClassifier(knni, n_jobs=-1)
       knn.fit(X_train,y_train)

〜.conda envs nimrod lib shutil.py在_rmtree_unsafe中(路径,错误)其他391条:392尝试:-> 393 os.unlink(全名)394(除OSError外):395 onerror(os.unlink,fullname,sys.exc_info())

PermissionError:[WinError 32]该进程无法访问文件因为它正在被另一个进程使用:'C: Users NIMROID AppData Local Temp\ joblib_memmapping_folder_9268_8897152710 9268-2914779649472-8c4617f562464b2e82b25e82d72afe18.pkl'MemoryError追溯(最近一次通话)〜.conda envs nimrod lib site-packages joblib parallel.py在个体检索中907如果getattr(self._backend,'supports_timeout',False):-> 908 self._output.extend(job.get(timeout = self.timeout))909其他:

       ~.condaenvs
imrodlibsite-packagesjoblib\_parallel_backends.py in

wrap_future_result(未来,超时)553尝试:-> 554返回future.result(timeout = timeout)555,除了LokyTimeoutError:

答案

阅读您的错误消息:PermissionError: [WinError 32] The process cannot access the file because it is being used by another process可以这么简单吗?

以上是关于KNN图像分类器:权限和内存错误的主要内容,如果未能解决你的问题,请参考以下文章

使用交叉验证的 KNN 分类器

Knn 分类器图

5月深度学习班第2课高效计算基础与图像线性分类器

使用 Knn 分类器时出现无效的形状错误

用 numpy 从头开始​​构建 KNN 分类器,代码有啥问题?

使用 KNN 分类器进行数字识别前的预处理