_csv.Error: iterator should return strings, not float

While trying to make an ImageDataBunch.from_df, or from_csv (gives the same error), I got this:

File “hptrain-dota-64.py”, line 64, in
fn_col=fn_col, label_col=label_col
File “/home/adrian/anaconda3/envs/fastai11/lib/python3.7/site-packages/fastai/vision/data.py”, line 295, in from_df
fnames, labels = _df_to_fns_labels(df, suffix=suffix, label_delim=sep, fn_col=fn_col, label_col=label_col)
File “/home/adrian/anaconda3/envs/fastai11/lib/python3.7/site-packages/fastai/vision/data.py”, line 256, in _df_to_fns_labels
df.iloc[:,label_col] = list(csv.reader(df.iloc[:,label_col], delimiter=label_delim))
_csv.Error: iterator should return strings, not float (did you open the file in text mode?)

How should I fix this problem? I don’t see what float the error is referring to. My CSV looks like (it’s DOTA):

image,label
P0000,plane small-vehicle large-vehicle
P0001,ship harbor
P0002,small-vehicle large-vehicle
P0005,small-vehicle large-vehicle
P0008,small-vehicle large-vehicle
P0010,small-vehicle large-vehicle
P0011,small-vehicle large-vehicle ship

etc.

It’s best to use pd.read_csv to read a csv in a dataframe, AFAIK.

I do indeed use that function. Then when I call ImageDataBunch.from_df, it gives me that error above.

fn_col = 0
label_col = 1
SUFFIX = '.png'
df = pd.read_csv(IMG_PATH+CSV_NAME, header='infer')

data = ImageDataBunch.from_df(IMG_PATH + IMG_FOLDER, df,
                           valid_pct=0.2, ds_tfms=get_transforms(),
                           suffix=SUFFIX, sep=' ', size=sz, bs=bs,
                           fn_col=fn_col, label_col=label_col
                          ).normalize(imagenet_stats)

Ok, I was saying this because your error is looking like it’s not loaded properly.
Can you share more of your code?

I edited above to show the code I was using.

Did you try that line alone?

df.iloc[:,label_col] = list(csv.reader(df.iloc[:,label_col], delimiter=label_delim))

That line is generated somewhere in fastai functions, I never wrote that explicitly.

Yes, I know. Can you run it manually after creating the dataframe since it seems the problem comes from here? With label_col=1 and label_delim=’ '.

I received the same error:


Error Traceback (most recent call last)
in
----> 1 df.iloc[:,1] = list(csv.reader(df.iloc[:,1], delimiter=’ '))

Error: iterator should return strings, not float (did you open the file in text mode?)

The dataframe itself looks fine as per the image: