百度AI攻略:Paddlehub實現圖像生成

PaddleHub可以便捷地獲取PaddlePaddle生態下的預訓練模型,完成模型的管理和一鍵預測。配合使用Fine-tune API,可以基於大規模預訓練模型快速完成遷移學習,讓預訓練模型能更好地服務於用戶特定場景的應用。

模型概述

CycleGAN是生成對抗網絡(Generative Adversarial Networks )的一種,與傳統的GAN只能單向生成圖片不同,CycleGAN可以同時完成兩個domain的圖片進行相互轉換。該PaddleHub Module使用Cityscapes數據集訓練完成,支持圖片從實景圖轉換爲語義分割結果,也支持從語義分割結果轉換爲實景圖。

代碼及效果示例:

import paddlehub as hub

import matplotlib.pyplot as plt

import matplotlib.image as mpimg

cyclegan = hub.Module(name="cyclegan_cityscapes")

test_img_path = "./body2.jpg"

# 預測結果展示

img = mpimg.imread(test_img_path)

plt.imshow(img)

plt.axis('off')

plt.show()

# set input dict

input_dict = {"image": [test_img_path]}

# execute predict and print the result

results = cyclegan.generate(data=input_dict)

for result in results:

    print(result)

test_img_path = "./cyclegan_output/body2.jpg"

img = mpimg.imread(test_img_path)

plt.imshow(img)

plt.axis('off')

plt.show()

[2020-01-06 08:47:55,320] [    INFO] - Installing cyclegan_cityscapes module

2020-01-06 08:47:55,320-INFO: Installing cyclegan_cityscapes module

[2020-01-06 08:47:55,353] [    INFO] - Module cyclegan_cityscapes already installed in /home/aistudio/.paddlehub/modules/cyclegan_cityscapes

2020-01-06 08:47:55,353-INFO: Module cyclegan_cityscapes already installed in /home/aistudio/.paddlehub/modules/cyclegan_cityscapes


1240

[2020-01-06 08:47:55,728] [    INFO] - 234 pretrained paramaters loaded by PaddleHub

2020-01-06 08:47:55,728-INFO: 234 pretrained paramaters loaded by PaddleHub

File ./body2.jpg is processed successfully and the result is saved to the cyclegan_output/body2.jpg


1240

In[8]

cyclegan = hub.Module(name="cyclegan_cityscapes")

test_img_path = "./cbd1.jpg"

# 預測結果展示

img = mpimg.imread(test_img_path)

plt.imshow(img)

plt.axis('off')

plt.show()

# set input dict

input_dict = {"image": [test_img_path]}

# execute predict and print the result

results = cyclegan.generate(data=input_dict)

for result in results:

    print(result)

test_img_path = "./cyclegan_output/cbd1.jpg"

img = mpimg.imread(test_img_path)

plt.imshow(img)

plt.axis('off')

plt.show()

[2020-01-06 08:49:16,726] [    INFO] - Installing cyclegan_cityscapes module

2020-01-06 08:49:16,726-INFO: Installing cyclegan_cityscapes module

[2020-01-06 08:49:16,746] [    INFO] - Module cyclegan_cityscapes already installed in /home/aistudio/.paddlehub/modules/cyclegan_cityscapes

2020-01-06 08:49:16,746-INFO: Module cyclegan_cityscapes already installed in /home/aistudio/.paddlehub/modules/cyclegan_cityscapes


1240

[2020-01-06 08:49:17,164] [    INFO] - 234 pretrained paramaters loaded by PaddleHub

2020-01-06 08:49:17,164-INFO: 234 pretrained paramaters loaded by PaddleHub

File ./cbd1.jpg is processed successfully and the result is saved to the cyclegan_output/cbd1.jpg


1240


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