示例#1
0
	def train(self, epoch):
		os.makedirs('tb/%s' % self.model_name)
		os.makedirs('trained_model/%s' % self.model_name)

		tf.summary.image('input x_image', self.x_image, 4)
		tf.summary.image('y_prediction', self.y_conv, 4)
		tf.summary.image('y_GT', self.y_, 4)
		tf.summary.image('y_pred_softmax', self.y_soft, 4)
		tf.summary.scalar('cross_entropy', self.cross_entropy)
		tf.summary.scalar('learning rate', self.lr)

		sess = tf.Session()

		saver = tf.train.Saver()

		merged = tf.summary.merge_all()
		train_writer = tf.summary.FileWriter('/home/lsmjn/Drone-CNN/tb/%s' % self.model_name, sess.graph)

		conn, cur = data.get_db_connection()

		steps = data.get_steps(self.batch_size)

		filename_queue = tf.train.string_input_producer(['Drone-CNN.tfrecords'])

		image, annotation = data.read_and_decode(filename_queue, self.batch_size)

		coord = tf.train.Coordinator()
		threads = tf.train.start_queue_runners(sess=sess, coord=coord)

		sess.run(tf.global_variables_initializer())
		sess.run(tf.local_variables_initializer())

		print('Training...')
		for k in tqdm(range(0, steps*epoch)):
			x_batch, y_batch = sess.run([image, annotation])

			summary, _ = sess.run([merged, self.train_step], feed_dict={self.x_image: x_batch, self.y_: y_batch, self.lr:self.lr_value, self.m:self.m_value, self.keep_prob: 0.5})

			k = k + 1
			train_writer.add_summary(summary, k)

		cur.close()
		conn.close()

		save_path = saver.save(sess, "trained_model/%s/Drone_CNN.ckpt" % self.model_name)
		print('Model saved in file: %s' % save_path)
		f_log.close()
		train_writer.close()
示例#2
0
	def drone_prediction(self, mode, window_sliding_stride=8, interest_label='Building'):
		conn, cur = data.get_db_connection()
		drone_dir = data.get_drone_dir_all()

		y_conv_argmax = tf.argmax(self.y_conv, 1)
		y_conv_softmax= tf.nn.softmax(self.y_conv)

		with self.restore() as sess:
			#for each drone ortho-images
			for row in drone_dir:
				print('Current Dataset: %s (%s)' % (row[0], mode))
				curr_image = cv2.imread(row[1])

				x_batch_drone = []

				k = 0
				prob_list = []

				curr_image_h = len(curr_image)
				curr_image_w = len(curr_image[0])

				result_image_h = curr_image_h - curr_image_h%window_sliding_stride - self.input_patch_size
				result_image_w = curr_image_w - curr_image_w%window_sliding_stride - self.input_patch_size

				#For each patch...
				for i in range(0, result_image_h, window_sliding_stride):
					for j in range(0, result_image_w, window_sliding_stride):
						k = k + 1

						patch = np.array(curr_image[i:i+self.input_patch_size, j:j+self.input_patch_size])

						x_batch_drone.append(patch)

						#Run tensorflow with 128 patches
						if k%self.batch_size==0:
							self.run_prediction(sess, x_batch_drone, y_conv_argmax, y_conv_softmax, mode, interest_label, prob_list)
							x_batch_drone = []

				#Run prediction for rest of data
				self.run_prediction(sess, x_batch_drone, y_conv_argmax, y_conv_softmax, mode, interest_label, prob_list)

				result = np.reshape(prob_list, (int(result_image_h/window_sliding_stride), int(result_image_w/window_sliding_stride)))

				if mode == 'PROB_OF_INTEREST':
					result = cv2.resize(result, (len(curr_image[0]), len(curr_image)), interpolation=cv2.INTER_LINEAR) * 255

				cv2.imwrite('result_%s_%s.png' % (row[0], mode), result)
				print('Prediction Complete.')
