mobilenet.load失败

img

img

uniapp项目tensorflowjs中mobilenet的load方法失败,请问各位牛佬们应该怎么解决啊

不知道你这个问题是否已经解决, 如果还没有解决的话:
  • 这篇博客: MobileNet V2 tensorflow复现中的 代码细节: 部分也许能够解决你的问题, 你可以仔细阅读以下内容或者直接跳转源博客中阅读:
    # -*- coding: utf-8 -*-
    """
    Created on Wed Apr  8 11:33:35 2020
    
    @author: Ke
    """
    
    # -*- coding: utf-8 -*-
    """
    Created on Wed Apr  8 11:31:50 2020
    
    @author: Ke
    """
    import tensorflow as tf
    import matplotlib.pyplot as plt
    import cv2
    import numpy as np
    
    class MobileNet(object):
        def __init__(self, inputs, num_classes=8, is_training=True,
                     width_multiplier=1, scope="MobileNet_v2"):
            self.inputs = inputs
            self.num_classes = num_classes
            self.is_training = is_training
            self.width_multiplier = width_multiplier
     
            with tf.variable_scope(scope):
                self.predictions = self.forward(inputs)
                
                
        def forward(self,img_input):#224*224*3
            
            conv1 = self.Conv2D('convlayer1',img_input,3,3,32,2) #输出大小112*112*32
            
            #只有步长为1并且输入维度等于输出时,才使用残差
            b1_1=self.BottleNeck('bottlelayer1_1',conv1,32,1,16,1,False) #输出大小112*112*16,一层不使用残差
            
            
            b2_1=self.BottleNeck('bottlelayer2_1',b1_1,16,6,24,2,False)#输出大小56*56*24
            b2_2=self.BottleNeck('bottlelayer2_2',b2_1,24,6,24,1,True)#输出大小56*56*24
            
            b3_1=self.BottleNeck('bottlelayer3_1',b2_2,24,6,32,2,False)#输出大小28*28*32
            b3_2=self.BottleNeck('bottlelayer3_2',b3_1,32,6,32,1,True)#输出大小28*28*32
            b3_3=self.BottleNeck('bottlelayer3_3',b3_2,32,6,32,1,True)#输出大小28*28*32  
            
            b4_1=self.BottleNeck('bottlelayer4_1',b3_3,32,6,64,2,False)#输出大小14*14*64
            b4_2=self.BottleNeck('bottlelayer4_2',b4_1,64,6,64,1,True)#输出大小14*14*64
            b4_3=self.BottleNeck('bottlelayer4_3',b4_2,64,6,64,1,True)#输出大小14*14*64        
            b4_4=self.BottleNeck('bottlelayer4_4',b4_3,64,6,64,1,True)#输出大小14*14*64 
            
            b5_1=self.BottleNeck('bottlelayer5_1',b4_4,64,6,96,1,False)#输出大小14*14*96
            b5_2=self.BottleNeck('bottlelayer5_2',b5_1,96,6,96,1,True)#输出大小14*14*96
            b5_3=self.BottleNeck('bottlelayer5_3',b5_2,96,6,96,1,True)#输出大小14*14*96 
              
            b6_1=self.BottleNeck('bottlelayer6_1',b5_3,96,6,160,2,False)#输出大小7*7*160
            b6_2=self.BottleNeck('bottlelayer6_2',b6_1,160,6,160,1,True)#输出大小7*7*160
            b6_3=self.BottleNeck('bottlelayer6_3',b6_2,160,6,160,1,True)#输出大小7*7*160 
            
            b7_1=self.BottleNeck('bottlelayer7_1',b6_3,160,6,320,1,False)#输出大小7*7*320
            
            conv8 = self.Conv2D('convlayer8_1',b7_1,1,320,1280,1) #输出大小7*7*1280
            
            pool9_1 = tf.reduce_mean(conv8, [1,2], name='global_pool', keep_dims=True)#输出大小1*1*1280
        
            conv10_1 = self.Conv2D('convlayer10_1',pool9_1,1,1280,self.num_classes,1) #输出大小1*1*8
            
            result=tf.squeeze(conv10_1)#输出大小8
            
            return result   
        
        #定义两种不同的卷积:普通卷积和深度卷积
        def Conv2D(self,name, inputs, kernel_size, in_channels, out_channels, stride):
            with tf.variable_scope(name, reuse=tf.AUTO_REUSE):
                #tf.get_variable如果已经创建的变量对象,就把那个对象返回,如果没有创建变量对象的话,就创建一个新的。
                kernel = tf.get_variable('weight',shape=[kernel_size,kernel_size,in_channels,out_channels],
                                         initializer=tf.truncated_normal_initializer(mean=0.0, stddev=1.0, dtype=tf.float32))
                conv = tf.nn.conv2d(inputs, kernel, [1,stride,stride,1], padding='SAME')
                biases = tf.get_variable('biase',initializer=tf.constant(0.01, shape=[out_channels],
                                                                 dtype=tf.float32))
                output = tf.nn.bias_add(conv, biases)
                
                output_bn = tf.layers.batch_normalization(output, name='bn',
                                                          training=self.is_training)
                
                return output_bn
            
        def DepthwiseConv2D(self,name,inputs,channels,stride):
            with tf.variable_scope(name, reuse=tf.AUTO_REUSE):
                
                kernel = tf.get_variable('weight',shape=[3,3,channels,self.width_multiplier],
                                    initializer=tf.truncated_normal_initializer(mean=0.0, stddev=1.0, dtype=tf.float32))
                
                
                conv = tf.nn.depthwise_conv2d(inputs, kernel,[1,stride,stride,1],rate=[1,1], padding='SAME')
                #偏置为channels*channel_multiplier
                biases = tf.get_variable('biase',initializer=tf.constant(0.01, shape=[channels*self.width_multiplier],
                                                                         dtype=tf.float32))
                output = tf.nn.bias_add(conv, biases)
                
                output_bn = tf.layers.batch_normalization(output, name='bn',training=self.is_training)
    
                return tf.nn.relu6(output_bn)
        
        #瓶颈块
        def BottleNeck(self,name,inputs,in_channels,t,out_channels,stride,r=False):
            '''
            参数r:表示是否用残差(只有当步长为1并且输入维度等于输出维度时才用)
            '''
            with tf.variable_scope(name, reuse=tf.AUTO_REUSE):
                
                expand_dim=t*in_channels
                conv1 = self.Conv2D('conv1',inputs,1,in_channels,expand_dim,1) #1*1卷积升维,步长为1
                output = tf.nn.relu6(conv1)
    
                dwconv=self.DepthwiseConv2D('dw_conv',output,expand_dim,stride)#深度卷积
    
                outputs = self.Conv2D('conv3',dwconv,1,expand_dim,out_channels,1) #点卷积 
                
                if r:
                    outputs = tf.add(outputs, inputs)
                return outputs
    
  • 以下回答来自chatgpt: 很抱歉,作为AI助手,我不能使用第三方库加载模型。建议您首先检查以下几点: 1. 检查模型文件的路径是否正确,并确保模型文件存在。 2. 检查是否有其他的JS脚本文件同时使用了tensorflowjs,可能会导致加载冲突。 3. 检查您的网络连接是否正常,有没有被防火墙等安全软件限制了。如果是内网环境,请确认模型文件是否可以访问。 4. 如果您使用的是最新版本的tensorflowjs,可以尝试将代码修改如下: ``` import * as mobilenet from '@tensorflow-models/mobilenet/dist/mobilenet'; const model = await mobilenet.load(); ``` 这里使用了最新版本的mobilenet库,并采用了ES6的import语法。如果您使用的是旧版本的tensorflowjs,建议您升级到最新版本或者使用官方文档中提供的加载方法。如果还是无法解决问题,请联系tensorflowjs官方支持。

如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^