我在Python中通过kerasapi使用TensorFlow创建了一个神经网络,它利用了ResNet50
预训练网络,能够对133种不同品种的狗进行分类。在
我现在希望能够部署此模型,以便可以通过TensorFlow.js,但是我在让ResNet50
工作时遇到了困难。我能把我从头创建的神经网络转移到TensorFlow.js没有问题,但是使用预先训练过的网络传输一个并不那么简单。在
下面是我尝试修改的Python代码:
from keras.applications.resnet50 import ResNet50
ResNet50_model = ResNet50(weights="imagenet") # download ImageNet challenge weights
def extractResNet50(tensor): # tensor shape is (1, 224, 224, 3)
return ResNet50(weights='imagenet', include_top=False, pooling="avg").predict(preprocess_input(tensor))
def dogBreed(img_path):
tensor = convertToTensor(img_path) # I can do this in TF.js already with no issue
resnetTensor = extractResNet50(tensor) # tensor returned in shape (1, 2048)
resnetTensor = np.expand_dims(resnetTensor, axis=0) # repeat this line 2 more times to get shape (1, 1, 1, 1, 2048)
# code below I can convert to TF.js without issue
prediction = model.predict(resnetTensor[0])
除了dogBreed()
的第1行和第4行代码之外,我如何将上面的所有内容转换为TensorFlow.js?在
Resnet是一个如此庞大的网络,它还没有被导入浏览器,我怀疑它是否有一天会被导入。至少对于最新版本的tensorflowJs(版本0.14)
另一方面,您可以保存pythonkeras模型,然后将冻结的模型导入Js进行预测。在
更新: 您正在使用resnet50作为模型的功能提取器。在这种情况下,要保存的冻结模型需要同时包含Resnet50和模型拓扑和权重。在
1-不要在python中使用两个独立的体系结构,而是直接使用tensorflow而不是keras来创建一个网络。然后冻结的模型将包含Resnet。这在浏览器中可能无法正常工作,因为Resnet的大小相当大(我自己还没有测试过)
2-不要在浏览器中使用Resnet,而是考虑使用可以在浏览器中用作特征提取程序的}。您可以在official repo上看到如何使用它们
coco-ssd
或{相关问题 更多 >
编程相关推荐