public < M extends Model > M initPretrained ( PretrainedType pretrainedType ) throws IOException { String remoteUrl = pretrainedUrl ( pretrainedType ) ; if ( remoteUrl == null ) throw new UnsupportedOperationException ( "Pretrained " + pretrainedType + " weights are not available for this model." ) ; String localFilename = new File ( remoteUrl ) . getName ( ) ; File rootCacheDir = DL4JResources . getDirectory ( ResourceType . ZOO_MODEL , modelName ( ) ) ; File cachedFile = new File ( rootCacheDir , localFilename ) ; if ( ! cachedFile . exists ( ) ) { log . info ( "Downloading model to " + cachedFile . toString ( ) ) ; FileUtils . copyURLToFile ( new URL ( remoteUrl ) , cachedFile ) ; } else { log . info ( "Using cached model at " + cachedFile . toString ( ) ) ; } long expectedChecksum = pretrainedChecksum ( pretrainedType ) ; if ( expectedChecksum != 0L ) { log . info ( "Verifying download..." ) ; Checksum adler = new Adler32 ( ) ; FileUtils . checksum ( cachedFile , adler ) ; long localChecksum = adler . getValue ( ) ; log . info ( "Checksum local is " + localChecksum + ", expecting " + expectedChecksum ) ; if ( expectedChecksum != localChecksum ) { log . error ( "Checksums do not match. Cleaning up files and failing..." ) ; cachedFile . delete ( ) ; throw new IllegalStateException ( "Pretrained model file failed checksum. If this error persists, please open an issue at https://github.com/deeplearning4j/deeplearning4j." ) ; } } if ( modelType ( ) == MultiLayerNetwork . class ) { return ( M ) ModelSerializer . restoreMultiLayerNetwork ( cachedFile ) ; } else if ( modelType ( ) == ComputationGraph . class ) { return ( M ) ModelSerializer . restoreComputationGraph ( cachedFile ) ; } else { throw new UnsupportedOperationException ( "Pretrained models are only supported for MultiLayerNetwork and ComputationGraph." ) ; } }
Returns a pretrained model for the given dataset if available .