Accuracy drops when using ImageDataGenerator

Viewed 16

I am working on skin disease classification and trying to build a CNN model. I have approximitly 200 images. I have splited my data using by importing train_test_split from sklearn.model_selection. code snippet:

data = [] #this is where I will store all the data

for category in categories:
    path = os.path.join(data_dir,category)
    # print(path)
    class_num = categories.index(category)
    # print(class_num)
    for img in os.listdir(path):
      # print(img)
            try:
                img_array = cv2.imread(os.path.join(path,img))
                new_array = cv2.resize(img_array,img_size) 
                data.append([new_array,class_num])
            except Exception as e:
                pass


X = []
Y = []
for features,label in data:
    X.append(features)
    Y.append(label)

X = np.array(X)
X = X.astype('float32')/255.0  
X = X.reshape(-1,height, width,3)
Y = np.array(Y)

from keras.utils.np_utils import to_categorical  
Y = to_categorical(Y, num_classes = 4)


from sklearn.model_selection import train_test_split

# train_ratio = 0.75
# validation_ratio = 0.15
# test_ratio = 0.10

X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.1, random_state=42, stratify=Y) 

X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size=0.15, random_state=1, stratify=y_train)
import pickle

pickle.dump(X_train, open('/content/drive/MyDrive/Data/X_train', 'wb'))
pickle.dump(y_train, open('/content/drive/MyDrive/Data/y_train', 'wb'))
pickle.dump(X_test, open('/content/drive/MyDrive/Data/X_test', 'wb'))
pickle.dump(y_test, open('/content/drive/MyDrive/Data/y_test', 'wb'))
pickle.dump(X_val, open('/content/drive/MyDrive/Data/X_val', 'wb'))
pickle.dump(y_val, open('/content/drive/MyDrive/Data/y_val', 'wb'))

The accuracy drastically drops when I use ImageDataGenerator for data augmentation. code snippet:

Adam(learning_rate=0.00001, name='Adam')
model.compile(optimizer = 'Adam',loss = 'categorical_crossentropy',metrics = ['accuracy'])

epochs = 80
from tensorflow.keras import callbacks
import time
import keras
from keras.callbacks import EarlyStopping
es_callback = keras.callbacks.EarlyStopping(monitor='val_accuracy', patience=20)

datagen = ImageDataGenerator(
                    rescale=1./255,
                    rotation_range=30,
                    shear_range=0.3,
                    zoom_range=0.3,
                    width_shift_range=0.4,
                    height_shift_range=0.4,
                    horizontal_flip=True,
                    fill_mode='nearest'
          )


checkpoint = callbacks.ModelCheckpoint(
                                       filepath='/content/drive/MyDrive/Model1/model.{epoch:02d}-{accuracy:.2f}-{val_accuracy:.2f}.h5',
                                       monitor='val_accuracy',
                                       verbose=1,
                                       save_best_only=True,
                                       mode='auto'
                                       )

history5 = model.fit(datagen.flow(X_train,
                      y_train, 
                      batch_size=32),
                      epochs = epochs, 
                      validation_data = (X_val,y_val)
                      )

without data sugmentation the validation accuracy is 55%

with data augmentation the validation accuracy is 30%

0 Answers
Related