def plot_non_mask_rate_iou(self, model, dataset, metric, epoch): run_id = model.run_id test_y = self.data_collection.test_y_dec test_predict = self.data_collection.test_predict_dec size = 512 test_y = test_y[:size] test_predict = test_predict[:size] xs = masks_rate(test_y) xs = xs.reshape([-1]) xs /= 255 ys = np.array([ Metrics.miou(true, predict) for true, predict in zip(test_y, test_predict) ]) dots = np.array([[x, y] for x, y in zip(xs, ys)]) self.plot.scatter_2d(dots, title=f'test set mask rate and iou', path=path_join( PLOT_PATH, run_id, f'test_set_mask_rate_iou/({epoch}).png'), x_label='mask_rate', y_label='iou')
def test_TGS_salt_metric(): print(f'collect train mask images') train_mask_images, _, _ = collect_images(TRAIN_MASK_PATH) train_mask_images = train_mask_images.reshape([-1, 101, 101]) idx = 10 size = 10 images = train_mask_images[idx:idx + size] metric_images = [] for a in images: for b in images: a = a.reshape([101, 101, 1]) b = b.reshape([101, 101, 1]) zero_channel = np.zeros([101, 101, 1]) rgb_image = np.concatenate([a, b, zero_channel], axis=2) metric_images += [rgb_image] metric_images = np.array(metric_images) plot.plot_image_tile(metric_images, title='test_metric', column=size) metric_score = [] for a in images: for b in images: metric_score += [Metrics.TGS_salt_score(a, b)] metric_score = np.array(metric_score).reshape([size, size]) print(metric_score)
def fold_score(self, k=5): models = [] train_TGS_scores = [] valid_TGS_scores = [] train_mious = [] valid_mious = [] for i in range(k): path = f'./instance/TGS_salt/SS/baseline/fold_{i}' model = self.load_model(path) models += model datas = self.prepare_set(k=5, index=i) train_x_enc, train_y_enc, valid_x_enc, valid_y_enc = self.encode_datas( datas) train_predict = model.predict(train_x_enc) valid_predict = model.predict(valid_y_enc) train_TGS_score = Metrics.TGS_salt_score(train_y_enc, train_predict) valid_TGS_score = Metrics.TGS_salt_score(valid_y_enc, valid_predict) train_iou_score = Metrics.miou(train_y_enc, train_predict) valid_iou_score = Metrics.miou(valid_y_enc, valid_predict) train_TGS_scores += [train_TGS_score] valid_TGS_scores += [valid_TGS_score] train_mious += [train_iou_score] valid_mious += [valid_iou_score] def print_score(scores): for i, score in enumerate(scores): print(i, score) print(f'train TGS score') print_score(train_TGS_scores) print(f'valid TGS score') print_score(valid_TGS_scores) print(f'train ious') print_score(train_mious) print(f'valid ious') print_score(valid_mious)
def log_score(self, k, index): baseline = self.model datas = self.prepare_set(k, index) train_x_enc, train_y_enc, valid_x_enc, valid_y_enc = self.encode_datas( datas) train_predict = baseline.predict(train_x_enc) valid_predict = baseline.predict(valid_x_enc) train_TGS_score = Metrics.TGS_salt_score(train_y_enc, train_predict) valid_TGS_score = Metrics.TGS_salt_score(valid_y_enc, valid_predict) train_iou_score = Metrics.miou(train_y_enc, train_predict) valid_iou_score = Metrics.miou(valid_y_enc, valid_predict) print(f'train TGS score = {train_TGS_score}\n' f'test TGS score = {valid_TGS_score}\n' f'train miou = {train_iou_score}\n' f'test miou = {valid_iou_score}\n' # f'train loss = {train_loss}\n' # f'test loss = {test_loss}\n' )
def test_set_sorted_by_mask_rate(self): pipe = self.load_baseline() baseline = pipe.model train_predict = baseline.predict(pipe.train_x) train_predict = mask_label_encoder.from_label(train_predict) test_predict = baseline.predict(pipe.valid_x) test_predict = mask_label_encoder.from_label(test_predict) train_loss = baseline.metric(pipe.train_x, pipe.train_y_encode) test_loss = baseline.metric(pipe.valid_x, pipe.valid_y_encode) train_proba = baseline.predict_proba(pipe.train_x) test_proba = baseline.predict_proba(pipe.valid_x) train_score = Metrics.TGS_salt_score(pipe.train_y, train_predict) test_score = Metrics.TGS_salt_score(pipe.valid_y, test_predict) train_mask_rate = masks_rate(pipe.train_y) test_mask_rate = masks_rate(pipe.valid_y) train_miou = Metrics.miou(pipe.train_y, train_predict) test_miou = Metrics.miou(pipe.valid_y, test_predict) train_ious = Metrics.iou(pipe.train_y, train_predict) test_ious = Metrics.iou(pipe.valid_y, test_predict) test_x = pipe.valid_x test_y = pipe.valid_y test_predict = test_predict test_mask_rate = test_mask_rate zipped = zip(test_x, test_y, test_predict, test_mask_rate) sort = list(sorted(zipped, key=lambda a: a[3])) test_x, test_y, test_predict, test_mask_rate = zip(*sort) test_x = np.array(test_x) test_y = np.array(test_y) test_predict = np.array(test_predict) test_mask_rate = np.array(test_mask_rate) test_x_image = test_x[:, :, :, 0] test_x_image = test_x_image.reshape([-1, 101, 101, 1]) test_y = test_y.reshape([-1, 101, 101, 1]) test_predict = test_predict.reshape([-1, 101, 101, 1]) # plot_path = f'./matplot/baseline' tile_image = self.scramble_column(test_x_image, test_y, test_predict) plot.plot_image_tile( tile_image, title='all', path=f'./matplot/baseline/test_set_sorted_by_mask_rate.png')
def update_data(self, model, dataset, metric, epoch): def decode(x): return x * 255 self.train_loss = metric # train_x_enc, train_y_enc = dataset.next_batch(500) # self.train_x_enc = train_x_enc # self.train_x_dec = decode(self.train_x_enc) # self.train_y_enc = train_y_enc # self.train_y_dec = decode(self.train_y_enc) # self.train_predict_enc = model.predict(self.train_x_enc) # self.train_predict_dec = decode(self.train_predict_enc) self.test_predict_enc = model.predict(self.test_x_enc) self.test_predict_dec = decode(self.test_predict_enc) # self.train_score = Metrics.TGS_salt_score(self.train_y_enc, self.train_predict_enc) self.test_score = Metrics.TGS_salt_score(self.test_y_enc, self.test_predict_enc)
def log_split_mask_rate_score(self): pipe = self.load_baseline() baseline = pipe.model helper = pipe.data_helper train_set = helper.train_set_non_empty_mask_with_depth_image_under_1p train_set.x_keys = ['x_with_depth'] x, y = train_set.full_batch() predict = baseline.predict(x) score_under_1 = Metrics.TGS_salt_score( y, mask_label_encoder.from_label(predict)) iou_score_under_1 = Metrics.miou( y, mask_label_encoder.from_label(predict)) train_set = helper.train_set_non_empty_mask_with_depth_image_under_5p train_set.x_keys = ['x_with_depth'] x, y = train_set.full_batch() predict = baseline.predict(x) score_under_5 = Metrics.TGS_salt_score( y, mask_label_encoder.from_label(predict)) iou_score_under_5 = Metrics.miou( y, mask_label_encoder.from_label(predict)) train_set = helper.train_set_non_empty_mask_with_depth_image_under_10p train_set.x_keys = ['x_with_depth'] x, y = train_set.full_batch() predict = baseline.predict(x) score_under_10 = Metrics.TGS_salt_score( y, mask_label_encoder.from_label(predict)) iou_score_under_10 = Metrics.miou( y, mask_label_encoder.from_label(predict)) train_set = helper.train_set_non_empty_mask_with_depth_image_under_20p train_set.x_keys = ['x_with_depth'] x, y = train_set.full_batch() predict = baseline.predict(x) score_under_20 = Metrics.TGS_salt_score( y, mask_label_encoder.from_label(predict)) iou_score_under_20 = Metrics.miou( y, mask_label_encoder.from_label(predict)) train_set = helper.train_set_non_empty_mask_with_depth_image_upper_1p train_set.x_keys = ['x_with_depth'] x, y = train_set.full_batch() predict = baseline.predict(x) score_upper_1 = Metrics.TGS_salt_score( y, mask_label_encoder.from_label(predict)) iou_score_upper_1 = Metrics.miou( y, mask_label_encoder.from_label(predict)) train_set = helper.train_set_non_empty_mask_with_depth_image_upper_5p train_set.x_keys = ['x_with_depth'] x, y = train_set.full_batch() predict = baseline.predict(x) score_upper_5 = Metrics.TGS_salt_score( y, mask_label_encoder.from_label(predict)) iou_score_upper_5 = Metrics.miou( y, mask_label_encoder.from_label(predict)) train_set = helper.train_set_non_empty_mask_with_depth_image_upper_10p train_set.x_keys = ['x_with_depth'] x, y = train_set.full_batch() predict = baseline.predict(x) score_upper_10 = Metrics.TGS_salt_score( y, mask_label_encoder.from_label(predict)) iou_score_upper_10 = Metrics.miou( y, mask_label_encoder.from_label(predict)) train_set = helper.train_set_non_empty_mask_with_depth_image_upper_20p train_set.x_keys = ['x_with_depth'] x, y = train_set.full_batch() predict = baseline.predict(x) score_upper_20 = Metrics.TGS_salt_score( y, mask_label_encoder.from_label(predict)) iou_score_upper_20 = Metrics.miou( y, mask_label_encoder.from_label(predict)) print(f'\n' f' TGS score\n' f'under 1 {score_under_1}\n' f'under 5 {score_under_5}\n' f'under 10 {score_under_10}\n' f'under 20 {score_under_20}\n' f'upper 1 {score_upper_1}\n' f'upper 5 {score_upper_5}\n' f'upper 10 {score_upper_10}\n' f'upper 20 {score_upper_20}\n' f'\n' f'\n' f' iou score\n' f'under 1 {iou_score_under_1}\n' f'under 5 {iou_score_under_5}\n' f'under 10 {iou_score_under_10}\n' f'under 20 {iou_score_under_20}\n' f'upper 1 {iou_score_upper_1}\n' f'upper 5 {iou_score_upper_5}\n' f'upper 10 {iou_score_upper_10}\n' f'upper 20 {iou_score_upper_20}\n' f'\n')