U-netを使って、セマンティックセグメンテーションをやっているのですが、
なかなか安定しないようで、添付グラフのようにギザギザな感じになってしまっています。
このノイズを取り除くには画像を増やす、画像を変えるなど、する以外の方法が何かありますでしょうか?
教えていただければと思います。
画像枚数は127枚を拡張で増やしつつ、学習している感じです。
何かアドバイスなどありましたらよろしくお願いいたします。。
画像は空中写真を使っています。
python
1def image_augmentation(imgs, masks, batch_size, seed): 2 # create two instances with the same arguments 3 # create dictionary with the input augmentation values 4 data_gen_args = dict(featurewise_center=False, 5 featurewise_std_normalization=False, 6 rotation_range=25., 7 width_shift_range=0.5, 8 height_shift_range=0.5, 9 zoom_range=[0.5, 1], 10 horizontal_flip=False, 11 vertical_flip=False, 12 fill_mode="constant") 13 14 ## use this method with both images and masks 15 image_datagen = ImageDataGenerator(**data_gen_args) 16 mask_datagen = ImageDataGenerator(**data_gen_args) 17 18 ## fit the augmentation model to the images and masks with the same seed 19 image_datagen.fit(imgs, augment=True, seed=seed) 20 mask_datagen.fit(masks, augment=True, seed=seed) 21 22 ## set the parameters for the data to come from (images) 23 image_generator = image_datagen.flow(imgs, 24 shuffle=True, 25 batch_size=batch_size, 26 save_to_dir=None, 27 save_prefix='out_image', 28 save_format='jpg', 29 seed=seed) 30 31 ## set the parameters for the data to come from (masks) 32 mask_generator = mask_datagen.flow(masks, 33 shuffle=True, 34 batch_size=batch_size, 35 save_to_dir=None, 36 save_prefix='out_mask', 37 save_format='png', 38 seed=seed) 39 40 # combine generators into one which yields image and masks 41 train_generator = zip(image_generator, mask_generator) 42 43 ## return the train generator for input in the CNN 44 return train_generator 45 46import warnings 47warnings.filterwarnings('ignore') 48import cv2 49from sklearn.model_selection import train_test_split 50from tensorflow.keras.preprocessing.image import ImageDataGenerator 51import glob 52import numpy as np 53import matplotlib.pyplot as plt 54from tensorflow.keras.callbacks import LearningRateScheduler , EarlyStopping, ModelCheckpoint, ReduceLROnPlateau,TensorBoard 55from tensorflow.keras import backend as K 56import tensorflow as tf 57 58img_size = 512 59 60input_path=glob.glob("trainingData/left_images/*") 61output_path=glob.glob("trainingData/left_groundTruth/*") 62 63train_x=[] 64train_y=[] 65 66for path in input_path: 67 image=cv2.imread(path) 68 image=cv2.resize(image, (img_size, img_size),interpolation = cv2.INTER_LANCZOS4) 69 image = image / 255. 70 train_x.append(image) 71 72for path in output_path: 73 image=cv2.imread(path,0) 74 image=cv2.resize(image, (img_size, img_size),interpolation = cv2.INTER_LANCZOS4) 75 image = image / 255. 76 train_y.append(image) 77 78train_x=np.array(train_x) 79train_y=np.array(train_y) 80train_y=np.reshape(train_y,(-1,img_size,img_size,1)) 81 82X_train, X_test, Y_train, Y_test = train_test_split(train_x, train_y,train_size=0.8,shuffle=True) 83 84# #hard_swishの定義 85def hard_swish(x): 86 return x * (K.relu(x + 3., max_value = 6.) / 6.) 87 88# #swishの定義 89def swish(x): 90 return x * K.sigmoid(x) 91 92# 正当なU-netのモデル(mishを導入している) 93# 正則化(L2)を導入して変化するかを試す。 94 95from tensorflow.keras.layers import Conv2D, Input, LeakyReLU, MaxPooling2D, UpSampling2D, Dropout, BatchNormalization, Conv2DTranspose 96from tensorflow.keras.models import Model 97from tensorflow.keras.layers import concatenate 98from tensorflow.keras.layers import PReLU 99from tensorflow.keras.optimizers import RMSprop, Adam 100from tensorflow.keras import backend as K 101from tensorflow.keras import regularizers 102from tf_rectified_adam import RectifiedAdam 103 104input_img = Input(shape=(img_size, img_size, 3)) 105 106enc1 = Conv2D(64, kernel_size=3, strides=1, activation=hard_swish, 107 padding="same")(input_img) 108enc1 = BatchNormalization()(enc1) 109enc1 = Conv2D(64, kernel_size=3, strides=1, activation=hard_swish, 110 padding="same")(enc1) 111enc1 = BatchNormalization()(enc1) 112down1 = MaxPooling2D(pool_size=2, strides=2)(enc1) 113 114enc2 = Conv2D(128, kernel_size=3, strides=1, activation=hard_swish, 115 padding="same")(down1) 116enc2 = BatchNormalization()(enc2) 117enc2 = Conv2D(128, kernel_size=3, strides=1, activation=hard_swish, 118 padding="same")(enc2) 119enc2 = BatchNormalization()(enc2) 120down2 = MaxPooling2D(pool_size=2, strides=2)(enc2) 121 122enc3 = Conv2D(256, kernel_size=3, strides=1, activation=hard_swish, 123 padding="same")(down2) 124enc3 = BatchNormalization()(enc3) 125enc3 = Conv2D(256, kernel_size=3, strides=1, activation=hard_swish, 126 padding="same")(enc3) 127enc3 = BatchNormalization()(enc3) 128down3 = MaxPooling2D(pool_size=2, strides=2)(enc3) 129 130enc4 = Conv2D(512, kernel_size=3, strides=1, activation=hard_swish, 131 padding="same")(down3) 132enc4 = BatchNormalization()(enc4) 133enc4 = Conv2D(512, kernel_size=3, strides=1, activation=hard_swish, 134 padding="same")(enc4) 135enc4 = BatchNormalization()(enc4) 136down4 = MaxPooling2D(pool_size=2, strides=2)(enc4) 137 138enc5 = Conv2D(1024, kernel_size=3, strides=1, activation=hard_swish, 139 padding="same")(down4) 140enc5 = BatchNormalization()(enc5) 141enc5 = Conv2D(1024, kernel_size=3, strides=1, activation=hard_swish, 142 padding="same")(enc5) 143enc5 = BatchNormalization()(enc5) 144 145up4 = UpSampling2D(size=2)(enc5) 146dec4 = concatenate([up4, enc4], axis=-1) 147dec4 = Conv2D(512, kernel_size=3, strides=1, activation=hard_swish, 148 padding="same")(dec4) 149dec4 = BatchNormalization()(dec4) 150dec4 = Conv2D(512, kernel_size=3, strides=1, activation=hard_swish, 151 padding="same")(dec4) 152dec4 = BatchNormalization()(dec4) 153 154up3 = UpSampling2D(size=2)(dec4) 155dec3 = concatenate([up3, enc3], axis=-1) 156dec3 = Conv2D(256, kernel_size=3, strides=1, activation=hard_swish, 157 padding="same")(dec3) 158dec3 = BatchNormalization()(dec3) 159dec3 = Conv2D(256, kernel_size=3, strides=1, activation=hard_swish, 160 padding="same")(dec3) 161dec3 = BatchNormalization()(dec3) 162 163up2 = UpSampling2D(size=2)(dec3) 164dec2 = concatenate([up2, enc2], axis=-1) 165dec2 = Conv2D(128, kernel_size=3, strides=1, activation=hard_swish, 166 padding="same")(dec2) 167dec2 = BatchNormalization()(dec2) 168dec2 = Conv2D(128, kernel_size=3, strides=1, activation=hard_swish, 169 padding="same")(dec2) 170dec2 = BatchNormalization()(dec2) 171 172up1 = UpSampling2D(size=2)(dec2) 173dec1 = concatenate([up1, enc1], axis=-1) 174dec1 = Conv2D(64, kernel_size=3, strides=1, activation=hard_swish, 175 padding="same")(dec1) 176dec1 = BatchNormalization()(dec1) 177dec1 = Conv2D(64, kernel_size=3, strides=1, activation=hard_swish, 178 padding="same")(dec1) 179dec1 = BatchNormalization()(dec1) 180 181deca = Conv2D(1,kernel_size=1,strides=1,activation="sigmoid", padding="same")(dec1) 182 183batch_size = 1 184epoch_num =1000 185seed = 2020 186 187train_generator = image_augmentation(X_train, Y_train,batch_size,seed) 188test_generator = image_augmentation(X_test, Y_test,batch_size,seed) 189 190model=Model(inputs=input_img, outputs=deca) 191 192 193# 評価指標IOUのための関数 194 195def true_positive(y_true, y_pred): 196 return K.sum(K.cast(K.equal(y_true * y_pred, 1), K.floatx())) 197 198def true_negative(y_true, y_pred): 199 return K.sum(K.cast(K.equal(y_true + y_pred, 0), K.floatx())) 200 201def false_positive(y_true, y_pred): 202 return K.sum(K.cast(K.less(y_true, y_pred), K.floatx())) 203 204def false_negative(y_true, y_pred): 205 return K.sum(K.cast(K.greater(y_true, y_pred), K.floatx())) 206 207def IoU(y_true, y_pred): 208 y_pred = K.round(y_pred) 209 return true_positive(y_true, y_pred) / (K.epsilon()+false_negative(y_true, y_pred) + true_positive(y_true, y_pred) + false_positive(y_true, y_pred)) 210 211 212# 最良の重みのみを保存する。 213cp = ModelCheckpoint("weight/unet_weights_testmodel.hdf5", monitor="val_loss", verbose=1,save_best_only=True, save_weights_only=True) 214 215 216model.compile(optimizer=RectifiedAdam(lr=1e-3), loss='mse', metrics=[IoU]) 217 218history = model.fit_generator(train_generator, 219 steps_per_epoch = X_train.shape[0]//batch_size, 220 validation_steps = X_test.shape[0]//batch_size, 221 epochs = epoch_num, 222 shuffle = True, 223 verbose = 1, 224 validation_data = test_generator, 225 callbacks = [cp]) 226
あなたの回答
tips
プレビュー