示例#1
0
def run(which,
        model,
        optimizer,
        epoch,
        training_mode='change_points',
        validation_mode='sequential',
        model_num=None,
        experiment_number=None,
        label_output='double'):
    _loss_steps = []
    assert (which in ['train', 'test', 'val'])
    assert validation_mode in ['sequential', 'random']
    assert training_mode in [
        'far', 'close', 'both', 'change_points'
    ]  # far = frames are >1 apart, close = frames are 1 apart
    assert label_output in [
        'single', 'double'
    ]  # single= 1 output, double= 2 outputs (class+reg)

    val_idx = None
    steps = None
    if which == 'train':
        val_idx = valid_story_idx_all[0]
        steps = train_total_steps
    elif which == 'val':
        val_idx = valid_story_idx_all[1]
        steps = val_total_steps
    elif which == 'test':
        raise NotImplemented

    # print('%s, steps: %d' % (which, steps))

    if which == 'train':
        # for s in tqdm(range(steps)):
        for s in range(steps):
            data_left, data_right, labels = L.load_data_relative(
                which,
                frame_matrix,
                val_idx,
                batches,
                label_mode='difference',
                data_mix=training_mode,
                label_output=label_output)
            labels_1, labels_2 = labels

            if C.ON_GPU:
                data_left = to_gpu(data_left, device=C.DEVICE)
                data_right = to_gpu(data_right, device=C.DEVICE)
                labels_1 = to_gpu(labels_1, device=C.DEVICE)
                labels_2 = to_gpu(labels_2, device=C.DEVICE)

            with cp.cuda.Device(C.DEVICE):
                if which == 'train':
                    config = True
                else:
                    config = False

                with chainer.using_config('train', config):
                    if which == 'train':
                        model.cleargrads()
                    pred_1, pred_2 = model(data_left, data_right)

                    # TODO: make better compound loss
                    # TODO: make a ratio 10* one
                    # classification_loss = softmax_cross_entropy(pred_1, labels_1)
                    classification_loss = sigmoid_cross_entropy(
                        pred_1, labels_1)  # big one
                    regression_loss = mean_squared_error(pred_2,
                                                         labels_2)  # small one

                    upper = 10
                    lower = 2

                    if classification_loss.data / regression_loss.data >= upper:
                        loss = classification_loss
                    elif classification_loss.data / regression_loss.data < lower:
                        loss = regression_loss
                    else:
                        loss = classification_loss + regression_loss

                    print(
                        'classification loss: %f, regression loss: %f, loss: %f'
                        % (float(to_cpu(classification_loss.data)),
                           float(to_cpu(regression_loss.data)),
                           float(to_cpu(loss.data))))

                    _loss_steps.append(float(loss.data))

                    if which == 'train':
                        loss.backward()
                        optimizer.update()

        # save model
        if not DEBUG:
            plots_folder = 'model_%d_experiment_%d' % (model_num,
                                                       experiment_number)
            save_location = '/scratch/users/gabras/data/omg_empathy/saving_data/models'
            model_folder = os.path.join(save_location, plots_folder)
            if not os.path.exists(model_folder):
                os.mkdir(model_folder)
            name = os.path.join(model_folder, 'epoch_%d' % e)
            chainer.serializers.save_npz(name, my_model)

    else:
        for subject in range(10):
            _loss_steps_subject = []
            previous_prediction = np.array([0.], dtype=np.float32)
            all_predictions = []

            name = 'Subject_%d_Story_1' % (subject + 1)
            path = '/scratch/users/gabras/data/omg_empathy/Validation'
            subject_folder = os.path.join(path, 'jpg_participant_662_542',
                                          name)
            all_frames = os.listdir(subject_folder)

            full_name = os.path.join(path, 'Annotations', name + '.csv')
            all_labels = np.genfromtxt(full_name,
                                       dtype=np.float32,
                                       skip_header=True)

            # num_frames = len(all_frames)

            if DEBUG:
                num_frames = 10
            else:
                num_frames = 1000

            # for f in tqdm(range(num_frames)):
            for f in range(num_frames):
                if f == 0:
                    with cp.cuda.Device(C.DEVICE):

                        with chainer.using_config('train', False):
                            prediction = np.array([0.0], dtype=np.float32)
                            labels = np.array([all_labels[f]])

                            loss = mean_squared_error(prediction, labels)
                            _loss_steps_subject.append(float(loss.data))
                else:
                    data_left, data_right = L.get_left_right_consecutively(
                        which, name, f)
                    data_left = np.expand_dims(data_left, 0)
                    data_right = np.expand_dims(data_right, 0)
                    labels = np.array([all_labels[f]])

                    if C.ON_GPU:
                        previous_prediction = to_gpu(previous_prediction,
                                                     device=C.DEVICE)
                        data_left = to_gpu(data_left, device=C.DEVICE)
                        data_right = to_gpu(data_right, device=C.DEVICE)
                        labels = to_gpu(labels, device=C.DEVICE)

                    with cp.cuda.Device(C.DEVICE):

                        with chainer.using_config('train', False):
                            pred_1, pred_2 = model(data_left, data_right)

                            pred_1 = chainer.functions.sigmoid(pred_1)

                            pred_1 = U.threshold_all(to_cpu(pred_1.data))

                            prediction = to_gpu(pred_1,
                                                device=C.DEVICE) * pred_2

                            prediction = previous_prediction + prediction

                            loss = mean_squared_error(prediction.data[0],
                                                      labels)
                            _loss_steps_subject.append(float(loss.data))

                            prediction = float(prediction.data)

                previous_prediction = to_cpu(previous_prediction)

                previous_prediction[0] = float(prediction)
                all_predictions.append(prediction)

            print('loss %s: %f' % (name, float(np.mean(_loss_steps_subject))))
            _loss_steps.append(np.mean(_loss_steps_subject))

            # save graph
            if not DEBUG:
                p = '/scratch/users/gabras/data/omg_empathy/saving_data/logs/val/epochs'
                plots_folder = 'model_%d_experiment_%d' % (model_num,
                                                           experiment_number)
                plot_path = os.path.join(p, plots_folder)
                if not os.path.exists(plot_path):
                    os.mkdir(plot_path)

                fig = plt.figure()
                x = range(num_frames)
                plt.plot(x, all_labels[:num_frames], 'g')
                plt.plot(x, all_predictions, 'b')
                plt.savefig(
                    os.path.join(plot_path,
                                 '%s_epoch_%d_.png' % (name, epoch)))
                del fig

    return _loss_steps
