8.7 KiB
8.7 KiB
import shutil
import tensorflow as tf
from tensorflow.keras import backend as K
from tensorflow.keras.layers import Activation, Lambda, GlobalAveragePooling2D, concatenate
from tensorflow.keras.layers import UpSampling2D, Conv2D, Dropout, MaxPooling2D, Conv2DTranspose
from tensorflow.keras.layers import Dense, Flatten, Input
from tensorflow.keras.models import Model, Sequential, load_model
from tensorflow.keras.callbacks import ModelCheckpoint, ReduceLROnPlateau
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import cv2
import pickle
import random
import os
from src.metrics import IOU
IMG_HEIGHT = 512
IMG_WIDTH = 512
img_dir = '/images'
EPOCHS = 30
batch_size = 16
Unet model
class Unet():
def __init__(self, num_classes=1):
self.num_classes=num_classes
def build_model(self):
in1 = Input(shape=(IMG_HEIGHT, IMG_WIDTH, 3 ))
conv1 = Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(in1)
conv1 = Dropout(0.2)(conv1)
conv1 = Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(conv1)
pool1 = MaxPooling2D((2, 2))(conv1)
conv2 = Conv2D(64, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(pool1)
conv2 = Dropout(0.2)(conv2)
conv2 = Conv2D(64, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(conv2)
pool2 = MaxPooling2D((2, 2))(conv2)
conv3 = Conv2D(128, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(pool2)
conv3 = Dropout(0.2)(conv3)
conv3 = Conv2D(128, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(conv3)
pool3 = MaxPooling2D((2, 2))(conv3)
conv4 = Conv2D(128, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(pool3)
conv4 = Dropout(0.2)(conv4)
conv4 = Conv2D(128, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(conv4)
up1 = concatenate([UpSampling2D((2, 2))(conv4), conv3], axis=-1)
conv5 = Conv2D(64, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(up1)
conv5 = Dropout(0.2)(conv5)
conv5 = Conv2D(64, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(conv5)
up2 = concatenate([UpSampling2D((2, 2))(conv5), conv2], axis=-1)
conv6 = Conv2D(64, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(up2)
conv6 = Dropout(0.2)(conv6)
conv6 = Conv2D(64, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(conv6)
up2 = concatenate([UpSampling2D((2, 2))(conv6), conv1], axis=-1)
conv7 = Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(up2)
conv7 = Dropout(0.2)(conv7)
conv7 = Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(conv7)
segmentation = Conv2D(self.num_classes, (1, 1), activation='sigmoid', name='seg')(conv7)
#segmentation = Conv2D(3, (1, 1), activation='sigmoid', name='seg')(conv7)
model = Model(inputs=[in1], outputs=[segmentation])
return model
from src.loss import jaccard_loss
model = Unet(num_classes=1).build_model()
compile_params ={
'loss':jaccard_loss(smooth=90),
'optimizer':'rmsprop',
'metrics':[IOU]
}
model.compile(**compile_params)
tf.keras.utils.plot_model(model, show_shapes=True)
model_name = "models/unet.h5"
modelcheckpoint = ModelCheckpoint(model_name,
monitor='val_loss',
mode='auto',
verbose=1,
save_best_only=True)
history = model.fit_generator(train_gen,
validation_data=val_gen,
epochs=EPOCHS,
steps_per_epoch=100,
validation_steps = 100,
shuffle=True,
validation_freq=1
)
[0;31m---------------------------------------------------------------------------[0m [0;31mNameError[0m Traceback (most recent call last) [1;32m/Users/patrycjalazna/Desktop/wko-projekt/unet.ipynb Cell 7'[0m in [0;36m<module>[0;34m[0m [1;32m <a href='vscode-notebook-cell:/Users/patrycjalazna/Desktop/wko-projekt/unet.ipynb#ch0000003?line=0'>1</a>[0m model [39m=[39m Unet(num_classes[39m=[39m[39m1[39m)[39m.[39mbuild_model() [1;32m <a href='vscode-notebook-cell:/Users/patrycjalazna/Desktop/wko-projekt/unet.ipynb#ch0000003?line=2'>3</a>[0m compile_params [39m=[39m{ [0;32m----> <a href='vscode-notebook-cell:/Users/patrycjalazna/Desktop/wko-projekt/unet.ipynb#ch0000003?line=3'>4</a>[0m [39m'[39m[39mloss[39m[39m'[39m:jaccard_loss(smooth[39m=[39m[39m90[39m), [1;32m <a href='vscode-notebook-cell:/Users/patrycjalazna/Desktop/wko-projekt/unet.ipynb#ch0000003?line=4'>5</a>[0m [39m'[39m[39moptimizer[39m[39m'[39m:[39m'[39m[39mrmsprop[39m[39m'[39m, [1;32m <a href='vscode-notebook-cell:/Users/patrycjalazna/Desktop/wko-projekt/unet.ipynb#ch0000003?line=5'>6</a>[0m [39m'[39m[39mmetrics[39m[39m'[39m:[IOU] [1;32m <a href='vscode-notebook-cell:/Users/patrycjalazna/Desktop/wko-projekt/unet.ipynb#ch0000003?line=6'>7</a>[0m } [1;32m <a href='vscode-notebook-cell:/Users/patrycjalazna/Desktop/wko-projekt/unet.ipynb#ch0000003?line=9'>10</a>[0m model[39m.[39mcompile([39m*[39m[39m*[39mcompile_params) [1;32m <a href='vscode-notebook-cell:/Users/patrycjalazna/Desktop/wko-projekt/unet.ipynb#ch0000003?line=11'>12</a>[0m tf[39m.[39mkeras[39m.[39mutils[39m.[39mplot_model(model, show_shapes[39m=[39m[39mTrue[39;00m) [0;31mNameError[0m: name 'jaccard_loss' is not defined