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

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

模型概述app

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

代碼及效果示例:ide

import paddlehub as hub學習

import matplotlib.pyplot as pltspa

import matplotlib.image as mpimg3d

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

test_img_path = "./body2.jpg"blog

# 預測結果展現圖片

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

相關文章
相關標籤/搜索