def parse_instance(default_module, name, instance, self_instance=None, parse_shape=False) -> "Object": from elichika.parser import values_builtin if values_builtin.is_builtin_chainer_link(instance): return Object( values_builtin.ChainerLinkInstance(default_module, instance)) # need to check whether is value bool before check whether is value int if isinstance(instance, bool): return Object(BoolValue(instance)) if isinstance(instance, int): return Object(NumberValue(instance)) if isinstance(instance, np.int32): return Object(NumberValue(instance)) if isinstance(instance, np.int64): return Object(NumberValue(instance)) if isinstance(instance, float): return Object(NumberValue(instance)) if isinstance(instance, np.float32): return Object(NumberValue(instance)) if isinstance(instance, np.float64): return Object(NumberValue(instance)) if isinstance(instance, str): return Object(StrValue(instance)) if isinstance(instance, list): if parse_shape: return Object(ListValue()) else: print('List is not supported now!!!') return Object(NumberValue(0.0)) if instance is inspect._empty: return None if inspect.isfunction(instance): func = UserDefinedFunction(instance) return Object(FuncValue(func, self_instance)) if inspect.ismethod(instance): func = UserDefinedFunction(instance) return Object(FuncValue(func, self_instance)) if inspect.isclass(instance): func = functions.UserDefinedClassConstructorFunction(instance) return Object(FuncValue(func, None)) if isinstance(instance, tuple) and 'Undefined' in instance: shape = list(instance) shape = -1 if shape == 'Undefined' else shape tensorValue = TensorValue() tensorValue.shape = tuple(shape) return Object(tensorValue) if isinstance(instance, np.ndarray): tensorValue = TensorValue() tensorValue.value = instance tensorValue.shape = instance.shape return Object(tensorValue) if instance == inspect._empty: return Object(NoneValue()) if instance is None: return Object(NoneValue()) model_inst = UserDefinedInstance(default_module, instance, None, isinstance(instance, chainer.Link)) return Object(model_inst)
def parse_instance(default_module, name, instance, self_instance=None): from elichika.parser import values_builtin if values_builtin.is_builtin_chainer_link(instance): return values_builtin.ChainerLinkInstance(default_module, instance) # need to check whether is value bool before check whether is value int if isinstance(instance, bool): return BoolValue(instance) if isinstance(instance, int) or isinstance(instance, float): return NumberValue(instance) if isinstance(instance, str): return StrValue(instance) if isinstance(instance, list): ret = ListValue() ind = 0 for e in instance: element_value = parse_instance(default_module, '', e) ret.get_field().get_attribute(str(ind)).revise(element_value) ind += 1 return ret if instance is inspect._empty: return None if inspect.isfunction(instance): func = UserDefinedFunction(instance) return FuncValue(func, self_instance) if inspect.ismethod(instance): func = UserDefinedFunction(instance) return FuncValue(func, self_instance) if inspect.isclass(instance): func = functions.UserDefinedClassConstructorFunction(instance) return FuncValue(func, None) if isinstance(instance, tuple) and 'Undefined' in instance: shape = list(instance) shape = -1 if shape == 'Undefined' else shape tensorValue = TensorValue() tensorValue.shape = tuple(shape) return tensorValue if isinstance(instance, np.ndarray): tensorValue = TensorValue() tensorValue.value = instance tensorValue.shape = instance.shape return tensorValue if instance == inspect._empty: return NoneValue() if instance is None: return NoneValue() model_inst = UserDefinedInstance(default_module, instance, None, isinstance(instance, chainer.Link)) return model_inst
def parse_instance(default_module, name, instance, self_instance=None, parse_shape=False, from_member = False) -> "ValueRef": for converter in instance_converters: ret = converter(default_module, instance) if ret is not None: return ValueRef(ret) if inspect.ismethod(instance) or inspect.isfunction(instance): if instance in function_converters.keys(): func = function_converters[instance] return ValueRef(func) # need to check whether is value bool before check whether is value int if isinstance(instance, bool): return ValueRef(BoolValue(instance)) if isinstance(instance, int): return ValueRef(NumberValue(instance)) if isinstance(instance, np.int32): return ValueRef(NumberValue(instance)) if isinstance(instance, np.int64): return ValueRef(NumberValue(instance)) if isinstance(instance, float): return ValueRef(NumberValue(instance)) if isinstance(instance, np.float32): return ValueRef(NumberValue(instance)) if isinstance(instance, np.float64): return ValueRef(NumberValue(instance)) if isinstance(instance, str): return ValueRef(StrValue(instance)) if isinstance(instance, list): if parse_shape: return ValueRef(ListValue()) else: print('List is not supported now!!!') return ValueRef(NumberValue(0.0)) if instance is inspect._empty: return None if inspect.ismethod(instance): func = UserDefinedFunction(instance) return ValueRef(FuncValue(func, self_instance)) if inspect.isfunction(instance): func = UserDefinedFunction(instance) if from_member: return ValueRef(FuncValue(func, self_instance)) else: return ValueRef(FuncValue(func, None)) if inspect.isclass(instance): func = functions.UserDefinedClassConstructorFunction(instance) return ValueRef(FuncValue(func, None)) if isinstance(instance, tuple) and 'Undefined' in instance: shape = list(instance) shape = -1 if shape == 'Undefined' else shape tensorValue = TensorValue() tensorValue.shape = tuple(shape) return ValueRef(tensorValue) if isinstance(instance, tuple): value_in_tuple = [] for v in instance: o = parse_instance(default_module, '', v) value_in_tuple.append(o) return ValueRef(TupleValue(value_in_tuple)) if isinstance(instance, np.ndarray): tensorValue = TensorValue(instance) tensorValue.value = instance tensorValue.shape = instance.shape return ValueRef(tensorValue) if instance == inspect._empty: return ValueRef(NoneValue()) if instance is None: return ValueRef(NoneValue()) model_inst = UserDefinedInstance( default_module, instance, None, isinstance(instance, chainer.Link)) return ValueRef(model_inst)
def parse_instance(default_module, name, instance, self_instance=None, from_member = False, root_graph : 'graphs.Graph' = None) -> "ValueRef": for converter in instance_converters: ret = converter(default_module, instance) if ret is not None: return ValueRef(ret) if inspect.ismethod(instance) or inspect.isfunction(instance): if instance in function_converters.keys(): func = function_converters[instance] return ValueRef(func) # need to check whether is value bool before check whether is value int if isinstance(instance, bool): return ValueRef(BoolValue(instance)) if isinstance(instance, int): return ValueRef(NumberValue(instance)) if isinstance(instance, np.int32): return ValueRef(NumberValue(instance)) if isinstance(instance, np.int64): return ValueRef(NumberValue(instance)) if isinstance(instance, float): return ValueRef(NumberValue(instance)) if isinstance(instance, np.float32): return ValueRef(NumberValue(instance)) if isinstance(instance, np.float64): return ValueRef(NumberValue(instance)) if isinstance(instance, str): return ValueRef(StrValue(instance)) if instance is inspect._empty: return None if inspect.ismethod(instance): func = UserDefinedFunction(instance) return ValueRef(FuncValue(func, self_instance)) if inspect.isfunction(instance): func = UserDefinedFunction(instance) if from_member: return ValueRef(FuncValue(func, self_instance)) else: return ValueRef(FuncValue(func, None)) if inspect.isclass(instance): func = functions.UserDefinedClassConstructorFunction(instance) return ValueRef(FuncValue(func, None)) if isinstance(instance, list): if root_graph is None: value_in_tuple = [] for v in instance: o = parse_instance(default_module, '', v) value_in_tuple.append(o) ret = ListValue(value_in_tuple) else: value_in_tuple = [] vs = [] for v in instance: o = parse_instance(default_module, '', v) value_in_tuple.append(o) value = o.get_value() if isinstance(value, TupleValue): assert(False) if isinstance(value, ListValue): assert(False) vs.append(value) node = nodes.NodeGenerate('List', vs) ret = ListValue(value_in_tuple) node.set_outputs([ret]) root_graph.add_initial_node(node) ret.estimate_type() return ValueRef(ret) if isinstance(instance, tuple) and 'Undefined' in instance: shape = list(instance) shape = -1 if shape == 'Undefined' else shape tensorValue = TensorValue() tensorValue.shape = tuple(shape) return ValueRef(tensorValue) if isinstance(instance, tuple): if root_graph is None: value_in_tuple = [] for v in instance: o = parse_instance(default_module, '', v) value_in_tuple.append(o) return ValueRef(TupleValue(value_in_tuple)) else: value_in_tuple = [] vs = [] for v in instance: o = parse_instance(default_module, '', v) value_in_tuple.append(o) value = o.get_value() if isinstance(value, TupleValue): assert(False) if isinstance(value, ListValue): assert(False) vs.append(value) node = nodes.NodeGenerate('Tuple', vs) ret = TupleValue(value_in_tuple) node.set_outputs([ret]) root_graph.add_initial_node(node) return ValueRef(ret) if isinstance(instance, np.ndarray): tensorValue = TensorValue(instance) tensorValue.value = instance tensorValue.shape = instance.shape return ValueRef(tensorValue) if instance == inspect._empty: return ValueRef(NoneValue()) if instance is None: return ValueRef(NoneValue()) model_inst = UserDefinedInstance(default_module, instance, None) return ValueRef(model_inst)