def run(which, model, optimizer, epoch, training_mode='close', validation_mode='sequential', model_num=None, experiment_number=None):
    _loss_steps = []
    assert (which in ['train', 'test', 'val'])
    assert validation_mode in ['sequential', 'random']
    assert training_mode in ['far', 'close', 'both']  # far = frames are >1 apart, close = frames are 1 apart

    val_idx = None
    steps = None
    if which == 'train':
        val_idx = valid_story_idx_all[0]
        steps = train_total_steps
    elif which == 'val':
        val_idx = valid_story_idx_all[1]
        steps = val_total_steps
    elif which == 'test':
        raise NotImplemented

    # print('%s, steps: %d' % (which, steps))

    if not (which == 'val' and validation_mode == 'sequential'):
        for s in tqdm(range(steps)):
            data_left, data_right, labels = L.load_data_relative(which, frame_matrix, val_idx, batches,
                                                                 label_mode='difference', data_mix=training_mode, step=s)

            if C.ON_GPU:
                data_left = to_gpu(data_left, device=C.DEVICE)
                data_right = to_gpu(data_right, device=C.DEVICE)
                labels = to_gpu(labels, device=C.DEVICE)

            with cp.cuda.Device(C.DEVICE):
                if which == 'train':
                    config = True
                else:
                    config = False

                with chainer.using_config('train', config):
                    if which == 'train':
                        model.cleargrads()
                    prediction = model(data_left, data_right)

                    loss = mean_squared_error(prediction, labels)
                    _loss_steps.append(float(loss.data))

                    if which == 'train':
                        loss.backward()
                        optimizer.update()

        # save model
        # plots_folder = 'model_%d_experiment_%d' % (model_num, experiment_number)
        # save_location = '/scratch/users/gabras/data/omg_empathy/saving_data/models'
        # model_folder = os.path.join(save_location, plots_folder)
        # if not os.path.exists(model_folder):
        #     os.mkdir(model_folder)
        # name = os.path.join(model_folder, 'epoch_%d' % e)
        # chainer.serializers.save_npz(name, my_model)

    else:
        for subject in range(10):
            _loss_steps_subject = []
            previous_prediction = np.array([0.], dtype=np.float32)
            all_predictions = []

            name = 'Subject_%d_Story_1' % (subject + 1)
            path = '/scratch/users/gabras/data/omg_empathy/Validation'
            subject_folder = os.path.join(path, 'jpg_participant_662_542', name)
            all_frames = os.listdir(subject_folder)

            full_name = os.path.join(path, 'Annotations', name + '.csv')
            all_labels = np.genfromtxt(full_name, dtype=np.float32, skip_header=True)

            # num_frames = len(all_frames)
            num_frames = 1000

            for f in tqdm(range(num_frames)):
                if f == 0:
                    with cp.cuda.Device(C.DEVICE):

                        with chainer.using_config('train', False):
                            prediction = np.array([0.0], dtype=np.float32)  # baseline
                            labels = np.array([all_labels[f]])

                            loss = mean_squared_error(prediction, labels)
                            _loss_steps_subject.append(float(loss.data))
                else:
                    data_left, data_right = L.get_left_right_consecutively(which, name, f)
                    data_left = np.expand_dims(data_left, 0)
                    data_right = np.expand_dims(data_right, 0)
                    labels = np.array([all_labels[f]])

                    if C.ON_GPU:
                        previous_prediction = to_gpu(previous_prediction, device=C.DEVICE)
                        data_left = to_gpu(data_left, device=C.DEVICE)
                        data_right = to_gpu(data_right, device=C.DEVICE)
                        labels = to_gpu(labels, device=C.DEVICE)

                    with cp.cuda.Device(C.DEVICE):

                        with chainer.using_config('train', False):
                            prediction = model(data_left, data_right)

                            prediction = previous_prediction + prediction

                            loss = mean_squared_error(prediction.data[0], labels)
                            _loss_steps_subject.append(float(loss.data))

                            prediction = float(prediction.data)

                previous_prediction = to_cpu(previous_prediction)

                previous_prediction[0] = float(prediction)
                all_predictions.append(prediction)

            print('loss %s: %f' % (name, float(np.mean(_loss_steps_subject))))
            _loss_steps.append(np.mean(_loss_steps_subject))

            # save graph
            # p = '/scratch/users/gabras/data/omg_empathy/saving_data/logs/val/epochs'
            # plots_folder = 'model_%d_experiment_%d' % (model_num, experiment_number)
            # plot_path = os.path.join(p, plots_folder)
            # if not os.path.exists(plot_path):
            #     os.mkdir(plot_path)
            #
            # fig = plt.figure()
            # x = range(num_frames)
            # plt.plot(x, all_labels[:num_frames], 'g')
            # plt.plot(x, all_predictions, 'b')
            # plt.savefig(os.path.join(plot_path, '%s_epoch_%d_.png' % (name, epoch)))

    return _loss_steps
