【问题标题】:I want all the output of the pretrained VGG16 model as well as the new classes it is trained on我想要预训练的 VGG16 模型的所有输出以及它训练的新类
【发布时间】:2018-11-21 10:02:34
【问题描述】:

我尝试过使用 VGG16 进行迁移学习,但只获得了那些经过训练的类的结果。我希望输出包含 VGG16 类和我的新训练类。有可能吗?

我已经附上了我的整个代码。

import matplotlib.pyplot as plt
import os, PIL
import tensorflow as tf
import numpy as np

import keras 
from keras.models import Sequential, Model
from keras.layers.core import Dense, Dropout, Flatten, Reshape, Activation
from keras.layers import Embedding, Input, merge, ELU
from keras.layers.convolutional import Conv2D, MaxPooling2D
from keras.optimizers import SGD, Adam, RMSprop
from keras.regularizers import l2
from keras.utils.np_utils import to_categorical
import sklearn.metrics as metrics
from PIL import Image, ImageDraw
from keras.applications import VGG16
from keras.applications.vgg16 import preprocess_input, decode_predictions
from keras.preprocessing.image import ImageDataGenerator


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 path_join(dirname, filenames):
    return [os.path.join(dirname, filename) for filename in filenames]


train_dir = "/home/priyank/Jupyter_notebook/plant_leaves_train_set"
test_dir = "/home/priyank/Jupyter_notebook/val_data_plant"

# # Pre-Trained Model: VGG16
# Downloading the pretrained model of imagenet dataset.

model = VGG16(include_top=True, weights='imagenet')

# # Input Pipeline
# First we need to know the shape of the tensors expected as input by the pre-trained VGG16 model. In this case it is images of shape 224 x 224 x 3.

input_shape = model.layers[0].output_shape[1:3] # the input shape of the vgg16 model
input_shape


# # ImageDataGenerator 
# It will pick the image one-by-one and transform all the data each time the image is loaded in the training set.

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')

datagen_test = ImageDataGenerator(rescale=1./255)

# 
# The datagenerator will return the batches of the images. VGG16 model is too large so we can't create the batches too large otherwise we will run out of the RAM and GPU.


# Taking small batch size
batch_size = 20

# 
# We can save the randomly transformed images during training, so as to inspect whether they have been overly distorted, so we have to adjust the parameters for the data-generator above.

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

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)


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
steps_test

image_paths_train = path_join(train_dir, generator_train.filenames)
image_paths_test = path_join(test_dir, generator_test.filenames)

cls_train = generator_train.classes
cls_test = generator_test.classes

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

num_classes = generator_train.num_classes
num_classes

# The dataset we have is imbalanced so the gradients for 9.01192 will remain higher adn the gradients of 0.8080 will reamin lower so that model can learn from higher gradient more than the lower gradient.
# 

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)
class_weight

#  
#  Predicting the our data image with the already trained VGG16 model. Using  a helper function which can resize the image so it can be the input to VGG16 model



def predict(image_path):
    # Load and resize the image using PIL.
    img = PIL.Image.open(image_path)
    img_resized = img.resize(input_shape, PIL.Image.LANCZOS)

    # Plot the image.
    plt.imshow(img_resized)
    plt.show()

    # Convert the PIL image to a numpy-array with the proper shape.
    img_array = np.expand_dims(np.array(img_resized), axis=0)

    # Use the VGG16 model to make a prediction.
    # This outputs an array with 1000 numbers corresponding to
    # the classes of the ImageNet-dataset.
    print(img_array.shape)
    pred = model.predict(img_array)

    # Decode the output of the VGG16 model.
    print(pred)
    print(pred.shape)
    pred_decoded = decode_predictions(pred)[0]

    # Print the predictions.
    for code, name, score in pred_decoded:
        print("{0:>6.2%} : {1}".format(score, name))

predict(image_path='/home/priyank/Pictures/people.jpg')
predict(image_path=image_paths_train[0])

# The pre-trained VGG16 model was unable to classify images from the plant disease dataset. The reason is perhaps that the VGG16 model was trained on the so-called ImageNet dataset which may not have contained many images of plant diseases.
# 
# The lower layers of a Convolutional Neural Network can recognize many different shapes or features in an image. It is the last few fully-connected layers that combine these featuers into classification of a whole image. So we can try and re-route the output of the last convolutional layer of the VGG16 model to a new fully-connected neural network that we create for doing classification 


# summary of VGG16 model.
model.summary()

# We can see that the last convolutional layer is called 'block5_pool' so we use Keras to get a reference to that layer.

transfer_layer = model.get_layer('block5_pool')


# 
# 
# We refer to this layer as the Transfer Layer because its output will be re-routed to our new fully-connected neural network which will do the classification for the Knifey-Spoony dataset.
# 
# The output of the transfer layer has the following shape:
# 

transfer_layer.output

# we take the part of the VGG16 model from its input-layer to the output of the transfer-layer. We may call this the convolutional model, because it consists of all the convolutional layers from the VGG16 model.


conv_model = Model(inputs=model.input,
                   outputs=transfer_layer.output)

# Start a new Keras Sequential model.
new_model = Sequential()

# Add the convolutional part of the VGG16 model from above.
new_model.add(conv_model)

# Flatten the output of the VGG16 model because it is from a
# convolutional layer.
new_model.add(Flatten())

# Add a dense (aka. fully-connected) layer.
# This is for combining features that the VGG16 model has
# recognized in the image.
new_model.add(Dense(1024, activation='relu'))

# Add a dropout-layer which may prevent overfitting and
# improve generalization ability to unseen data e.g. the test-set.
new_model.add(Dropout(0.5))

# Add the final layer for the actual classification.
new_model.add(Dense(num_classes, activation='softmax'))

optimizer = Adam(lr=1e-5)
loss = 'categorical_crossentropy'
metrics = ['categorical_accuracy']

# Helper-function for printing whether a layer in the VGG16 model should be trained.

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

print_layer_trainable()

# 
# 
# In Transfer Learning we are initially only interested in reusing the pre-trained VGG16 model as it is, so we will disable training for all its layers.
# 

conv_model.trainable = False

for layer in conv_model.layers:
    layer.trainable = False

print_layer_trainable()
new_model.compile(optimizer=optimizer, loss=loss, metrics=metrics)
epochs = 15
steps_per_epoch = 100
# Steps per epochs are multiplied with the epoch here 100*20 = 2000  means 2000 random images will be selected.

history = new_model.fit_generator(generator=generator_train,
                                  epochs=epochs,
                                  steps_per_epoch=steps_per_epoch,
                                  class_weight=class_weight,
                                  validation_data=generator_test,
                                  validation_steps=steps_test)

new_model.save("trained_new.h5")
predict(image_path = "/home/priyank/Jupyter_notebook/pp.jpg")

**IT 只是在预测我想要的 38 个类,如果新图像不属于这 38 个类,那么模型应该返回 VGG16 类或找不到匹配项。请帮忙 ** 提前致谢。

【问题讨论】:

    标签: keras deep-learning prediction keras-layer transfer-learning


    【解决方案1】:

    使用函数式 api 而不是 Sequential, 官方指南在这里:https://keras.io/getting-started/functional-api-guide/

    您可以在此处找到多输入和多输出模型示例。你想要的非常相似,但只使用一个输入而不是多个。

    【讨论】:

      猜你喜欢
      • 2020-01-15
      • 2020-10-10
      • 2019-08-15
      • 1970-01-01
      • 2020-06-13
      • 2017-08-19
      • 2018-03-05
      • 2017-03-28
      • 2021-02-19
      相关资源
      最近更新 更多