python与h5结合实例_迁移学习的Python实例

迁移学习的Python实例

作者:PHPYuan 时间:2018-07-10 23:39:15

在这篇文章中,我将帮助您了解我们如何使用最重要的深度学习设计方法(TRANSFER LEARNING)来实现图像或对象识别的真实实现。在我们深入学习转学习之前,让我们回顾一些我们需要理解的概念。

当我们没有为我们关心的特定任务或领域培训可靠模型时,传统的机器学习模型给出了很多痛苦。例如,我需要使用在IAM数据集上训练的一些模型来训练手写文本识别。迁移学习允许我们通过利用某些相关任务或域的现有标记数据来处理这些场景。我们尝试将这些知识存储在解决源域中的源任务中,并将其应用于我们感兴趣的问题。简而言之,迁移学习只不过是从别的知识中学习。

迁移学习和机器学习

本图清楚地反映了转移学习的优点,因为您可以看到,您可以使用最少标记的数据集来训练您自己的定制模型,而您可以看到,在机器学习中缺乏这种特性。

为了更好地理解,我在Fashion MNIST上实现了简单卷积神经网络的工作。在这篇文章中,我们将学习各种迁移学习方法,并检查每一种方法的准确性。让我们快速了解一下CNN的过程。使用python库加载数据。

数据的预处理,包括整形,一热编码和分割。

构建CNN的模型层,然后进行模型编译,模型训练。

评估测试数据的模型。

最后,预测正确和不正确的标签。

让我们从使用标准CNN模型的传统机器学习方法开始。

首先加载和预处理数据。"""

Created on Wed May 23 23:50:53 2018

@author: Anchit Jain

"""

from comet_ml import Experiment

from keras.preprocessing.image import ImageDataGenerator

from keras.models import Sequential

from keras.layers import Conv2D, MaxPooling2D

from keras.layers import Activation, Dropout, Flatten, Dense

from keras import backend as K

experiment = Experiment(api_key="4A4Q2iIKeHwXdfEDxbEhmkgoN",project_name="cnn")

# dimensions of our images.

img_width, img_height = 150, 150

train_data_dir = 'data/train'

validation_data_dir = 'data/validation'

nb_train_samples = 200

nb_validation_samples = 50

epochs = 5

batch_size = 16

if K.image_data_format() == 'channels_first':

input_shape = (3, img_width, img_height)

else:

input_shape = (img_width, img_height, 3)

确保你已经完全理解了每一层# layer1

model = Sequential()

model.add(Conv2D(32, (3, 3), input_shape=input_shape))

model.add(Activation('relu'))

model.add(MaxPooling2D(pool_size=(2, 2)))

#layer2

model.add(Conv2D(64, (3, 3)))

model.add(Activation('relu'))

model.add(MaxPooling2D(pool_size=(3, 3)))

#layer3

model.add(Flatten())

model.add(Dense(64))

model.add(Activation('relu'))

model.add(Dropout(0.5))

#layer4

model.add(Dense(1))

model.add(Activation('sigmoid'))

# Model compiling

model.compile(loss='binary_crossentropy', # or categorical_crossentropy

optimizer='rmsprop',# or adagrad

metrics=['accuracy'])

# this is the augmentation configuration we will use for training

train_datagen = ImageDataGenerator(

rescale=1. / 255,

shear_range=0.2,

zoom_range=0.2,

horizontal_flip=True)

# this is the augmentation configuration we will use for testing:

# only rescaling

test_datagen = ImageDataGenerator(rescale=1. / 255)

train_generator = train_datagen.flow_from_directory(

train_data_dir,

target_size=(img_width, img_height),

batch_size=batch_size,

class_mode='binary')

print(train_generator.class_indices)

validation_generator = test_datagen.flow_from_directory(

validation_data_dir,

target_size=(img_width, img_height),

batch_size=batch_size,

class_mode='binary')

model.fit_generator(

train_generator,

steps_per_epoch=nb_train_samples // batch_size,

epochs=epochs,

validation_data=validation_generator,

validation_steps=nb_validation_samples // batch_size)

model.save('scratch_model.h5')

print("Model saved")

继续前进,我们需要检查一下精度# Model evaluation on test set

test_eval = fashion_model.evaluate(test_X, test_Y_one_hot, verbose=0)

print('Test loss:', test_eval[0])

print('Test accuracy:', test_eval[1])

# Model evaluation

accuracy = fashion_train_dropout.history['acc']

val_accuracy = fashion_train_dropout.history['val_acc']

loss = fashion_train_dropout.history['loss']

val_loss = fashion_train_dropout.history['val_loss']

epochs = range(len(accuracy))

plt.plot(epochs, accuracy, 'bo', label='Training accuracy')

plt.plot(epochs, val_accuracy, 'b', label='Validation accuracy')

plt.title('Training and validation accuracy')

plt.legend()

plt.figure()

plt.plot(epochs, loss, 'bo', label='Training loss')

plt.plot(epochs, val_loss, 'b', label='Validation loss')

plt.title('Training and validation loss')

plt.legend()

plt.show()

## Predict labels

predicted_classes = fashion_model.predict(test_X)

predicted_classes = np.argmax(np.round(predicted_classes),axis=1)

predicted_classes.shape, test_Y.shape

correct = np.where(predicted_classes==test_Y)[0]

print ("Found %d correct labels" % len(correct))

for i, correct in enumerate(correct[:9]):

plt.subplot(3,3,i+1)

plt.imshow(test_X[correct].reshape(28,28), cmap='gray', interpolation='none')

plt.title("Predicted {}, Class {}".format(predicted_classes[correct], test_Y[correct]))

plt.tight_layout()

incorrect = np.where(predicted_classes!=test_Y)[0]

print ("Found %d incorrect labels" % len(incorrect))

for i, incorrect in enumerate(incorrect[:9]):

plt.subplot(3,3,i+1)

plt.imshow(test_X[incorrect].reshape(28,28), cmap='gray', interpolation='none')

plt.title("Predicted {}, Class {}".format(predicted_classes[incorrect], test_Y[incorrect]))

plt.tight_layout()

一旦我们完成了预测,9193标签是正确的,807是错误的,准确率达到64%但这不是我们可以停止的地方。即使是对隐藏层的实验,隐藏层中神经元的数量和drop out率。我们没办法大幅度提高我的训练精度。那么,我们如何才能达到高达85%甚至更高的准确度呢?是的,这是可能的,这时迁移学习发挥作用。将迁移学习的概念嵌入,我们将看到的是如何冻结现有模型的层将会触发模型的性能。

接下来,我们将使用预训练过的图像识别模型INCEPTION V3,它将帮助我们从现有的CNN中提取特征和对全连接的softmax层进行分类。

整个过程将涉及以下场景。从头开始训练神经网络(见上文)

使用inception v3模型的bottleneck features。

对新训练的模型进行微调。

更多地研究模型层,我们将看到使用inception v3模型的CNN层的变化将如何帮助我们在准确性方面取得良好的结果。一旦您成功地从头开始构建模型,那么就不会给您带来太多麻烦。让我们看看吧。from comet_ml import Experiment

from keras.applications.inception_v3 import InceptionV3

from keras.preprocessing.image import ImageDataGenerator

from keras.models import Sequential

from keras.models import Model

from keras.layers import Conv2D, MaxPooling2D,GlobalAveragePooling2D

from keras.layers import Activation, Dropout, Flatten, Dense

from keras import backend as K

# to track the accuracy of the model on comet.ml

experiment = Experiment(api_key="4A4Q2iIKeHwXdfEDxbEhmkgoN",project_name="cnn")

# dimensions of our images.

img_width, img_height = 299, 299

train_data_dir = 'data/train'

validation_data_dir = 'data/validation'

nb_train_samples = 200

nb_validation_samples = 50

epochs = 5

batch_size = 16

if K.image_data_format() == 'channels_first':

input_shape = (3, img_width, img_height)

else:

input_shape = (img_width, img_height, 3)

# create the base pre-trained model

base_model = InceptionV3(weights='imagenet', include_top=False)

# add a global spatial average pooling layer

x = base_model.output

x = GlobalAveragePooling2D()(x)

# let's add a fully-connected layer

x = Dense(1024, activation='relu')(x)

# and a logistic layer -- we have 2 classes

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

# this is the model we will train

model = Model(input=base_model.input, output=predictions)

# i.e. freeze all convolutional InceptionV3 layers

for layer in base_model.layers:

layer.trainable = False

确保首先导入comet_ml。如果您可以在上面的行代码中注意到,整个代码的功能保持原样,直到第26行,然后使用经过预先训练的权值(imagenet)的inception v3模型,并在网络的顶部包含全连接层(include_top=False)。一旦定义了具有bottleneck 的inception v3层,我们就要编译模型,并使用训练和测试数据的数据增强过程,我们可以通过最终增加数据集的大小来帮助我们的模型获取更多的图像特征。数据增强对于提高任何模型的准确性都起着至关重要的作用。

用新的精确度训练我们的模型model.compile(loss='categorical_crossentropy', # or categorical_crossentropy

optimizer='rmsprop',# or adagrad

metrics=['accuracy'])

# this is the augmentation configuration we will use for training

train_datagen = ImageDataGenerator(

rescale=1. / 255,

shear_range=0.2,

zoom_range=0.2,

horizontal_flip=True)

# this is the augmentation configuration we will use for testing:

# only rescaling

test_datagen = ImageDataGenerator(rescale=1. / 255)

train_generator = train_datagen.flow_from_directory(

train_data_dir,

target_size=(img_width, img_height),

batch_size=batch_size,

class_mode='categorical')

print(train_generator.class_indices)

validation_generator = test_datagen.flow_from_directory(

validation_data_dir,

target_size=(img_width, img_height),

batch_size=batch_size,

class_mode='categorical')

model.fit_generator(

train_generator,

steps_per_epoch=nb_train_samples // batch_size,

epochs=epochs,

validation_data=validation_generator,

validation_steps=nb_validation_samples // batch_size)

model.save('inception_model.h5')

print("Model saved")

分享到:

<< 上一篇:机器学习结合音乐教育,目前已碰撞出哪些火花? (2018-07-11 13:30)

>> 下一篇:学习Python技术,高薪并没有那么难! (2018-07-10 23:39)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值