Skip to content
This repository was archived by the owner on Oct 9, 2023. It is now read-only.

TypeError: string indices must be integers with model.predict #842

Closed
carmocca opened this issue Oct 8, 2021 · 3 comments
Closed

TypeError: string indices must be integers with model.predict #842

carmocca opened this issue Oct 8, 2021 · 3 comments
Labels
bug / fix Something isn't working help wanted Extra attention is needed

Comments

@carmocca
Copy link
Contributor

carmocca commented Oct 8, 2021

🐛 Bug

Traceback (most recent call last):
  File "/Users/carlosmocholi/git/project-flusso/script/kk2.py", line 37, in <module>
    predictions = model.predict(
  File "/Users/carlosmocholi/git/lightning-flash/flash/core/model.py", line 257, in wrapper
    result = func(self, *args, **kwargs)
  File "/Users/carlosmocholi/git/lightning-flash/flash/core/model.py", line 464, in predict
    predictions = self.predict_step(x, 0)  # batch_idx is always 0 when running with `model.predict`
  File "/Users/carlosmocholi/git/lightning-flash/flash/core/adapter.py", line 99, in predict_step
    return self.adapter.predict_step(batch, batch_idx, dataloader_idx=dataloader_idx)
  File "/Users/carlosmocholi/git/lightning-flash/flash/image/classification/adapters.py", line 512, in predict_step
    self._task.task, (batch[DefaultDataKeys.INPUT]), batch_idx, dataloader_idx=dataloader_idx
TypeError: string indices must be integers

Code sample

# Copyright The PyTorch Lightning team.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import torch

import flash
from flash.core.data.utils import download_data
from flash.image import ImageClassificationData, ImageClassifier

# 1. Create the DataModule
# Data set from the paper “Movie Genre Classification based on Poster Images with Deep Neural Networks”.
# More info here: https://www.cs.ccu.edu.tw/~wtchu/projects/MoviePoster/
download_data("https://pl-flash-data.s3.amazonaws.com/movie_posters.zip")

datamodule = ImageClassificationData.from_csv(
    "Id",
    ["Action", "Romance", "Crime", "Thriller", "Adventure"],
    train_file="data/movie_posters/train/metadata.csv",
    val_file="data/movie_posters/val/metadata.csv",
    image_size=(128, 128),
)

# 2. Build the task
model = ImageClassifier(backbone="resnet18", num_classes=datamodule.num_classes, multi_label=True)

# 4. Predict the genre of a few movies!
predictions = model.predict(
    [
        "data/movie_posters/predict/tt0085318.jpg",
        "data/movie_posters/predict/tt0089461.jpg",
        "data/movie_posters/predict/tt0097179.jpg",
    ]
)
print(predictions)

Adapted from https://github.com/PyTorchLightning/lightning-flash/blob/master/flash_examples/image_classification_multi_label.py

Expected behavior

Runs

Environment

master

@carmocca carmocca added bug / fix Something isn't working help wanted Extra attention is needed labels Oct 8, 2021
@ethanwharris
Copy link
Collaborator

This is a quirk of how the data pipeline is set-up. Until a task is trained / finetuned it doesn't have an attached preprocess (which is then responsible for loading images from files in this case). I think we can set a default preprocess for the task, but ideally we would refactor this in a way that avoids this strange dependency or at least give a warning saying you need to fit or finetune it first.

@stale
Copy link

stale bot commented Dec 8, 2021

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the won't fix This will not be worked on label Dec 8, 2021
@ethanwharris ethanwharris removed the won't fix This will not be worked on label Dec 9, 2021
@ethanwharris
Copy link
Collaborator

This is now fixed following the Data Pipeline V2 refactor 😃 All prediction data should be loaded via a datamodule and prediction performed via a Trainer.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug / fix Something isn't working help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants