ConvertTorchToBoofForward { public static <T>T convert(TorchObject input ) { if( input instanceof TorchGeneric ) { TorchGeneric t = (TorchGeneric)input; if( t.torchName == null ) throw new IllegalArgumentException("Input object has no torchName. " + "Maybe the object you wish to convert is contained inside of it?"); FunctionAndParameters ret = new FunctionAndParameters(); String _type = findTorchType(t); if( _type == null ) _type = "Type Not Specified"; switch( t.torchName ) { case "nn.ReLU": { switch (_type) { case "torch.DoubleTensor": ret.function = new ActivationReLU_F64(); break; case "torch.FloatTensor": ret.function = new ActivationReLU_F32(); break; default: throw new RuntimeException("Unsupported data "+_type); } }break; case "nn.Sigmoid": { switch (_type) { case "torch.DoubleTensor": ret.function = new ActivationSigmoid_F64(); break; case "torch.FloatTensor": ret.function = new ActivationSigmoid_F32(); break; default: throw new RuntimeException("Unsupported data "+_type); } }break; case "nn.Tanh": { switch (_type) { case "torch.DoubleTensor": ret.function = new ActivationTanH_F64(); break; case "torch.FloatTensor": ret.function = new ActivationTanH_F32(); break; default: throw new RuntimeException("Unsupported data "+_type); } }break; case "nn.Linear": { Tensor weight = convert(t.map.get("weight")); Tensor bias = convert(t.map.get("bias")); int numOutput = bias.length(); switch (_type) { case "torch.DoubleTensor": ret.function = new FunctionLinear_F64(numOutput); break; case "torch.FloatTensor": ret.function = new FunctionLinear_F32(numOutput); break; default: throw new RuntimeException("Unsupported data "+_type); } ret.parameters.add(weight); ret.parameters.add(bias); }break; case "nn.BatchNormalization": return (T)convertBatchNormalization(t,_type); case "nn.SpatialConvolution": return (T)convertSpatialConvolution(t,_type); case "nn.SpatialMaxPooling": return (T) convertSpatialPooling(t,PoolingType.MAX,_type); case "nn.SpatialAveragePooling": return (T) convertSpatialPooling(t,PoolingType.AVE,_type); case "nn.SpatialBatchNormalization": return (T)convertSpatialBatchNormalization(t,_type); case "nn.Sequential": return (T)convertSequential(t,_type); case "nn.View": return null; case "nn.Dropout": case "nn.SpatialDropout": return (T)convertDropout(t,_type); default: throw new RuntimeException("Unsupported "+t.torchName); } return (T)ret; } else if( input instanceof TorchTensor ) { TorchTensor t = (TorchTensor)input; switch( t.torchName ) { case "torch.FloatTensor": return (T)convert_F32(t); case "torch.DoubleTensor": return (T)convert_F64(t); case "torch.ByteTensor": return (T)convert_U8(t); case "torch.LongTensor": return (T)convert_S64(t); default: throw new RuntimeException("Unsupported data "+t.torchName); } } else if( input instanceof TorchNumber ) { return (T)new Double(((TorchNumber)input).value); } return null; }  static T convert(TorchObject input );  }
@Test public void tensorStorage() { File pathToTensors = new File(pathToData,"tensor_storage"); if( !pathToTensors.exists() ) fail("Missing tensor_storage"); int count = 0; for( File d : pathToTensors.listFiles() ) { if (!d.isDirectory()) continue; d = new File(d,"001"); Tensor tensor; tensor = convert(readBinary(new File(d,"tensor"))); performTensorStorageChecks(tensor); tensor = convert(readAscii(new File(d,"tensor_ascii"))); performTensorStorageChecks(tensor); } }