2020-09-29 kubeflow pipeline 如何掛載PVC肩榕,并在組件之間傳遞文件

參考資料

https://ubuntu.com/blog/data-science-workflows-on-kubernetes-with-kubeflow-pipelines-part-2?

https://www.tensorflow.org/tutorials/keras/classification

https://github.com/manceps/fashion-mnist-kfp-lab

正文

如標(biāo)題所示动猬,本文闡述在kubeflow的pipeline組件中如何創(chuàng)建pvc,并在pipeline的流水線中傳遞文件腔寡。

眾所周知奈揍,pipeline制定了從數(shù)據(jù)到模型的整個(gè)流程,那么紊册,按照一個(gè)嚴(yán)謹(jǐn)?shù)拇a農(nóng)思維比肄,我們應(yīng)該這么準(zhǔn)備:

1、創(chuàng)建存儲(chǔ)的地方

2囊陡、準(zhǔn)備數(shù)據(jù)

3芳绩、訓(xùn)練模型并輸出

4、測(cè)試模型

而且這四步應(yīng)該發(fā)生在不同的容器中关斜,即指定不同的鏡像示括。

好了,閑話不說(shuō)痢畜,開(kāi)始干垛膝!

用的例子是最基礎(chǔ)的例子鳍侣,fashion mnist

直接上最終的代碼:

import kfp

import kfp.dsl as dsl

import kfp.components as comp

def train(data_path, model_file):

? ? # func_to_container_op requires packages to be imported inside of the function.

? ? import pickle

? ? import tensorflow as tf

? ? from tensorflow.python import keras

? ? # Download the dataset and split into training and test data.

? ? fashion_mnist = keras.datasets.fashion_mnist

? ? (train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()

? ? # Normalize the data so that the values all fall between 0 and 1.

? ? train_images = train_images / 255.0

? ? test_images = test_images / 255.0

? ? # Define the model using Keras.

? ? model = keras.Sequential([

? ? keras.layers.Flatten(input_shape=(28, 28)),

? ? keras.layers.Dense(128, activation='relu'),

? ? keras.layers.Dense(10)

? ? ])

? ? model.compile(optimizer='adam',

? ? ? ? ? ? ? ? ? loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True),

? ? ? ? ? ? ? ? ? metrics=['accuracy'])

? ? # Run a training job with specified number of epochs

? ? model.fit(train_images, train_labels, epochs=10)

? ? # Evaluate the model and print the results

? ? test_loss, test_acc = model.evaluate(test_images,? test_labels, verbose=2)

? ? print('Test accuracy:', test_acc)

? ? # Save the model to the designated

? ? model.save(f'{data_path}/{model_file}')

? ? # Save the test_data as a pickle file to be used by the predict component.

? ? with open(f'{data_path}/test_data', 'wb') as f:

? ? ? ? pickle.dump((test_images,test_labels), f)

def predict(data_path, model_file, image_number):

? ? # func_to_container_op requires packages to be imported inside of the function.

? ? import pickle

? ? import tensorflow as tf

? ? from tensorflow import keras

? ? import numpy as np

? ? # Load the saved Keras model

? ? model = keras.models.load_model(f'{data_path}/{model_file}')

? ? # Load and unpack the test_data

? ? with open(f'{data_path}/test_data','rb') as f:

? ? ? ? test_data = pickle.load(f)

? ? # Separate the test_images from the test_labels.

? ? test_images, test_labels = test_data

? ? # Define the class names.

? ? class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat',

? ? ? ? ? ? ? ? ? 'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']

? ? # Define a Softmax layer to define outputs as probabilities

? ? probability_model = tf.keras.Sequential([model,

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? tf.keras.layers.Softmax()])

? ? # See https://github.com/kubeflow/pipelines/issues/2320 for explanation on this line.

? ? image_number = int(image_number)

? ? # Grab an image from the test dataset.

? ? img = test_images[image_number]

? ? # Add the image to a batch where it is the only member.

? ? img = (np.expand_dims(img,0))

? ? # Predict the label of the image.

? ? predictions = probability_model.predict(img)

? ? # Take the prediction with the highest probability

? ? prediction = np.argmax(predictions[0])

? ? # Retrieve the true label of the image from the test labels.

? ? true_label = test_labels[image_number]

? ? class_prediction = class_names[prediction]

? ? confidence = 100*np.max(predictions)

? ? actual = class_names[true_label]

? ? with open(f'{data_path}/result.txt', 'w') as result:

? ? ? ? result.write(" Prediction: {} | Confidence: {:2.0f}% | Actual: {}".format(class_prediction,

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? confidence,

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? actual))

? ? print('Prediction has be saved successfully!')


# Define the pipeline

@dsl.pipeline(

? name='Fashion MNIST Pipeline',

? description='A toy pipeline that performs fashion mnist model training and prediction.')

# Define parameters to be fed into pipeline

def mnist_container_pipeline(

? ? data_path='/mnt',

? ? model_file='mnist_model_0929.h5',

? ? image_number=0):

? ? train_op = comp.func_to_container_op(train, base_image='0.0.0.0/library/tensorflow:2.3.0-gpu')

? ? predict_op = comp.func_to_container_op(predict, base_image='0.0.0.0/library/tensorflow:2.3.0-gpu')

? ? # Define volume to share data between components.

? ? vop = dsl.VolumeOp(

? ? name="create_volume",

? ? resource_name="data-volume",

? ? size="1Gi",

? ? modes=dsl.VOLUME_MODE_RWM)

? ? # Create MNIST training component.

? ? mnist_training_container = train_op(data_path, model_file) \

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? .add_pvolumes({data_path: vop.volume})

? ? # Create MNIST prediction component.

? ? mnist_predict_container = predict_op(data_path, model_file, image_number) \

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? .add_pvolumes({data_path: mnist_training_container.pvolume})

? ? # Print the result of the prediction

? ? mnist_result_container = dsl.ContainerOp(

? ? ? ? name="print_prediction",

? ? ? ? image='0.0.0.0/library/bash:4.4.23',

? ? ? ? pvolumes={data_path: mnist_predict_container.pvolume},

? ? ? ? arguments=['cat', f'{data_path}/result.txt']

? ? )

if __name__ == '__main__':

? ? import kfp.compiler as compiler

? ? compiler.Compiler().compile(mnist_container_pipeline, __file__ + '.tar.gz')

代碼解釋:

前面定義了train、predict兩個(gè)函數(shù)吼拥,這個(gè)就不說(shuō)了倚聚,是我們的主要執(zhí)行代碼

后面就是一個(gè)mnist_container_pipeline,這個(gè)流程制定了整個(gè)工作流程1凿可、創(chuàng)建pvc? 2惑折、訓(xùn)練? 3、 測(cè)試? 4枯跑、打印輸出

其中第1步創(chuàng)建的pvc會(huì)被后面3步用到惨驶,第2步訓(xùn)練產(chǎn)生的模型文件和用于第3步測(cè)試的數(shù)據(jù)都會(huì)被保存在pvc中,第3步從pvc中讀取數(shù)據(jù)并進(jìn)行測(cè)試敛助,測(cè)試結(jié)果寫(xiě)在txt中粗卜,依舊保存在pvc中,第四步從pvc中讀取txt并進(jìn)行輸出纳击。

完畢续扔!

編譯命令:dsl-compile --py? pipeline.py --output pipeline.tar.gz? ? 然后上傳執(zhí)行即可

持續(xù)學(xué)習(xí)中。焕数。纱昧。

希望能給大家?guī)?lái)幫助。

分享知識(shí)堡赔,分享進(jìn)步

歡迎大家分享交流识脆。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市加匈,隨后出現(xiàn)的幾起案子存璃,更是在濱河造成了極大的恐慌,老刑警劉巖雕拼,帶你破解...
    沈念sama閱讀 217,657評(píng)論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異粘招,居然都是意外死亡啥寇,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,889評(píng)論 3 394
  • 文/潘曉璐 我一進(jìn)店門洒扎,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)辑甜,“玉大人,你說(shuō)我怎么就攤上這事袍冷×状祝” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 164,057評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵胡诗,是天一觀的道長(zhǎng)邓线。 經(jīng)常有香客問(wèn)我淌友,道長(zhǎng),這世上最難降的妖魔是什么骇陈? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,509評(píng)論 1 293
  • 正文 為了忘掉前任震庭,我火速辦了婚禮,結(jié)果婚禮上你雌,老公的妹妹穿的比我還像新娘器联。我一直安慰自己,他們只是感情好婿崭,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,562評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布拨拓。 她就那樣靜靜地躺著,像睡著了一般氓栈。 火紅的嫁衣襯著肌膚如雪渣磷。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 51,443評(píng)論 1 302
  • 那天颤绕,我揣著相機(jī)與錄音幸海,去河邊找鬼。 笑死奥务,一個(gè)胖子當(dāng)著我的面吹牛物独,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播氯葬,決...
    沈念sama閱讀 40,251評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼挡篓,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了帚称?” 一聲冷哼從身側(cè)響起官研,我...
    開(kāi)封第一講書(shū)人閱讀 39,129評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎闯睹,沒(méi)想到半個(gè)月后戏羽,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,561評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡楼吃,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,779評(píng)論 3 335
  • 正文 我和宋清朗相戀三年始花,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片孩锡。...
    茶點(diǎn)故事閱讀 39,902評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡酷宵,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出躬窜,到底是詐尸還是另有隱情浇垦,我是刑警寧澤,帶...
    沈念sama閱讀 35,621評(píng)論 5 345
  • 正文 年R本政府宣布荣挨,位于F島的核電站男韧,受9級(jí)特大地震影響朴摊,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜煌抒,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,220評(píng)論 3 328
  • 文/蒙蒙 一仍劈、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧寡壮,春花似錦贩疙、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,838評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至棒仍,卻和暖如春悲靴,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背莫其。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,971評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工癞尚, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人乱陡。 一個(gè)月前我還...
    沈念sama閱讀 48,025評(píng)論 2 370
  • 正文 我出身青樓浇揩,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親憨颠。 傳聞我的和親對(duì)象是個(gè)殘疾皇子胳徽,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,843評(píng)論 2 354