用于分发的 tensorflow-python 更轻的替代品

Lighter alternative to tensorflow-python for distribution

我试图将一个 py 文件编译为二进制文件,它只是从 json 文件中读取模型并通过导入的模型预测输出。 现在的问题是,当我尝试通过 pyinstaller 编译程序时,生成的文件大约为 290mb,因为它试图编译整个 tensorflow 包并且它不需要依赖项。更不用说在尝试提取文件时启动非常非常慢。

如您所见,它只是一个简单的代码,它运行一个图像文件夹并将它们识别为 meme 或非 meme 内容以清理我的 whatsapp 文件夹。

import os
from tensorflow.keras.preprocessing.image import ImageDataGenerator 
from tensorflow.keras.models import model_from_json
import shutil

BATCH_SIZE = 8
SHAPE = (150,150,3)

p = input("Enter path of folder to extract memes from")
p = p.replace("'","")
p = p.replace('"','')

json_file = open('model.json', 'r')
loaded_model_json = json_file.read()
json_file.close()
model = model_from_json(loaded_model_json)
model.load_weights('weights.best.hdf5')
test_datagen = ImageDataGenerator(rescale=1./255)
generator = test_datagen.flow_from_directory(
        p,
        target_size=SHAPE[:-1],
        batch_size=BATCH_SIZE,
        class_mode=None,
        shuffle=False)

pred = model.predict_generator(generator)
# print(pred)
# print(model.summary())
pred[pred>0.5] = 1
pred[pred<=0.5] = 0
d = ["garbage","good"]
for i in range(len(pred)):
  filename = generator.filenames[i].split('\')[1]
  if(pred[i][0] == 0):
    shutil.move(generator.filepaths[i],os.path.join(os.path.join('test',str(int(pred[i][0]))),filename))

所以我的问题是,是否有 model.predict 函数的替代方法,它可能比一个 tensorflow 具有的函数轻很多,因为我不想在我的发行版中包含整个 600mb 的 tensorflow 包。

您为什么不在 Tensorflow 模型上使用量化?模型大小可以减少到 75%。这是使 Tensorflow Lite 能够实时对图片进行预测的处理,仅在移动设备上 phone CPU.

本质上,权重可以转换为精度降低的类型,例如 16 位浮点数或 8 位整数。 Tensorflow 通常建议 16 位浮点数用于 GPU 加速和 8 位整数用于 CPU 执行。阅读 guide here.

Quantization brings improvements via model compression and latency reduction. With the API defaults, the model size shrinks by 4x, and we typically see between 1.5 - 4x improvements in CPU latency in the tested backends. Eventually, latency improvements can be seen on compatible machine learning accelerators, such as the EdgeTPU and NNAPI.

Post-training quantization includes general techniques to reduce CPU and hardware accelerator latency, processing, power, and model size with little degradation in model accuracy. These techniques can be performed on an already-trained float TensorFlow model and applied during TensorFlow Lite conversion.

详细了解 post-培训 model quantization here