示例#3
0
def make_pred_vs_y_plot(experiment_number,
                        epoch,
                        which='val',
                        time_gap=1,
                        model_number=1,
                        label_type='discrete'):
    use_ccc = True
    use_pearson = True
    _loss_steps = []
    _all_ccc = []
    _all_pearson = []

    if model_number == 1:
        model = Siamese()
    elif model_number == 3:
        model = Triplet()
    elif model_number == 4:
        model = TernaryClassifier()

    models_path = '/scratch/users/gabras/data/omg_empathy/saving_data/models'
    p = os.path.join(
        models_path,
        'model_%d_experiment_%d' % (model_number, experiment_number),
        'epoch_%d' % epoch)
    chainer.serializers.load_npz(p, model)
    if C.ON_GPU:
        model = model.to_gpu(device=C.DEVICE)

    for subject in range(10):
        _loss_steps_subject = []
        previous_prediction = np.array([0.], dtype=np.float32)
        all_predictions = []

        name = 'Subject_%d_Story_1' % (subject + 1)
        path = '/scratch/users/gabras/data/omg_empathy/Validation'
        subject_folder = os.path.join(path, 'jpg_participant_662_542', name)
        all_frames = os.listdir(subject_folder)

        if label_type == 'discrete':
            full_name = os.path.join(path, 'Annotations', name + '.csv')
        elif label_type == 'smooth':
            full_name = os.path.join(path, C.SMOOTH_ANNOTATIONS_PATH,
                                     name + '.csv')
        all_labels = np.genfromtxt(full_name,
                                   dtype=np.float32,
                                   skip_header=True)

        # num_frames = len(all_frames)
        num_frames = 50
        # num_frames = 10

        if time_gap > 1:
            _b = C.OMG_EMPATHY_FRAME_RATE
            _e = _b + num_frames
        else:
            _b = 0
            _e = num_frames

        # for f in tqdm(range(_b, _e)):
        for f in range(_b, _e):
            if f == 0:
                with cp.cuda.Device(C.DEVICE):

                    with chainer.using_config('train', False):
                        prediction = np.array([0.0],
                                              dtype=np.float32)  # baseline
                        labels = np.array([all_labels[f]])

                        loss = mean_squared_error(prediction, labels)
                        _loss_steps_subject.append(float(loss.data))
            else:
                data_left, data_right = L.get_left_right_consecutively(
                    which, name, f, time_gap=time_gap)
                data_left = np.expand_dims(data_left, 0)
                data_right = np.expand_dims(data_right, 0)
                labels = np.array([all_labels[f]])

                if C.ON_GPU:
                    previous_prediction = to_gpu(previous_prediction,
                                                 device=C.DEVICE)
                    data_left = to_gpu(data_left, device=C.DEVICE)
                    data_right = to_gpu(data_right, device=C.DEVICE)
                    labels = to_gpu(labels, device=C.DEVICE)

                with cp.cuda.Device(C.DEVICE):

                    with chainer.using_config('train', False):
                        if model_number == 3:
                            pred_1, pred_2 = model(data_left, data_right)

                            pred_1 = chainer.functions.sigmoid(pred_1)

                            pred_1 = U.threshold_all(to_cpu(pred_1.data))

                            prediction = to_gpu(pred_1,
                                                device=C.DEVICE) * pred_2

                            prediction = previous_prediction + prediction

                            loss = mean_squared_error(prediction.data[0],
                                                      labels)
                            _loss_steps_subject.append(float(loss.data))

                            prediction = float(prediction.data)
                        else:
                            prediction = model(data_left, data_right)

                            prediction = previous_prediction + prediction

                            loss = mean_squared_error(prediction.data[0],
                                                      labels)
                            _loss_steps_subject.append(float(loss.data))

                            prediction = float(prediction.data)

            previous_prediction = to_cpu(previous_prediction)

            previous_prediction[0] = float(prediction)
            all_predictions.append(prediction)

        # if use_ccc:
        #     ccc_subject = calculate_ccc(all_predictions, all_labels[_b:_e])
        #     _all_ccc.append(ccc_subject)
        #     print('%s, loss: %f, ccc: %f' % (name, float(np.mean(_loss_steps_subject)), ccc_subject))
        #
        # if use_pearson:
        #     pearson_subject, p_vals = calculate_pearson(all_predictions, all_labels[_b:_e])
        #     _all_pearson.append(pearson_subject)
        #     print('%s, loss: %f, pearson: %f' % (name, float(np.mean(_loss_steps_subject)), pearson_subject))
        #
        # _loss_steps.append(np.mean(_loss_steps_subject))

        # save graph
        save_graph = True
        if save_graph:
            labs = all_labels[_b:_e]
            diff_arr = labs - all_predictions

            diff_matrix = np.zeros((num_frames, num_frames))

            for i in range(num_frames):
                diff_matrix[i, i] = diff_arr[i]

            p = '/scratch/users/gabras/data/omg_empathy/saving_data/logs/val/pred_vs_y'
            plots_folder = 'model_%d_experiment_%d' % (model_number,
                                                       experiment_number)
            plot_path = os.path.join(p, plots_folder)

            if not os.path.exists(plot_path):
                os.mkdir(plot_path)

            fig = plt.figure()

            # assert len(labs) == len(all_predictions)
            # plt.scatter(labs, all_predictions)

            fig, ax = plt.subplots()
            im = ax.imshow(diff_matrix)

            # We want to show all ticks...
            ax.set_xticks(np.arange(len(diff_arr)))
            ax.set_yticks(np.arange(len(diff_arr)))
            # ... and label them with the respective list entries
            # ax.set_xticklabels(farmers)
            # ax.set_yticklabels(vegetables)

            # Rotate the tick labels and set their alignment.
            # plt.setp(ax.get_xticklabels(), rotation=45, ha="right",
            #          rotation_mode="anchor")

            # Loop over data dimensions and create text annotations.
            # for i in range(len(diff_arr)):
            #     for j in range(len(diff_arr)):
            #         text = ax.text(j, i, str(diff_matrix[i, j])[0:7], ha="center", va="center", color="w")

            ax.set_title('difference: labels - predictions')
            fig.tight_layout()

            plt.savefig(
                os.path.join(plot_path, '%s_epoch_%d_.png' % (name, epoch)))
示例#4
0
def predict_value_model_0(experiment_number,
                          epoch,
                          which='val',
                          time_gap=1,
                          model_number=0):
    use_ccc = True
    use_pearson = True
    _loss_steps = []
    _all_ccc = []
    _all_pearson = []

    model = Siamese_pilot()

    models_path = '/scratch/users/gabras/data/omg_empathy/saving_data/models'
    p = os.path.join(
        models_path,
        'model_%d_experiment_%d' % (model_number, experiment_number),
        'epoch_%d' % epoch)
    chainer.serializers.load_npz(p, model)
    if C.ON_GPU:
        model = model.to_gpu(device=C.DEVICE)

    for subject in range(10):
        _loss_steps_subject = []
        all_predictions = []

        name = 'Subject_%d_Story_1' % (subject + 1)
        path = '/scratch/users/gabras/data/omg_empathy/Validation'
        subject_folder = os.path.join(path, 'jpg_participant_662_542', name)
        all_frames = os.listdir(subject_folder)

        full_name = os.path.join(path, 'Annotations', name + '.csv')
        all_labels = np.genfromtxt(full_name,
                                   dtype=np.float32,
                                   skip_header=True)

        # num_frames = len(all_frames)
        num_frames = 1000
        _b = 0
        _e = num_frames

        for f in range(0, num_frames):
            if f == 0:
                with cp.cuda.Device(C.DEVICE):

                    with chainer.using_config('train', False):
                        prediction = np.array([0.0],
                                              dtype=np.float32)  # baseline
                        labels = np.array([all_labels[f]])

                        loss = mean_squared_error(prediction, labels)
                        _loss_steps_subject.append(float(loss.data))

                all_predictions.append(0.0)
            else:
                data_left, data_right = L.get_left_right_consecutively(
                    which, name, f, time_gap=time_gap)
                data_left = np.expand_dims(data_left, 0)
                data_right = np.expand_dims(data_right, 0)
                labels = np.array([all_labels[f]])

                if C.ON_GPU:
                    data_left = to_gpu(data_left, device=C.DEVICE)
                    data_right = to_gpu(data_right, device=C.DEVICE)
                    labels = to_gpu(labels, device=C.DEVICE)

                with cp.cuda.Device(C.DEVICE):

                    with chainer.using_config('train', False):
                        prediction = model(data_left, data_right)
                        loss = mean_squared_error(prediction.data[0], labels)

                _loss_steps_subject.append(float(to_cpu(loss.data)))

                all_predictions.append(float(to_cpu(prediction.data)))

        if use_ccc:
            ccc_subject = calculate_ccc(all_predictions, all_labels[_b:_e])
            _all_ccc.append(ccc_subject)
            print('%s, loss: %f, ccc: %f' %
                  (name, float(np.mean(_loss_steps_subject)), ccc_subject))

        if use_pearson:
            pearson_subject, p_vals = calculate_pearson(
                all_predictions, all_labels[_b:_e])
            _all_pearson.append(pearson_subject)
            print('%s, loss: %f, pearson: %f' %
                  (name, float(np.mean(_loss_steps_subject)), pearson_subject))
        _loss_steps.append(np.mean(_loss_steps_subject))

    print(
        'model_%d_experiment_%d_epoch_%d, val_loss: %f, CCC: %f, pearson: %f' %
        (model_number, experiment_number, epoch, float(np.mean(_loss_steps)),
         float(np.mean(_all_ccc)), float(np.mean(_all_pearson))))
