-
Notifications
You must be signed in to change notification settings - Fork 0
/
land_train.py
167 lines (136 loc) · 5.91 KB
/
land_train.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!usr/bin/env python
# -*- coding:utf-8 _*-
"""
@author:yaoli
@file: land_train.py
@time: 2018/12/19
"""
from utils.data_utils import DataSet, read_data
from tensorflow.python.framework import ops
from deeplab_v3 import Deeplab_v3
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split
import numpy as np
import tensorflow as tf
import pandas as pd
import os
ops.reset_default_graph()
tf.logging.set_verbosity(tf.logging.INFO)
class args:
batch_size = 2
lr = 0.0002
display = 1
weight_decay = 0.00001
model_name = 'deeplab_v3'
batch_norm_decay = 0.95
# prepare data
# train_path has 643 img and labels
# valid_path has 160 img and labels
train_sat_path = 'dataset/train_sat'
train_mask_path = 'dataset/train_mask'
train_label_path = 'dataset/train_label'
valid_sat_path = 'dataset/valid_sat'
valid_mask_path = 'dataset/valid_mask'
valid_label_path = 'dataset/valid_label'
train_img = read_data(train_sat_path)
train_label = read_data(train_label_path)
valid_img = read_data(valid_sat_path)
valid_label = read_data(valid_label_path)
# print(train_img)
# print(train_label)
dataset_tr = DataSet(image_path=train_img, label_path=train_label)
dataset_val = DataSet(image_path=valid_img, label_path=valid_label)
tf.logging.info('hello world')
print('Data input success.')
model = Deeplab_v3(batch_norm_decay=args.batch_norm_decay)
image = tf.placeholder(tf.float32, [None, 2448, 2448, 3], name='input_x')
label = tf.placeholder(tf.int32, [None, 2448, 2448])
lr = tf.placeholder(tf.float32)
logits = model.forward_pass(image)
predicts = tf.argmax(logits, axis=-1, name='predicts')
variables_to_restore = tf.trainable_variables(scope='resnet_v2_50')
# print the parameters need to finetune
tf.logging.info('variables_to_restore:')
for var in variables_to_restore:
tf.logging.info(var.name)
restorer = tf.train.Saver(variables_to_restore)
# cross entropy
cross_entropy = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits, labels=label))
# l2_norm l2 正则化
l2_loss = args.weight_decay * tf.add_n([tf.nn.l2_loss(tf.cast(v, tf.float32)) for v in tf.trainable_variables()])
optimizer = tf.train.AdadeltaOptimizer(learning_rate=lr)
loss = cross_entropy + l2_loss
update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
with tf.control_dependencies(update_ops):
train_op = optimizer.minimize(loss = loss)
total_acc_tr, total_acc_val, total_loss_tr, total_loss_val, total_l2_loss = 0, 0, 0, 0, 0
saver = tf.train.Saver(tf.global_variables())
best_val_acc = 0.8
with tf.Session() as sess:
sess.run(tf.local_variables_initializer())
sess.run(tf.global_variables_initializer())
log_path = 'logs/%s/' % args.model_name
model_path = 'ckpts/%s/' % args.model_name
if not os.path.exists(model_path): os.makedirs(model_path)
if not os.path.exists('./logs'): os.makedirs('./logs')
if not os.path.exists(log_path): os.makedirs(log_path)
train_summary_writer = tf.summary.FileWriter('%s/train' % log_path, sess.graph)
val_summary_writer = tf.summary.FileWriter('%s/val'%log_path, sess.graph)
learning_rate = args.lr
for step in range(1, 100000):
if step == 40000 or step == 80000:
learning_rate = learning_rate / 10
x_tr, y_tr = dataset_tr.next_batch(args.batch_size)
x_val, y_val = dataset_val.next_batch(args.batch_size)
loss_tr, l2_loss_tr, predicts_tr, _ = sess.run(
fetches= [cross_entropy, l2_loss, predicts, train_op],
feed_dict={
image: x_val,
label: y_val,
model._is_training: True,
lr: learning_rate
})
loss_val, predicts_val = sess.run(
fetches=[cross_entropy, predicts],
feed_dict={
image: x_val,
label: y_val,
model._is_training: False
})
# count
total_loss_tr += loss_tr
total_loss_val += loss_val
total_l2_loss += l2_loss_tr
acc_tr = accuracy_score(np.reshape(y_tr, [-1]), np.reshape(predicts_tr, [-1]))
acc_val = accuracy_score(np.reshape(y_val, [-1]), np.reshape(predicts_val, [-1]))
total_acc_tr += acc_tr
total_acc_val += acc_val
# print log
if step % args.display == 0:
tf.logging.info("Iter:%d, lr:%.6f, loss_tr: %.4f, acc_tr:%.4f, loss_val:%.4f, loss_val:%.4f, acc_val:$.4f"%
(step,
learning_rate,
total_loss_val / args.display,
total_acc_val / args.display,
total_loss_val / args.display,
total_acc_val / args.display))
train_summary = tf.Summary(
value = [tf.Summary.Value(tag='loss', simple_value=total_loss_val / args.display),
tf.Summary.Value(tag='accuracy', simple_value=total_acc_val / args.display),
tf.Summary.Value(tag='l2_loss', simple_value=total_l2_loss / args.display)]
)
val_summary = tf.Summary(
value = [tf.Summary.Value(tag='loss', simple_value=total_loss_val / args.display),
tf.Summary.Value(tag='accuracy', simple_value=total_acc_val / args.display)]
)
# record summary
train_summary_writer.add_summary(train_summary, step)
train_summary_writer.flush()
val_summary_writer.add_summary(val_summary, step)
val_summary_writer.flush()
# save model
if (total_acc_val / args.display) > best_val_acc:
saver.save(sess, model_path + 'model.ckpt')
best_val_acc = total_acc_val / args.display
tf.logging.info('Save model successfully to "%s"!' % (model_path + 'model.ckpt'))
total_acc_tr, total_acc_val, total_loss_tr, total_loss_val, total_l2_loss = 0, 0, 0, 0, 0