分类迁移学习的源代码
时间: 2024-10-11 14:06:01 浏览: 20
皮肤病-图像分类-迁移学习
5星 · 资源好评率100%
分类迁移学习是一种机器学习技术,它允许我们在训练一个特定任务的模型后,将其知识应用到另一个相关但不同任务上。这通常涉及到将预训练的模型(比如在大规模数据集如ImageNet上训练过的模型)的某些层固定,然后只微调部分网络以适应新任务。
在Python中,深度学习库如TensorFlow和PyTorch提供了丰富的支持。例如,在TensorFlow中,可以使用`tf.keras.applications`模块加载预训练的模型,然后通过`tf.keras.layers.experimental.preprocessing`对输入进行适配并添加自定义的分类头:
```python
from tensorflow import keras
from tensorflow.keras.applications import VGG16
base_model = VGG16(weights='imagenet', include_top=False, input_shape=(224, 224, 3))
# Freeze the base model's layers
base_model.trainable = False
# Add a new classification head
x = base_model.output
x = keras.layers.Flatten()(x)
predictions = keras.layers.Dense(num_classes, activation='softmax')(x)
model = keras.Model(inputs=base_model.input, outputs=predictions)
# Compile the model for fine-tuning
model.compile(optimizer=keras.optimizers.Adam(), loss='categorical_crossentropy', metrics=['accuracy'])
# Train only the top layers on your custom dataset
model.fit(train_dataset, epochs=num_epochs, validation_data=val_dataset)
```
在PyTorch中,可以使用`torchvision.models`类似的方式加载模型:
```python
import torch
import torchvision.models as models
pretrained_model = models.resnet18(pretrained=True)
for param in pretrained_model.parameters():
param.requires_grad = False
num_classes_your_task = ... # Number of classes for your task
num_features = pretrained_model.fc.in_features
pretrained_model.fc = nn.Linear(num_features, num_classes_your_task)
optimizer = optim.SGD(model.fc.parameters(), lr=0.001) # Fine-tune the last layer
# Train with your custom dataset
model.train()
...
```
阅读全文