1. 程式人生 > >1.CNN圖片單標籤分類(基於TensorFlow實現基礎VGG16網路)

1.CNN圖片單標籤分類(基於TensorFlow實現基礎VGG16網路)

本文所使用的開源資料集(kaggle貓狗大戰):

www.kaggle.com/c/dogs-vs-c…

國內百度網盤下載地址:

pan.baidu.com/s/12ab32UNY…

利用本文程式碼訓練並生成的模型(對應專案中的model資料夾):

pan.baidu.com/s/1tBkVQKoH…

簡單介紹:

(需要預先安裝pip install opencv-python, pip install flask, pip install tensorflow/pip install tensorflow-gpu) 本文使用Python3,TensorFlow實現適合新手的VGG16模型(不瞭解VGG16的同學可以自行百度一下,本文沒有使用slim或者keras實現,對VGG16逐層實現,便於新手理解,有經驗的同學可以用高階庫重寫這部分)可應用於單標籤分類(一張圖片要麼是貓,要麼是狗)任務。

預告:之後會寫一篇多標籤分類任務,與單標籤分類有些區別 juejin.im/post/5c073b…

整體訓練邏輯:

0,使用pipeline方式非同步讀取訓練集圖片,節省記憶體消耗,提高效率

1,將影象傳入到CNN(VGG16)中提取特徵

2,將特徵圖拉伸輸入到FC layer中得出分類預測向量

3,通過softmax交叉熵函式對預測向量和標籤向量進行訓練,得出最終模型

整體預測邏輯:

1,將影象傳入到CNN(VGG16)中提取特徵

2,將特徵圖拉伸輸入到FC layer中得出分類預測向量

3,將預測向量做softmax操作,取向量中的最大值,並對映到對應類別中

製作成web服務:

利用flask框架將整個專案啟動成web服務,使得專案支援http方式呼叫

啟動服務後呼叫以下地址測試:

http://127.0.0.1:5050/dogOrCat?img_path=./data/test1/1.jpg

http://127.0.0.1:5050/dogOrCat?img_path=./data/test1/5.jpg

後續優化邏輯:

可以採用遷移學習,模型融合等方案進一步提高acc

可以左右翻轉圖片,將訓練集翻倍

執行命令:

對資料集進行訓練:python DogVsCat.py train

對新的圖片進行測試:python DogVsCat.py test

啟動成http服務:python

DogVsCat.py start

專案整體目錄結構:

專案結構
model結構:
model資料夾結構

訓練過程:

epoch-1
epoch-2
epoch-3
epoch-13
epoch-13-中段
epoch-13-結束

整體程式碼如下:

# coding:utf-8

import tensorflow as tf
import os, sys, random
import numpy as np
import cv2

from flask import request
from flask import Flask
import json
app = Flask(__name__)


