python转移如何使用_迁移学习的Python实例

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

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

简而言之,迁移学习只不过是从另外知识中学习。

迁移学习的Python实例-1.jpg (39.05 KB, 下载次数: 0)

2018-7-11 05:51 上传

迁移学习和机器学习

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

为了更好地理解,我在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")

更多内容回复查看:

游客,如果您要查看本帖隐藏内容请回复

Python迁移学习算法可以帮助我们利用在一个任务上学到的知识和模型,来加速另一个任务的学习过程。 迁移学习是指在一个任务上训练好的模型在另一个相关或不相关的任务上进行学习和应用。Python提供了一些强大的迁移学习算法和工具包,如scikit-learn和TensorFlow等。 在迁移学习中,我们首先需要选择一个在源任务上训练好的模型作为基础模型。然后我们可以使用几种不同的方法来进行迁移学习。 一种常见的方法是使用预训练模型。这些是在大规模的数据集上提前训练好的模型,例如ImageNet图像数据集上的预训练卷积神经网络模型。我们可以利用这些预训练模型,将它们作为特征提取器,从原始数据中提取有用的特征。 另一种常用的方法是对基础模型进行微调。微调是指在源任务的基础上,继续在目标任务上进行训练调整。通过微调,我们可以逐渐适应目标任务的特征和标签,从而提高模型的性能。 上述的方法都可以在Python使用。例如,scikit-learn提供了多种迁移学习算法的实现,包括基于特征提取和基于实例的方法。TensorFlow则提供了预训练模型和微调的框架,使我们能够方便地进行迁移学习。 总结来说,Python迁移学习算法是一种强大而灵活的方法,可以帮助我们在一个任务上学习到的知识和模型,用于加速另一个任务的学习过程。使用Python中的迁移学习算法和工具包,我们能够快速有效地进行迁移学习,并且在各种不同的任务中取得良好的性能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值