FineTuneConfiguration fineTuneConf = new FineTuneConfiguration.Builder() .updater(new Nesterovs.Builder().learningRate(3e-5).build()) .seed(seed) .build(); ComputationGraph vgg16Transfer = new TransferLearning.GraphBuilder(vgg16) .fineTuneConfiguration(fineTuneConf) .setFeatureExtractor(featureExtractionLayer) //the specified layer and below are "frozen" .removeVertexKeepConnections("predictions") //replace the functionality of the final vertex .addLayer("predictions", new OutputLayer.Builder(LossFunctions.LossFunction.NEGATIVELOGLIKELIHOOD) .nIn(4096).nOut(numClasses) .activation(Activation.SOFTMAX).build(), "fc2") .build(); log.info(vgg16Transfer.summary()); TransferLearningHelper transferLearningHelper = new TransferLearningHelper(vgg16Transfer); log.info(transferLearningHelper.unfrozenGraph().summary()); ParallelWrapper wrapper = new ParallelWrapper.Builder(transferLearningHelper.unfrozenGraph())
public static void main(String [] args) throws UnsupportedKerasConfigurationException, IOException, InvalidKerasConfigurationException { //import org.deeplearning4j.transferlearning.vgg16 and print summary LOGGER.info("\n\nLoading org.deeplearning4j.transferlearning.vgg16...\n\n"); ZooModel zooModel = VGG16.builder().build(); ComputationGraph vgg16 = (ComputationGraph) zooModel.initPretrained(); LOGGER.info(vgg16.summary()); //use the TransferLearningHelper to freeze the specified vertices and below //NOTE: This is done in place! Pass in a cloned version of the model if you would prefer to not do this in place TransferLearningHelper transferLearningHelper = new TransferLearningHelper(vgg16, featurizeExtractionLayer); LOGGER.info(vgg16.summary()); FlowerDataSetIterator.setup(batchSize,trainPerc); DataSetIterator trainIter = FlowerDataSetIterator.trainIterator(); DataSetIterator testIter = FlowerDataSetIterator.testIterator(); int trainDataSaved = 0; while(trainIter.hasNext()) { DataSet currentFeaturized = transferLearningHelper.featurize(trainIter.next()); saveToDisk(currentFeaturized,trainDataSaved,true); trainDataSaved++; } int testDataSaved = 0; while(testIter.hasNext()) { DataSet currentFeaturized = transferLearningHelper.featurize(testIter.next()); saveToDisk(currentFeaturized,testDataSaved,false); testDataSaved++; } LOGGER.info("Finished pre saving featurized test and train data"); }
/** * Expects a computation graph where some vertices are frozen * * @param orig */ public TransferLearningHelper(ComputationGraph orig) { origGraph = orig; initHelperGraph(); }
/** * Modified nOut of specified layer. Also affects layers following layerName unless they are output layers * @param layerName The name of the layer to change nOut of * @param nOut Value of nOut to change to * @param dist Weight distribution scheme to use for layerName * @param distNext Weight distribution scheme for layers following layerName * @return GraphBuilder * @see org.deeplearning4j.nn.weights.WeightInit DISTRIBUTION */ public GraphBuilder nOutReplace(String layerName, int nOut, Distribution dist, Distribution distNext) { return nOutReplace(layerName, nOut, WeightInit.DISTRIBUTION, WeightInit.DISTRIBUTION, dist, distNext); }
/** * Modify the architecture of a layer by changing nOut * Note this will also affect the layer that follows the layer specified, unless it is the output layer * * @param layerNum The index of the layer to change nOut of * @param nOut Value of nOut to change to * @param scheme Weight Init scheme to use for params in layernum and layernum+1 * @return Builder */ public Builder nOutReplace(int layerNum, int nOut, WeightInit scheme) { return nOutReplace(layerNum, nOut, scheme, scheme, null, null); }
/** * Returns the unfrozen layers of the MultiLayerNetwork as a multilayernetwork * Note that with each call to featurizedFit the parameters to the original MLN are also updated */ public MultiLayerNetwork unfrozenMLN() { if (isGraph) errorIfGraphIfMLN(); return unFrozenSubsetMLN; }
/** * Expects a MLN where some layers are frozen * * @param orig */ public TransferLearningHelper(MultiLayerNetwork orig) { isGraph = false; origMLN = orig; initHelperMLN(); }
public Builder updater(IUpdater updater) { return iUpdater(updater); }
FineTuneConfiguration fineTuneConf = new FineTuneConfiguration.Builder() .updater(new Nesterovs(3e-5)) .seed(seed) .build(); ComputationGraph vgg16Transfer = new TransferLearning.GraphBuilder(vgg16) .fineTuneConfiguration(fineTuneConf) .setFeatureExtractor(featureExtractionLayer) //the specified layer and below are "frozen" .removeVertexKeepConnections("predictions") //replace the functionality of the final vertex .addLayer("predictions", new OutputLayer.Builder(LossFunctions.LossFunction.NEGATIVELOGLIKELIHOOD) .nIn(4096).nOut(numClasses) .activation(Activation.SOFTMAX).build(), "fc2") .build(); TransferLearningHelper transferLearningHelper = new TransferLearningHelper(vgg16Transfer); log.info(transferLearningHelper.unfrozenGraph().summary()); FileSystem fs = FileSystem.get(sc.hadoopConfiguration()); SparkComputationGraph sparkComputationGraph = new SparkComputationGraph(sc,transferLearningHelper.unfrozenGraph(),tm);
public static void main(String [] args) throws UnsupportedKerasConfigurationException, IOException, InvalidKerasConfigurationException { //import org.deeplearning4j.transferlearning.vgg16 and print summary LOGGER.info("\n\nLoading org.deeplearning4j.transferlearning.vgg16...\n\n"); ZooModel zooModel = VGG16.builder().build(); ComputationGraph vgg16 = (ComputationGraph) zooModel.initPretrained(); LOGGER.info(vgg16.summary()); //use the TransferLearningHelper to freeze the specified vertices and below //NOTE: This is done in place! Pass in a cloned version of the model if you would prefer to not do this in place TransferLearningHelper transferLearningHelper = new TransferLearningHelper(vgg16, featurizeExtractionLayer); LOGGER.info(vgg16.summary()); FlowerDataSetIterator.setup(batchSize,trainPerc); DataSetIterator trainIter = FlowerDataSetIterator.trainIterator(); DataSetIterator testIter = FlowerDataSetIterator.testIterator(); int trainDataSaved = 0; while(trainIter.hasNext()) { DataSet currentFeaturized = transferLearningHelper.featurize(trainIter.next()); saveToDisk(currentFeaturized,trainDataSaved,true); trainDataSaved++; } int testDataSaved = 0; while(testIter.hasNext()) { DataSet currentFeaturized = transferLearningHelper.featurize(testIter.next()); saveToDisk(currentFeaturized,testDataSaved,false); testDataSaved++; } LOGGER.info("Finished pre saving featurized test and train data"); }
public GraphBuilder nOutReplace(String layerName, int nOut, WeightInit scheme, Distribution dist) { return nOutReplace(layerName, nOut, scheme, WeightInit.DISTRIBUTION, null, dist); }
/** * Modify the architecture of a layer by changing nOut * Note this will also affect the layer that follows the layer specified, unless it is the output layer * * @param layerNum The index of the layer to change nOut of * @param nOut Value of nOut to change to * @param dist Distribution to use in conjunction with weight init DISTRIBUTION for params in layernum and layernum+1 * @return Builder * @see org.deeplearning4j.nn.weights.WeightInit DISTRIBUTION */ public Builder nOutReplace(int layerNum, int nOut, Distribution dist) { return nOutReplace(layerNum, nOut, WeightInit.DISTRIBUTION, WeightInit.DISTRIBUTION, dist, dist); }
/** * Will modify the given comp graph (in place!) to freeze vertices from input to the vertex specified. * * @param orig Comp graph * @param frozenOutputAt vertex to freeze at (hold params constant during training) */ public TransferLearningHelper(ComputationGraph orig, String... frozenOutputAt) { origGraph = orig; this.frozenOutputAt = frozenOutputAt; applyFrozen = true; initHelperGraph(); }
/** * Returns the unfrozen subset of the original computation graph as a computation graph * Note that with each call to featurizedFit the parameters to the original computation graph are also updated */ public ComputationGraph unfrozenGraph() { if (!isGraph) errorIfGraphIfMLN(); return unFrozenSubsetGraph; }
/** * Will modify the given MLN (in place!) to freeze layers (hold params constant during training) specified and below * * @param orig MLN to freeze * @param frozenTill integer indicating the index of the layer and below to freeze */ public TransferLearningHelper(MultiLayerNetwork orig, int frozenTill) { isGraph = false; this.frozenTill = frozenTill; applyFrozen = true; origMLN = orig; initHelperMLN(); }
public GraphBuilder nOutReplace(String layerName, int nOut, Distribution dist, WeightInit scheme) { return nOutReplace(layerName, nOut, WeightInit.DISTRIBUTION, scheme, dist, null); }
/** * Modify the architecture of a layer by changing nOut * Note this will also affect the layer that follows the layer specified, unless it is the output layer * Can specify different weight init schemes for the specified layer and the layer that follows it. * * @param layerNum The index of the layer to change nOut of * @param nOut Value of nOut to change to * @param scheme Weight init scheme to use for params in layerNum * @param distNext Distribution to use for parmas in layerNum+1 * @return Builder * @see org.deeplearning4j.nn.weights.WeightInit DISTRIBUTION */ public Builder nOutReplace(int layerNum, int nOut, WeightInit scheme, Distribution distNext) { return nOutReplace(layerNum, nOut, scheme, WeightInit.DISTRIBUTION, null, distNext); }
public GraphBuilder nOutReplace(String layerName, int nOut, WeightInit scheme, WeightInit schemeNext) { return nOutReplace(layerName, nOut, scheme, schemeNext, null, null); }
/** * Modify the architecture of a layer by changing nOut * Note this will also affect the layer that follows the layer specified, unless it is the output layer * Can specify different weight init schemes for the specified layer and the layer that follows it. * * @param layerNum The index of the layer to change nOut of * @param nOut Value of nOut to change to * @param scheme Weight Init scheme to use for params in the layerNum * @param schemeNext Weight Init scheme to use for params in the layerNum+1 * @return Builder */ public Builder nOutReplace(int layerNum, int nOut, WeightInit scheme, WeightInit schemeNext) { return nOutReplace(layerNum, nOut, scheme, schemeNext, null, null); }
/** * Modify the architecture of a vertex layer by changing nOut * Note this will also affect the vertex layer that follows the layer specified, unless it is the output layer * Currently does not support modifying nOut of layers that feed into non-layer vertices like merge, subset etc * To modify nOut for such vertices use remove vertex, followed by add vertex * Can specify different weight init schemes for the specified layer and the layer that follows it. * * @param layerName The name of the layer to change nOut of * @param nOut Value of nOut to change to * @param dist Weight distribution scheme to use * @return GraphBuilder * @see org.deeplearning4j.nn.weights.WeightInit DISTRIBUTION */ public GraphBuilder nOutReplace(String layerName, int nOut, Distribution dist) { return nOutReplace(layerName, nOut, WeightInit.DISTRIBUTION, WeightInit.DISTRIBUTION, dist, dist); }