def make_tfrecords_list(self, sample_list, ext='.tfrecord'): """ sample_list is orig sample_list from split file eg. sample_list[i] = ['ApplyEyeMakeup/v_ApplyEyeMakeup_g01_c01', '1'] to make the tfrecords folder flat, replace '/' to '@' :return list[i] = '/absolute/datasets/UCF101frames_tfrecords/YoYo@v_YoYo_g08_c01.tfrecord' """ baseio.check_or_create_path(self.tfrecords_dpath, create=True, show=False) return [ osp.join(self.tfrecords_dpath, i[0].replace('/', '@') + ext) for i in sample_list ]
def main(results_draw=RESULTS_DRAW, gt_folder_path=GT_FOLDER_PATH, save_dir_path=SAVE_DIR_PATH): to_draw_list = basepy.get_1tier_file_path_list( results_draw, suffix=get_charactor(results_draw)[1]) to_draw_list = sorted(to_draw_list, key=lambda x: int(osp.basename(x).split('.')[0])) video_gt_path = osp.join(gt_folder_path, osp.basename(results_draw)) save_img_path = basepy.check_or_create_path( osp.join(save_dir_path, osp.basename(results_draw))) image_size = get_charactor(results_draw)[2] for frame in to_draw_list: img = cv2.imread(frame) img_gt_file = osp.join(video_gt_path, osp.basename(frame).split('.')[0] + '.txt') if osp.exists(img_gt_file): spatial_annotations = [ yoloLine2Shape(image_size, k[1], k[2], k[3], k[4]) for k in basepy.read_txt_lines2list(img_gt_file, ' ') ] for y0, x0, y1, x1 in spatial_annotations: cv2.rectangle(img, (y0, x0), (y1, x1), (53, 134, 238), 1) save_frame = osp.join(save_img_path, osp.basename(frame)) cv2.imwrite(save_frame, img)
def decode_flags(F, npy_reformed_file_path): # set np height from 'anoma_motion_reformed_pyramid_120_85_4region_maxtop_1000_c3d_npy' segment_num = int(npy_reformed_file_path.split('_')[-3]) segment_num = segment_num * 4 if '4region' in npy_reformed_file_path else segment_num timestamp = time.strftime("%y%m%d%H%M%S", time.localtime()) save_file_path = osp.join('/absolute/tensorflow_models', timestamp + '_' + osp.basename(npy_reformed_file_path), timestamp + '.ckpt') json_file_path = osp.join(basepy.check_or_create_path(osp.dirname(save_file_path), show=True), 'keys.json') D = basepy.DictCtrl(zdefault_dict.EXPERIMENT_KEYS).save2path(json_path=json_file_path, batch_size=F.batch_size, epoch_num=F.epoch_num, learning_rate_base=F.learning_rate_base, moving_average_decay=F.moving_average_decay, regularization_scale=F.regularization_scale, npy_file_path=npy_reformed_file_path, segment_num=segment_num, set_gpu=F.set_gpu, lambda1=0.00008, lambda2=0.00008, fusion=F.fusion, lasting=F.lasting, testing_list=F.testing_list, saving_interval=F.saving_interval) _ = [print('Preparing training ...... D values:')] + [print(' ', i, ":", D[i]) for i in D] return D, save_file_path
def main(_): _ = basepy.check_or_create_path(CLIPS_JSON_PATH, create=True, show=True) # write tfrecords # sample_path_list = basepy.get_2tier_folder_path_list(DATASET_PATH) remaining_list, _ = basepy.get_remaining_to_multi( basepy.get_2tier_folder_path_list(DATASET_PATH), basepy.get_1tier_file_path_list(CLIPS_JSON_PATH), if_print=True) random.shuffle(remaining_list) print('FRAME_SIZE :', FRAME_SIZE) if TYPE != 'original': print('EDGE :', EDGE) print('AREA_CROPS :', AREA_CROPS) print('TRACK_WINDOW :', TRACK_WINDOW) print('CLIPS_JSON_PATH :', CLIPS_JSON_PATH) # single processing # write_all_clips2json(remaining_list, CLIPS_JSON_PATH) if F.multiprocessing: basepy.non_output_multiprocessing(write_all_clips2json, remaining_list, CLIPS_JSON_PATH, num=int(mp.cpu_count())) else: write_all_clips2json(remaining_list, CLIPS_JSON_PATH) print('------ Finish ------ Debug Symbol ------ %s ------' % time.asctime(time.localtime(time.time())))
def eval_one_ckpt(merged_keys, merged_features, d, ckpt_file=None, npy_folder_suffix='_eval_json'): if not osp.exists(ckpt_file + npy_folder_suffix): with tf.name_scope('input'): input_test = tf.placeholder(tf.float32, [1, 1, d['feature_len']], name='anom') with tf.name_scope('forward-propagation'): score_anomaly = base.network_fn(input_test, fusion=d['fusion'], feature_len=d['feature_len'], segment_num=1, is_training=False) saver = tf.train.Saver() init_op = tf.global_variables_initializer() os.environ["CUDA_VISIBLE_DEVICES"] = d['set_gpu'] os.environ['TF_CPP_MIN_LOG_LEVEL'] = '1' gpu_options = tf.GPUOptions(allow_growth=True) config = tf.ConfigProto(gpu_options=gpu_options) with tf.Session(config=config) as sess: sess.run(init_op) saver.restore(sess, ckpt_file) # print('Program begins, timestamp %s' % time.asctime(time.localtime(time.time()))) step, dict_key, dict_key_json, height = 0, None, [], None while merged_keys[step]: s = sess.run(score_anomaly, feed_dict={ input_test: np.expand_dims(np.expand_dims( merged_features[step][:4096], axis=0), axis=0) }) class_name, video_name = merged_keys[step].split('@') line_to_json = [ class_name, video_name ] + merged_features[step][4096:].tolist() + s.tolist()[0] if merged_keys[step] != dict_key: dict_key = merged_keys[step] height = [ clip for clip in merged_keys[:-1] if clip == dict_key ].__len__() dict_key_json.append(line_to_json) else: dict_key_json.append(line_to_json) if dict_key_json.__len__() == height: eval_npy_path = basepy.check_or_create_path( ckpt_file + npy_folder_suffix) with open(osp.join(eval_npy_path, dict_key + '.json'), 'w') as f: json.dump(dict_key_json, f) # print('%s done.' % merged_keys[step]) dict_key, dict_key_json, height = None, [], None step += 1
def draw_folder(video_folder, save_folder): frames_path = basepy.get_1tier_file_path_list(video_folder, suffix='.jpg') frames_list = sorted(frames_path, key=lambda x: int(osp.basename(x).split('.')[0])) save_folder_video = basepy.check_or_create_path( osp.join(save_folder, osp.basename(video_folder))) for j, i in enumerate(frames_list[:-1]): draw_one(i, frames_list[j + 1], save_folder_video)
def show_something(score_and_truth_fig_save_path, deflated_length, inflate, temporal_score, temporal_truth, video_name): _ = basepy.check_or_create_path(score_and_truth_fig_save_path) x = np.linspace(0, deflated_length * inflate, deflated_length) y1 = np.array(temporal_score) y2 = np.array(temporal_truth) # plt.figure() plt.plot(x, y1) plt.plot(x, y2) plt.ylim(0, 1.1) plt.xlim(0, deflated_length * inflate + 4) plt.title(video_name) plt.xlabel('Frame number') plt.ylabel('Anomaly score') plt.savefig(osp.join(score_and_truth_fig_save_path, video_name) + '.png') plt.close()
def main(_): remaining_list, split_list = basepy.get_remaining_to_multi( basepy.get_1tier_file_path_list(JSON_FILE_FOLDER), basepy.get_1tier_file_path_list( basepy.check_or_create_path(EVAL_RESULT_FOLDER), suffix='.npy'), divide_num=SPLIT_NUM, if_print=True) print('Converting %s to %s' % (JSON_FILE_FOLDER, EVAL_RESULT_FOLDER)) # run_test(remaining_list, DATASET_PATH, EVAL_RESULT_FOLDER, BATCH_SIZE, GPU_LIST[0]) split_list = [i for i in split_list if i != []] p = mp.Pool(split_list.__len__()) for j, em in enumerate(split_list): p.apply_async(run_test, args=(em, DATASET_PATH, EVAL_RESULT_FOLDER, BATCH_SIZE, GPU_LIST[j])) p.close() p.join()
def devide_long_video_folder(json_file='./temp/video_info.json'): try: with open(json_file, 'r') as f: info = json.load(f) except FileNotFoundError: info = get_video_info_json() norm = [(i[0], i[1]) for i in info if 'normal' in i[0].lower()] anom = [(i[0], i[1]) for i in info if 'normal' not in i[0].lower()] norm = sorted(norm, key=lambda x: x[1][0], reverse=True) anom = sorted(anom, key=lambda x: x[1][0], reverse=True) max_anom = anom[0][1][0] need_divide = [i for i in norm if i[1][0] > max_anom] print('Max anomaly length is %d' % max_anom) print('Videos to be divided:') _ = [ print(' ' + i[0] + ', original length: ' + str(i[1][1])) for i in need_divide ] for video_name in need_divide: # ('Normal_Videos308_x264', [976504, 976504]) video_folder = [i for i in VIDEO_FOLDER_SET if video_name[0] in i][0] sep = int(video_name[1][0] / 100000 + 1) frame_list = basepy.get_1tier_file_path_list(video_folder) frame_list = sorted(frame_list, key=lambda x: int(osp.basename(x).split('.')[0])) print('Dividing %s to %d splits' % (video_folder, sep)) index = [int(i * len(frame_list) / sep) for i in range(sep + 1)] for i in range(sep): new_folder = basepy.check_or_create_path( video_folder.replace('_x264', '_%i_x264' % i)) _ = [ shutil.move(j, new_folder) for j in frame_list[index[i]:index[i + 1]] ] os.rmdir(video_folder) print('Dividing done. All empty original folders removed.') print('------ Finish ------ Debug Symbol ------ %s ------' % time.asctime(time.localtime(time.time())))
def main(): _ = basepy.check_or_create_path(CLIPS_TFRECS_PATH, create=True, show=True) # write tfrecords sample_path_list = basepy.get_1tier_file_path_list(JSON_FILE_PATH, suffix='.json') random.shuffle(sample_path_list) test_list = sample_path_list[0:5] t1 = time.time() # single processing write_tfrecords(test_list, CLIPS_TFRECS_PATH, DATASET_PATH, REDUCE_METHOD, REDUCE_NUM, CLIP_LEN) t2=time.time() # basepy.non_output_multiprocessing(write_tfrecords, sample_path_list[1:2], CLIPS_TFRECS_PATH, DATASET_PATH, # REDUCE_METHOD, REDUCE_NUM, CLIP_LEN, num=int(mp.cpu_count())) see = direct_json_to_np(test_list, CLIPS_TFRECS_PATH, DATASET_PATH, REDUCE_METHOD, REDUCE_NUM, CLIP_LEN) t3 = time.time() print('%f, %f' % (t2-t1, t3-t2)) print('------ Finish ------ Debug Symbol ------ %s ------' % time.asctime(time.localtime(time.time())))
def draw_spatial(info_all, data_path='/absolute/datasets/UCSDped2_reform', save_path='./temp/UCSDped2_spatial', select_id=2): suffix = '.tif' if 'ucsd' in data_path.lower() else '.jpg' inflate = 8 if 'ucsd' in data_path.lower() else 16 for one_info in info_all: class_name, video_name, frame_index, _, \ a1_c, a1_r, a1_w, a1_h, a2_c, a2_r, a2_w, a2_h, \ _, _, anomaly_score = one_info if select_id == 2: area = (a2_c, a2_r, a2_c + a2_w, a2_r + a2_h) elif select_id == 1: area = (a1_c, a1_r, a1_c + a1_w, a1_r + a1_h) else: raise ValueError('Wrong select in results %d' % select_id) frame_index, area = int(frame_index), [int(i) for i in area] video_orig_path = osp.join(data_path, class_name, video_name) # print(video_orig_path) frame_sorted = sorted(basepy.get_1tier_file_path_list(video_orig_path, suffix=suffix), key=lambda x: int(osp.basename(x).split('.')[0])) which_frame = frame_sorted[frame_index + int(inflate / 2)] save_frame = osp.join(save_path, video_name, osp.basename(which_frame)) # save_frame = which_frame.replace(data_path,save_path) _ = basepy.check_or_create_path(osp.dirname(save_frame)) img = cv2.imread(save_frame) if osp.isfile(save_frame) else cv2.imread( which_frame) cv2.rectangle(img, (area[0], area[1]), (area[2], area[3]), (int(255 * anomaly_score), 0, 0), 1) font = cv2.FONT_HERSHEY_PLAIN text = '%.4f' % anomaly_score cv2.putText(img, text, (area[0], area[1]), font, 1, (int(255 * anomaly_score), 0, 0), 1) cv2.imwrite(save_frame, img)
def npy_reform(npy_file_folder_path, multiscale, multiregion, reform_type, reform_num, if_multiprocessing, test_file): try: results_folder_path = npy_file_folder_path.replace('_motion_', '_motion_reformed_') \ .replace('_pyramid_', '_%s_' % multiscale) \ .replace('_c3d_npy', '_%dregion_c3d_npy' % multiregion) except: results_folder_path = npy_file_folder_path.replace( '_motion_', '_motion_reformed_') results_folder_path = results_folder_path.replace( '_c3d_npy', '_%s_%d_c3d_npy' % (reform_type, reform_num)) test_str = str(basepy.read_txt_lines2list(test_file, sep=' ')) print('Converting %s to %s :' % (npy_file_folder_path, results_folder_path)) multiprocessing_num = int(mp.cpu_count() / 4) remaining_list, split_list = basepy.get_remaining_to_multi( basepy.get_1tier_file_path_list(npy_file_folder_path, '.npy'), basepy.get_1tier_file_path_list( basepy.check_or_create_path(results_folder_path), suffix='.npy'), divide_num=multiprocessing_num, if_print=True) # npy_list_preprocessing(remaining_list, EVAL_RESULT_FOLDER, MULTISCALE, MULTIREGION) if if_multiprocessing: p = mp.Pool(multiprocessing_num) for j, em in enumerate(split_list): p.apply_async(npy_list_preprocessing, args=(em, results_folder_path, multiscale, multiregion, reform_type, reform_num, test_str)) p.close() p.join() else: npy_list_preprocessing(remaining_list, results_folder_path, multiscale, multiregion, reform_type, reform_num, test_str) # END print('Converting DONE ------ Debug Symbol ------ %s ------' % time.asctime(time.localtime(time.time()))) return results_folder_path
def write_tfrecords(json_path_list, tfrecords_path=CLIPS_TFRECS_PATH, dataset_path=DATASET_PATH, reduce_method=REDUCE_METHOD, reduce_num=REDUCE_NUM, clip_len=CLIP_LEN): for json_path in json_path_list: # eg. json_path: '/absolute/datasets/anoma_motion_all_json_type_1/normal_train@Normal_Videos308_3_x264.json' # get original list form json: with open(json_path, 'r') as f: clips_info = json.load(f) # how to reduce clip json if reduce_method == 'simple': clips_info = [ i for i in clips_info if i[2] % clip_len == 0] if len(clips_info) > reduce_num * 3 else clips_info clips_info = sorted(clips_info, key=lambda x: x[-1], reverse=True)[:reduce_num] else: raise ValueError('Wrong reduce method: %s' % reduce_method) tfrecord_name = str(osp.basename(json_path).split('.')[0]) class_name, video_name = tfrecord_name.split('@') tfrecord_path = osp.join(tfrecords_path, tfrecord_name + '.tfr') motioncv_path = basepy.check_or_create_path(osp.join(tfrecords_path, tfrecord_name)) original_video_path = osp.join(dataset_path, class_name, video_name) frames_path = basepy.get_1tier_file_path_list(original_video_path, suffix='.jpg') frame_list = sorted(frames_path, key=lambda x: int(osp.basename(x).split('.')[0])) # clean wrong .tfrecord file: exists but 0 bytes if osp.isfile(tfrecord_path) and osp.getsize(tfrecord_path) == 0: os.remove(tfrecord_path) # if tfrecord not exist, then write one if not osp.isfile(tfrecord_path): writer = tf.python_io.TFRecordWriter(tfrecord_path) for _, _, index, nj, c, r, w, h, _ in clips_info: frames = [cv2.imread(frame_list[index + i])[r:r + h, c:c + w] for i in range(clip_len)] frame_crops_path = [ osp.join(motioncv_path, tfrecord_name + '_' + str(index) + '_' + str(nj) + '_' + str(i) + '.jpg') for i in range(clip_len)] _ = [cv2.imwrite(frame_crops_path[i], frames[i]) for i in range(clip_len)] image_raw_array = [tf.gfile.FastGFile(i, 'rb').read() for i in frame_crops_path] example = tf.train.Example(features=tf.train.Features( feature={'class_name': basetf.bytes_feature(tf.compat.as_bytes(class_name)), 'video_name': basetf.bytes_feature(tf.compat.as_bytes(video_name)), 'frame_index': basetf.int64_feature(index), 'crop_area': basetf.int64_feature(nj), 'c': basetf.int64_feature(c), 'r': basetf.int64_feature(r), 'w': basetf.int64_feature(w), 'h': basetf.int64_feature(h), 'frame0': basetf.bytes_feature(image_raw_array[0]), 'frame1': basetf.bytes_feature(image_raw_array[1]), 'frame2': basetf.bytes_feature(image_raw_array[2]), 'frame3': basetf.bytes_feature(image_raw_array[3]), 'frame4': basetf.bytes_feature(image_raw_array[4]), 'frame5': basetf.bytes_feature(image_raw_array[5]), 'frame6': basetf.bytes_feature(image_raw_array[6]), 'frame7': basetf.bytes_feature(image_raw_array[7]), 'frame8': basetf.bytes_feature(image_raw_array[8]), 'frame9': basetf.bytes_feature(image_raw_array[9]), 'frame10': basetf.bytes_feature(image_raw_array[10]), 'frame11': basetf.bytes_feature(image_raw_array[11]), 'frame12': basetf.bytes_feature(image_raw_array[12]), 'frame13': basetf.bytes_feature(image_raw_array[13]), 'frame14': basetf.bytes_feature(image_raw_array[14]), 'frame15': basetf.bytes_feature(image_raw_array[15]), })) writer.write(example.SerializeToString()) writer.close()