Spaces:
Running
Running
File size: 8,359 Bytes
b110593 |
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 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 |
// _ _
// __ _____ __ ___ ___ __ _| |_ ___
// \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \
// \ V V / __/ (_| |\ V /| | (_| | || __/
// \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___|
//
// Copyright © 2016 - 2024 Weaviate B.V. All rights reserved.
//
// CONTACT: [email protected]
//
package vectorizer
import (
"context"
"github.com/pkg/errors"
"github.com/go-openapi/strfmt"
"github.com/weaviate/weaviate/entities/models"
"github.com/weaviate/weaviate/entities/moduletools"
"github.com/weaviate/weaviate/modules/multi2vec-bind/ent"
libvectorizer "github.com/weaviate/weaviate/usecases/vectorizer"
)
type Vectorizer struct {
client Client
}
func New(client Client) *Vectorizer {
return &Vectorizer{
client: client,
}
}
type Client interface {
Vectorize(ctx context.Context,
texts, images, audio, video, imu, thermal, depth []string,
) (*ent.VectorizationResult, error)
}
type ClassSettings interface {
ImageField(property string) bool
ImageFieldsWeights() ([]float32, error)
TextField(property string) bool
TextFieldsWeights() ([]float32, error)
AudioField(property string) bool
AudioFieldsWeights() ([]float32, error)
VideoField(property string) bool
VideoFieldsWeights() ([]float32, error)
IMUField(property string) bool
IMUFieldsWeights() ([]float32, error)
ThermalField(property string) bool
ThermalFieldsWeights() ([]float32, error)
DepthField(property string) bool
DepthFieldsWeights() ([]float32, error)
}
func (v *Vectorizer) Object(ctx context.Context, object *models.Object,
objDiff *moduletools.ObjectDiff, settings ClassSettings,
) error {
vec, err := v.object(ctx, object.ID, object.Properties, objDiff, settings)
if err != nil {
return err
}
object.Vector = vec
return nil
}
func (v *Vectorizer) VectorizeImage(ctx context.Context, image string) ([]float32, error) {
res, err := v.client.Vectorize(ctx, nil, []string{image}, nil, nil, nil, nil, nil)
if err != nil {
return nil, err
}
return v.getVector(res.ImageVectors)
}
func (v *Vectorizer) VectorizeAudio(ctx context.Context, audio string) ([]float32, error) {
res, err := v.client.Vectorize(ctx, nil, nil, []string{audio}, nil, nil, nil, nil)
if err != nil {
return nil, err
}
return v.getVector(res.AudioVectors)
}
func (v *Vectorizer) VectorizeVideo(ctx context.Context, video string) ([]float32, error) {
res, err := v.client.Vectorize(ctx, nil, nil, nil, []string{video}, nil, nil, nil)
if err != nil {
return nil, err
}
return v.getVector(res.VideoVectors)
}
func (v *Vectorizer) VectorizeIMU(ctx context.Context, imu string) ([]float32, error) {
res, err := v.client.Vectorize(ctx, nil, nil, nil, nil, []string{imu}, nil, nil)
if err != nil {
return nil, err
}
return v.getVector(res.IMUVectors)
}
func (v *Vectorizer) VectorizeThermal(ctx context.Context, thermal string) ([]float32, error) {
res, err := v.client.Vectorize(ctx, nil, nil, nil, nil, nil, []string{thermal}, nil)
if err != nil {
return nil, err
}
return v.getVector(res.ThermalVectors)
}
func (v *Vectorizer) VectorizeDepth(ctx context.Context, depth string) ([]float32, error) {
res, err := v.client.Vectorize(ctx, nil, nil, nil, nil, nil, nil, []string{depth})
if err != nil {
return nil, err
}
return v.getVector(res.DepthVectors)
}
func (v *Vectorizer) getVector(vectors [][]float32) ([]float32, error) {
if len(vectors) != 1 {
return nil, errors.New("empty vector")
}
return vectors[0], nil
}
func (v *Vectorizer) object(ctx context.Context, id strfmt.UUID,
schema interface{}, objDiff *moduletools.ObjectDiff, ichek ClassSettings,
) ([]float32, error) {
vectorize := objDiff == nil || objDiff.GetVec() == nil
// vectorize image and text
var texts, images, audio, video, imu, thermal, depth []string
if schema != nil {
for prop, value := range schema.(map[string]interface{}) {
if ichek.ImageField(prop) {
valueString, ok := value.(string)
if ok {
images = append(images, valueString)
vectorize = vectorize || (objDiff != nil && objDiff.IsChangedProp(prop))
}
}
if ichek.TextField(prop) {
valueString, ok := value.(string)
if ok {
texts = append(texts, valueString)
vectorize = vectorize || (objDiff != nil && objDiff.IsChangedProp(prop))
}
valueArr, ok := value.([]interface{})
if ok {
for _, value := range valueArr {
valueString, ok := value.(string)
if ok {
texts = append(texts, valueString)
vectorize = vectorize || (objDiff != nil && objDiff.IsChangedProp(prop))
}
}
}
}
if ichek.AudioField(prop) {
valueString, ok := value.(string)
if ok {
audio = append(audio, valueString)
vectorize = vectorize || (objDiff != nil && objDiff.IsChangedProp(prop))
}
}
if ichek.VideoField(prop) {
valueString, ok := value.(string)
if ok {
video = append(video, valueString)
vectorize = vectorize || (objDiff != nil && objDiff.IsChangedProp(prop))
}
}
if ichek.IMUField(prop) {
valueString, ok := value.(string)
if ok {
imu = append(imu, valueString)
vectorize = vectorize || (objDiff != nil && objDiff.IsChangedProp(prop))
}
}
if ichek.ThermalField(prop) {
valueString, ok := value.(string)
if ok {
thermal = append(thermal, valueString)
vectorize = vectorize || (objDiff != nil && objDiff.IsChangedProp(prop))
}
}
if ichek.DepthField(prop) {
valueString, ok := value.(string)
if ok {
depth = append(depth, valueString)
vectorize = vectorize || (objDiff != nil && objDiff.IsChangedProp(prop))
}
}
}
}
// no property was changed, old vector can be used
if !vectorize {
return objDiff.GetVec(), nil
}
vectors := [][]float32{}
if len(texts) > 0 || len(images) > 0 || len(audio) > 0 || len(video) > 0 ||
len(imu) > 0 || len(thermal) > 0 || len(depth) > 0 {
res, err := v.client.Vectorize(ctx, texts, images, audio, video, imu, thermal, depth)
if err != nil {
return nil, err
}
vectors = append(vectors, res.TextVectors...)
vectors = append(vectors, res.ImageVectors...)
vectors = append(vectors, res.AudioVectors...)
vectors = append(vectors, res.VideoVectors...)
vectors = append(vectors, res.IMUVectors...)
vectors = append(vectors, res.ThermalVectors...)
vectors = append(vectors, res.DepthVectors...)
}
weights, err := v.getWeights(ichek)
if err != nil {
return nil, err
}
return libvectorizer.CombineVectorsWithWeights(vectors, weights), nil
}
func (v *Vectorizer) getWeights(ichek ClassSettings) ([]float32, error) {
weights := []float32{}
textFieldsWeights, err := ichek.TextFieldsWeights()
if err != nil {
return nil, err
}
imageFieldsWeights, err := ichek.ImageFieldsWeights()
if err != nil {
return nil, err
}
audioFieldsWeights, err := ichek.AudioFieldsWeights()
if err != nil {
return nil, err
}
videoFieldsWeights, err := ichek.VideoFieldsWeights()
if err != nil {
return nil, err
}
imuFieldsWeights, err := ichek.IMUFieldsWeights()
if err != nil {
return nil, err
}
thermalFieldsWeights, err := ichek.ThermalFieldsWeights()
if err != nil {
return nil, err
}
depthFieldsWeights, err := ichek.DepthFieldsWeights()
if err != nil {
return nil, err
}
weights = append(weights, textFieldsWeights...)
weights = append(weights, imageFieldsWeights...)
weights = append(weights, audioFieldsWeights...)
weights = append(weights, videoFieldsWeights...)
weights = append(weights, imuFieldsWeights...)
weights = append(weights, thermalFieldsWeights...)
weights = append(weights, depthFieldsWeights...)
normalizedWeights := v.normalizeWeights(weights)
return normalizedWeights, nil
}
func (v *Vectorizer) normalizeWeights(weights []float32) []float32 {
if len(weights) > 0 {
var denominator float32
for i := range weights {
denominator += weights[i]
}
normalizer := 1 / denominator
normalized := make([]float32, len(weights))
for i := range weights {
normalized[i] = weights[i] * normalizer
}
return normalized
}
return nil
}
|