pix是什么意思(pixio)
本文會(huì)介紹cGAN和pix2pix,并在 TensorFlow 中使用 pix2pix 模型。
一、cGAN原理
使用GAN可以無(wú)監(jiān)督生成全新的圖片,比如使用GAN生成MNIST數(shù)字,雖然可以生成數(shù)字,但是不能生成確定的數(shù)字。如果希望控制生成的結(jié)果,例如生成數(shù)字1,此時(shí)就要用到cGAN了。
cGAN 的全稱為 Conditional Generative Adversarial Networks, 即條件對(duì)抗生成網(wǎng) 絡(luò),它為生成器、判別器都額外加入了一個(gè)條件 y, 這個(gè)條件實(shí)際是希望生成的標(biāo)簽 。 生成器 G 必須要生成和條件 y 匹配的樣本,判別器不僅要判別圖像是否真實(shí),還要判別圖像和條件y是否匹配。cGAN的輸入輸出為:
(1)生成器 G, 輸入一個(gè)噪聲 z, 一個(gè)條件 y,輸出符合該條件的圖像 G(z[y)。
(2)判別器 D ,輸入一張圖像、一個(gè)條件 y,輸出該圖像在該條件下的真實(shí)概率 D(x[y)。
cGAN損失定義:在GAN的優(yōu)化目標(biāo)中加入條件y,即:
二、pix2pix模型的原理
在自然語(yǔ)言處理領(lǐng)域,機(jī)器翻譯,也就是將中文譯成英文,對(duì)應(yīng)的,在圖像領(lǐng)域,也有圖像翻譯。例如:將白天的圖片轉(zhuǎn)換為夜晚的圖片、將街景的標(biāo)注圖像變?yōu)檎鎸?shí)圖片。使用pix2pix可以處理這類問(wèn)題,模型結(jié)構(gòu)如下:
它是一種特殊的cGAN,設(shè)要將 Y類型的圖像轉(zhuǎn)換為 X類型的圖像, G、 D 的任務(wù)分別為 :
(1)G 的輸入是一個(gè) Y類圖像y (條件y) ,輸出為生成圖像 G(y)。
(2)D 的輸入為一個(gè)X類圖像x,一個(gè) y類圖像y。D需要判斷x圖像是否是真正的y對(duì)應(yīng)的圖像,并輸出一個(gè)概率。
模型損失定義為:L1+cGAN。實(shí)驗(yàn)中發(fā)現(xiàn),在生成圖像和真實(shí)圖像中加入L1/L2損失,可以加速模型收斂以及提高準(zhǔn)確率。
在pix2pix2提出一種PatchGAN的思想:PatchGAN 對(duì)圖片中每個(gè) NxN 的小塊計(jì)算概率, 然后再將這些概率求平均值作為整體的輸出,這樣做可以加快計(jì)算速度以及加快收斂。
三、TensorFlow中的pix2pix模型
1、執(zhí)行已有數(shù)據(jù)集
Facades數(shù)據(jù)集包含了建筑的外觀圖像和建筑的標(biāo)注 。 建筑的標(biāo)注同樣是圖像形式,用不同顏色的色塊表示不同的類別。Facades 數(shù)據(jù)集將建筑外觀分為墻壁、窗戶、門、檐口等 12 個(gè)類別。下載Facades 數(shù)據(jù)集:python tools/download-dataset.py facades
所有的樣本圖像都是兩張圖片拼接起來(lái)的,訓(xùn)練時(shí),可以將A類圖像翻譯成B類圖像。本例將標(biāo)注圖像生成真實(shí)圖像,運(yùn)行命令:
python pix2pix.py --mode train # 表示從頭訓(xùn)練模型 test表示用已有模型測(cè)試 --output_dir facades_train # 保存模型的位置 --max_epochs 200 # epoch數(shù) --input_dir facades/train/ # 訓(xùn)練數(shù)據(jù) --which_direction BtoA # 翻譯方向
訓(xùn)練結(jié)束后,使用命令進(jìn)行測(cè)試:
python pix2pix.py --mode test # 用已有模型測(cè)試 --output_dir facades_test # 保存所有圖片的測(cè)試結(jié)果 --input_dir facades/val # 訓(xùn)練數(shù)據(jù) --checkpoint facades_train # 之前保存模型的位置,表示從此處恢復(fù)模型
執(zhí)行測(cè)試后,在 facades_test 文件夾下,會(huì)產(chǎn)生一個(gè) index.html 文件 。 打開(kāi)后可以看到一個(gè)可視化展示生成結(jié)果的網(wǎng)頁(yè)。
2、創(chuàng)建自己的數(shù)據(jù)集
通過(guò)程序,將訓(xùn)練數(shù)據(jù)也整理為之前所說(shuō)的 A、 B 圖像并列排列的形式,用對(duì)應(yīng)的指令進(jìn)行訓(xùn)練和測(cè)試,相應(yīng)代碼在process.py文件中。
(1)將圖片縮放到同樣的大小
def resize(src):
height, width, _ = src.shape
dst = src
if height != width:
if a.pad:
size = max(height, width)
# pad to correct ratio
oh = (size - height) // 2
ow = (size - width) // 2
dst = im.pad(image=dst, offset_height=oh, offset_width=ow, target_height=size, target_width=size)
else:
# crop to correct ratio
size = min(height, width)
oh = (height - size) // 2
ow = (width - size) // 2
dst = im.crop(image=dst, offset_height=oh, offset_width=ow, target_height=size, target_width=size)
assert(dst.shape[0] == dst.shape[1])
size, _, _ = dst.shape
if size > a.size:
dst = im.downscale(images=dst, size=[a.size, a.size])
elif size < a.size:
dst = im.upscale(images=dst, size=[a.size, a.size])
return dst
(2)轉(zhuǎn)換圖像井合并
對(duì) A 類圖像做某種操作以生成對(duì)應(yīng)的 B 類圖像,并將轉(zhuǎn)煥后的圖像合起來(lái)變成一個(gè)訓(xùn)練樣本。比如將A圖像挖去一部分成為B圖像,再合并。
# 填充部分圖像為空白
def blank(src):
height, width, _ = src.shape
if height != width:
raise Exception("non-square image")
image_size = width
size = int(image_size * 0.3)
offset = int(image_size / 2 - size / 2)
dst = src
dst[offset:offset + size,offset:offset + size,:] = np.ones([size, size, 3])
return dst
# 合并
def combine(src, src_path):
if a.b_dir is None:
raise Exception("missing b_dir")
# find corresponding file in b_dir, could have a different extension
basename, _ = os.path.splitext(os.path.basename(src_path))
for ext in [".png", ".jpg"]:
sibling_path = os.path.join(a.b_dir, basename + ext)
if os.path.exists(sibling_path):
sibling = im.load(sibling_path)
break
else:
raise Exception("could not find sibling image for " + src_path)
# make sure that dimensions are correct
height, width, _ = src.shape
if height != sibling.shape[0] or width != sibling.shape[1]:
raise Exception("differing sizes")
# convert both images to RGB if necessary
if src.shape[2] == 1:
src = im.grayscale_to_rgb(images=src)
if sibling.shape[2] == 1:
sibling = im.grayscale_to_rgb(images=sibling)
# remove alpha channel
if src.shape[2] == 4:
src = src[:,:,:3]
if sibling.shape[2] == 4:
sibling = sibling[:,:,:3]
return np.concatenate([src, sibling], axis=1)
(3)分割數(shù)據(jù)集,把數(shù)據(jù)集分割為訓(xùn)練集和驗(yàn)證集
總結(jié)
以上是生活随笔為你收集整理的pix是什么意思(pixio)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 原型工具 墨刀_原型设计工具 axure
- 下一篇: srvctl命令_clc命令