class DogVsCat:
    def __init__(self):
        # 可調引數
        self.save_epoch = 1  # 每相隔多少個epoch儲存一次模型
        self.train_max_num = 25000  # 訓練時讀取的最大圖片數目 0~25000之間,記憶體不足的可以調小
        self.epoch_max = 13  # 最大迭代epoch次數
        self.batch_size = 16  # 訓練時每個批次參與訓練的影象數目,視訊記憶體不足的可以調小
        self.class_num = 2  # 分類數目,貓狗共兩類
        self.val_num = 20 * self.batch_size  # 不能大於self.train_max_num  做驗證集用
        self.lr = 1e-4  # 初始學習率

        # 無需修改引數
        self.x_val = []
        self.y_val = []
        self.x = None  # 每批次的影象資料
        self.y = None  # 每批次的one-hot標籤
        self.learning_rate = None  # 學習率
        self.sess = None  # 持久化的tf.session
        self.pred = None  # cnn網路結構的預測
        self.keep_drop = tf.placeholder(tf.float32)  # dropout比例


    def dogOrCat(self, img_path):
        """
        貓狗分類
        :param img_path:
        :return:
        """
        im = cv2.imread(img_path)
        im = cv2.resize(im, (224, 224))
        im = [im]
        im = np.array(im, dtype=np.float32)
        im -= 147
        output = self.sess.run(self.output, feed_dict={self.x: im, self.keep_drop: 1.})
        ret = output.tolist()[0]
        ret = 'It is a cat' if ret[0] <= ret[1] else 'It is a dog'
        return ret


    def test(self, img_path):
        """
        測試介面
        :param img_path:
        :return:
        """
        self.x = tf.placeholder(tf.float32, [None, 224, 224, 3])  # 輸入資料
        self.pred = self.CNN()
        self.output = tf.nn.softmax(self.pred)

        saver = tf.train.Saver()
        # tfconfig = tf.ConfigProto(allow_soft_placement=True)
        # tfconfig.gpu_options.per_process_gpu_memory_fraction = 0.3  # 佔用視訊記憶體的比例
        # self.ses = tf.Session(config=tfconfig)
        self.sess = tf.Session()
        self.sess.run(tf.global_variables_initializer())  # 全域性tf變數初始化

        # 載入w,b引數
        saver.restore(self.sess, './model/DogVsCat-13')
        im = cv2.imread(img_path)
        im = cv2.resize(im, (224, 224))
        im = [im]
        im = np.array(im, dtype=np.float32)
        im -= 147
        output = self.sess.run(self.output, feed_dict={self.x: im, self.keep_drop: 1.})
        ret = output.tolist()[0]
        ret = 'It is a cat' if ret[0] <= ret[1] else 'It is a dog'
        print(ret)


    def train(self):
        """
        開始訓練
        :return: 
        """
        self.x = tf.placeholder(tf.float32, [None, 224, 224, 3])  # 輸入資料
        self.y = tf.placeholder(tf.float32, [None, self.class_num])  # 標籤資料
        self.learning_rate = tf.placeholder(tf.float32)  # 學習率

        # 生成訓練用資料集
        x_train_list, y_train_list, x_val_list, y_val_list = self.getTrainDataset()

        print('開始轉換tensor佇列')
        x_train_list_tensor = tf.convert_to_tensor(x_train_list, dtype=tf.string)
        y_train_list_tensor = tf.convert_to_tensor(y_train_list, dtype=tf.float32)

        x_val_list_tensor = tf.convert_to_tensor(x_val_list, dtype=tf.string)
        y_val_list_tensor = tf.convert_to_tensor(y_val_list, dtype=tf.float32)

        x_train_queue = tf.train.slice_input_producer(tensor_list=[x_train_list_tensor], shuffle=False)
        y_train_queue = tf.train.slice_input_producer(tensor_list=[y_train_list_tensor], shuffle=False)

        x_val_queue = tf.train.slice_input_producer(tensor_list=[x_val_list_tensor], shuffle=False)
        y_val_queue = tf.train.slice_input_producer(tensor_list=[y_val_list_tensor], shuffle=False)

        train_im, train_label = self.dataset_opt(x_train_queue, y_train_queue)
        train_batch = tf.train.batch(tensors=[train_im, train_label], batch_size=self.batch_size, num_threads=2)

        val_im, val_label = self.dataset_opt(x_val_queue, y_val_queue)
        val_batch = tf.train.batch(tensors=[val_im, val_label], batch_size=self.batch_size, num_threads=2)


        # VGG16網路
        print('開始載入網路')
        self.pred = self.CNN()

        # 損失函式
        self.loss = tf.nn.softmax_cross_entropy_with_logits(logits=self.pred, labels=self.y)

        # 優化器
        self.opt = tf.train.AdamOptimizer(learning_rate=self.learning_rate).minimize(self.loss)

        # acc
        self.acc_tf = tf.equal(tf.argmax(self.pred, 1), tf.argmax(self.y, 1))
        self.acc = tf.reduce_mean(tf.cast(self.acc_tf, tf.float32))

        with tf.Session() as self.sess:
            # 全域性tf變數初始化
            self.sess.run(tf.global_variables_initializer())
            coordinator = tf.train.Coordinator()
            threads = tf.train.start_queue_runners(sess=self.sess, coord=coordinator)

            # 模型儲存
            saver = tf.train.Saver()

            batch_max = len(x_train_list) // self.batch_size
            total_step = 1
            for epoch_num in range(self.epoch_max):
                lr = self.lr * (1 - (epoch_num/self.epoch_max) ** 2)  # 動態學習率
                for batch_num in range(batch_max):
                    x_train_tmp, y_train_tmp = self.sess.run(train_batch)
                    self.sess.run(self.opt, feed_dict={self.x: x_train_tmp, self.y: y_train_tmp, self.learning_rate: lr, self.keep_drop: 0.5})

                    # 輸出評價標準
                    if total_step % 20 == 0 or total_step == 1:
                        print()
                        print('epoch:%d/%d batch:%d/%d step:%d lr:%.10f' % ((epoch_num + 1), self.epoch_max, (batch_num + 1), batch_max, total_step, lr))

                        # 輸出訓練集評價
                        train_loss, train_acc = self.sess.run([self.loss, self.acc], feed_dict={self.x: x_train_tmp, self.y: y_train_tmp, self.keep_drop: 1.})
                        print('train_loss:%.10f  train_acc:%.10f' % (np.mean(train_loss), train_acc))

                        # 輸出驗證集評價
                        val_loss_list, val_acc_list = [], []
                        for i in range(int(self.val_num/self.batch_size)):
                            x_val_tmp, y_val_tmp = self.sess.run(val_batch)
                            val_loss, val_acc = self.sess.run([self.loss, self.acc], feed_dict={self.x: x_val_tmp, self.y: y_val_tmp, self.keep_drop: 1.})
                            val_loss_list.append(np.mean(val_loss))
                            val_acc_list.append(np.mean(val_acc))
                        print('  val_loss:%.10f    val_acc:%.10f' % (np.mean(val_loss), np.mean(val_acc)))

                    total_step += 1

                # 儲存模型
                if (epoch_num + 1) % self.save_epoch == 0:
                    print('正在儲存模型:')
                    saver.save(self.sess, './model/DogVsCat', global_step=(epoch_num + 1))
            coordinator.request_stop()
            coordinator.join(threads)


    def CNN(self):
        """
        VGG16 + FC
        :return: 
        """
        # 權重
        weight = {
            # 輸入 batch_size*224*224*3

            # 第一層
            'wc1_1': tf.get_variable('wc1_1', [3, 3, 3, 64]),  # 卷積 輸出:batch_size*224*224*64
            'wc1_2': tf.get_variable('wc1_2', [3, 3, 64, 64]),  # 卷積 輸出:batch_size*224*224*64
            # 池化 輸出:112*112*64

            # 第二層
            'wc2_1': tf.get_variable('wc2_1', [3, 3, 64, 128]),  # 卷積 輸出:batch_size*112*112*128
            'wc2_2': tf.get_variable('wc2_2', [3, 3, 128, 128]),  # 卷積 輸出:batch_size*112*112*128
            # 池化 輸出:56*56*128

            # 第三層
            'wc3_1': tf.get_variable('wc3_1', [3, 3, 128, 256]),  # 卷積 輸出:batch_size*56*56*256
            'wc3_2': tf.get_variable('wc3_2', [3, 3, 256, 256]),  # 卷積 輸出:batch_size*56*56*256
            'wc3_3': tf.get_variable('wc3_3', [3, 3, 256, 256]),  # 卷積 輸出:batch_size*56*56*256
            # 池化 輸出:28*28*256

            # 第四層
            'wc4_1': tf.get_variable('wc4_1', [3, 3, 256, 512]),  # 卷積 輸出:batch_size*28*28*512
            'wc4_2': tf.get_variable('wc4_2', [3, 3, 512, 512]),  # 卷積 輸出:batch_size*28*28*512
            'wc4_3': tf.get_variable('wc4_3', [3, 3, 512, 512]),  # 卷積 輸出:batch_size*28*28*512
            # 池化 輸出:14*14*512

            # 第五層
            'wc5_1': tf.get_variable('wc5_1', [3, 3, 512, 512]),  # 卷積 輸出:batch_size*14*14*512
            'wc5_2': tf.get_variable('wc5_2', [3, 3, 512, 512]),  # 卷積 輸出:batch_size*14*14*512
            'wc5_3': tf.get_variable('wc5_3', [3, 3, 512, 512]),  # 卷積 輸出:batch_size*14*14*512
            # 池化 輸出:7*7*512

            # 全連結第一層
            'wfc_1': tf.get_variable('wfc_1', [7*7*512, 4096]),

            # 全連結第二層
            'wfc_2': tf.get_variable('wfc_2', [4096, 4096]),

            # 全連結第三層
            'wfc_3': tf.get_variable('wfc_3', [4096, self.class_num]),
        }

        # 偏移量
        biase = {
            # 第一層
            'bc1_1': tf.get_variable('bc1_1', [64]),
            'bc1_2': tf.get_variable('bc1_2', [64]),

            # 第二層
            'bc2_1': tf.get_variable('bc2_1', [128]),
            'bc2_2': tf.get_variable('bc2_2', [128]),

            # 第三層
            'bc3_1': tf.get_variable('bc3_1', [256]),
            'bc3_2': tf.get_variable('bc3_2', [256]),
            'bc3_3': tf.get_variable('bc3_3', [256]),

            # 第四層
            'bc4_1': tf.get_variable('bc4_1', [512]),
            'bc4_2': tf.get_variable('bc4_2', [512]),
            'bc4_3': tf.get_variable('bc4_3', [512]),

            # 第五層
            'bc5_1': tf.get_variable('bc5_1', [512]),
            'bc5_2': tf.get_variable('bc5_2', [512]),
            'bc5_3': tf.get_variable('bc5_3', [512]),

            # 全連結第一層
            'bfc_1': tf.get_variable('bfc_1', [4096]),

            # 全連結第二層
            'bfc_2': tf.get_variable('bfc_2', [4096]),

            # 全連結第三層
            'bfc_3': tf.get_variable('bfc_3', [self.class_num]),
        }

        # 第一層
        net = tf.nn.conv2d(input=self.x, filter=weight['wc1_1'], strides=[1, 1, 1, 1], padding='SAME')  # 卷積
        net = tf.nn.leaky_relu(tf.nn.bias_add(net, biase['bc1_1']))  # 加b 然後 啟用
        net = tf.nn.conv2d(net, filter=weight['wc1_2'], strides=[1, 1, 1, 1], padding='SAME')  # 卷積
        net = tf.nn.leaky_relu(tf.nn.bias_add(net, biase['bc1_2']))  # 加b 然後 啟用
        net = tf.nn.max_pool(value=net, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='VALID')  # 池化

        # 第二層
        net = tf.nn.conv2d(net, weight['wc2_1'], [1, 1, 1, 1], padding='SAME')  # 卷積
        net = tf.nn.leaky_relu(tf.nn.bias_add(net, biase['bc2_1']))  # 加b 然後 啟用
        net = tf.nn.conv2d(net, weight['wc2_2'], [1, 1, 1, 1], padding='SAME')  # 卷積
        net = tf.nn.leaky_relu(tf.nn.bias_add(net, biase['bc2_2']))  # 加b 然後 啟用
        net = tf.nn.max_pool(net, [1, 2, 2, 1], [1, 2, 2, 1], padding='VALID')  # 池化

        # 第三層
        net = tf.nn.conv2d(net, weight['wc3_1'], [1, 1, 1, 1], padding='SAME')  # 卷積
        net = tf.nn.leaky_relu(tf.nn.bias_add(net, biase['bc3_1']))  # 加b 然後 啟用
        net = tf.nn.conv2d(net, weight['wc3_2'], [1, 1, 1, 1], padding='SAME')  # 卷積
        net = tf.nn.leaky_relu(tf.nn.bias_add(net, biase['bc3_2']))  # 加b 然後 啟用
        net = tf.nn.conv2d(net, weight['wc3_3'], [1, 1, 1, 1], padding='SAME')  # 卷積
        net = tf.nn.leaky_relu(tf.nn.bias_add(net, biase['bc3_3']))  # 加b 然後 啟用
        net = tf.nn.max_pool(net, [1, 2, 2, 1], [1, 2, 2, 1], padding='VALID')  # 池化

        # 第四層
        net = tf.nn.conv2d(net, weight['wc4_1'], [1, 1, 1, 1], padding='SAME')  # 卷積
        net = tf.nn.leaky_relu(tf.nn.bias_add(net, biase['bc4_1']))  # 加b 然後 啟用
        net = tf.nn.conv2d(net, weight['wc4_2'], [1, 1, 1, 1], padding='SAME')  # 卷積
        net = tf.nn.leaky_relu(tf.nn.bias_add(net, biase['bc4_2']))  # 加b 然後 啟用
        net = tf.nn.conv2d(net, weight['wc4_3'], [1, 1, 1, 1], padding='SAME')  # 卷積
        net = tf.nn.leaky_relu(tf.nn.bias_add(net, biase['bc4_3']))  # 加b 然後 啟用
        net = tf.nn.max_pool(net, [1, 2, 2, 1], [1, 2, 2, 1], padding='VALID')  # 池化

        # 第五層
        net = tf.nn.conv2d(net, weight['wc5_1'], [1, 1, 1, 1], padding='SAME')  # 卷積
        net = tf.nn.leaky_relu(tf.nn.bias_add(net, biase['bc5_1']))  # 加b 然後 啟用
        net = tf.nn.conv2d(net, weight['wc5_2'], [1, 1, 1, 1], padding='SAME')  # 卷積
        net = tf.nn.leaky_relu(tf.nn.bias_add(net, biase['bc5_2']))  # 加b 然後 啟用
        net = tf.nn.conv2d(net, weight['wc5_3'], [1, 1, 1, 1], padding='SAME')  # 卷積
        net = tf.nn.leaky_relu(tf.nn.bias_add(net, biase['bc5_3']))  # 加b 然後 啟用
        net = tf.nn.max_pool(net, [1, 2, 2, 1], [1, 2, 2, 1], padding='VALID')  # 池化
        print('last-net', net)

        # 拉伸flatten,把多個圖片同時分別拉伸成一條向量
        net = tf.reshape(net, shape=[-1, weight['wfc_1'].get_shape()[0]])
        print(weight['wfc_1'].get_shape()[0])
        print('拉伸flatten', net)

        # 全連結層
        # fc第一層
        net = tf.matmul(net, weight['wfc_1']) + biase['bfc_1']
        net = tf.nn.dropout(net, self.keep_drop)
        net = tf.nn.relu(net)
        print('fc第一層', net)

        # fc第二層
        net = tf.matmul(net, weight['wfc_2']) + biase['bfc_2']
        net = tf.nn.dropout(net, self.keep_drop)
        net = tf.nn.relu(net)
        print('fc第二層', net)

        # fc第三層
        net = tf.matmul(net, weight['wfc_3']) + biase['bfc_3']
        print('fc第三層', net)
        return net


    def getTrainDataset(self):
        """
        整理資料集,把影象resize為224*224*3,訓練集做成25000*224*224*3,把label做成one-hot形式
        :return: 
        """
        train_data_list = os.listdir('./data/train_data/')
        print('共有%d張訓練圖片, 讀取%d張:' % (len(train_data_list), self.train_max_num))
        random.shuffle(train_data_list)  # 打亂順序

        x_val_list = train_data_list[:self.val_num]
        y_val_list = [[0, 1] if file_name.find('cat') > -1 else [1, 0] for file_name in x_val_list]

        x_train_list = train_data_list[self.val_num:self.train_max_num]
        y_train_list = [[0, 1] if file_name.find('cat') > -1 else [1, 0] for file_name in x_train_list]

        return x_train_list, y_train_list, x_val_list, y_val_list


    def dataset_opt(self, x_train_queue, y_train_queue):
        """
        處理圖片和標籤
        :param queue: 
        :return: 
        """
        queue = x_train_queue[0]
        contents = tf.read_file('./data/train_data/' + queue)
        im = tf.image.decode_jpeg(contents)
        im = tf.image.resize_images(images=im, size=[224, 224])
        im = tf.reshape(im, tf.stack([224, 224, 3]))
        im -= 147  # 去均值化
        # im /= 255  # 將畫素處理在0~1之間,加速收斂
        # im -= 0.5  # 將畫素處理在-0.5~0.5之間
        return im, y_train_queue[0]



if __name__ == '__main__':
    opt_type = sys.argv[1:][0]

    instance = DogVsCat()

    if opt_type == 'train':
        instance.train()
    elif opt_type == 'test':
        instance.test('./data/test1/1.jpg')
    elif opt_type == 'start':
        # 將session持久化到記憶體中
        instance.test('./data/test1/1.jpg')
        # 啟動web服務
        # http://127.0.0.1:5050/dogOrCat?img_path=./data/test1/1.jpg
        @app.route('/dogOrCat', methods=['GET', 'POST'])
        def dogOrCat():
            img_path = ''
            if request.method == 'POST':
                img_path = request.form.to_dict().get('img_path')
            elif request.method == 'GET':
                # img_path = request.args.get('img_path')
                img_path = request.args.to_dict().get('img_path')
            print(img_path)
            ret = instance.dogOrCat(img_path)
            print(ret)
            return json.dumps({'type': ret})
        app.run(host='0.0.0.0', port=5050, debug=False)
複製程式碼