一、簡述
這里的代碼主要是基于圖像的推薦系統,該系統利用 ResNet-50 深度學習模型作為特征提取器,并采用余弦相似度來查找給定輸入圖像的最相似嵌入。
該系統旨在根據所提供圖像的視覺內容為用戶提供個性化推薦。
二、所需環境
Python 3.x
tensorflow ==2.5.0
numpy==1.21.0
streamlit
pillow==8.3.1
pandas
三、特征提取
首先加載ResNet50的基于imagenet預訓練模型。
凍結模型的權重,使其在訓練過程中不會更新。
創建一個新模型,在ResNet50模型之后添加一個GlobalMaxPooling2D層。
使用預先訓練的模型從圖像中提取特征。
import tensorflow as tf
import numpy as np
from numpy.linalg import norm
import os
from tqdm import tqdm
import pickle# Load the pre-trained ResNet50 model
model = tf.keras.applications.resnet50.ResNet50(weights='imagenet', include_top=False, input_shape=(224, 224, 3))
# Freeze the model's weights, so they won't be updated during training
model.trainable = False# Create a ne