我正在试着运行这个代码
利用
卷积神经网络(CNN)
. 这段代码可以在网上查到,但它对图像中是否有狗或猫进行了分类。它
工作正常
具有
分类但是
当我更改培训和测试目录时
到X射线
我得到一个错误
值错误:用序列设置数组元素。
我试着看类似的问题,但不明白如何解决这个问题。
谢谢
import cv2
import os
import numpy as np
from random import shuffle
from tqdm import tqdm
'''Setting up the env'''
TRAIN_DIR = 'C:\\Users\\waqar\\Desktop\\Temp\\chest_xray\\train'
TEST_DIR = 'C:\\Users\\waqar\\Desktop\\Temp\\chest_xray\\test'
IMG_SIZE = 50
LR = 1e-3
'''Setting up the model which will help with tensorflow models'''
MODEL_NAME = 'dogsvscats-{}-{}.model'.format(LR, '6conv-basic')
'''Labelling the dataset'''
def label_img(img):
word_label = img.split('.')[-3]
if word_label == 'cat':
return [1, 0]
elif word_label == 'dog':
return [0, 1]
'''Creating the training data'''
def create_train_data():
training_data = []
for img in tqdm(os.listdir(TRAIN_DIR)):
label = label_img(img)
path = os.path.join(TRAIN_DIR, img)
img = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
img = cv2.resize(img, (IMG_SIZE, IMG_SIZE))
training_data.append([np.array(img), np.array(label)])
shuffle(training_data)
np.save('train_data.npy', training_data)
return training_data
'''Processing the given test data'''
def process_test_data():
testing_data = []
for img in tqdm(os.listdir(TEST_DIR)):
path = os.path.join(TEST_DIR, img)
img_num = img.split('.')[0]
img = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
img = cv2.resize(img, (IMG_SIZE, IMG_SIZE))
testing_data.append([np.array(img), img_num])
shuffle(testing_data)
np.save('test_data.npy', testing_data)
return testing_data
'''Running the training and the testing in the dataset for our model'''
train_data = create_train_data()
test_data = process_test_data()
train_data = np.load('train_data.npy')
test_data = np.load('test_data.npy')
'''Creating the neural network using tensorflow'''
import tflearn
from tflearn.layers.conv import conv_2d, max_pool_2d
from tflearn.layers.core import input_data, dropout, fully_connected
from tflearn.layers.estimator import regression
import tensorflow as tf
tf.reset_default_graph()
convnet = input_data(shape=[None, IMG_SIZE, IMG_SIZE, 1], name='input')
convnet = conv_2d(convnet, 32, 5, activation='relu')
convnet = max_pool_2d(convnet, 5)
convnet = conv_2d(convnet, 64, 5, activation='relu')
convnet = max_pool_2d(convnet, 5)
convnet = conv_2d(convnet, 128, 5, activation='relu')
convnet = max_pool_2d(convnet, 5)
convnet = conv_2d(convnet, 64, 5, activation='relu')
convnet = max_pool_2d(convnet, 5)
convnet = conv_2d(convnet, 32, 5, activation='relu')
convnet = max_pool_2d(convnet, 5)
convnet = fully_connected(convnet, 1024, activation='relu')
convnet = dropout(convnet, 0.8)
convnet = fully_connected(convnet, 2, activation='softmax')
convnet = regression(convnet, optimizer='adam', learning_rate=LR,
loss='categorical_crossentropy', name='targets')
model = tflearn.DNN(convnet, tensorboard_dir='log')
train = train_data[:-500]
test = train_data[-500:]
'''Setting up the features and lables'''
X = np.array([i[0] for i in train]).reshape(-1, IMG_SIZE, IMG_SIZE, 1)
Y = [i[1] for i in train]
test_x = np.array([i[0] for i in test]).reshape(-1, IMG_SIZE, IMG_SIZE, 1)
test_y = [i[1] for i in test]
'''Fitting the data into our model'''
model.fit({'input': X}, {'targets': Y}, n_epoch=5,
validation_set=({'input': test_x}, {'targets': test_y}),
snapshot_step=500, show_metric=True, run_id=MODEL_NAME)
model.save(MODEL_NAME)
'''Testing the data'''
import matplotlib.pyplot as plt
# if you need to create the data:
test_data = process_test_data()
# if you already have some saved:
test_data = np.load('test_data.npy')
fig = plt.figure()
for num, data in enumerate(test_data[:20]):
# cat: [1, 0]
# dog: [0, 1]
img_num = data[1]
img_data = data[0]
y = fig.add_subplot(4, 5, num + 1)
orig = img_data
data = img_data.reshape(IMG_SIZE, IMG_SIZE, 1)
model_out = model.predict([data])[0]
if np.argmax(model_out) == 1:
str_label = 'Dog'
else:
str_label = 'Cat'
y.imshow(orig, cmap='gray')
plt.title(str_label)
y.axes.get_xaxis().set_visible(False)
y.axes.get_yaxis().set_visible(False)
plt.show()**
显示的完整错误为:
C:\Users\waqar\AppData\Local\Programs\Python\Python36\python.exe C:/Users/waqar/.PyCharm2018.2/config/scratches/scratch.py
100%|ââââââââââ| 5200/5200 [01:00<00:00, 85.55it/s]
Traceback (most recent call last):
File "C:/Users/waqar/.PyCharm2018.2/config/scratches/scratch.py", line 87, in <module>
train_data = create_train_data()
File "C:/Users/waqar/.PyCharm2018.2/config/scratches/scratch.py", line 63, in create_train_data
np.save('train_data.npy', training_data)
File "C:\Users\waqar\AppData\Local\Programs\Python\Python36\lib\site-packages\numpy\lib\npyio.py", line 509, in save
arr = np.asanyarray(arr)
File "C:\Users\waqar\AppData\Local\Programs\Python\Python36\lib\site-packages\numpy\core\numeric.py", line 544, in asanyarray
return array(a, dtype=long, copy=False, order=order, subok=True)
ValueError: setting an array element with a sequence.
Process finished with exit code 1
定义标签(img):
word_label=img.split('.')[-3]
if word_label == 'cat':
return [1, 0]
elif word_label == 'dog':
return [0, 1]
else : #add this line
return [0, 0]