這個 2 類 NSFW 偵測器是一個輕量級 Autokeras 模型,採用 CLIP ViT L/14 嵌入作為輸入。它估計 0 到 1 之間的值(1 = NSFW),並且非常適合圖像嵌入。
示範-Colab:https://colab.research.google.com/drive/19Acr4grlk5oQws7BHTqNIK-80XGw2u8Z?usp=sharing
訓練 CLIP VL/14 嵌入可以在此處下載:https://drive.google.com/file/d/1yenil0R4GqmTOFQ_GVw__x61ofZ-OBcS/view?usp=sharing(未完全手動註釋,因此不能用作測試)
(手動註解的)測試集位於 https://github.com/LAION-AI/CLIP-based-NSFW-Detector/blob/main/nsfw_testset.zip
https://github.com/rom1504/embedding-reader/blob/main/examples/inference_example.py laion5B 上的推理
模型使用範例:
@ lru_cache ( maxsize = None )
def load_safety_model ( clip_model ):
"""load the safety model"""
import autokeras as ak # pylint: disable=import-outside-toplevel
from tensorflow . keras . models import load_model # pylint: disable=import-outside-toplevel
cache_folder = get_cache_folder ( clip_model )
if clip_model == "ViT-L/14" :
model_dir = cache_folder + "/clip_autokeras_binary_nsfw"
dim = 768
elif clip_model == "ViT-B/32" :
model_dir = cache_folder + "/clip_autokeras_nsfw_b32"
dim = 512
else :
raise ValueError ( "Unknown clip model" )
if not os . path . exists ( model_dir ):
os . makedirs ( cache_folder , exist_ok = True )
from urllib . request import urlretrieve # pylint: disable=import-outside-toplevel
path_to_zip_file = cache_folder + "/clip_autokeras_binary_nsfw.zip"
if clip_model == "ViT-L/14" :
url_model = "https://raw.githubusercontent.com/LAION-AI/CLIP-based-NSFW-Detector/main/clip_autokeras_binary_nsfw.zip"
elif clip_model == "ViT-B/32" :
url_model = (
"https://raw.githubusercontent.com/LAION-AI/CLIP-based-NSFW-Detector/main/clip_autokeras_nsfw_b32.zip"
)
else :
raise ValueError ( "Unknown model {}" . format ( clip_model )) # pylint: disable=consider-using-f-string
urlretrieve ( url_model , path_to_zip_file )
import zipfile # pylint: disable=import-outside-toplevel
with zipfile . ZipFile ( path_to_zip_file , "r" ) as zip_ref :
zip_ref . extractall ( cache_folder )
loaded_model = load_model ( model_dir , custom_objects = ak . CUSTOM_OBJECTS )
loaded_model . predict ( np . random . rand ( 10 ** 3 , dim ). astype ( "float32" ), batch_size = 10 ** 3 )
return loaded_model
nsfw_values = safety_model . predict ( embeddings , batch_size = embeddings . shape [ 0 ])
此程式碼和模型是在 MIT 許可證下發布的:
版權所有 2022,克里斯托夫舒曼
特此免費授予任何獲得本軟體和相關文件文件(「軟體」)副本的人不受限制地使用本軟體,包括但不限於使用、複製、修改、合併的權利、發布、分發、再授權和/或銷售軟體的副本,並允許向其提供軟體的人員這樣做,但須滿足以下條件:
上述版權聲明和本授權聲明應包含在本軟體的所有副本或主要部分中。
本軟體以「現況」提供,不提供任何明示或暗示的保證,包括但不限於適銷性、特定用途的適用性和不侵權的保證。 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE軟體.