def test_model_predictions_on_images():
    weight_path = Pth('Torch_Models/ResNet/resnet50_places365.pth.tar')
    category_list_path = Pth('Places365/annotation/categories_places365.txt')

    # load the class label
    category_list = utils.txt_load(category_list_path)

    # load the pre-trained weights
    model = __load_model_pretrained(weight_path)
    model = model.cuda()
    model.eval()

    image_names = ['01.jpg', '02.jpg', '03.jpg', '12.jpg']
    for image_name in image_names:
        image_path = '/home/nour/Pictures/scene_images/%s' % image_name

        img = __read_image_preprocessed(image_path)
        img = torch.from_numpy(np.array([img])).cuda()

        # forward pass
        logit = model.forward_no_activation(img)
        h_x = F.softmax(logit, 1).data.squeeze()
        probs, idx = h_x.sort(0, True)

        print('\n prediction on {}'.format(image_name, ))
        # output the prediction
        for i in range(0, 5):
            print('{:.3f} -> {}'.format(probs[i], category_list[idx[i]]))
Esempio n. 2
0
def __get_gt_activities_and_actions(root_path, video_names, activities,
                                    unit_actions):
    y_activities = []
    y_actions = []

    for video_name in video_names:
        # first, get idx of activity
        activity = utils.remove_extension(video_name).split('_')[1]
        idx_activity = np.where(activity == activities)[0][0]
        y_activity = np.zeros((N_CLASSES_ACTIVITIES, ), dtype=np.int)
        y_activity[idx_activity] = 1
        y_activities.append(y_activity)

        # then, get idx of actions
        action_txt_path = '%s/%s.txt' % (root_path,
                                         utils.remove_extension(video_name))
        lines = utils.txt_load(action_txt_path)
        idx_actions = [
            np.where(unit_actions == l.split(' ')[1])[0][0] for l in lines
        ]
        y_action = np.zeros((N_CLASSES_ACTIONS, ), dtype=np.int)
        y_action[idx_actions] = 1
        y_actions.append(y_action)

    return y_activities, y_actions
Esempio n. 3
0
def __get_action_names_from_files(pathes):
    action_names = []

    for path in pathes:
        lines = utils.txt_load(path)
        for l in lines:
            action_name = l.split(' ')[1]
            action_names.append(action_name)

    return action_names
Esempio n. 4
0
def _01_prepare_annotation_class_names():
    root_path = c.data_root_path
    annot_text_path = '%s/Charades/annotation/Charades_v1_classes.txt' % (root_path)
    annot_pkl_path = '%s/Charades/annotation/class_names.pkl' % (root_path)

    class_names = utils.txt_load(annot_text_path)

    class_ids = [int(n[1:5]) for n in class_names]
    for i_1, i_2 in zip(class_ids, np.arange(N_CLASSES)):
        assert i_1 == i_2

    class_names = [n[5:] for n in class_names]
    class_names = np.array(class_names)

    utils.pkl_dump(class_names, annot_pkl_path, is_highest=True)
    _ = 10
Esempio n. 5
0
def __get_gt_actions_timestamped(video_pathes, unit_actions):
    y_actions = []

    for video_path in video_pathes:
        # then, get idx of actions
        action_txt_path = '%s.txt' % (video_path)
        lines = utils.txt_load(action_txt_path)

        video_annot = []
        for l in lines:
            line_splits = l.split(' ')
            idx_action = np.where(unit_actions == line_splits[1])[0][0]
            frame_start, frame_end = line_splits[0].split('-')
            frame_start = int(frame_start)
            frame_end = int(frame_end)
            video_annot.append((idx_action, frame_start, frame_end))

        y_actions.append(video_annot)

    return y_actions
def test_resnet():
    # testing these networks
    mean = np.array([0.485, 0.456, 0.406], dtype=np.float32)
    std = np.array([0.229, 0.224, 0.225], dtype=np.float32)

    # load model and weights
    model_type = c.CNN_MODEL_TYPES.resnet50
    model_path = Pth('Torch_Models/ResNet/resnet50-19c8e357.pth')
    model = resnet50()

    class_names_path = Pth('ImageNet/class_names.txt')
    test_img1 = '/local/mnt/workspace/Pictures/test_img_car.jpg'
    test_img2 = '/local/mnt/workspace/Pictures/test_img_cat.jpg'
    test_img3 = '/local/mnt/workspace/Pictures/test_img_stove.jpg'
    test_img4 = '/local/mnt/workspace/Pictures/test_img_dog.jpg'

    test_imgs = [test_img1, test_img2, test_img3, test_img4]

    class_names = utils.txt_load(class_names_path)
    model_dict = torch.load(model_path)
    model.load_state_dict(model_dict, strict=True)

    # flag the model as testing only
    model = model.cuda()
    model.eval()
    model.training = False

    # print summary
    input_size = (3, 224, 224)  # (B, C, H, W)
    torchsummary.summary(model, input_size)

    for test_img in test_imgs:
        # load test imag, and pre-process it
        img = cv2.imread(test_img)
        img = img[:, :, (2, 1, 0)]
        img = image_utils.resize_crop(img)
        img = img.astype(np.float32)

        # normalize image
        img /= 255.0
        img[:, :] -= mean
        img[:, :] /= std

        print(np.min(img))
        print(np.max(img))

        print(img.shape)
        img = np.transpose(img, (2, 0, 1))
        input = np.expand_dims(img, axis=0)
        print(input.shape)

        input = torch.from_numpy(input).cuda()
        predictions = model(input)
        predictions = F.softmax(predictions)
        predictions = predictions.tolist()
        predictions = np.array(predictions)
        predictions *= 100

        print(np.min(predictions))
        print(np.max(predictions))
        predictions = predictions[0]

        idx = np.argsort(predictions)[::-1][:5]
        class_name = ' # '.join([class_names[i] for i in idx])
        prob = ' # '.join(['%.02f' % predictions[i] for i in idx])
        print('#########################')
        print('')
        print(test_img)
        print(class_name)
        print(prob)
        print('')