def __init__(self, actions, epsilon=1, n_history=4, on_gpu=False, model_path="", load_if_exist=True):
self.actions = actions
self.epsilon = epsilon
self.q = Q(n_history, len(actions), on_gpu)
self._state = []
self._observations = [
np.zeros((self.q.SIZE, self.q.SIZE), np.float32),
np.zeros((self.q.SIZE, self.q.SIZE), np.float32)
] # now & pre
self.last_action = 0
self.model_path = model_path if model_path else os.path.join(os.path.dirname(__file__), "./store")
if not os.path.exists(self.model_path):
print("make directory to store model at {0}".format(self.model_path))
os.mkdir(self.model_path)
else:
models = self.get_model_files()
if load_if_exist and len(models) > 0:
print("load model file {0}.".format(models[-1]))
serializers.load_npz(os.path.join(self.model_path, models[-1]), self.q) # use latest model
python类load_npz()的实例源码
def __init__(self, model_path, config):
# hyper parameters
self.n_boxes = 5
self.config = config
self.labels = config['categories']
self.n_classes = len(self.labels)
self.detection_thresh = config['confidence']
self.iou_thresh = config['iou']
anchors = config['anchors']
# load model
print('loading model...')
yolov2 = YOLOv2(n_classes=self.n_classes, n_boxes=self.n_boxes)
serializers.load_npz(model_path, yolov2)
model = YOLOv2Predictor(yolov2)
model.init_anchor(anchors)
model.predictor.finetune = False
self.model = model
yolov2_predict_caltech.py 文件源码
项目:chainer-object-detection
作者: dsanno
项目源码
文件源码
阅读 20
收藏 0
点赞 0
评论 0
def __init__(self, model_path, config):
# hyper parameters
self.n_boxes = 5
self.config = config
self.labels = config['categories']
self.n_classes = len(self.labels)
self.detection_thresh = config['confidence']
self.iou_thresh = config['iou']
anchors = config['anchors']
# load model
print('loading model...')
yolov2 = YOLOv2(n_classes=self.n_classes, n_boxes=self.n_boxes)
serializers.load_npz(model_path, yolov2)
model = YOLOv2Predictor(yolov2)
model.init_anchor(anchors)
model.predictor.finetune = False
self.model = model
def test(args,encdec,model_name,categ_arr=[],predictFlag=False):
serializers.load_npz(model_name,encdec)
if args.gpu>=0:
import cupy as cp
global xp;xp=cp
encdec.to_gpu()
encdec.setBatchSize(args.batchsize)
if "cvae" in model_name:
for categ in categ_arr:
print("categ:{}".format(encdec.categ_vocab.itos(categ)))
if predictFlag:
encdec.predict(args.batchsize,tag=categ,randFlag=False)
elif predictFlag:
encdec.predict(args.batchsize,randFlag=False)
return encdec
def loadModel(self,model_name_base,args):
first_e = 0
model_name = ""
for e in range(args.epoch):
model_name_tmp = model_name_base.format(args.dataname, args.dataname, e,args.n_latent)
if os.path.exists(model_name_tmp):
model_name = model_name_tmp
self.setEpochNow(e + 1)
if os.path.exists(model_name):
print(model_name)
# serializers.load_npz(model_name, encdec)
serializers.load_npz(model_name, self)
print("loaded_{}".format(model_name))
first_e = self.epoch_now
else:
print("loadW2V")
if os.path.exists(args.premodel):
self.loadW(args.premodel)
else:
print("wordvec model doesnt exists.")
return first_e
def loadInfo(self, folder, model, state, smanager):
if(not os.path.exists(folder)):
return (model, state, 1)
list_files = []
model_name = model.getName()
for file in os.listdir(folder):
if(file.startswith(model_name) and file.endswith(".state")):
list_files.append(file)
if(len(list_files) > 0):
sorted_list = self.natural_sort(list_files)
fname_state = sorted_list[-1]
bname = re.split('\.',fname_state)[0]
fname_model = bname + '.model'
fname_stats = bname + '.stats'
epoch = int(re.split('_|\.', bname)[-1]) + 1
serializers.load_npz(folder + '/' + fname_state, state)
serializers.load_npz(folder + '/' + fname_model, model)
smanager.load(folder + '/' + fname_stats)
else:
epoch = 1
# no prev. models...
return (model, state, epoch)
def __init__(self,modelpath='misc/VGG16_faster_rcnn_final.model',
mean=[102.9801, 115.9465, 122.7717],
in_size=224):
super(FasterRCNN,self).__init__('FasterRCNN',in_size)
self.func = FRCNN(Deel.gpu)
self.func.train=False
serializers.load_npz('misc/VGG16_faster_rcnn_final.model', self.func)
ImageNet.mean_image = np.ndarray((3, 256, 256), dtype=np.float32)
ImageNet.mean_image[0] = mean[0]
ImageNet.mean_image[1] = mean[1]
ImageNet.mean_image[2] = mean[2]
ImageNet.in_size = in_size
self.labels = CLASSES
self.batchsize = 1
xp = Deel.xp
self.x_batch = xp.ndarray((self.batchsize, 3, self.in_size, self.in_size), dtype=np.float32)
if Deel.gpu >=0:
self.func = self.func.to_gpu(Deel.gpu)
self.optimizer = optimizers.Adam()
self.optimizer.setup(self.func)
def predict(limit):
_limit = limit if limit > 0 else 5
td = TrainingData(LABEL_FILE, img_root=IMAGES_ROOT, mean_image_file=MEAN_IMAGE_FILE, image_property=IMAGE_PROP)
label_def = LabelingMachine.read_label_def(LABEL_DEF_FILE)
model = alex.Alex(len(label_def))
serializers.load_npz(MODEL_FILE, model)
i = 0
for arr, im in td.generate():
x = np.ndarray((1,) + arr.shape, arr.dtype)
x[0] = arr
x = chainer.Variable(np.asarray(x), volatile="on")
y = model.predict(x)
p = np.argmax(y.data)
print("predict {0}, actual {1}".format(label_def[p], label_def[im.label]))
im.image.show()
i += 1
if i >= _limit:
break
def test(self, cgp, model_file, comp_graph='comp_graph.dot', batchsize=256):
chainer.cuda.get_device(0).use() # Make a specified GPU current
model = CGP2CNN(cgp, self.n_class)
print('\tLoad model from', model_file)
serializers.load_npz(model_file, model)
model.to_gpu(0)
test_accuracy, test_loss = self.__test(model, batchsize)
print('\tparamNum={}'.format(model.param_num))
print('\ttest mean loss={}, test accuracy={}'.format(test_loss / self.test_data_num, test_accuracy / self.test_data_num))
if comp_graph is not None:
with open(comp_graph, 'w') as o:
g = computational_graph.build_computational_graph((model.loss,))
o.write(g.dump())
del g
print('\tCNN graph generated ({}).'.format(comp_graph))
return test_accuracy, test_loss
def main():
model = voxelchain.VoxelChain()
serializers.load_npz('result/VoxelChain.model',model)
use_model(model)
def main():
model = voxelchain.VoxelChain()
serializers.load_npz('result/VoxelChain.model',model)
conv1(model)
conv2(model)
create_graph()
def main():
args = parse_args()
print("loading classifier model...")
input_model = YOLOv2Classifier(args.input_class)
serializers.load_npz(args.input_path, input_model)
model = YOLOv2(args.output_class, args.box)
copy_conv_layer(input_model, model, partial_layer)
copy_bias_layer(input_model, model, partial_layer)
copy_bn_layer(input_model, model, partial_layer)
print("saving model to %s" % (args.output_path))
serializers.save_npz(args.output_path, model)
def load_npz_no_strict(filename, obj):
try:
serializers.load_npz(filename, obj)
except KeyError as e:
warnings.warn(repr(e))
with numpy.load(filename) as f:
d = serializers.NpzDeserializer(f, strict=False)
d.load(obj)
def get_model(gpu):
model = FasterRCNN(gpu)
model.train = False
serializers.load_npz('data/VGG16_faster_rcnn_final.model', model)
return model
def model_init(self):
load_model = self.load_model
model = self.model
gpu = self.gpu
if load_model is None:
print('ReLU weight initialization')
model.weight_initialization()
else:
print('loading ' + self.load_model)
serializers.load_npz(load_model, model)
model.check_gpu(gpu)
def model_init(self):
load_model = self.load_model
model = self.model
gpu = self.gpu
if load_model is None:
print('ReLU weight initialization')
model.weight_initialization()
else:
print('loading ' + self.load_model)
serializers.load_npz(load_model, model)
model.check_gpu(gpu)
nutszebra_ilsvrc_object_localization.py 文件源码
项目:trainer
作者: nutszebra
项目源码
文件源码
阅读 20
收藏 0
点赞 0
评论 0
def model_init(self):
load_model = self.load_model
model = self.model
gpu = self.gpu
if load_model is None:
print('ReLU weight initialization')
model.weight_initialization()
else:
print('loading ' + self.load_model)
serializers.load_npz(load_model, model)
model.check_gpu(gpu)
nutszebra_ilsvrc_object_localization_with_multi_gpus.py 文件源码
项目:trainer
作者: nutszebra
项目源码
文件源码
阅读 22
收藏 0
点赞 0
评论 0
def model_init(model, load_model):
if load_model is None:
print('Weight initialization')
model.weight_initialization()
else:
print('loading {}'.format(load_model))
serializers.load_npz(load_model, model)
def load_model(self, path=''):
serializers.load_npz(path, self)
def model_init(self):
load_model = self.load_model
model = self.model
gpu = self.gpu
if load_model is None:
print('ReLU weight initialization')
model.weight_initialization()
else:
print('loading ' + self.load_model)
serializers.load_npz(load_model, model)
model.check_gpu(gpu)
def read_lstm_model(self, params, train):
assert train == False # reading a model to continue training is currently not supported
words_file = params['config_path'] + params['words_file']
model_file = params['config_path'] + params['model_file']
unit = int(params['unit'])
deep = (params['deep'] == 'yes')
drop_ratio = float(params['drop_ratio'])
#read and normalize target word embeddings
w, word2index, index2word = self.read_words(words_file)
s = numpy.sqrt((w * w).sum(1))
s[s==0.] = 1.
w /= s.reshape((s.shape[0], 1)) # normalize
context_word_units = unit
lstm_hidden_units = IN_TO_OUT_UNITS_RATIO*unit
target_word_units = IN_TO_OUT_UNITS_RATIO*unit
cs = [1 for _ in range(len(word2index))] # dummy word counts - not used for eval
loss_func = L.NegativeSampling(target_word_units, cs, NEGATIVE_SAMPLING_NUM) # dummy loss func - not used for eval
model = BiLstmContext(deep, self.gpu, word2index, context_word_units, lstm_hidden_units, target_word_units, loss_func, train, drop_ratio)
S.load_npz(model_file, model)
return w, word2index, index2word, model
def __init__(self, Nj, gpu, model_file, filename):
# initialize model to estimate.
self.model = AlexNet(Nj)
self.gpu = gpu
serializers.load_npz(model_file, self.model)
# prepare gpu.
if self.gpu >= 0:
chainer.cuda.get_device(gpu).use()
self.model.to_gpu()
# load dataset to estimate.
self.dataset = PoseDataset(filename)
def __init__(self, Nj, gpu, model_file, filename):
# initialize model to estimate.
self.model = AlexNet(Nj, use_visibility=True)
self.gpu = gpu
serializers.load_npz(model_file, self.model)
# prepare gpu.
if self.gpu >= 0:
chainer.cuda.get_device(gpu).use()
self.model.to_gpu()
# load dataset to estimate.
self.dataset = PoseDataset(filename)
def load(load_dir, epoch):
with (load_dir/meta_name).open('rb') as f:
storage = Storage(*np.load(f)[0])
serializers.load_npz(
str(load_dir/model_name(epoch)),
storage.model
)
serializers.load_npz(
str(load_dir/optimizer_name(epoch)),
storage.optimizer
)
return storage
def load(load_dir, epoch):
with (load_dir/meta_name).open('rb') as f:
storage = Storage(*np.load(f)[0])
serializers.load_npz(
str(load_dir/model_name(epoch)),
storage.model
)
serializers.load_npz(
str(load_dir/optimizer_name(epoch)),
storage.optimizer
)
return storage
def load_chain_model(self, **kwargs):
name = self.get_name(**kwargs)
path = '{}/{}'.format(self.folder,name)
epoch = int(kwargs.get("nepochs",2))
fn = "{}/chain_snapshot_epoch_{:06}".format(path,epoch)
chain, model = self.setup_chain_model(**kwargs)
S.load_npz(fn, chain)
return chain, model
def load_chain_model(self, **kwargs):
name = self.get_name(**kwargs)
path = '{}/{}'.format(self.folder,name)
epoch = int(kwargs.get("nepochs",2))
fn = "{}/chain_snapshot_epoch_{:06}".format(path,epoch)
chain, model = self.setup_chain_model(**kwargs)
S.load_npz(fn, chain)
return chain, model
def load_chain_model(self, **kwargs):
name = self.get_name(**kwargs)
path = '{}/{}'.format(self.folder,name)
epoch = int(kwargs.get("nepochs",2))
fn = "{}/chain_snapshot_epoch_{:06}".format(path,epoch)
chain, model = self.setup_chain_model(**kwargs)
S.load_npz(fn, chain)
return chain, model
def load_chain_model(self, **kwargs):
name = self.get_name(**kwargs)
path = '{}/{}'.format(self.folder,name)
epoch = int(kwargs.get("nepochs",2))
fn = "{}/chain_snapshot_epoch_{:06}".format(path,epoch)
chain, model = self.setup_chain_model(**kwargs)
S.load_npz(fn, chain)
return chain, model
def load_chain_model(self, **kwargs):
name = self.get_name(**kwargs)
path = '{}/{}'.format(self.folder,name)
epoch = int(kwargs.get("nepochs",2))
fn = "{}/chain_snapshot_epoch_{:06}".format(path,epoch)
chain, model = self.setup_chain_model(**kwargs)
S.load_npz(fn, chain)
return chain, model