tensorflow-Inception3

总结:

https://blog.csdn.net/weixin_43624538/article/details/84963116

https://www.cnblogs.com/eniac1946/p/8669937.html

代码参考:

https://blog.csdn.net/shankezh/article/details/89088085

设计原则

  • 2.1避免特征表示瓶颈,尤其是在网络的前面。要避免严重压缩导致的瓶颈。特征表示尺寸应该温和的减少,从输入端到输出端。特征表示的维度只是一个粗浅的信息量表示,它丢掉了一些重要的因素如相关性结构。
  • 2.2高纬信息更适合在网络的局部处理。在卷积网络中逐步增加非线性激活响应可以解耦合更多的特征,那么网络就会训练的更快。
  • 2.3空间聚合可以通过低纬嵌入,不会导致网络表示能力的降低。例如在进行大尺寸的卷积(如3*3)之前,我们可以在空间聚合前先对输入信息进行降维处理,如果这些信号是容易压缩的,那么降维甚至可以加快学习速度。
  • 2.4平衡好网络的深度和宽度。通过平衡网络每层滤波器的个数和网络的层数可以是网络达到最佳性能。增加网络的宽度和深度都会提升网络的性能,但是两者并行增加获得的性能提升是最大的。
    import tensorflow as tf
    import numpy as np
    from sklearn.model_selection import train_test_split
    import tensorflow.contrib.slim as slim
    
    class InceptionV3(object):
        def __init__(self):
            self.n_input = 299*299*3
            self.n_classes = 10
            self.batch_size = 10
            self.training_iters = 20
            self.display_step = 20
            self.learning_rate = 0.001
    
    
        def conv2d(self,x,filters,k_size,stride=[1,1],padding='SAME',activation = tf.nn.relu,scope='conv2d'):
            return tf.layers.conv2d(inputs=x,filters=filters,kernel_size=k_size,
                                    strides=stride,padding=padding,name=scope,activation=activation)
    
        def maxpool2d(self,x,pool_size=[2,2],stride=[2,2],padding='same',scope='conv2d'):
            return tf.layers.max_pooling2d(inputs=x,pool_size=pool_size,strides=stride,padding=padding,name=scope)
    
        def dropoutx(self,x,d_rate):
            return tf.layers.dropout(x,rate=d_rate)
    
        def avgpool2d(self,x,pool_size=[1,1],stride=[1,1],padding='same',scope='avgpool2d'):
            return tf.layers.average_pooling2d(x,pool_size=pool_size,strides=stride,padding=padding,name=scope)
    
        def inception_module_v3_1(self,x,filter_num,stride=[1,1],scope='inception_moudle_v3_1'):
            with tf.variable_scope(scope):
                with tf.variable_scope("bh1"):
                    bh1 = self.conv2d(x,filters=filter_num[0],k_size=[1,1],stride=stride,scope='bh1_conv1_1x1')
                with tf.variable_scope("bh2"):
                    bh2 = self.avgpool2d(x,pool_size=[1,1],stride=stride,scope='bh2_avg_3x3')
                    bh2 = self.conv2d(bh2,filters=filter_num[2],k_size=[3,3],stride=stride,scope='bh2_conv_1x1')
                with tf.variable_scope("bh3"):
                    bh3 = self.conv2d(x,filters=filter_num[3],k_size=[1,1],stride=stride,scope='bh3_conv1_1x1')
                    bh3 = self.conv2d(bh3,filters=filter_num[4],k_size=[5,5],stride=stride,scope='bh3_conv2_3x3')
                with tf.variable_scope("bh4"):
                    bh4 = self.conv2d(x,filters=filter_num[4],k_size=[1,1],stride=stride,scope='bh4_conv1_1x1')
                    bh4 = self.conv2d(bh4,filters=filter_num[5],k_size=[3,3],stride=stride,scope='bh4_conv2_3x3')
                    bh4 = self.conv2d(bh4,filters=filter_num[6],k_size=[3,3],stride=stride,scope='bh4_conv3_3x3')
                return tf.concat([bh1,bh2,bh3,bh4],axis=3)
    
    
        def inception_moudle_v3_2(self,net, scope, filter_num, stride=[1,1]):
            with tf.variable_scope(scope):
                with tf.variable_scope("bh1"):
                    bh1 = self.conv2d(net, filters=filter_num[0], k_size=[1, 1], stride=stride, scope="bh1_conv_1x1")
                with tf.variable_scope("bh2"):
                    bh2 = self.avgpool2d(net, [3, 3], stride=stride, scope='bh2_avg_3x3')
                    bh2 = self.conv2d(bh2, filter_num[1], k_size=[1, 1], stride=stride, scope='bh2_conv_1x1')
                with tf.variable_scope("bh3"):
                    bh3 = self.conv2d(net, filter_num[2], k_size=[1, 1], stride=stride, scope='bh3_conv1_1x1')
                    bh3 = self.conv2d(bh3, filter_num[3], k_size=[1, 7], stride=stride, scope='bh3_conv2_1x7')
                    bh3 = self.conv2d(bh3, filter_num[4], k_size=[7, 1], stride=stride, scope='bh3_conv3_7x1')
                with tf.variable_scope("bh4"):
                    bh4 = self.conv2d(net, filter_num[5], [1, 1], stride=stride, scope='bh4_conv1_1x1')
                    bh4 = self.conv2d(bh4, filter_num[6], [1, 7], stride=stride, scope='bh4_conv2_1x7')
                    bh4 = self.conv2d(bh4, filter_num[7], [7, 1], stride=stride, scope='bh4_conv3_7x1')
                    bh4 = self.conv2d(bh4, filter_num[8], [1, 7], stride=stride, scope='bh4_conv4_1x7')
                    bh4 = self.conv2d(bh4, filter_num[9], [7, 1], stride=stride, scope='bh4_conv5_7x1')
                net = tf.concat([bh1, bh2, bh3, bh4], axis=3)
            return net
    
        def inception_moudle_v3_3(self,net, scope, filter_num, stride=[1,1]):
            with tf.variable_scope(scope):
                with tf.variable_scope("bh1"):
                    bh1 = self.conv2d(net, filter_num[0], [1, 1], stride=stride, scope='bh1_conv_1x1')
                with tf.variable_scope("bh2"):
                    bh2 = self.avgpool2d(net, [3, 3], stride=stride, scope='bh2_avg_3x3')
                    bh2 = self.conv2d(bh2, filter_num[1], [1, 1], stride=stride, scope='bh2_conv_1x1')
                with tf.variable_scope("bh3"):
                    bh3 = self.conv2d(net, filter_num[2], [1, 1], stride=stride, scope='bh3_conv1_1x1')
                    bh3_1 = self.conv2d(bh3, filter_num[3], [3, 1], stride=stride, scope='bh3_conv2_3x1')
                    bh3_2 = self.conv2d(bh3, filter_num[4], [1, 3], stride=stride, scope='bh3_conv2_1x3')
                with tf.variable_scope("bh4"):
                    bh4 = self.conv2d(net, filter_num[5], [1, 1], stride=stride, scope='bh4_conv1_1x1')
                    bh4 = self.conv2d(bh4, filter_num[6], [3, 3], stride=stride, scope='bh4_conv2_3x3')
                    bh4_1 = self.conv2d(bh4, filter_num[7], [3, 1], stride=stride, scope='bh4_conv3_3x1')
                    bh4_2 = self.conv2d(bh4, filter_num[8], [1, 3], stride=stride, scope='bh4_conv3_1x3')
                net = tf.concat([bh1, bh2, bh3_1, bh3_2, bh4_1, bh4_2], axis=3)
            return net
    
        def inception_moudle_v3_reduce(self,net, scope, filter_num):
            with tf.variable_scope(scope):
                with tf.variable_scope("bh1"):
                    bh1 = self.avgpool2d(net, [3, 3], stride=[2,2], padding='VALID', scope="bh1_max_3x3")
                with tf.variable_scope("bh2"):
                    bh2 = self.conv2d(net, filter_num[0], [1, 1], stride=[1,1], scope='bh2_conv1_1x1')
                    bh2 = self.conv2d(bh2, filter_num[1], [3, 3], stride=[2,2], padding='VALID', scope='bh2_conv2_3x3')
                with tf.variable_scope("bh3"):
                    bh3 = self.conv2d(net, filter_num[2], [1, 1], stride=[1,1], scope='bh3_conv1_1x1')
                    bh3 = self.conv2d(bh3, filter_num[3], [3, 3], stride=[1,1], scope='bh3_conv2_3x3')
                    bh3 = self.conv2d(bh3, filter_num[4], [3, 3], stride=[2,2], padding='VALID', scope='bh3_conv3_3x3')
                net = tf.concat([bh1, bh2, bh3], axis=3)
            return net
    
        def set_net(self,x,keep_prob,is_train=True,spatital_squeeze=True):
            check_point = {}
            net = tf.reshape(x,[-1,299,299,3])
    
            net = self.conv2d(net, 32, [3, 3], stride=[2,2], scope="layer1")  # 149x149
            net = self.conv2d(net, 32, [3, 3], scope='layer2')  # 147x147
            net = self.conv2d(net, 64, [3, 3], padding='SAME', scope='layer3')  # 147x147
            net = self.maxpool2d(net, [3, 3], stride=[2,2],scope='layer4')  # 73x73
            net = self.conv2d(net, 80, [3, 3], scope='layer5')  # 71x71
            net = self.conv2d(net, 192, [3, 3], stride=[2,2],scope='layer6')  # 35x35
    
            net = self.conv2d(net, 288, [3, 3], scope='layer7')
            # 3 x inception
            net = self.inception_module_v3_1(net, scope='layer8', filter_num=[64, 32, 48, 64, 64, 96, 96])  # 35x35
            net = self.inception_module_v3_1(net, scope='layer11', filter_num=[64, 64, 48, 64, 64, 96, 96])
            net = self.inception_module_v3_1(net, scope='layer14', filter_num=[64, 64, 48, 64, 64, 96, 96])
            print('1111',net)
            # 5 x inception
            net = self.inception_moudle_v3_reduce(net, scope='layer17', filter_num=[192, 384, 64, 96, 96])  # 17x17
            print('2222',net)
            net = self.inception_moudle_v3_2(net, scope='layer20', filter_num=[192, 192, 128, 128, 192, 128, 128, 128, 128, 192])
            net = self.inception_moudle_v3_2(net, scope='layer25', filter_num=[192, 192, 160, 160, 192, 160, 160, 160, 160, 192])
            net = self.inception_moudle_v3_2(net, scope='layer30', filter_num=[192, 192, 160, 160, 192, 160, 160, 160, 160, 192])
            net = self.inception_moudle_v3_2(net, scope='layer35', filter_num=[192, 192, 160, 160, 192, 160, 160, 160, 160, 192])
            print(net)
            # 3 x inception
            net = self.inception_moudle_v3_reduce(net, scope='layer40', filter_num=[192, 320, 192, 192, 192])  # 8x8
            net = self.inception_moudle_v3_3(net, scope='layer43', filter_num=[320, 192, 384, 384, 384, 448, 384, 384, 384])
            net = self.inception_moudle_v3_3(net, scope='layer46', filter_num=[320, 192, 384, 384, 384, 448, 384, 384, 384])
            print(net)
            net = self.avgpool2d(net, [8, 8], padding='VALID', scope='layer49')
            net = slim.dropout(net)
            net = slim.conv2d(net, self.n_classes, [1, 1], activation_fn=None, normalizer_fn=None, scope='layer50')
            print(net)
            if spatital_squeeze:
                net = tf.squeeze(net, [1, 2], name='squeeze')
            net = slim.softmax(net, scope='softmax')
            return net
    
        def inceptionv1_prediction(self, X,Y, scope='vgg'):
            X_train, X_vaild, y_train, y_vaild = train_test_split(X, Y, test_size=0.2)
            x = tf.placeholder(tf.float32, [None, self.n_input])
            y = tf.placeholder(tf.float32, [None, self.n_classes])
    
            pred = self.set_net(x,0.8)  # pred是计算完的值,此时还没归一化
            a = tf.nn.softmax(pred)  # a是归一化后的值。
    
            # 定义损失函数和学习步骤
            cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=pred, labels=y))  # 这个是损失loss
            optimizer = tf.train.AdamOptimizer(learning_rate=self.learning_rate).minimize(cost)  # 最小化loss
            # 测试网络
            correct_pred = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1))
            accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))
            # 初始化所有的共享变量
            init = tf.initialize_all_variables()
            with tf.Session() as sess:
                sess.run(init)
                step = 1
                # Keep training until reach max iterations
                while step * self.batch_size < self.training_iters:  # 直到达到最大迭代次数,没考虑梯度!!!
                    batch_xs, batch_ys = X_train[self.batch_size*(step-1):self.batch_size*step],y_train[self.batch_size*(step-1):self.batch_size*step]
    
                    batch_xs = np.reshape(batch_xs,(-1,self.n_input))
                    print('~!!!!!!!!',batch_xs.shape)
                    print(batch_ys.shape)
                    # 获取批数据
                    sess.run(optimizer, feed_dict={x: batch_xs, y: batch_ys})
                    if step % self.display_step == 0:  # 每一步里有64batch,64*20=1280
                        # 计算精度
                        acc = sess.run(accuracy, feed_dict={x: batch_xs, y: batch_ys})
                        # 计算损失值
                        loss = sess.run(cost, feed_dict={x: batch_xs, y: batch_ys})
                        print("Iter " + str(step * self.batch_size) + ", Minibatch Loss= " + "{:.6f}".format(
                            loss) + ", Training Accuracy = " + "{:.5f}".format(acc))
                    step += 1
                print("Optimization Finished!")
                # 计算测试精度
                X_vaild = np.reshape(X_vaild, (-1, self.n_input))
                print("Testing Accuracy:", sess.run(accuracy,
                                                    feed_dict={x: X_vaild[:256], y: y_vaild[:256],
                                                               }))  # 拿前256个来测试
                print("Testing Result:", sess.run(a, feed_dict={x: X_vaild[63:64], y: y_vaild[63:64],
                                                                }))  # 数组范围,从0开始,含左不含右
    
                print(y_vaild[63:64])
    
    

     

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章