Commit 28de4da4 authored by Evgeny Kusmenko's avatar Evgeny Kusmenko
Browse files

Merge branch 'adapt_cnntrainer_template' into 'master'

Adapt cnntrainer template

See merge request !6
parents ba0e6728 c24a0047
Pipeline #76008 passed with stages
in 6 minutes and 13 seconds
......@@ -99,11 +99,11 @@ ${tc.include(tc.architecture.body)}
accuracy = brew.accuracy(model, [output, label], "accuracy", top_k=3)
return accuracy
def train(self, num_epoch=1000, batch_size=64, device_opts='gpu', eval_metric='accuracy', opt_type='adam', base_learning_rate=0.001, weight_decay=0.001, policy='fixed', stepsize=1, epsilon=1E-8, beta1=0.9, beta2=0.999, gamma=0.999, momentum=0.9) :
if device_opts == 'cpu':
def train(self, num_epoch=1000, batch_size=64, context='gpu', eval_metric='accuracy', opt_type='adam', base_learning_rate=0.001, weight_decay=0.001, policy='fixed', stepsize=1, epsilon=1E-8, beta1=0.9, beta2=0.999, gamma=0.999, momentum=0.9) :
if context == 'cpu':
device_opts = core.DeviceOption(caffe2_pb2.CPU, 0)
print("CPU mode selected")
elif device_opts == 'gpu':
elif context == 'gpu':
device_opts = core.DeviceOption(caffe2_pb2.CUDA, 0)
print("GPU mode selected")
......
from caffe2.python import workspace, core, model_helper, brew, optimizer
from caffe2.python.predictor import mobile_exporter
from caffe2.proto import caffe2_pb2
import numpy as np
import cv2
import logging
import mxnet as mx
<#list configurations as config>
import CNNCreator_${config.instanceName}
</#list>
......@@ -13,31 +18,86 @@ if __name__ == "__main__":
<#list configurations as config>
${config.instanceName} = CNNCreator_${config.instanceName}.CNNCreator_${config.instanceName}()
${config.instanceName}.train(
<#if (config.batchSize)??>
batch_size=${config.batchSize},
</#if>
<#if (config.numEpoch)??>
num_epoch=${config.numEpoch},
</#if>
<#if (config.batchSize)??>
batch_size=${config.batchSize},
</#if>
<#if (config.loadCheckpoint)??>
load_checkpoint=${config.loadCheckpoint?string("True","False")},
<#--load_checkpoint=${config.loadCheckpoint?string("True","False")}, -->
</#if>
<#if (config.context)??>
context='${config.context}',
</#if>
<#if (config.normalize)??>
normalize=${config.normalize?string("True","False")},
<#--normalize=${config.normalize?string("True","False")}, -->
</#if>
<#if (config.evalMetric)??>
eval_metric='${config.evalMetric}',
</#if>
<#if (config.configuration.optimizer)??>
optimizer='${config.optimizerName}',
optimizer_params={
<#list config.optimizerParams?keys as param>
'${param}': ${config.optimizerParams[param]}<#sep>,
</#list>
}
opt_type='${config.optimizerName}',
<#--The following gets the optimizer parameter values separately instead of getting them as a list to avoid unsupported parameters in Caffe2-->
<#if (config.optimizerParams["learning_rate"])??>
base_learning_rate=${config.optimizerParams["learning_rate"]},
</#if>
<#if (config.optimizerParams["weight_decay"])??>
weight_decay=${config.optimizerParams["weight_decay"]},
</#if>
<#if (config.optimizerParams["learning_rate_policy"])??>
policy=${config.optimizerParams["learning_rate_policy"]},
</#if>
<#if (config.optimizerParams["step_size"])??>
stepsize=${config.optimizerParams["step_size"]},
</#if>
<#if (config.optimizerParams["epsilon"])??>
epsilon=${config.optimizerParams["epsilon"]},
</#if>
<#if (config.optimizerParams["beta1"])??>
beta1=${config.optimizerParams["beta1"]},
</#if>
<#if (config.optimizerParams["beta2"])??>
beta2=${config.optimizerParams["beta2"]},
</#if>
<#if (config.optimizerParams["gamma1"])??>
gamma=${config.optimizerParams["gamma1"]},
</#if>
<#if (config.optimizerParams["momentum"])??>
momentum=${config.optimizerParams["momentum"]},
</#if>
</#if>
)
</#list>
\ No newline at end of file
</#list>
<#--Below code can be removed. It is only an specific example to verify that deploy_net works-->
print '\n********************************************'
print("Loading Deploy model")
<#list configurations as config>
<#if (config.context)??>
context='${config.context}'
<#else>
context = 'gpu'
</#if>
<#--Code section that decides the mode cannot be moved into the load_net function since workspace.FeedBlob also needs this parameter-->
if context == 'cpu':
device_opts = core.DeviceOption(caffe2_pb2.CPU, 0)
print("CPU mode selected")
elif context == 'gpu':
device_opts = core.DeviceOption(caffe2_pb2.CUDA, 0)
print("GPU mode selected")
</#list>
LeNet.load_net(LeNet.INIT_NET, LeNet.PREDICT_NET, device_opts=device_opts)
img = cv2.imread("3.jpg") # Load test image
img = cv2.resize(img, (28,28)) # Resize to 28x28
img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY ) # Covert to grayscale
img = img.reshape((1,1,28,28)).astype('float32') # Reshape to (1,1,28,28)
workspace.FeedBlob("data", img, device_option=device_opts) # FeedBlob
workspace.RunNet('deploy_net', num_iter=1) # Forward
print("\nInput: {}".format(img.shape))
pred = workspace.FetchBlob("predictions")
print("Output: {}".format(pred))
print("Output class: {}".format(np.argmax(pred)))
......@@ -192,11 +192,11 @@ class CNNCreator_Alexnet:
accuracy = brew.accuracy(model, [output, label], "accuracy", top_k=3)
return accuracy
def train(self, num_epoch=1000, batch_size=64, device_opts='gpu', eval_metric='accuracy', opt_type='adam', base_learning_rate=0.001, weight_decay=0.001, policy='fixed', stepsize=1, epsilon=1E-8, beta1=0.9, beta2=0.999, gamma=0.999, momentum=0.9) :
if device_opts == 'cpu':
def train(self, num_epoch=1000, batch_size=64, context='gpu', eval_metric='accuracy', opt_type='adam', base_learning_rate=0.001, weight_decay=0.001, policy='fixed', stepsize=1, epsilon=1E-8, beta1=0.9, beta2=0.999, gamma=0.999, momentum=0.9) :
if context == 'cpu':
device_opts = core.DeviceOption(caffe2_pb2.CPU, 0)
print("CPU mode selected")
elif device_opts == 'gpu':
elif context == 'gpu':
device_opts = core.DeviceOption(caffe2_pb2.CUDA, 0)
print("GPU mode selected")
......
......@@ -278,11 +278,11 @@ class CNNCreator_CifarClassifierNetwork:
accuracy = brew.accuracy(model, [output, label], "accuracy", top_k=3)
return accuracy
def train(self, num_epoch=1000, batch_size=64, device_opts='gpu', eval_metric='accuracy', opt_type='adam', base_learning_rate=0.001, weight_decay=0.001, policy='fixed', stepsize=1, epsilon=1E-8, beta1=0.9, beta2=0.999, gamma=0.999, momentum=0.9) :
if device_opts == 'cpu':
def train(self, num_epoch=1000, batch_size=64, context='gpu', eval_metric='accuracy', opt_type='adam', base_learning_rate=0.001, weight_decay=0.001, policy='fixed', stepsize=1, epsilon=1E-8, beta1=0.9, beta2=0.999, gamma=0.999, momentum=0.9) :
if context == 'cpu':
device_opts = core.DeviceOption(caffe2_pb2.CPU, 0)
print("CPU mode selected")
elif device_opts == 'gpu':
elif context == 'gpu':
device_opts = core.DeviceOption(caffe2_pb2.CUDA, 0)
print("GPU mode selected")
......
......@@ -167,11 +167,11 @@ class CNNCreator_VGG16:
accuracy = brew.accuracy(model, [output, label], "accuracy", top_k=3)
return accuracy
def train(self, num_epoch=1000, batch_size=64, device_opts='gpu', eval_metric='accuracy', opt_type='adam', base_learning_rate=0.001, weight_decay=0.001, policy='fixed', stepsize=1, epsilon=1E-8, beta1=0.9, beta2=0.999, gamma=0.999, momentum=0.9) :
if device_opts == 'cpu':
def train(self, num_epoch=1000, batch_size=64, context='gpu', eval_metric='accuracy', opt_type='adam', base_learning_rate=0.001, weight_decay=0.001, policy='fixed', stepsize=1, epsilon=1E-8, beta1=0.9, beta2=0.999, gamma=0.999, momentum=0.9) :
if context == 'cpu':
device_opts = core.DeviceOption(caffe2_pb2.CPU, 0)
print("CPU mode selected")
elif device_opts == 'gpu':
elif context == 'gpu':
device_opts = core.DeviceOption(caffe2_pb2.CUDA, 0)
print("GPU mode selected")
......
from caffe2.python import workspace, core, model_helper, brew, optimizer
from caffe2.python.predictor import mobile_exporter
from caffe2.proto import caffe2_pb2
import numpy as np
import cv2
import logging
import mxnet as mx
import CNNCreator_emptyConfig
if __name__ == "__main__":
......@@ -11,3 +16,28 @@ if __name__ == "__main__":
emptyConfig = CNNCreator_emptyConfig.CNNCreator_emptyConfig()
emptyConfig.train(
)
print '\n********************************************'
print("Loading Deploy model")
context = 'gpu'
if context == 'cpu':
device_opts = core.DeviceOption(caffe2_pb2.CPU, 0)
print("CPU mode selected")
elif context == 'gpu':
device_opts = core.DeviceOption(caffe2_pb2.CUDA, 0)
print("GPU mode selected")
LeNet.load_net(LeNet.INIT_NET, LeNet.PREDICT_NET, device_opts=device_opts)
img = cv2.imread("3.jpg") # Load test image
img = cv2.resize(img, (28,28)) # Resize to 28x28
img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY ) # Covert to grayscale
img = img.reshape((1,1,28,28)).astype('float32') # Reshape to (1,1,28,28)
workspace.FeedBlob("data", img, device_option=device_opts) # FeedBlob
workspace.RunNet('deploy_net', num_iter=1) # Forward
print("\nInput: {}".format(img.shape))
pred = workspace.FetchBlob("predictions")
print("Output: {}".format(pred))
print("Output class: {}".format(np.argmax(pred)))
from caffe2.python import workspace, core, model_helper, brew, optimizer
from caffe2.python.predictor import mobile_exporter
from caffe2.proto import caffe2_pb2
import numpy as np
import cv2
import logging
import mxnet as mx
import CNNCreator_fullConfig
if __name__ == "__main__":
......@@ -10,25 +15,40 @@ if __name__ == "__main__":
fullConfig = CNNCreator_fullConfig.CNNCreator_fullConfig()
fullConfig.train(
batch_size=100,
num_epoch=5,
load_checkpoint=True,
batch_size=100,
context='gpu',
normalize=True,
eval_metric='mse',
optimizer='rmsprop',
optimizer_params={
'weight_decay': 0.01,
'centered': True,
'gamma2': 0.9,
'gamma1': 0.9,
'clip_weights': 10.0,
'learning_rate_decay': 0.9,
'epsilon': 1.0E-6,
'rescale_grad': 1.1,
'clip_gradient': 10.0,
'learning_rate_minimum': 1.0E-5,
'learning_rate_policy': 'step',
'learning_rate': 0.001,
'step_size': 1000}
opt_type='rmsprop',
base_learning_rate=0.001,
weight_decay=0.01,
policy='step',
stepsize=1000,
epsilon=1.0E-6,
gamma=0.9,
)
print '\n********************************************'
print("Loading Deploy model")
context='gpu'
if context == 'cpu':
device_opts = core.DeviceOption(caffe2_pb2.CPU, 0)
print("CPU mode selected")
elif context == 'gpu':
device_opts = core.DeviceOption(caffe2_pb2.CUDA, 0)
print("GPU mode selected")
LeNet.load_net(LeNet.INIT_NET, LeNet.PREDICT_NET, device_opts=device_opts)
img = cv2.imread("3.jpg") # Load test image
img = cv2.resize(img, (28,28)) # Resize to 28x28
img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY ) # Covert to grayscale
img = img.reshape((1,1,28,28)).astype('float32') # Reshape to (1,1,28,28)
workspace.FeedBlob("data", img, device_option=device_opts) # FeedBlob
workspace.RunNet('deploy_net', num_iter=1) # Forward
print("\nInput: {}".format(img.shape))
pred = workspace.FetchBlob("predictions")
print("Output: {}".format(pred))
print("Output class: {}".format(np.argmax(pred)))
from caffe2.python import workspace, core, model_helper, brew, optimizer
from caffe2.python.predictor import mobile_exporter
from caffe2.proto import caffe2_pb2
import numpy as np
import cv2
import logging
import mxnet as mx
import CNNCreator_simpleConfig
if __name__ == "__main__":
......@@ -10,9 +15,33 @@ if __name__ == "__main__":
simpleConfig = CNNCreator_simpleConfig.CNNCreator_simpleConfig()
simpleConfig.train(
batch_size=100,
num_epoch=50,
optimizer='adam',
optimizer_params={
'learning_rate': 0.001}
batch_size=100,
opt_type='adam',
base_learning_rate=0.001,
)
print '\n********************************************'
print("Loading Deploy model")
context = 'gpu'
if context == 'cpu':
device_opts = core.DeviceOption(caffe2_pb2.CPU, 0)
print("CPU mode selected")
elif context == 'gpu':
device_opts = core.DeviceOption(caffe2_pb2.CUDA, 0)
print("GPU mode selected")
LeNet.load_net(LeNet.INIT_NET, LeNet.PREDICT_NET, device_opts=device_opts)
img = cv2.imread("3.jpg") # Load test image
img = cv2.resize(img, (28,28)) # Resize to 28x28
img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY ) # Covert to grayscale
img = img.reshape((1,1,28,28)).astype('float32') # Reshape to (1,1,28,28)
workspace.FeedBlob("data", img, device_option=device_opts) # FeedBlob
workspace.RunNet('deploy_net', num_iter=1) # Forward
print("\nInput: {}".format(img.shape))
pred = workspace.FetchBlob("predictions")
print("Output: {}".format(pred))
print("Output class: {}".format(np.argmax(pred)))
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment