I wrote a python class, but when using it, it can not find any of the functions inside. I tested it on windows and MAC, it does not work both. Here is the some of the code of the class I defined:(I here is not the full code as it it too many codes and it's not allowed to copy here)
import numpy as np
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
import matplotlib.pyplot as plt
class VanillaInfoGAN(object):
def __init__(self,learning_rate,batch_size,num_epoches,z_dim,c_cat_dim,c_cont_dim,model_num,print_every=30,show_every=50,
store_path = './ckpt/',training_picture_path='./pictures/'):
self.learning_rate = learning_rate
self.batch_size = batch_size
self.num_epoches = num_epoches
self.z_dim = z_dim
self.c_cat_dim = c_cat_dim
self.c_cont_dim = c_cont_dim
self.print_every = print_every
self.show_every = show_every
self.data =input_data.read_data_sets('MNIST',one_hot=True)
self.data_dim = 784
self.store_path = store_path
self.model_num = model_num
self.training_picture_path = training_picture_path
self.real_data_placeholder = tf.placeholder(tf.float32,[None,self.data_dim])
self.z_placeholder = tf.placeholder(tf.float32,[None,self.z_dim])
self.c_cat_placeholder = tf.placeholder(tf.float32,[None,self.c_cat_dim])
self.c_cont_placeholder = tf.placeholder(tf.float32,[None,self.c_cont_dim])
self.c = tf.concat([self.c_cat_placeholder,self.c_cont_placeholder],axis=1)
self.z_c = tf.concat([self.z_placeholder,self.c_cat_placeholder,self.c_cont_placeholder],axis=1)
self.g_out = self.generator()
d_out_real = self.discriminator(self.real_data_placeholder)
d_out_fake = self.discriminator(self.g_out,reuse=True)
q_out = self.q_net(self.g_out)
self.g_loss,self.d_loss,self.q_loss = self.build_loss(self.g_out,d_out_real,d_out_fake,q_out)
self.g_opt,self.d_opt,self.q_opt = self.optimizer(self.g_loss,self.d_loss,self.q_loss)
self.saver= tf.train.Saver()
print('Model graph has built')
def generator(self,reuse=False):
with tf.variable_scope('generator',reuse=reuse):
layer = tf.layers.dense(self.z_c,128,activation = tf.nn.relu)
layer = tf.layers.dense(layer,self.data_dim,activation = tf.nn.sigmoid)
return layer
def discriminator(self,d_input,reuse=False):
with tf.variable_scope('discriminator',reuse=reuse):
layer = tf.layers.dense(d_input,128,activation = tf.nn.relu)
layer = tf.layers.dense(layer,1,activation = tf.nn.sigmoid)
return layer
def q_net(self,g_out,reuse=False):
with tf.variable_scope('Q',reuse=reuse):
layer = tf.layers.dense(g_out,128,activation = tf.nn.relu)
layer = tf.layers.dense(layer,self.c_cat_dim+self.c_cont_dim,activation = None)
layer_cat = tf.nn.softmax(layer[:,:self.c_cat_dim])
layer_cont =tf.nn.sogmoid(layer[:,self.c_cat_dim:])
q_out = tf.concat([layer_cat,layer_cont],axis=1)
return q_out
Here is the running warpper I used to create the object:
from VanillaInfoGAN import VanillaInfoGAN
gan = VanillaInfoGAN(learning_rate = learning_rate,batch_size=batch_size,num_epoches=num_epoches,z_dim=z_dim,c_cat_dim=c_cat_dim,c_cont_dim=c_cont_dim,
model_num=model_num,print_every=print_every,show_every=show_every)
I got error:
File "<ipython-input-2-5252a711a145>", line 1, in <module>
runfile('/Users/shiyanpei/Documents/embeddings/infogan/test/run.py', wdir='/Users/shiyanpei/Documents/embeddings/infogan/test')
File "/Users/shiyanpei/Applications/anaconda3/lib/python3.6/site-packages/spyder/utils/site/sitecustomize.py", line 692, in runfile
execfile(filename, namespace)
File "/Users/shiyanpei/Applications/anaconda3/lib/python3.6/site-packages/spyder/utils/site/sitecustomize.py", line 101, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "/Users/shiyanpei/Documents/embeddings/infogan/test/run.py", line 28, in <module>
model_num=model_num,print_every=print_every,show_every=show_every)
File "/Users/shiyanpei/Documents/embeddings/infogan/test/VanillaInfoGAN.py", line 40, in __init__
self.g_out = self.generator()
AttributeError: 'VanillaInfoGAN' object has no attribute 'generator'
I have wrote many other classes but I have not seen this error yet
Can Anybody help?
Thanks!!
Your indentation is wrong:
class VanillaInfoGAN(object):
def __init__(self,learning_rate,batch_size,num_epoches,z_dim,c_cat_dim,c_cont_dim,model_num,print_every=30,show_every=50,
store_path = './ckpt/',training_picture_path='./pictures/'):
# a lot of lines skipped
self.g_out = self.generator()
# a lot of lines skipped
def generator(self,reuse=False):
with tf.variable_scope('generator',reuse=reuse):
Python code is executed from top to bottom. def generator(self,reuse=False): is not even executed when you call self.generator. Make generator a property of the class instead by outdenting it:
class VanillaInfoGAN(object):
def __init__(self,learning_rate,batch_size,num_epoches,z_dim,c_cat_dim,c_cont_dim,model_num,print_every=30,show_every=50,
store_path = './ckpt/',training_picture_path='./pictures/'):
# a lot of line skipped
self.g_out = self.generator()
# a lot of line skipped
def generator(self,reuse=False):
with tf.variable_scope('generator',reuse=reuse):
While you're at it, also strongly consider to split the code into multiple classes and methods; its excessive complexity makes it very easy to commit mistakes.
Related
I have a pytorch text multiclass classifier model based on a XLMR based architecture. Due to IP reasons I can't share the architecture code. I have tried putting as much detail as I can. Please point out if more information is needed. But it outputs 28 classes from 'class0' to 'class27' with probability scores that add to 1.
I am trying to use shap package to explain the results. I have wrapped my model into huggingface's custom pipeline object and I get the following output for 1 input text:
pipe = CustomPipeline(model = model, tokenizer = base_tokenizer)
output = pipe(list_of_inputs) # list_of_inputs = ['this is test input']
Output:
[[{"label": "class0","score": 0.01500235591083765},{"label": "class1","score": 0.001698049483820796},{"label": "class2","score": 0.0019644589629024267},{"label": "class3","score": 0.0004418794414959848},{"label": "class4","score": 5.9095666074426845e-05},{"label": "class5","score": 0.0007908751722425222},{"label": "class6","score": 0.002379569923505187},{"label": "class7","score": 0.0035733324475586414},{"label": "class8","score": 0.0014360857894644141},{"label": "class9","score": 0.0007365105557255447},{"label": "class10","score": 0.0014471099711954594},{"label": "class11","score": 0.0011013210751116276},{"label": "class12","score": 0.0010048456024378538},{"label": "class13","score": 0.000885132874827832},{"label": "class14","score": 0.0022015925496816635},{"label": "class15","score": 0.0013197452062740922},{"label": "class16","score": 0.0037292027845978737},{"label": "class17","score": 0.004212632775306702},{"label": "class18","score": 0.9481304287910461},{"label": "class19","score": 0.001469381619244814},{"label": "class20","score": 0.0009713817853480577},{"label": "class21","score": 0.0018773127812892199},{"label": "class22","score": 0.0009251375449821353},{"label": "class23","score": 0.0007248060428537428},{"label": "class24","score": 0.00031718137324787676},{"label": "class25","score": 0.0011144360760226846},{"label": "class26","score": 0.0002294857840752229},{"label": "class27","score": 0.00025681318948045373}]]
The output is in same format as the notebook specified by shap package.
Now, when I try to use shap Explainer:
pipe = UDTMPipeline(model = model, tokenizer = base_tokenizer)
explainer = shap.Explainer(pipe)
shap_values = explainer(list_of_inputs)
shap.plots.text(shap_values)
Error:
File "C:\pipeline.py", line 104, in udtm_xai
shap_values = explainer(query_data)
File "C:\Users\Miniconda3\envs\dtlr_udtm\lib\site-packages\shap\explainers\_partition.py", line 136, in __call__
return super().__call__(
File "C:\Users\Miniconda3\envs\dtlr_udtm\lib\site-packages\shap\explainers\_explainer.py", line 266, in __call__
row_result = self.explain_row(
File "C:\Users\Miniconda3\envs\dtlr_udtm\lib\site-packages\shap\explainers\_partition.py", line 161, in explain_row
self._curr_base_value = fm(m00.reshape(1, -1), zero_index=0)[0] # the zero index param tells the masked model what the baseline is
File "C:\Users\Miniconda3\envs\dtlr_udtm\lib\site-packages\shap\utils\_masked_model.py", line 67, in __call__
return self._full_masking_call(masks, batch_size=batch_size)
File "C:\Users\Miniconda3\envs\dtlr_udtm\lib\site-packages\shap\utils\_masked_model.py", line 144, in _full_masking_call
outputs = self.model(*joined_masked_inputs)
File "C:\Users\Miniconda3\envs\dtlr_udtm\lib\site-packages\shap\models\_transformers_pipeline.py", line 35, in __call__
output[i, self.label2id[obj["label"]]] = sp.special.logit(obj["score"]) if self.rescale_to_logits else obj["score"]
KeyError: 'class0'
The code is unable to find 'class0'. In the postprocess function of pipeline class, I read a file containing label mappings, obtain the softmax scores from _forward function of pipeline class and create a dictionary in the final format to send as output:
class CustomPipeline(Pipeline): # Ignore no indentation below, formatting issue in stackoverflow
def _sanitize_parameters(self, **kwargs):
self.mapping_json = json.loads(open("mapping_file.json", "r", encoding = "utf-8").read().strip())
return {}, {}, {}
def preprocess(self, inputs):
inputs_df = pd.DataFrame([inputs], columns = ["Query"])
inference_dataloader = getInferenceDataloader(inputs_df, self.tokenizer, batch_size = 16)
return inference_dataloader
def softmax_with_temp(self, input, t):
ex = torch.exp(input/t)
sum = torch.sum(ex,0)
return ex / sum
def _forward(self, model_inputs):
if torch.cuda.is_available():
device = torch.device('cuda')
else:
device = torch.device('cpu')
final_pred_labels = []
final_scores = []
batch_count = 0
self.model.eval()
for batch in model_inputs:
batch_count += 1
b_input_ids = batch[0].to(device)
b_input_mask = batch[1].to(device)
b_input_task = torch.full((b_input_ids.shape[0],), -1, dtype=torch.int32).to(device)
with torch.no_grad():
result = self.model((b_input_ids, b_input_mask, b_input_task))
logits = result
logits = logits.detach().cpu().numpy()
pred_softmax_t = self.softmax_with_temp(torch.from_numpy(logits[0]), 2).numpy()
return pred_softmax_t
def postprocess(self, model_outputs):
output_list = [{"label":"class0", "score":float(model_outputs[0])}]
index = 1
for label in self.mapping_json.keys(): #self.mapping_json contains label names that has been read from a file
output_list.append({"label":label, "score":float(model_outputs[index])}) #model_outputs is a list of 28 floating scores
index += 1
return output_list
Am I missing some to define any label based variable which is why I am getting 'class0' key error?
Error converting pytorch model to coreml.
How do I convert a model with two inputs?
This model is style transfer.
var' was not implemented in the model, so it was searched and added. And an error occurred in 'var'.
var
#register_torch_op()
def var(context, node):
inputs = _get_inputs(context, node, expected=4)
x = inputs[0]
axes = inputs[1].val
print(inputs)
# Assert we can have biased divisor (N).
# An unbiased divisor (N - 1) would be much more complex,
# since we can't use reduce_mean. We therefore would need
# to otherwise do manual computation of the divisor for each axis.
assert (inputs[2].val == False)
keepdim = inputs[3].val
x_mean = mb.reduce_mean(x = x, axes = axes, keep_dims=keepdim)
x_sub_mean = mb.sub(x = x, y = x_mean)
x_sub_mean_square = mb.square(x = x_sub_mean)
x_var = mb.reduce_mean(x = x_sub_mean_square, axes = axes, keep_dims=keepdim)
context.add(x_var, torch_name=node.name)
code
import coremltools as ct
import urllib
import torch
import torch.nn as nn
import torchvision
from PIL import Image
from torchvision import transforms
from model import Model, AdaIN
class ConvertModel(nn.Module):
def __init__(self):
super(ConvertModel, self).__init__()
self.model = Model()
self.model.decoder.state_dict(torch.load('./models/decoder_3.pth', map_location='cpu'))
self.model.encoder
def forward(self, content, style, alpha=0.6):
content_f = self.model.encoder(content)
style_f = self.model.encoder(style)
feat = AdaIN(content_f, style_f)
feat = feat * alpha + content_f * (1 - alpha)
return self.model.decoder(feat)
model = ConvertModel()
dummy_input = torch.rand(1, 3, 512, 512)
traced_model = torch.jit.trace(model, (dummy_input, dummy_input))
input_content = ct.ImageType(name='content', shape=(1, 3, 512, 512),
bias=[-0.485/0.229, -0.456/0.224, -0.406/0.225], scale=1./(0.226*255.0))
input_style = ct.ImageType(name='style', shape=(1, 3, 512, 512),
bias=[-0.485/0.229, -0.456/0.224, -0.406/0.225], scale=1./(0.226*255.0))
coreml_model = ct.convert(traced_model, inputs=[input_content, input_style])
spec = coreml_model.get_spec()
ct.utils.rename_feature(spec, "current", "output")
coreml_model_updated = ct.models.MLModel(spec)
coreml_model_updated.save('style_transfer.mlmodel')
log
WARNING:root:Torch version 1.11.0 has not been tested with coremltools. You may run into unexpected errors. Torch 1.10.2 is the most recent version that has been tested.
Converting Frontend ==> MIL Ops: 54%|█████▎ | 197/367 [00:00<00:00, 2564.34 ops/s]
Traceback (most recent call last):
File "/Users/sinmugyeol/deeplearning/generator_model/style_transfer/convert2coreml.py", line 36, in <module>
coreml_model = ct.convert(traced_model, inputs=[input_content, input_style])
File "/Users/sinmugyeol/opt/anaconda3/envs/core/lib/python3.10/site-packages/coremltools/converters/_converters_entry.py", line 352, in convert
mlmodel = mil_convert(
File "/Users/sinmugyeol/opt/anaconda3/envs/core/lib/python3.10/site-packages/coremltools/converters/mil/converter.py", line 183, in mil_convert
return _mil_convert(model, convert_from, convert_to, ConverterRegistry, MLModel, compute_units, **kwargs)
File "/Users/sinmugyeol/opt/anaconda3/envs/core/lib/python3.10/site-packages/coremltools/converters/mil/converter.py", line 210, in _mil_convert
proto, mil_program = mil_convert_to_proto(
File "/Users/sinmugyeol/opt/anaconda3/envs/core/lib/python3.10/site-packages/coremltools/converters/mil/converter.py", line 273, in mil_convert_to_proto
prog = frontend_converter(model, **kwargs)
File "/Users/sinmugyeol/opt/anaconda3/envs/core/lib/python3.10/site-packages/coremltools/converters/mil/converter.py", line 105, in __call__
return load(*args, **kwargs)
File "/Users/sinmugyeol/opt/anaconda3/envs/core/lib/python3.10/site-packages/coremltools/converters/mil/frontend/torch/load.py", line 47, in load
return _perform_torch_convert(converter, debug)
File "/Users/sinmugyeol/opt/anaconda3/envs/core/lib/python3.10/site-packages/coremltools/converters/mil/frontend/torch/load.py", line 84, in _perform_torch_convert
prog = converter.convert()
File "/Users/sinmugyeol/opt/anaconda3/envs/core/lib/python3.10/site-packages/coremltools/converters/mil/frontend/torch/converter.py", line 250, in convert
convert_nodes(self.context, self.graph)
File "/Users/sinmugyeol/opt/anaconda3/envs/core/lib/python3.10/site-packages/coremltools/converters/mil/frontend/torch/ops.py", line 89, in convert_nodes
add_op(context, node)
File "/Users/sinmugyeol/opt/anaconda3/envs/core/lib/python3.10/site-packages/coremltools/converters/mil/frontend/torch/ops.py", line 256, in var
assert (inputs[2].val == False)
AssertionError
macOS Monterey 12.3.1 M1.
python 3.10.4.
pytorch 1.11.0.
coremltools 5.2.0.
numpy 1.21.5.
I am trying to run the following code. The following code runs fine on google colab, however on my system it throws an error. Tensorflow version installed on my system is is 1.12.0 and keras version is 2.2.4. Help is highly appreciated.
def profiler(layer, test_input):
data_input = test_input
start = time.time()
data_input = layer.predict(data_input)
end = time.time() - start
milliseconds = end * 1000
return milliseconds
def dense_layer(input_dim, dense_size):
x = tf.keras.layers.Input((input_dim))
dense = tf.keras.layers.Dense(dense_size)(x)
model = tf.keras.models.Model(inputs=x, outputs=dense)
return model
def process_config(config):
tokens = config.split(",")
values = []
for token in tokens:
token = token.strip()
if token.find("-") == -1:
token = int(token)
values.append(token)
else:
start,end = token.split("-")
start = int(start.strip())
end = int(end.strip())
values = values + list(range(start,end+1))
return values
def evaluate_dense(input_shapes_range, dense_size_range):
for input_shape in input_shapes_range:
for dense_size in dense_size_range:
to_write = open("dense_data.csv", "a+")
model = dense_layer(input_shape, dense_size)
random_input = np.random.randn(1, input_shape)
running_time = profiler(model, random_input)
del model
input_size = "2000"
dense_size = "1000, 4096"
input_size_range = process_config(input_size)
dense_size_range = process_config(dense_size)
evaluate_dense(input_size_range, dense_size_range)
Error trace
File "C:/Users/Dense-layer.py", line 59, in <module>
evaluate_dense(input_size_range, dense_size_range)
File "C:/Users/Dense-layer.py", line 44, in evaluate_dense
model = dense_layer(input_shape, dense_size)
File "C:/Users/Dense-layer.py", line 16, in dense_layer
x = tf.keras.layers.Input((input_dim))
File "C:\Users\learn\miniconda3\envs\tensorflow\lib\site-packages\tensorflow\python\keras\engine\input_layer.py", line 229, in Input
input_tensor=tensor)
File "C:\Users\learn\miniconda3\envs\tensorflow\lib\site-packages\tensorflow\python\keras\engine\input_layer.py", line 91, in __init__
batch_input_shape = (batch_size,) + tuple(input_shape)
TypeError: 'int' object is not iterable
input_shape should be a tuple, but input_dim is an integer. You have passed input_dim, and since you have not specified it by name, it considers it as input_shape. So, just specify it by name:
tf.keras.layers.Input(input_dim=input_dim)
Or if you want to specify the shape, use it like:
tf.keras.layers.Input((input_dim,))
I have raised this issue in GitHub: https://github.com/tensorflow/tensorflow/issues/46917
I am trying to use multiprocessing threads to speedup the some of my code. In which I have to send a Keras model to each thread and use it to predict on some inputs and do some following computations. However, I end up with the following error
Tensflow Keras: TypeError: can't pickle _thread.RLock objects
I tried,
using partial to fix the model argument and use the resulting partial-function.
cloning the model and using a clone for each thread
saving and reloading a model for each thread
tried using pathos.multiprocessing
but none of them worked.
The following is the MWE
import tensorflow as tf
from tensorflow import keras
import numpy as np
from multiprocessing import Pool
# from multiprocessing.dummy import Pool as ThreadPool
# from pathos.multiprocessing import ProcessingPool as Pool
from functools import partial
def simple_model():
model = keras.models.Sequential([
keras.layers.Dense(units = 10, input_shape = [1]),
keras.layers.Dense(units = 1, activation = 'sigmoid')
])
model.compile(optimizer = 'sgd', loss = 'mean_squared_error')
return model
def clone_model(model):
model_clone = tf.keras.models.clone_model(model)
model_clone.set_weights(model.get_weights())
model_clone.build((None, 1))
model_clone.compile(optimizer = 'sgd', loss = 'mean_squared_error')
return model_clone
def work(model, seq):
return model.predict(seq)
def load_model(model_savepath):
return tf.keras.models.load_model(model_savepath)
def worker(model, n = 4):
seqences = np.arange(0,100).reshape(n, -1)
pool = Pool()
model_savepath = './simple_model.h5'
model.save(model_savepath)
model_list = [load_model(model_savepath) for _ in range(n)]
# model_list = [clone_model(model) for _ in range(n)]
results = pool.map(work, zip(model_list,seqences))
# partial_work = partial(work, model=model)
# results = pool.map(partial_work, seqences)
pool.close()
pool.join()
return np.reshape(results, (-1, ))
if __name__ == '__main__':
model = simple_model()
out = worker(model, n=4)
print(out)
This results in the following error trace:
File "c:/Users/***/Documents/GitHub/COVID-NSF/test4.py", line 42, in <module>
out = worker(model, n=4)
File "c:/Users/****/Documents/GitHub/COVID-NSF/test4.py", line 30, in worker
results = pool.map(work, zip(model_list,seqences))
File "C:\Users\****\anaconda3\envs\tf-gpu\lib\multiprocessing\pool.py", line 268, in map
return self._map_async(func, iterable, mapstar, chunksize).get()
File "C:\Users\****\anaconda3\envs\tf-gpu\lib\multiprocessing\pool.py", line 657, in get
raise self._value
File "C:\Users\***\anaconda3\envs\tf-gpu\lib\multiprocessing\pool.py", line 431, in _handle_tasks
put(task)
File "C:\Users\***\anaconda3\envs\tf-gpu\lib\multiprocessing\connection.py", line 206, in send
self._send_bytes(_ForkingPickler.dumps(obj))
File "C:\Users\***\anaconda3\envs\tf-gpu\lib\multiprocessing\reduction.py", line 51, in dumps
cls(buf, protocol).dump(obj)
TypeError: can't pickle _thread.RLock objects
#Aaron thanks for explaining the comment made by amahendrakar on GitHub. I modified the code such that the code sends the path of the model, rather than the model itself, to the child processes. Below is the working code
import tensorflow as tf
from tensorflow import keras
import numpy as np
# from multiprocessing import Pool
from multiprocessing.dummy import Pool as ThreadPool
from pathos.multiprocessing import ProcessingPool as Pool
from functools import partial
import time
def simple_model():
model = keras.models.Sequential([
keras.layers.Dense(units = 10, input_shape = [1]),
keras.layers.Dense(units = 1, activation = 'sigmoid')
])
model.compile(optimizer = 'sgd', loss = 'mean_squared_error')
return model
def clone_model(model):
model_clone = tf.keras.models.clone_model(model)
model_clone.set_weights(model.get_weights())
model_clone.build((None, 1))
model_clone.compile(optimizer = 'sgd', loss = 'mean_squared_error')
return model_clone
def work(model, seq):
return model.predict(seq)
def work_new(seq):
model_savepath = './simple_model.h5'
model = tf.keras.models.load_model(model_savepath)
return model.predict(seq)
def load_model(model_savepath):
return tf.keras.models.load_model(model_savepath)
def worker(model, n = 4):
seqences = np.arange(0,10*n).reshape(n, -1)
pool = Pool()
model_savepath = './simple_model.h5'
model.save(model_savepath)
# model_list = [load_model(model_savepath) for _ in range(n)]
# model_list = [clone_model(model) for _ in range(n)]
# results = pool.map(work, zip(model_list,seqences))
# path_list = [[model_savepath] for _ in range(n)]
# print(np.shape(path_list), np.shape(seqences))
# work_new_partial = partial(work_new, path=model_savepath)
results = pool.map(work_new, seqences)
# partial_work = partial(work, model=model)
# results = pool.map(partial_work, seqences)
pool.close()
pool.join()
# print(t1-t0)
return np.reshape(results, (-1, ))
if __name__ == '__main__':
model = simple_model()
t0 = time.perf_counter()
out = worker(model, n=40)
t1 = time.perf_counter()
# print(out)
print(f"time taken {t1 - t0}")
This results in
time taken 8.521342800000001
I'm trying to train a network using NiftyNet with my own data (CT images and their corresponding labels). I designed the Net class shortly following some other training with similar sample data, all NiftyNet documentation I could find and parameters of my own data adjusted. But I keep getting this error:
"TypeError: init() got an unexpected keyword argument 'w_initializer'".
I've tried every change I could think of in my config.ini, Net class, etc. But I can't make it work nor find the reason. Can anyone help with this error? Or maybe share some guidelines to train my own network from the beginning so I can at least try to start an alternative from zero and see if I find a way out?
Training command:
! net_segment train -c /home/niftynet/extensions/dense_vnet_TC/config.ini --name dense_vnet_TC.net_TC.MyNet
Some values in config.ini:
[NETWORK]
name = dense_vnet
batch_size = 6
volume_padding_size = 0
window_sampling = resize
[TRAINING]
sample_per_volume = 1
lr = 0.001
loss_type = dense_vnet_TC.dice_hinge.dice
starting_iter = 0
save_every_n = 1000
max_iter = 3001
[INFERENCE]
border = (0, 0, 0)
inference_iter = 3000
output_interp_order = 0
spatial_window_size = (512, 512, 40)
save_seg_dir = ./segmentation_output/
############################ Custom configuration
[SEGMENTATION]
image = ct
label = label
label_normalisation = False
output_prob = False
num_classes = 2
Basics of Net class:
from niftynet.network.base_net import BaseNet
class MyNet(BaseNet):
def __init__(self, num_classes, name='MyNet'):
super(MyNet, self).__init__(num_classes=num_classes, acti_func=acti_func, name=name)
# network specific property
self.hidden_features = 10
def layer_op(self, images, is_training):
# create layer instances
conv_1 = ConvolutionalLayer(self.hidden_features, kernel_size=3, name='conv_input')
conv_2 = ConvolutionalLayer(self.num_classes, kernel_size=1, acti_func=None, name='conv_output')
# apply layer instances
flow = conv_1(images, is_training)
flow = conv_2(flow, is_training)
return flow
End of output, after doing some of the processing as expected:
Traceback (most recent call last): File
"/home/niftynet/bin/net_segment", line 10, in
sys.exit(main()) File "/home/niftynet/lib/python3.6/site- packages/niftynet/init.py",
line 142, in main
app_driver.run(app_driver.app) File "/home/niftynet/lib/python3.6/site-packages/niftynet/engine/application_driver.py",
line 189, in run
is_training_action=self.is_training_action) File "/home/niftynet/lib/python3.6/site- packages/niftynet/engine/application_driver.py",
line 258, in create_graph
application.initialise_network() File "/home/niftynet/lib/python3.6/site-packages/niftynet/application/segmentation_application.py",
line 280, in initialise_network
acti_func=self.net_param.activation_function) TypeError: init() got an unexpected keyword argument 'w_initializer'
I think you need to change this line (based on a similar problem I had):
super(MyNet, self).__init__(num_classes=num_classes, acti_func=acti_func, name=name)
for (just add w_regularizer) :
super(MyNet, self).__init__(num_classes=num_classes, w_regularizer=w_regularizer, acti_func=acti_func, name=name)
if not try also to add it here :
def __init__(self, num_classes, w_regularizer=w_regularizer, name='MyNet'):
I hope it helps.