[英]TypeError: can't pickle _thread.RLock objects ( Deep Learning)
I'm Getting:- loss: 0.4413 - accuracy: 0.8617 - val_loss: 0.7057 - val_accuracy: 0.8038我得到:- 损失:0.4413 - 准确度:0.8617 - val_loss:0.7057 - val_accuracy:0.8038
Code:
import numpy as np
import pandas as pd
from tensorflow.keras.preprocessing.image import ImageDataGenerator
import os
import random
import cv2
import imutils
import random
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.preprocessing import LabelBinarizer
from keras.utils import np_utils
from tensorflow.keras.models import Sequential
from tensorflow.keras import optimizers
from sklearn.preprocessing import LabelBinarizer
from tensorflow.keras import backend as K
from tensorflow.keras.layers import Dense, Activation, Flatten, Dense,MaxPooling2D, Dropout
from tensorflow.keras.layers import Conv2D, MaxPooling2D, BatchNormalization
DATADIR = "E:\Final Year project\Virtual Robotic Arm controlled using Hand Gesture Reconginse & AI\Traning Data\Dataset\Train"
CATEGORIES = ["0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","a1","b1","c1","d1","e1","f1","g1","h1","i1","j1","k1","l1","m1","n1","o1","p1","q1","r1","s1","t1","u1","v1","w1","x1","y1","z1"]
for category in CATEGORIES: #
path = os.path.join(DATADIR,category) # create path to dogs and cats
for img in os.listdir(path): # iterate over each image per dogs and cats
img_array = cv2.imread(os.path.join(path,img) ,cv2.IMREAD_GRAYSCALE) # convert to array
plt.imshow(img_array, cmap='gray') # graph it
plt.show() # display!
break # we just want one for now so break
break #...and one more!
img_array
print(img_array.shape)
# Image Resize
IMG_SIZE = 32
new_array = cv2.resize(img_array, (IMG_SIZE, IMG_SIZE))
plt.imshow(new_array, cmap='gray')
plt.show()
from tqdm import *
training_data = []
def create_training_data():
for category in CATEGORIES: # do dogs and cats
path = os.path.join(DATADIR,category) # create path to dogs and cats
class_num = CATEGORIES.index(category) # get the classification (0 or a 1). 0=dog 1=cat
for img in tqdm(os.listdir(path)): # iterate over each image per dogs and cats
try:
img_array = cv2.imread(os.path.join(path,img) ,cv2.IMREAD_GRAYSCALE) # convert to array
new_array = cv2.resize(img_array, (IMG_SIZE, IMG_SIZE)) # resize to normalize data size
training_data.append([new_array, class_num]) # add this to our training_data
except Exception as e: # in the interest in keeping the output clean...
pass
#except OSError as e:
# print("OSErrroBad img most likely", e, os.path.join(path,img))
#except Exception as e:
# print("general exception", e, os.path.join(path,img))
create_training_data()
print(len(training_data))
random.shuffle(training_data)
from sklearn.model_selection import train_test_split
train_data , test_data = train_test_split(training_data,test_size=0.2,random_state=42)
train_data , val_data = train_test_split(train_data,test_size=0.2,random_state=42)
print(len(train_data))--->4012
print(len(test_data))--->1255
print(len(val_data))---> 1004
train_X = []
train_Y = []
for features,label in train_data:
train_X.append(features)
train_Y.append(label)
test_X = []
test_Y = []
for features,label in test_data:
test_X.append(features)
test_Y.append(label)
val_X = []
val_Y = []
for features,label in val_data:
val_X.append(features)
val_Y.append(label)
LB = LabelBinarizer()
train_Y = LB.fit_transform(train_Y)
test_Y = LB.fit_transform(test_Y)
val_Y = LB.fit_transform(val_Y)
train_X = np.array(train_X)/255.0
train_X = train_X.reshape(-1,32,32,1)
train_Y = np.array(train_Y)
test_X = np.array(test_X)/255.0
test_X = test_X.reshape(-1,32,32,1)
test_Y = np.array(test_Y)
val_X = np.array(val_X)/255.0
val_X = val_X.reshape(-1,32,32,1)
val_Y = np.array(val_Y)
print(train_X.shape,test_X.shape,val_X.shape)---> (4012, 32, 32, 1) (1255, 32, 32, 1) (1004,
32, 32, 1)
print(train_Y.shape,test_Y.shape,val_Y.shape)---> (4012, 62) (1255, 62) (1004, 62)
model = Sequential()
model.add(Conv2D(32, (3, 3), padding = "same", activation='relu', input_shape=(32,32,1)))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Conv2D(128, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(62, activation='softmax'))
model.summary()
model.compile(loss='categorical_crossentropy', optimizer="adam",metrics=['accuracy'])
history = model.fit(train_X,train_Y, epochs=25, batch_size=32, validation_data = (val_X, val_Y), verbose=1)
import pickle
# Save the Modle to file in the current working directory
Filename = "Model.pkl"
with open(Filename, 'wb') as file:
pickle.dump(model, file)
TypeError Traceback (most recent call last) ~\AppData\Local\Temp/ipykernel_13132/1527247688.py in 5 6 with open(Pkl_Filename, 'wb') as file: ----> 7 pickle.dump(model, file) TypeError Traceback(最近一次调用最后一次)~\AppData\Local\Temp/ipykernel_13132/1527247688.py in 5 6 with open(Pkl_Filename, 'wb') as file: ----> 7 pickle.dump(model, file)
TypeError: can't pickle _thread.RLock objects类型错误:无法腌制 _thread.RLock 对象
This code executed successfully when I tried replicating the same error in both Google Colab
and Jupyter notebook
.当我尝试在Google Colab
和Jupyter notebook
中复制相同的错误时,此代码成功执行。 You can check the saved.pkl file in the left pane.您可以在左窗格中检查 saved.pkl 文件。
Please find the attached gist for your reference.(I have used binary dataset to reproduce the issue).请查找随附的要点供您参考。(我已使用二进制数据集来重现该问题)。
You can try again executing the same code and let us know if the issue still persists with the details of python and tensorflow version you are using.您可以再次尝试执行相同的代码,如果问题仍然存在,请告诉我们您正在使用的 python 和 tensorflow 版本的详细信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.