异常解决StanfordCoreNLP:cococaption包出现找不到edu.stanford.nlp.semgraph.semgrex.SemgrexPattern错误
问题:
你可能复现别人代码的时候(NLP领域)出现以下问题:
- 找不到xxx文件,NoClassDefFoundError类
Exception in thread "main" java.lang.NoClassDefFoundError: edu/stanford/nlp/semgraph/semgrex/SemgrexPattern
at edu.anu.spice.SpiceParser.<clinit>(SpiceParser.java:64)
at edu.anu.spice.SpiceScorer.scoreBatch(SpiceScorer.java:70)
at edu.anu.spice.SpiceScorer.main(SpiceScorer.java:60)
Caused by: java.lang.ClassNotFoundException: edu.stanford.nlp.semgraph.semgrex.SemgrexPattern
at java.net.URLClassLoader.findClass(URLClassLoader.java:387)
at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:355)
at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
- 直接报磁盘空间不足,或者最后报[‘jar’, ‘javac’ …cache xx out xx] returned non-zero exit status 1 错误
Threads( StanfordCoreNLP ) [0.667 seconds]
Error: Could not score batched file input:
org.fusesource.lmdbjni.LMDBException: 磁盘空间不足。
at org.fusesource.lmdbjni.Util.checkErrorCode(Util.java:44)
at org.fusesource.lmdbjni.Env.open(Env.java:192)
at org.fusesource.lmdbjni.Env.open(Env.java:72)
at org.fusesource.lmdbjni.Env.open(Env.java:65)
at edu.anu.spice.LmdbTupleDB.putTransaction(LmdbTupleDB.java:69)
at edu.anu.spice.SpiceParser.loadTuplesFromDB(SpiceParser.java:216)
at edu.anu.spice.SpiceParser.loadTuples(SpiceParser.java:245)
at edu.anu.spice.SpiceParser.parseCaptions(SpiceParser.java:251)
at edu.anu.spice.SpiceScorer.scoreBatch(SpiceScorer.java:109)
at edu.anu.spice.SpiceScorer.main(SpiceScorer.java:60)
Traceback (most recent call last):
File "h:/thrid_paper/github_code/my_image_captioning_CNN/image_captioning_to_cnn/eval_demo.py", line 53, in <module>
scorer.compute_scores()
File "h:/thrid_paper/github_code/my_image_captioning_CNN/image_captioning_to_cnn/eval_demo.py", line 29, in compute_scores
score, scores = scorer.compute_score(self.gt, self.ref)
File "h:\thrid_paper\github_code\my_image_captioning_CNN\image_captioning_to_cnn\coco_caption\pycocoevalcap\spice\spice.py", line 75, in compute_score
subprocess.check_call(spice_cmd,
File "H:\thrid_paper\github_code\image_caption\image_captioning_env\lib\subprocess.py", line 364, in check_call
raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command '['java', '-jar', '-Xmx8G', 'spice-1.0.jar', 'h:\\thrid_paper\\github_code\\my_image_captioning_CNN\\image_captioning_to_cnn\\coco_caption\\pycocoevalcap\\spice\\tmp\\tmpm4frve7c', '-cache', 'h:\\thrid_paper\\github_code\\my_image_captioning_CNN\\image_captioning_to_cnn\\coco_caption\\pycocoevalcap\\spice\\cache', '-out', 'h:\\thrid_paper\\github_code\\my_image_captioning_CNN\\image_captioning_to_cnn\\coco_caption\\pycocoevalcap\\spice\\tmp\\tmph6xire8e', '-subset', '-silent']' returned non-zero exit status 1.
edu.stanford.nlp.semgraph.semgrex.SemgrexPattern错误
https://blog.csdn.net/chenzhiwen1998/article/details/125984810
这篇文章说的其实是ok的,但是下面这句话,bash命令明显是linux的
执行coco-caption项目目录下的get_stanford_models.sh文件即可。
所以,但是他说的是对的
#!/usr/bin/env sh
# This script downloads the Stanford CoreNLP models.
CORENLP=stanford-corenlp-full-2015-12-09
SPICELIB=pycocoevalcap/spice/lib
JAR=stanford-corenlp-3.6.0
DIR="$( cd "$(dirname "$0")" ; pwd -P )"
cd $DIR
if [ -f $SPICELIB/$JAR.jar ]; then
echo "Found Stanford CoreNLP."
else
echo "Downloading..."
wget http://nlp.stanford.edu/software/$CORENLP.zip
echo "Unzipping..."
unzip $CORENLP.zip -d $SPICELIB/
mv $SPICELIB/$CORENLP/$JAR.jar $SPICELIB/
mv $SPICELIB/$CORENLP/$JAR-models.jar $SPICELIB/
rm -f $CORENLP.zip
rm -rf $SPICELIB/$CORENLP/
echo "Done."
fi
注意,CORENLP=stanford-corenlp-full-2015-12-09,就是我们缺少的东西,那么能不能在们当前目录下找到呢?实际上,这个包已经存在我们本地了。
在报错的下方找到scipy.py打开,右键,文件路径,显示如下:
点到nlgeval,你会惊讶地发现 CORENLP=stanford-corenlp-full-2015-12-09 怎么在这?
把这个文件 解压缩 到 nlgeval\pycocoevalcap\spice\lib
目录下,就是这个目录找到pycocoevalcap往子目录找就行了。
问题解决。
了吗?
cache 错误
这时候 运行 发现还有错误,这时候该这位大神登场了
https://blog.csdn.net/qq_40837795/article/details/130362191
我就不赘述了,他说的都对,跟着做就行了,我只是改了
subprocess.py 将shell=False更改为:shell=True
spice.py 注释掉 : # ‘-cache’, cache_dir,
总结
讲道理,其实忙活一大圈,就是StanfordCoreNL没导进去,cache需要注释掉就能成功解决!