示例#3
0
	def drone_prediction(self):
		y_conv_argmax = tf.argmax(self.y_conv, 1)
		conn, cur = data.get_db_connection()
		drone_dir = data.get_drone_dir_all()

		with self.restore() as sess:
			for i in range(0, len(drone_dir)):
				curr_dataset_name = drone_dir[i][0]
				print('Current Dataset: %s' % curr_dataset_name)
				end_idx = data.get_patch_num(curr_dataset_name)

				for start_idx in range(0, end_idx, 20):
					x_batch_drone = data.make_batch_drone(conn, cur, start_idx, 20)
					y_prediction = sess.run(y_conv_argmax, feed_dict={self.x_image:x_batch_drone, self.keep_prob: 1.0})

					'''
					for pred_result in y_prediction:
						if pred_result == 0:
							y_label = 'Building'
						elif pred_result== 1:
							y_label = 'Road'
						elif pred_result== 2:
							y_label = 'Otherwise'
						else:
							y_label = "???"
						print(y_label)
					'''

					f, axarr = plt.subplots(2, 10)

					f.suptitle('Prediction Result of %s Dataset' % curr_dataset_name)

					for i in range(0, 20):
						if y_prediction[i] == 0:
							y_label = 'Building'
						elif y_prediction[i] == 1:
							y_label = 'Road'
						elif y_prediction[i] == 2:
							y_label = 'Otherwise'
						else:
							y_label = "???"
						axarr[0, i].imshow(x_batch_drone[i]) if i < 10 else axarr[1, i-10].imshow(x_batch_drone[i])
						axarr[0, i].set_title(y_label) if i < 10 else axarr[1, i-10].set_title(y_label)

					plt.show()
def build_tfrecords(target_dataset, augment_op=False):
    conn, cur = data.get_db_connection()
    filename_pairs, _, _ = data.get_patch_all(conn, cur, target_dataset)
    tfrecords_filename = '/media/lsmjn/56fcc20e-a0ee-45e0-8df1-bf8b2e9a43b2/tfrecords/NGII_%s.tfrecords' % target_dataset
    writer = tf.python_io.TFRecordWriter(tfrecords_filename)

    sess = tf.Session()
    sess.run(tf.global_variables_initializer())

    for img_path, annotation_path in tqdm(filename_pairs):
        img = cv2.imread(img_path)
        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        annotation = cv2.imread(annotation_path)
        annotation = cv2.cvtColor(annotation, cv2.COLOR_BGR2RGB)

        if augment_op:
            augmented_img_list = augment_image(img, sess)
            augmented_annotation_list = augment_image(annotation, sess)

            for i in range(0, 7):
                img_raw = augmented_img_list[i].tostring()
                annotation_raw = augmented_annotation_list[i][:, :,
                                                              0].tostring()

                example = tf.train.Example(features=tf.train.Features(
                    feature={
                        'image_raw': _bytes_feature(img_raw),
                        'mask_raw': _bytes_feature(annotation_raw)
                    }))

                writer.write(example.SerializeToString())
        else:
            img_raw = img.tostring()
            annotation_raw = annotation[:, :, 0].tostring()

            example = tf.train.Example(features=tf.train.Features(
                feature={
                    'image_raw': _bytes_feature(img_raw),
                    'mask_raw': _bytes_feature(annotation_raw)
                }))

            writer.write(example.SerializeToString())
    writer.close()
示例#5
0
	def create_test_patches(self):
		y_conv_argmax = tf.argmax(self.y_conv, 1)
		with self.restore() as sess:
			conn, cur = data.get_db_connection()
			x_batch_test, _, y_batch_test = data.make_batch(conn, cur, 'test', 20)
			y_prediction, test_accuracy = sess.run([y_conv_argmax, self.accuracy], feed_dict={self.x_image:x_batch_test, self.y_:y_batch_test, self.keep_prob: 1.0})

			f, axarr = plt.subplots(2, 10)

			f.suptitle('Test Result of Test Dataset (Accuracy: %f)' % test_accuracy)

			for i in range(0, 20):
				if y_prediction[i] == 0:
					y_label = 'Building'
				elif y_prediction[i] == 1:
					y_label = 'Road'
				elif y_prediction[i] == 2:
					y_label = 'Otherwise'
				else:
					y_label = "???"
				axarr[0, i].imshow(x_batch_test[i]) if i < 10 else axarr[1, i-10].imshow(x_batch_test[i])
				axarr[0, i].set_title(y_label) if i < 10 else axarr[1, i-10].set_title(y_label)

			plt.show()
