Skip to content

Instantly share code, notes, and snippets.

@NKUCodingCat
Created August 1, 2016 05:56
Show Gist options
  • Save NKUCodingCat/4b7e9fda5a559f586b0b28bc89c87650 to your computer and use it in GitHub Desktop.
Save NKUCodingCat/4b7e9fda5a559f586b0b28bc89c87650 to your computer and use it in GitHub Desktop.
import cPickle as pickle
import numpy
import PyDeepCL
import re
# Warning: this module is just an approximate seriallize to the net
# It does not cover each property of NeuralNet
# Use it carefully
# Working in Python 2.7, PyDeepCL 10.3.0a3 / DeepCL 10.1.0
def NNSave(net):
# get a PyDeepCL.NeuralNet Object and return a serialized result with pickle protocol
DB = [None, ]*net.getNumLayers()
for idx in range(net.getNumLayers()):
try:
M = net.getLayer(idx)
DB[idx] = {
"NetdefString": M.getNetdefString(),
"OutPutSize": M.getOutputSize(),
"Planes" : M.getOutputPlanes(),
"Name": M.getClassName(),
"Weight": M.getWeights()
}
NetString = net.asString()
Q = net.getLayer(idx).getWeights()
# print(numpy.array(Q).shape, M.getClassName())
except:
import traceback
print "Caught error when dealing layer%s "%idx, net.getLayer(idx).getClassName()
traceback.print_exc()
return pickle.dumps((DB, net.getNetdef(), net.asString()))
def NNLoad(cl, Seriallized_net):
# I don't check if is illegal string of input, take care about it'
assert isinstance(cl, PyDeepCL.DeepCL)
H, NetString, NetasString = pickle.loads(Seriallized_net)
# Input Layer
net = PyDeepCL.NeuralNet(cl, H[0]["Planes"], H[0]['OutPutSize'])
# Uh......
for i in range(1, len(H)):
Layer_Now = H[i]
Layer_Name = re.sub("Layer$", "", Layer_Now["Name"])
Dict = {
"RandomTranslations": ("rt(\d+)", (lambda p1: PyDeepCL.RandomTranslationsMaker().translateSize(int(p1)))),
"Convolutional" : ("(\d+)c(\d+)(z?)", (lambda p1, p2, p3: PyDeepCL.ConvolutionalMaker().numFilters(int(p1)).filterSize(int(p2)).padZeros().biased())),
"Activation" : ("\S+", (lambda p1: getattr(PyDeepCL.ActivationMaker(), p1)())),
"Pooling" : ("mp(\d+)(z?)", (lambda p1, p2: PyDeepCL.PoolingMaker().poolingSize(int(p1)))),
"FullyConnected" : ("(\d+)n", (lambda p1: PyDeepCL.FullyConnectedMaker().numPlanes(Layer_Now["Planes"]).imageSize(1).biased())), # ImageSize is a default value
"SoftMax" : ("", (PyDeepCL.SoftMaxMaker)),
"Input" : ("", (lambda : (_ for _ in ()).throw(Exception("InputLayer should be created in initialize, There must be something wrong")))),
"Normalization" : ("", (lambda : (_ for _ in ()).throw(Exception("I don't know how to rebuild this layer, could you help me?"))))
}
print "Adding %s%s......."%(Layer_Name, " - %s"%Layer_Now["NetdefString"] if Layer_Now["NetdefString"] else "")
if Layer_Name in Dict.keys():
Tmp = Dict[Layer_Name]
G = re.findall(Tmp[0], Layer_Now["NetdefString"])[0]
if isinstance(G, tuple):
net.addLayer(Tmp[1](*G))
else:
net.addLayer(Tmp[1](G))
else:
raise BaseException("Layer_Name %s cannot be added beacause of NOT FOUND"%Layer_Name)
print "rebuild the net are as follows:"
print net.asString()
print "the net storage in file are as follows, please check it:"
print NetasString
print "Loading Weights......"
for i in range(0, len(H)):
if not isinstance(H[i]["Weight"],type(None)):
net.getLayer(i).setWeights(H[i]["Weight"])
return net
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment