Sorry to bother you.
I change the loss to be focal loss,it trains normally.
However,when i try to evaluate the effect of it, it's so slow and i can't understand why.
This is the code:
def focal_loss(self, onehot_labels, cls_preds,
alpha=0.25, gamma=2.0, name=None, scope=None):
with tf.name_scope(scope, 'focal_loss', [cls_preds, onehot_labels]) as sc:
logits = tf.convert_to_tensor(cls_preds)
onehot_labels = tf.convert_to_tensor(onehot_labels)
precise_logits = tf.cast(logits, tf.float32) if (
logits.dtype == tf.float16) else logits
onehot_labels = tf.cast(onehot_labels, precise_logits.dtype)
predictions = tf.nn.sigmoid(precise_logits)
predictions_pt = tf.where(tf.equal(onehot_labels, 1), predictions, 1. - predictions)
# add small value to avoid 0
epsilon = 1e-8
alpha_t = tf.scalar_mul(alpha, tf.ones_like(onehot_labels, dtype=tf.float32))
alpha_t = tf.where(tf.equal(onehot_labels, 1.0), alpha_t, 1 - alpha_t)
losses = tf.reduce_mean(-alpha_t * tf.pow(1. - predictions_pt, gamma) * tf.log(predictions_pt + epsilon),
name=name)
return losses
def build_loss(self, seg_labels, seg_offsets, link_labels, do_summary=True):
batch_size = config.batch_size_per_gpu
# note that for label values in both seg_labels and link_labels:
# -1 stands for negative
# 1 stands for positive
# 0 stands for ignored
def get_pos_and_neg_masks(labels):
if config.train_with_ignored:
pos_mask = labels >= 0
neg_mask = tf.logical_not(pos_mask)
else:
pos_mask = tf.equal(labels, 1)
neg_mask = tf.equal(labels, -1)
return pos_mask, neg_mask
def OHNM_single_image(scores, n_pos, neg_mask):
"""Online Hard Negative Mining.
scores: the scores of being predicted as negative cls
n_pos: the number of positive samples
neg_mask: mask of negative samples
Return:
the mask of selected negative samples.
if n_pos == 0, no negative samples will be selected.
"""
def has_pos():
n_neg = n_pos * config.max_neg_pos_ratio
max_neg_entries = tf.reduce_sum(tf.cast(neg_mask, tf.int32))
n_neg = tf.minimum(n_neg, max_neg_entries)
n_neg = tf.cast(n_neg, tf.int32)
neg_conf = tf.boolean_mask(scores, neg_mask)
vals, _ = tf.nn.top_k(-neg_conf, k=n_neg)
threshold = vals[-1] # a negtive value
selected_neg_mask = tf.logical_and(neg_mask, scores <= -threshold)
return tf.cast(selected_neg_mask, tf.float32)
def no_pos():
return tf.zeros_like(neg_mask, tf.float32)
return tf.cond(n_pos > 0, has_pos, no_pos)
def OHNM_batch(neg_conf, pos_mask, neg_mask):
selected_neg_mask = []
for image_idx in xrange(batch_size):
image_neg_conf = neg_conf[image_idx, :]
image_neg_mask = neg_mask[image_idx, :]
image_pos_mask = pos_mask[image_idx, :]
n_pos = tf.reduce_sum(tf.cast(image_pos_mask, tf.int32))
selected_neg_mask.append(OHNM_single_image(image_neg_conf, n_pos, image_neg_mask))
selected_neg_mask = tf.stack(selected_neg_mask)
selected_mask = tf.cast(pos_mask, tf.float32) + selected_neg_mask
return selected_mask
# OHNM on segments
seg_neg_scores = self.seg_scores[:, :, 0]
seg_pos_mask, seg_neg_mask = get_pos_and_neg_masks(seg_labels)
seg_selected_mask = OHNM_batch(seg_neg_scores, seg_pos_mask, seg_neg_mask)
n_seg_pos = tf.reduce_sum(tf.cast(seg_pos_mask, tf.float32))
with tf.name_scope('seg_cls_loss'):
def has_pos():
#seg_cls_loss = tf.nn.sparse_softmax_cross_entropy_with_logits(
# logits=self.seg_score_logits,
# labels=tf.cast(seg_pos_mask, dtype=tf.int32))
#return tf.reduce_sum(seg_cls_loss * seg_selected_mask) / n_seg_pos
seg_cls_loss = self.focal_loss(tf.one_hot(seg_labels, 2), self.seg_score_logits)
return seg_cls_loss
def no_pos():
return tf.constant(.0);
seg_cls_loss = tf.cond(n_seg_pos > 0, has_pos, no_pos)
tf.add_to_collection(tf.GraphKeys.LOSSES, seg_cls_loss)
def smooth_l1_loss(pred, target, weights):
diff = pred - target
abs_diff = tf.abs(diff)
abs_diff_lt_1 = tf.less(abs_diff, 1)
if len(target.shape) != len(weights.shape):
loss = tf.reduce_sum(tf.where(abs_diff_lt_1, 0.5 * tf.square(abs_diff), abs_diff - 0.5), axis=2)
return tf.reduce_sum(loss * tf.cast(weights, tf.float32))
else:
loss = tf.where(abs_diff_lt_1, 0.5 * tf.square(abs_diff), abs_diff - 0.5)
return tf.reduce_sum(loss * tf.cast(weights, tf.float32))
with tf.name_scope('seg_loc_loss'):
def has_pos():
seg_loc_loss = smooth_l1_loss(self.seg_offsets, seg_offsets,
seg_pos_mask) * config.seg_loc_loss_weight / n_seg_pos
names = ['loc_cx_loss', 'loc_cy_loss', 'loc_w_loss', 'loc_h_loss', 'loc_theta_loss']
sub_loc_losses = []
from tensorflow.python.ops import control_flow_ops
for idx, name in enumerate(names):
name_loss = smooth_l1_loss(self.seg_offsets[:, :, idx], seg_offsets[:, :, idx],
seg_pos_mask) * config.seg_loc_loss_weight / n_seg_pos
name_loss = tf.identity(name_loss, name=name)
if do_summary:
tf.summary.scalar(name, name_loss)
sub_loc_losses.append(name_loss)
seg_loc_loss = control_flow_ops.with_dependencies(sub_loc_losses, seg_loc_loss)
return seg_loc_loss
def no_pos():
return tf.constant(.0);
seg_loc_loss = tf.cond(n_seg_pos > 0, has_pos, no_pos)
tf.add_to_collection(tf.GraphKeys.LOSSES, seg_loc_loss)
link_neg_scores = self.link_scores[:, :, 0]
link_pos_mask, link_neg_mask = get_pos_and_neg_masks(link_labels)
link_selected_mask = OHNM_batch(link_neg_scores, link_pos_mask, link_neg_mask)
n_link_pos = tf.reduce_sum(tf.cast(link_pos_mask, dtype=tf.float32))
with tf.name_scope('link_cls_loss'):
def has_pos():
#link_cls_loss = tf.nn.sparse_softmax_cross_entropy_with_logits(
# logits=self.link_score_logits,
# labels=tf.cast(link_pos_mask, tf.int32))
#return tf.reduce_sum(link_cls_loss * link_selected_mask) / n_link_pos
link_cls_loss = self.focal_loss(tf.one_hot(link_labels, 2), self.link_score_logits)
return link_cls_loss
def no_pos():
return tf.constant(.0);
link_cls_loss = tf.cond(n_link_pos > 0, has_pos, no_pos) * config.link_cls_loss_weight
tf.add_to_collection(tf.GraphKeys.LOSSES, link_cls_loss)
if do_summary:
tf.summary.scalar('seg_cls_loss', seg_cls_loss)
tf.summary.scalar('seg_loc_loss', seg_loc_loss)
tf.summary.scalar('link_cls_loss', link_cls_loss)
Thanks in advance.
Dear Deng:
I'm sorry I am a newer for TensorFlow framework and get an error of following:
++ set -e
++ export CUDA_VISIBLE_DEVICES=0
++ CUDA_VISIBLE_DEVICES=0
++ CHECKPOINT_PATH=/home/jin/zs/seglink/seglink-512/
++ DATASET_DIR=/home/jin/data/ch4/ch4_test_imgs
++ python test_seglink.py --checkpoint_path=/home/jin/zs/seglink/seglink-512/ --gpu_memory_fraction=-1 --seg_conf_threshold=0.8 --link_conf_threshold=0.5 --dataset_dir=/home/jin/data/ch4/ch4_test_imgs
I tensorflow/stream_executor/dso_loader.cc:111] successfully opened CUDA library libcublas.so locally
I tensorflow/stream_executor/dso_loader.cc:111] successfully opened CUDA library libcudnn.so locally
I tensorflow/stream_executor/dso_loader.cc:111] successfully opened CUDA library libcufft.so locally
I tensorflow/stream_executor/dso_loader.cc:111] successfully opened CUDA library libcuda.so.1 locally
I tensorflow/stream_executor/dso_loader.cc:111] successfully opened CUDA library libcurand.so locally
Traceback (most recent call last):
File "test_seglink.py", line 9, in
from tensorflow.contrib.training.python.training import evaluation
ImportError: cannot import name evaluation
I will appreciate it very much!
Thanks!
Hi. I want to test the pretrained model on my image. when i run'./scripts/test.sh 0 model/model.ckpt-217867 /home/wh/work/jyh_dataset/testimage', i get this error:
Traceback (most recent call last):
File "/usr/lib/python2.7/logging/init.py", line 861, in emit
msg = self.format(record)
File "/usr/lib/python2.7/logging/init.py", line 734, in format
return fmt.format(record)
File "/usr/lib/python2.7/logging/init.py", line 465, in format
record.message = record.getMessage()
File "/usr/lib/python2.7/logging/init.py", line 329, in getMessage
msg = msg % self.args
TypeError: not all arguments converted during string formatting
os: ubuntu 16.04
python:2.7
opencv:3.3.0
tensorflow-gpu 1.4.0
It is me again....
I tried to speed up evaluation by changing eval_seglink.py 's batch_size to 8 instead of 1 , which took me 40 mins to evaluate my own entire dataset of about 14000 images
config.init_config(image_shape,
batch_size = 8,
seg_conf_threshold = FLAGS.seg_conf_threshold,
link_conf_threshold = FLAGS.link_conf_threshold,
train_with_ignored = FLAGS.train_with_ignored,
seg_loc_loss_weight = FLAGS.seg_loc_loss_weight,
link_cls_loss_weight = FLAGS.link_cls_loss_weight,
)
but changing to 8 will report error
ValueError: slice index 1 of dimension 0 out of bounds. for 'evaluation_512x512/strided_slice_4' (op: 'StridedSlice') with input shapes: [1,5460], [2], [2], [2] and with computed input tensors: input[1] = <1 0>, input[2] = <2 0>, input[3] = <1 1>.
What does it mean....? Can only evaluate with batch_size more than one ?
hi,can i test images with no gpu?
Hi, I'm checking your code and thank you for your code.
But I didn't found the negtive link
which was mentioned in the paper. Does the code has not implemented this part?
Thank you.
Hi, dengdan, thank you for your hard work,
i am trying to train seglink model on my own datasets, i meet such situation:
- I only get one GPU card which is TITAN XP, i have rewrite your train scripts,but i get some warnings
- which pretrain model should i prepare for trainning progress? i got imagenet vgg16 checkpoints from SSD-tensorflow project, does this which part of the code should i rewrite to train on this pretrain model?
Thank you again, your work is awesome.
Hi, dengdan, thanks for your repo to help me with the process of my project. I am now training on my own dataset, with your pre-trained 512 weights. Everything goes fine, but I met a problem when doing inference with my trained weights. The bboxes found by my weights are all slightly rotated to a particular orientation. More specifically, all horizontal bboxes are slightly rotated clockwise to various degrees, like 5 degree to 15 degree. This is wired since no bboxes are rotated anti-clockwise. Just before I check the code with care, could you give me a hint of what might be going wrong? Thanks in advance.
I read the code and something make me confused.
In the process of data augmentation,the following function appears.
bboxes_filter_overlap(labels, bboxes,xs, ys, threshold, scope=None, assign_negative = False)
Is the value in bboxes of parameters may be negative?
I am looking forward to your help!
I am proceeding exactly how you describe. However, once I run the code I receive
DataLossError (see above for traceback): Unable to open table file /home.net/vs17dow/Desktop/A_M-arbeit/G_Code/E_seglink/models/model.ckpt-136750.data-00000-of-00001: Data loss: not an sstable (bad magic number): perhaps your file is in a different file format and you need to use a different restore operator?
[[Node: save/RestoreV2_24 = RestoreV2[dtypes=[DT_FLOAT], _device="/job:localhost/replica:0/task:0/cpu:0"](_arg_save/Const_0_0, save/RestoreV2_24/tensor_names, save/RestoreV2_24/shape_and_slices)]]
[[Node: save/RestoreV2_56/_73 = _Recvclient_terminated=false, recv_device="/job:localhost/replica:0/task:0/gpu:0", send_device="/job:localhost/replica:0/task:0/cpu:0", send_device_incarnation=1, tensor_name="edge_260_save/RestoreV2_56", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/gpu:0"]]
./scripts/test.sh 0 ~/Desktop/A_M-arbeit/G_Code/E_seglink/models/seglink/model.ckpt-136750.data-00000-of-00001 ~/Desktop/A_M-arbeit/G_Code/E_seglink/datasets/mydata/
I do not see an error in my command. Any advice how to proceed here?
Best
Valentin
I have successfully convert SynthText and ICDAR2015 datasets to tfrecords but when I try to test_seglink.py I got the following error !
AttributeError: 'NoneType' object has no attribute 'startswith'
能否花点时间写个详细的微调文档或指导
如果成型不胜感激
@dengdan Can you please share your pre-trained models using gdrive or one drive. I can't download models from Baidu.
Hi,
I'm trying to run eval.sh to do offline evaluation since robust reading competition website is down.
I got this message and process stucks there. Any advise?
INFO:tensorflow:Waiting for new checkpoint at /home/rp/code/seglink/seglink-512/model.ckpt-217867
what's the effect when seglink applied to chinese ocr? can it achieve the state-of-the-art effect ?
where can i get the model files?
I run test_seglink.py, and the script meet below error when it run to line "image_bboxes = sess.run([bboxes_pred], feed_dict = {image:image_data, image_shape:image_data.shape})
"
I use tensorflow-gpu (1.2.0) python 2.7
Traceback (most recent call last):
File "", line 1, in
runfile('/home/user/MZH/seglink-master/test_seglink.py', args='--dataset_dir=datasets/ICDAR-Test-Images --checkpoint_path=/home/user/MZH/seglink-master/seglink-384/model.ckpt-136750', wdir='/home/user/MZH/seglink-master')
File "/home/user/anaconda2/lib/python2.7/site-packages/spyder/utils/site/sitecustomize.py", line 880, in runfile
execfile(filename, namespace)
File "/home/user/anaconda2/lib/python2.7/site-packages/spyder/utils/site/sitecustomize.py", line 94, in execfile
builtins.execfile(filename, *where)
File "/home/user/MZH/seglink-master/test_seglink.py", line 164, in
tf.app.run()
File "/home/user/.local/lib/python2.7/site-packages/tensorflow/python/platform/app.py", line 48, in run
_sys.exit(main(_sys.argv[:1] + flags_passthrough))
File "/home/user/MZH/seglink-master/test_seglink.py", line 160, in main
eval()
File "/home/user/MZH/seglink-master/test_seglink.py", line 147, in eval
image_bboxes = sess.run([bboxes_pred], feed_dict = {image:image_data, image_shape:image_data.shape})
File "/home/user/.local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 789, in run
run_metadata_ptr)
File "/home/user/.local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 997, in _run
feed_dict_string, options, run_metadata)
File "/home/user/.local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1132, in _do_run
target_list, options, run_metadata)
File "/home/user/.local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1152, in _do_call
raise type(e)(node_def, op, message)
UnknownError: exceptions.AttributeError: 'module' object has no attribute 'cv'
[[Node: test/PyFunc = PyFunc[Tin=[DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_INT32, DT_FLOAT, DT_FLOAT], Tout=[DT_FLOAT], token="pyfunc_0", _device="/job:localhost/replica:0/task:0/cpu:0"](test/strided_slice_4, test/strided_slice_5, test/strided_slice_2, test/strided_slice_3, test/PyFunc/input_4, test/PyFunc/input_5)]]
Caused by op u'test/PyFunc', defined at:
File "/home/user/anaconda2/lib/python2.7/site-packages/spyder/utils/ipython/start_kernel.py", line 231, in
main()
File "/home/user/anaconda2/lib/python2.7/site-packages/spyder/utils/ipython/start_kernel.py", line 227, in main
kernel.start()
File "/home/user/anaconda2/lib/python2.7/site-packages/ipykernel/kernelapp.py", line 477, in start
ioloop.IOLoop.instance().start()
File "/home/user/anaconda2/lib/python2.7/site-packages/zmq/eventloop/ioloop.py", line 177, in start
super(ZMQIOLoop, self).start()
File "/home/user/anaconda2/lib/python2.7/site-packages/tornado/ioloop.py", line 888, in start
handler_func(fd_obj, events)
File "/home/user/anaconda2/lib/python2.7/site-packages/tornado/stack_context.py", line 277, in null_wrapper
return fn(*args, **kwargs)
File "/home/user/anaconda2/lib/python2.7/site-packages/zmq/eventloop/zmqstream.py", line 440, in _handle_events
self._handle_recv()
File "/home/user/anaconda2/lib/python2.7/site-packages/zmq/eventloop/zmqstream.py", line 472, in _handle_recv
self._run_callback(callback, msg)
File "/home/user/anaconda2/lib/python2.7/site-packages/zmq/eventloop/zmqstream.py", line 414, in _run_callback
callback(*args, **kwargs)
File "/home/user/anaconda2/lib/python2.7/site-packages/tornado/stack_context.py", line 277, in null_wrapper
return fn(*args, **kwargs)
File "/home/user/anaconda2/lib/python2.7/site-packages/ipykernel/kernelbase.py", line 283, in dispatcher
return self.dispatch_shell(stream, msg)
File "/home/user/anaconda2/lib/python2.7/site-packages/ipykernel/kernelbase.py", line 235, in dispatch_shell
handler(stream, idents, msg)
File "/home/user/anaconda2/lib/python2.7/site-packages/ipykernel/kernelbase.py", line 399, in execute_request
user_expressions, allow_stdin)
File "/home/user/anaconda2/lib/python2.7/site-packages/ipykernel/ipkernel.py", line 196, in do_execute
res = shell.run_cell(code, store_history=store_history, silent=silent)
File "/home/user/anaconda2/lib/python2.7/site-packages/ipykernel/zmqshell.py", line 533, in run_cell
return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
File "/home/user/anaconda2/lib/python2.7/site-packages/IPython/core/interactiveshell.py", line 2717, in run_cell
interactivity=interactivity, compiler=compiler, result=result)
File "/home/user/anaconda2/lib/python2.7/site-packages/IPython/core/interactiveshell.py", line 2827, in run_ast_nodes
if self.run_code(code, result):
File "/home/user/anaconda2/lib/python2.7/site-packages/IPython/core/interactiveshell.py", line 2881, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "", line 1, in
runfile('/home/user/MZH/seglink-master/test_seglink.py', args='--dataset_dir=datasets/ICDAR-Test-Images --checkpoint_path=/home/user/MZH/seglink-master/seglink-384/model.ckpt-136750', wdir='/home/user/MZH/seglink-master')
File "/home/user/anaconda2/lib/python2.7/site-packages/spyder/utils/site/sitecustomize.py", line 880, in runfile
execfile(filename, namespace)
File "/home/user/anaconda2/lib/python2.7/site-packages/spyder/utils/site/sitecustomize.py", line 94, in execfile
builtins.execfile(filename, *where)
File "/home/user/MZH/seglink-master/test_seglink.py", line 164, in
tf.app.run()
File "/home/user/.local/lib/python2.7/site-packages/tensorflow/python/platform/app.py", line 48, in run
_sys.exit(main(_sys.argv[:1] + flags_passthrough))
File "/home/user/MZH/seglink-master/test_seglink.py", line 160, in main
eval()
File "/home/user/MZH/seglink-master/test_seglink.py", line 98, in eval
link_conf_threshold = config.link_conf_threshold)
File "tf_extended/seglink.py", line 680, in tf_seglink_to_bbox
tf.float32);
File "/home/user/.local/lib/python2.7/site-packages/tensorflow/python/ops/script_ops.py", line 198, in py_func
input=inp, token=token, Tout=Tout, name=name)
File "/home/user/.local/lib/python2.7/site-packages/tensorflow/python/ops/gen_script_ops.py", line 38, in _py_func
name=name)
File "/home/user/.local/lib/python2.7/site-packages/tensorflow/python/framework/op_def_library.py", line 767, in apply_op
op_def=op_def)
File "/home/user/.local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 2506, in create_op
original_op=self._default_original_op, op_def=op_def)
File "/home/user/.local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1269, in init
self._traceback = _extract_stack()
UnknownError (see above for traceback): exceptions.AttributeError: 'module' object has no attribute 'cv'
[[Node: test/PyFunc = PyFunc[Tin=[DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_INT32, DT_FLOAT, DT_FLOAT], Tout=[DT_FLOAT], token="pyfunc_0", _device="/job:localhost/replica:0/task:0/cpu:0"](test/strided_slice_4, test/strided_slice_5, test/strided_slice_2, test/strided_slice_3, test/PyFunc/input_4, test/PyFunc/input_5)]]
@dengdan Hello! Thanks for your kindly sharing your repo here, now while I trained your model, I find the loss stuck in the range [3.5, 4.5] for a long time, does it normal? my learning rate is 0.001, trained on ICDAR2015.
@dengdan When I set using_moving_average=1, the program report error:
NotFoundError (see above for traceback): Key vgg/conv5/conv5_2/weights/ExponentialMovingAverage not found in checkpoint. How to use this feature in this repo?
How to identify the words inside?
Hi, the package util implement used here is missing in this repo?
It seems that the links for the trained models seglink-384 and seglink-512 are not longer active. Could someone please update these to working links. Thanks
seglink_synthtext 放入的什么文件
seglink_synthtext 放入的什么文件
What files are put in by seglink_synthtext
How meta format files are generated?
xiexie tk;
How do I add src folder to pythonpath?
can you provide caffe model trained using ssd-caffe on coco,thank you
can you provide the pretrained model of tensorflow version, which transformed from vgg_coco_ssd_512✘512_iter_360000.caffemodel。
thanks!
Recommend Projects
-
-
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
An Open Source Machine Learning Framework for Everyone
-
The Web framework for perfectionists with deadlines.
-
A PHP framework for web artisans
-
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
Some thing interesting about web. New door for the world.
-
A server is a program made to process requests and deliver data to clients.
-
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Some thing interesting about visualization, use data art
-
Some thing interesting about game, make everyone happy.
-
Recommend Org
-
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Open source projects and samples from Microsoft.
-
Google ❤️ Open Source for everyone.
-
Alibaba Open Source for everyone
-
Data-Driven Documents codes.
-
China tencent open source team.
-