def preprocess(image, size=(224, 224)): """Preprocess image. :param image: PIL image :param size: resize size :return: numpy.ndarray """ # Resize image. # image = image.resize(size, resample=Image.BICUBIC) image = image.resize(size) # Convert to numpy. numpy_image = img_to_array(image) # Reshape for MobileNet numpy_image = np.expand_dims(numpy_image, axis=0) numpy_image = preprocess_input(numpy_image, backend=tf.keras.backend) return numpy_image
def preprocess_input(*args, **kwargs): return mobilenet.preprocess_input(*args, **kwargs)
def batch_preprocess_input(x_batch, network): if network == 'Xception': x_batch = xception.preprocess_input(x_batch, backend=keras.backend, layers=keras.layers, models=keras.models, utils=keras.utils) elif network == 'VGG16': x_batch = vgg16.preprocess_input(x_batch, backend=keras.backend, layers=keras.layers, models=keras.models, utils=keras.utils) elif network == 'VGG19': x_batch = vgg19.preprocess_input(x_batch, backend=keras.backend, layers=keras.layers, models=keras.models, utils=keras.utils) elif network == 'ResNet50' or network == 'ResNet101' or network == 'ResNet152': x_batch = resnet.preprocess_input(x_batch, backend=keras.backend, layers=keras.layers, models=keras.models, utils=keras.utils) elif network == 'ResNet50V2' or network == 'ResNet101V2' or network == 'ResNet152V2': x_batch = resnet_v2.preprocess_input(x_batch, backend=keras.backend, layers=keras.layers, models=keras.models, utils=keras.utils) elif network == 'ResNeXt50' or network == 'ResNeXt101': x_batch = resnext.preprocess_input(x_batch, backend=keras.backend, layers=keras.layers, models=keras.models, utils=keras.utils) elif network == 'InceptionV3': x_batch = inception_v3.preprocess_input(x_batch, backend=keras.backend, layers=keras.layers, models=keras.models, utils=keras.utils) elif network == 'InceptionResNetV2': x_batch = inception_resnet_v2.preprocess_input(x_batch, backend=keras.backend, layers=keras.layers, models=keras.models, utils=keras.utils) elif network == 'MobileNet': x_batch = mobilenet.preprocess_input(x_batch, backend=keras.backend, layers=keras.layers, models=keras.models, utils=keras.utils) elif network == 'MobileNetV2': x_batch = mobilenet_v2.preprocess_input(x_batch, backend=keras.backend, layers=keras.layers, models=keras.models, utils=keras.utils) elif network == 'DenseNet121' or network == 'DenseNet169' or network == 'DenseNet201': x_batch = densenet.preprocess_input(x_batch, backend=keras.backend, layers=keras.layers, models=keras.models, utils=keras.utils) elif network == 'NASNetMobile' or network == 'NASNetLarge': x_batch = nasnet.preprocess_input(x_batch, backend=keras.backend, layers=keras.layers, models=keras.models, utils=keras.utils) elif 'EfficientNet' in network: x_batch = efficientnet.preprocess_input(x_batch) else: return None return x_batch