示例#1
0
def main():
    PROJECT_PATH = "/rapids/notebooks/host/BM_GPU"
    config_path = f"{PROJECT_PATH}/config_aicrowd.yaml"
    num_videos_per_clusters = 9
    video_duration = 200 # frames

    Data = Dataset(PROJECT_PATH, 'task1_etho', config_path)
    Data.load_data()

    # configuration
    INFO = Data.info
    INFO_values = Data.info_values

    with open(f"{PROJECT_PATH}/config_aicrowd.yaml") as f:
        config = yaml.load(f, Loader=yaml.FullLoader)
    # config = Data.config
    skeleton = config['skeleton']
    skeleton_color= config['skeleton_color']
    skeleton_fill = config['skeleton_fill']
    
    # features
    #tot_bp = Data.data_obj['rotated_bodypoints']
    tot_bp = Data.data_obj['scaled_bodypoints']
    # embeddings
    tot_embed = Data.data_obj['all_embeddings']
    # cluster
    tot_clusters = Data.data_obj['cluster']
    num_clusters = int(np.max(tot_clusters))+1

    # Determine Which frames to Abstract
    video_cluster_idx = {}
    for clust_i in range(num_clusters):
        clust_idx = np.where(tot_clusters==clust_i)[0]
        difference = np.diff(clust_idx)

        # Find consecutive break
        break_idx = np.where(difference != 1)[0]
        mod_break_idx = np.insert(break_idx, 0, 0)
        break_difference = np.diff(mod_break_idx)

        # Find max consecutive
        sorted_idx = np.argsort(break_difference)
        top_idx = sorted_idx[-num_videos_per_clusters:]
        video_idx = np.array([[ clust_idx[mod_break_idx[idx]+1], clust_idx[mod_break_idx[idx+1]+1]] for idx in top_idx])
        video_cluster_idx[clust_i] = video_idx
    
    global_start_frames = np.array([val['global_start_fr'] for val in INFO_values])
    global_stop_frames = np.array([val['global_stop_fr'] for val in INFO_values])
    global_directories = np.array([val['directory'] for val in INFO_values])
    
    for clust_i in range(0, num_clusters):
        # Create video
        fig, ax = plt.subplots(3,3,figsize=(10,10))
        fig.suptitle(f"Cluster {clust_i} Sample Videos")
        fig.tight_layout(rect=[0, 0.03, 1, 0.95])

        # # animal video data
        # video_i, file_start_fr = {}, {}
        # for i, (start, stop) in enumerate(tqdm(video_cluster_idx[clust_i], desc="Collecting Videos")):
        #     file_bool = start > global_start_frames
        #     if any(file_bool):
        #         file_start_fr[i] = global_start_frames[file_bool][-1]
        #         file_path = global_directories[file_bool][-1]
        #         file_key = file_path.split("/")[-1]
        #         video_path = glob(f"{VIDEO_PATH}/{file_key}.avi")[0]
        #         video = skvideo.io.vread(video_path)

        #         video_start = start-file_start_fr[i]
        #         video_stop = video_start+video_duration
        #         if video_stop < len(video):
        #             video_i[i] = video[video_start:video_stop]
        #         else:
        #             video_i[i] = video[video_start:]
        #     else:
        #         return # don't create a video

        # video format        
        FFMpegWriter = animation.writers['ffmpeg']
        writer = FFMpegWriter(fps=25)
        SAVE_PATH=f"videos/task1_etho/mutivideo_cluster{clust_i}.mp4"
        with writer.saving(fig, SAVE_PATH, dpi=300):
            for fr_i in tqdm(np.arange(0, video_duration), desc=f"Cluster {clust_i} Frame Loop"):
                for i, (start, stop) in enumerate(video_cluster_idx[clust_i]):
                    fr = start+fr_i

                    # configure plot
                    ax[i//3,i%3].clear()
                    ax[i//3,i%3].set_axis_off()
                    ax[i//3,i%3].set(title=f"Cluster {int(tot_clusters[fr])}")
                    ax[i//3,i%3].set(xlim=(-3,3), ylim=(-3,3))

                    # ax[i//3,i%3].imshow(video_i[i][fr_i])

                    for skeleton_i, color_i in zip(skeleton, skeleton_color):
                        ax[i//3,i%3].plot(tot_bp[fr,skeleton_i,0], tot_bp[fr,skeleton_i,1], marker="o", markersize=2,
                            linewidth=2, alpha=0.6, c=color_i)
                    for fill_obj in skeleton_fill:   
                        ax[i//3,i%3].add_patch(matplotlib.patches.Polygon(xy=tot_bp[fr,fill_obj['trapezoid'],0:2], fill=True, 
                            alpha=0.7, color=fill_obj['fill']))

                writer.grab_frame()
            plt.close()
示例#2
0
if __name__ == '__main__':

    args = yaml.load(open('./configs/runner.yml', 'r'), Loader=yaml.FullLoader)
    args = {**args, **yaml.load(open('./configs/data.yml', 'r'), Loader=yaml.FullLoader)}

    seed = args["seed"]
    random.seed(seed)
    np.random.seed(seed)
    torch.manual_seed(seed)
    if torch.cuda.is_available():
        torch.cuda.manual_seed_all(seed)

    train_file, test_file, embedding_source = args["train_file"], args["test_file"], args["embedding_source"]

    dataset = Dataset(data_columns=args["data_info"], batch_size=args["batch_size"], seq_len=args["seq_len"])
    dataset.load_data(train_file, test_file, embedding_source)

    vocab_size = len(dataset.vocab)
    embeddings = dataset.embeddings

    model = instantiate_model(args["model"], vocab_size, embeddings)

    # tqdm_range = trange(args["epochs"], desc="Epoch", unit="epoch")
    print("=" * 54)
    for epoch in range(args["epochs"]):
        _, _ = model.run_epoch(dataset.train_batch_loader, dataset.valid_batch_loader)
        print("-" * 54)
        train_acc = model.evaluate(dataset.train_batch_loader)
        val_acc = model.evaluate(dataset.valid_batch_loader)
        print("Epoch %3d ended | train acc.: %3.2f | val acc.: %.2f" % ((epoch + 1), train_acc * 100, val_acc * 100))
        # tqdm_range.set_postfix_str("Loss: %g" % loss)
示例#3
0
def main():
    config_name = sys.argv[1]

    # load configuration and data
    with open(config_name) as f:
        config = yaml.load(f, Loader=yaml.FullLoader)
    PROJECT_PATH = config['GPU_project_path']
    config_path = f"{PROJECT_PATH}/{config_name}"
    Data = Dataset(PROJECT_PATH, config_path)
    Data.load_data()

    # configuration
    INFO = Data.info
    INFO_values = Data.info_values
    skeleton = config['skeleton']
    skeleton_color = config['skeleton_color']
    skeleton_fill = config['skeleton_fill']
    num_videos_per_clusters = config['num_sample_videos']
    video_duration = config['max_video_length']
    video_type = config['video_type']

    # bodypoints
    if video_type == 0:
        tot_bp = Data.data_obj['bodypoints']
    elif video_type == 1:
        tot_bp = Data.data_obj['rotated_bodypoints']
    elif video_type == 2:
        tot_bp = Data.data_obj['scaled_bodypoints']
    # embeddings
    tot_embed = Data.data_obj['all_embeddings']
    # cluster
    tot_clusters = Data.data_obj['cluster']
    num_clusters = int(np.max(tot_clusters)) + 1

    # Determine Which frames to Abstract
    video_cluster_idx = {}
    for clust_i in range(num_clusters):
        sorted_list_idx = sorted((list(y) for (x, y) in itertools.groupby((
            enumerate(tot_clusters)), operator.itemgetter(1)) if x == clust_i),
                                 key=len,
                                 reverse=True)
        top_start_stop_idx = map(lambda x: [x[0][0], x[-1][0]],
                                 sorted_list_idx[0:num_videos_per_clusters])
        video_cluster_idx[clust_i] = np.array(list(top_start_stop_idx))

    global_start_frames = np.array(
        [val['global_start_fr'] for val in INFO_values])
    global_stop_frames = np.array(
        [val['global_stop_fr'] for val in INFO_values])
    global_directories = np.array([val['directory'] for val in INFO_values])

    for clust_i in range(0, num_clusters):
        # Create video
        fig, ax = plt.subplots(3, 3, figsize=(10, 10))
        fig.suptitle(f"Cluster {clust_i} Sample Videos")
        fig.tight_layout(rect=[0, 0.03, 1, 0.95])

        # animal video data
        if video_type == 0:
            video_i, file_start_fr = {}, {}
            for i, (start, stop) in enumerate(
                    tqdm(video_cluster_idx[clust_i],
                         desc="Collecting Videos")):
                file_bool = start > global_start_frames
                if any(file_bool):
                    file_start_fr[i] = global_start_frames[file_bool][-1]
                    file_path = global_directories[file_bool][-1]
                    file_key = file_path.split("/")[-1]
                    video_path = glob(
                        f"{config['raw_video_path']}/{file_key}.avi")[0]
                    video = skvideo.io.vread(video_path)

                    video_start = start - file_start_fr[i]
                    video_stop = video_start + video_duration
                    if video_stop < len(video):
                        video_i[i] = video[video_start:video_stop]
                    else:
                        video_i[i] = video[video_start:]
                else:
                    return  # don't create a video

        # video format
        FFMpegWriter = animation.writers['ffmpeg']
        writer = FFMpegWriter(fps=25)
        SAVE_PATH = f"{config['save_video_path']}/mutivideo_cluster{clust_i}.mp4"
        if not os.path.exists(f"{config['save_video_path']}"):
            os.makedirs(f"{config['save_video_path']}")
        with writer.saving(fig, SAVE_PATH, dpi=300):
            for fr_i in tqdm(np.arange(0, video_duration),
                             desc=f"Cluster {clust_i} Frame Loop"):
                for i, (start, stop) in enumerate(video_cluster_idx[clust_i]):
                    fr = start + fr_i

                    # stop animation if video ends
                    if (video_type == 0) & (fr_i >= len(video_i[i])):
                        continue
                    # configure plot
                    ax[i // 3, i % 3].clear()
                    ax[i // 3, i % 3].set_axis_off()
                    ax[i // 3,
                       i % 3].set(title=f"Cluster {int(tot_clusters[fr])}")

                    if (video_type == 1) | (video_type == 2):
                        ax[i // 3, i % 3].set(xlim=(-3, 3), ylim=(-3, 3))
                    if video_type == 0:
                        ax[i // 3, i % 3].imshow(video_i[i][fr_i])
                    for skeleton_i, color_i in zip(skeleton, skeleton_color):
                        ax[i // 3, i % 3].plot(tot_bp[fr, skeleton_i, 0],
                                               tot_bp[fr, skeleton_i, 1],
                                               marker="o",
                                               markersize=2,
                                               linewidth=2,
                                               alpha=0.6,
                                               c=color_i)
                    for fill_obj in skeleton_fill:
                        ax[i // 3, i % 3].add_patch(
                            matplotlib.patches.Polygon(
                                xy=tot_bp[fr, fill_obj['trapezoid'], 0:2],
                                fill=True,
                                alpha=0.7,
                                color=fill_obj['fill']))
                writer.grab_frame()
            plt.close()
示例#4
0
def main():
    # grab arguments
    config_name = sys.argv[1]

    with open(config_name) as f:
        config = yaml.load(f, Loader=yaml.FullLoader)

    config_path = f"{config['GPU_project_path']}/{config_name}"
    PROJECT_PATH = config['GPU_project_path']
    Data = Dataset(PROJECT_PATH, config_path)
    Data.load_data()

    # configuration
    INFO = Data.info
    INFO_items = list(INFO.items())
    INFO_items.sort(key=lambda x: x[1]['order'])
    config = Data.config

    with open(
            f"{PROJECT_PATH}/{config['result_path']}/angle_scale_model.pickle",
            'rb') as file:
        angle_scaler = pickle.load(file)
    with open(
            f"{PROJECT_PATH}/{config['result_path']}/limb_scale_model.pickle",
            'rb') as file:
        limb_scaler = pickle.load(file)

    # features
    tot_bp = Data.data_obj['rotated_bodypoints'] * config['postural_weight']
    tot_angle = angle_scaler.transform(
        Data.data_obj['angles']) * config['postural_weight']
    tot_limb = limb_scaler.transform(
        Data.data_obj['limbs']) * config['postural_weight']
    tot_marker_pwr = None  # Implement this later maybe
    tot_angle_pwr = Data.data_obj['angle_power'] * config['kinematic_weight']
    tot_limb_pwr = Data.data_obj['limb_power'] * config['kinematic_weight']

    # take out bad frames
    tot_good_fr, tot_bad_fr, tot_disregard_fr = locate_bad_fr(config, tot_bp)

    tot_angle = tot_angle[tot_good_fr]
    tot_limb = tot_limb[tot_good_fr]
    tot_angle_pwr = tot_angle_pwr[tot_good_fr]
    tot_limb_pwr = tot_limb_pwr[tot_good_fr]

    print("******************")
    nan_fr = np.where(np.isnan(tot_limb_pwr))[0]
    print(len(np.unique(nan_fr)))
    print("******************")

    # Postural Embedding
    if config['include_marker_postural']:
        start_timer = time.time()
        print(f"::: Marker Postural ::: START")
        p_marker_embed(config, INFO_items, tot_bp)
        print(
            f"::: Marker Postural ::: Computation Time: {time.time()-start_timer}"
        )
    if config['include_angle_postural']:
        start_timer = time.time()
        print(f"::: Angle Postural ::: START")
        p_angle_embed(config, INFO_items, tot_angle)
        print(
            f"::: Angle Postural  ::: Computation Time: {time.time()-start_timer}"
        )
    if config['include_limb_postural']:
        start_timer = time.time()
        print(f"::: Limb Postural  ::: START")
        p_limb_embed(config, INFO_items, tot_limb)
        print(
            f"::: Limb Postural  ::: Computation Time: {time.time()-start_timer}"
        )
    if config['include_all_postural']:
        start_timer = time.time()
        print(f"::: Angle & Limb Postural ::: START Timer")
        p_angle_limb_embed(config, INFO_items, tot_angle, tot_limb)
        print(
            f"::: Angle & Limb Postural ::: Time Stamp: {time.time()-start_timer}"
        )

    # Kinematic Embedding
    if config['include_marker_kinematic']:
        start_timer = time.time()
        print(f"::: Marker Kinematic ::: START")
        k_marker_embed(config, INFO_items, tot_marker_pwr)
        print(
            f"::: Marker Kinematic ::: Computation Time: {time.time()-start_timer}"
        )
    if config['include_angle_kinematic']:
        start_timer = time.time()
        print(f"::: Angle Kinematic ::: START")
        k_angle_embed(config, INFO_items, tot_angle_pwr)
        print(
            f"::: Angle Kinematic ::: Computation Time: {time.time()-start_timer}"
        )
    if config['include_limb_kinematic']:
        start_timer = time.time()
        print(f"::: Limb Kinematic ::: START")
        k_limb_embed(config, INFO_items, tot_limb_pwr)
        print(
            f"::: Limb Kinematic ::: Computation Time: {time.time()-start_timer}"
        )
    if config['include_all_kinematic']:
        start_timer = time.time()
        print(f"::: Angle & Limb Kinematic ::: START Timer")
        k_angle_limb_embed(config, INFO_items, tot_angle_pwr, tot_limb_pwr)
        print(
            f"::: Angle & Limb Kinematic ::: Time Stamp: {time.time()-start_timer}"
        )

    if config['include_all_features']:
        start_timer = time.time()
        print(f"::: Angle & Limb Kinematic & Postural ::: START Timer")
        kp_angle_limb_embed(config, INFO_items, tot_angle, tot_limb,
                            tot_angle_pwr, tot_limb_pwr)
        print(
            f"::: Angle & Limb Kinematic & Postural ::: Time Stamp: {time.time()-start_timer}"
        )
示例#5
0
def main():
    PROJECT_PATH = "/rapids/notebooks/host/BM_GPU"
    config_path = f"{PROJECT_PATH}/config_aicrowd.yaml"
    Data = Dataset(PROJECT_PATH, 'task1_etho', config_path)
    Data.load_data()

    # configuration
    INFO = Data.info
    INFO_items = list(INFO.items())
    INFO_items.sort(key=lambda x: x[1]['order'])
    config = Data.config

    with open (f"{PROJECT_PATH}/{config['result_path']}/angle_scale_model.pickle", 'rb') as file:
        angle_scaler = pickle.load(file)
    with open (f"{PROJECT_PATH}/{config['result_path']}/limb_scale_model.pickle", 'rb') as file:
        limb_scaler = pickle.load(file)

    # features
    tot_bp = Data.data_obj['rotated_bodypoints']*config['postural_weight']
    tot_angle = angle_scaler.transform(Data.data_obj['angles'])*config['postural_weight']
    tot_limb = limb_scaler.transform(Data.data_obj['limbs'])*config['postural_weight']
    tot_marker_pwr = None # Implement this later maybe
    tot_angle_pwr = Data.data_obj['angle_power']*config['kinematic_weight']
    tot_limb_pwr = Data.data_obj['limb_power']*config['kinematic_weight']

    # Postural Embedding
    if config['include_marker_postural']:
        start_timer = time.time()
        print(f"::: Marker Postural ::: START")
        p_marker_embed(config, INFO_items, tot_bp)
        print(f"::: Marker Postural ::: Computation Time: {time.time()-start_timer}")
    if config['include_angle_postural']:
        start_timer = time.time()
        print(f"::: Angle Postural ::: START")
        p_angle_embed(config, INFO_items, tot_angle)
        print(f"::: Angle Postural  ::: Computation Time: {time.time()-start_timer}")
    if config['include_limb_postural']:
        start_timer = time.time()
        print(f"::: Limb Postural  ::: START")
        p_limb_embed(config, INFO_items, tot_limb)
        print(f"::: Limb Postural  ::: Computation Time: {time.time()-start_timer}")
    if config['include_all_postural']:
        start_timer = time.time()
        print(f"::: Angle & Limb Postural ::: START Timer")
        p_angle_limb_embed(config, INFO_items, tot_angle, tot_limb)
        print(f"::: Angle & Limb Postural ::: Time Stamp: {time.time()-start_timer}")

    # Kinematic Embedding
    if config['include_marker_kinematic']:
        start_timer = time.time()
        print(f"::: Marker Kinematic ::: START")
        k_marker_embed(config, INFO_items, tot_marker_pwr)
        print(f"::: Marker Kinematic ::: Computation Time: {time.time()-start_timer}")
    if config['include_angle_kinematic']:
        start_timer = time.time()
        print(f"::: Angle Kinematic ::: START")
        k_angle_embed(config, INFO_items, tot_angle_pwr)
        print(f"::: Angle Kinematic ::: Computation Time: {time.time()-start_timer}")
    if config['include_limb_kinematic']:
        start_timer = time.time()
        print(f"::: Limb Kinematic ::: START")
        k_limb_embed(config, INFO_items, tot_limb_pwr)
        print(f"::: Limb Kinematic ::: Computation Time: {time.time()-start_timer}")
    if config['include_all_kinematic']:
        start_timer = time.time()
        print(f"::: Angle & Limb Kinematic ::: START Timer")
        k_angle_limb_embed(config, INFO_items, tot_angle_pwr, tot_limb_pwr)
        print(f"::: Angle & Limb Kinematic ::: Time Stamp: {time.time()-start_timer}")

    if config['include_all_features']:
        start_timer = time.time()
        print(f"::: Angle & Limb Kinematic & Postural ::: START Timer")
        kp_angle_limb_embed(config, INFO_items, tot_angle, tot_limb, tot_angle_pwr, tot_limb_pwr)
        print(f"::: Angle & Limb Kinematic & Postural ::: Time Stamp: {time.time()-start_timer}")