spark-nlp 4.2.6__py2.py3-none-any.whl → 6.2.1__py2.py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- com/johnsnowlabs/ml/__init__.py +0 -0
- com/johnsnowlabs/ml/ai/__init__.py +10 -0
- spark_nlp-6.2.1.dist-info/METADATA +362 -0
- spark_nlp-6.2.1.dist-info/RECORD +292 -0
- {spark_nlp-4.2.6.dist-info → spark_nlp-6.2.1.dist-info}/WHEEL +1 -1
- sparknlp/__init__.py +81 -28
- sparknlp/annotation.py +3 -2
- sparknlp/annotator/__init__.py +6 -0
- sparknlp/annotator/audio/__init__.py +2 -0
- sparknlp/annotator/audio/hubert_for_ctc.py +188 -0
- sparknlp/annotator/audio/wav2vec2_for_ctc.py +14 -14
- sparknlp/annotator/audio/whisper_for_ctc.py +251 -0
- sparknlp/{base → annotator}/chunk2_doc.py +4 -7
- sparknlp/annotator/chunker.py +1 -2
- sparknlp/annotator/classifier_dl/__init__.py +17 -0
- sparknlp/annotator/classifier_dl/albert_for_multiple_choice.py +161 -0
- sparknlp/annotator/classifier_dl/albert_for_question_answering.py +3 -15
- sparknlp/annotator/classifier_dl/albert_for_sequence_classification.py +4 -18
- sparknlp/annotator/classifier_dl/albert_for_token_classification.py +3 -17
- sparknlp/annotator/classifier_dl/albert_for_zero_shot_classification.py +211 -0
- sparknlp/annotator/classifier_dl/bart_for_zero_shot_classification.py +225 -0
- sparknlp/annotator/classifier_dl/bert_for_multiple_choice.py +161 -0
- sparknlp/annotator/classifier_dl/bert_for_question_answering.py +6 -20
- sparknlp/annotator/classifier_dl/bert_for_sequence_classification.py +3 -17
- sparknlp/annotator/classifier_dl/bert_for_token_classification.py +3 -17
- sparknlp/annotator/classifier_dl/bert_for_zero_shot_classification.py +212 -0
- sparknlp/annotator/classifier_dl/camembert_for_question_answering.py +168 -0
- sparknlp/annotator/classifier_dl/camembert_for_sequence_classification.py +5 -19
- sparknlp/annotator/classifier_dl/camembert_for_token_classification.py +5 -19
- sparknlp/annotator/classifier_dl/camembert_for_zero_shot_classification.py +202 -0
- sparknlp/annotator/classifier_dl/classifier_dl.py +4 -4
- sparknlp/annotator/classifier_dl/deberta_for_question_answering.py +3 -17
- sparknlp/annotator/classifier_dl/deberta_for_sequence_classification.py +4 -19
- sparknlp/annotator/classifier_dl/deberta_for_token_classification.py +5 -21
- sparknlp/annotator/classifier_dl/deberta_for_zero_shot_classification.py +193 -0
- sparknlp/annotator/classifier_dl/distil_bert_for_question_answering.py +3 -17
- sparknlp/annotator/classifier_dl/distil_bert_for_sequence_classification.py +4 -18
- sparknlp/annotator/classifier_dl/distil_bert_for_token_classification.py +3 -17
- sparknlp/annotator/classifier_dl/distil_bert_for_zero_shot_classification.py +211 -0
- sparknlp/annotator/classifier_dl/distilbert_for_multiple_choice.py +161 -0
- sparknlp/annotator/classifier_dl/longformer_for_question_answering.py +3 -17
- sparknlp/annotator/classifier_dl/longformer_for_sequence_classification.py +4 -18
- sparknlp/annotator/classifier_dl/longformer_for_token_classification.py +3 -17
- sparknlp/annotator/classifier_dl/mpnet_for_question_answering.py +148 -0
- sparknlp/annotator/classifier_dl/mpnet_for_sequence_classification.py +188 -0
- sparknlp/annotator/classifier_dl/mpnet_for_token_classification.py +173 -0
- sparknlp/annotator/classifier_dl/multi_classifier_dl.py +3 -3
- sparknlp/annotator/classifier_dl/roberta_for_multiple_choice.py +161 -0
- sparknlp/annotator/classifier_dl/roberta_for_question_answering.py +3 -17
- sparknlp/annotator/classifier_dl/roberta_for_sequence_classification.py +4 -18
- sparknlp/annotator/classifier_dl/roberta_for_token_classification.py +1 -1
- sparknlp/annotator/classifier_dl/roberta_for_zero_shot_classification.py +225 -0
- sparknlp/annotator/classifier_dl/sentiment_dl.py +4 -4
- sparknlp/annotator/classifier_dl/tapas_for_question_answering.py +2 -2
- sparknlp/annotator/classifier_dl/xlm_roberta_for_multiple_choice.py +149 -0
- sparknlp/annotator/classifier_dl/xlm_roberta_for_question_answering.py +3 -17
- sparknlp/annotator/classifier_dl/xlm_roberta_for_sequence_classification.py +4 -18
- sparknlp/annotator/classifier_dl/xlm_roberta_for_token_classification.py +6 -20
- sparknlp/annotator/classifier_dl/xlm_roberta_for_zero_shot_classification.py +225 -0
- sparknlp/annotator/classifier_dl/xlnet_for_sequence_classification.py +4 -18
- sparknlp/annotator/classifier_dl/xlnet_for_token_classification.py +3 -17
- sparknlp/annotator/cleaners/__init__.py +15 -0
- sparknlp/annotator/cleaners/cleaner.py +202 -0
- sparknlp/annotator/cleaners/extractor.py +191 -0
- sparknlp/annotator/coref/spanbert_coref.py +4 -18
- sparknlp/annotator/cv/__init__.py +15 -0
- sparknlp/annotator/cv/blip_for_question_answering.py +172 -0
- sparknlp/annotator/cv/clip_for_zero_shot_classification.py +193 -0
- sparknlp/annotator/cv/convnext_for_image_classification.py +269 -0
- sparknlp/annotator/cv/florence2_transformer.py +180 -0
- sparknlp/annotator/cv/gemma3_for_multimodal.py +346 -0
- sparknlp/annotator/cv/internvl_for_multimodal.py +280 -0
- sparknlp/annotator/cv/janus_for_multimodal.py +351 -0
- sparknlp/annotator/cv/llava_for_multimodal.py +328 -0
- sparknlp/annotator/cv/mllama_for_multimodal.py +340 -0
- sparknlp/annotator/cv/paligemma_for_multimodal.py +308 -0
- sparknlp/annotator/cv/phi3_vision_for_multimodal.py +328 -0
- sparknlp/annotator/cv/qwen2vl_transformer.py +332 -0
- sparknlp/annotator/cv/smolvlm_transformer.py +426 -0
- sparknlp/annotator/cv/swin_for_image_classification.py +242 -0
- sparknlp/annotator/cv/vision_encoder_decoder_for_image_captioning.py +240 -0
- sparknlp/annotator/cv/vit_for_image_classification.py +36 -4
- sparknlp/annotator/dataframe_optimizer.py +216 -0
- sparknlp/annotator/date2_chunk.py +88 -0
- sparknlp/annotator/dependency/dependency_parser.py +2 -3
- sparknlp/annotator/dependency/typed_dependency_parser.py +3 -4
- sparknlp/annotator/document_character_text_splitter.py +228 -0
- sparknlp/annotator/document_normalizer.py +37 -1
- sparknlp/annotator/document_token_splitter.py +175 -0
- sparknlp/annotator/document_token_splitter_test.py +85 -0
- sparknlp/annotator/embeddings/__init__.py +11 -0
- sparknlp/annotator/embeddings/albert_embeddings.py +4 -18
- sparknlp/annotator/embeddings/auto_gguf_embeddings.py +539 -0
- sparknlp/annotator/embeddings/bert_embeddings.py +9 -22
- sparknlp/annotator/embeddings/bert_sentence_embeddings.py +12 -24
- sparknlp/annotator/embeddings/bge_embeddings.py +199 -0
- sparknlp/annotator/embeddings/camembert_embeddings.py +4 -20
- sparknlp/annotator/embeddings/chunk_embeddings.py +1 -2
- sparknlp/annotator/embeddings/deberta_embeddings.py +2 -16
- sparknlp/annotator/embeddings/distil_bert_embeddings.py +5 -19
- sparknlp/annotator/embeddings/doc2vec.py +7 -1
- sparknlp/annotator/embeddings/e5_embeddings.py +195 -0
- sparknlp/annotator/embeddings/e5v_embeddings.py +138 -0
- sparknlp/annotator/embeddings/elmo_embeddings.py +2 -2
- sparknlp/annotator/embeddings/instructor_embeddings.py +204 -0
- sparknlp/annotator/embeddings/longformer_embeddings.py +3 -17
- sparknlp/annotator/embeddings/minilm_embeddings.py +189 -0
- sparknlp/annotator/embeddings/mpnet_embeddings.py +192 -0
- sparknlp/annotator/embeddings/mxbai_embeddings.py +184 -0
- sparknlp/annotator/embeddings/nomic_embeddings.py +181 -0
- sparknlp/annotator/embeddings/roberta_embeddings.py +9 -21
- sparknlp/annotator/embeddings/roberta_sentence_embeddings.py +7 -21
- sparknlp/annotator/embeddings/sentence_embeddings.py +2 -3
- sparknlp/annotator/embeddings/snowflake_embeddings.py +202 -0
- sparknlp/annotator/embeddings/uae_embeddings.py +211 -0
- sparknlp/annotator/embeddings/universal_sentence_encoder.py +3 -3
- sparknlp/annotator/embeddings/word2vec.py +7 -1
- sparknlp/annotator/embeddings/word_embeddings.py +4 -5
- sparknlp/annotator/embeddings/xlm_roberta_embeddings.py +9 -21
- sparknlp/annotator/embeddings/xlm_roberta_sentence_embeddings.py +7 -21
- sparknlp/annotator/embeddings/xlnet_embeddings.py +4 -18
- sparknlp/annotator/er/entity_ruler.py +37 -23
- sparknlp/annotator/keyword_extraction/yake_keyword_extraction.py +2 -3
- sparknlp/annotator/ld_dl/language_detector_dl.py +2 -2
- sparknlp/annotator/lemmatizer.py +3 -4
- sparknlp/annotator/matcher/date_matcher.py +35 -3
- sparknlp/annotator/matcher/multi_date_matcher.py +1 -2
- sparknlp/annotator/matcher/regex_matcher.py +3 -3
- sparknlp/annotator/matcher/text_matcher.py +2 -3
- sparknlp/annotator/n_gram_generator.py +1 -2
- sparknlp/annotator/ner/__init__.py +3 -1
- sparknlp/annotator/ner/ner_converter.py +18 -0
- sparknlp/annotator/ner/ner_crf.py +4 -5
- sparknlp/annotator/ner/ner_dl.py +10 -5
- sparknlp/annotator/ner/ner_dl_graph_checker.py +293 -0
- sparknlp/annotator/ner/ner_overwriter.py +2 -2
- sparknlp/annotator/ner/zero_shot_ner_model.py +173 -0
- sparknlp/annotator/normalizer.py +2 -2
- sparknlp/annotator/openai/__init__.py +16 -0
- sparknlp/annotator/openai/openai_completion.py +349 -0
- sparknlp/annotator/openai/openai_embeddings.py +106 -0
- sparknlp/annotator/pos/perceptron.py +6 -7
- sparknlp/annotator/sentence/sentence_detector.py +2 -2
- sparknlp/annotator/sentence/sentence_detector_dl.py +3 -3
- sparknlp/annotator/sentiment/sentiment_detector.py +4 -5
- sparknlp/annotator/sentiment/vivekn_sentiment.py +4 -5
- sparknlp/annotator/seq2seq/__init__.py +17 -0
- sparknlp/annotator/seq2seq/auto_gguf_model.py +304 -0
- sparknlp/annotator/seq2seq/auto_gguf_reranker.py +334 -0
- sparknlp/annotator/seq2seq/auto_gguf_vision_model.py +336 -0
- sparknlp/annotator/seq2seq/bart_transformer.py +420 -0
- sparknlp/annotator/seq2seq/cohere_transformer.py +357 -0
- sparknlp/annotator/seq2seq/cpm_transformer.py +321 -0
- sparknlp/annotator/seq2seq/gpt2_transformer.py +1 -1
- sparknlp/annotator/seq2seq/llama2_transformer.py +343 -0
- sparknlp/annotator/seq2seq/llama3_transformer.py +381 -0
- sparknlp/annotator/seq2seq/m2m100_transformer.py +392 -0
- sparknlp/annotator/seq2seq/marian_transformer.py +124 -3
- sparknlp/annotator/seq2seq/mistral_transformer.py +348 -0
- sparknlp/annotator/seq2seq/nllb_transformer.py +420 -0
- sparknlp/annotator/seq2seq/olmo_transformer.py +326 -0
- sparknlp/annotator/seq2seq/phi2_transformer.py +326 -0
- sparknlp/annotator/seq2seq/phi3_transformer.py +330 -0
- sparknlp/annotator/seq2seq/phi4_transformer.py +387 -0
- sparknlp/annotator/seq2seq/qwen_transformer.py +340 -0
- sparknlp/annotator/seq2seq/starcoder_transformer.py +335 -0
- sparknlp/annotator/seq2seq/t5_transformer.py +54 -4
- sparknlp/annotator/similarity/__init__.py +0 -0
- sparknlp/annotator/similarity/document_similarity_ranker.py +379 -0
- sparknlp/annotator/spell_check/context_spell_checker.py +116 -17
- sparknlp/annotator/spell_check/norvig_sweeting.py +3 -6
- sparknlp/annotator/spell_check/symmetric_delete.py +1 -1
- sparknlp/annotator/stemmer.py +2 -3
- sparknlp/annotator/stop_words_cleaner.py +3 -4
- sparknlp/annotator/tf_ner_dl_graph_builder.py +1 -1
- sparknlp/annotator/token/__init__.py +0 -1
- sparknlp/annotator/token/recursive_tokenizer.py +2 -3
- sparknlp/annotator/token/tokenizer.py +2 -3
- sparknlp/annotator/ws/word_segmenter.py +35 -10
- sparknlp/base/__init__.py +2 -3
- sparknlp/base/doc2_chunk.py +0 -3
- sparknlp/base/document_assembler.py +5 -5
- sparknlp/base/embeddings_finisher.py +14 -2
- sparknlp/base/finisher.py +15 -4
- sparknlp/base/gguf_ranking_finisher.py +234 -0
- sparknlp/base/image_assembler.py +69 -0
- sparknlp/base/light_pipeline.py +53 -21
- sparknlp/base/multi_document_assembler.py +9 -13
- sparknlp/base/prompt_assembler.py +207 -0
- sparknlp/base/token_assembler.py +1 -2
- sparknlp/common/__init__.py +2 -0
- sparknlp/common/annotator_type.py +1 -0
- sparknlp/common/completion_post_processing.py +37 -0
- sparknlp/common/match_strategy.py +33 -0
- sparknlp/common/properties.py +914 -9
- sparknlp/internal/__init__.py +841 -116
- sparknlp/internal/annotator_java_ml.py +1 -1
- sparknlp/internal/annotator_transformer.py +3 -0
- sparknlp/logging/comet.py +2 -2
- sparknlp/partition/__init__.py +16 -0
- sparknlp/partition/partition.py +244 -0
- sparknlp/partition/partition_properties.py +902 -0
- sparknlp/partition/partition_transformer.py +200 -0
- sparknlp/pretrained/pretrained_pipeline.py +1 -1
- sparknlp/pretrained/resource_downloader.py +126 -2
- sparknlp/reader/__init__.py +15 -0
- sparknlp/reader/enums.py +19 -0
- sparknlp/reader/pdf_to_text.py +190 -0
- sparknlp/reader/reader2doc.py +124 -0
- sparknlp/reader/reader2image.py +136 -0
- sparknlp/reader/reader2table.py +44 -0
- sparknlp/reader/reader_assembler.py +159 -0
- sparknlp/reader/sparknlp_reader.py +461 -0
- sparknlp/training/__init__.py +1 -0
- sparknlp/training/conll.py +8 -2
- sparknlp/training/spacy_to_annotation.py +57 -0
- sparknlp/util.py +26 -0
- spark_nlp-4.2.6.dist-info/METADATA +0 -1256
- spark_nlp-4.2.6.dist-info/RECORD +0 -196
- {spark_nlp-4.2.6.dist-info → spark_nlp-6.2.1.dist-info}/top_level.txt +0 -0
- /sparknlp/annotator/{token/token2_chunk.py → token2_chunk.py} +0 -0
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
# Copyright 2017-2022 John Snow Labs
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
|
|
15
|
+
"""Contains classes concerning ConvNextForImageClassification."""
|
|
16
|
+
|
|
17
|
+
from sparknlp.common import *
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class ConvNextForImageClassification(AnnotatorModel,
|
|
21
|
+
HasBatchedAnnotateImage,
|
|
22
|
+
HasImageFeatureProperties,
|
|
23
|
+
HasEngine):
|
|
24
|
+
"""ConvNextForImageClassification is an image classifier based on ConvNet models.
|
|
25
|
+
|
|
26
|
+
The ConvNeXT model was proposed in A ConvNet for the 2020s by Zhuang Liu, Hanzi Mao, Chao-Yuan
|
|
27
|
+
Wu, Christoph Feichtenhofer, Trevor Darrell, Saining Xie. ConvNeXT is a pure convolutional
|
|
28
|
+
model (ConvNet), inspired by the design of Vision Transformers, that claims to outperform
|
|
29
|
+
them.
|
|
30
|
+
|
|
31
|
+
For available pretrained models please see the
|
|
32
|
+
`Models Hub <https://sparknlp.org/models?task=Image+Classification>`__.
|
|
33
|
+
|
|
34
|
+
Models from the HuggingFace 🤗 Transformers library are also compatible with Spark
|
|
35
|
+
NLP 🚀. To see which models are compatible and how to import them see
|
|
36
|
+
https://github.com/JohnSnowLabs/spark-nlp/discussions/5669 and to see more extended
|
|
37
|
+
examples, see
|
|
38
|
+
`ConvNextForImageClassificationTestSpec <https://github.com/JohnSnowLabs/spark-nlp/blob/master/src/test/scala/com/johnsnowlabs/nlp/annotators/cv/ConvNextForImageClassificationTestSpec.scala>`__.
|
|
39
|
+
|
|
40
|
+
====================== ======================
|
|
41
|
+
Input Annotation types Output Annotation type
|
|
42
|
+
====================== ======================
|
|
43
|
+
``IMAGE`` ``CATEGORY``
|
|
44
|
+
====================== ======================
|
|
45
|
+
|
|
46
|
+
**Paper Abstract:**
|
|
47
|
+
|
|
48
|
+
*The "Roaring 20s" of visual recognition began with the introduction of Vision Transformers (ViTs), which quickly
|
|
49
|
+
superseded ConvNets as the state-of-the-art image classification model. A vanilla ViT, on the other hand, faces
|
|
50
|
+
difficulties when applied to general computer vision tasks such as object detection and semantic segmentation. It is
|
|
51
|
+
the hierarchical Transformers (e.g., Swin Transformers) that reintroduced several ConvNet priors, making
|
|
52
|
+
Transformers practically viable as a generic vision backbone and demonstrating remarkable performance on a wide
|
|
53
|
+
variety of vision tasks. However, the effectiveness of such hybrid approaches is still largely credited to the
|
|
54
|
+
intrinsic superiority of Transformers, rather than the inherent inductive biases of convolutions. In this work, we
|
|
55
|
+
reexamine the design spaces and test the limits of what a pure ConvNet can achieve. We gradually "modernize" a
|
|
56
|
+
standard ResNet toward the design of a vision Transformer, and discover several key components that contribute to
|
|
57
|
+
the performance difference along the way. The outcome of this exploration is a family of pure ConvNet models dubbed
|
|
58
|
+
ConvNeXt. Constructed entirely from standard ConvNet modules, ConvNeXts compete favorably with Transformers in terms
|
|
59
|
+
of accuracy and scalability, achieving 87.8% ImageNet top-1 accuracy and outperforming Swin Transformers on COCO
|
|
60
|
+
detection and ADE20K segmentation, while maintaining the simplicity and efficiency of standard ConvNets. *
|
|
61
|
+
|
|
62
|
+
References
|
|
63
|
+
----------
|
|
64
|
+
|
|
65
|
+
`A ConvNet for the 2020s <https://arxiv.org/abs/2201.03545>`__
|
|
66
|
+
|
|
67
|
+
Parameters
|
|
68
|
+
----------
|
|
69
|
+
doResize
|
|
70
|
+
Whether to resize the input to a certain size
|
|
71
|
+
doNormalize
|
|
72
|
+
Whether to normalize the input with mean and standard deviation
|
|
73
|
+
featureExtractorType
|
|
74
|
+
Name of model's architecture for feature extraction
|
|
75
|
+
imageMean
|
|
76
|
+
The sequence of means for each channel, to be used when normalizing images
|
|
77
|
+
imageStd
|
|
78
|
+
The sequence of standard deviations for each channel, to be used when normalizing images
|
|
79
|
+
resample
|
|
80
|
+
An optional resampling filter. This can be one of `PIL.Image.NEAREST`, `PIL.Image.BILINEAR` or
|
|
81
|
+
`PIL.Image.BICUBIC`. Only has an effect if do_resize is set to True.
|
|
82
|
+
size
|
|
83
|
+
Resize the input to the given size. If a tuple is provided, it should be (width, height). If only an integer is
|
|
84
|
+
provided, then the input will be resized to (size, size). Only has an effect if do_resize is set to True.
|
|
85
|
+
doRescale
|
|
86
|
+
Whether to rescale the image values by rescaleFactor
|
|
87
|
+
rescaleFactor
|
|
88
|
+
Factor to scale the image values
|
|
89
|
+
cropPct
|
|
90
|
+
Percentage of the resized image to crop
|
|
91
|
+
configProtoBytes
|
|
92
|
+
ConfigProto from tensorflow, serialized into byte array.
|
|
93
|
+
|
|
94
|
+
Examples
|
|
95
|
+
--------
|
|
96
|
+
>>> import sparknlp
|
|
97
|
+
>>> from sparknlp.base import *
|
|
98
|
+
>>> from sparknlp.annotator import *
|
|
99
|
+
>>> from pyspark.ml import Pipeline
|
|
100
|
+
>>> imageDF = spark.read \\
|
|
101
|
+
... .format("image") \\
|
|
102
|
+
... .option("dropInvalid", value = True) \\
|
|
103
|
+
... .load("src/test/resources/image/")
|
|
104
|
+
>>> imageAssembler = ImageAssembler() \\
|
|
105
|
+
... .setInputCol("image") \\
|
|
106
|
+
... .setOutputCol("image_assembler")
|
|
107
|
+
>>> imageClassifier = ConvNextForImageClassification \\
|
|
108
|
+
... .pretrained() \\
|
|
109
|
+
... .setInputCols(["image_assembler"]) \\
|
|
110
|
+
... .setOutputCol("class")
|
|
111
|
+
>>> pipeline = Pipeline().setStages([imageAssembler, imageClassifier])
|
|
112
|
+
>>> pipelineDF = pipeline.fit(imageDF).transform(imageDF)
|
|
113
|
+
>>> pipelineDF \\
|
|
114
|
+
... .selectExpr("reverse(split(image.origin, '/'))[0] as image_name", "class.result") \\
|
|
115
|
+
... .show(truncate=False)
|
|
116
|
+
+-----------------+----------------------------------------------------------+
|
|
117
|
+
|image_name |result |
|
|
118
|
+
+-----------------+----------------------------------------------------------+
|
|
119
|
+
|bluetick.jpg |[bluetick] |
|
|
120
|
+
|chihuahua.jpg |[Chihuahua] |
|
|
121
|
+
|egyptian_cat.jpeg|[tabby, tabby cat] |
|
|
122
|
+
|hen.JPEG |[hen] |
|
|
123
|
+
|hippopotamus.JPEG|[hippopotamus, hippo, river horse, Hippopotamus amphibius]|
|
|
124
|
+
|junco.JPEG |[junco, snowbird] |
|
|
125
|
+
|ostrich.JPEG |[ostrich, Struthio camelus] |
|
|
126
|
+
|ox.JPEG |[ox] |
|
|
127
|
+
|palace.JPEG |[palace] |
|
|
128
|
+
|tractor.JPEG |[thresher, thrasher, threshing machine |
|
|
129
|
+
+-----------------+----------------------------------------------------------+
|
|
130
|
+
"""
|
|
131
|
+
name = "ConvNextForImageClassification"
|
|
132
|
+
|
|
133
|
+
inputAnnotatorTypes = [AnnotatorType.IMAGE]
|
|
134
|
+
|
|
135
|
+
outputAnnotatorType = AnnotatorType.CATEGORY
|
|
136
|
+
|
|
137
|
+
configProtoBytes = Param(Params._dummy(),
|
|
138
|
+
"configProtoBytes",
|
|
139
|
+
"ConfigProto from tensorflow, serialized into byte array. Get with "
|
|
140
|
+
"config_proto.SerializeToString()",
|
|
141
|
+
TypeConverters.toListInt)
|
|
142
|
+
|
|
143
|
+
doRescale = Param(Params._dummy(), "doRescale",
|
|
144
|
+
"Whether to rescale the image values by rescaleFactor.",
|
|
145
|
+
TypeConverters.toBoolean)
|
|
146
|
+
|
|
147
|
+
rescaleFactor = Param(Params._dummy(), "rescaleFactor",
|
|
148
|
+
"Factor to scale the image values",
|
|
149
|
+
TypeConverters.toFloat)
|
|
150
|
+
|
|
151
|
+
cropPct = Param(Params._dummy(), "cropPct",
|
|
152
|
+
"Percentage of the resized image to crop",
|
|
153
|
+
TypeConverters.toFloat)
|
|
154
|
+
|
|
155
|
+
def setDoRescale(self, value):
|
|
156
|
+
"""Sets Whether to rescale the image values by rescaleFactor, by default `True`.
|
|
157
|
+
|
|
158
|
+
Parameters
|
|
159
|
+
----------
|
|
160
|
+
value : Boolean
|
|
161
|
+
Whether to rescale the image values by rescaleFactor.
|
|
162
|
+
"""
|
|
163
|
+
return self._set(doRescale=value)
|
|
164
|
+
|
|
165
|
+
def setRescaleFactor(self, value):
|
|
166
|
+
"""Sets Factor to scale the image values, by default `1/255.0`.
|
|
167
|
+
|
|
168
|
+
Parameters
|
|
169
|
+
----------
|
|
170
|
+
value : Boolean
|
|
171
|
+
Whether to rescale the image values by rescaleFactor.
|
|
172
|
+
"""
|
|
173
|
+
return self._set(rescaleFactor=value)
|
|
174
|
+
|
|
175
|
+
def setCropPct(self, value):
|
|
176
|
+
"""Determines rescale and crop percentage for images smaller than the configured size, by default `224 / 256`.
|
|
177
|
+
|
|
178
|
+
If the image size is smaller than the specified size, the smaller edge of the image will be
|
|
179
|
+
matched to `int(size / cropPct)`. Afterwards the image is cropped to `(size, size)`.
|
|
180
|
+
|
|
181
|
+
Parameters
|
|
182
|
+
----------
|
|
183
|
+
value : Float
|
|
184
|
+
Percentage of the resized image to crop
|
|
185
|
+
"""
|
|
186
|
+
return self._set(cropPct=value)
|
|
187
|
+
|
|
188
|
+
def getClasses(self):
|
|
189
|
+
"""
|
|
190
|
+
Returns labels used to train this model
|
|
191
|
+
"""
|
|
192
|
+
return self._call_java("getClasses")
|
|
193
|
+
|
|
194
|
+
def setConfigProtoBytes(self, b):
|
|
195
|
+
"""Sets configProto from tensorflow, serialized into byte array.
|
|
196
|
+
|
|
197
|
+
Parameters
|
|
198
|
+
----------
|
|
199
|
+
b : List[int]
|
|
200
|
+
ConfigProto from tensorflow, serialized into byte array
|
|
201
|
+
"""
|
|
202
|
+
return self._set(configProtoBytes=b)
|
|
203
|
+
|
|
204
|
+
@keyword_only
|
|
205
|
+
def __init__(self,
|
|
206
|
+
classname="com.johnsnowlabs.nlp.annotators.cv.ConvNextForImageClassification",
|
|
207
|
+
java_model=None):
|
|
208
|
+
super(ConvNextForImageClassification, self).__init__(
|
|
209
|
+
classname=classname,
|
|
210
|
+
java_model=java_model
|
|
211
|
+
)
|
|
212
|
+
self._setDefault(
|
|
213
|
+
batchSize=2,
|
|
214
|
+
doNormalize=True,
|
|
215
|
+
doRescale=True,
|
|
216
|
+
doResize=True,
|
|
217
|
+
imageMean=[0.485, 0.456, 0.406],
|
|
218
|
+
imageStd=[0.229, 0.224, 0.225],
|
|
219
|
+
resample=3,
|
|
220
|
+
size=224,
|
|
221
|
+
rescaleFactor=1 / 255.0,
|
|
222
|
+
cropPct=224 / 256.0
|
|
223
|
+
)
|
|
224
|
+
|
|
225
|
+
@staticmethod
|
|
226
|
+
def loadSavedModel(folder, spark_session):
|
|
227
|
+
"""Loads a locally saved model.
|
|
228
|
+
|
|
229
|
+
Parameters
|
|
230
|
+
----------
|
|
231
|
+
folder : str
|
|
232
|
+
Folder of the saved model
|
|
233
|
+
spark_session : pyspark.sql.SparkSession
|
|
234
|
+
The current SparkSession
|
|
235
|
+
|
|
236
|
+
Returns
|
|
237
|
+
-------
|
|
238
|
+
ConvNextForImageClassification
|
|
239
|
+
The restored model
|
|
240
|
+
"""
|
|
241
|
+
from sparknlp.internal import _ConvNextForImageClassification
|
|
242
|
+
jModel = _ConvNextForImageClassification(folder,
|
|
243
|
+
spark_session._jsparkSession)._java_obj
|
|
244
|
+
return ConvNextForImageClassification(java_model=jModel)
|
|
245
|
+
|
|
246
|
+
@staticmethod
|
|
247
|
+
def pretrained(name="image_classifier_convnext_tiny_224_local", lang="en",
|
|
248
|
+
remote_loc=None):
|
|
249
|
+
"""Downloads and loads a pretrained model.
|
|
250
|
+
|
|
251
|
+
Parameters
|
|
252
|
+
----------
|
|
253
|
+
name : str, optional
|
|
254
|
+
Name of the pretrained model, by default
|
|
255
|
+
"image_classifier_convnext_tiny_224_local"
|
|
256
|
+
lang : str, optional
|
|
257
|
+
Language of the pretrained model, by default "en"
|
|
258
|
+
remote_loc : str, optional
|
|
259
|
+
Remote address of the resource, by default None. Will use
|
|
260
|
+
Spark NLPs repositories otherwise.
|
|
261
|
+
|
|
262
|
+
Returns
|
|
263
|
+
-------
|
|
264
|
+
ConvNextForImageClassification
|
|
265
|
+
The restored model
|
|
266
|
+
"""
|
|
267
|
+
from sparknlp.pretrained import ResourceDownloader
|
|
268
|
+
return ResourceDownloader.downloadModel(ConvNextForImageClassification, name, lang,
|
|
269
|
+
remote_loc)
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
# Copyright 2017-2024 John Snow Labs
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
|
|
15
|
+
from sparknlp.common import *
|
|
16
|
+
|
|
17
|
+
class Florence2Transformer(AnnotatorModel,
|
|
18
|
+
HasBatchedAnnotateImage,
|
|
19
|
+
HasImageFeatureProperties,
|
|
20
|
+
HasEngine):
|
|
21
|
+
"""Florence2Transformer can load Florence-2 models for a variety of vision and vision-language tasks using prompt-based inference.
|
|
22
|
+
|
|
23
|
+
The model supports image captioning, object detection, segmentation, OCR, and more, using prompt tokens as described in the Florence-2 documentation.
|
|
24
|
+
|
|
25
|
+
Pretrained models can be loaded with :meth:`.pretrained` of the companion object:
|
|
26
|
+
|
|
27
|
+
>>> florence2 = Florence2Transformer.pretrained() \
|
|
28
|
+
... .setInputCols(["image_assembler"]) \
|
|
29
|
+
... .setOutputCol("answer")
|
|
30
|
+
|
|
31
|
+
The default model is ``"florence2_base_ft_int4"``, if no name is provided.
|
|
32
|
+
|
|
33
|
+
For available pretrained models please see the `Models Hub <https://sparknlp.org/models?task=Vision+Tasks>`__.
|
|
34
|
+
|
|
35
|
+
====================== ======================
|
|
36
|
+
Input Annotation types Output Annotation type
|
|
37
|
+
====================== ======================
|
|
38
|
+
``IMAGE`` ``DOCUMENT``
|
|
39
|
+
====================== ======================
|
|
40
|
+
|
|
41
|
+
Parameters
|
|
42
|
+
----------
|
|
43
|
+
batchSize
|
|
44
|
+
Batch size. Large values allows faster processing but requires more memory, by default 2
|
|
45
|
+
maxOutputLength
|
|
46
|
+
Maximum length of output text, by default 200
|
|
47
|
+
minOutputLength
|
|
48
|
+
Minimum length of the sequence to be generated, by default 10
|
|
49
|
+
doSample
|
|
50
|
+
Whether or not to use sampling; use greedy decoding otherwise, by default False
|
|
51
|
+
temperature
|
|
52
|
+
The value used to module the next token probabilities, by default 1.0
|
|
53
|
+
topK
|
|
54
|
+
The number of highest probability vocabulary tokens to keep for top-k-filtering, by default 50
|
|
55
|
+
topP
|
|
56
|
+
If set to float < 1, only the most probable tokens with probabilities that add up to ``top_p`` or higher are kept for generation, by default 1.0
|
|
57
|
+
repetitionPenalty
|
|
58
|
+
The parameter for repetition penalty. 1.0 means no penalty, by default 1.0
|
|
59
|
+
noRepeatNgramSize
|
|
60
|
+
If set to int > 0, all ngrams of that size can only occur once, by default 3
|
|
61
|
+
ignoreTokenIds
|
|
62
|
+
A list of token ids which are ignored in the decoder's output, by default []
|
|
63
|
+
beamSize
|
|
64
|
+
The Number of beams for beam search, by default 1
|
|
65
|
+
|
|
66
|
+
Examples
|
|
67
|
+
--------
|
|
68
|
+
>>> import sparknlp
|
|
69
|
+
>>> from sparknlp.base import *
|
|
70
|
+
>>> from sparknlp.annotator import *
|
|
71
|
+
>>> from pyspark.ml import Pipeline
|
|
72
|
+
>>> image_df = spark.read.format("image").load(path=images_path)
|
|
73
|
+
>>> test_df = image_df.withColumn("text", lit("<OD>"))
|
|
74
|
+
>>> imageAssembler = ImageAssembler() \
|
|
75
|
+
... .setInputCol("image") \
|
|
76
|
+
... .setOutputCol("image_assembler")
|
|
77
|
+
>>> florence2 = Florence2Transformer.pretrained() \
|
|
78
|
+
... .setInputCols(["image_assembler"]) \
|
|
79
|
+
... .setOutputCol("answer")
|
|
80
|
+
>>> pipeline = Pipeline().setStages([
|
|
81
|
+
... imageAssembler,
|
|
82
|
+
... florence2
|
|
83
|
+
... ])
|
|
84
|
+
>>> result = pipeline.fit(test_df).transform(test_df)
|
|
85
|
+
>>> result.select("image_assembler.origin", "answer.result").show(False)
|
|
86
|
+
"""
|
|
87
|
+
|
|
88
|
+
name = "Florence2Transformer"
|
|
89
|
+
|
|
90
|
+
inputAnnotatorTypes = [AnnotatorType.IMAGE]
|
|
91
|
+
outputAnnotatorType = AnnotatorType.DOCUMENT
|
|
92
|
+
|
|
93
|
+
minOutputLength = Param(Params._dummy(), "minOutputLength", "Minimum length of the sequence to be generated", typeConverter=TypeConverters.toInt)
|
|
94
|
+
maxOutputLength = Param(Params._dummy(), "maxOutputLength", "Maximum length of output text", typeConverter=TypeConverters.toInt)
|
|
95
|
+
doSample = Param(Params._dummy(), "doSample", "Whether or not to use sampling; use greedy decoding otherwise", typeConverter=TypeConverters.toBoolean)
|
|
96
|
+
temperature = Param(Params._dummy(), "temperature", "The value used to module the next token probabilities", typeConverter=TypeConverters.toFloat)
|
|
97
|
+
topK = Param(Params._dummy(), "topK", "The number of highest probability vocabulary tokens to keep for top-k-filtering", typeConverter=TypeConverters.toInt)
|
|
98
|
+
topP = Param(Params._dummy(), "topP", "If set to float < 1, only the most probable tokens with probabilities that add up to top_p or higher are kept for generation", typeConverter=TypeConverters.toFloat)
|
|
99
|
+
repetitionPenalty = Param(Params._dummy(), "repetitionPenalty", "The parameter for repetition penalty. 1.0 means no penalty.", typeConverter=TypeConverters.toFloat)
|
|
100
|
+
noRepeatNgramSize = Param(Params._dummy(), "noRepeatNgramSize", "If set to int > 0, all ngrams of that size can only occur once", typeConverter=TypeConverters.toInt)
|
|
101
|
+
ignoreTokenIds = Param(Params._dummy(), "ignoreTokenIds", "A list of token ids which are ignored in the decoder's output", typeConverter=TypeConverters.toListInt)
|
|
102
|
+
beamSize = Param(Params._dummy(), "beamSize", "The Number of beams for beam search.", typeConverter=TypeConverters.toInt)
|
|
103
|
+
batchSize = Param(Params._dummy(), "batchSize", "Batch size. Large values allows faster processing but requires more memory", typeConverter=TypeConverters.toInt)
|
|
104
|
+
|
|
105
|
+
@keyword_only
|
|
106
|
+
def __init__(self, classname="com.johnsnowlabs.nlp.annotators.cv.Florence2Transformer", java_model=None):
|
|
107
|
+
super(Florence2Transformer, self).__init__(
|
|
108
|
+
classname=classname,
|
|
109
|
+
java_model=java_model
|
|
110
|
+
)
|
|
111
|
+
self._setDefault(
|
|
112
|
+
batchSize=2,
|
|
113
|
+
minOutputLength=10,
|
|
114
|
+
maxOutputLength=200,
|
|
115
|
+
doSample=False,
|
|
116
|
+
temperature=1.0,
|
|
117
|
+
topK=50,
|
|
118
|
+
topP=1.0,
|
|
119
|
+
repetitionPenalty=1.0,
|
|
120
|
+
noRepeatNgramSize=3,
|
|
121
|
+
ignoreTokenIds=[],
|
|
122
|
+
beamSize=1,
|
|
123
|
+
)
|
|
124
|
+
|
|
125
|
+
def setMinOutputLength(self, value):
|
|
126
|
+
"""Sets minimum length of the sequence to be generated."""
|
|
127
|
+
return self._set(minOutputLength=value)
|
|
128
|
+
|
|
129
|
+
def setMaxOutputLength(self, value):
|
|
130
|
+
"""Sets maximum length of output text."""
|
|
131
|
+
return self._set(maxOutputLength=value)
|
|
132
|
+
|
|
133
|
+
def setDoSample(self, value):
|
|
134
|
+
"""Sets whether or not to use sampling; use greedy decoding otherwise."""
|
|
135
|
+
return self._set(doSample=value)
|
|
136
|
+
|
|
137
|
+
def setTemperature(self, value):
|
|
138
|
+
"""Sets the value used to module the next token probabilities."""
|
|
139
|
+
return self._set(temperature=value)
|
|
140
|
+
|
|
141
|
+
def setTopK(self, value):
|
|
142
|
+
"""Sets the number of highest probability vocabulary tokens to keep for top-k-filtering."""
|
|
143
|
+
return self._set(topK=value)
|
|
144
|
+
|
|
145
|
+
def setTopP(self, value):
|
|
146
|
+
"""Sets the top cumulative probability for vocabulary tokens."""
|
|
147
|
+
return self._set(topP=value)
|
|
148
|
+
|
|
149
|
+
def setRepetitionPenalty(self, value):
|
|
150
|
+
"""Sets the parameter for repetition penalty. 1.0 means no penalty."""
|
|
151
|
+
return self._set(repetitionPenalty=value)
|
|
152
|
+
|
|
153
|
+
def setNoRepeatNgramSize(self, value):
|
|
154
|
+
"""Sets size of n-grams that can only occur once."""
|
|
155
|
+
return self._set(noRepeatNgramSize=value)
|
|
156
|
+
|
|
157
|
+
def setIgnoreTokenIds(self, value):
|
|
158
|
+
"""A list of token ids which are ignored in the decoder's output."""
|
|
159
|
+
return self._set(ignoreTokenIds=value)
|
|
160
|
+
|
|
161
|
+
def setBeamSize(self, value):
|
|
162
|
+
"""Sets the number of beams for beam search."""
|
|
163
|
+
return self._set(beamSize=value)
|
|
164
|
+
|
|
165
|
+
def setBatchSize(self, value):
|
|
166
|
+
"""Sets the batch size."""
|
|
167
|
+
return self._set(batchSize=value)
|
|
168
|
+
|
|
169
|
+
@staticmethod
|
|
170
|
+
def loadSavedModel(folder, spark_session, use_openvino=False):
|
|
171
|
+
"""Loads a locally saved model."""
|
|
172
|
+
from sparknlp.internal import _Florence2TransformerLoader
|
|
173
|
+
jModel = _Florence2TransformerLoader(folder, spark_session._jsparkSession, use_openvino)._java_obj
|
|
174
|
+
return Florence2Transformer(java_model=jModel)
|
|
175
|
+
|
|
176
|
+
@staticmethod
|
|
177
|
+
def pretrained(name="florence2_base_ft_int4", lang="en", remote_loc=None):
|
|
178
|
+
"""Downloads and loads a pretrained model."""
|
|
179
|
+
from sparknlp.pretrained import ResourceDownloader
|
|
180
|
+
return ResourceDownloader.downloadModel(Florence2Transformer, name, lang, remote_loc)
|