Skip to content

Instantly share code, notes, and snippets.

@ivarreukers
Last active April 6, 2018 07:55
Show Gist options
  • Select an option

  • Save ivarreukers/ce636cd4e93d504bc3696199afd73930 to your computer and use it in GitHub Desktop.

Select an option

Save ivarreukers/ce636cd4e93d504bc3696199afd73930 to your computer and use it in GitHub Desktop.
Network
protected static final long seed = 12345;
protected static final int numClasses = 41;
protected static final int nEpochs = 20;
private static TransferLearningHelper transferLearningHelper;
private static ComputationGraph vgg16transfer, vgg16;
public static void main(String args[]) {
ZooModel zooModel = new VGG16();
vgg16 = (ComputationGraph) zooModel.initPretrained(PretrainedType.VGGFACE);
log.info(vgg16.summary());
FineTuneConfiguration fineTuneConf = new FineTuneConfiguration.Builder()
.learningRate(3e-5)
.optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT)
.updater(Updater.NESTEROVS)
.seed(seed)
.build();
//Construct a new model with the intended architecture and print summary
vgg16transfer = new TransferLearning.GraphBuilder(vgg16)
.fineTuneConfiguration(fineTuneConf)
.setFeatureExtractor("fc8") //the specified layer and below are "frozen"
.addLayer("predictions",
new OutputLayer.Builder(LossFunctions.LossFunction.XENT)
.nIn(2622).nOut(numClasses)
.weightInit(WeightInit.DISTRIBUTION)
.dist(new NormalDistribution(0,0.2*(2.0/(2622+numClasses))))
.activation(Activation.SIGMOID).build(),
"fc8")
.build();
log.info(vgg16transfer.summary());
vgg16transfer.setListeners(new StatsListener(statsStorage));
transferLearningHelper = new TransferLearningHelper(vgg16transfer);
}
@ivarreukers
Copy link
Copy Markdown
Author

Log: (First summary is of plain pretrained VGGFace net, second summary after OutputLayer is added)

09:41:57.121 [main] INFO org.deeplearning4j.zoo.ZooModel - Verifying download...
09:41:57.356 [main] INFO org.deeplearning4j.zoo.ZooModel - Checksum local is 2706403553, expecting 2706403553
09:42:03.101 [main] INFO test2.FeaturizedPreSave - 
============================================================================================================================================
VertexName (VertexType)                 nIn,nOut       TotalParams    ParamsShape                    Vertex Inputs
============================================================================================================================================
input_1 (InputVertex)                   -,-            -              -                              -
conv1_1 (ConvolutionLayer)              3,64           1792           b:{1,64}, W:{64,3,3,3}         [input_1]
conv1_2 (ConvolutionLayer)              64,64          36928          b:{1,64}, W:{64,64,3,3}        [conv1_1]
pool1 (SubsamplingLayer)                -,-            0              -                              [conv1_2]
conv2_1 (ConvolutionLayer)              64,128         73856          b:{1,128}, W:{128,64,3,3}      [pool1]
conv2_2 (ConvolutionLayer)              128,128        147584         b:{1,128}, W:{128,128,3,3}     [conv2_1]
pool2 (SubsamplingLayer)                -,-            0              -                              [conv2_2]
conv3_1 (ConvolutionLayer)              128,256        295168         b:{1,256}, W:{256,128,3,3}     [pool2]
conv3_2 (ConvolutionLayer)              256,256        590080         b:{1,256}, W:{256,256,3,3}     [conv3_1]
conv3_3 (ConvolutionLayer)              256,256        590080         b:{1,256}, W:{256,256,3,3}     [conv3_2]
pool3 (SubsamplingLayer)                -,-            0              -                              [conv3_3]
conv4_1 (ConvolutionLayer)              256,512        1180160        b:{1,512}, W:{512,256,3,3}     [pool3]
conv4_2 (ConvolutionLayer)              512,512        2359808        b:{1,512}, W:{512,512,3,3}     [conv4_1]
conv4_3 (ConvolutionLayer)              512,512        2359808        b:{1,512}, W:{512,512,3,3}     [conv4_2]
pool4 (SubsamplingLayer)                -,-            0              -                              [conv4_3]
conv5_1 (ConvolutionLayer)              512,512        2359808        b:{1,512}, W:{512,512,3,3}     [pool4]
conv5_2 (ConvolutionLayer)              512,512        2359808        b:{1,512}, W:{512,512,3,3}     [conv5_1]
conv5_3 (ConvolutionLayer)              512,512        2359808        b:{1,512}, W:{512,512,3,3}     [conv5_2]
pool5 (SubsamplingLayer)                -,-            0              -                              [conv5_3]
flatten (PreprocessorVertex)            -,-            -              -                              [pool5]
fc6 (DenseLayer)                        25088,4096     102764544      b:{1,4096}, W:{25088,4096}     [flatten]
fc7 (DenseLayer)                        4096,4096      16781312       b:{1,4096}, W:{4096,4096}      [fc6]
fc8 (DenseLayer)                        4096,2622      10742334       b:{1,2622}, W:{4096,2622}      [fc7]
--------------------------------------------------------------------------------------------------------------------------------------------
            Total Parameters:  145002878
        Trainable Parameters:  145002878
           Frozen Parameters:  0
