-
-
Save xiangze/d42d7c8e5ae967fba388d5c639365f3a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os, cv2 | |
import os | |
import numpy as np | |
def normalize_x(image): return image/127.5 - 1 | |
def normalize_y(image): return image/255 | |
def denormalize_y(image): image*255 | |
def load_X_files(image_files,IMAGE_SIZE): | |
images = np.zeros((len(image_files), IMAGE_SIZE, IMAGE_SIZE, 3), np.float32) | |
for i, image_file in enumerate(image_files): | |
image = cv2.imread(image_file) | |
image = cv2.resize(image, (IMAGE_SIZE, IMAGE_SIZE)) | |
images[i] = normalize_x(image) | |
return images, image_files | |
def load_X(fdir,IMAGE_SIZE): | |
image_files=os.listdir(fdir).sort() | |
return load_X_flies(image_files,IMAGE_SIZE) | |
def load_Y_files(image_files,IMAGE_SIZE): | |
images = np.zeros((len(image_files), IMAGE_SIZE, IMAGE_SIZE, 1), np.float32) | |
for i, image_file in enumerate(image_files): | |
image = cv2.imread(image_file, cv2.IMREAD_GRAYSCALE) | |
image = cv2.resize(image, (IMAGE_SIZE, IMAGE_SIZE)) | |
image = image[:, :, np.newaxis] | |
images[i] = normalize_y(image) | |
return images | |
def load_Y(fdir,IMAGE_SIZE): | |
image_files = os.listdir(fdir) | |
image_files.sort() | |
return load_Y_files(image_files,IMAGE_SIZE) | |
def segOnehot(y): | |
return y | |
def genVOC2012filelist(ddir,istrain=True,rate=0.9): | |
yfiles=os.listdir(ddir+os.sep+'SegmentationObject' + os.sep) | |
yfiles.sort() | |
xfiles=[ ddir+os.sep+'JPEGImages'+os.sep+os.path.splitext(os.path.basename(y))[0]+".jpg" for y in yfiles] | |
yfiles=[ ddir+os.sep+'SegmentationObject'+os.sep+y for y in yfiles] | |
if(istrain): | |
xfiles=xfiles[:int(rate*len(yfiles))] | |
yfiles=yfiles[:int(rate*len(yfiles))] | |
else: | |
xfiles=xfiles[int((1-rate)*len(yfiles)):] | |
yfiles=yfiles[int((1-rate)*len(yfiles)):] | |
print("yfiles=%d"%len(yfiles)) | |
print("xfiles=%d"%len(xfiles)) | |
return xfiles,yfiles | |
def gettrain(rate=0.9,imsize=256): | |
xfiles,yfiles=genVOC2012filelist(ddir,True,rate) | |
X_train, file_names = load_X_files(xfiles,imsize) | |
Y_train = load_Y_files(yfiles,imsize) | |
return X_train,Y_train | |
def gettest(rate=0.9,imsize=256): | |
xfiles,yfiles=genVOC2012filelist(ddir,False,rate) | |
X_test, file_names = load_X_files(xfiles,imsize) | |
Y_test = load_Y_files(yfiles,imsize) | |
return X_test,Y_test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os, cv2 | |
import os | |
import numpy as np | |
def normalize_x(image): return image/127.5 - 1 | |
def normalize_y(image): return image/255 | |
def denormalize_y(image): image*255 | |
def load_X_files(image_files,IMAGE_SIZE): | |
images = np.zeros((len(image_files), IMAGE_SIZE, IMAGE_SIZE, 3), np.float32) | |
for i, image_file in enumerate(image_files): | |
image = cv2.imread(image_file) | |
image = cv2.resize(image, (IMAGE_SIZE, IMAGE_SIZE)) | |
images[i] = normalize_x(image) | |
return images, image_files | |
def load_X(fdir,IMAGE_SIZE): | |
image_files=os.listdir(fdir).sort() | |
return load_X_flies(image_files,IMAGE_SIZE) | |
def load_Y_files(image_files,IMAGE_SIZE): | |
images = np.zeros((len(image_files), IMAGE_SIZE, IMAGE_SIZE, 1), np.float32) | |
for i, image_file in enumerate(image_files): | |
image = cv2.imread(image_file, cv2.IMREAD_GRAYSCALE) | |
image = cv2.resize(image, (IMAGE_SIZE, IMAGE_SIZE)) | |
image = image[:, :, np.newaxis] | |
images[i] = normalize_y(image) | |
return images | |
def load_Y(fdir,IMAGE_SIZE): | |
image_files = os.listdir(fdir) | |
image_files.sort() | |
return load_Y_files(image_files,IMAGE_SIZE) | |
def segOnehot(y): | |
return y | |
def genVOC2012filelist(ddir,istrain=True,rate=0.9): | |
yfiles=os.listdir(ddir+os.sep+'SegmentationObject' + os.sep) | |
yfiles.sort() | |
xfiles=[ ddir+os.sep+'JPEGImages'+os.sep+os.path.splitext(os.path.basename(y))[0]+".jpg" for y in yfiles] | |
yfiles=[ ddir+os.sep+'SegmentationObject'+os.sep+y for y in yfiles] | |
if(istrain): | |
xfiles=xfiles[:int(rate*len(yfiles))] | |
yfiles=yfiles[:int(rate*len(yfiles))] | |
else: | |
xfiles=xfiles[int((1-rate)*len(yfiles)):] | |
yfiles=yfiles[int((1-rate)*len(yfiles)):] | |
print("yfiles=%d"%len(yfiles)) | |
print("xfiles=%d"%len(xfiles)) | |
return xfiles,yfiles | |
def gettrain(rate=0.9,imsize=256): | |
xfiles,yfiles=genVOC2012filelist(ddir,True,rate) | |
X_train, file_names = load_X_files(xfiles,imsize) | |
Y_train = load_Y_files(yfiles,imsize) | |
return X_train,Y_train | |
def gettest(rate=0.9,imsize=256): | |
xfiles,yfiles=genVOC2012filelist(ddir,False,rate) | |
X_test, file_names = load_X_files(xfiles,imsize) | |
Y_test = load_Y_files(yfiles,imsize) | |
return X_test,Y_test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import numpy as np | |
from keras.optimizers import Adam | |
import keras.backend as K | |
from keras.callbacks import ModelCheckpoint, EarlyStopping | |
import os, cv2 | |
import Unet | |
def normalize_x(image): | |
return image/127.5 - 1 | |
def normalize_y(image): return image/255 | |
def denormalize_y(image): image*255 | |
def load_X_files(image_files,IMAGE_SIZE): | |
images = np.zeros((len(image_files), IMAGE_SIZE, IMAGE_SIZE, 3), np.float32) | |
for i, image_file in enumerate(image_files): | |
image = cv2.imread(fdir + os.sep + image_file) | |
image = cv2.resize(image, (IMAGE_SIZE, IMAGE_SIZE)) | |
images[i] = normalize_x(image) | |
return images, image_files | |
def load_X(fdir,IMAGE_SIZE): | |
image_files=os.listdir(fdir).sort() | |
return load_X_flies(image_files,IMAGE_SIZE) | |
def load_Y_files(image_files,IMAGE_SIZE): | |
images = np.zeros((len(image_files), IMAGE_SIZE, IMAGE_SIZE, 1), np.float32) | |
for i, image_file in enumerate(image_files): | |
image = cv2.imread(dir + os.sep + image_file, cv2.IMREAD_GRAYSCALE) | |
image = cv2.resize(image, (IMAGE_SIZE, IMAGE_SIZE)) | |
image = image[:, :, np.newaxis] | |
images[i] = normalize_y(image) | |
return images | |
def load_Y(fdir,IMAGE_SIZE): | |
image_files = os.listdir(fdir) | |
image_files.sort() | |
return load_Y_files(image_files,IMAGE_SIZE) | |
def train(ddir="VOC2012",rate=0.9): | |
imsize=256 | |
if(ddir=="VOC2012"): | |
yfiles=os.listdir(ddir+os.sep+'SegmentationOjbect' + os.sep) | |
xfiles=[ ddir+os.sep+'JPEGImages'+os.sep+y. for y in yfiles] | |
X_train, file_names = load_X_files(xfiles,imsize) | |
Y_train = load_Y_files(yfiles,imsize) | |
elif(ddir=="PedCut2013"): | |
X_train, file_names = load_X(ddir+os.sep+'trainingData' + os.sep + 'left_images',imsize) | |
Y_train = load_Y(ddir+os.sep+'trainingData' + os.sep + 'left_groundTruth',imsize) | |
inputs= Input((imsize, imsize, 3)) | |
model = Unet.genUnetModel(inputs) | |
model.compile(loss=dice_coef_loss, optimizer=Adam(), metrics=[dice_coef]) | |
BATCH_SIZE = 12 | |
NUM_EPOCH = 20 | |
history = model.fit(X_train, Y_train, batch_size=BATCH_SIZE, epochs=NUM_EPOCH, verbose=1) | |
model.save_weights('segunet_weights.hdf5') | |
def predict(ddir="VOC2012",rate=0.1): | |
imsize=256 | |
if(ddir=="VOC2012"): | |
ddir+os.sep+'JPEGImages'+os.sep,imsize) | |
xfiles=os.listdir(ddir+os.sep+'JPEGImages' + os.sep) | |
X_train, file_names = load_X_files(xfiles) | |
elif(ddir=="PedCut2013"): | |
X_test, file_names = load_X('testData' + os.sep + 'left_images') | |
inputs= Input((imsize, imsize, 3)) | |
model = Unet.genUNet(input_channel_count, output_channel_count, first_layer_filter_count) | |
model.load_weights('segunet_weights.hdf5') | |
BATCH_SIZE = 12 | |
Y_pred = model.predict(X_test, BATCH_SIZE) | |
for i, y in enumerate(Y_pred): | |
if(ddir=="VOC2012"): | |
img=cv2.imread(ddir+os.sep+'SegmentationOjbect' + os.sep+ file_names[i]) | |
elif(ddir=="PedCut2013"): | |
img=cv2.imread('testData' + os.sep + 'left_images' + os.sep + file_names[i]) | |
y = cv2.resize(y, (img.shape[1], img.shape[0])) | |
cv2.imwrite('prediction' + str(i) + '.png', denormalize_y(y)) | |
if __name__ == '__main__': | |
train() | |
predict() | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"from matplotlib import pyplot as plt\n", | |
"%matplotlib inline \n", | |
"import os\n", | |
"#from keras.datasets import cifar10\n", | |
"import numpy as np" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 42, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"from keras.optimizers import Adam\n", | |
"from keras.callbacks import EarlyStopping\n", | |
"\n", | |
"from keras.models import Sequential\n", | |
"from keras.layers.core import Dense, Activation\n", | |
"from keras.callbacks import Callback\n", | |
"\n", | |
"from keras.layers.convolutional import Conv2DTranspose\n", | |
"from keras.layers.convolutional import Convolution2D\n", | |
"from keras.layers.convolutional import MaxPooling2D\n", | |
"from keras.layers.merge import Concatenate\n", | |
"from keras.layers.merge import concatenate\n", | |
"from keras.layers.core import Dropout\n", | |
"from keras.layers.core import Flatten\n", | |
"from keras.layers.core import Lambda\n", | |
"\n", | |
"from keras.utils import np_utils\n", | |
"\n", | |
"from keras.models import Model\n", | |
"from keras.layers import Input\n", | |
"from keras import backend as K\n", | |
"\n", | |
"import tensorflow as tf" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"import glob\n", | |
"import cv2" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"ename": "NameError", | |
"evalue": "name 'pd' is not defined", | |
"output_type": "error", | |
"traceback": [ | |
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", | |
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", | |
"\u001b[0;32m<ipython-input-4-2dcbdb0b2191>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mlabels\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mpd\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mread_csv\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"train_master.tsv\"\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\"\\t\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0;31m#labels.head()\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0mnclass\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mlabels\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m\"category_id\"\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0munique\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", | |
"\u001b[0;31mNameError\u001b[0m: name 'pd' is not defined" | |
] | |
} | |
], | |
"source": [ | |
"labels=pd.read_csv(\"train_master.tsv\",\"\\t\")\n", | |
"#labels.head()\n", | |
"nclass=len(labels[\"category_id\"].unique())\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"imgs=glob.glob(\"train_1/*.jpg\")+glob.glob(\"train_1/*.jpeg\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"ima=cv2.imread(imgs[0])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"(576, 1024, 3)" | |
] | |
}, | |
"execution_count": 7, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"ima.shape" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"IMG_HEIGHT=ima.shape[0]\n", | |
"IMG_WIDTH= ima.shape[1]\n", | |
"IMG_CHANNELS=3" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Define IoU metric" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 43, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"def mean_iou(y_true, y_pred):\n", | |
" prec = []\n", | |
" for t in np.arange(0.5, 1.0, 0.05):\n", | |
" y_pred_ = tf.to_int32(y_pred > t)\n", | |
" score, up_opt = tf.metrics.mean_iou(y_true, y_pred_, 2)\n", | |
" K.get_session().run(tf.local_variables_initializer())\n", | |
" with tf.control_dependencies([up_opt]):\n", | |
" score = tf.identity(score)\n", | |
" prec.append(score)\n", | |
" return K.mean(K.stack(prec), axis=0)\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Unet" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"https://www.kaggle.com/keegil/keras-u-net-starter-lb-0-277" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 37, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"from keras.layers.convolutional import Conv2D\n", | |
"\n", | |
"def Conv2D16(s):\n", | |
" return Conv2D(16, (3, 3), activation='elu', kernel_initializer='he_normal', padding='same') (s)\n", | |
"\n", | |
"def _Conv2D(s,size,dsize=3):\n", | |
" return Conv2D(size, (dsize, dsize), activation='elu', kernel_initializer='he_normal', padding='same')(s)\n", | |
" \n", | |
"def CDCP(s,size,dsize=3,droprate=0.1,withpooling=True):\n", | |
" c =_Conv2D(s,size,dsize)\n", | |
" c = Dropout(droprate)(c)\n", | |
" c =_Conv2D(c,size,dsize)\n", | |
" if(withpooling):\n", | |
" p=MaxPooling2D((2, 2)) (c)\n", | |
" return p,c\n", | |
" else:\n", | |
" return c\n", | |
" \n", | |
"def Ulayer(s,t,size,dsize=3,droprate=0.2):\n", | |
" u = Conv2DTranspose(size, (2, 2), strides=(2, 2), padding='same') (s)\n", | |
" u = concatenate([u, t])\n", | |
" c = _Conv2D(u,size, dsize)\n", | |
" c = Dropout(droprate)(c)\n", | |
" return _Conv2D(c,size,dsize)\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 38, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"inputs = Input((IMG_HEIGHT, IMG_WIDTH, IMG_CHANNELS))\n", | |
"\n", | |
"def genUnet(inputs):\n", | |
" s = Lambda(lambda x: x / 255) (inputs)\n", | |
" a0,c0=CDCP(s,16,3,0.1)\n", | |
" a1,c1=CDCP(a0,32,3,0.1)\n", | |
" a2,c2=CDCP(a1,64,3,0.2)\n", | |
" a3,c3=CDCP(a2,128,3,0.2)\n", | |
" a4=CDCP(a3,256,3,0.2,False)\n", | |
"\n", | |
" a5=Ulayer(a4,c3,128)\n", | |
" a6=Ulayer(a5,c2,64)\n", | |
" a7=Ulayer(a6,c1,32)\n", | |
" a8=Ulayer(a7,c0,16)\n", | |
"# return Conv2D(1, (1, 1), activation='sigmoid') (a8)\n", | |
" a9=Conv2D(1, (1, 1), activation='sigmoid')(a8)\n", | |
" return Model(inputs=[inputs],outputs=[a9])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 44, | |
"metadata": { | |
"scrolled": true | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"__________________________________________________________________________________________________\n", | |
"Layer (type) Output Shape Param # Connected to \n", | |
"==================================================================================================\n", | |
"input_12 (InputLayer) (None, 576, 1024, 3) 0 \n", | |
"__________________________________________________________________________________________________\n", | |
"lambda_14 (Lambda) (None, 576, 1024, 3) 0 input_12[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"conv2d_157 (Conv2D) (None, 576, 1024, 16 448 lambda_14[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"dropout_75 (Dropout) (None, 576, 1024, 16 0 conv2d_157[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"conv2d_158 (Conv2D) (None, 576, 1024, 16 2320 dropout_75[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"max_pooling2d_53 (MaxPooling2D) (None, 288, 512, 16) 0 conv2d_158[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"conv2d_159 (Conv2D) (None, 288, 512, 32) 4640 max_pooling2d_53[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"dropout_76 (Dropout) (None, 288, 512, 32) 0 conv2d_159[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"conv2d_160 (Conv2D) (None, 288, 512, 32) 9248 dropout_76[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"max_pooling2d_54 (MaxPooling2D) (None, 144, 256, 32) 0 conv2d_160[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"conv2d_161 (Conv2D) (None, 144, 256, 64) 18496 max_pooling2d_54[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"dropout_77 (Dropout) (None, 144, 256, 64) 0 conv2d_161[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"conv2d_162 (Conv2D) (None, 144, 256, 64) 36928 dropout_77[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"max_pooling2d_55 (MaxPooling2D) (None, 72, 128, 64) 0 conv2d_162[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"conv2d_163 (Conv2D) (None, 72, 128, 128) 73856 max_pooling2d_55[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"dropout_78 (Dropout) (None, 72, 128, 128) 0 conv2d_163[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"conv2d_164 (Conv2D) (None, 72, 128, 128) 147584 dropout_78[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"max_pooling2d_56 (MaxPooling2D) (None, 36, 64, 128) 0 conv2d_164[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"conv2d_165 (Conv2D) (None, 36, 64, 256) 295168 max_pooling2d_56[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"dropout_79 (Dropout) (None, 36, 64, 256) 0 conv2d_165[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"conv2d_166 (Conv2D) (None, 36, 64, 256) 590080 dropout_79[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"conv2d_transpose_19 (Conv2DTran (None, 72, 128, 128) 131200 conv2d_166[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"concatenate_18 (Concatenate) (None, 72, 128, 256) 0 conv2d_transpose_19[0][0] \n", | |
" conv2d_164[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"conv2d_167 (Conv2D) (None, 72, 128, 128) 295040 concatenate_18[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"dropout_80 (Dropout) (None, 72, 128, 128) 0 conv2d_167[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"conv2d_168 (Conv2D) (None, 72, 128, 128) 147584 dropout_80[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"conv2d_transpose_20 (Conv2DTran (None, 144, 256, 64) 32832 conv2d_168[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"concatenate_19 (Concatenate) (None, 144, 256, 128 0 conv2d_transpose_20[0][0] \n", | |
" conv2d_162[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"conv2d_169 (Conv2D) (None, 144, 256, 64) 73792 concatenate_19[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"dropout_81 (Dropout) (None, 144, 256, 64) 0 conv2d_169[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"conv2d_170 (Conv2D) (None, 144, 256, 64) 36928 dropout_81[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"conv2d_transpose_21 (Conv2DTran (None, 288, 512, 32) 8224 conv2d_170[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"concatenate_20 (Concatenate) (None, 288, 512, 64) 0 conv2d_transpose_21[0][0] \n", | |
" conv2d_160[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"conv2d_171 (Conv2D) (None, 288, 512, 32) 18464 concatenate_20[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"dropout_82 (Dropout) (None, 288, 512, 32) 0 conv2d_171[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"conv2d_172 (Conv2D) (None, 288, 512, 32) 9248 dropout_82[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"conv2d_transpose_22 (Conv2DTran (None, 576, 1024, 16 2064 conv2d_172[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"concatenate_21 (Concatenate) (None, 576, 1024, 32 0 conv2d_transpose_22[0][0] \n", | |
" conv2d_158[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"conv2d_173 (Conv2D) (None, 576, 1024, 16 4624 concatenate_21[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"dropout_83 (Dropout) (None, 576, 1024, 16 0 conv2d_173[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"conv2d_174 (Conv2D) (None, 576, 1024, 16 2320 dropout_83[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"conv2d_175 (Conv2D) (None, 576, 1024, 1) 17 conv2d_174[0][0] \n", | |
"==================================================================================================\n", | |
"Total params: 1,941,105\n", | |
"Trainable params: 1,941,105\n", | |
"Non-trainable params: 0\n", | |
"__________________________________________________________________________________________________\n" | |
] | |
} | |
], | |
"source": [ | |
"#model = Model(inputs=[inputs],outputs=[genUnet])\n", | |
"model=genUnet(inputs)\n", | |
"model.compile(optimizer='adam', loss='binary_crossentropy', metrics=[mean_iou])\n", | |
"model.summary()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"earlystopper = EarlyStopping(patience=5, verbose=1)\n", | |
"checkpointer = ModelCheckpoint('model-unet.h5', verbose=1, save_best_only=True)\n", | |
"results = model.fit(X_train, Y_train, validation_split=0.1, batch_size=16, epochs=50, \n", | |
" callbacks=[earlystopper, checkpointer])" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "tf", | |
"language": "python", | |
"name": "tf" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.5.4" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from keras.models import Sequential | |
from keras.layers.core import Dense, Activation | |
from keras.layers.convolutional import Conv2DTranspose | |
from keras.layers.convolutional import Convolution2D | |
from keras.layers.convolutional import MaxPooling2D | |
from keras.layers.merge import Concatenate | |
from keras.layers.merge import concatenate | |
from keras.layers.core import Dropout | |
from keras.layers.core import Flatten | |
from keras.layers.core import Lambda | |
from keras.layers.convolutional import Conv2D | |
def Conv2D16(s): | |
return Conv2D(16, (3, 3), activation='elu', kernel_initializer='he_normal', padding='same') (s) | |
def _Conv2D(s,size,dsize=3): | |
return Conv2D(size, (dsize, dsize), activation='elu', kernel_initializer='he_normal', padding='same')(s) | |
def CDCP(s,size,dsize=3,droprate=0.1,withpooling=True): | |
c =_Conv2D(s,size,dsize) | |
c = Dropout(droprate)(c) | |
c =_Conv2D(c,size,dsize) | |
if(withpooling): | |
p=MaxPooling2D((2, 2)) (c) | |
return p,c | |
else: | |
return c | |
def Ulayer(s,t,size,dsize=3,droprate=0.2): | |
u = Conv2DTranspose(size, (2, 2), strides=(2, 2), padding='same') (s) | |
u = concatenate([u, t]) | |
c = _Conv2D(u,size, dsize) | |
c = Dropout(droprate)(c) | |
return _Conv2D(c,size,dsize) | |
# | |
def genUnet(inputs): | |
s = Lambda(lambda x: x / 255) (inputs) | |
a0,c0=CDCP(s,16,3,0.1) | |
a1,c1=CDCP(a0,32,3,0.1) | |
a2,c2=CDCP(a1,64,3,0.2) | |
a3,c3=CDCP(a2,128,3,0.2) | |
a4=CDCP(a3,256,3,0.2,False) | |
a5=Ulayer(a4,c3,128) | |
a6=Ulayer(a5,c2,64) | |
a7=Ulayer(a6,c1,32) | |
a8=Ulayer(a7,c0,16) | |
# return Conv2D(1, (1, 1), activation='sigmoid') (a8) | |
a9=Conv2D(1, (1, 1), activation='sigmoid')(a8) | |
return Model(inputs=[inputs],outputs=[a9]) | |
def genUnetmodel(inputs): | |
return Model(inputs=[inputs],outputs=[senUnet(inputs)]) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment