Skip to content

Instantly share code, notes, and snippets.

View markusdumke's full-sized avatar

Markus Dumke markusdumke

  • Munich, Germany
View GitHub Profile
@rstml
rstml / crop.md
Last active June 9, 2023 06:02
Keras center and random crop support for ImageDataGenerator

Keras center and random crop support for ImageDataGenerator

preprocess_crop.py script below adds center and random crop to Keras's flow_from_directory data generator.

It first resizes image preserving aspect ratio and then performs crop. Resized image size is based on crop_fraction which is hardcoded but can be changed. See crop_fraction = 0.875 line where 0.875 appears to be the most common, e.g. 224px crop from 256px image.

Note that the implementation has been done by monkey patching keras_preprocessing.image.utils.loag_img function as I couldn't find any other way to perform crop before resizing without rewriting many other classes above.

Due to these limitations, the cropping method is enumerated into the interpolation field. Methods are delimited by : where the first part is interpolation and second is crop e.g. lanczos:random. Supported crop methods are none, center, random. When no crop method is specified, none is assumed.

---
apiVersion: "v1"
kind: "Service"
metadata:
name: "nginx-hello-world"
labels:
app: "hello-world"
annotations:
load-balancer.hetzner.cloud/name: "devopslb"
load-balancer.hetzner.cloud/health-check-port: "10254"