Я попробовал следующий код.
import cv2 # working with, mainly resizing, images
import numpy as np # dealing with arrays
import os # dealing with directories
from random import shuffle # mixing up or currently ordered data that might lead our network astray in training.
from tqdm import tqdm # a nice pretty percentage bar for tasks. Thanks to viewer Daniel BA1/4hler for this suggestion
TRAIN_DIR = 'D:/Plant/train'`enter code here`
TEST_DIR = 'D:/Plant/test'
IMG_SIZE = 50
LR = 1e-3
MODEL_NAME = 'healthyvsunhealthy-{}-{}.model'.format(LR, '2conv-basic') # just so we remember which saved model is which, sizes must match
#to convert the images and labels to array information that we can pass through network
def label_img(img):
word_label = img[0] # conversion to one-hot array [h,b,v,l]
if word_label == 'h': return [1,0,0,0] #match h
elif word_label == 'b': return [0,1,0,0]
elif word_label == 'p': return [0,0,1,0]
elif word_label == 'r': return [0,0,0,1]
#build another function to fully process the training images and their labels into arrays:
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_COLOR)
if img is not None:
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
#make a function to process the testing 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_COLOR)
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
#we can run the training:
train_data = create_train_data()
# If you have already created the dataset:
#train_data = np.load('train_data.npy')
#we're ready to define our neural network:
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
from keras.models import Sequential
import matplotlib.pyplot as plt
#from zipline.api import get_environment
tf.reset_default_graph()
#shape [batch_size, image_height, image_width, channels]
convnet = input_data(shape=[None, IMG_SIZE, IMG_SIZE, 3], name='input')
# convolution layer by using the “Conv2D”
convnet = conv_2d(convnet, 32, 3, activation='relu')
convnet = max_pool_2d(convnet, 3)
convnet = conv_2d(convnet, 64, 3, activation='relu')
convnet = max_pool_2d(convnet, 3)
convnet = conv_2d(convnet, 128, 3, activation='relu')
convnet = max_pool_2d(convnet, 3)
convnet = conv_2d(convnet, 32, 3, activation='relu')
convnet = max_pool_2d(convnet, 3)
convnet = conv_2d(convnet, 64, 3, activation='relu')
convnet = max_pool_2d(convnet, 3)
#Dense Layer: 1,024 neurons, with dropout regularization rate of 0.6
convnet = fully_connected(convnet, 1024, activation='relu')
convnet = dropout(convnet, 0.6)
convnet = fully_connected(convnet, 4, activation='softmax')
convnet = regression(convnet, optimizer='adam', learning_rate=LR,loss='categorical_crossentropy', name='targets')
model = tflearn.DNN(convnet, tensorboard_dir='log')
if os.path.exists('{}.meta'.format(MODEL_NAME)):
model.load(MODEL_NAME)
print('model loaded!')
#split out training and testing data
train = train_data[:-60]
test = train_data[-50:]
#test = test_data[:-10]
X = np.array([i[0] for i in train]).reshape(-1,IMG_SIZE,IMG_SIZE,3)
Y = [i[1] for i in train]
test_x = np.array([i[0] for i in test]).reshape(-1,IMG_SIZE,IMG_SIZE,3)
test_y = [i[1] for i in test]
history=model.fit({'input': X}, {'targets': Y}, n_epoch=8, validation_set=({'input': test_x}{'targets': test_y}),snapshot_step=40, show_metric=True, run_id=MODEL_NAME)
model.save(MODEL_NAME)
Я уже попробовал следующий ответ [ Как построить график эпохи против val_a cc и графика эпохи против val_loss в CNN? но не смог бы подготовить эти гапы. при попытке training_loss = history.history['loss']
показывать компилятор, AttributeError: объект 'NoneType' не имеет атрибута 'history'