示例#6
0
                ground_truth = cv2.imread(y_index_filename_list[i])
                ground_truth = y_index_one_hot(ground_truth)
                prediction = cv2.imread(
                    p_filename_list[i][interest_category])[:, :,
                                                           interest_category]
                prediction = prediction > t

                n_TP_sum = n_TP_sum + np.sum((ground_truth == True)
                                             & (prediction == True))
                n_FP_sum = n_FP_sum + np.sum((ground_truth == False)
                                             & (prediction == True))
                n_FN_sum = n_FN_sum + np.sum((ground_truth == True)
                                             & (prediction == False))

            recall = n_TP_sum / (n_TP_sum + n_FN_sum)
            precision = n_TP_sum / (n_TP_sum + n_FP_sum)

            line = '%f,%f,%f\n' % (t, recall, precision)
            print(line)
            f.write(line)


if __name__ == '__main__':
    conn, cur = data.get_db_connection()
    patch_filename_list, _, _ = data.get_patch_all(conn, cur, 'test')

    x_filename_list, y_index_filename_list, p_filename_list = create_prediction_result_patches(
        patch_filename_list)

    for i in range(0, 3):
        recall_precision(y_index_filename_list, p_filename_list, i)
示例#7
0
	def train(self, epoch):
		os.makedirs('tb/%s' % self.model_name)
		os.makedirs('trained_model/%s' % self.model_name)

		tf.summary.image('input x_image', self.x_image)
		tf.summary.scalar('train_accuracy', self.accuracy)
		tf.summary.scalar('cross_entropy', self.cross_entropy)
		tf.summary.scalar('learning rate', self.lr)

		sess = tf.Session()

		sess.run(tf.global_variables_initializer())
		sess.run(tf.local_variables_initializer())

		saver = tf.train.Saver()

		f_log = open('trained_model/%s/log.csv' % self.model_name, 'w')

		merged = tf.summary.merge_all()
		train_writer = tf.summary.FileWriter('/home/lsmjn/Drone-CNN/tb/%s' % self.model_name, sess.graph)

		ngii_dir_training = data.get_ngii_dir('training')
		ngii_dir_test = data.get_ngii_dir('test')

		conn, cur = data.get_db_connection()

		steps = data.get_steps(self.batch_size)

		k = 0

		print('\nCurrent Model: %s' % self.model_name)

		for i in range(0, epoch):
			for j in range(0, steps):
				x_batch, y_batch, _ = data.make_batch(conn, cur, 'training', self.batch_size, 'Building')

				if k%10 == 0:
					print('\nstep %d, epoch %d' % (k, i))
					train_xe, train_accuracy = sess.run([self.cross_entropy, self.accuracy], feed_dict={self.x_image:x_batch, self.y_:y_batch, self.keep_prob: 1.0})
					print('Train XE:')
					print(train_xe)
					print('Train Accuracy:')
					print(train_accuracy)

					for l in range(0, len(ngii_dir_test)):
						dataset_test_name = ngii_dir_test[l][0]
						x_batch_test, y_batch_test, _ = data.make_batch(conn, cur, 'test', self.batch_size, 'Building')
						test_accuracy = sess.run(self.accuracy, feed_dict={self.x_image:x_batch_test, self.y_:y_batch_test, self.keep_prob: 1.0})
						print('Test Accuracy:')
						print(test_accuracy)

					f_log.write('%d,%d,%f,%f,%f\n' % (i, k, train_xe, train_accuracy, test_accuracy))
				if k%self.lr_decay_freq == 0:
					self.lr_value = self.lr_value * 0.1
					print('Learning rate:')
					print(self.lr_value)

				summary, _ = sess.run([merged, self.train_step], feed_dict={self.x_image: x_batch, self.y_: y_batch, self.lr:self.lr_value, self.m:self.m_value, self.keep_prob: 0.5})
				#sess.run(self.train_step, feed_dict={self.x_image: x_batch, self.y_: y_batch, self.lr:self.lr_value, self.m:self.m_value, self.keep_prob: 0.5})
				train_writer.add_summary(summary, k)
				k = k + 1

		cur.close()
		conn.close()

		save_path = saver.save(sess, "trained_model/%s/Drone_CNN.ckpt" % self.model_name)
		print('Model saved in file: %s' % save_path)
		#train_writer.close()
		f_log.close()