Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,10 +1,132 @@
##### TransferLearning
Demonstrates use of the dl4j transfer learning API which allows users to construct a model based off an existing model by modifying the architecture, freezing certain parts selectively and then fine tuning parameters. Read the documentation for the Transfer Learning API at [https://deeplearning4j.konduit.ai/tuning-and-training/transfer-learning](https://deeplearning4j.konduit.ai/tuning-and-training/transfer-learning).
* [EditLastLayerOthersFrozen.java](./editlastlayer/EditLastLayerOthersFrozen.java)
Modifies just the last layer in vgg16, freezes the rest and trains the network on the flower dataset.
* [FeaturizedPreSave.java](./editlastlayer/presave/FeaturizedPreSave.java) & [FitFromFeaturized.java](./editlastlayer/presave/FitFromFeaturized.java)
Save time on the forward pass during multiple epochs by "featurizing" the datasets. FeaturizedPreSave saves the output at the last frozen layer and FitFromFeaturize fits to the presaved data so you can iterate quicker with different learning parameters.
* [EditAtBottleneckOthersFrozen.java](./editfrombottleneck/EditAtBottleneckOthersFrozen.java)
A more complex example of modifying model architecure by adding/removing vertices
* [FineTuneFromBlockFour.java](./finetuneonly/FineTuneFromBlockFour.java)
Reads in a saved model (training information and all) and fine tunes it by overriding its training information with what is specified
# Transfer Learning Examples – DeepLearning4J

This folder demonstrates multiple ways to use the DL4J Transfer Learning API.
Transfer learning allows you to take an existing pretrained model (such as VGG16) and adapt it to a new dataset by:

- Freezing certain layers
- Modifying the architecture
- Replacing the final classifier
- Fine-tuning deeper layers
- Speeding up training through featurized datasets

Official documentation:
🔗 https://deeplearning4j.konduit.ai/tuning-and-training/transfer-learning

---

# 📁 Folder Overview

The transfer learning examples are grouped into three major strategies, depending on how much of the pretrained model you want to modify or retrain.

---

## 1️⃣ **Edit Last Layer Only** (`editlastlayer/`)

### ➤ `EditLastLayerOthersFrozen.java`
Replaces only the final output layer of VGG16 and freezes all previous layers.

**Use Case:**
- New dataset is small
- New dataset is similar (e.g., flower classification)
- Extremely fast training
- Very little overfitting risk

---

### ⚡ Presaving Featurized Data (`editlastlayer/presave/`)
This subfolder contains examples for speeding up transfer learning.

#### ➤ `FeaturizedPreSave.java`
Runs the dataset once through the frozen layers and **saves the output features** to disk.

#### ➤ `FitFromFeaturized.java`
Uses the presaved features to train the classifier **without repeating the forward pass**.

**Why this helps:**
Huge speed-up when training for many epochs or trying multiple learning rates.

---

## 2️⃣ **Edit from Bottleneck** (`editfrombottleneck/`)

### ➤ `EditAtBottleneckOthersFrozen.java`
Modifies the model at the **bottleneck layer** (middle of the network).
Allows adding/removing vertices, changing layer shapes, or inserting new layers.

**Use Case:**
- New dataset is somewhat different from ImageNet
- Need more flexibility in how features are combined
- Want deeper customization but still freeze earlier layers

This is a more advanced example showing how to modify the computation graph.

---

## 3️⃣ **Fine-Tune Some Layers Only** (`finetuneonly/`)

### ➤ `FineTuneFromBlockFour.java`
Loads a previously saved model and fine-tunes only the last few blocks while keeping earlier layers frozen.

**Use Case:**
- Dataset is moderately different
- Need the network to adapt more deeply
- Fine-tuning block 4+ is common with VGG/ResNet architectures
- Allows training more parameters without overfitting as much as full fine-tuning

---

## 4️⃣ **Dataset Iterators** (`iterators/`)

These classes prepare the flower dataset for transfer learning.

### ➤ `FlowerDataSetIterator.java`
Loads images from disk and prepares them for VGG16-style input.

### ➤ `FlowerDataSetIteratorFeaturized.java`
Loads presaved (featurized) data for fast training.

---

# 🚀 How to Run Any Transfer Learning Example

Use:

mvn -q exec:java -Dexec.mainClass="org.deeplearning4j.examples.advanced.features.transferlearning.<folder>.<ClassName>"


Examples:



mvn -q exec:java -Dexec.mainClass="org.deeplearning4j.examples.advanced.features.transferlearning.editlastlayer.EditLastLayerOthersFrozen"

mvn -q exec:java -Dexec.mainClass="org.deeplearning4j.examples.advanced.features.transferlearning.editfrombottleneck.EditAtBottleneckOthersFrozen"

mvn -q exec:java -Dexec.mainClass="org.deeplearning4j.examples.advanced.features.transferlearning.finetuneonly.FineTuneFromBlockFour"


---

# 🎯 Summary of Strategies

| Strategy | Layers Trained | Best For |
|---------|----------------|----------|
| **Edit Last Layer Only** | Only final Dense layer | Small, similar datasets; fastest training |
| **Edit from Bottleneck** | Middle layers | More control; moderate dataset differences |
| **Fine-Tune Block Four** | Last few blocks | Moderate differences; more expressive training |
| **Full Featurization** | Only classifier | Fast experiments; repeated runs |

---

# 🙌 Why This README Matters

The original README was brief and lacked conceptual explanations.
This improved version:

- Documents each example clearly
- Explains when to use each transfer learning strategy
- Provides run commands
- Helps new users understand DL4J transfer learning best practices
- Improves readability and usefulness of advanced examples

This makes transfer learning in DL4J easier to understand and use.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
* SPDX-License-Identifier: Apache-2.0
******************************************************************************/

// Transfer learning example: modify architecture at the bottleneck layer.
// Earlier layers remain frozen; mid-level layers can be added/edited.

package org.deeplearning4j.examples.advanced.features.transferlearning.editfrombottleneck;

import org.deeplearning4j.examples.advanced.features.transferlearning.iterators.FlowerDataSetIterator;
Expand Down Expand Up @@ -140,6 +143,8 @@ public static void main(String [] args) throws Exception {
// 2. in place with the TransferLearningHelper constructor which will take a model, and a specific vertexname
// and freeze it and the vertices on the path from an input to it (as seen in the FeaturizePreSave class)
//The saved model can be "fine-tuned" further as in the class "FitFromFeaturized"
// Insert/remove vertices to customize part of the model

File locationToSave = new File("MyComputationGraph.zip");
boolean saveUpdater = false;
vgg16Transfer.save(locationToSave, saveUpdater);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
* SPDX-License-Identifier: Apache-2.0
******************************************************************************/

// Transfer learning example: replace only the final classification layer of VGG16.
// All earlier layers are frozen for extremely fast training on a new dataset.

package org.deeplearning4j.examples.advanced.features.transferlearning.editlastlayer;

import org.deeplearning4j.examples.advanced.features.transferlearning.iterators.FlowerDataSetIterator;
Expand Down Expand Up @@ -73,6 +76,8 @@ public static void main(String [] args) throws IOException {
//Decide on a fine tune configuration to use.
//In cases where there already exists a setting the fine tune setting will
// override the setting for all layers that are not "frozen".
// Remove the original output layer and attach a new one for 5 flower classes

FineTuneConfiguration fineTuneConf = new FineTuneConfiguration.Builder()
.updater(new Nesterovs(5e-5))
.seed(seed)
Expand All @@ -93,6 +98,8 @@ public static void main(String [] args) throws IOException {
log.info(vgg16Transfer.summary());

//Dataset iterators
// Train only the new output layer; frozen layers are not updated

FlowerDataSetIterator.setup(batchSize,trainPerc);
DataSetIterator trainIter = FlowerDataSetIterator.trainIterator();
DataSetIterator testIter = FlowerDataSetIterator.testIterator();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
* SPDX-License-Identifier: Apache-2.0
******************************************************************************/

// Fine-tune last few blocks of a pretrained model while freezing early layers.
// Provides more adaptation than last-layer editing but avoids full retraining.

package org.deeplearning4j.examples.advanced.features.transferlearning.finetuneonly;

import org.deeplearning4j.examples.advanced.features.transferlearning.iterators.FlowerDataSetIterator;
Expand Down