def test_save(self): obj = mock.MagicMock() npz.save_npz(self.temp_file_path, obj, self.compress) self.assertEqual(obj.serialize.call_count, 1) (serializer,), _ = obj.serialize.call_args self.assertIsInstance(serializer, npz.DictionarySerializer)
def setUp(self): if self.file_type == 'filename': fd, path = tempfile.mkstemp() os.close(fd) self.file = path elif self.file_type == 'bytesio': self.file = six.BytesIO() else: assert False # Create and save a link child = link.Chain() with child.init_scope(): child.linear = links.Linear(2, 3) parent = link.Chain() with parent.init_scope(): parent.linear = links.Linear(3, 2) parent.child = child npz.save_npz(self.file, parent) self.source = parent if self.file_type == 'bytesio': self.file.seek(0) self.npzfile = numpy.load(self.file) self.deserializer = npz.NpzDeserializer(self.npzfile, strict=False)
def test_load_optimizer(self): for param in self.parent.params(): param.data.fill(1) npz.save_npz(self.temp_file_path, self.parent, self.compress) for param in self.parent.params(): param.data.fill(0) npz.load_npz(self.temp_file_path, self.parent) for param in self.parent.params(): self.assertTrue((param.data == 1).all())
def setUp(self): fd, path = tempfile.mkstemp() os.close(fd) self.temp_file_path = path child = link.Chain(child_linear=links.Linear(2, 3)) parent = link.Chain( parent_linear=links.Linear(3, 2), child=child) npz.save_npz(path, parent, self.compress) self.source_child = child self.source_parent = parent
def test_load_optimizer_without_strict(self): for param in self.parent.params(): param.data.fill(1) npz.save_npz(self.temp_file_path, self.parent, self.compress) # Remove a param del self.parent.child.linear.b for param in self.parent.params(): param.data.fill(0) npz.load_npz(self.temp_file_path, self.parent, strict=False) for param in self.parent.params(): self.assertTrue((param.data == 1).all()) self.assertFalse(hasattr(self.parent.child.linear, 'b'))
def setUp(self): fd, path = tempfile.mkstemp() os.close(fd) self.temp_file_path = path child = link.Chain(linear=links.Linear(2, 3)) parent = link.Chain(linear=links.Linear(3, 2), child=child) npz.save_npz(self.temp_file_path, parent) self.source = parent self.npzfile = numpy.load(path) self.deserializer = npz.NpzDeserializer(self.npzfile, strict=False)
def convert_caffemodel_to_npz(cls, path_caffemodel, path_npz): """Converts a pre-trained caffemodel to a chainer model. Args: path_caffemodel (str): Path of the pre-trained caffemodel. path_npz (str): Path of the converted chainer model. """ # As CaffeFunction uses shortcut symbols, # we import CaffeFunction here. from chainer.links.caffe.caffe_function import CaffeFunction caffemodel = CaffeFunction(path_caffemodel) npz.save_npz(path_npz, caffemodel, compression=False)
def test_serialization(self): lin1 = links.SimplifiedDropconnect(None, self.out_size) x = chainer.Variable(self.x) # Must call the link to initialize weights. lin1(x) w1 = lin1.W.data fd, temp_file_path = tempfile.mkstemp() os.close(fd) npz.save_npz(temp_file_path, lin1) lin2 = links.SimplifiedDropconnect(None, self.out_size) npz.load_npz(temp_file_path, lin2) w2 = lin2.W.data self.assertEqual((w1 == w2).all(), True)
def setUp(self): fd, path = tempfile.mkstemp() os.close(fd) self.temp_file_path = path child = link.Chain() with child.init_scope(): child.child_linear = links.Linear(2, 3) parent = link.Chain() with parent.init_scope(): parent.parent_linear = links.Linear(3, 2) parent.child = child npz.save_npz(path, parent, self.compress) self.source_child = child self.source_parent = parent
def setUp(self): fd, path = tempfile.mkstemp() os.close(fd) self.temp_file_path = path child = link.Chain() with child.init_scope(): child.linear2 = links.Linear(2, 3) parent = link.Chain() with parent.init_scope(): parent.linear = links.Linear(3, 2) parent.child = child npz.save_npz(self.temp_file_path, parent) self.source = parent self.npzfile = numpy.load(path) self.deserializer = npz.NpzDeserializer( self.npzfile, ignore_names=self.ignore_names)
def convert_caffemodel_to_npz(cls, path_caffemodel, path_npz, n_layers=50): """Converts a pre-trained caffemodel to a chainer model. Args: path_caffemodel (str): Path of the pre-trained caffemodel. path_npz (str): Path of the converted chainer model. """ # As CaffeFunction uses shortcut symbols, # we import CaffeFunction here. from chainer.links.caffe.caffe_function import CaffeFunction caffemodel = CaffeFunction(path_caffemodel) chainermodel = cls(pretrained_model=None, n_layers=n_layers) if n_layers == 50: _transfer_resnet50(caffemodel, chainermodel) elif n_layers == 101: _transfer_resnet101(caffemodel, chainermodel) elif n_layers == 152: _transfer_resnet152(caffemodel, chainermodel) else: raise ValueError('The n_layers argument should be either 50, 101,' ' or 152, but {} was given.'.format(n_layers)) npz.save_npz(path_npz, chainermodel, compression=False)
def setUp(self): if self.file_type == 'filename': fd, path = tempfile.mkstemp() os.close(fd) self.file = path elif self.file_type == 'bytesio': self.file = six.BytesIO() else: assert False child = link.Chain() with child.init_scope(): child.child_linear = links.Linear(2, 3) parent = link.Chain() with parent.init_scope(): parent.parent_linear = links.Linear(3, 2) parent.child = child npz.save_npz(self.file, parent, self.compress) if self.file_type == 'bytesio': self.file.seek(0) self.source_child = child self.source_parent = parent
from chainer.links.caffe.caffe_function import CaffeFunction from chainer.serializers import npz caffemodel = CaffeFunction('./src/VGG_ILSVRC_19_layers.caffemodel') npz.save_npz('VGG_ILSVRC_19_layers.npz', caffemodel, compression=False)
def _convert_caffemodel_to_npz(path_caffemodel, path_npz): # As CaffeFunction uses shortcut symbols, # we import CaffeFunction here. from chainer.links.caffe.caffe_function import CaffeFunction caffemodel = CaffeFunction(path_caffemodel) npz.save_npz(path_npz, caffemodel, compression=False)
def test_save_optimizer2(self): npz.save_npz(self.temp_file_path, self.optimizer, self.compress) with numpy.load(self.temp_file_path) as f: self._check_optimizer_group(f, ("Wp/msg", "Wp/msdx", "epoch", "t"))
def test_save_chain2(self): npz.save_npz(self.temp_file_path, self.parent, self.compress) with numpy.load(self.temp_file_path) as f: self._check_chain_group(f, ("Wp",))
def caffe2npz(caffe_path): caffe_model = CaffeFunction(caffe_path) npz_filename = Path(caffe_path).stem + '.npz' npz.save_npz(npz_filename, caffe_model, compression=False) return npz_filename
import argparse from chainer.links.caffe.caffe_function import CaffeFunction from chainer.serializers import npz if __name__ == '__main__': parser = argparse.ArgumentParser(description='Convert caffemodel to npz') parser.add_argument('--input_caffemodel', '-i', default='VGG_ILSVRC_16_layers.caffemodel') parser.add_argument('--output_npz', '-o', default='VGG_ILSVRC_16_layers.npz') args = parser.parse_args() caffemodel = CaffeFunction(args.input_caffemodel) npz.save_npz(args.output_npz, caffemodel, compression=False)
def test_save_optimizer2(self): npz.save_npz(self.temp_file_path, self.optimizer, self.compress) with numpy.load(self.temp_file_path) as f: self._check_optimizer_group(f, ('Wp/msg', 'Wp/msdx', 'epoch', 't'))
def test_save_chain2(self): npz.save_npz(self.temp_file_path, self.parent, self.compress) with numpy.load(self.temp_file_path) as f: self._check_chain_group(f, ('Wp', ))
def _save_npz(self, file, obj, compress): npz.save_npz(file, obj, compress) if self.file_type == 'bytesio': self.file.seek(0)
def convert_caffemodel_to_npz(path_caffemodel, path_npz): from chainer.links.caffe.caffe_function import CaffeFunction caffemodel = CaffeFunction(path_caffemodel) npz.save_npz(path_npz, caffemodel, compression=False)
def convert_tf_to_chainer(cls, tf_npz_path, chainer_npz_path, depth_mult): tf_model = np.load(tf_npz_path) chainermodel = cls(depth_multiplier=depth_mult) cls._transfer_mobile_net_v1(tf_model, chainermodel) npz.save_npz(chainer_npz_path, chainermodel, compression=False)
from chainer.serializers import npz import os, tkinter.filedialog, tkinter, tkinter.messagebox from chainer.links.caffe import CaffeFunction root = tkinter.Tk() root.withdraw() fTyp = [("", "*")] iDir = os.path.abspath(os.path.dirname(__file__)) path = tkinter.filedialog.askopenfilename(filetypes=fTyp, initialdir=iDir) model = CaffeFunction(path) npz.save_npz('vgg16.npz', model, compression=False)
self.conv3 = L.Convolution2D(None, 384, 3, pad=1) self.conv4 = L.Convolution2D(None, 384, 3, pad=1) self.conv5 = L.Convolution2D(None, 256, 3, pad=1) self.fc6 = L.Linear(None, 4096) self.fc7 = L.Linear(None, 4096) self.fc8 = L.Linear(None, nb_class) def __call__(self, x, t): h = F.max_pooling_2d(F.local_response_normalization( F.relu(self.conv1(x))), 3, stride=2) h = F.max_pooling_2d(F.local_response_normalization( F.relu(self.conv2(h))), 3, stride=2) h = F.relu(self.conv3(h)) h = F.relu(self.conv4(h)) h = F.max_pooling_2d(F.relu(self.conv5(h)), 3, stride=2) h = F.dropout(F.relu(self.fc6(h))) h = F.dropout(F.relu(self.fc7(h))) h = self.fc8(h) return h if __name__ == "__main__": caffemodel = CaffeFunction("bvlc_alexnet.caffemodel") npz.save_npz("alexnet.npz", caffemodel, compression=False) alexnet = Alex() npz.load_npz("alexnet.npz", alexnet)