vae_size[1]), axis=0) pred = sess.run([model.z_mean, model.z_sigma], feed_dict={ model.inputs['X']: img2encode, model.trainable: False }) encoded = np.zeros((1, model.latent_num * 2)) encoded[0, :model.latent_num] = pred[0][0, :] encoded[0, model.latent_num:] = pred[1][0, :] # compute llh llh = np.zeros(5) for i in range(5): llh[i] = gmm_models[i].score_samples(encoded)[0] llh_mask[i].append(llh[i] * np.ones( (patch_size[0], patch_size[1], 1))) # get mask plt.figure(figsize=(15, 5)) for i in range(5): conf_map = uabUtilreader.un_patchify(np.stack(llh_mask[i], axis=0), tile_dim, patch_size, 0) plt.subplot(151 + i) plt.imshow(conf_map[:, :, 0]) plt.axis('off') plt.tight_layout() #plt.savefig(os.path.join(img_dir, '{}_{}_{}_gmm_all.png'.format(city_name, city_id, model_name))) plt.show()
def run(self, train_reader=None, valid_reader=None, test_reader=None, pretrained_model_dir=None, layers2load=None, isTrain=False, img_mean=np.array((0, 0, 0), dtype=np.float32), verb_step=100, save_epoch=5, gpu=None, tile_size=(5000, 5000), patch_size=(572, 572), truth_val=1, continue_dir=None, load_epoch_num=None, fineTune=False, valid_iou=False, best_model=True): if gpu is not None: os.environ['CUDA_DEVICE_ORDER'] = 'PCI_BUS_ID' os.environ['CUDA_VISIBLE_DEVICES'] = str(gpu) if isTrain: coord = tf.train.Coordinator() with tf.Session(config=self.config) as sess: # init model init = [ tf.global_variables_initializer(), tf.local_variables_initializer() ] sess.run(init) saver = tf.train.Saver(var_list=tf.global_variables(), max_to_keep=1) # load model if pretrained_model_dir is not None: if layers2load is not None: self.load_weights(pretrained_model_dir, layers2load) else: if not fineTune: restore_var = [ v for v in tf.global_variables() if 'resnet_v1' in v.name and not 'Adam' in v.name ] loader = tf.train.Saver(var_list=restore_var) self.load(pretrained_model_dir, sess, loader, epoch=load_epoch_num) else: self.load(pretrained_model_dir, sess, saver, epoch=load_epoch_num) threads = tf.train.start_queue_runners(coord=coord, sess=sess) try: train_summary_writer = tf.summary.FileWriter( self.ckdir, sess.graph) self.train('X', 'Y', self.n_train, sess, train_summary_writer, n_valid=self.n_valid, train_reader=train_reader, valid_reader=valid_reader, image_summary=util_functions.image_summary, img_mean=img_mean, verb_step=verb_step, save_epoch=save_epoch, continue_dir=continue_dir, valid_iou=valid_iou) finally: coord.request_stop() coord.join(threads) saver.save(sess, '{}/model.ckpt'.format(self.ckdir), global_step=self.global_step) else: kernel = _window_2D(patch_size[0] + 182, patch_size[0]) with tf.Session() as sess: init = tf.global_variables_initializer() sess.run(init) self.load(pretrained_model_dir, sess, epoch=load_epoch_num, best_model=best_model) self.model_name = pretrained_model_dir.split('/')[-1] result = self.test('X', sess, test_reader) n_patch = result.shape[0] for patch_cnt in range(n_patch): result[patch_cnt, :, :, :] *= kernel image_pred = uabUtilreader.un_patchify(result, tile_size, patch_size) return image_pred