示例#5
0
def predict_valence_sequential_relative(which,
                                        experiment_number,
                                        epoch,
                                        time_gap=1,
                                        model_number=1,
                                        label_type='discrete'):
    use_ccc = True
    use_pearson = True
    _loss_steps = []
    _all_ccc = []
    _all_pearson = []
    _all_mad = []

    if model_number == 1:
        model = Siamese()
    elif model_number == 0:
        model = Siamese_pilot()
    elif model_number == 3:
        model = Triplet()
    elif model_number == 4:
        model = TernaryClassifier()
    models_path = '/scratch/users/gabras/data/omg_empathy/saving_data/models'
    p = os.path.join(
        models_path,
        'model_%d_experiment_%d' % (model_number, experiment_number),
        'epoch_%d' % epoch)
    chainer.serializers.load_npz(p, model)
    if C.ON_GPU:
        model = model.to_gpu(device=C.DEVICE)

    for subject in range(10):
        mean_directional_accuracy = 0
        _loss_steps_subject = []
        if model_number != 0:
            previous_prediction = np.array([0.], dtype=np.float32)
        all_predictions = []

        name = 'Subject_%d_Story_1' % (subject + 1)
        path = '/scratch/users/gabras/data/omg_empathy/Validation'
        subject_folder = os.path.join(path, 'jpg_participant_662_542', name)
        all_frames = os.listdir(subject_folder)

        if label_type == 'discrete':
            full_name = os.path.join(path, 'Annotations', name + '.csv')
        elif label_type == 'smooth':
            full_name = os.path.join(path, C.SMOOTH_ANNOTATIONS_PATH,
                                     name + '.csv')
        all_labels = np.genfromtxt(full_name,
                                   dtype=np.float32,
                                   skip_header=True)

        if model_number == 4:
            all_labels = np.genfromtxt(full_name,
                                       dtype=np.float32,
                                       skip_header=True)
            all_labels = U.to_ternary(all_labels, time_gap=time_gap)
            all_labels = np.array(all_labels)

        # num_frames = len(all_frames)
        num_frames = 1000
        # num_frames = 50

        if time_gap > 0:
            _b = C.OMG_EMPATHY_FRAME_RATE
            _e = _b + num_frames
        else:
            _b = 0
            _e = num_frames

        # for f in tqdm(range(_b, _e)):
        for f in range(_b, _e):
            # print(f)
            if f == 0:
                with cp.cuda.Device(C.DEVICE):

                    with chainer.using_config('train', False):
                        prediction = np.array([0.0],
                                              dtype=np.float32)  # baseline
                        labels = np.array([all_labels[f]])

                        loss = mean_squared_error(prediction, labels)
                        _loss_steps_subject.append(float(loss.data))
            else:
                data_left, data_right = L.get_left_right_consecutively(
                    which, name, f, time_gap=time_gap)
                data_left = np.expand_dims(data_left, 0)
                data_right = np.expand_dims(data_right, 0)
                labels = np.array([all_labels[f]])

                if C.ON_GPU:
                    if model_number != 0:
                        previous_prediction = to_gpu(previous_prediction,
                                                     device=C.DEVICE)
                    data_left = to_gpu(data_left, device=C.DEVICE)
                    data_right = to_gpu(data_right, device=C.DEVICE)
                    labels = to_gpu(labels, device=C.DEVICE)

                with cp.cuda.Device(C.DEVICE):

                    with chainer.using_config('train', False):
                        if model_number == 3:
                            pred_1, pred_2 = model(data_left, data_right)

                            pred_1 = chainer.functions.sigmoid(pred_1)

                            pred_1 = U.threshold_all(to_cpu(pred_1.data))

                            prediction = to_gpu(pred_1,
                                                device=C.DEVICE) * pred_2

                            prediction = previous_prediction + prediction

                            loss = mean_squared_error(prediction.data[0],
                                                      labels)
                            _loss_steps_subject.append(float(loss.data))

                            prediction = float(prediction.data)
                        else:
                            prediction = model(data_left, data_right)

                            # del data_left
                            # del data_right

                            if model_number == 4:
                                prediction = chainer.functions.softmax(
                                    prediction)
                                prediction = U.threshold_ternary(
                                    to_cpu(prediction.data))
                                prediction = np.array(prediction)
                                prediction = to_gpu(prediction,
                                                    device=C.DEVICE)

                                all_predictions.append(int(prediction))

                                if prediction == labels:
                                    mean_directional_accuracy += 1

                            elif model_number != 0:

                                prediction = previous_prediction + prediction

                                loss = mean_squared_error(
                                    prediction.data[0], labels)
                                _loss_steps_subject.append(float(loss.data))

                                prediction = float(prediction.data)

            if model_number == 4:
                pass
                # mad_subject = mean_directional_accuracy / num_frames
                # _all_mad.append(mad_subject)
            else:
                if model_number != 0:
                    previous_prediction = to_cpu(previous_prediction)
                    previous_prediction[0] = float(prediction)

                all_predictions.append(prediction)

        if model_number == 4:
            ccc_subject = calculate_ccc(all_predictions, all_labels[_b:_e])
            _all_ccc.append(ccc_subject)
            pearson_subject, p_vals = calculate_pearson(
                all_predictions, all_labels[_b:_e])
            _all_pearson.append(pearson_subject)

            mad_subject = mean_directional_accuracy / num_frames
            _all_mad.append(mad_subject)
            print('mean_directional_accuracy %s: %f, ccc: %f, pearson: %f' %
                  (name, mad_subject, ccc_subject, pearson_subject))

        else:
            if use_ccc:
                ccc_subject = calculate_ccc(all_predictions, all_labels[_b:_e])
                _all_ccc.append(ccc_subject)
                print('%s, loss: %f, ccc: %f' %
                      (name, float(np.mean(_loss_steps_subject)), ccc_subject))

            if use_pearson:
                pearson_subject, p_vals = calculate_pearson(
                    all_predictions, all_labels[_b:_e])
                _all_pearson.append(pearson_subject)
                print('%s, loss: %f, pearson: %f' %
                      (name, float(
                          np.mean(_loss_steps_subject)), pearson_subject))
            _loss_steps.append(np.mean(_loss_steps_subject))

        # save graph
        save_graph = False
        if save_graph:
            p = '/scratch/users/gabras/data/omg_empathy/saving_data/logs/val/epochs'
            plots_folder = 'model_%d_experiment_%d_VO' % (model_number,
                                                          experiment_number)
            plot_path = os.path.join(p, plots_folder)
            if not os.path.exists(plot_path):
                os.mkdir(plot_path)

            fig = plt.figure()
            x = range(num_frames)
            plt.plot(x, all_labels[:num_frames], 'g')
            plt.plot(x, all_predictions, 'b')
            plt.savefig(
                os.path.join(plot_path, '%s_epoch_%d_.png' % (name, epoch)))

    if model_number == 4:
        print(
            'model_%d_experiment_%d_epoch_%d, val_mad: %f, ccc: %f, pearson: %f'
            %
            (model_number, experiment_number, epoch, float(np.mean(_all_mad)),
             float(np.mean(_all_ccc)), float(np.mean(_all_pearson))))
    else:
        if use_ccc:
            print('model_%d_experiment_%d_epoch_%d, val_loss: %f, CCC: %f' %
                  (model_number, experiment_number, epoch,
                   float(np.mean(_loss_steps)), float(np.mean(_all_ccc))))
        if use_pearson:
            print(
                'model_%d_experiment_%d_epoch_%d, val_loss: %f, pearson: %f' %
                (model_number, experiment_number, epoch,
                 float(np.mean(_loss_steps)), float(np.mean(_all_pearson))))