成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

深夜趕工:CNN神經(jīng)網(wǎng)絡(luò)做彩色圖像識別,用以測試天價核彈

kelvinlee / 2707人閱讀

摘要:此服務(wù)器搭載了塊顯卡,是目前頂級的深度學(xué)習(xí)計算卡,單卡售價萬,整機售價接近萬,天價核彈,有錢真好。此神經(jīng)網(wǎng)絡(luò)參考了的圖像識別項目,采用了模型,增加了函數(shù)以擴充數(shù)據(jù)集。在天價核彈上會是個什么情況呢明天去試試看咯

在圖像識別的道路越走越遠?( ?? ω ?? )y

1.解釋一下

深夜腦子不是很清楚,大部分代碼參考了github……
此CNN圖像識別神經(jīng)網(wǎng)絡(luò)的用途是之后用來評估NVIDIA-DGX服務(wù)器的性能,因此盡量擴大網(wǎng)絡(luò)的訓(xùn)練時間。
此服務(wù)器搭載了8塊NVIDIA TESLA V100顯卡,是目前頂級的深度學(xué)習(xí)計算卡,單卡售價102萬RMB,整機售價接近1000萬,天價核彈,有錢真好。根據(jù)網(wǎng)上的信息,此服務(wù)器可在8小時內(nèi)完成titanX 8天的工作量,頂級民用cpu數(shù)個月工作量。

此神經(jīng)網(wǎng)絡(luò)參考了GITHUB的圖像識別項目,采用了DenseNet模型,增加了ImageDataGenerator函數(shù)以擴充數(shù)據(jù)集。打算后續(xù)通過改變常量epoch的值在各個平臺進行運算。

由于深夜倉促,尚未完成GPU的配置,因此把epoch設(shè)置為1先在CPU上跑跑試試,通過經(jīng)驗估計在GTX1080上所需的時間。

2.數(shù)據(jù)集說明
該訓(xùn)練采用cifar10數(shù)據(jù)集,包含60000張32x32像素的彩色圖片,這些圖片分屬不同的類別,如圖所示:

具體說明參考多倫多大學(xué)官網(wǎng):http://www.cs.toronto.edu/~kr...

此網(wǎng)絡(luò)的目的是盡量精確地通過圖像識別將圖片分類到自己所屬類別當(dāng)中。

下載數(shù)據(jù)集后直接改名后放入user.kerasdatasets文件夾中:

解壓后可發(fā)現(xiàn),數(shù)據(jù)集分成6個batch,其中5個為訓(xùn)練集,1個為測試集:

3.深夜倉促,直接上代碼:

導(dǎo)入第三方庫(numpy/keras/math):

import numpy as np
import keras
import math
from keras.datasets import cifar10
from keras.preprocessing.image import ImageDataGenerator
from keras.layers.normalization import BatchNormalization
from keras.layers import Conv2D, Dense, Input, add, Activation, AveragePooling2D, GlobalAveragePooling2D
from keras.layers import Lambda, concatenate
from keras.initializers import he_normal
from keras.layers.merge import Concatenate
from keras.callbacks import LearningRateScheduler, TensorBoard, ModelCheckpoint
from keras.models import Model
from keras import optimizers
from keras import regularizers
from keras.utils.vis_utils import plot_model as plot

設(shè)置常量:

growth_rate        = 12 
depth              = 100
compression        = 0.5

img_rows, img_cols = 32, 32           #圖片尺寸
img_channels       = 3                #圖片色彩通道數(shù),RGB
num_classes        = 10               #數(shù)據(jù)集類別數(shù)量
batch_size         = 64               #訓(xùn)練batch所包含的example數(shù)量,只能是64或者32
epochs             = 1                #全數(shù)據(jù)集迭代次數(shù),這里打算用cpu運算一次。
                                      #根據(jù)測試的顯卡和自己的要求改epoch數(shù)量
                                      #當(dāng)epoch數(shù)量為250時識別效果較好,但這里不考慮效果

iterations         = 782              #每一次epoch的步數(shù)
weight_decay       = 0.0001

mean = [125.307, 122.95, 113.865]
std  = [62.9932, 62.0887, 66.7048]

根迭代次數(shù)改變scheduler,越迭代到后面該值越小,這意味著希望訓(xùn)練過程中隨機因素逐步減?。?/em>

def scheduler(epoch):
    if epoch <= 100:
       return 0.1
    if epoch <= 180:
       return 0.01
    return 0.0005

定義一個DenseNet模型(github搬運工上線!):

def densenet(img_input,classes_num):

    def bn_relu(x):
        x = BatchNormalization()(x)
        x = Activation("relu")(x)
        return x

    def bottleneck(x):
        channels = growth_rate * 4
        x = bn_relu(x)
        x = Conv2D(channels,kernel_size=(1,1),strides=(1,1),padding="same",kernel_initializer=he_normal(),kernel_regularizer=regularizers.l2(weight_decay),use_bias=False)(x)
        x = bn_relu(x)
        x = Conv2D(growth_rate,kernel_size=(3,3),strides=(1,1),padding="same",kernel_initializer=he_normal(),kernel_regularizer=regularizers.l2(weight_decay),use_bias=False)(x)
        return x

    def single(x):
        x = bn_relu(x)
        x = Conv2D(growth_rate,kernel_size=(3,3),strides=(1,1),padding="same",kernel_initializer=he_normal(),kernel_regularizer=regularizers.l2(weight_decay),use_bias=False)(x)
        return x

    def transition(x, inchannels):
        x = bn_relu(x)
        x = Conv2D(int(inchannels * compression),kernel_size=(1,1),strides=(1,1),padding="same",kernel_initializer=he_normal(),kernel_regularizer=regularizers.l2(weight_decay),use_bias=False)(x)
        x = AveragePooling2D((2,2), strides=(2, 2))(x)
        return x

    def dense_block(x,blocks,nchannels):
        concat = x
        for i in range(blocks):
            x = bottleneck(concat)
            concat = concatenate([x,concat], axis=-1)
            nchannels += growth_rate
        return concat, nchannels

    def dense_layer(x):
        return Dense(classes_num,activation="softmax",kernel_initializer=he_normal(),kernel_regularizer=regularizers.l2(weight_decay))(x)


    # nblocks = (depth - 4) // 3 
    nblocks = (depth - 4) // 6 
    nchannels = growth_rate * 2

    x = Conv2D(nchannels,kernel_size=(3,3),strides=(1,1),padding="same",kernel_initializer=he_normal(),kernel_regularizer=regularizers.l2(weight_decay),use_bias=False)(img_input)

    x, nchannels = dense_block(x,nblocks,nchannels)
    x = transition(x,nchannels)
    x, nchannels = dense_block(x,nblocks,nchannels)
    x = transition(x,nchannels)
    x, nchannels = dense_block(x,nblocks,nchannels)
    x = bn_relu(x)
    x = GlobalAveragePooling2D()(x)
    x = dense_layer(x)
    return x

載入數(shù)據(jù)集,并對標簽進行矩陣設(shè)置,改變數(shù)據(jù)集數(shù)據(jù)類型:

(x_train, y_train), (x_test, y_test) = cifar10.load_data()
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test  = keras.utils.to_categorical(y_test, num_classes)
x_train = x_train.astype("float32")
x_test  = x_test.astype("float32")

將數(shù)據(jù)集歸一化,方便訓(xùn)練:

for i in range(3):
    x_train[:,:,:,i] = (x_train[:,:,:,i] - mean[i]) / std[i]
    x_test[:,:,:,i] = (x_test[:,:,:,i] - mean[i]) / std[i]

定義模型并打印簡圖,shell中打印的模型圖太長了,就不貼了,長得一逼,需要看的話直接在shell中print summary就可以:

img_input = Input(shape=(img_rows,img_cols,img_channels))
output    = densenet(img_input,num_classes)
model     = Model(img_input, output)
# model.load_weights("ckpt.h5")
print(model.summary())
plot(model, to_file="cnn_model.png",show_shapes=True)

這個模型的參數(shù)情況如下圖所示。圖像識別的問題就是這點麻煩,參數(shù)太多了,大批求導(dǎo),怪不得天價核彈這么貴還這么有市場:

本質(zhì)上還是一個分類問題,使用交叉熵作為損失函數(shù),定義輸出結(jié)果的好壞:

sgd = optimizers.SGD(lr=.1, momentum=0.9, nesterov=True)
model.compile(loss="categorical_crossentropy", optimizer=sgd, metrics=["accuracy"])

設(shè)定回饋:

tb_cb     = TensorBoard(log_dir="./densenet/", histogram_freq=0)
change_lr = LearningRateScheduler(scheduler)
ckpt      = ModelCheckpoint("./ckpt.h5", save_best_only=False, mode="auto", period=10)
cbks      = [change_lr,tb_cb,ckpt]

添加上數(shù)據(jù)集擴充功能,對圖像做一些彈性變換,比如水平翻轉(zhuǎn),垂直翻轉(zhuǎn),旋轉(zhuǎn):

print("Using real-time data augmentation.")
datagen   = ImageDataGenerator(horizontal_flip=True,width_shift_range=0.125,height_shift_range=0.125,fill_mode="constant",cval=0.)

datagen.fit(x_train)

訓(xùn)練模型:

model.fit_generator(datagen.flow(x_train, y_train,batch_size=batch_size), steps_per_epoch=iterations, epochs=epochs, callbacks=cbks,validation_data=(x_test, y_test))
model.save("densenet.h5")

訓(xùn)練過程cpu(i7-7820hk)滿載:

在cpu上進行一次訓(xùn)練需要將近10000秒:

根據(jù)之前手寫數(shù)字文本識別模型的經(jīng)驗(cpu需要12秒,gtx1080只需要0.47秒,gpu是cpu性能的25.72倍),把本程序的epoch改到2500,則gtx1080需要大概270小時。

在v100天價核彈上會是個什么情況呢?明天去試試看咯!

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/40764.html

相關(guān)文章

  • 深度卷積神經(jīng)網(wǎng)絡(luò)演化歷史及結(jié)構(gòu)改進脈絡(luò)-40頁長文全面解讀

    早期成果卷積神經(jīng)網(wǎng)絡(luò)是各種深度神經(jīng)網(wǎng)絡(luò)中應(yīng)用最廣泛的一種,在機器視覺的很多問題上都取得了當(dāng)前較好的效果,另外它在自然語言處理,計算機圖形學(xué)等領(lǐng)域也有成功的應(yīng)用。第一個真正意義上的卷積神經(jīng)網(wǎng)絡(luò)由LeCun在1989年提出[1],后來進行了改進,它被用于手寫字符的識別,是當(dāng)前各種深度卷積神經(jīng)網(wǎng)絡(luò)的鼻祖。接下來我們介紹LeCun在早期提出的3種卷積網(wǎng)絡(luò)結(jié)構(gòu)。?文獻[1]的網(wǎng)絡(luò)由卷積層和全連接層構(gòu)成,網(wǎng)絡(luò)...

    xiaodao 評論0 收藏0
  • 【機器學(xué)習(xí)基礎(chǔ)】卷積神經(jīng)網(wǎng)絡(luò)CNN)基礎(chǔ)

    摘要:而在卷積神經(jīng)網(wǎng)絡(luò)中,這兩個神經(jīng)元可以共用一套參數(shù),用來做同一件事情。卷積神經(jīng)網(wǎng)絡(luò)的基本結(jié)構(gòu)卷積神經(jīng)網(wǎng)絡(luò)的基本結(jié)構(gòu)如圖所示從右到左,輸入一張圖片卷積層池化層卷積層池化層展開全連接神經(jīng)網(wǎng)絡(luò)輸出。最近幾天陸續(xù)補充了一些線性回歸部分內(nèi)容,這節(jié)繼續(xù)機器學(xué)習(xí)基礎(chǔ)部分,這節(jié)主要對CNN的基礎(chǔ)進行整理,僅限于基礎(chǔ)原理的了解,更復(fù)雜的內(nèi)容和實踐放在以后再進行總結(jié)。卷積神經(jīng)網(wǎng)絡(luò)的基本原理  前面對全連接神經(jīng)網(wǎng)絡(luò)...

    番茄西紅柿 評論0 收藏2637
  • 圖像到知識:深度神經(jīng)網(wǎng)絡(luò)實現(xiàn)圖像理解的原理解析

    摘要:本文將詳細解析深度神經(jīng)網(wǎng)絡(luò)識別圖形圖像的基本原理。卷積神經(jīng)網(wǎng)絡(luò)與圖像理解卷積神經(jīng)網(wǎng)絡(luò)通常被用來張量形式的輸入,例如一張彩色圖象對應(yīng)三個二維矩陣,分別表示在三個顏色通道的像素強度。 本文將詳細解析深度神經(jīng)網(wǎng)絡(luò)識別圖形圖像的基本原理。針對卷積神經(jīng)網(wǎng)絡(luò),本文將詳細探討網(wǎng)絡(luò) 中每一層在圖像識別中的原理和作用,例如卷積層(convolutional layer),采樣層(pooling layer),...

    UnixAgain 評論0 收藏0
  • 一文讀懂 CNN、DNN、RNN 內(nèi)部網(wǎng)絡(luò)結(jié)構(gòu)區(qū)別

    摘要:在普通的全連接網(wǎng)絡(luò)或中,每層神經(jīng)元的信號只能向上一層傳播,樣本的處理在各個時刻獨立,因此又被成為前向神經(jīng)網(wǎng)絡(luò)。不難想象隨著深度學(xué)習(xí)熱度的延續(xù),更靈活的組合方式更多的網(wǎng)絡(luò)結(jié)構(gòu)將被發(fā)展出來。 從廣義上來說,NN(或是更美的DNN)確實可以認為包含了CNN、RNN這些具體的變種形式。在實際應(yīng)用中,所謂的深度神經(jīng)網(wǎng)絡(luò)DNN,往往融合了多種已知的結(jié)構(gòu),包括卷積層或是LSTM單元。這里的DNN特指全連接...

    cheng10 評論0 收藏0

發(fā)表評論

0條評論

最新活動
閱讀需要支付1元查看
<