使用预训练模型的训练模型keras准确性没有提高

时间:2019-01-02 21:55:22

标签: python keras computer-vision conv-neural-network

所以不知道该怎么办。我使用的是InceptionResnetV2预训练模型,然后进行微调以将其用于我的数据集,该数据集是从Kaggle跑步或步行的图像。当我对猫和狗做相同的事情时,此模型有效。但是,当我取消冻结所有图层时,它可以为该项目学习。我的准确性并没有真正提高,只是从不同的值跳下并重复。我是菜鸟,因此对代码错误或错误表示歉意。但是任何提示都会有所帮助。

我尝试过的事情
1-增加/减少学习率

2-更改batch_size和steps_per_batch

import matplotlib.pyplot as plt
import PIL
import tensorflow as tf
import numpy as np
import os
from keras.models import Model, Sequential
from keras.layers import Dense, Flatten, Dropout
from keras.applications.inception_resnet_v2 import InceptionResNetV2
from keras.applications.inception_resnet_v2 import preprocess_input, decode_predictions
from keras.preprocessing.image import ImageDataGenerator
from keras.optimizers import Adam, RMSprop
from keras.preprocessing import image
from keras.models import Model
from keras.layers import Dense, GlobalAveragePooling2D
from keras import backend as K


# to join directories together
def path_join(dirname, filenames):
    return [os.path.join(dirname, filename) for filename in filenames]


def plot_images(images, cls_true, cls_pred = None, smooth = True):
    assert len(images) == len(cls_true)

    # Create figure with sub-plots.
    fig, axes = plt.subplots(3, 3)

    if cls_pred is None:
        height = 0.3

    else:
        height = 0.6
    fig.subplots_adjust(hspace=height, wspace=0.3)

    # Interpolation type 
    if smooth:
        interpolation = 'spline16'
    else:
        interpolation = 'nearest'

    for i, ax in enumerate(axes.flat):
        # There may be less than 9 images, ensure it doesn't crash.
        if i < len(images):
            # Plot image.
            ax.imshow(images[i],
                      interpolation=interpolation)

            # Name of the true class.
            cls_true_name = class_names[cls_true[i]]

            # Show true and predicted classes.
            if cls_pred is None:
                xlabel = "True: {0}".format(cls_true_name)
            else:
                # Name of the predicted class.
                cls_pred_name = class_names[cls_pred[i]]

                xlabel = "True: {0}\nPred: {1}".format(cls_true_name, cls_pred_name)

            # Show the classes as the label on the x-axis.
            ax.set_xlabel(xlabel)

        # Remove ticks from the plot.
        ax.set_xticks([])
        ax.set_yticks([])

    # Ensure the plot is shown correctly with multiple plots
    # in a single Notebook cell.
    plt.show()


# Import a function from sklearn to calculate the confusion-matrix.
from sklearn.metrics import confusion_matrix

def print_confusion_matrix(cls_pred):
    # cls_pred is an array of the predicted class-number for
    # all images in the test-set.

    # Get the confusion matrix using sklearn.
    cm = confusion_matrix(y_true=cls_test,  # True class for test-set.
                          y_pred=cls_pred)  # Predicted class.

    print("Confusion matrix:")

    # Print the confusion matrix as text.
    print(cm)

    # Print the class-names for easy reference.
    for i, class_name in enumerate(class_names):
        print("({0}) {1}".format(i, class_name))



def load_images(image_paths):
    # Load the images from disk.
    images = [plt.imread(path) for path in image_paths]

    # Convert to a numpy array and return it.
    return np.asarray(images)


def plot_training_history(history):

    # Get it for the validation-set (we only use the test-set).
    val_acc = history.history['val_categorical_accuracy']
    val_loss = history.history['val_loss']

    plt.plot(val_loss, linestyle='--', color='b', label='Test Loss')

    # Plot title and legend.
    plt.title('Training and Test Accuracy')
    plt.legend()

    # Ensure the plot shows correctly.
    plt.show()


# run vs walk data Set 
train_dir = '../input/walk-or-run/walk_or_run_train/train'
test_dir = '../input/walk-or-run/walk_or_run_test/test'


input_shape = (299,299)
input_shape

datagen_train = ImageDataGenerator(
      rescale=1./255,
      rotation_range=180,
      width_shift_range=0.1,
      height_shift_range=0.1,
      shear_range=0.1,
      zoom_range=[0.9, 1.5],
      horizontal_flip=True,
      vertical_flip=True,
      fill_mode='nearest')

if True:
    save_to_dir = None
else:
    save_to_dir='augmented_images'


batch_size = 20

generator_train = datagen_train.flow_from_directory(directory=train_dir,
                                                    target_size=input_shape,
                                                    batch_size=batch_size,
                                                    shuffle=True,
                                                    save_to_dir=save_to_dir)


datagen_test = ImageDataGenerator(rescale=1./255)

generator_test = datagen_test.flow_from_directory(directory=test_dir,
                                                  target_size=input_shape,
                                                  batch_size=batch_size,
                                                  shuffle=False)


steps_test = generator_test.n // batch_size
print(steps_test)

steps_train = generator_train.n // batch_size
print(steps_train)

image_paths_train = path_join(train_dir, generator_train.filenames)
cls_train = generator_train.classes

image_paths_test = path_join(test_dir, generator_test.filenames)
cls_test = generator_test.classes


class_names = list(generator_train.class_indices.keys())
class_names


num_classes = generator_train.num_classes
num_classes


# Load the first images from the train-set.
images = load_images(image_paths=image_paths_train[24:35])

# Get the true classes for those images.
cls_true = cls_train[24:35]

# Plot the images and labels using our helper-function above.
plot_images(images=images, cls_true=cls_true, smooth=True)


from sklearn.utils.class_weight import compute_class_weight


class_weight = compute_class_weight(class_weight='balanced',
                                    classes=np.unique(cls_train),
                                    y=cls_train)

from PIL import Image
def predict(image_path):

    image = Image.open(image_path).resize((299,299))
    image = np.array(image)
    image = image.reshape(-1,299,299,3)
    image = 2 * (image/255.0)-1.0

    # Use the inception model to make a prediction.
    # This outputs an array with 1000 numbers corresponding to
    # the classes of the ImageNet-dataset.
    pred = model.predict(image)
    print(pred[0][0])
    print(pred[0][1])

    if pred[0][0] >.5:
        print('cat', float(pred[0][0]*100))
    if pred[0][1] > .5:
        print('dog', float(pred[0][1]*100))

# predict(image_path='../input/cat_dog/cat_dog/train/cat/cat.40.jpg')

base_model = InceptionResNetV2(input_shape = (299,299,3), include_top= False, weights='imagenet')


#combine base and top model
# model = Model(input= base_model.input, output= base_model.output)

top_layers = base_model.output
top_layers = GlobalAveragePooling2D()(top_layers)
top_layers = Dense(1024, activation='relu')(top_layers)

predictions = Dense(2, activation='softmax')(top_layers)

model= Model(inputs =base_model.input, outputs = predictions)
model.save_weights('../input/run_walk_model')

# compile the model (should be done *after* setting layers to non-trainable)

optimizer = Adam(lr=1e-2)
loss = 'binary_crossentropy'
metrics = ['categorical_accuracy']

def print_layer_trainable():
    for layer in model.layers:
        print("{0}:\t{1}".format(layer.trainable, layer.name))

model.compile(optimizer=optimizer, loss=loss, metrics=metrics)

epochs = 20

history = model.fit_generator(generator=generator_train,
                                  epochs=epochs,
                                  steps_per_epoch=30,
                                  class_weight=class_weight,
                                  validation_data=generator_test,
                                  validation_steps=10)

plot_training_history(history)

0 个答案:

没有答案