============================================================================================================================================

09:42:09.560 [main] INFO test2.FeaturizedPreSave - 
============================================================================================================================================
VertexName (VertexType)                 nIn,nOut       TotalParams    ParamsShape                    Vertex Inputs
============================================================================================================================================
input_1 (InputVertex)                   -,-            -              -                              -
conv1_1 (Frozen ConvolutionLayer)       3,64           1792           b:{1,64}, W:{64,3,3,3}         [input_1]
conv1_2 (Frozen ConvolutionLayer)       64,64          36928          b:{1,64}, W:{64,64,3,3}        [conv1_1]
pool1 (Frozen SubsamplingLayer)         -,-            0              -                              [conv1_2]
conv2_1 (Frozen ConvolutionLayer)       64,128         73856          b:{1,128}, W:{128,64,3,3}      [pool1]
conv2_2 (Frozen ConvolutionLayer)       128,128        147584         b:{1,128}, W:{128,128,3,3}     [conv2_1]
pool2 (Frozen SubsamplingLayer)         -,-            0              -                              [conv2_2]
conv3_1 (Frozen ConvolutionLayer)       128,256        295168         b:{1,256}, W:{256,128,3,3}     [pool2]
conv3_2 (Frozen ConvolutionLayer)       256,256        590080         b:{1,256}, W:{256,256,3,3}     [conv3_1]
conv3_3 (Frozen ConvolutionLayer)       256,256        590080         b:{1,256}, W:{256,256,3,3}     [conv3_2]
pool3 (Frozen SubsamplingLayer)         -,-            0              -                              [conv3_3]
conv4_1 (Frozen ConvolutionLayer)       256,512        1180160        b:{1,512}, W:{512,256,3,3}     [pool3]
conv4_2 (Frozen ConvolutionLayer)       512,512        2359808        b:{1,512}, W:{512,512,3,3}     [conv4_1]
conv4_3 (Frozen ConvolutionLayer)       512,512        2359808        b:{1,512}, W:{512,512,3,3}     [conv4_2]
pool4 (Frozen SubsamplingLayer)         -,-            0              -                              [conv4_3]
conv5_1 (Frozen ConvolutionLayer)       512,512        2359808        b:{1,512}, W:{512,512,3,3}     [pool4]
conv5_2 (Frozen ConvolutionLayer)       512,512        2359808        b:{1,512}, W:{512,512,3,3}     [conv5_1]
conv5_3 (Frozen ConvolutionLayer)       512,512        2359808        b:{1,512}, W:{512,512,3,3}     [conv5_2]
pool5 (Frozen SubsamplingLayer)         -,-            0              -                              [conv5_3]
flatten (PreprocessorVertex)            -,-            -              -                              [pool5]
fc6 (Frozen DenseLayer)                 25088,4096     102764544      b:{1,4096}, W:{25088,4096}     [flatten]
fc7 (Frozen DenseLayer)                 4096,4096      16781312       b:{1,4096}, W:{4096,4096}      [fc6]
fc8 (Frozen DenseLayer)                 4096,2622      10742334       b:{1,2622}, W:{4096,2622}      [fc7]
predictions (OutputLayer)               2622,40        104920         b:{1,40}, W:{2622,40}          [fc8]
--------------------------------------------------------------------------------------------------------------------------------------------
            Total Parameters:  145107798
        Trainable Parameters:  104920
           Frozen Parameters:  145002878
============================================================================================================================================

Exception in thread "main" java.lang.IllegalStateException: Invalid configuration: Output name "fc8" is not a valid vertex
	at org.deeplearning4j.nn.conf.ComputationGraphConfiguration.validate(ComputationGraphConfiguration.java:296)
	at org.deeplearning4j.nn.conf.ComputationGraphConfiguration$GraphBuilder.build(ComputationGraphConfiguration.java:822)
	at org.deeplearning4j.nn.transferlearning.TransferLearning$GraphBuilder.build(TransferLearning.java:755)
	at org.deeplearning4j.nn.transferlearning.TransferLearningHelper.initHelperGraph(TransferLearningHelper.java:252)
	at org.deeplearning4j.nn.transferlearning.TransferLearningHelper.<init>(TransferLearningHelper.java:66)
	at test2.FeaturizedPreSave.main(FeaturizedPreSave.java:72)
	at test2.FlowerDataSetIteratorFeaturized.runFeaturize(FlowerDataSetIteratorFeaturized.java:42)
	at test2.FlowerDataSetIteratorFeaturized.trainIterator(FlowerDataSetIteratorFeaturized.java:27)
	at test2.FitFromFeaturized.buildModel(FitFromFeaturized.java:139)
	at test2.FitFromFeaturized.setup(FitFromFeaturized.java:133)
	at test2.FitFromFeaturized.main(FitFromFeaturized.java:65)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment