在keras-python中使用ImageDataGenerator进行数据增强

时间:2018-12-01 07:37:35

标签: python tensorflow keras conv-neural-network

我尝试使用imageDataGenerator进行数据增强,以遵循 CNN ,但我需要训练5种不同的图像类别。当我运行此代码时,发生以下错误:

"Traceback (most recent call last):

File "", line 1, in runfile('E:/Final Project/FinalProject/AIModule/cnn.py', wdir='E:/Final Project/FinalProject/AIModule')

File "C:\Users\Lakwin\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 705, in runfile execfile(filename, namespace)

File "C:\Users\Lakwin\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile exec(compile(f.read(), filename, 'exec'), namespace)

File "E:/Final Project/FinalProject/AIModule/cnn.py", line 166, in class_mode='categorical')

File "C:\Users\Lakwin\Anaconda3\lib\site-packages\keras_preprocessing\image.py", line 1013, in flow_from_directory interpolation=interpolation)

File "C:\Users\Lakwin\Anaconda3\lib\site-packages\keras_preprocessing\image.py", line 1857, in init interpolation)

File "C:\Users\Lakwin\Anaconda3\lib\site-packages\keras_preprocessing\image.py", line 1453, in common_init self.target_size = tuple(target_size)

TypeError: 'int' object is not iterable"

我该如何解决此错误以及我应该在此代码中进行哪些更改?

import cv2                
import numpy as np         
import os                
from random import shuffle 
from tqdm import tqdm      
#from tensorflow import keras
#from tensorflow.contrib import lite
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential

TRAIN_DIR = 'train'
VALID_DIR = 'validate'
TEST_DIR = 'test'
IMG_SIZE = 28
LR = 1e-3
train_samples = 250
valdate_samples = 250
epochs = 5
batch_size = 10

MODEL_NAME = 'snakes-{}-{}.model'.format(LR, '2conv-basic')

def label_img(img):
    print("\nImage = ",img)
    print("\n",img.split('.')[-2])
    temp_name= img.split('.')[-2]
    #print("\n",temp_name[0:3])
    #temp_name=temp_name[0:3]
    print("\n",temp_name[:1])
    temp_name=temp_name[:1]
    #word_label = img.split('.')[-3]
    word_label = temp_name

   # word_label = img[0]

    if word_label == 'A': return [0,0,0,0,1]    
    elif word_label == 'B': return [0,0,0,1,0]
    elif word_label == 'C': return [0,0,1,0,0]
    elif word_label == 'D': return [0,1,0,0,0]
    elif word_label == 'E' : return [1,0,0,0,0]   

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


def create_validate_data():
    validating_data = []
    for img in tqdm(os.listdir(TRAIN_DIR)):
        label = label_img(img)
        path = os.path.join(VALID_DIR,img)
        img = cv2.imread(path,cv2.IMREAD_GRAYSCALE)
        img = cv2.resize(img, (IMG_SIZE,IMG_SIZE))
        validating_data.append([np.array(img),np.array(label)])
    shuffle(validating_data)
    np.save('validate_data.npy', validating_data)
    return validating_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

train_data = create_train_data()
validate_data = create_validate_data()


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, 5, 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!')

#train = train_data[:-500]
#test = train_data[-500:]

#train = train_data[:-200]
#test = train_data[-200:]
train = train_data[0:]
validate = validate_data[0:]

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]
validate_x = np.array([i[0] for i in validate]).reshape(-1,IMG_SIZE,IMG_SIZE,1)
validate_y = [i[1] for i in validate]

model.fit({'input': X}, {'targets': Y}, n_epoch=epochs, validation_set=({'input': validate_x}, {'targets': validate_y}), 
    snapshot_step=500, show_metric=True, run_id=MODEL_NAME)


train_datagen = ImageDataGenerator(
    rescale=1. / 255,
    shear_range=0.2,
    zoom_range=0.2,
    horizontal_flip=True)

validaton_datagen = ImageDataGenerator(rescale=1. / 255)

train_generator = train_datagen.flow_from_directory('data/train',
                                                    target_size=(IMG_SIZE),
                                                    batch_size=batch_size,
                                                    class_mode='categorical')

validation_generator = validaton_datagen.flow_from_directory('data/validation',
                                                        target_size=(IMG_SIZE),
                                                        batch_size=batch_size,
                                                        class_mode='categorical')

model.fit_generator(train_generator,
                    steps_per_epoch=25,
                    epochs=epochs,
                    validation_data=validation_generator,
                    validation_steps=25)


model.save(MODEL_NAME)

#keras_file = "linear.h5"
#keras.models.save_model(model,keras_file)

1 个答案:

答案 0 :(得分:1)

您必须将整数元组作为target_size传递给train_datagen.flow_from_directory() method, actually a method of keras.preprocessing.image.ImageDataGenerator()实例的方法,但是您传递了整数 >,一维尺寸:

target_size=(IMG_SIZE)

那是你的错误信息。

来自Keras Image Preprocessing keras.preprocessing.image.ImageDataGenerator()

  

target_size:
  整数(height, width)的元组,                默认值:(256, 256)。                所有图像的尺寸                找到的内容将被调整大小。

您必须执行以下操作:

train_generator = train_datagen.flow_from_directory('data/train',
                                                    target_size=(IMG_SIZE, IMG_SIZE),
                                                    batch_size=batch_size,
                                                    class_mode='categorical')

validation_generator = validaton_datagen.flow_from_directory('data/validation',
                                                        target_size=(IMG_SIZE, IMG_SIZE),
                                                        batch_size=batch_size,
                                                        class_mode='categorical')