Preprocess images and form dataset for model training