/
CamemBertForSequenceClassificationTestSpec.scala
170 lines (131 loc) · 6.04 KB
/
CamemBertForSequenceClassificationTestSpec.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
* Copyright 2017-2022 John Snow Labs
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.johnsnowlabs.nlp.annotators.classifier.dl
import com.johnsnowlabs.nlp.annotator._
import com.johnsnowlabs.nlp.base._
import com.johnsnowlabs.nlp.training.CoNLL
import com.johnsnowlabs.nlp.util.io.ResourceHelper
import com.johnsnowlabs.tags.SlowTest
import com.johnsnowlabs.util.Benchmark
import org.apache.spark.ml.{Pipeline, PipelineModel}
import org.apache.spark.sql.functions.{col, explode, size}
import org.scalatest.flatspec.AnyFlatSpec
class CamemBertForSequenceClassificationTestSpec extends AnyFlatSpec {
import ResourceHelper.spark.implicits._
"CamemBertForSequenceClassification" should "correctly load custom model with extracted signatures" taggedAs SlowTest in {
val ddd = Seq(
"Je t'apprécie beaucoup. Je t'aime.",
"Cette banque est très bien, mais elle n'offre pas les services de paiements sans contact.",
"J'aime me promener en forêt même si ça me donne mal aux pieds.",
"Je pensais lire un livre nul, mais finalement je l'ai trouvé super !")
.toDF("text")
val document = new DocumentAssembler()
.setInputCol("text")
.setOutputCol("document")
val tokenizer = new Tokenizer()
.setInputCols(Array("document"))
.setOutputCol("token")
val classifier = CamemBertForSequenceClassification
.pretrained()
.setInputCols(Array("token", "document"))
.setOutputCol("label")
.setCaseSensitive(false)
.setCoalesceSentences(false)
val pipeline = new Pipeline().setStages(Array(document, tokenizer, classifier))
val pipelineModel = pipeline.fit(ddd)
val pipelineDF = pipelineModel.transform(ddd)
pipelineDF.select("label").show(20, truncate = false)
pipelineDF.select("document.result", "label.result").show(20, truncate = false)
pipelineDF
.withColumn("doc_size", size(col("document")))
.withColumn("label_size", size(col("label")))
.where(col("doc_size") =!= col("label_size"))
.select("doc_size", "label_size", "document.result", "label.result")
.show(20, truncate = false)
val totalDocs = pipelineDF.select(explode($"document.result")).count.toInt
val totalLabels = pipelineDF.select(explode($"label.result")).count.toInt
println(s"total tokens: $totalDocs")
println(s"total embeddings: $totalLabels")
assert(totalDocs == totalLabels)
}
"CamemBertForSequenceClassification" should "be saved and loaded correctly" taggedAs SlowTest in {
import ResourceHelper.spark.implicits._
val ddd = Seq(
"John Lenon was born in London and lived in Paris. My name is Sarah and I live in London",
"Rare Hendrix song draft sells for almost $17,000.",
"EU rejects German call to boycott British lamb .",
"TORONTO 1996-08-21").toDF("text")
val document = new DocumentAssembler()
.setInputCol("text")
.setOutputCol("document")
val tokenizer = new Tokenizer()
.setInputCols(Array("document"))
.setOutputCol("token")
val classifier = CamemBertForSequenceClassification
.pretrained()
.setInputCols(Array("token", "document"))
.setOutputCol("label")
.setCaseSensitive(true)
val pipeline = new Pipeline().setStages(Array(document, tokenizer, classifier))
val pipelineModel = pipeline.fit(ddd)
val pipelineDF = pipelineModel.transform(ddd)
pipelineDF.select("label.result").show(false)
Benchmark.time("Time to save CamemBertForSequenceClassification pipeline model") {
pipelineModel.write.overwrite().save("./tmp_forsequence_pipeline")
}
Benchmark.time("Time to save CamemBertForSequenceClassification model") {
pipelineModel.stages.last
.asInstanceOf[CamemBertForSequenceClassification]
.write
.overwrite()
.save("./tmp_forsequence_model")
}
val loadedPipelineModel = PipelineModel.load("./tmp_forsequence_pipeline")
loadedPipelineModel.transform(ddd).select("label.result").show(false)
val loadedSequenceModel = CamemBertForSequenceClassification.load("./tmp_forsequence_model")
println(loadedSequenceModel.getClasses.mkString("Array(", ", ", ")"))
}
"CamemBertForSequenceClassification" should "benchmark test" taggedAs SlowTest in {
val conll = CoNLL()
val training_data =
conll.readDataset(ResourceHelper.spark, "src/test/resources/conll2003/eng.train")
val classifier = CamemBertForSequenceClassification
.pretrained()
.setInputCols(Array("token", "document"))
.setOutputCol("class")
.setCaseSensitive(true)
val pipeline = new Pipeline()
.setStages(Array(classifier))
val pipelineDF = pipeline.fit(training_data).transform(training_data).cache()
Benchmark.time("Time to save pipeline results") {
pipelineDF.write.mode("overwrite").parquet("./tmp_sequence_classifier")
}
pipelineDF.select("label").show(2, false)
pipelineDF.select("document.result", "label.result").show(2, false)
// only works if it's softmax - one label per row
pipelineDF
.withColumn("doc_size", size(col("document")))
.withColumn("label_size", size(col("class")))
.where(col("doc_size") =!= col("label_size"))
.select("doc_size", "label_size", "document.result", "class.result")
.show(20, false)
val totalDocs = pipelineDF.select(explode($"document.result")).count.toInt
val totalLabels = pipelineDF.select(explode($"class.result")).count.toInt
println(s"total docs: $totalDocs")
println(s"total classes: $totalLabels")
assert(totalDocs == totalLabels)
}
}