Repository navigation
Expand file tree
/
Copy pathtrain_NN_model.sql
More file actions
175 lines (154 loc) · 5.93 KB
/
Copy pathtrain_NN_model.sql
File metadata and controls
175 lines (154 loc) · 5.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
-- =============================================
-- Description: Galaxies Training with Microsoft ML Neural Nets.
-- =============================================
CREATE PROCEDURE [dbo].[TrainGalaxiesNN]
AS
BEGIN
SET NOCOUNT ON;
-- Construct the RML script.
declare @cmd nvarchar(max)
set @cmd = N'
library(MicrosoftML)
library(dplyr)
set.seed(123)
dataParms <- list(
image_root = "C:/Galaxies/data",
outmodel_root = "C:/Galaxies/models",
training_rows = 238000,
test_rows = 1500
)
hyperParms <- list(
comment = "Add rotations for 45 and 90 degrees, test on 10K rows, increase iterations to 250",
augmentation = "none",
netDefSummary = "conv(6x6) => norm() => pool(2x2) => conv(5x5) => norm() => pool(2x2) => full(128) => full(128) => out(13)",
numIterations = 250,
miniBatchSize = 128,
acceleration = "gpu"
)
optimParms <- list(
optimizer = "sgd",
learningRate = 0.05,
lRateRedRatio = 0.99,
lRateRedFreq = 5,
momentum = 0.2,
decay = 0.95,
conditioningConst = 1e-6
)
# Read csv with Labels info.
dat_all <- read.csv("C:/Galaxies/data/galaxiesTraining.csv", stringsAsFactors = FALSE)
dat_all$specobjid <- as.character(dat_all$specobjid)
dat_all$toplevel_class <- as.factor(dat_all$toplevel_class)
sample_train <- base::sample(nrow(dat_all),
size = dataParms$training_rows)
sample_test <- base::sample((1:nrow(dat_all))[-sample_train],
size = dataParms$test_rows)
dat_train <- dat_all %>%
slice(sample_train)
dat_test <- dat_all %>%
slice(sample_test)
# This function assists with images loading during training.
# It s called from mlTransforms and uses Microsoft ML internal features that a subject to change.
# Next release will have more elagant solution.
getBitmapLoader <- function(mode)
{
if (mode == "path" )
return("BitmapLoaderTransform{col=Image:path}")
if (mode == "scaler" )
return("BitmapScalerTransform{col=bitmap:Image width=50 height=50}")
if (mode == "pixel" )
return("PixelExtractorTransform{col=pixels:bitmap}")
}
# Data augmentation: adding the rotations.
dat_train <- dat_train %>%
bind_rows(
dat_train %>% mutate(
path = gsub("raw-resized", "raw-resized-rotated-45", path)
) %>% bind_rows(
dat_train %>% mutate(
path = gsub("raw-resized", "raw-resized-rotated-90", path)
)
)
)
# Define NN using Net# language.
# See https://docs.microsoft.com/en-us/azure/machine-learning/machine-learning-azure-ml-netsharp-reference-guide for more info.
netDefinition <- ("input pixels [3, 50, 50];
hidden conv1 [64, 23, 23] rlinear from pixels convolve {
InputShape = [3, 50, 50];
KernelShape = [3, 6, 6];
Stride = [1, 2, 2];
Sharing = [false, true, true];
MapCount = 64;
}
hidden norm1 [64, 23, 23] from conv1 response norm {
InputShape = [64, 23, 23];
KernelShape = [1, 1, 1];
Alpha = 0.0001;
Beta = 0.75;
Offset = 1;
AvgOverFullKernel = true;
}
hidden pool1 [64, 12, 12] from norm1 max pool {
InputShape = [64, 23, 23];
KernelShape = [1, 2, 2];
Stride = [1, 2, 2];
Padding = [true, true, true];
}
hidden conv2 [128, 5, 5] rlinear from pool1 convolve {
InputShape = [64, 12, 12];
KernelShape = [1, 5, 5];
Stride = [1, 2, 2];
LowerPad = [0, 1, 1];
Sharing = [false, true, true];
MapCount = 2;
}
hidden norm2 [128, 5, 5] from conv2 response norm {
InputShape = [128, 5, 5];
KernelShape = [1, 1, 1];
Alpha = 0.0001;
Beta = 0.75;
Offset = 1;
AvgOverFullKernel = true;
}
hidden pool2 [128, 3, 3] from norm2 max pool {
InputShape = [128, 5, 5];
KernelShape = [1, 2, 2];
Stride = [1, 2, 2];
Padding = [true, true, true];
}
hidden hid1 [128] rlinear from pool2 all;
hidden hid2 [128] rlinear from hid1 all;
output Class [13] softmax from hid2 all;"
)
optimiser <- with(optimParms, sgd(learningRate = learningRate,
lRateRedRatio = lRateRedRatio,
lRateRedFreq = lRateRedFreq,
momentum = momentum))
model <- rxNeuralNet(toplevel_class ~ pixels, data = dat_train,
type = "multiClass",
mlTransformVars = "path",
mlTransforms = list(
getBitmapLoader("path"),
getBitmapLoader("scaler"),
getBitmapLoader("pixel")
),
netDefinition = netDefinition,
optimizer = optimiser,
acceleration = hyperParms$acceleration,
miniBatchSize = hyperParms$miniBatchSize,
numIterations = hyperParms$numIterations,
normalize = "no",
initWtsDiameter = 0.1,
verbose = 1,
postTransformCache = "Disk")
trained_model <- data.frame(payload = as.raw(serialize(model, connection=NULL)));
'
create table #m (model varbinary(max));
insert into #m
execute sp_execute_external_script
@language = N'R'
, @script = @cmd
, @output_data_1_name = N'trained_model ';
insert into [dbo].GalaxiesModels(CreationDate, Model, [Name])
select CURRENT_TIMESTAMP as timest, model, 'prod' as summary from #m;
drop table #m
END