def save(self, f, buffer_size=10, use_pickle=False): '''Save model parameters using io/snapshot. Args: f: file name buffer_size: size (MB) of the IO, default setting is 10MB; Please make sure it is larger than any single parameter object. use_pickle(Boolean): if true, it would use pickle for dumping; otherwise, it would use protobuf for serialization, which uses less space. ''' if use_pickle: params = {} # since SINGA>=1.1.1 (1101) params['SINGA_VERSION'] = __version__ for (name, val) in zip(self.param_names(), self.param_values()): val.to_host() params[name] = tensor.to_numpy(val) if not f.endswith('.pickle'): f = f + '.pickle' with open(f, 'wb') as fd: pickle.dump(params, fd) else: if f.endswith('.bin'): f = f[0:-4] sp = snapshot.Snapshot(f, True, buffer_size) for (name, val) in zip(self.param_names(), self.param_values()): val.to_host() sp.write(name, val)
def save(self, f): """Save model parameters using cpickle""" params = {} for (specs, val) in zip(self.param_specs(), self.param_values()): val.to_host() params[specs.name] = tensor.to_numpy(val) with open(f, 'wb') as fd: pickle.dump(params, fd)
def forward(self, x, y): '''Compute the precision for each sample. Convert tensor to numpy for computation Args: x (Tensor): predictions, one row per sample y (Tensor): ground truth labels, one row per sample Returns: a tensor of floats, one per sample ''' dev = x.device x.to_host() y.to_host() x_np = tensor.to_numpy(x) y_np = tensor.to_numpy(y) pred_np = np.argsort(-x_np)[:, 0:self.top_k] #Sort in descending order prcs_np = np.zeros(pred_np.shape[0], dtype=np.float32) for i in range(pred_np.shape[0]): #groundtruth labels label_np = np.argwhere(y_np[i]) #Num of common labels among prediction and groundtruth num_intersect = np.intersect1d(pred_np[i], label_np).size prcs_np[i] = num_intersect / float(self.top_k) precision = tensor.from_numpy(prcs_np) x.to_device(dev) y.to_device(dev) precision.to_device(dev) return precision
def save(self, f, buffer_size=10, use_pickle=False): '''Save model parameters using io/snapshot. Args: f: file name buffer_size: size (MB) of the IO, default setting is 10MB; Please make sure it is larger than any single parameter object. use_pickle(Boolean): if true, it would use pickle for dumping; otherwise, it would use protobuf for serialization, which uses less space. ''' if use_pickle: params = {} for (specs, val) in zip(self.param_specs(), self.param_values()): val.to_host() params[specs.name] = tensor.to_numpy(val) with open(f, 'wb') as fd: pickle.dump(params, fd) else: sp = snapshot.Snapshot(f, True, buffer_size) for (specs, val) in zip(self.param_specs(), self.param_values()): val.to_host() sp.write(specs.name, val)
def save(self, f, buffer_size=10, use_pickle=False): """Save model parameters using io/snapshot. Args: f: file name buffer_size: size (MB) of the IO, default setting is 10MB; Please make sure it is larger than any single parameter object. use_pickle(Boolean): if true, it would use pickle for dumping; otherwise, it would use protobuf for serialization, which uses less space. """ if use_pickle: params = {} for (specs, val) in zip(self.param_specs(), self.param_values()): val.to_host() params[specs.name] = tensor.to_numpy(val) with open(f, "wb") as fd: pickle.dump(params, fd) else: sp = snapshot.Snapshot(f, True, buffer_size) for (specs, val) in zip(self.param_specs(), self.param_values()): val.to_host() sp.write(specs.name, val)
def to_onnx_model(inputs, y, model_name='sonnx'): ''' get onnx model from singa computational graph Args: inputs: a list of input tensors (each is initialized with a name) y: a Tensor instance, usually the output of the graph Return: the onnx model ''' node = [] dependency = autograd.infer_dependency(y.creator) ready = deque([y.creator]) def output_name(op, extra=''): return '{}'.format(op.name + str(extra)) input_ids = set(id(x) for x in inputs) X = [] for x in inputs: dtype = TensorProto.FLOAT if y.dtype == tensor.int32: dtype = TensorProto.INT X.append(helper.make_tensor_value_info(x.name, dtype, x.shape)) Y = [ helper.make_tensor_value_info(output_name(y.creator), TensorProto.FLOAT, y.shape) ] while len(ready) > 0: op = ready.pop() assert not isinstance(op, autograd.Dummy) outputs = [output_name(op) for _, idx in op.y_id2idx.items()] if (len(outputs) != 1): outputs = [output_name(op, idx) for _, idx in op.y_id2idx.items()] inputs = [output_name(srcop) for (srcop, yid, _, _) in op.src] curop = str(op).split('.')[-1].split(' ')[0] if isinstance(op, autograd.Concat): node.append( helper.make_node('Concat', inputs=inputs, outputs=outputs, name=op.name, axis=op.axis)) elif isinstance(op, autograd._Conv2d): pads = [ op.handle.pad_h, op.handle.pad_h, op.handle.pad_h, op.handle.pad_h ] stride = [op.handle.stride_h, op.handle.stride_w] k = [op.handle.kernel_h, op.handle.kernel_w] node.append( helper.make_node('Conv', inputs=inputs, outputs=outputs, name=op.name, kernel_shape=k, pads=pads, strides=stride)) # TODO groups elif isinstance(op, autograd._Pooling2d): k = [op.handle.kernel_h, op.handle.kernel_w] s = [op.handle.stride_h, op.handle.stride_w] p = [ op.handle.pad_h, op.handle.pad_h, op.handle.pad_w, op.handle.pad_w ] if (op.handle.is_max_pooling): node.append( helper.make_node('MaxPool', inputs=inputs, outputs=outputs, name=op.name, kernel_shape=k, pads=p, strides=s)) else: node.append( helper.make_node('AveragePool', inputs=inputs, outputs=outputs, name=op.name, kernel_shape=k, pads=p, strides=s)) elif (isinstance(op, autograd._BatchNorm2d)): dummy0 = tensor.to_numpy( tensor.Tensor(device=op.running_mean.device(), data=op.running_mean)) dummy1 = tensor.to_numpy( tensor.Tensor(device=op.running_var.device(), data=op.running_var)) node.append( helper.make_node('BatchNormalization', inputs=inputs, outputs=outputs, name=op.name, momentum=op.handle.factor)) dummy0 = helper.make_node('Constant', inputs=[], outputs=[inputs[3]], value=numpy_helper.from_array(dummy0)) dummy1 = helper.make_node('Constant', inputs=[], outputs=[inputs[4]], value=numpy_helper.from_array(dummy1)) node.append(dummy0) node.append(dummy1) else: singa2onnx = { 'SoftMax': 'Softmax', 'AddBias': 'Add', 'Add': 'Add', 'Matmul': 'MatMul', 'ReLU': 'Relu', 'ElemMatmul': 'Mul', 'Flatten': 'Flatten', 'Tanh': 'Tanh' } if (curop in singa2onnx): onnx_op = singa2onnx[curop] else: onnx_op = curop node.append( helper.make_node(onnx_op, inputs=inputs, outputs=outputs, name=op.name)) for srcop, yid, y, _ in op.src: dependency[srcop] -= 1 if dependency[srcop] == 0: if isinstance(srcop, autograd.Dummy): if yid not in input_ids: tmp = helper.make_node( 'Constant', inputs=[], outputs=[output_name(srcop)], value=helper.make_tensor( name=op.name, data_type=TensorProto.FLOAT, dims=y.shape, vals=tensor.to_numpy(y).flatten().astype( float))) node.append(tmp) else: ready.append(srcop) onnx_model = helper.make_model( helper.make_graph(node[::-1], model_name, X, Y)) checker.check_model(onnx_model) return onnx_model