text_plain_output_1.png
from keras import models, layers, backend, optimizers, regularizers, metrics #for model manipulation

from keras.applications import MobileNet

from keras.applications import VGG16

from keras.applications.resnet import ResNet50

from keras.callbacks import EarlyStopping, ModelCheckpoint, ReduceLROnPlateau

from sklearn.model_selection import train_test_split, StratifiedKFold, KFold, cross_val_score, GridSearchCV

from tensorflow.keras.applications import EfficientNetB0

import matplotlib.pyplot as plt #for plotting results

import pandas as pd

import tensorflow as tf

image_dir = '/kaggle/input/balanced-datasets/Adasyn_dataset'
df = pd.read_csv('/kaggle/input/balanced-datasets/Adasyn_dataset/labels.csv')
'\ny_one_hot = np.array(df.drop(columns = ["image"], axis = 1))\ny = np.argmax(y_one_hot, axis = 1)\ndf["label"] = y\ndf["label"] = df["label"].astype(str)\ndf[\'image\'] = df[\'image\']+\'.jpg\'\n'

from keras.applications import MobileNet
from keras.applications import VGG16
from keras.applications.resnet import ResNet50
from tensorflow.keras.applications import EfficientNetB0
def choose_conv_base(name='from_scratch', input_shape=(224, 224, 3)):
    if name == 'from_scratch':
        conv_base = models.Sequential()
        conv_base.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=input_shape))
        conv_base.add(layers.MaxPooling2D((2, 2)))
        conv_base.add(layers.Conv2D(64, (3, 3), activation='relu'))
        conv_base.add(layers.MaxPooling2D((2, 2)))
        conv_base.add(layers.Conv2D(128, (3, 3), activation='relu'))
        conv_base.add(layers.MaxPooling2D((2, 2)))
        conv_base.add(layers.Conv2D(128, (3, 3), activation='relu'))
        conv_base.add(layers.MaxPooling2D((2, 2)))
    elif name == 'mobilenet':
        conv_base = MobileNet(weights='imagenet', include_top=False, input_shape=input_shape)
        conv_base.trainable = False
    elif name == 'vgg16':
        conv_base = VGG16(weights='imagenet', include_top=False, input_shape=input_shape)
        conv_base.trainable = False
    elif name == 'resnet':
        conv_base = ResNet50(weights='imagenet', include_top=False, input_shape=input_shape)
        conv_base.trainable = False
    elif name == 'efficientnet':
        conv_base = EfficientNetB0(weights='imagenet', include_top=False, input_shape=input_shape)
        conv_base.trainable = False
    return conv_base

def build_model(input=(224, 224, 3), loss='categorical_crossentropy', optimizer=tf.keras.optimizers.experimental.AdamW()):
    model = models.Sequential()
    model.add(choose_conv_base(name='mobilenet'))
    model.add(layers.GlobalAveragePooling2D())
    model.add(layers.Dropout(0.2))
    model.add(layers.Dense(32, activation='relu'))
    model.add(layers.Dense(7, activation='softmax'))
    model.compile(optimizer='adam', loss=loss, metrics=['accuracy'])
    return model
es = EarlyStopping(monitor='val_accuracy', verbose=1, min_delta=0.01, patience=10)
mc = ModelCheckpoint(monitor='val_accuracy', verbose=1, filepath='./1_best.h5', save_best_only=True)
reducelr = ReduceLROnPlateau(monitor='val_accuracy', verbose=1, patience=5, factor=0.5, min_lr=1e-07)
cb = [mc]
batch_size = 32
epochs = 100

""""""
train_val_df, test_df = train_test_split(df, stratify=df['label'], test_size=0.1, random_state=42)
train_df, validation_df = train_test_split(train_val_df, stratify=train_val_df['label'], test_size=0.2, random_state=42)
augmented_datagen = ImageDataGenerator(rescale=1.0 / 255, rotation_range=40, width_shift_range=0.2, height_shift_range=0.2, shear_range=0.2, zoom_range=0.2, horizontal_flip=True, fill_mode='nearest')
datagen = ImageDataGenerator(rescale=1.0 / 255)
train_generator = datagen.flow_from_dataframe(train_df, directory=image_dir, batch_size=batch_size, target_size=(224, 224), x_col='filename', y_col='label', class_mode='categorical', shuffle=True)
val_generator = datagen.flow_from_dataframe(validation_df, directory=image_dir, batch_size=batch_size, target_size=(224, 224), x_col='filename', y_col='label', class_mode='categorical', shuffle=True)
model = build_model()
history = model.fit(train_generator, steps_per_epoch=len(train_df) // batch_size, epochs=epochs, validation_data=val_generator, validation_steps=len(validation_df) // batch_size, callbacks=cb)
history_dict = history.history
loss_values = history_dict['loss']
val_loss_values = history_dict['val_loss']
acc_values = history_dict['accuracy']
val_acc_values = history_dict['val_accuracy']
def smooth_curve(points, factor=0.8):
    smoothed_points = []
    for point in points:
        if smoothed_points:
            previous = smoothed_points[-1]
            smoothed_points.append(previous * factor + point * (1 - factor))
        else:
            smoothed_points.append(point)
    return smoothed_points
epochs = range(1, len(loss_values) + 1)

datagen = ImageDataGenerator(rescale=1.0 / 255)
test_generator = datagen.flow_from_dataframe(test_df, directory=image_dir, x_col='filename', y_col='label', target_size=(224, 224), batch_size=20, class_mode='categorical')
print('last model testing:')
result = model.evaluate(test_generator)
print(result)
print('best model testing:')
best_model = models.load_model('./1_best.h5')
result = best_model.evaluate(test_generator)
print(result)