Sistemi di AI Multimodale: Analisi di Immagini, Testo e Audio
L’AI multimodale è costituita da sistemi di intelligenza artificiale in grado di comprendere ed elaborare molteplici tipologie di dati (testo, immagine, audio, video). Modelli come GPTV, Gemini e Claude 3 hanno aperto nuove frontiere in questo campo.
Fondamenti dell’AI Multimodale
Tipologie di Modalità
- Testo: Linguaggio naturale, codice, dati strutturati
- Visione: Foto, diagramma, screenshot
- Audio: Voce, musica, suoni ambientali
- Video: Combinazione di immagini in movimento + audio
Perché Multimodale?
- La comunicazione umana è intrinsecamente multimodale
- Informazioni contestuali perse con una singola modalità
- Estrazione di significato più ricca
- Maggiore adeguatezza per applicazioni reali
Modelli Vision-Language
Approcci Architetturali
1. Contrastive Learning (stile CLIP)
1Image Encoder → Image Embedding 2Text Encoder → Text Embedding 3Contrastive Loss: Match(image, text)
2. Generativo (stile GPTV)
Image → Vision Encoder → Visual Tokens Visual Tokens + Text Tokens → LLM → Response
3. Fusione con Cross-Attention
Image Features ←Cross-Attention→ Text Features
Tipi di Vision Encoder
| Encoder | Architettura | Risoluzione | Caratteristica |
|---|---|---|---|
| ViT | Transformer | 224-1024 | Patch-based |
| CLIP ViT | Transformer | 336 | Contrastive |
| SigLIP | Transformer | 384 | Sigmoid loss |
| ConvNeXt | CNN | Flessibile | Efficiente |
Tokenizzazione delle Immagini
Patch Embedding:
224×224 image → 14×14 patch grid → 196 visual tokens Each patch: 16×16 pixel → Linear projection → Embedding
Risoluzione Variabile:
1Anyres approach: 21. Divide image into tiles 32. Encode each tile separately 43. Add global thumbnail 54. Concatenate all tokens
Implementazione di LLM Multimodali
Utilizzo di GPTV
1from openai import OpenAI 2import base64 3 4client = OpenAI() 5 6def encode_image(image_path): 7 with open(image_path, "rb") as f: 8 return base64.b64encode(f.read()).decode('utf-8') 9 10response = client.chat.completions.create( 11 model="gpt-4-vision-preview", 12 messages=[ 13 { 14 "role": "user", 15 "content": [ 16 {"type": "text", "text": "Analyze this image"}, 17 { 18 "type": "image_url", 19 "image_url": { 20 "url": f"data:image/jpeg;base64,{encode_image('image.webp')}", 21 "detail": "high" # low, high, auto 22 } 23 } 24 ] 25 } 26 ], 27 max_tokens=1000 28)
Claude 3 Vision
1from anthropic import Anthropic 2import base64 3 4client = Anthropic() 5 6with open("image.webp", "rb") as f: 7 image_data = base64.standard_b64encode(f.read()).decode("utf-8") 8 9message = client.messages.create( 10 model="claude-3-opus-20240229", 11 max_tokens=1024, 12 messages=[ 13 { 14 "role": "user", 15 "content": [ 16 { 17 "type": "image", 18 "source": { 19 "type": "base64", 20 "media_type": "image/jpeg", 21 "data": image_data 22 } 23 }, 24 {"type": "text", "text": "What is in this image?"} 25 ] 26 } 27 ] 28) 29## Elaborazione Audio 30 31### Speech-to-Text (STT) 32 33**Modello Whisper:** 34```python 35from openai import OpenAI 36 37client = OpenAI() 38 39with open("audio.mp3", "rb") as audio_file: 40 transcript = client.audio.transcriptions.create( 41 model="whisper-1", 42 file=audio_file, 43 language="en" 44 ) 45 46print(transcript.text)
Text-to-Speech (TTS)
1response = client.audio.speech.create( 2 model="tts-1-hd", 3 voice="alloy", # alloy, echo, fable, onyx, nova, shimmer 4 input="Hello, I am an AI assistant." 5) 6 7response.stream_to_file("output.mp3")
Pipeline Audio in Tempo Reale
1Microphone → VAD → Chunking → STT → LLM → TTS → Speaker 2 ↓ 3 Voice Activity 4 Detection
Comprensione Video
Strategie di Campionamento dei Frame
1. Campionamento Uniforme:
1def uniform_sample(video_path, num_frames=8): 2 cap = cv2.VideoCapture(video_path) 3 total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) 4 indices = np.linspace(0, total_frames-1, num_frames, dtype=int) 5 6 frames = [] 7 for idx in indices: 8 cap.set(cv2.CAP_PROP_POS_FRAMES, idx) 9 ret, frame = cap.read() 10 if ret: 11 frames.append(frame) 12 13 return frames
2. Estrazione dei Keyframe:
1def extract_keyframes(video_path, threshold=30): 2 # Finding keyframes with Scene change detection 3 pass
Pipeline Video-LLM
1Video → Frame Sampling → Per-frame Encoding → Temporal Aggregation → LLM 2 ↓ 3 Audio Extraction → STT → Text
Fusione di Modalità
Early Fusion
Combinazione delle modalità in ingresso al modello:
[CLS] [IMG_1] ... [IMG_N] [SEP] [TXT_1] ... [TXT_M] [SEP]
Late Fusion
Elaborazione separata di ciascuna modalità e combinazione dei risultati:
1Image → Image Model → Image Features ─┐ 2 ├→ Fusion Layer → Output 3Text → Text Model → Text Features ────┘
Cross-Modal Attention
Attenzione tra modalità:
1Q = Text Features 2K, V = Image Features 3Cross_Attention(Q, K, V) = softmax(QK^T/√d)V
OCR e Comprensione dei Documenti
Pipeline Document AI
1def process_document(image_path): 2 # 1. Layout Detection 3 layout = detect_layout(image) # Headings, paragraphs, tables 4 5 # 2. OCR 6 text_regions = ocr_extract(image) 7 8 # 3. Structure Understanding 9 structured_doc = parse_structure(layout, text_regions) 10 11 # 4. LLM Analysis 12 analysis = llm_analyze(structured_doc) 13 14 return analysis
Estrazione di Tabelle
1response = client.chat.completions.create( 2 model="gpt-4-vision-preview", 3 messages=[{ 4 "role": "user", 5 "content": [ 6 {"type": "image_url", "image_url": {"url": table_image_url}}, 7 {"type": "text", "text": "Extract this table in JSON format"} 8 ] 9 }] 10)
Applicazioni Multimodali Enterprise
1. Elaborazione Documenti
- OCR di fatture/scontrini
- Analisi di contratti
- Estrazione di dati da moduli
2. Ricerca Visiva
- Ricerca da immagine di un prodotto
- Trovare immagini simili
- Q&A visivo
3. Moderazione dei Contenuti
- Rilevamento di immagini inappropriate
- Controllo del logo del brand
- Coerenza testo + immagine
4. Assistenza Clienti
- Analisi di screenshot
- Troubleshooting visivo
- Supporto vocale
Ottimizzazione delle Prestazioni
Pre-elaborazione delle Immagini
1def optimize_image(image_path, max_size=1024, quality=85): 2 img = Image.open(image_path) 3 4 # Resize 5 if max(img.size) > max_size: 6 ratio = max_size / max(img.size) 7 new_size = tuple(int(d * ratio) for d in img.size) 8 img = img.resize(new_size, Image.LANCZOS) 9 10 # Compress 11 buffer = io.BytesIO() 12 img.save(buffer, format="JPEG", quality=quality) 13 14 return buffer.getvalue()
Elaborazione in Batch
1async def batch_image_analysis(images, batch_size=5): 2 results = [] 3 for i in range(0, len(images), batch_size): 4 batch = images[i:i+batch_size] 5 tasks = [analyze_image(img) for img in batch] 6 batch_results = await asyncio.gather(*tasks) 7 results.extend(batch_results) 8 return results
Gestione dei Costi
Calcolo dei Token (Vision)
1GPTV Token Cost: 2- Low detail: 85 token/image 3- High detail: 85 + 170 × tile_count 4 5Example (2048×1024, high): 6Tiles: ceil(2048/512) × ceil(1024/512) = 4 × 2 = 8 7Tokens: 85 + 170 × 8 = 1445 token
Strategie di Ottimizzazione
- Regolare il livello di dettaglio: Non usare "high" se non necessario
- Ridurre la dimensione dell’immagine: Riduce il numero di token
- Caching: Evita di analizzare nuovamente la stessa immagine
- Operazioni in batch: Riduci il numero di chiamate API
Conclusione
L’AI multimodale è l’approccio più vicino alla capacità di comprensione umana dell’intelligenza artificiale. La combinazione di immagini, testo e audio rende possibile creare applicazioni AI più potenti e utili.
In Veni AI, sviluppiamo soluzioni AI multimodali. Contattaci per i tuoi progetti.
