2 Star 0 Fork 1

ttarey/deep-unet-for-satellite-image-segmentation

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
train_unet.py 2.97 KB
一键复制 编辑 原始数据 按行查看 历史
Sumit Kumar 提交于 2018-07-11 17:37 . additional optimizations
from unet_model import *
from gen_patches import *
import os.path
import numpy as np
import tifffile as tiff
from keras.callbacks import CSVLogger
from keras.callbacks import TensorBoard
from keras.callbacks import ModelCheckpoint, EarlyStopping, ReduceLROnPlateau
def normalize(img):
min = img.min()
max = img.max()
x = 2.0 * (img - min) / (max - min) - 1.0
return x
N_BANDS = 8
N_CLASSES = 5 # buildings, roads, trees, crops and water
CLASS_WEIGHTS = [0.2, 0.3, 0.1, 0.1, 0.3]
N_EPOCHS = 150
UPCONV = True
PATCH_SZ = 160 # should divide by 16
BATCH_SIZE = 150
TRAIN_SZ = 4000 # train size
VAL_SZ = 1000 # validation size
def get_model():
return unet_model(N_CLASSES, PATCH_SZ, n_channels=N_BANDS, upconv=UPCONV, class_weights=CLASS_WEIGHTS)
weights_path = 'weights'
if not os.path.exists(weights_path):
os.makedirs(weights_path)
weights_path += '/unet_weights.hdf5'
trainIds = [str(i).zfill(2) for i in range(1, 25)] # all availiable ids: from "01" to "24"
if __name__ == '__main__':
X_DICT_TRAIN = dict()
Y_DICT_TRAIN = dict()
X_DICT_VALIDATION = dict()
Y_DICT_VALIDATION = dict()
print('Reading images')
for img_id in trainIds:
img_m = normalize(tiff.imread('./data/mband/{}.tif'.format(img_id)).transpose([1, 2, 0]))
mask = tiff.imread('./data/gt_mband/{}.tif'.format(img_id)).transpose([1, 2, 0]) / 255
train_xsz = int(3/4 * img_m.shape[0]) # use 75% of image as train and 25% for validation
X_DICT_TRAIN[img_id] = img_m[:train_xsz, :, :]
Y_DICT_TRAIN[img_id] = mask[:train_xsz, :, :]
X_DICT_VALIDATION[img_id] = img_m[train_xsz:, :, :]
Y_DICT_VALIDATION[img_id] = mask[train_xsz:, :, :]
print(img_id + ' read')
print('Images were read')
def train_net():
print("start train net")
x_train, y_train = get_patches(X_DICT_TRAIN, Y_DICT_TRAIN, n_patches=TRAIN_SZ, sz=PATCH_SZ)
x_val, y_val = get_patches(X_DICT_VALIDATION, Y_DICT_VALIDATION, n_patches=VAL_SZ, sz=PATCH_SZ)
model = get_model()
if os.path.isfile(weights_path):
model.load_weights(weights_path)
#model_checkpoint = ModelCheckpoint(weights_path, monitor='val_loss', save_weights_only=True, save_best_only=True)
#early_stopping = EarlyStopping(monitor='val_loss', min_delta=0, patience=10, verbose=1, mode='auto')
#reduce_lr = ReduceLROnPlateau(monitor='loss', factor=0.1, patience=5, min_lr=0.00001)
model_checkpoint = ModelCheckpoint(weights_path, monitor='val_loss', save_best_only=True)
csv_logger = CSVLogger('log_unet.csv', append=True, separator=';')
tensorboard = TensorBoard(log_dir='./tensorboard_unet/', write_graph=True, write_images=True)
model.fit(x_train, y_train, batch_size=BATCH_SIZE, epochs=N_EPOCHS,
verbose=2, shuffle=True,
callbacks=[model_checkpoint, csv_logger, tensorboard],
validation_data=(x_val, y_val))
return model
train_net()
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/ttarrr/deep-unet-for-satellite-image-segmentation.git
git@gitee.com:ttarrr/deep-unet-for-satellite-image-segmentation.git
ttarrr
deep-unet-for-satellite-image-segmentation
deep-unet-for-satellite-image-segmentation
master

搜索帮助

D67c1975 1850385 1daf7b77 1850385