def test_Reshape(self): class Link(chainer.Chain): def __call__(self, x): return F.reshape(x, (-1,)) caffe.export(Link(), [self.x], None, True, 'test')
def main(): parser = argparse.ArgumentParser(description='Export') parser.add_argument('--arch', '-a', type=str, required=True, choices=archs.keys(), help='Arch name. models: ' + ', '.join(archs.keys()) + '.') parser.add_argument('--out-dir', '-o', type=str, required=True, help='Output directory name. ' 'chainer_model.prototxt, chainer_model.caffemodel' ' will be created in it') args = parser.parse_args() if not os.path.exists(args.out_dir): print('Created output directory: ' + args.out_dir) os.mkdir(args.out_dir) else: print('Overwriting the existing directory: ' + args.out_dir) if not os.path.isdir(args.out_dir): raise ValueError(args.out_dir + ' exists but not a directory!') print("load model") model, input = get_network_for_imagenet(args.arch) print("convert to caffe model") caffe.export(model, [input], args.out_dir, True)
def main(): parser = argparse.ArgumentParser(description='Export') parser.add_argument( '--arch', '-a', type=str, required=True, choices=archs.keys(), help='Arch name. models: ' + ', '.join(archs.keys()) + '.') parser.add_argument( '--out-dir', '-o', type=str, required=True, help='Output directory name. ' 'chainer_model.prototxt, chainer_model.caffemodel' ' will be created in it') args = parser.parse_args() if not os.path.exists(args.out_dir): print('Created output directory: ' + args.out_dir) os.mkdir(args.out_dir) else: print('Overwriting the existing directory: ' + args.out_dir) if not os.path.isdir(args.out_dir): raise ValueError(args.out_dir + ' exists but not a directory!') print("load model") model, input = get_network_for_imagenet(args.arch) print("convert to caffe model") caffe.export(model, [input], args.out_dir, True)
def test_caffe_export_no_save(self): class Model(chainer.Chain): def __init__(self): super(Model, self).__init__() with self.init_scope(): self.l1 = L.Convolution2D(None, 1, 1, 1, 0) self.b2 = L.BatchNormalization(1) self.l3 = L.Linear(None, 1) def __call__(self, x): h = F.relu(self.l1(x)) h = self.b2(h) return self.l3(h) caffe.export(Model(), [self.x], None, True, 'test')
def test_caffe_export_no_save(self): class Model(chainer.Chain): def __init__(self): super(Model, self).__init__() with self.init_scope(): self.l1 = L.Convolution2D(None, 1, 1, 1, 0) self.b2 = L.BatchNormalization(1) self.l3 = L.Linear(None, 1) def __call__(self, x): h = F.relu(self.l1(x)) h = self.b2(h) return self.l3(h) caffe.export(Model(), [self.x], None, True, 'test')
def export_and_import(l, args, export_params=True, graph_name='test'): """Exports the given Link as Caffe model and returns the re-imported Link. """ with chainer.utils.tempdir() as tempdir: caffe.export(l, args, tempdir, export_params, graph_name) prototxt = os.path.join(tempdir, 'chainer_model.prototxt') caffemodel = os.path.join(tempdir, 'chainer_model.caffemodel') assert os.path.exists(prototxt) assert os.path.exists(caffemodel) # with open(prototxt) as f: print(f.read()) return L.caffe.CaffeFunction(caffemodel)
def export_and_import(l, args, export_params=True, graph_name='test'): """Exports the given Link as Caffe model and returns the re-imported Link. """ with chainer.utils.tempdir() as tempdir: caffe.export(l, args, tempdir, export_params, graph_name) prototxt = os.path.join(tempdir, 'chainer_model.prototxt') caffemodel = os.path.join(tempdir, 'chainer_model.caffemodel') assert os.path.exists(prototxt) assert os.path.exists(caffemodel) # with open(prototxt) as f: print(f.read()) return L.caffe.CaffeFunction(caffemodel)
def main(): for key, value in srcnn.archs.items(): model_dir = '../models/{}'.format(key.lower()) for filename in os.listdir(model_dir): basename, ext = os.path.splitext(filename) onnx_path = os.path.join(model_dir, basename + '.onnx') if ext == '.npz': model_path = os.path.join(model_dir, filename) print(model_path) channels = 3 if 'rgb' in filename else 1 model = value(channels) size = 64 + model.offset data = np.zeros((1, channels, size, size), dtype=np.float32) x = chainer.Variable(data) try: chainer.serializers.load_npz(model_path, model) caffe.export(model, [x], model_dir, True, basename) rename_caffe_model(model_dir, basename) except Exception: print('Skipped caffe model export') onnx_chainer.export(model, x, filename=onnx_path)
def rename_caffe_model(dir, filename): model_path = os.path.join(dir, 'chainer_model.caffemodel') prototxt_path = os.path.join(dir, 'chainer_model.prototxt') new_model_path = os.path.join(dir, filename + '.caffemodel') new_prototxt_path = os.path.join(dir, filename + '.prototxt') os.rename(model_path, new_model_path) os.rename(prototxt_path, new_prototxt_path) if __name__ == '__main__': for key, value in srcnn.archs.items(): model_dir = '../models/{}'.format(key.lower()) for filename in os.listdir(model_dir): basename, ext = os.path.splitext(filename) onnx_path = os.path.join(model_dir, basename + '.onnx') if ext == '.npz': model_path = os.path.join(model_dir, filename) print(model_path) channels = 3 if 'rgb' in filename else 1 model = value(channels) size = 64 + model.offset data = np.zeros((1, channels, size, size), dtype=np.float32) x = chainer.Variable(data) try: chainer.serializers.load_npz(model_path, model) caffe.export(model, [x], model_dir, True, basename) rename_caffe_model(model_dir, basename) except Exception: print('Skipped caffe model export') onnx_chainer.export(model, x, filename=onnx_path)
def test_Add(self): class Link(chainer.Chain): def __call__(self, x): return x + x caffe.export(Link(), [self.x], None, True, 'test')
def test_Softmax(self): class Link(chainer.Chain): def __call__(self, x): return F.softmax(x) caffe.export(Link(), [self.x], None, True, 'test')
def test_MaxPooling2D(self): class Link(chainer.Chain): def __call__(self, x): return F.max_pooling_2d(x, 1, 1, 0) caffe.export(Link(), [self.x], None, True, 'test')
def test_Reshape(self): class Link(chainer.Chain): def __call__(self, x): return F.reshape(x, (-1, )) caffe.export(Link(), [self.x], None, True, 'test')
def test_Add(self): class Link(chainer.Chain): def __call__(self, x): return x + x caffe.export(Link(), [self.x], None, True, 'test')
def test_Softmax(self): class Link(chainer.Chain): def __call__(self, x): return F.softmax(x) caffe.export(Link(), [self.x], None, True, 'test')
def test_MaxPooling2D(self): class Link(chainer.Chain): def __call__(self, x): return F.max_pooling_2d(x, 1, 1, 0) caffe.export(Link(), [self.x], None, True, 'test')