uniapp项目tensorflowjs中mobilenet的load方法失败,请问各位牛佬们应该怎么解决啊
不知道你这个问题是否已经解决, 如果还没有解决的话:# -*- 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