예제 #1
0
            public Model(string name, torch.Device device = null) : base(name)
            {
                RegisterComponents();

                if (device != null && device.type == DeviceType.CUDA)
                {
                    this.to(device);
                }
            }
예제 #2
0
            public Model(string name, int numClasses, torch.Device device = null) : base(name)
            {
                features = Sequential(
                    ("c1", Conv2d(3, 64, kernelSize: 3, stride: 2, padding: 1)),
                    ("r1", ReLU(inPlace: true)),
                    ("mp1", MaxPool2d(kernelSize: new long[] { 2, 2 })),
                    ("c2", Conv2d(64, 192, kernelSize: 3, padding: 1)),
                    ("r2", ReLU(inPlace: true)),
                    ("mp2", MaxPool2d(kernelSize: new long[] { 2, 2 })),
                    ("c3", Conv2d(192, 384, kernelSize: 3, padding: 1)),
                    ("r3", ReLU(inPlace: true)),
                    ("c4", Conv2d(384, 256, kernelSize: 3, padding: 1)),
                    ("r4", ReLU(inPlace: true)),
                    ("c5", Conv2d(256, 256, kernelSize: 3, padding: 1)),
                    ("r5", ReLU(inPlace: true)),
                    ("mp3", MaxPool2d(kernelSize: new long[] { 2, 2 })));

                avgPool = AdaptiveAvgPool2d(new long[] { 2, 2 });

                classifier = Sequential(
                    ("d1", Dropout()),
                    ("l1", Linear(256 * 2 * 2, 4096)),
                    ("r1", ReLU(inPlace: true)),
                    ("d2", Dropout()),
                    ("l2", Linear(4096, 4096)),
                    ("r3", ReLU(inPlace: true)),
                    ("d3", Dropout()),
                    ("l3", Linear(4096, numClasses))
                    );

                RegisterModule("features", features);
                RegisterModule("avg", avgPool);
                RegisterModule("classify", classifier);

                if (device != null && device.type == DeviceType.CUDA)
                {
                    this.to(device);
                }
            }
예제 #3
0
        public static torch.Tensor CollateTokens(IList <torch.Tensor> values, int padIndex, int?eosIndex = null,
                                                 bool leftPad = false, bool moveEosToBeginning = false, torch.Device device = null)
        {
            Contracts.AssertNonEmpty(values, "Can't collate 0 values");
            Contracts.Assert(values.All(v => v.dim() == 1), "All tensors should be 1D to collate.");

            var size = values.Select(v => v.size(0)).Max();
            var res  = values[0].new_full(values.Count, size, padIndex, device: device);

            for (var i = 0; i < values.Count; ++i)
            {
                var v = values[i];
                CopyTensor(
                    v,
                    leftPad
                        ? res[torch.TensorIndex.Single(i), torch.TensorIndex.Slice(start: size - v.size(0))]
                        : res[torch.TensorIndex.Single(i), torch.TensorIndex.Slice(stop: v.size(0))],
                    moveEosToBeginning,
                    eosIndex);
            }

            return(res);
        }
예제 #4
0
 /// <summary>
 /// Normalize a float tensor image with mean and standard deviation.
 /// </summary>
 /// <param name="means">Sequence of means for each channel.</param>
 /// <param name="stdevs">Sequence of standard deviations for each channel.</param>
 /// <param name="dtype">Bool to make this operation inplace.</param>
 /// <param name="device">The device to place the output tensor on.</param>
 /// <returns></returns>
 static public ITransform Normalize(double[] means, double[] stdevs, ScalarType dtype = ScalarType.Float32, torch.Device device = null)
 {
     return(new Normalize(means, stdevs, dtype, device));
 }
예제 #5
0
        internal Normalize(double[] means, double[] stdevs, ScalarType dtype = ScalarType.Float32, torch.Device device = null)
        {
            if (means.Length != stdevs.Length)
            {
                throw new ArgumentException("means and stdevs must be the same length in call to Normalize");
            }

            this.means  = means.ToTensor(new long[] { 1, means.Length, 1, 1 });    // Assumes NxCxHxW
            this.stdevs = stdevs.ToTensor(new long[] { 1, stdevs.Length, 1, 1 });  // Assumes NxCxHxW

            if (dtype != ScalarType.Float64)
            {
                this.means  = this.means.to_type(dtype);
                this.stdevs = this.stdevs.to_type(dtype);
            }

            if (device != null && device.type != DeviceType.CPU)
            {
                this.means  = this.means.to(device);
                this.stdevs = this.stdevs.to(device);
            }
        }