Object Detection With Monk

Original article can be found here (source): Deep Learning on Medium

3. Conversion to Monk Format

If you notice, the annotations in our folder are in the .mat format. That’s no bueno for our purposes, we need to convert it into the Monk format if we want to use all the benefits of the Monk pipelines.

But wait, what is the Monk pipeline? Basically, your images and annotations should be in the following form

No worries, first we have to convert the .mat annotations into a pandas dataframe. The code to do that is as follows:

import h5pyimport numpy as npimport pandas as pddef load_data(base_path): with h5py.File(base_path + 'imgIdx.mat','r') as f: data = [["".join([chr(val) for val in f[reference][()]]) for reference in f['imgIdx']['name'][:,0]], [[val[0] for val in f[reference][()]] for reference in f['imgIdx']['label'][:,0]], [np.array(f[reference][()]) for reference in f['imgIdx']['anno'][:,0]]] df = pd.DataFrame(np.column_stack(data), columns=['filename','label','annotation']) df['annotation'] = df['annotation'].apply(lambda x:x.T.astype(int)) return df
df = load_data('/content/')

Once you run the above code, your dataframe should contain the annotations. Run df.sample(5) to see how your annotations look like.

Our work is not yet done though. We now also have to remove all images where there are no objects and then finally convert our dataframe to the Monk format given above. Run the following code to achieve that:

string_anno = []df = df[df["label"] != 0]for i in df.annotation: s = "" for j in range(len(i)): s += ' '.join([str(elem) for elem in i[j]]) s+= " Object " string_anno.append(s)
new_df = pd.DataFrame()
new_df["Id"] = df["filename"]new_df["Label"] = string_annonew_df.to_csv("annotation.csv", index=False)

And we are done. We have converted the list annotation to strings, removed the images with no objects and have saved the dataframe in the annotation.csv file. Now that our data is finally in the monk format, we are ready to move onto the model.